From 1aa3051e977dceb04fbe656763d8ef8a9803a571 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Sun, 22 May 2016 20:54:52 -0700 Subject: move chatroom stuff to zlib --- Zotlabs/Lib/Chatroom.php | 267 +++++++++++++++++++++++++++++++++++++++++++++ Zotlabs/Module/Chat.php | 22 ++-- Zotlabs/Module/Chatsvc.php | 12 +- include/chat.php | 262 -------------------------------------------- include/conversation.php | 3 +- include/widgets.php | 3 +- 6 files changed, 288 insertions(+), 281 deletions(-) create mode 100644 Zotlabs/Lib/Chatroom.php delete mode 100644 include/chat.php diff --git a/Zotlabs/Lib/Chatroom.php b/Zotlabs/Lib/Chatroom.php new file mode 100644 index 000000000..e1a9a10b3 --- /dev/null +++ b/Zotlabs/Lib/Chatroom.php @@ -0,0 +1,267 @@ + 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; + } + + $r = q("select count(cr_id) as total from chatroom where cr_aid = %d", + intval($channel['channel_account_id']) + ); + if($r) + $limit = service_class_fetch($channel['channel_id'], 'chatrooms'); + + if(($r) && ($limit !== false) && ($r[0]['total'] >= $limit)) { + $ret['message'] = upgrade_message(); + return $ret; + } + + if(! array_key_exists('expire', $arr)) + $arr['expire'] = 120; // minutes, e.g. 2 hours + + $created = datetime_convert(); + + $x = q("insert into chatroom ( cr_aid, cr_uid, cr_name, cr_created, cr_edited, cr_expire, allow_cid, allow_gid, deny_cid, deny_gid ) + values ( %d, %d , '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s' ) ", + intval($channel['channel_account_id']), + intval($channel['channel_id']), + dbesc($name), + dbesc($created), + dbesc($created), + intval($arr['expire']), + dbesc($arr['allow_cid']), + dbesc($arr['allow_gid']), + dbesc($arr['deny_cid']), + dbesc($arr['deny_gid']) + ); + + if($x) + $ret['success'] = true; + + return $ret; + } + + + static public function 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; + } + + build_sync_packet($channel['channel_id'],array('chatroom' => $r)); + + q("delete from chatroom where cr_id = %d", + intval($r[0]['cr_id']) + ); + if($r[0]['cr_id']) { + q("delete from chatpresence where cp_room = %d", + intval($r[0]['cr_id']) + ); + q("delete from chat where chat_room = %d", + intval($r[0]['cr_id']) + ); + } + + $ret['success'] = true; + return $ret; + } + + + static public function 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) { + notice( t('Room not found.') . EOL); + return false; + } + require_once('include/security.php'); + $sql_extra = permissions_sql($r[0]['cr_uid']); + + $x = q("select * from chatroom where cr_id = %d and cr_uid = %d $sql_extra limit 1", + intval($room_id), + intval($r[0]['cr_uid']) + ); + if(! $x) { + notice( t('Permission denied.') . EOL); + return false; + } + + $limit = service_class_fetch($r[0]['cr_uid'], 'chatters_inroom'); + if($limit !== false) { + $y = q("select count(*) as total from chatpresence where cp_room = %d", + intval($room_id) + ); + if($y && $y[0]['total'] > $limit) { + notice( t('Room is full') . EOL); + return false; + } + } + + if(intval($x[0]['cr_expire'])) { + $r = q("delete from chat where created < %s - INTERVAL %s and chat_room = %d", + db_utcnow(), + db_quoteinterval( intval($x[0]['cr_expire']) . ' MINUTE' ), + intval($x[0]['cr_id']) + ); + } + + $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_last = '%s' where cp_id = %d and cp_client = '%s'", + dbesc(datetime_convert()), + intval($r[0]['cp_id']), + dbesc($client) + ); + 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 leave($observer_xchan, $room_id, $client) { + if(! $room_id || ! $observer_xchan) + return; + + $r = q("select * from chatpresence where cp_xchan = '%s' and cp_room = %d and cp_client = '%s' limit 1", + dbesc($observer_xchan), + intval($room_id), + dbesc($client) + ); + if($r) { + q("delete from chatpresence where cp_id = %d", + intval($r[0]['cp_id']) + ); + } + + return true; + } + + + static public function roomlist($uid) { + require_once('include/security.php'); + $sql_extra = permissions_sql($uid); + + $r = q("select allow_cid, allow_gid, deny_cid, deny_gid, cr_name, cr_expire, cr_id, count(cp_id) as cr_inroom from chatroom left join chatpresence on cr_id = cp_room where cr_uid = %d $sql_extra group by cr_name, cr_id order by cr_name", + intval($uid) + ); + + return $r; + } + + static public function list_count($uid) { + require_once('include/security.php'); + $sql_extra = permissions_sql($uid); + + $r = q("select count(*) as total from chatroom where cr_uid = %d $sql_extra", + intval($uid) + ); + + return $r[0]['total']; + } + + /** + * create a chat message via API. + * It is the caller's responsibility to enter the room. + */ + + static public function message($uid, $room_id, $xchan, $text) { + + $ret = array('success' => false); + + if(! $text) + return; + + $sql_extra = permissions_sql($uid); + + $r = q("select * from chatroom where cr_uid = %d and cr_id = %d $sql_extra", + intval($uid), + intval($room_id) + ); + if(! $r) + return $ret; + + $arr = array( + 'chat_room' => $room_id, + 'chat_xchan' => $xchan, + 'chat_text' => $text + ); + + call_hooks('chat_message', $arr); + + $x = q("insert into chat ( chat_room, chat_xchan, created, chat_text ) + values( %d, '%s', '%s', '%s' )", + intval($room_id), + dbesc($xchan), + dbesc(datetime_convert()), + dbesc($arr['chat_text']) + ); + + $ret['success'] = true; + return $ret; + } +} diff --git a/Zotlabs/Module/Chat.php b/Zotlabs/Module/Chat.php index 9508ed3de..d14c32b7d 100644 --- a/Zotlabs/Module/Chat.php +++ b/Zotlabs/Module/Chat.php @@ -1,9 +1,11 @@ - $room)); + Zlib\Chatroom::destroy($channel,array('cr_name' => $room)); goaway(z_root() . '/chat/' . $channel['channel_address']); } @@ -67,7 +69,7 @@ class Chat extends \Zotlabs\Web\Controller { if(intval($arr['expire']) < 0) $arr['expire'] = 0; - chatroom_create($channel,$arr); + Zlib\Chatroom::create($channel,$arr); $x = q("select * from chatroom where cr_name = '%s' and cr_uid = %d limit 1", dbesc($room), @@ -87,7 +89,7 @@ class Chat extends \Zotlabs\Web\Controller { } - function get() { + function get() { if(local_channel()) $channel = \App::get_channel(); @@ -105,7 +107,7 @@ class Chat extends \Zotlabs\Web\Controller { } if((argc() > 3) && intval(argv(2)) && (argv(3) === 'leave')) { - chatroom_leave($observer,argv(2),$_SERVER['REMOTE_ADDR']); + Zlib\Chatroom::leave($observer,argv(2),$_SERVER['REMOTE_ADDR']); goaway(z_root() . '/channel/' . argv(1)); } @@ -158,7 +160,7 @@ class Chat extends \Zotlabs\Web\Controller { $room_id = intval(argv(2)); $bookmark_link = get_bookmark_link($ob); - $x = chatroom_enter($observer,$room_id,'online',$_SERVER['REMOTE_ADDR']); + $x = Zlib\Chatroom::enter($observer,$room_id,'online',$_SERVER['REMOTE_ADDR']); if(! $x) return; $x = q("select * from chatroom where cr_id = %d and cr_uid = %d $sql_extra limit 1", @@ -238,7 +240,7 @@ class Chat extends \Zotlabs\Web\Controller { )); } - $rooms = chatroom_list(\App::$profile['profile_uid']); + $rooms = Zlib\Chatroom::roomlist(\App::$profile['profile_uid']); $o .= replace_macros(get_markup_template('chatrooms.tpl'), array( '$header' => sprintf( t('%1$s\'s Chatrooms'), \App::$profile['name']), diff --git a/Zotlabs/Module/Chatsvc.php b/Zotlabs/Module/Chatsvc.php index a9bc97301..6a28a7c4d 100644 --- a/Zotlabs/Module/Chatsvc.php +++ b/Zotlabs/Module/Chatsvc.php @@ -1,14 +1,16 @@ - false); @@ -27,7 +29,7 @@ class Chatsvc extends \Zotlabs\Web\Controller { } - function post() { + function post() { $ret = array('success' => false); @@ -65,7 +67,7 @@ class Chatsvc extends \Zotlabs\Web\Controller { json_return_and_die($ret); } - function get() { + function get() { $status = strip_tags($_REQUEST['status']); $room_id = intval(\App::$data['chat']['room_id']); diff --git a/include/chat.php b/include/chat.php deleted file mode 100644 index 604402045..000000000 --- a/include/chat.php +++ /dev/null @@ -1,262 +0,0 @@ - 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; - } - - $r = q("select count(cr_id) as total from chatroom where cr_aid = %d", - intval($channel['channel_account_id']) - ); - if($r) - $limit = service_class_fetch($channel['channel_id'], 'chatrooms'); - - if(($r) && ($limit !== false) && ($r[0]['total'] >= $limit)) { - $ret['message'] = upgrade_message(); - return $ret; - } - - if(! array_key_exists('expire', $arr)) - $arr['expire'] = 120; // minutes, e.g. 2 hours - - $created = datetime_convert(); - - $x = q("insert into chatroom ( cr_aid, cr_uid, cr_name, cr_created, cr_edited, cr_expire, allow_cid, allow_gid, deny_cid, deny_gid ) - values ( %d, %d , '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s' ) ", - intval($channel['channel_account_id']), - intval($channel['channel_id']), - dbesc($name), - dbesc($created), - dbesc($created), - intval($arr['expire']), - 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; - } - - build_sync_packet($channel['channel_id'],array('chatroom' => $r)); - - q("delete from chatroom where cr_id = %d", - intval($r[0]['cr_id']) - ); - if($r[0]['cr_id']) { - q("delete from chatpresence where cp_room = %d", - intval($r[0]['cr_id']) - ); - q("delete from chat where chat_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) { - notice( t('Room not found.') . EOL); - return false; - } - require_once('include/security.php'); - $sql_extra = permissions_sql($r[0]['cr_uid']); - - $x = q("select * from chatroom where cr_id = %d and cr_uid = %d $sql_extra limit 1", - intval($room_id), - intval($r[0]['cr_uid']) - ); - if(! $x) { - notice( t('Permission denied.') . EOL); - return false; - } - - $limit = service_class_fetch($r[0]['cr_uid'], 'chatters_inroom'); - if($limit !== false) { - $y = q("select count(*) as total from chatpresence where cp_room = %d", - intval($room_id) - ); - if($y && $y[0]['total'] > $limit) { - notice( t('Room is full') . EOL); - return false; - } - } - - if(intval($x[0]['cr_expire'])) { - $r = q("delete from chat where created < %s - INTERVAL %s and chat_room = %d", - db_utcnow(), - db_quoteinterval( intval($x[0]['cr_expire']) . ' MINUTE' ), - intval($x[0]['cr_id']) - ); - } - - $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_last = '%s' where cp_id = %d and cp_client = '%s'", - dbesc(datetime_convert()), - intval($r[0]['cp_id']), - dbesc($client) - ); - 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, $client) { - if(! $room_id || ! $observer_xchan) - return; - - $r = q("select * from chatpresence where cp_xchan = '%s' and cp_room = %d and cp_client = '%s' limit 1", - dbesc($observer_xchan), - intval($room_id), - dbesc($client) - ); - if($r) { - q("delete from chatpresence where cp_id = %d", - intval($r[0]['cp_id']) - ); - } - - return true; -} - - -function chatroom_list($uid) { - require_once('include/security.php'); - $sql_extra = permissions_sql($uid); - - $r = q("select allow_cid, allow_gid, deny_cid, deny_gid, cr_name, cr_expire, cr_id, count(cp_id) as cr_inroom from chatroom left join chatpresence on cr_id = cp_room where cr_uid = %d $sql_extra group by cr_name, cr_id order by cr_name", - intval($uid) - ); - - return $r; -} - -function chatroom_list_count($uid) { - require_once('include/security.php'); - $sql_extra = permissions_sql($uid); - - $r = q("select count(*) as total from chatroom where cr_uid = %d $sql_extra", - intval($uid) - ); - - return $r[0]['total']; -} - -/** - * create a chat message via API. - * It is the caller's responsibility to enter the room. - */ - -function chat_message($uid, $room_id, $xchan, $text) { - - $ret = array('success' => false); - - if(! $text) - return; - - $sql_extra = permissions_sql($uid); - - $r = q("select * from chatroom where cr_uid = %d and cr_id = %d $sql_extra", - intval($uid), - intval($room_id) - ); - if(! $r) - return $ret; - - $arr = array( - 'chat_room' => $room_id, - 'chat_xchan' => $xchan, - 'chat_text' => $text - ); - - call_hooks('chat_message', $arr); - - $x = q("insert into chat ( chat_room, chat_xchan, created, chat_text ) - values( %d, '%s', '%s', '%s' )", - intval($room_id), - dbesc($xchan), - dbesc(datetime_convert()), - dbesc($arr['chat_text']) - ); - - $ret['success'] = true; - return $ret; -} diff --git a/include/conversation.php b/include/conversation.php index ca73a7a07..f405bc9d5 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -1658,8 +1658,7 @@ function profile_tabs($a, $is_owner = false, $nickname = null){ if ($p['chat'] && feature_enabled($uid,'ajaxchat')) { - require_once('include/chat.php'); - $has_chats = chatroom_list_count($uid); + $has_chats = Zotlabs\Lib\Chatroom::list_count($uid); if ($has_chats) { $tabs[] = array( 'label' => t('Chatrooms'), diff --git a/include/widgets.php b/include/widgets.php index 27f50e9be..e6d162ccd 100644 --- a/include/widgets.php +++ b/include/widgets.php @@ -834,8 +834,7 @@ function widget_menu_preview($arr) { function widget_chatroom_list($arr) { - require_once("include/chat.php"); - $r = chatroom_list(App::$profile['profile_uid']); + $r = Zotlabs\Lib\Chatroom::roomlist(App::$profile['profile_uid']); if($r) { return replace_macros(get_markup_template('chatroomlist.tpl'), array( -- cgit v1.2.3