1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/hubzilla_ynh.git synced 2024-09-03 19:26:21 +02:00
hubzilla_ynh/sources/mod/chatsvc.php

163 lines
4.2 KiB
PHP
Raw Normal View History

<?php /** @file */
require_once('include/security.php');
function chatsvc_init(&$a) {
//logger('chatsvc');
$ret = array('success' => false);
2016-04-17 16:29:18 +02:00
App::$data['chat']['room_id'] = intval($_REQUEST['room_id']);
$x = q("select cr_uid from chatroom where cr_id = %d and cr_id != 0 limit 1",
2016-04-17 16:29:18 +02:00
intval(App::$data['chat']['room_id'])
);
if(! $x)
json_return_and_die($ret);
2016-04-17 16:29:18 +02:00
App::$data['chat']['uid'] = $x[0]['cr_uid'];
2016-04-17 16:29:18 +02:00
if(! perm_is_allowed(App::$data['chat']['uid'],get_observer_hash(),'chat')) {
json_return_and_die($ret);
}
}
function chatsvc_post(&$a) {
$ret = array('success' => false);
2016-04-17 16:29:18 +02:00
$room_id = App::$data['chat']['room_id'];
$text = escape_tags($_REQUEST['chat_text']);
if(! $text)
return;
2016-04-17 16:29:18 +02:00
$sql_extra = permissions_sql(App::$data['chat']['uid']);
$r = q("select * from chatroom where cr_uid = %d and cr_id = %d $sql_extra",
2016-04-17 16:29:18 +02:00
intval(App::$data['chat']['uid']),
intval(App::$data['chat']['room_id'])
);
if(! $r)
json_return_and_die($ret);
$arr = array(
2016-04-17 16:29:18 +02:00
'chat_room' => App::$data['chat']['room_id'],
'chat_xchan' => get_observer_hash(),
'chat_text' => $text
);
call_hooks('chat_post',$arr);
$x = q("insert into chat ( chat_room, chat_xchan, created, chat_text )
values( %d, '%s', '%s', '%s' )",
2016-04-17 16:29:18 +02:00
intval(App::$data['chat']['room_id']),
dbesc(get_observer_hash()),
dbesc(datetime_convert()),
dbesc($arr['chat_text'])
);
$ret['success'] = true;
json_return_and_die($ret);
}
function chatsvc_content(&$a) {
$status = strip_tags($_REQUEST['status']);
2016-04-17 16:29:18 +02:00
$room_id = intval(App::$data['chat']['room_id']);
$stopped = ((x($_REQUEST,'stopped') && intval($_REQUEST['stopped'])) ? true : false);
if($status && $room_id) {
$x = q("select channel_address from channel where channel_id = %d limit 1",
2016-04-17 16:29:18 +02:00
intval(App::$data['chat']['uid'])
);
$r = q("update chatpresence set cp_status = '%s', cp_last = '%s' where cp_room = %d and cp_xchan = '%s' and cp_client = '%s'",
dbesc($status),
dbesc(datetime_convert()),
intval($room_id),
dbesc(get_observer_hash()),
dbesc($_SERVER['REMOTE_ADDR'])
);
goaway(z_root() . '/chat/' . $x[0]['channel_address'] . '/' . $room_id);
}
if(! $stopped) {
$lastseen = intval($_REQUEST['last']);
$ret = array('success' => false);
2016-04-17 16:29:18 +02:00
$sql_extra = permissions_sql(App::$data['chat']['uid']);
$r = q("select * from chatroom where cr_uid = %d and cr_id = %d $sql_extra",
2016-04-17 16:29:18 +02:00
intval(App::$data['chat']['uid']),
intval(App::$data['chat']['room_id'])
);
if(! $r)
json_return_and_die($ret);
$inroom = array();
$r = q("select * from chatpresence left join xchan on xchan_hash = cp_xchan where cp_room = %d order by xchan_name",
2016-04-17 16:29:18 +02:00
intval(App::$data['chat']['room_id'])
);
if($r) {
foreach($r as $rr) {
switch($rr['cp_status']) {
case 'away':
$status = t('Away');
2016-04-17 16:29:18 +02:00
$status_class = 'away';
break;
case 'online':
default:
$status = t('Online');
2016-04-17 16:29:18 +02:00
$status_class = 'online';
break;
}
2016-04-17 16:29:18 +02:00
$inroom[] = array('img' => zid($rr['xchan_photo_m']), 'img_type' => $rr['xchan_photo_mimetype'],'name' => $rr['xchan_name'], 'status' => $status, 'status_class' => $status_class);
}
}
$chats = array();
$r = q("select * from chat left join xchan on chat_xchan = xchan_hash where chat_room = %d and chat_id > %d order by created",
2016-04-17 16:29:18 +02:00
intval(App::$data['chat']['room_id']),
intval($lastseen)
);
if($r) {
foreach($r as $rr) {
$chats[] = array(
'id' => $rr['chat_id'],
'img' => zid($rr['xchan_photo_m']),
'img_type' => $rr['xchan_photo_mimetype'],
'name' => $rr['xchan_name'],
'isotime' => datetime_convert('UTC', date_default_timezone_get(), $rr['created'], 'c'),
'localtime' => datetime_convert('UTC', date_default_timezone_get(), $rr['created'], 'r'),
2016-04-17 16:29:18 +02:00
'text' => smilies(bbcode($rr['chat_text'])),
'self' => ((get_observer_hash() == $rr['chat_xchan']) ? 'self' : '')
);
}
}
}
$r = q("update chatpresence set cp_last = '%s' where cp_room = %d and cp_xchan = '%s' and cp_client = '%s'",
dbesc(datetime_convert()),
2016-04-17 16:29:18 +02:00
intval(App::$data['chat']['room_id']),
dbesc(get_observer_hash()),
dbesc($_SERVER['REMOTE_ADDR'])
);
$ret['success'] = true;
if(! $stopped) {
$ret['inroom'] = $inroom;
$ret['chats'] = $chats;
}
json_return_and_die($ret);
}