From 9261a170eb45f0b189afb4c1a30603d0cbb8f31c Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 29 Jan 2014 00:08:29 -0800 Subject: basic chatroom management backend --- include/chat.php | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 include/chat.php (limited to 'include/chat.php') diff --git a/include/chat.php b/include/chat.php new file mode 100644 index 000000000..4df419294 --- /dev/null +++ b/include/chat.php @@ -0,0 +1,117 @@ + 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['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) { + if(! $room_id || ! $observer) + 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 ) + values ( %d, '%s', '%s', '%s' )", + intval($room_id), + dbesc($observer_xchan), + dbesc(datetime_convert()), + dbesc($status) + ); + return $r; +} + + +function chatroom_leave($observer_xchan,$room_id,$status) { + if(! $room_id || ! $observer) + 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; +} \ No newline at end of file -- cgit v1.2.3 From 9f546757021305b6cfe924f38ca1af5fd5d3e69b Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 29 Jan 2014 01:52:23 -0800 Subject: chatroom list widget backend --- include/chat.php | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'include/chat.php') diff --git a/include/chat.php b/include/chat.php index 4df419294..cb910bd62 100644 --- a/include/chat.php +++ b/include/chat.php @@ -114,4 +114,13 @@ function chatroom_leave($observer_xchan,$room_id,$status) { ); } 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 order by cr_name group by cp_id", + intval($uid) + ); + return $r; } \ No newline at end of file -- cgit v1.2.3 From 10b51a9471bba2a1b058eee2d362d3d2189627be Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 29 Jan 2014 02:25:21 -0800 Subject: issues uncovered whilst testing the chatroom API --- include/chat.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'include/chat.php') diff --git a/include/chat.php b/include/chat.php index cb910bd62..8011fdb5b 100644 --- a/include/chat.php +++ b/include/chat.php @@ -23,8 +23,8 @@ function chatroom_create($channel,$arr) { $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['account_id']), + values ( %d, %d , '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ", + intval($channel['channel_account_id']), intval($channel['channel_id']), dbesc($name), dbesc($created), @@ -34,6 +34,7 @@ function chatroom_create($channel,$arr) { dbesc($arr['deny_cid']), dbesc($arr['deny_gid']) ); + if($x) $ret['success'] = true; @@ -74,8 +75,8 @@ function chatroom_destroy($channel,$arr) { } -function chatroom_enter($observer_xchan,$room_id,$status) { - if(! $room_id || ! $observer) +function chatroom_enter($observer_xchan,$room_id,$status,$client) { + if(! $room_id || ! $observer_xchan) return; $r = q("select * from chatpresence where cp_xchan = '%s' and cp_room = %d limit 1", dbesc($observer_xchan), @@ -90,19 +91,20 @@ function chatroom_enter($observer_xchan,$room_id,$status) { return true; } - $r = q("insert into chatpresence ( cp_room, cp_xchan, cp_last, cp_status ) - values ( %d, '%s', '%s', '%s' )", + $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($status), + dbesc($client) ); return $r; } function chatroom_leave($observer_xchan,$room_id,$status) { - if(! $room_id || ! $observer) + if(! $room_id || ! $observer_xchan) return; $r = q("select * from chatpresence where cp_xchan = '%s' and cp_room = %d limit 1", dbesc($observer_xchan), -- cgit v1.2.3 From 6a9d43bcbe167ff3a8f9bd8a2ce93d9fc298fcdf Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 29 Jan 2014 03:16:07 -0800 Subject: debug chatroom_list widget --- include/chat.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include/chat.php') diff --git a/include/chat.php b/include/chat.php index 8011fdb5b..9d90f7970 100644 --- a/include/chat.php +++ b/include/chat.php @@ -121,8 +121,9 @@ function chatroom_leave($observer_xchan,$room_id,$status) { 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 order by cr_name group by cp_id", + $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; } \ No newline at end of file -- cgit v1.2.3 From a1e7c65d51a6472cf7fe95686883f77953d7dfd7 Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 29 Jan 2014 03:39:32 -0800 Subject: chatroom permissions enforcement --- include/chat.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'include/chat.php') diff --git a/include/chat.php b/include/chat.php index 9d90f7970..6bcb003ff 100644 --- a/include/chat.php +++ b/include/chat.php @@ -76,8 +76,27 @@ function chatroom_destroy($channel,$arr) { 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) -- cgit v1.2.3