9 require_once
'base/Site.php';
10 require_once
'base/Page.php';
11 require_once
'rights/User_SQL.php';
12 require_once
'rights/Group_SQL.php';
13 require_once
'rights/Role_SQL.php';
56 private $requestMethod;
63 private $requestType =
'';
70 private $permissions = array();
77 private $params = array();
90 $this->requestMethod = strtoupper($_SERVER[
'REQUEST_METHOD']);
93 $request = file_get_contents(
'php://input');
94 $_REQUEST = array_merge(array_merge($_GET, $_POST), (array) json_decode($request,
true));
96 if ($_REQUEST[
'@data']) {
97 $_REQUEST = json_decode($_REQUEST[
'@data'],
true);
100 if (!is_array($_REQUEST)) {
105 foreach ($_REQUEST as $key => $value) {
106 if ($value ==
'@file' && !empty($source = array_shift($_FILES))) {
107 $_REQUEST[$key] = $source[
'tmp_name'];
118 $convert_pages =
function ($values) use (&$convert_pages) {
119 foreach ($values as $key => $value) {
120 if (is_array($value)) {
121 $values[$key] = $convert_pages($value);
122 } elseif (strpos($value,
'@identity:') === 0) {
125 'deleted_or' =>
'1=1',
127 'only_active' =>
false
133 $_REQUEST = $convert_pages($_REQUEST);
136 $uri = $_SERVER[
'REQUEST_URI'];
137 if (strpos($uri, self::BASE_URI) === 0) {
139 $uri = explode(
'/', substr(parse_url($uri, PHP_URL_PATH), strlen(self::BASE_URI)));
141 if (
sizeof($uri) > 1) {
146 $this->requestType =
'user';
147 $this->params[
'user_id'] = (string) $uri[1];
148 if (isset($uri[2])) {
149 $this->params[
'method'] = (string) $uri[2];
154 $this->requestType =
'group';
155 $this->params[
'group_id'] = (string) $uri[1];
156 if (isset($uri[2])) {
157 $this->params[
'method'] = (string) $uri[2];
162 $this->requestType =
'role';
163 $this->params[
'role_id'] = (string) $uri[1];
164 if (isset($uri[2])) {
165 $this->params[
'method'] = (string) $uri[2];
169 $this->requestType =
'system';
170 if (isset($uri[1])) {
171 $this->params[
'method'] = (string) $uri[1];
176 $this->requestType =
'site';
178 $this->params[
'site'] = $uri[0];
179 $this->params[
'lang'] = $uri[1];
180 if (is_numeric($uri[2])) {
181 $this->requestType =
'page';
183 $this->params[
'id'] = $uri[2];
184 if (isset($uri[3])) {
185 if ($uri[3] ==
'pool' && isset($uri[4])) {
186 $this->requestType =
'pool';
187 $this->params[
'method'] = (string) $uri[4];
189 $this->params[
'method'] = (string) $uri[3];
193 $this->params[
'method'] = (string) $uri[2];
198 $this->params[
'method'] = $uri[0];
202 if (!$GLOBALS[
'auth']->isNobody()) {
203 $cache = $GLOBALS[
'egotec_conf'][
'cache_dir'].
'api-' . md5(serialize([
204 $GLOBALS[
'auth']->user->field[
'user_id'],
205 $GLOBALS[
'auth']->user->extra[
'api_token'],
206 (
string) $this->params[
'site']
212 $permissions = array(
213 $GLOBALS[
'egotec_conf'][
'lib_dir'].
'base/rest.ini',
214 $GLOBALS[
'egotec_conf'][
'site_dir'].
'_global/rest.ini'
216 if (!empty($this->params[
'site'])) {
217 $permissions[] = $GLOBALS[
'egotec_conf'][
'site_dir'].$this->params[
'site'].
'/rest.ini';
220 require_once(
'base/Site.php');
222 $this->site =
new Site($this->params[
'site'], (
string) $this->params[
'lang']);
223 if ($this->site->theme) {
224 $permissions[] = $GLOBALS[
'egotec_conf'][
'pub_dir'].
'theme/'.$this->site->theme.
'/site/rest.ini';
228 egotec_error_log($e->getMessage());
235 foreach ($permissions as $file) {
237 $data = (array) @parse_ini_file($file,
true);
239 $this->permissions = array_merge_recursive($this->permissions, $data);
259 if ($this->requestType && $GLOBALS[
'auth']->isNobody()) {
263 switch ($this->requestType) {
272 require_once(
'base/Site.php');
273 $site =
new Site($this->params[
'site'], (
string) $this->params[
'lang']);
275 require_once(
'base/Page.php');
278 if (!isset($this->params[
'sitemap'])) {
279 $site->addParam([
'sitemap' =>
true]);
282 if (!$site->hasRight(
'view')) {
287 if (!empty($this->params[
'id'])) {
289 if (empty($this->params[
'method'])) {
291 $page_param = $_REQUEST;
292 } elseif (isset($_REQUEST[
'page_param'])) {
294 $page_param = $_REQUEST[
'page_param'];
295 unset($_REQUEST[
'page_param']);
299 if ($_SERVER[
'HTTP_X_REPLICATION']) {
300 $page_param = array_merge($page_param, [
302 'only_active' =>
false,
304 'deleted_or' =>
'1=1'
308 $page = $GLOBALS[
'page'] = $site->getPage($this->params[
'id'], $page_param);
314 switch ($this->requestMethod) {
317 if (!empty($this->params[
'method'])) {
319 if ($this->requestType ==
'pool') {
321 $result = $this->call($page->getMediapool(), $this->params[
'method'], $_REQUEST);
324 $result = $this->call($page, $this->params[
'method'], $_REQUEST);
328 $result = $this->call($site, $this->params[
'method'], $_REQUEST);
331 switch ($this->requestMethod) {
333 if (!empty($this->params[
'id'])) {
334 if ($this->requestType ==
'pool') {
336 $result = $this->call($page->getMediapool(),
'put', $_REQUEST);
339 $result = $this->call($page,
'newChild', $_REQUEST);
343 if (!$GLOBALS[
'auth']->hasSuperuserPermission()) {
352 if (!$page->hasRights(
'view')) {
356 if ($this->requestType ==
'pool') {
358 $result = $this->call($page->getMediapool(),
'list', $_REQUEST);
372 if (empty($this->params[
'method'])) {
373 if ($this->requestType ==
'pool') {
375 $result = $this->call($page->getMediapool(),
'edit', $_REQUEST);
378 $result = $this->call($page,
'update', $_REQUEST);
381 if ($this->requestType ==
'pool') {
383 $result = $this->call($page->getMediapool(), $this->params[
'method'], $_REQUEST);
386 $result = $this->call($page, $this->params[
'method'], $_REQUEST);
393 if ($this->requestType ==
'pool') {
395 $result = $this->call($page->getMediapool(),
'delete', $_REQUEST);
398 $result = $this->call($page,
'delete', $_REQUEST);
406 $user =
new User_SQL($this->params[
'user_id']);
408 switch ($this->requestMethod) {
411 if (!empty($this->params[
'method'])) {
413 $result = $this->call($user, $this->params[
'method'], $_REQUEST);
415 if ($this->requestMethod ==
'POST') {
417 if (!empty($user->field[
'user_id'])) {
418 $user =
new User_SQL();
420 $this->call($user,
'update', $_REQUEST);
429 $result = $this->call($user,
'update', $_REQUEST);
433 $result = $this->call($user,
'delete', $_REQUEST);
439 $group =
new Group_SQL($this->params[
'group_id']);
441 switch ($this->requestMethod) {
444 if (!empty($this->params[
'method'])) {
446 $result = $this->call($group, $this->params[
'method'], $_REQUEST);
448 if ($this->requestMethod ==
'POST') {
450 $new_group =
new Group_SQL();
451 if ($this->call($group,
'addChild', array($new_group))) {
452 $new_group =
new Group_SQL($new_group->field[
'group_id']);
453 $this->call($new_group,
'update', $_REQUEST);
466 $result = $this->call($group,
'update', $_REQUEST);
470 $result = $this->call($group,
'delete', $_REQUEST);
476 $role =
new Role_SQL($this->params[
'role_id']);
478 switch ($this->requestMethod) {
481 if (!empty($this->params[
'method'])) {
483 $result = $this->call($role, $this->params[
'method'], $_REQUEST);
485 if ($this->requestMethod ==
'POST') {
487 $new_role =
new Role_SQL();
488 if ($this->call($role,
'addChild', array($new_role))) {
489 $new_role =
new Role_SQL($new_role->field[
'role_id']);
490 $this->call($new_role,
'update', $_REQUEST);
503 $result = $this->call($role,
'update', $_REQUEST);
507 $result = $this->call($role,
'delete', $_REQUEST);
513 $result = $this->call(
'Ego_System', $this->params[
'method'], $_REQUEST);
519 $result = $this->call($this, $this->params[
'method'], $_REQUEST);
521 $this->sendSuccess();
524 switch ($e->getCode()) {
532 'error' => $e->getMessage(),
533 'code' => $e->getCode()
535 }
catch (Exception $e) {
538 egotec_error_log($e->getMessage());
545 return $this->toJSON($result);
556 private function call($object, $method, $params = array()) {
557 if (empty($method) || !method_exists($object, $method)) {
564 if (!empty($this->requestType)) {
565 if (isset($this->permissions[$this->requestType]) && isset($this->permissions[$this->requestType][$method])) {
566 $values = explode(
':', $this->permissions[$this->requestType][$method]);
567 $methods = explode(
',', $values[0]);
568 $perms = array_filter(explode(
',', $values[1]),
function($value) {
569 return trim($value) !=
'';
572 if (!empty($methods) && !in_array($this->requestMethod, $methods)) {
578 if ($authorized && !empty($perms)) {
580 if (is_a($object,
'Site')) {
581 $object->setRights($perms);
585 if (is_a($object,
'Page') && !$object->hasRights($perms)) {
590 if (is_a($object,
'Mediapool') && ($page = $object->getPage()) && !$page->hasRights($perms)) {
598 && (is_a($object,
'User_SQL')
599 || is_a($object,
'Group_SQL')
600 || is_a($object,
'Role_SQL'))
601 && !$GLOBALS[
'auth']->hasPermissionOn($object)
607 if ($this->requestType ==
'system') {
608 if (!empty($perms)) {
610 foreach ($perms as $perm) {
611 list($group, $role) = explode(
';', $perm);
612 if ($GLOBALS[
'auth']->hasPermission($group, $role)) {
617 } elseif (!$GLOBALS[
'auth']->hasSuperuserPermission()) {
631 if (!is_array($params)) {
634 return @call_user_func_array(array($object, $method), $params);
643 private function toJSON($object) {
646 is_a($object,
'Page')
647 || is_a($object,
'User_SQL')
648 || is_a($object,
'Group_SQL')
649 || is_a($object,
'Role_SQL')
652 'field' => $object->field,
653 'extra' => $object->extra
659 is_a($object,
'Page_Iterator')
660 || is_a($object,
'User_Iterator')
661 || is_a($object,
'Group_Iterator')
662 || is_a($object,
'Role_Iterator')
665 foreach ($object as $item) {
666 $results[] = $this->toJSON($item);
679 private function sendSuccess() {
680 switch ($this->requestMethod) {
704 private function sendError() {
705 switch ($this->requestMethod) {
729 private function startSession($user_id, $token) {
730 $user =
new User_SQL($user_id);
731 if (!empty($token) && !empty($user->extra[
'api_token']) && $user->extra[
'api_token'] == $token) {
733 Auth_Factory::login($user_id);
734 return !$GLOBALS[
'auth']->isNobody() ? session_id() : null;
745 private function closeSession() {
746 if (!empty($_SESSION[
'auth_id'])) {
747 Auth_Factory::start($GLOBALS[
'egotec_conf'][
'auth'][
'type'], $_SESSION[
'auth_id'],
false,
'logout');
const NOT_AUTHORIZED_TEXT
const INVALID_PARAMS_TEXT
const INVALID_METHOD_TEXT
static file_put_contents($filename, $data, $flags=0, $context=null)
static header($header, $replace=true)
static isDevMode($ignore=true)
static file_exists($file)
static file_get_contents($filename, $utf8=true, $context=null)
static byIdentity($identity, $param=array(), $site=null)
static createSite($new_site)