aboutsummaryrefslogtreecommitdiffstats
path: root/include/chat.php
blob: 6bcb003ff1c8f9423a4a6cee69851795e9fc4906 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php /** @file */


function chatroom_create($channel,$arr) {

	$ret = array('success' => false);

	$name = trim($arr['name']);
	if(! $name) {
		$ret['message'] = t('Missing room name');
		return $ret;
	}

	$r = q("select cr_id from chatroom where cr_uid = %d and cr_name = '%s' limit 1",
		intval($channel['channel_id']),
		dbesc($name)
	);
	if($r) {
		$ret['message'] = t('Duplicate room name');
		return $ret;
	}

	$created = datetime_convert();

	$x = q("insert into chatroom ( cr_aid, cr_uid, cr_name, cr_created, cr_edited, allow_cid, allow_gid, deny_cid, deny_gid )
		values ( %d, %d , '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
		intval($channel['channel_account_id']),
		intval($channel['channel_id']),
		dbesc($name),
		dbesc($created),
		dbesc($created),
		dbesc($arr['allow_cid']),
		dbesc($arr['allow_gid']),
		dbesc($arr['deny_cid']),
		dbesc($arr['deny_gid'])
	);

	if($x)
		$ret['success'] = true;

	return $ret;
}


function chatroom_destroy($channel,$arr) {

	$ret = array('success' => false);
	if(intval($arr['cr_id']))
		$sql_extra = " and cr_id = " . intval($arr['cr_id']) . " ";
	elseif(trim($arr['cr_name']))
		$sql_extra = " and cr_name = '" . protect_sprintf(dbesc(trim($arr['cr_name']))) . "' ";
	else {
		$ret['message'] = t('Invalid room specifier.');
		return $ret;
	}

	$r = q("select * from chatroom where cr_uid = %d $sql_extra limit 1",
		intval($channel['channel_id'])
	);
	if(! $r) {		
		$ret['message'] = t('Invalid room specifier.');
		return $ret;
	}

	q("delete from chatroom where cr_id = %d limit 1",
		intval($r[0]['cr_id'])
	);
	if($r[0]['cr_id']) {
		q("delete from chatpresence where cp_room = %d",
			intval($r[0]['cr_id'])
		);
	}
	$ret['success'] = true;
	return $ret;
}


function chatroom_enter($observer_xchan,$room_id,$status,$client) {

	if(! $room_id || ! $observer_xchan)
		return;

	$r = q("select * from chatroom where cr_id = %d limit 1",
		intval($room_id)
	);
	if(! $r)
		return;
	require_once('include/security.php');
	$sql_extra = permissions_sql($r[0]['cr_uid']);

	$x = q("select * from chatroom where cr_id = %d and uid = %d $sql_extra limit 1",
			intval($room_id)
			intval($r[0]['cr_uid'])
	);
	if(! $x) {
		notice( t('Permission denied.') . EOL);
		return;
	}

	$r = q("select * from chatpresence where cp_xchan = '%s' and cp_room = %d limit 1",
		dbesc($observer_xchan),
		intval($room_id)
	);
	if($r) {
		q("update chatpresence set cp_status = %d and cp_last = '%s' where cp_id = %d limit 1",
			dbesc($status),
			dbesc(datetime_convert()),
			intval($r[0]['cp_id'])
		);
		return true;
	}

	$r = q("insert into chatpresence ( cp_room, cp_xchan, cp_last, cp_status, cp_client )
		values ( %d, '%s', '%s', '%s', '%s' )",
		intval($room_id),
		dbesc($observer_xchan),
		dbesc(datetime_convert()),
		dbesc($status),
		dbesc($client)
	);
	return $r;
}


function chatroom_leave($observer_xchan,$room_id,$status) {
	if(! $room_id || ! $observer_xchan)
		return;
	$r = q("select * from chatpresence where cp_xchan = '%s' and cp_room = %d limit 1",
		dbesc($observer_xchan),
		intval($room_id)
	);
	if($r) {
		q("delete from chatpresence where cp_id = %d limit 1",
			intval($r[0]['cp_id'])
		);
	}
	return true;
}


function chatroom_list($uid) {

	$r = q("select cr_name, cr_id, count(cp_id) as cr_inroom from chatroom left join chatpresence on cr_id = cp_room where cr_uid = %d  group by cp_id order by cr_name",
		intval($uid)
	);

	return $r;
}