PageRenderTime 31ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/aoliz/core/api/shop_api_object.php

http://phpfor.googlecode.com/
PHP | 346 lines | 235 code | 23 blank | 88 comment | 43 complexity | 338c53eba0ec3691b9ab3fb675c8c3da MD5 | raw file
  1. <?php
  2. /**
  3. * api ??
  4. * @package
  5. * @version 1.0:
  6. * @copyright 2003-2009 ShopEx
  7. * @author dreamdream
  8. * @license Commercial
  9. */
  10. class shop_api_object{
  11. var $data_format='xml';//????? ?????? 0,1 ?? xml,2 ?? json
  12. var $version='1.0';
  13. var $columns_split='|';//??????
  14. var $select_limited=20;//???????
  15. var $model_instance;
  16. var $gzip;
  17. var $application_error=array();
  18. /**
  19. * ????
  20. */
  21. function shop_api_object(){
  22. if(!$this->system){
  23. $this->system = &$GLOBALS['system'];
  24. }
  25. if(!$this->db){
  26. $this->db = $this->system->database();
  27. }
  28. }
  29. /**
  30. * API ??????
  31. * @param string link?????
  32. * @param string api???
  33. * @return api????
  34. */
  35. function load_api_instance($act,$api_version){
  36. if(!$this->method){
  37. $this->method=include(CORE_DIR.'/api/include/api_link.php');
  38. }
  39. $callmethod=$this->method[$act][$api_version];
  40. if($ctl=$callmethod['ctl']){
  41. if(!$this->model_instance[$ctl]){
  42. include_once(CORE_DIR.'/'.dirname($ctl).'/'.$api_version.'/'.basename($ctl).'.php');
  43. $ctl=basename($ctl);
  44. $this->model_instance[$ctl]=new $ctl;
  45. $this->model_instance[$ctl]->data_format=$this->data_format;
  46. }
  47. return $this->model_instance[$ctl];
  48. }
  49. }
  50. /**
  51. * ????
  52. * @param array ???
  53. * @author DreamDream
  54. * @return ????????????
  55. */
  56. function return_date($data){
  57. switch($this->data_format){
  58. case 'string':
  59. $result=print_r($data,true);
  60. break;
  61. case 'json':
  62. $this->_header('text/html');
  63. // ?uf8????unicode?? b2b 2010-02-26 18:34 wubin
  64. $data = function_exists('ucs_encode')? ucs_encode($data) : $data;
  65. $result= json_encode($data);
  66. break;
  67. case 'xml':
  68. $this->_header('text/xml');
  69. $xml=&$this->system->loadModel('utility/xml');
  70. $result= $xml->array2xml($data,'shopex');
  71. break;
  72. case 3:
  73. break;
  74. case 'soap':
  75. //soap
  76. break;
  77. default:
  78. $this->api_response('fail','language error',$data);
  79. break;
  80. }
  81. if($this->gzip && function_exists('gzencode')){
  82. echo @gzencode($result);
  83. }else{
  84. echo $result;
  85. }
  86. exit();
  87. }
  88. function varify_date_whole(&$data){
  89. $aData=$this->getColumns();
  90. foreach($data as $key=>$v){
  91. if($aData[$key]){
  92. $result[$key]=$v;
  93. unset($data[$key]);
  94. }
  95. }
  96. if($data){
  97. $this->api_response('fail','data fail',$data);
  98. }
  99. return $result;
  100. }
  101. /**
  102. * ???
  103. * @param string ????
  104. * @param string ????
  105. * @author DreamDream
  106. */
  107. function _header($content='text/html',$charset='utf-8'){
  108. header('Content-type: '.$content.';charset='.$charset);
  109. if($this->gzip && function_exists('gzencode')){
  110. header('Content-Encoding: gzip');
  111. }
  112. header("Cache-Control: no-cache,no-store , must-revalidate");
  113. $expires = gmdate ("D, d M Y H:i:s", time() + 20);
  114. header("Expires: " .$expires. " GMT");
  115. }
  116. /**
  117. * API LOG
  118. * @param log???
  119. * @param ????
  120. * @author DreamDream
  121. */
  122. function api_error_log($msg,$data){
  123. $path=HOME_DIR.'/logs';
  124. $handle=fopen($path.'/'.date("Ymd").'.log','a+');
  125. $content='data:'.date("Y m d H:i:s").print_r($data,true)."\r\n";
  126. fwrite($content);
  127. fclose($handle);
  128. }
  129. /**
  130. * ?????
  131. * @param ????
  132. * @param ?????
  133. * @author DreamDream
  134. * @return ????????
  135. */
  136. function _filter($where=array(1),$filter){
  137. $filter['pages']=$filter['pages']?intval($filter['pages']):1;
  138. $filter['counts']=$filter['counts']?intval($filter['counts']):$this->select_limited;
  139. if($filter['counts']>$this->select_limited){
  140. $filter['counts']=$this->select_limited;
  141. }
  142. $limit=' limit '.intval($filter['pages']-1)*$filter['counts'].','.$filter['counts'];
  143. if(count($where) > 1){
  144. $result=' where '.implode($where,' and ');
  145. }else{
  146. $result=' where '.$where[0];
  147. }
  148. if($filter['orderby']){
  149. $result.=' order by '.$filter['orderby'];
  150. }
  151. $result .= $limit;
  152. if(trim($result)=='where limit 0,100'){
  153. $result = $limit;
  154. }
  155. return $result;
  156. }
  157. function load_model($path,$apiversion='1.0'){
  158. $file=API_DIR.'/'.dirname($path).'/'.$apiversion.'/model/mdl.'.basename($path).'.php';
  159. if(file_exists($file)){
  160. require_once($file);
  161. $mdl_instalce='mdl_'.basename($path);
  162. if(!$this->model[$path]){
  163. $this->model[$path]=new $mdl_instalce;
  164. }else{
  165. return $this->model[$path];
  166. }
  167. }
  168. }
  169. /**
  170. * ????
  171. * @param ????
  172. * @param ???????
  173. * @author DreamDream
  174. * @return ???????
  175. */
  176. function verify_data(&$data,&$key_value){
  177. if($key_value['required']){
  178. foreach($key_value['required'] as $value){
  179. if(!isset($data[$value])){
  180. $this->api_response('fail','data fail');
  181. }
  182. }
  183. }
  184. if($key_value['columns']){
  185. if($data['columns']){
  186. $data['columns']=explode('|',$data['columns']);
  187. $_tmpcolumns="";
  188. $columns=$this->getColumns();
  189. foreach($data['columns'] as $key=>$v){
  190. if($columns[$v]){
  191. if($columns[$v]['join']){
  192. $data['columns_join'][$v]=true;
  193. unset($data['columns'][$key]);
  194. }
  195. if($columns[$v]['name']){
  196. $data['columns'][$key]=$columns[$v]['name'].' as '.$v;
  197. }
  198. }
  199. }
  200. }else{
  201. if(method_exists($this,'getColumns')){
  202. foreach((array)$this->getColumns() as $key=>$v){
  203. if($v['join']){
  204. $data['columns_join'][$key]=true;
  205. }else if($v['name']){
  206. $data['columns'][]=$v['name'].' as '.$key;
  207. }else{
  208. $data['columns'][]=$key;
  209. }
  210. }
  211. }
  212. }
  213. if(is_array($key_value['columns'])){
  214. foreach($key_value['columns'] as $value){
  215. if(!in_array($value,$data['columns'])){
  216. $this->api_response('fail','data fail');
  217. }
  218. }
  219. }
  220. }
  221. return true;
  222. }
  223. /*
  224. function application_error_handle($code,$debug,$info,$desc){
  225. if($debug){
  226. $this->app_error[$code]['debug']=$debug;
  227. }
  228. if($info){
  229. $this->app_error[$code]['info']=$info;
  230. }
  231. if($desc){
  232. $this->app_error[$code]['desc']=$desc;
  233. }
  234. return $this->app_error[$code];
  235. }
  236. */
  237. /**
  238. * API ???????
  239. * @param ???
  240. * @param ???????????
  241. * @param ????
  242. * @param ??????
  243. * @author DreamDream
  244. * @return ?????????????
  245. */
  246. function add_application_error($code,$debug,$info,$desc){
  247. if($debug){
  248. $this->app_error[$code]['debug']=$debug;
  249. }
  250. if($info){
  251. $this->app_error[$code]['info']=$info;
  252. }
  253. if($desc){
  254. $this->app_error[$code]['desc']=$desc;
  255. }
  256. if($this->app_error[$code]){
  257. $this->application_error[]=$this->app_error[$code];
  258. if($this->app_error[$code]['level']=='error'){
  259. $this->api_response('fail','data fail',null,'application error');
  260. }
  261. }
  262. }
  263. function api_erro_table($code){
  264. return $error($code);
  265. }
  266. /**
  267. * API ???
  268. * @param ???
  269. * @param ??????
  270. * @param ?????
  271. * @author DreamDream
  272. * @return ?????????????
  273. */
  274. function api_response($resCode,$errorCode=false,$data=null,$info=null){
  275. $resposilbe=array(
  276. 'true'=>'success',
  277. 'fail'=>'fail',
  278. 'wait'=>'wait'
  279. );
  280. if($errorCode){
  281. $error=$this->error_code($errorCode);
  282. if(constant('API_ERROR_LOG')){
  283. if(constant('API_ERROR_LOG_LEVEL')){
  284. if($error['level']>=API_ERROR_LOG_LEVEL){
  285. $this->api_error_log($error,$data);
  286. }
  287. }else{
  288. $this->api_error_log($error,$data);
  289. }
  290. }
  291. $result['result']=$resposilbe[$resCode];
  292. $result['msg']=$error['code'];
  293. $result['info']=$info?$info:$errorCode;
  294. }else{
  295. foreach($data['data_info'] as $key=>$value){
  296. if(!$data['data_info'][$key] && $data['data_info'][$key]!=='0' && $data['data_info'][$key]!==0){
  297. $data['data_info'][$key]='';
  298. }
  299. }
  300. $result['result']=$resposilbe[$resCode];
  301. $result['msg']='';
  302. $result['info']=$data;
  303. }
  304. if($this->application_error&&is_array($this->application_error)){
  305. $result['application_error']=$this->application_error;
  306. }
  307. echo $this->return_date($result);
  308. exit();
  309. }
  310. /**
  311. * API ???
  312. * @param ????
  313. * @author DreamDream
  314. * @return ??????
  315. */
  316. function &error_code($code){
  317. if($this->error){
  318. $this->error=include('include/api_error_handle.php');
  319. }
  320. return $this->error[$code];
  321. }
  322. }
  323. ?>