Skip to content

Instantly share code, notes, and snippets.

@emanuele45
Created October 18, 2014 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emanuele45/cce035c4da149b4f425e to your computer and use it in GitHub Desktop.
Save emanuele45/cce035c4da149b4f425e to your computer and use it in GitHub Desktop.
<?php
class Members_Restv1
{
public function __construct($db)
{
$this->db = $db;
}
public function get($id, $params)
{
if ($id === null)
return $this->getMembers($params);
else
return $this->getMember($id);
}
protected function getMembers($params)
{
$default = array(
'start' => 0,
'limit' => 10,
'sort' => 'mem.id_member',
);
$params = array_merge($default, $params);
// Let's jsut be sure people do not request too many of them
$params['limit'] = min($params['limit'], 50);
$where_params = array('activated' => array(1, 2));
if (isset($params['where']))
$params['where'] .= ' AND mem.is_activated IN ({array_int:activated})';
else
$params['where'] = 'mem.is_activated IN ({array_int:activated})';
require_once(SUBSDIR . '/Members.subs.php');
$members = list_getMembers($params['start'], $params['limit'], $params['sort'], $params['where'], $where_params);
$return = array();
foreach ($members as $member)
$return['member'][] = $this->sanitizeMember($member);
return $return;
}
protected function sanitizeMember($member)
{
$unset_array = array('email', 'ip', 'pref', 'notify');
$output = array();
foreach ($member as $key => $val)
{
foreach ($unset_array as $to_unset)
{
if (strpos($key, $to_unset) !== false)
continue 2;
}
$output[$key] = $val;
}
return $output;
}
protected function getMember($id)
{
$id = (int) $id;
if (empty($id))
return false;
require_once(SUBSDIR . '/Members.subs.php');
$member = getBasicMemberData($id, array('preferences' => true, 'moderation' => true));
return $this->sanitizeMember($member);
}
}
<?php
/**
* The first implementation of a REST API for ElkArte.
*
* @name ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause
*
* @version 1.0
*
*/
if (!defined('ELK'))
die('No access...');
class Restv1_Controller extends Action_Controller
{
public function action_index()
{
if (isset($_REQUEST['sa']))
$sa = Util::htmlspecialchars($_REQUEST['sa']);
else
$sa = '';
$this->format = isset($_REQUEST['api']) && $_REQUEST['api'] == 'xml' ? 'xml' : 'json';
$this->interpret_request($sa);
$this->dispatch();
}
protected function dispatch()
{
if ($this->validAction())
{
$db = database();
$method = ucfirst($this->action) . '_Restv1';
$instance = new $method($db);
$this->response = $instance->get($this->id, $this->params);
}
if (empty($this->response))
{
$this->noAction();
}
return $this->sendResponse();
}
protected function validAction()
{
$valid = false;
if (!empty($this->action) && file_exists(SUBSDIR . '/RestAPI/' . ucfirst($this->action) . '.restv1.php'))
{
require_once(SUBSDIR . '/RestAPI/' . ucfirst($this->action) . '.restv1.php');
$valid = true;
}
return $valid;
}
protected function noAction()
{
$this->response = array('no_data' => 'no_data');
}
protected function sendResponse()
{
global $txt, $context;
loadLanguage('Restv1');
// @todo temporary while working on it, afterwards uncomment the next line
loadTemplate('Restv1');
// loadTemplate(ucfirst($this->format));
Template_Layers::getInstance()->removeAll();
$context['sub_template'] = 'rest_' . $this->format;
$context[$this->format . '_data'] = $this->buildResponseRecursive($this->response);
}
protected function buildResponseRecursive($response)
{
global $txt;
$return = array();
foreach ($response as $key => $val)
{
if (is_array($val))
$return[$key] = $this->buildResponseRecursive($val);
elseif (isset($txt[$val]))
$return[$key] = $txt[$val];
elseif ((string) $val === (string) (int) $val)
$return[$key] = (int) $val;
else
$return[$key] = $val;
}
return $return;
}
protected function interpret_request($request)
{
$explode = explode('/', $request);
$this->action = array_shift($explode);
$this->id = array_shift($explode);
$this->params = array();
$total = count($explode);
for ($i = 0; $i < $total; $i + 2)
{
$this->params[$explode[$i]] = isset($explode[$i + 1]) ? $explode[$i + 1] : null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment