Permalink
Browse files

first steps concerning channel operators management

  • Loading branch information...
1 parent c780a27 commit 76a101e489ec94d45c6a8cce18e194f19f3da3fd @kerphi committed Jan 9, 2013
Showing with 198 additions and 0 deletions.
  1. +1 −0 doc/developer.md
  2. +3 −0 server/config.php
  3. +42 −0 server/container/channels-op.php
  4. +1 −0 server/index.php
  5. +151 −0 server/routes/channels-op.php
View
@@ -83,6 +83,7 @@ Example: TODO
* `server/channels/:cid/op/` - GET - returns the :cid channel operators list (list of :uid)
* `server/channels/:cid/op/:uid` - GET - tells if :uid is operator on :cid
* `server/channels/:cid/op/:uid` - PUT - add :uid to the operator list on :cid channel (try to)
+* `server/channels/:cid/op/:uid` - DELETE - removes operator rights to :uid on :cid channel (try to)
* `server/users/` - GET - returns the online users :uid currently online on the server [not implemented]
* `server/users/:uid/` - GET - returns :uid user info
* `server/users/:uid/msg/` - GET - pending messages for :uid user (from channels or a private messages)
View
@@ -9,6 +9,9 @@
// (Default value: 35 seconds)
$GLOBALS['pfc_timeout'] = 35;
+// Gives channel operator rights to the first connected user
+// (Default value: true)
+$GLOBALS['first_is_op'] = true;
// custom user hooks
$GLOBALS['pfc_hooks'] = array();
@@ -0,0 +1,42 @@
+<?php
+
+include_once 'container/channels.php';
+
+class Container_channels_op {
+
+ /**
+ * Returns the $cid channel operators list
+ */
+ static public function getOpList($cid) {
+ $cdir = Container_channels::getChannelsDir();
+ $p = $cdir.'/'.$cid.'/op/*';
+ return array_map('basename', glob($p));
+ }
+
+ /**
+ * Sets $uid as an operator on $cid
+ */
+ static public function addOp($cid, $uid) {
+ $cdir = Container_channels::getChannelsDir();
+ $p = $cdir.'/'.$cid.'/op/'.$uid;
+ return @touch($p);
+ }
+
+ /**
+ * Remove $uid operator on $cid
+ */
+ static public function rmOp($cid, $uid) {
+ $cdir = Container_channels::getChannelsDir();
+ $p = $cdir.'/'.$cid.'/op/'.$uid;
+ return @unlink($p);
+ }
+
+ /**
+ * Check if $uid is an operator on $cid
+ */
+ static public function isOp($cid, $uid) {
+ $cdir = Container_channels::getChannelsDir();
+ $p = $cdir.'/'.$cid.'/op/'.$uid;
+ return file_exists($p);
+ }
+}
View
@@ -40,6 +40,7 @@ function GetPfcError($code, $jsonformat = true) {
require 'routes/auth.php';
require 'routes/channels.php';
+require 'routes/channels-op.php';
require 'routes/users.php';
require 'routes/utils.php';
@@ -0,0 +1,151 @@
+<?php
+
+include_once 'container/users.php';
+include_once 'container/channels-op.php';
+
+/**
+ * Returns the :cid channel operators list (list of :uid)
+ */
+$app->get('/channels/:cid/op/', 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
+ return;
+ }
+
+ // get the operators on $cid
+ $ops = Container_channels_op::getOpList($cid);
+
+ $res->status(200);
+ $res['Content-Type'] = 'application/json; charset=utf-8';
+ $res->body(json_encode($ops));
+});
+
+/**
+ * Tells if :uid is operator on :cid
+ */
+$app->get('/channels/:cid/op/: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;
+ }
+ $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 if requested $uid is op on $cid
+ $op = Container_channels_op::isOp($cid, $uid);
+ if ($op) {
+ $res->status(200);
+ } else {
+ $res->status(404);
+ }
+ $res['Content-Type'] = 'application/json; charset=utf-8';
+ $res->body(json_encode($op));
+});
+
+
+/**
+ * Adds :uid to the operator list on :cid channel
+ * (only operators are allowed to add another op)
+ */
+$app->put('/channels/:cid/op/: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;
+ }
+ $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 is an operator on this channel
+ if (!Container_channels_op::isOp($cid, $online_uid)) {
+ $res->status(403); // You have to be an operator to give op to other user
+ return;
+ }
+
+ // add $uid user as a $cid channel operator
+ $ok = Container_channels_op::addOp($cid, $uid);
+ if ($ok) {
+ $res->status(200);
+ // todo: notification to other connected user of this new operator
+ } else {
+ $res->status(500);
+ }
+ $res['Content-Type'] = 'application/json; charset=utf-8';
+ $res->body(json_encode($ok));
+});
+
+/**
+ * Removes :uid from the operator list on :cid channel
+ * (only operators are allowed to remove other op)
+ */
+$app->delete('/channels/:cid/op/: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;
+ }
+ $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 is an operator on this channel
+ if (!Container_channels_op::isOp($cid, $online_uid)) {
+ $res->status(403); // You have to be an operator to give op to other user
+ return;
+ }
+
+ // removes $uid user as a $cid channel operator
+ $ok = Container_channels_op::rmOp($cid, $uid);
+ if ($ok) {
+ $res->status(200);
+ // todo: notification to other connected user of this removed operator
+ } else {
+ $res->status(500);
+ }
+ $res['Content-Type'] = 'application/json; charset=utf-8';
+ $res->body(json_encode($ok));
+});
+

0 comments on commit 76a101e

Please sign in to comment.