0
1
Fork 0
hedera-web-mindshore/web/rest-service.php

101 lines
2.1 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
namespace Vn\Web;
2016-08-22 10:41:05 +00:00
use Vn\Lib;
use Vn\Lib\Locale;
use Vn\Lib\UserException;
2016-08-22 10:41:05 +00:00
2016-07-22 20:00:27 +00:00
/**
2016-08-22 10:41:05 +00:00
* Base class for REST application.
*/
2018-05-23 10:14:20 +00:00
class RestService extends Service {
function run() {
ini_set('display_errors', _ENABLE_DEBUG);
set_error_handler([$this, 'errorHandler'], E_ALL);
set_exception_handler([$this, 'exceptionHandler']);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$this->init();
$this->startSession();
$this->loadMethod(__NAMESPACE__.'\RestRequest');
2016-08-22 10:41:05 +00:00
}
/**
* Runs a REST method.
*/
2018-05-23 10:14:20 +00:00
function loadMethod($class) {
$db = $this->db;
2018-05-23 10:14:20 +00:00
$this->login();
2018-05-23 10:14:20 +00:00
$method = $this->app->loadMethod(
$_REQUEST['method'], $class, './rest');
$method->service = $this;
2018-05-23 10:14:20 +00:00
if ($method::SECURITY == Security::DEFINER) {
$isAuthorized = $db->getValue('SELECT userCheckRestPriv(#)',
[$_REQUEST['method']]);
if (!$isAuthorized)
2018-05-23 10:14:20 +00:00
throw new UserException(s('You don\'t have enough privileges'));
$methodDb = $db;
}
else
2018-05-23 10:14:20 +00:00
$methodDb = $this->getUserDb($_SESSION['user']);
2018-05-23 10:14:20 +00:00
if ($method::PARAMS !== NULL && !$method->checkParams($_REQUEST, $method::PARAMS))
throw new UserException (s('Missing parameters'));
2018-05-23 10:14:20 +00:00
Locale::addPath('rest/'. dirname($_REQUEST['method']));
$res = NULL;
try {
2018-05-23 10:14:20 +00:00
$res = $method->run($methodDb);
}
2018-05-23 10:14:20 +00:00
catch (Db\Exception $e) {
if ($e->getCode() == 1644)
throw new UserException(s($e->getMessage()));
}
if ($method::SECURITY == Security::DEFINER)
2018-05-23 10:14:20 +00:00
$methodDb->query('CALL account.myUserLogout()');
2018-05-23 10:14:20 +00:00
$db->query('CALL account.myUserLogout()');
return $res;
}
2016-08-22 10:41:05 +00:00
2018-05-23 10:14:20 +00:00
function statusFromException($e) {
2016-08-22 10:41:05 +00:00
try {
throw $e;
2016-07-22 20:00:27 +00:00
}
2018-05-23 10:14:20 +00:00
catch (SessionExpiredException $e) { $status = 401; }
catch (BadLoginException $e) { $status = 401; }
catch (Lib\UserException $e) { $status = 400; }
catch (\Exception $e) { $status = 500; }
2016-08-22 10:41:05 +00:00
2018-05-23 10:14:20 +00:00
http_response_code($status);
2016-08-22 10:41:05 +00:00
}
2018-05-23 10:14:20 +00:00
function errorHandler($errno, $message, $file, $line, $context) {
2016-08-22 10:41:05 +00:00
$eFlag =
E_USER_NOTICE
| E_USER_WARNING
| E_USER_DEPRECATED
| E_NOTICE
| E_WARNING
| E_DEPRECATED;
if (!($errno & $eFlag))
2018-05-23 10:14:20 +00:00
http_response_code(500);
2016-08-22 10:41:05 +00:00
return FALSE;
2016-07-22 20:00:27 +00:00
}
2018-05-23 10:14:20 +00:00
function exceptionHandler($e) {
$this->statusFromException($e);
2016-08-22 10:41:05 +00:00
throw $e;
2016-07-22 20:00:27 +00:00
}
}