PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/components/Api.php

http://github.com/Shadez/Framework
PHP | 349 lines | 211 code | 51 blank | 87 comment | 39 complexity | 8228b2d39fe8fb4362ab91b65047ff72 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Copyright (C) 2011-2012 Shadez <https://github.com/Shadez/Framework>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. **/
  19. class Api extends Component
  20. {
  21. protected $m_apiInitialized = false;
  22. protected $m_apiMethods = array();
  23. protected $m_apiRequests = array();
  24. protected $m_apiResponse = array();
  25. protected $m_apiLevels = array();
  26. protected $m_apiDisabledMethods = array();
  27. protected $m_apiSignature = array();
  28. protected $m_apiErrorResponse = array(
  29. 'errno' => 1,
  30. 'errmsg' => 'Unable to run this method',
  31. );
  32. public function initialize()
  33. {
  34. if ($this->c('Config')->getValue('api.disabled'))
  35. {
  36. $this->m_apiErrorResponse = array(
  37. 'errno' => -999,
  38. 'errmsg' => 'API Feature was disabled on this site',
  39. );
  40. $this->m_apiInitialized = false;
  41. return $this;
  42. }
  43. require_once(APP_DIR . 'API.php');
  44. $this->m_apiMethods = array();
  45. $this->m_apiDisabledMethods = array();
  46. foreach($APIMethods as $type => $methods)
  47. {
  48. if (!$type || !$methods)
  49. continue;
  50. foreach ($methods as $method)
  51. {
  52. if (!$method)
  53. continue;
  54. $this->m_apiMethods[$method['request']] = $method;
  55. if ($method['disabled'])
  56. $this->m_apiDisabledMethods[] = $method;
  57. }
  58. }
  59. unset($APIMethods, $type, $methods, $method);
  60. $this->m_apiInitialized = true;
  61. return $this;
  62. }
  63. /**
  64. * Returns all available API methods defined in API methods holder
  65. * @return array
  66. **/
  67. public function getApiMethods()
  68. {
  69. return $this->m_apiMethods;
  70. }
  71. /**
  72. * Checks API user signature to grant access to some methods
  73. * @return bool
  74. **/
  75. public function checkSignature()
  76. {
  77. if (!$this->m_apiSignature)
  78. $this->m_apiSignature = $this->c('Events')->triggerEvent('onApiSignatureCheck', array('sig' => isset($_GET['apiSig']) ? $_GET['apiSig'] : ''), $this);
  79. return isset($this->m_apiSignature['sig']);
  80. }
  81. /**
  82. * Returns API user signature data
  83. * @return array
  84. **/
  85. public function getApiSignature()
  86. {
  87. return $this->m_apiSignature;
  88. }
  89. /**
  90. * Returns specific data of API user signature
  91. * @return mixed
  92. **/
  93. public function getApiSignatureData($data)
  94. {
  95. return isset($this->m_apiSignature[$data]) ? $this->m_apiSignature[$data] : false;
  96. }
  97. /**
  98. * Checks if provided method is allowed be be runned for current API user
  99. * @param string $method
  100. * @return bool
  101. **/
  102. public function isAllowedToRun($method)
  103. {
  104. if ($this->m_apiDisabledMethods)
  105. {
  106. foreach ($this->m_apiDisabledMethods as &$m)
  107. {
  108. if ($m['request'] == $method)
  109. return false;
  110. }
  111. }
  112. if (!$this->checkSignature())
  113. return false;
  114. return true;
  115. }
  116. /**
  117. * Returns API method's error response (or default if nothing provided)
  118. * @param int $errno = 0
  119. * @param string $errmsg = ''
  120. * @return array
  121. **/
  122. public function getErrResp($errno = 0, $errmsg = '')
  123. {
  124. $errresp = $this->m_apiErrorResponse;
  125. if (!$errno && !$errmsg)
  126. return $errresp;
  127. if ($errno != 0)
  128. $errresp['errno'] = $errno;
  129. if ($errmsg)
  130. $errresp['errmsg'] = $errmsg;
  131. return $errresp;
  132. }
  133. /**
  134. * Checks if provided method is a real method
  135. * @param string $method
  136. * @return bool
  137. **/
  138. protected function isApiMethod($method)
  139. {
  140. return isset($this->m_apiMethods[$method]);
  141. }
  142. /**
  143. * Returns method's type
  144. * @param string $method
  145. * @return mixed
  146. **/
  147. protected function getMethodType($method)
  148. {
  149. return $this->isApiMethod($method) ? $this->m_apiMethods[$method]['type'] : false;
  150. }
  151. /**
  152. * Returns method info by name
  153. * @param string $name
  154. * @return array
  155. **/
  156. protected function getMethod($method)
  157. {
  158. if ($this->isApiMethod($method))
  159. {
  160. $a = $this->m_apiMethods[$method];
  161. $a['apiSignature'] = $this->m_apiSignature;
  162. return $a;
  163. }
  164. return array();
  165. }
  166. /**
  167. * Tries to execute API method
  168. * @param string $method
  169. * @return array
  170. **/
  171. protected function runApi($method)
  172. {
  173. if (!$this->m_apiInitialized)
  174. return $this->m_apiErrorResponse;
  175. if (!$this->checkSignature())
  176. return $this->getErrResp(-2, 'Wrong API Signature provided');
  177. if (!$this->isApiMethod($method))
  178. return $this->getErrResp(2, 'Unknown method');
  179. if (!$this->isAllowedToRun($method))
  180. return $this->getErrResp();
  181. $apiData = array();
  182. $apiMethod = $this->getMethod($method);
  183. if (!$apiMethod)
  184. return $this->getErrResp(2, 'Unknown method');
  185. if ($apiMethod['disabled'])
  186. return $this->getErrResp(4, 'This method was disabled');
  187. if ($apiMethod['argc'] > 0)
  188. {
  189. if (isset($apiMethod['post']) && $apiMethod['post'])
  190. $holder = $_POST;
  191. else
  192. $holder = $_GET;
  193. foreach ($apiMethod['argk'] as $k => $t)
  194. {
  195. if (!isset($holder[$k]))
  196. return $this->getErrResp(3, 'Not enough actual parameters');
  197. if (is_string($holder[$k]))
  198. $apiData[$k] = addslashes(urldecode($holder[$k]));
  199. else
  200. $apiData[$k] = $holder[$k];
  201. switch ($t)
  202. {
  203. case 'int':
  204. $apiData[$k] = (int) $apiData[$k];
  205. break;
  206. case 'float':
  207. $apiData[$k] = (float) $apiData[$k];
  208. break;
  209. case 'double':
  210. $apiData[$k] = (double) $apiData[$k];
  211. break;
  212. case 'bool':
  213. if ($apiData[$k] == 'true')
  214. $apiData[$k] = true;
  215. elseif ($apiData[$k] == 'false')
  216. $apiData[$k] = false;
  217. break;
  218. case 'string':
  219. case 'array':
  220. default:
  221. break;
  222. }
  223. }
  224. }
  225. if (sizeof($apiData) != $apiMethod['argc'])
  226. return $this->getErrResp(3, 'Not enough actual parameters');
  227. if ($this->c('SiteApi')->isApiMethodImplemented($apiMethod['name']))
  228. $this->m_apiResponse = $this->c('SiteApi')->runApiMethod($apiMethod, $apiData);
  229. elseif (method_exists($this, 'apiMethod_' . $apiMethod['name']))
  230. {
  231. call_user_func_array(array($this, 'apiMethod_' . $apiMethod['name']), array(
  232. 'method' => $apiMethod,
  233. 'data' => $apiData
  234. ));
  235. }
  236. else
  237. $this->m_apiResponse = $this->getErrResp(2, 'Unknown method');
  238. if (!$this->m_apiResponse)
  239. $this->m_apiResponse = $this->getErrResp(2, 'Unknown method');
  240. if (!isset($this->m_apiResponse['errno']))
  241. $this->m_apiResponse['errno'] = 0;
  242. if (!isset($this->m_apiResponse['errmsg']))
  243. $this->m_apiResponse['errmsg'] = 'none';
  244. ksort($this->m_apiResponse);
  245. return $this->m_apiResponse;
  246. }
  247. /**
  248. * Public wrapper for Api_Component::runApi method
  249. * @param string $method
  250. * @returns array
  251. **/
  252. public function runApiMethod($method)
  253. {
  254. return $this->runApi($method);
  255. }
  256. /**
  257. * Handler for Core.getRawUrl API method. Listed as example
  258. * @param string $method
  259. * @param array $data
  260. * @return void
  261. **/
  262. protected function apiMethod_coregetrawurl($method, $data)
  263. {
  264. $this->m_apiResponse = array(
  265. 'rawUrl' => $this->getCore()->getRawUrl()
  266. );
  267. }
  268. /**
  269. * Handler for Core.getUrlAction API method. Listed as example
  270. * @param string $method
  271. * @param array $data
  272. * @return void
  273. **/
  274. protected function apiMethod_coregeturlaction($method, $data)
  275. {
  276. $this->m_apiResponse = array(
  277. 'action' => $this->getCore()->getUrlAction($data['idx'])
  278. );
  279. }
  280. /**
  281. * Handler for Core.getVersion API method. Listed as example
  282. * @param string $method
  283. * @param array $data
  284. * @return void
  285. **/
  286. protected function apiMethod_coregetversion($method, $data)
  287. {
  288. $this->m_apiResponse = array();
  289. if ($data['fullVersion'])
  290. $this->m_apiResponse['version'] = '0.5.12.2;0.5';
  291. else
  292. $this->m_apiResponse['version'] = '0.5';
  293. if ($data['info'])
  294. $this->m_apiResponse['info'] = 'Shadez Framework: Core Version 0.5.12.2, API Version 0.5';
  295. else
  296. $this->m_apiResponse['info'] = '';
  297. }
  298. };