PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/windwalker/component/api/sdk.php

https://bitbucket.org/asikart-extension/joomclouds-client
PHP | 416 lines | 224 code | 88 blank | 104 comment | 36 complexity | 7b52642169a9171cc5568005ac81e185 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Windwalker.Framework
  4. * @subpackage Component
  5. *
  6. * @copyright Copyright (C) 2012 Asikart. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. * @author Generated by AKHelper - http://asikart.com
  9. */
  10. // no direct access
  11. defined('_JEXEC') or die;
  12. class AKRequestSDK extends JObject
  13. {
  14. protected $username = "" ;
  15. protected $password = "" ;
  16. protected static $instances ;
  17. public $host = '' ;
  18. public $uri = '' ;
  19. public $uri_cache = array() ;
  20. public $result = array() ;
  21. protected $relogin = false ;
  22. protected $session_key = '';
  23. protected $session_cache_path = '' ;
  24. protected $session_cache_file = '' ;
  25. public $isLogin = true ;
  26. public $forceJson = false ;
  27. /**
  28. * function __construct
  29. * @param
  30. */
  31. public function __construct($option)
  32. {
  33. $this->uri = new JURI() ;
  34. $this->setHost($option['host']);
  35. $ssl = JArrayHelper::getValue($option, 'ssl', false);
  36. $this->useSSL($ssl);
  37. // User Info
  38. $this->username = !empty($option['username']) ? $option['username'] : $this->username ;
  39. $this->password = !empty($option['password']) ? $option['password'] : $this->password ;
  40. // Get Session Key
  41. $this->session_cache_path = JPATH_ROOT.'/cache/AKRequestSDK' ;
  42. $this->session_cache_file = $this->session_cache_path.'/session_key' ;
  43. $this->session_key = $this->getSessionKey() ;
  44. }
  45. /**
  46. * function getInstance
  47. * @param
  48. */
  49. public static function getInstance($option)
  50. {
  51. $hash = AKHelper::_('system.uuid', $option['host'], 3);
  52. if(!empty(self::$instances[$hash])) {
  53. return $instances ;
  54. }
  55. self::$instances[$hash] = new AKRequestSDK($option);
  56. return self::$instances[$hash] ;
  57. }
  58. /**
  59. * function setHost
  60. * @param $host
  61. */
  62. public function setHost($host)
  63. {
  64. $uri = new JURI($host);
  65. $this->host = $host = $uri->getHost().$uri->getPath();
  66. $this->uri->setHost($host);
  67. }
  68. /**
  69. * function getHost
  70. * @param $host
  71. */
  72. public function getHost($host)
  73. {
  74. return $this->host;
  75. }
  76. /**
  77. * function useSSL
  78. * @param $ssl
  79. */
  80. public function useSSL($ssl)
  81. {
  82. if($ssl) {
  83. $this->uri->setScheme('https');
  84. }else{
  85. $this->uri->setScheme('http');
  86. }
  87. }
  88. /**
  89. * function execute
  90. * @param $uri
  91. */
  92. public function execute($path, $query = '', $method = 'get', $type = 'object')
  93. {
  94. // Set Session Key
  95. $query = (array)$query ;
  96. $query['session_key'] = $this->session_key ;
  97. // Do Execute
  98. $result = $this->doExecute($path, $query, $method, $type) ;
  99. // If not login or session expire, relogin.
  100. if( !$result ) {
  101. if($this->relogin) {
  102. // Do Login
  103. $login_result = $this->login();
  104. if( !$login_result ) {
  105. return false ;
  106. }
  107. $this->isLogin = true ;
  108. // Reset session
  109. $query['session_key'] = $this->session_key = $login_result->session_key;
  110. // Write in cache file
  111. JFile::write( $this->session_cache_file, $this->session_key );
  112. // Debug ------------
  113. AKHelper::_('system.mark', 'New session_key: '. $this->session_key, 'WindWalker') ;
  114. // ------------------
  115. // Resend Request
  116. $result = $this->doExecute($path, $query, $method, $type) ;
  117. }
  118. }
  119. return $result ;
  120. }
  121. /**
  122. * function doExecute
  123. * @param
  124. */
  125. public function doExecute($path, $query = '', $method = 'get', $type = 'object', $ignore_cache = false)
  126. {
  127. if(!$this->isLogin) {
  128. return false ;
  129. }
  130. // Set URI Path
  131. $uri = $this->uri ;
  132. $uri->setPath('/'.trim($path, '/'));
  133. $uri->setQuery(array());
  134. // Add json format in Debug mode.
  135. if(AKDEBUG || $this->forceJson)
  136. {
  137. $query['format'] = 'json';
  138. }
  139. // Set Query
  140. if($method == 'post') {
  141. $query = $this->buildAPIQuery($query, false) ;
  142. }else{
  143. $query = $this->buildAPIQuery($query, false) ;
  144. $uri->setQuery($query);
  145. }
  146. // Set Cache
  147. $key = AKHelper::_('system.uuid', (string)$uri, 3) ;
  148. if( isset( $this->result[$key] ) && !$ignore_cache) {
  149. return $this->handleResult($this->result[$key], $type) ;
  150. }
  151. // Send Request By CURL
  152. $result = AKHelper::_('curl.getPage', (string)$uri, $method, $query) ;
  153. // Debug ------------
  154. $this->i++ ;
  155. $query = $this->buildAPIQuery($query, true);
  156. $query = $query ? '?'.$query : '' ;
  157. $query = $method == 'post' ? $query : '' ;
  158. AKHelper::_('system.mark', "Send {$this->i} ({$method}): ".(string)$uri , 'WindWalker') ;
  159. // ------------------
  160. if(!$result) {
  161. $this->setError(AKHelper::_('curl.getError'));
  162. return false ;
  163. }
  164. $this->result[$key] = $result ;
  165. return $this->handleResult($this->result[$key], $type) ;
  166. }
  167. /**
  168. * function handleResult
  169. */
  170. public function handleResult($data, $type = 'object')
  171. {
  172. $result = json_decode($data) ;
  173. // is json format?
  174. if($result === null) {
  175. $this->setError('Return string not JSON format.');
  176. return false ;
  177. }
  178. // Detect Error message
  179. if( !isset($result->ApiResult) ){
  180. if( isset($result->ApiError->errorMsg) ) {
  181. $this->setError($result->ApiError->errorMsg);
  182. // If 403, need relogin.
  183. if($result->ApiError->errorNum == 403) {
  184. if($this->relogin){
  185. $this->isLogin = false ;
  186. }
  187. $this->relogin = true ;
  188. }
  189. return false ;
  190. }else{
  191. $this->setError('API System return no result.');
  192. return false ;
  193. }
  194. }
  195. // Set return type.
  196. if($type == 'array') {
  197. $result = json_decode($data, true);
  198. return $result['ApiResult'] ;
  199. }elseif($type == 'raw'){
  200. return $data ;
  201. }else{
  202. return $result->ApiResult ;
  203. }
  204. }
  205. /**
  206. * function getSessionKey
  207. * @param
  208. */
  209. public function getSessionKey()
  210. {
  211. // read session key from cache file
  212. $cache_path = $this->session_cache_path ;
  213. $cache_file = $this->session_cache_file ;
  214. if( !JFile::exists($cache_file) ) {
  215. $content = '' ;
  216. JFolder::create($cache_path);
  217. JFile::write($cache_file, $content) ;
  218. }
  219. $session_key = JFile::read($cache_file) ;
  220. // Debug
  221. AKHelper::_('system.mark', 'session_key: '. $session_key, 'WindWalker') ;
  222. if(!$session_key) {
  223. // Login
  224. $result = $this->login();
  225. if(!$result) {
  226. $this->setError('Need login.');
  227. $this->isLogin = false ;
  228. return false ;
  229. }
  230. if(!$result->success) {
  231. $this->setError('Need login.');
  232. $this->isLogin = false ;
  233. return false ;
  234. }
  235. // Write in cache file
  236. JFile::write( $cache_file, $result->session_key );
  237. $session_key = $result->session_key ;
  238. }
  239. return $session_key ;
  240. }
  241. /**
  242. * function login
  243. * @param
  244. */
  245. public function login()
  246. {
  247. $uriQuery['username'] = $this->username;
  248. $uriQuery['password'] = $this->password;
  249. // Do execute
  250. $result = $this->doExecute('/user/login', $uriQuery, 'post') ;
  251. return $result ;
  252. }
  253. /**
  254. * function getURI
  255. * @param
  256. */
  257. public function getURI()
  258. {
  259. return $this->uri ;
  260. }
  261. /**
  262. * function setVar
  263. * @param $key
  264. */
  265. public function setVar($name, $value)
  266. {
  267. $this->uri->setVar($name, $value);
  268. }
  269. /**
  270. * function getVar
  271. * @param $key
  272. */
  273. public function getVar($name, $default = null)
  274. {
  275. $this->uri->getVar($name, $default);
  276. }
  277. /**
  278. * function delVar
  279. * @param $key
  280. */
  281. public function delVar($name)
  282. {
  283. $this->uri->delVar($name);
  284. }
  285. /**
  286. * function setQuery
  287. * @param $queries
  288. */
  289. public function setQuery($queries)
  290. {
  291. $this->uri->setQuery($queries);
  292. }
  293. /**
  294. * function hash
  295. * @param $datas
  296. */
  297. public static function hash($datas)
  298. {
  299. $datas = (array) $datas ;
  300. $datas = implode('', $datas) ;
  301. return md5($datas);
  302. }
  303. /**
  304. * function buildAPIQuery
  305. * @param $array
  306. */
  307. public function buildAPIQuery($array, $string = true)
  308. {
  309. // Remove empty values
  310. foreach( $array as $key => &$val ):
  311. if($val === '' || JString::substr($key, 0, 1) == '_') {
  312. unset( $array[$key] );
  313. }
  314. if( is_array($val) || is_object($val) ) {
  315. $val = (array) $val ;
  316. foreach( $val as $key2 => &$val2 ):
  317. if( $val2 === '' || JString::substr($key2, 0, 1) == '_') {
  318. unset( $array[$key][$key2] ) ;
  319. }
  320. endforeach;
  321. }
  322. endforeach;
  323. if($string){
  324. $array = http_build_query($array);
  325. }
  326. return $array ;
  327. }
  328. }