2016-05-23 03:54:52 +00:00
< ? php
2021-12-03 03:01:39 +00:00
2016-05-23 03:54:52 +00:00
namespace Zotlabs\Lib ;
2018-06-05 01:40:11 +00:00
use Zotlabs\Lib\Libsync ;
2022-01-25 01:26:12 +00:00
use Zotlabs\Lib\ServiceClass ;
2022-02-12 08:50:48 +00:00
use Zotlabs\Extend\Hook ;
2022-01-25 01:26:12 +00:00
2016-05-23 03:54:52 +00:00
/**
2017-09-04 22:23:42 +00:00
* @ brief A class with chatroom related static methods .
2016-05-23 03:54:52 +00:00
*/
2021-12-02 23:02:31 +00:00
class Chatroom
{
/**
* @ brief Creates a chatroom .
*
* @ param array $channel
* @ param array $arr
* @ return array An associative array containing :
* * \e boolean \b success - A boolean success status
* * \e string \b message - ( optional ) A string
*/
public static function create ( $channel , $arr )
{
$ret = array ( 'success' => false );
$name = trim ( $arr [ 'name' ]);
if ( ! $name ) {
$ret [ 'message' ] = t ( 'Missing room name' );
return $ret ;
}
2021-12-03 03:01:39 +00:00
$r = q (
" select cr_id from chatroom where cr_uid = %d and cr_name = '%s' limit 1 " ,
2021-12-02 23:02:31 +00:00
intval ( $channel [ 'channel_id' ]),
dbesc ( $name )
);
if ( $r ) {
$ret [ 'message' ] = t ( 'Duplicate room name' );
return $ret ;
}
2021-12-03 03:01:39 +00:00
$r = q (
" select count(cr_id) as total from chatroom where cr_aid = %d " ,
2021-12-02 23:02:31 +00:00
intval ( $channel [ 'channel_account_id' ])
);
2021-12-03 03:01:39 +00:00
if ( $r ) {
2022-01-25 01:26:12 +00:00
$limit = ServiceClass :: fetch ( $channel [ 'channel_id' ], 'chatrooms' );
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
if (( $r ) && ( $limit !== false ) && ( $r [ 0 ][ 'total' ] >= $limit )) {
2022-01-25 01:26:12 +00:00
$ret [ 'message' ] = ServiceClass :: upgrade_message ();
2021-12-02 23:02:31 +00:00
return $ret ;
}
2021-12-03 03:01:39 +00:00
if ( ! array_key_exists ( 'expire' , $arr )) {
2021-12-02 23:02:31 +00:00
$arr [ 'expire' ] = 120 ; // minutes, e.g. 2 hours
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$created = datetime_convert ();
2021-12-03 03:01:39 +00:00
$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 )
2016-05-23 03:54:52 +00:00
values ( % d , % d , '%s' , '%s' , '%s' , % d , '%s' , '%s' , '%s' , '%s' ) " ,
2021-12-02 23:02:31 +00:00
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' ])
);
2021-12-03 03:01:39 +00:00
if ( $x ) {
2021-12-02 23:02:31 +00:00
$ret [ 'success' ] = true ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
return $ret ;
}
public static function destroy ( $channel , $arr )
{
$ret = array ( 'success' => false );
2021-12-03 03:01:39 +00:00
if ( intval ( $arr [ 'cr_id' ])) {
2021-12-02 23:02:31 +00:00
$sql_extra = " and cr_id = " . intval ( $arr [ 'cr_id' ]) . " " ;
2021-12-03 03:01:39 +00:00
} elseif ( trim ( $arr [ 'cr_name' ])) {
2021-12-02 23:02:31 +00:00
$sql_extra = " and cr_name = ' " . protect_sprintf ( dbesc ( trim ( $arr [ 'cr_name' ]))) . " ' " ;
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
$ret [ 'message' ] = t ( 'Invalid room specifier.' );
return $ret ;
}
2021-12-03 03:01:39 +00:00
$r = q (
" select * from chatroom where cr_uid = %d $sql_extra limit 1 " ,
2021-12-02 23:02:31 +00:00
intval ( $channel [ 'channel_id' ])
);
if ( ! $r ) {
$ret [ 'message' ] = t ( 'Invalid room specifier.' );
return $ret ;
}
Libsync :: build_sync_packet ( $channel [ 'channel_id' ], array ( 'chatroom' => $r ));
2021-12-03 03:01:39 +00:00
q (
" delete from chatroom where cr_id = %d " ,
2021-12-02 23:02:31 +00:00
intval ( $r [ 0 ][ 'cr_id' ])
);
if ( $r [ 0 ][ 'cr_id' ]) {
2021-12-03 03:01:39 +00:00
q (
" delete from chatpresence where cp_room = %d " ,
2021-12-02 23:02:31 +00:00
intval ( $r [ 0 ][ 'cr_id' ])
);
2021-12-03 03:01:39 +00:00
q (
" delete from chat where chat_room = %d " ,
2021-12-02 23:02:31 +00:00
intval ( $r [ 0 ][ 'cr_id' ])
);
}
$ret [ 'success' ] = true ;
return $ret ;
}
public static function enter ( $observer_xchan , $room_id , $status , $client )
{
2021-12-03 03:01:39 +00:00
if ( ! $room_id || ! $observer_xchan ) {
2021-12-02 23:02:31 +00:00
return ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
$r = q (
" select * from chatroom where cr_id = %d limit 1 " ,
2021-12-02 23:02:31 +00:00
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' ]);
2021-12-03 03:01:39 +00:00
$x = q (
" select * from chatroom where cr_id = %d and cr_uid = %d $sql_extra limit 1 " ,
2021-12-02 23:02:31 +00:00
intval ( $room_id ),
intval ( $r [ 0 ][ 'cr_uid' ])
);
if ( ! $x ) {
notice ( t ( 'Permission denied.' ) . EOL );
return false ;
}
2022-01-25 01:26:12 +00:00
$limit = ServiceClass :: fetch ( $r [ 0 ][ 'cr_uid' ], 'chatters_inroom' );
2021-12-02 23:02:31 +00:00
if ( $limit !== false ) {
2021-12-03 03:01:39 +00:00
$y = q (
" select count(*) as total from chatpresence where cp_room = %d " ,
2021-12-02 23:02:31 +00:00
intval ( $room_id )
);
if ( $y && $y [ 0 ][ 'total' ] > $limit ) {
notice ( t ( 'Room is full' ) . EOL );
return false ;
}
}
if ( intval ( $x [ 0 ][ 'cr_expire' ])) {
2021-12-03 03:01:39 +00:00
$r = q (
" delete from chat where created < %s - INTERVAL %s and chat_room = %d " ,
2021-12-02 23:02:31 +00:00
db_utcnow (),
db_quoteinterval ( intval ( $x [ 0 ][ 'cr_expire' ]) . ' MINUTE' ),
intval ( $x [ 0 ][ 'cr_id' ])
);
}
2021-12-03 03:01:39 +00:00
$r = q (
" select * from chatpresence where cp_xchan = '%s' and cp_room = %d limit 1 " ,
2021-12-02 23:02:31 +00:00
dbesc ( $observer_xchan ),
intval ( $room_id )
);
if ( $r ) {
2021-12-03 03:01:39 +00:00
q (
" update chatpresence set cp_last = '%s' where cp_id = %d and cp_client = '%s' " ,
2021-12-02 23:02:31 +00:00
dbesc ( datetime_convert ()),
intval ( $r [ 0 ][ 'cp_id' ]),
dbesc ( $client )
);
return true ;
}
2021-12-03 03:01:39 +00:00
$r = q (
" insert into chatpresence ( cp_room, cp_xchan, cp_last, cp_status, cp_client )
2016-05-23 03:54:52 +00:00
values ( % d , '%s' , '%s' , '%s' , '%s' ) " ,
2021-12-02 23:02:31 +00:00
intval ( $room_id ),
dbesc ( $observer_xchan ),
dbesc ( datetime_convert ()),
dbesc ( $status ),
dbesc ( $client )
);
return $r ;
}
public function leave ( $observer_xchan , $room_id , $client )
{
2021-12-03 03:01:39 +00:00
if ( ! $room_id || ! $observer_xchan ) {
2021-12-02 23:02:31 +00:00
return ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
$r = q (
" select * from chatpresence where cp_xchan = '%s' and cp_room = %d and cp_client = '%s' limit 1 " ,
2021-12-02 23:02:31 +00:00
dbesc ( $observer_xchan ),
intval ( $room_id ),
dbesc ( $client )
);
if ( $r ) {
2021-12-03 03:01:39 +00:00
q (
" delete from chatpresence where cp_id = %d " ,
2021-12-02 23:02:31 +00:00
intval ( $r [ 0 ][ 'cp_id' ])
);
}
return true ;
}
public static function roomlist ( $uid )
{
require_once ( 'include/security.php' );
$sql_extra = permissions_sql ( $uid );
2021-12-03 03:01:39 +00:00
$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 " ,
2021-12-02 23:02:31 +00:00
intval ( $uid )
);
return $r ;
}
public static function list_count ( $uid )
{
require_once ( 'include/security.php' );
$sql_extra = permissions_sql ( $uid );
2021-12-03 03:01:39 +00:00
$r = q (
" select count(*) as total from chatroom where cr_uid = %d $sql_extra " ,
2021-12-02 23:02:31 +00:00
intval ( $uid )
);
return $r [ 0 ][ 'total' ];
}
/**
* @ brief Create a chat message via API .
*
* It is the caller ' s responsibility to enter the room .
*
* @ param int $uid
* @ param int $room_id
* @ param string $xchan
* @ param string $text
* @ return array
*/
public static function message ( $uid , $room_id , $xchan , $text )
{
$ret = array ( 'success' => false );
2021-12-03 03:01:39 +00:00
if ( ! $text ) {
2021-12-02 23:02:31 +00:00
return ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$sql_extra = permissions_sql ( $uid );
2021-12-03 03:01:39 +00:00
$r = q (
" select * from chatroom where cr_uid = %d and cr_id = %d $sql_extra " ,
2021-12-02 23:02:31 +00:00
intval ( $uid ),
intval ( $room_id )
);
2021-12-03 03:01:39 +00:00
if ( ! $r ) {
2021-12-02 23:02:31 +00:00
return $ret ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$arr = [
'chat_room' => $room_id ,
'chat_xchan' => $xchan ,
'chat_text' => $text
];
/**
* @ hooks chat_message
* Called to create a chat message .
* * \e int \b chat_room
* * \e string \b chat_xchan
* * \e string \b chat_text
*/
2022-02-12 08:50:48 +00:00
Hook :: call ( 'chat_message' , $arr );
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
$x = q (
" insert into chat ( chat_room, chat_xchan, created, chat_text )
2016-05-23 03:54:52 +00:00
values ( % d , '%s' , '%s' , '%s' ) " ,
2021-12-02 23:02:31 +00:00
intval ( $room_id ),
dbesc ( $xchan ),
dbesc ( datetime_convert ()),
dbesc ( str_rot47 ( base64url_encode ( $arr [ 'chat_text' ])))
);
$ret [ 'success' ] = true ;
return $ret ;
}
2016-05-23 03:54:52 +00:00
}