PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/api/manyou/Service/Server/My.php

https://github.com/kuaileshike/upload
PHP | 185 lines | 149 code | 30 blank | 6 comment | 29 complexity | 7a95aee75eab785c11547426f2ff7199 MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: My.php 29713 2012-04-26 01:51:38Z yexinhao $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. Cloud::loadFile('Service_Server_Restful');
  12. class Cloud_Service_Server_My extends Cloud_Service_Server_Restful {
  13. public $siteId;
  14. public $siteKey;
  15. public $timezone;
  16. public $version;
  17. public $charset;
  18. public $language;
  19. public $myAppStatus;
  20. public $mySearchStatus;
  21. protected static $_instance;
  22. public static function getInstance($siteId, $siteKey, $timezone, $version, $charset, $language, $myAppStatus, $mySearchStatus) {
  23. if (!(self::$_instance instanceof self)) {
  24. self::$_instance = new self($siteId, $siteKey, $timezone, $version, $charset, $language, $myAppStatus, $mySearchStatus);
  25. }
  26. return self::$_instance;
  27. }
  28. public function __construct($siteId, $siteKey, $timezone, $version, $charset, $language, $myAppStatus, $mySearchStatus) {
  29. $this->siteId = $siteId;
  30. $this->siteKey = $siteKey;
  31. $this->timezone = $timezone;
  32. $this->version = $version;
  33. $this->charset = $charset;
  34. $this->language = $language;
  35. $this->myAppStatus = $myAppStatus;
  36. $this->mySearchStatus = $mySearchStatus;
  37. }
  38. public function run() {
  39. try {
  40. $this->_checkRequest();
  41. $response = $this->_processServerRequest();
  42. } catch (Exception $e) {
  43. $response = new Cloud_Service_Server_ErrorResponse($e->getCode(), $e->getMessage());
  44. }
  45. @ob_end_clean();
  46. if(function_exists('ob_gzhandler')) {
  47. @ob_start('ob_gzhandler');
  48. } else {
  49. @ob_start();
  50. }
  51. echo serialize($this->_formatLocalResponse($response));
  52. exit;
  53. }
  54. protected function _checkRequest() {
  55. global $_G;
  56. if (empty($_G['setting']['siteuniqueid'])) {
  57. throw new Cloud_Service_Server_RestfulException('Client SiteKey NOT Exists', 11);
  58. } elseif (empty($this->siteKey)) {
  59. throw new Cloud_Service_Server_RestfulException('My SiteKey NOT Exists', 12);
  60. }
  61. }
  62. protected function _processServerRequest() {
  63. $request = $_POST;
  64. $module = $request['module'];
  65. $method = $request['method'];
  66. $params = $request['params'];
  67. if (!$module || !$method) {
  68. throw new Cloud_Service_Server_RestfulException('Invalid Method: ' . $method, 1);
  69. }
  70. $siteKey = $this->siteKey;
  71. if ($request['ptnId']) {
  72. $siteKey = md5($this->siteId . $this->siteKey . $request['ptnId'] . $request['ptnMethods']);
  73. }
  74. $sign = $this->_generateSign($module, $method, $params, $siteKey);
  75. if ($sign != $request['sign']) {
  76. throw new Cloud_Service_Server_RestfulException('Error Sign', 10);
  77. }
  78. if ($request['ptnId']) {
  79. if ($allowMethods = explode(',', $request['ptnMethods'])) {
  80. $manyouHelper = Cloud::loadClass('Service_ManyouHelper');
  81. if (!in_array($manyouHelper->getMethodCode($module, $method), $allowMethods)) {
  82. throw new Cloud_Service_Server_RestfulException('Method Not Allowed', 13);
  83. }
  84. }
  85. }
  86. $params = dunserialize($params);
  87. return $this->_callLocalMethod($module, $method, $params);
  88. }
  89. protected function _generateSign($module, $method, $params, $siteKey) {
  90. return md5($module . '|' . $method . '|' . $params . '|' . $siteKey);
  91. }
  92. protected function _callLocalMethod($module, $method, $params) {
  93. if ($module == 'Batch' && $method == 'run') {
  94. $response = array();
  95. foreach($params as $param) {
  96. try {
  97. $response[] = $this->_callLocalMethod($param['module'], $param['method'], $param['params']);
  98. } catch (Exception $e) {
  99. $response[] = new Cloud_Service_Server_ErrorResponse($e->getCode, $e->getMessage());
  100. }
  101. }
  102. return new Cloud_Service_Server_Response($response, 'Batch');
  103. } else {
  104. $methodName = $this->_getMethodName($module, $method);
  105. $className = sprintf('Cloud_Service_Server_%s', ucfirst($module));
  106. try {
  107. $class = Cloud::loadClass($className);
  108. } catch (Exception $e) {
  109. throw new Cloud_Service_Server_RestfulException('Class not implemented: ' . $className, 2);
  110. }
  111. if (method_exists($class, $methodName)) {
  112. $result = call_user_func_array(array(&$class, $methodName), $params);
  113. if ($result instanceof Cloud_Service_Server_ErrorResponse) {
  114. return $result;
  115. }
  116. return new Cloud_Service_Server_Response($result);
  117. } else {
  118. throw new Cloud_Service_Server_RestfulException('Method not implemented: ' . $methodName, 2);
  119. }
  120. }
  121. }
  122. protected function _getMethodName($module, $method) {
  123. return 'on' . ucfirst($module) . ucfirst($method);
  124. }
  125. protected function _formatLocalResponse($data) {
  126. $utilService = Cloud::loadClass('Service_Util');
  127. $res = array(
  128. 'my_version' => $utilService->getApiVersion(),
  129. 'timezone' => $this->timezone,
  130. 'version' => $this->version,
  131. 'charset' => $this->charset,
  132. 'language' => $this->language
  133. );
  134. if ($data instanceof Cloud_Service_Server_Response) {
  135. if (is_array($data->result) && $data->getMode() == 'Batch') {
  136. foreach($data->result as $result) {
  137. if ($result instanceof Cloud_Service_Server_Response) {
  138. $res['result'][] = $result->getResult();
  139. } else {
  140. $res['result'][] = array(
  141. 'errno' => $result->getCode(),
  142. 'errmsg' => $result->getMessage()
  143. );
  144. }
  145. }
  146. } else {
  147. $res['result'] = $data->getResult();
  148. }
  149. } else {
  150. $res['errCode'] = $data->getCode();
  151. $res['errMessage'] = $data->getMessage();
  152. }
  153. return $res;
  154. }
  155. }