public static function getChannelUsers($cid, $withudata = false) { $cdir = self::getChannelsDir(); $cupath = $cdir . '/' . $cid . '/users/'; $subscribers = array(); foreach (scandir($cupath) as $uid) { if ($uid === '.' || $uid === '..') { continue; } if ($withudata) { $subscribers[$uid] = Container_users::getUserData($uid); } else { $subscribers[] = $uid; } } return $subscribers; }
/** * 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; }
$res['Content-Type'] = 'application/json; charset=utf-8'; $res->body(Container_users::getUserMsgs($uid, true)); }); /** * Set the close flag (when a user reload or close his window) */ $app->put('/users/:uid/closed', function ($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; } if ($uid !== $_SESSION['userdata']['id']) { $res->status(403); // Forbidden return; } if (!Container_users::checkUserExists($uid)) { $res->status(404); $res['Content-Type'] = 'application/json; charset=utf-8'; $res->body('{ "error": "user data does not exist" }'); return; } // set the close flag Container_users::setCloseFlag($uid); $res->status(200); $res['Content-Type'] = 'application/json; charset=utf-8'; $res->body('1'); });
} }); /** * Remove :name64 from the :cid channel banished list */ $app->delete('/channels/:cid/ban/:name64', function ($cid, $name64) 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; } $online_uid = $_SESSION['userdata']['id']; // check this user is online if (!Container_users::checkUserExists($online_uid)) { $res->status(400); // User is not connected return; } // check this user has joined the channel if (!Container_channels::checkChannelUser($cid, $online_uid)) { $res->status(403); // You have to join channel return; } // check this user is an operator on this channel if (!Container_channels_op::isOp($cid, $online_uid)) { $res->status(403); // You have to be an operator to unban a user return;
} // check login/password if ($login and Container_indexes::getIndex('users/name', $login)) { $res->status(403); $res['Content-Type'] = 'application/json; charset=utf-8'; $res['Pfc-WWW-Authenticate'] = 'Basic realm="Authentication"'; $res->body(GetPfcError(40302)); // "Login already used" return; } else { if ($login) { $uid = Container_users::generateUid(); $udata = array('id' => $uid, 'name' => $login, 'role' => 'user'); Container_users::setUserData($uid, $udata); $_SESSION['userdata'] = $udata; Container_users::setIsAlive($uid); $res->status(200); $res['Content-Type'] = 'application/json; charset=utf-8'; $res->body(json_encode($_SESSION['userdata'])); return; } else { $res->status(403); $res['Pfc-WWW-Authenticate'] = 'Basic realm="Authentication"'; $res['Content-Type'] = 'application/json; charset=utf-8'; $res->body(GetPfcError(40303)); // "Wrong credentials" return; } } }); $app->delete('/auth', function () use($app, $req, $res) {
} }); /** * 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']; // check this user is online if (!Container_users::checkUserExists($uid)) { $res->status(400); // User is not connected return; } // check this user has joined the channel if (!Container_channels::checkChannelUser($cid, $uid)) { $res->status(403); // You have to join channel before post a message return; } // check that request content contains a message $data = json_decode($req->getBody()); if (!isset($data->body) or $data->body === '') { $res->status(400); // Missing parameter [body]