/
rengine.class.php
165 lines (165 loc) · 6.39 KB
/
rengine.class.php
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace rengine;
class api{
private $endpoint, $user, $pass;
private $appId, $modId, $extId, $filecache;
private $errorFunc, $lastError = Array('code' => 0, 'msg' => null);
public $enableCache = true, $internalErrors = false;
function __construct($endpoint, $user, $password){
$this->endpoint = $endpoint;
$this->user = $user;
$this->pass = md5($password);
$this->filecache = new filecache();
$this->filecache->cacheDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'rengine'.DIRECTORY_SEPARATOR;
return true;
}
function setCacheDir($dir){
if(!is_writable($dir)){
throw new \Exception("Cache directory '".$dir."' is not writable!");
return false;
}
$this->filecache->cacheDir = $dir;
return true;
}
function clearCache(){
if(is_dir($this->filecache->cacheDir)){
return $this->recursiveDelete($this->filecache->cacheDir);
}
return true;
}
function app($app){
return $this->application($app);
}
function application($app){
$this->appId = $app;
$this->modId = null;
$this->extId = null;
return $this;
}
function mod($mod){
return $this->module($mod);
}
function module($mod){
$this->modId = $mod;
$this->extId = null;
return $this;
}
function ext($ext){
return $this->extension($ext);
}
function extension($ext){
$this->extId = $ext;
return $this;
}
function __call($func, $params){
$retorno = false;
// REALIZAR CONSULTA NO CACHE
$cacheHash = '';
$cacheHash .= ($this->appId)? $this->appId.'-' : '';
$cacheHash .= ($this->modId)? $this->modId.'-' : '';
$cacheHash .= ($this->extId)? $this->extId.'-' : '';
$cacheHash .= $func.'-'.md5($this->endpoint.json_encode($params));
if($this->enableCache && $retorno = $this->filecache->cache_get($cacheHash, $cacheChangeTime)){
if(isset($retorno['cache_lifetime'])){
if((time() - $cacheChangeTime) > $retorno['cache_lifetime']){
$this->filecache->cache_unset($cacheHash);
$retorno = false;
}
}else{
$retorno = false;
}
}
// REALIZAR CONSULTA NO ENDPOINT
if(!$retorno){
if($params){
foreach($params as $key => $value){
$params[$key] = @json_encode($value);
}
}
$params['app'] = $this->appId;
$params['mod'] = $this->modId;
$params['ext'] = $this->extId;
$params['func'] = $func;
if($retorno = $this->request($this->endpoint, $params, $httpCode)){
if($retornoDecoded = json_decode($retorno, true)){
$retorno = $retornoDecoded;
// SALVAR CONSULTA EM CACHE
if($this->enableCache && isset($retorno['cache_lifetime']) && $retorno['cache_lifetime']){
$this->filecache->cache_set($cacheHash, $retorno);
}
}else{
return $this->throwError(null, 'Invalid Endpoint Result: HTTP Response Code: '.$httpCode.' Message: '.$retorno);
}
}
}
// RETORNAR O RESULTADO / ERRO
if($retorno){
if(isset($retorno['status']) && $retorno['status'] == 'ok' && array_key_exists('response', $retorno)){// OK
return $retorno['response'];
}elseif(isset($retorno['status']) && $retorno['status'] == 'error' && isset($retorno['error']['message'])){
return $this->throwError($retorno['error']['code'], $retorno['error']['message']);
}else{
$eMessage = (isset($retorno['error']['message']))? $retorno['error']['message'] : '';
$eMessage .= (isset($retorno['status']))? '(Status: '.$retorno['status'].')': '';
$eMessage .= (isset($retorno['response']))? 'Response: '.$retorno['response']: '';
$eCode = (isset($retorno['error']['code']))? $retorno['error']['code'] : 'n/a';
return $this->throwError($eCode, 'Unknown Error['.$eCode.']: '.$eMessage);
}
}else{
return $this->throwError(null, "Invalid Endpoint: ".$this->endpoint);
}
}
private function throwError($code, $message){
$lastError = Array('code' => $code, 'msg' => $message);
if($this->errorFunc && is_callable($this->errorFunc)){
call_user_func($this->errorFunc, $code, $message);
}
if(!$this->internalErrors){
throw new \Exception($message, $code);
}
return false;
}
function errorHandler($function){
if(is_callable($function)){
$this->errorFunc = $function;
return true;
}else{
$this->errorFunc = null;
return false;
}
}
private function request($endpoint, $data, &$httpCode = 0){
$data['user'] = $this->user;
$data['pass'] = $this->pass;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
$retorno = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $retorno;
}
private function recursiveDelete($dir){
if (!file_exists($dir)){return true;}
if (!is_dir($dir) || is_link($dir)){
return @unlink($dir);
}
foreach (scandir($dir) as $item){
if ($item == '.' || $item == '..'){continue;}
if (!$this->recursiveDelete($dir . "/" . $item, false)){
@chmod($dir . "/" . $item, 0777);
if(!$this->recursiveDelete($dir . "/" . $item, false)) return false;
};
}
@chmod($dir, 0777);
return @rmdir($dir);
}
}