43 const BASE_URI =
'/rest/';
50 private $requestMethod;
57 private $requestType =
'';
64 private $permissions = array();
71 private $params = array();
84 $this->requestMethod = strtoupper($_SERVER[
'REQUEST_METHOD']);
87 $request = file_get_contents(
'php://input');
88 switch ($this->requestMethod) {
90 parse_str($request, $_REQUEST);
93 $_REQUEST = array_merge(array_merge($_GET, $_POST), (array) json_decode($request,
true));
95 if (!is_array($_REQUEST)) {
100 $uri = $_SERVER[
'REQUEST_URI'];
101 if (strpos($uri, self::BASE_URI) === 0) {
103 $uri = explode(
'/', substr(parse_url($uri, PHP_URL_PATH), strlen(self::BASE_URI)));
105 if (
sizeof($uri) > 1) {
110 $this->requestType =
'user';
111 $this->params[
'user_id'] = (string) $uri[1];
112 if (isset($uri[2])) {
113 $this->params[
'method'] = (string) $uri[2];
118 $this->requestType =
'group';
119 $this->params[
'group_id'] = (string) $uri[1];
120 if (isset($uri[2])) {
121 $this->params[
'method'] = (string) $uri[2];
126 $this->requestType =
'role';
127 $this->params[
'role_id'] = (string) $uri[1];
128 if (isset($uri[2])) {
129 $this->params[
'method'] = (string) $uri[2];
133 $this->requestType =
'system';
134 if (isset($uri[1])) {
135 $this->params[
'method'] = (string) $uri[1];
140 $this->requestType =
'site';
142 $this->params[
'site'] = $uri[0];
143 $this->params[
'lang'] = $uri[1];
144 if (is_numeric($uri[2])) {
145 $this->requestType =
'page';
147 $this->params[
'id'] = $uri[2];
148 if (isset($uri[3])) {
149 $this->params[
'method'] = (string) $uri[3];
152 $this->params[
'method'] = (string) $uri[2];
157 $this->params[
'method'] = $uri[0];
161 if (!$GLOBALS[
'auth']->isNobody()) {
162 $cache = $GLOBALS[
'egotec_conf'][
'cache_dir'].
'api-'.$GLOBALS[
'auth']->user->field[
'user_id'].
'-'.$GLOBALS[
'auth']->user->extra[
'api_token'];
167 $permissions = array(
168 $GLOBALS[
'egotec_conf'][
'lib_dir'].
'base/rest.ini',
169 $GLOBALS[
'egotec_conf'][
'site_dir'].
'_global/rest.ini' 171 if (!empty($this->params[
'site'])) {
172 $permissions[] = $GLOBALS[
'egotec_conf'][
'site_dir'].$this->params[
'site'].
'/rest.ini';
175 require_once(
'base/Site.php');
177 $this->site =
new Site($this->params[
'site'], (
string) $this->params[
'lang']);
178 if ($this->site->theme) {
179 $permissions[] = $GLOBALS[
'egotec_conf'][
'pub_dir'].
'theme/'.$this->site->theme.
'/site/rest.ini';
183 egotec_error_log($e->getMessage());
190 foreach ($permissions as $file) {
192 $data = (array) @parse_ini_file($file,
true);
194 $this->permissions = array_merge_recursive($this->permissions, $data);
214 if ($this->requestType && $GLOBALS[
'auth']->isNobody()) {
218 switch ($this->requestType) {
226 require_once(
'base/Site.php');
227 $site =
new Site($this->params[
'site'], (
string) $this->params[
'lang']);
229 require_once(
'base/Page.php');
231 if (!$site->hasRight(
'view')) {
236 if (!empty($this->params[
'id'])) {
238 if (empty($this->params[
'method'])) {
240 $page_param = $_REQUEST;
241 } elseif (isset($_REQUEST[
'page_param'])) {
243 $page_param = $_REQUEST[
'page_param'];
244 unset($_REQUEST[
'page_param']);
246 $page = $site->getPage($this->params[
'id'], $page_param);
252 switch ($this->requestMethod) {
255 if (!empty($this->params[
'method'])) {
258 $result = $this->call($page, $this->params[
'method'], $_REQUEST);
261 $result = $this->call($site, $this->params[
'method'], $_REQUEST);
264 switch ($this->requestMethod) {
266 if (!empty($this->params[
'id'])) {
268 $result = $this->call($page,
'newChild', $_REQUEST);
271 if (!$GLOBALS[
'auth']->hasSuperuserPermission()) {
281 if (!$page->hasRights(
'view')) {
296 $result = $this->call($page,
'update', $_REQUEST);
302 $result = $this->call($page,
'delete', $_REQUEST);
309 require_once(
'rights/User_SQL.php');
310 $user =
new User_SQL($this->params[
'user_id']);
312 switch ($this->requestMethod) {
315 if (!empty($this->params[
'method'])) {
317 $result = $this->call($user, $this->params[
'method'], $_REQUEST);
319 if ($this->requestMethod ==
'POST') {
321 if (!empty($user->field[
'user_id'])) {
322 $user =
new User_SQL();
324 $this->call($user,
'update', $_REQUEST);
333 $result = $this->call($user,
'update', $_REQUEST);
337 $result = $this->call($user,
'delete', $_REQUEST);
343 require_once(
'rights/Group_SQL.php');
344 $group =
new Group_SQL($this->params[
'group_id']);
346 switch ($this->requestMethod) {
349 if (!empty($this->params[
'method'])) {
351 $result = $this->call($group, $this->params[
'method'], $_REQUEST);
353 if ($this->requestMethod ==
'POST') {
355 $new_group =
new Group_SQL();
356 if ($this->call($group,
'addChild', array($new_group))) {
357 $new_group =
new Group_SQL($new_group->field[
'group_id']);
358 $this->call($new_group,
'update', $_REQUEST);
371 $result = $this->call($group,
'update', $_REQUEST);
375 $result = $this->call($group,
'delete', $_REQUEST);
381 require_once(
'rights/Role_SQL.php');
382 $role =
new Role_SQL($this->params[
'role_id']);
384 switch ($this->requestMethod) {
387 if (!empty($this->params[
'method'])) {
389 $result = $this->call($role, $this->params[
'method'], $_REQUEST);
391 if ($this->requestMethod ==
'POST') {
393 $new_role =
new Role_SQL();
394 if ($this->call($role,
'addChild', array($new_role))) {
395 $new_role =
new Role_SQL($new_role->field[
'role_id']);
396 $this->call($new_role,
'update', $_REQUEST);
409 $result = $this->call($role,
'update', $_REQUEST);
413 $result = $this->call($role,
'delete', $_REQUEST);
420 if (!$GLOBALS[
'auth']->hasSuperuserPermission()) {
423 $result = $this->call(
'Ego_System', $this->params[
'method'], $_REQUEST);
429 $result = $this->call($this, $this->params[
'method'], $_REQUEST);
431 $this->sendSuccess();
434 switch ($e->getCode()) {
442 'error' => $e->getMessage(),
443 'code' => $e->getCode()
445 }
catch (Exception $e) {
448 egotec_error_log($e->getMessage());
455 return $this->toJSON($result);
466 private function call($object, $method, $params = array()) {
467 if (empty($method) || !method_exists($object, $method)) {
474 if (!empty($this->requestType)) {
475 if (isset($this->permissions[$this->requestType]) && isset($this->permissions[$this->requestType][$method])) {
476 $values = explode(
':', $this->permissions[$this->requestType][$method]);
477 $methods = explode(
',', $values[0]);
478 $perms = explode(
',', $values[1]);
480 if (!empty($methods) && !in_array($this->requestMethod, $methods)) {
486 if ($authorized && !empty($perms)) {
488 if (is_a($object,
'Site')) {
489 $object->setRights($perms);
493 if (is_a($object,
'Page') && !$object->hasRights($perms)) {
501 && (is_a($object,
'User_SQL')
502 || is_a($object,
'Group_SQL')
503 || is_a($object,
'Role_SQL'))
504 && !$GLOBALS[
'auth']->hasPermissionOn($object)
517 if (!is_array($params)) {
520 return @call_user_func_array(array($object, $method), $params);
529 private function toJSON($object) {
532 is_a($object,
'Page')
533 || is_a($object,
'User_SQL')
534 || is_a($object,
'Group_SQL')
535 || is_a($object,
'Role_SQL')
538 'field' => $object->field,
539 'extra' => $object->extra
545 is_a($object,
'Page_Iterator')
546 || is_a($object,
'User_Iterator')
547 || is_a($object,
'Group_Iterator')
548 || is_a($object,
'Role_Iterator')
551 foreach ($object as $item) {
552 $results[] = $this->toJSON($item);
565 private function sendSuccess() {
566 switch ($this->requestMethod) {
590 private function sendError() {
591 switch ($this->requestMethod) {
615 private function startSession($user_id, $token) {
616 $user =
new User_SQL($user_id);
617 if (!empty($token) && !empty($user->extra[
'api_token']) && $user->extra[
'api_token'] == $token) {
619 Auth_Factory::login($user_id);
620 return !$GLOBALS[
'auth']->isNobody() ? session_id() : null;
631 private function closeSession() {
632 if (!empty($_SESSION[
'auth_id'])) {
633 Auth_Factory::start($GLOBALS[
'egotec_conf'][
'auth'][
'type'], $_SESSION[
'auth_id'],
false,
'logout');
static file_put_contents($filename, $data, $flags=0, $context=null)
static createSite($new_site)
const INVALID_PARAMS_TEXT
static file_exists($file)
const NOT_AUTHORIZED_TEXT
const INVALID_METHOD_TEXT
static file_get_contents($filename, $utf8=true, $context=null)
static header($header, $replace=true)