[go: up one dir, main page]

Menu

[3822d7]: / core / Api.php  Maximize  Restore  History

Download this file

137 lines (116 with data), 4.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace ICT\Core;
/* * ***************************************************************
* Copyright © 2016 ICT Innovations Pakistan All Rights Reserved *
* Developed By: Nasir Iqbal *
* Website : http://www.ictinnovations.com/ *
* Mail : nasir@ictinnovations.com *
* *************************************************************** */
use Jacwright\RestServer\RestServer;
class Api
{
/** @var boolean include_subfolder */
private $include_subfolder = true;
/** @var string #interface_type */
private $interface_type = 'local';
/** @var RestServer $oInterface */
private $oInterface = null;
public function authenticate($credentials, $auth_type)
{
try {
$oUser = User::authenticate($credentials, $auth_type);
if ($oUser instanceof User) {
do_login($oUser);
return true;
}
return false;
} catch (CoreException $e) {
Corelog::log($e->getMessage(), Corelog::ERROR);
return false;
}
}
protected function _authorize($permission)
{
if (empty($permission) || can_access($permission) == false) {
throw new CoreException(403, 'User not permitted to perform required action');
}
return true;
}
protected function set($oEntity, $data)
{
foreach ($data as $key => $value) {
try {
$oEntity->$key = $value;
} catch (CoreException $ex) {
throw new CoreException(412, 'Data validation failed, for ' . $key, $ex);
}
}
}
public function create_interface($interface_type = null, $root_path = null)
{
global $path_cache;
if (!empty($interface_type) && $interface_type = 'rest') {
// Initialize the server
$this->interface_type = 'rest';
$realm = Conf::get('company:name', 'ICTCore') . ' :: REST API Server';
$this->oInterface = new RestServer('production', $realm); // debug / production
$this->oInterface->root = $root_path;
$this->oInterface->cacheDir = $path_cache; // set folder for rest server url mapping
$this->oInterface->jsonAssoc = true; // always get associated array for POST data
// CORS support
$origin_list = Conf::get('website:cors', '');
if (!empty($origin_list) && !in_array(trim($origin_list), array('no', '0', 'disable', 'disabled'))) {
$this->oInterface->useCors = true;
$this->oInterface->allowedOrigin = explode(',', $origin_list);
}
$this->oInterface->authHandler = new Http(); // Authentication via HTTP interface
self::rest_load($this->oInterface);
}
}
public function get_request_url()
{
return $this->server->url;
}
public function get_request_method()
{
return $this->server->method;
}
public function get_request_format()
{
return $this->server->format;
}
public function send_error($code, $message)
{
$this->oInterface->handleError($code, $message);
}
protected static function rest_include()
{
if (property_exists (get_called_class(), 'include_subfolder')) {
return 'Api'; // Api class return sub api folder
}
// in child class return null
return null;
}
protected static function rest_load(&$restInterface)
{
$dir = static::rest_include();
if (empty($restInterface) || empty($dir)) {
return false;
}
include_once_directory($dir);
$namespace = path_to_namespace($dir);
$listClass = get_declared_classes();
// escape slashes from namespace and add an extra slash to select child classes only
$listApi = preg_grep('!^'.addslashes($namespace.'\\').'!', $listClass);
foreach ($listApi as $apiClass) {
$restInterface->addClass($apiClass);
if (method_exists($apiClass, 'rest_include')) {
$apiClass::rest_load($restInterface);
}
}
}
public function process_request()
{
return $this->oInterface->handle();
}
}