/** * cid : recipient * uid : sender * msg : message to send */ public static function postMsgToChannel($cid, $uid, $body, $type = 'msg') { $mid = self::generateMid($cid); $msg = array('id' => $mid, 'sender' => $uid, 'recipient' => 'channel|' . $cid, 'type' => $type, 'body' => $body, 'timestamp' => time()); // json encode msg before storing $msg = json_encode($msg); // search users subscribed to the channel foreach (Container_channels::getChannelUsers($cid) as $subuid) { // post this message on each users subscribed on the channel // /users/:uid/pending/ if ($subuid != $uid) { // don't post message to the sender $umdir = Container_users::getDir() . '/' . $subuid . '/messages'; file_put_contents($umdir . '/' . $mid, $msg); } } return $msg; }
if (!Container_users::checkUserExists($uid)) { $res->status(400); // User is not connected return; } if (!Container_users::leaveChannel($uid, $cid)) { $res->status(404); // $res['Content-Type'] = 'application/json; charset=utf-8'; // $res->body(json_encode(Container_channels::getChannelUsers($cid, true))); return; } else { // post a leave message $msg = Container_messages::postMsgToChannel($cid, $uid, null, 'leave'); $res->status(200); $res['Content-Type'] = 'application/json; charset=utf-8'; $res->body(json_encode(Container_channels::getChannelUsers($cid, true))); return; } }); /** * Post a message on a channel */ $app->post('/channels/:cid/msg/', function ($cid) use($app, $req, $res) { // check user acces session_start(); if (!isset($_SESSION['userdata']) or !isset($_SESSION['userdata']['id'])) { $res->status(401); // Need to authenticate return; } $uid = $_SESSION['userdata']['id'];
} // first is op ? first connected user on the channel is an operator if ($GLOBALS['first_is_op'] and count(Container_channels::getChannelUsers($cid)) == 1) { $isop = Container_channels_op::addOp($cid, $uid); } if ($isop) { Container_channels_op::addOp($cid, $uid); } // post a join message // when a join message is sent, body contains user's data and the "op" flag $body = array('userdata' => Container_users::getUserData($uid), 'op' => $isop); $msg = Container_messages::postMsgToChannel($cid, $uid, $body, 'join'); $res->status(201); // User joined the channel $res['Content-Type'] = 'application/json; charset=utf-8'; $res->body(json_encode(array('users' => Container_channels::getChannelUsers($cid, true), 'op' => Container_channels_op::getOpList($cid)))); return; } }); /** * Leave a channel * or is kicked from a channel */ $app->delete('/channels/:cid/users/:uid', function ($cid, $uid) use($app, $req, $res) { // check user acces session_start(); if (!isset($_SESSION['userdata']) or !isset($_SESSION['userdata']['id'])) { $res->status(401); // Need to authenticate return; }