/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
- <?php
- /**
- * @package Windwalker.Framework
- * @subpackage Component
- *
- * @copyright Copyright (C) 2012 Asikart. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE.txt
- * @author Generated by AKHelper - http://asikart.com
- */
- // no direct access
- defined('_JEXEC') or die;
- class AKRequestSDK extends JObject
- {
- protected $username = "" ;
-
- protected $password = "" ;
-
- protected static $instances ;
-
- public $host = '' ;
-
- public $uri = '' ;
-
- public $uri_cache = array() ;
-
- public $result = array() ;
-
- protected $relogin = false ;
-
- protected $session_key = '';
-
- protected $session_cache_path = '' ;
-
- protected $session_cache_file = '' ;
-
- public $isLogin = true ;
-
- public $forceJson = false ;
-
- /**
- * function __construct
- * @param
- */
- public function __construct($option)
- {
- $this->uri = new JURI() ;
- $this->setHost($option['host']);
-
- $ssl = JArrayHelper::getValue($option, 'ssl', false);
- $this->useSSL($ssl);
-
- // User Info
- $this->username = !empty($option['username']) ? $option['username'] : $this->username ;
- $this->password = !empty($option['password']) ? $option['password'] : $this->password ;
-
- // Get Session Key
- $this->session_cache_path = JPATH_ROOT.'/cache/AKRequestSDK' ;
- $this->session_cache_file = $this->session_cache_path.'/session_key' ;
-
- $this->session_key = $this->getSessionKey() ;
- }
-
- /**
- * function getInstance
- * @param
- */
- public static function getInstance($option)
- {
- $hash = AKHelper::_('system.uuid', $option['host'], 3);
-
- if(!empty(self::$instances[$hash])) {
- return $instances ;
- }
-
- self::$instances[$hash] = new AKRequestSDK($option);
-
- return self::$instances[$hash] ;
- }
-
- /**
- * function setHost
- * @param $host
- */
- public function setHost($host)
- {
- $uri = new JURI($host);
- $this->host = $host = $uri->getHost().$uri->getPath();
-
- $this->uri->setHost($host);
- }
-
- /**
- * function getHost
- * @param $host
- */
- public function getHost($host)
- {
- return $this->host;
- }
-
- /**
- * function useSSL
- * @param $ssl
- */
- public function useSSL($ssl)
- {
- if($ssl) {
- $this->uri->setScheme('https');
- }else{
- $this->uri->setScheme('http');
- }
- }
-
- /**
- * function execute
- * @param $uri
- */
- public function execute($path, $query = '', $method = 'get', $type = 'object')
- {
- // Set Session Key
- $query = (array)$query ;
- $query['session_key'] = $this->session_key ;
-
- // Do Execute
- $result = $this->doExecute($path, $query, $method, $type) ;
-
- // If not login or session expire, relogin.
- if( !$result ) {
-
- if($this->relogin) {
-
- // Do Login
- $login_result = $this->login();
-
- if( !$login_result ) {
- return false ;
- }
-
- $this->isLogin = true ;
-
- // Reset session
- $query['session_key'] = $this->session_key = $login_result->session_key;
-
- // Write in cache file
- JFile::write( $this->session_cache_file, $this->session_key );
-
- // Debug ------------
- AKHelper::_('system.mark', 'New session_key: '. $this->session_key, 'WindWalker') ;
- // ------------------
-
- // Resend Request
- $result = $this->doExecute($path, $query, $method, $type) ;
- }
- }
-
- return $result ;
- }
-
- /**
- * function doExecute
- * @param
- */
- public function doExecute($path, $query = '', $method = 'get', $type = 'object', $ignore_cache = false)
- {
- if(!$this->isLogin) {
- return false ;
- }
-
- // Set URI Path
- $uri = $this->uri ;
- $uri->setPath('/'.trim($path, '/'));
- $uri->setQuery(array());
-
- // Add json format in Debug mode.
- if(AKDEBUG || $this->forceJson)
- {
- $query['format'] = 'json';
- }
-
- // Set Query
- if($method == 'post') {
- $query = $this->buildAPIQuery($query, false) ;
- }else{
- $query = $this->buildAPIQuery($query, false) ;
- $uri->setQuery($query);
- }
-
- // Set Cache
- $key = AKHelper::_('system.uuid', (string)$uri, 3) ;
- if( isset( $this->result[$key] ) && !$ignore_cache) {
- return $this->handleResult($this->result[$key], $type) ;
- }
-
- // Send Request By CURL
- $result = AKHelper::_('curl.getPage', (string)$uri, $method, $query) ;
-
- // Debug ------------
- $this->i++ ;
- $query = $this->buildAPIQuery($query, true);
- $query = $query ? '?'.$query : '' ;
- $query = $method == 'post' ? $query : '' ;
- AKHelper::_('system.mark', "Send {$this->i} ({$method}): ".(string)$uri , 'WindWalker') ;
- // ------------------
-
- if(!$result) {
- $this->setError(AKHelper::_('curl.getError'));
- return false ;
- }
-
- $this->result[$key] = $result ;
-
- return $this->handleResult($this->result[$key], $type) ;
- }
-
- /**
- * function handleResult
- */
- public function handleResult($data, $type = 'object')
- {
- $result = json_decode($data) ;
-
- // is json format?
- if($result === null) {
- $this->setError('Return string not JSON format.');
- return false ;
- }
-
- // Detect Error message
- if( !isset($result->ApiResult) ){
-
- if( isset($result->ApiError->errorMsg) ) {
- $this->setError($result->ApiError->errorMsg);
-
- // If 403, need relogin.
- if($result->ApiError->errorNum == 403) {
- if($this->relogin){
- $this->isLogin = false ;
- }
-
- $this->relogin = true ;
- }
-
- return false ;
- }else{
- $this->setError('API System return no result.');
- return false ;
- }
-
- }
-
- // Set return type.
- if($type == 'array') {
- $result = json_decode($data, true);
- return $result['ApiResult'] ;
- }elseif($type == 'raw'){
- return $data ;
- }else{
- return $result->ApiResult ;
- }
- }
-
- /**
- * function getSessionKey
- * @param
- */
- public function getSessionKey()
- {
- // read session key from cache file
- $cache_path = $this->session_cache_path ;
- $cache_file = $this->session_cache_file ;
-
- if( !JFile::exists($cache_file) ) {
- $content = '' ;
- JFolder::create($cache_path);
- JFile::write($cache_file, $content) ;
- }
-
- $session_key = JFile::read($cache_file) ;
-
- // Debug
- AKHelper::_('system.mark', 'session_key: '. $session_key, 'WindWalker') ;
-
- if(!$session_key) {
-
- // Login
- $result = $this->login();
-
- if(!$result) {
- $this->setError('Need login.');
- $this->isLogin = false ;
- return false ;
- }
-
- if(!$result->success) {
- $this->setError('Need login.');
- $this->isLogin = false ;
- return false ;
- }
-
- // Write in cache file
- JFile::write( $cache_file, $result->session_key );
- $session_key = $result->session_key ;
- }
-
- return $session_key ;
- }
-
- /**
- * function login
- * @param
- */
- public function login()
- {
- $uriQuery['username'] = $this->username;
- $uriQuery['password'] = $this->password;
-
- // Do execute
- $result = $this->doExecute('/user/login', $uriQuery, 'post') ;
-
- return $result ;
- }
-
- /**
- * function getURI
- * @param
- */
- public function getURI()
- {
- return $this->uri ;
- }
-
- /**
- * function setVar
- * @param $key
- */
- public function setVar($name, $value)
- {
- $this->uri->setVar($name, $value);
- }
-
- /**
- * function getVar
- * @param $key
- */
- public function getVar($name, $default = null)
- {
- $this->uri->getVar($name, $default);
- }
-
- /**
- * function delVar
- * @param $key
- */
- public function delVar($name)
- {
- $this->uri->delVar($name);
- }
-
- /**
- * function setQuery
- * @param $queries
- */
- public function setQuery($queries)
- {
- $this->uri->setQuery($queries);
- }
-
- /**
- * function hash
- * @param $datas
- */
- public static function hash($datas)
- {
- $datas = (array) $datas ;
- $datas = implode('', $datas) ;
-
- return md5($datas);
- }
-
- /**
- * function buildAPIQuery
- * @param $array
- */
- public function buildAPIQuery($array, $string = true)
- {
- // Remove empty values
- foreach( $array as $key => &$val ):
- if($val === '' || JString::substr($key, 0, 1) == '_') {
- unset( $array[$key] );
- }
-
- if( is_array($val) || is_object($val) ) {
-
- $val = (array) $val ;
-
- foreach( $val as $key2 => &$val2 ):
-
- if( $val2 === '' || JString::substr($key2, 0, 1) == '_') {
- unset( $array[$key][$key2] ) ;
- }
-
- endforeach;
-
- }
-
- endforeach;
-
- if($string){
- $array = http_build_query($array);
- }
-
- return $array ;
- }
- }