My.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 34000 2013-09-17 08:52:48Z nemohou $
  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. global $_G;
  30. $this->siteId = $siteId;
  31. $this->siteKey = $siteKey ? $siteKey : $_G['setting']['siteuniqueid'];
  32. $this->timezone = $timezone;
  33. $this->version = $version;
  34. $this->charset = $charset;
  35. $this->language = $language;
  36. $this->myAppStatus = $myAppStatus;
  37. $this->mySearchStatus = $mySearchStatus;
  38. }
  39. public function run() {
  40. try {
  41. $this->_checkRequest();
  42. $response = $this->_processServerRequest();
  43. } catch (Exception $e) {
  44. $response = new Cloud_Service_Server_ErrorResponse($e->getCode(), $e->getMessage());
  45. }
  46. @ob_end_clean();
  47. if(function_exists('ob_gzhandler')) {
  48. @ob_start('ob_gzhandler');
  49. } else {
  50. @ob_start();
  51. }
  52. echo serialize($this->_formatLocalResponse($response));
  53. exit;
  54. }
  55. protected function _checkRequest() {
  56. global $_G;
  57. if (empty($_G['setting']['siteuniqueid'])) {
  58. throw new Cloud_Service_Server_RestfulException('Client SiteKey NOT Exists', 11);
  59. } elseif (empty($this->siteKey)) {
  60. throw new Cloud_Service_Server_RestfulException('My SiteKey NOT Exists', 12);
  61. }
  62. }
  63. protected function _processServerRequest() {
  64. $request = $_POST;
  65. $module = $request['module'];
  66. $method = $request['method'];
  67. $params = $request['params'];
  68. if (!$module || !$method) {
  69. throw new Cloud_Service_Server_RestfulException('Invalid Method: ' . $method, 1);
  70. }
  71. $siteKey = $this->siteKey;
  72. if ($request['ptnId']) {
  73. $siteKey = md5($this->siteId . $this->siteKey . $request['ptnId'] . $request['ptnMethods']);
  74. }
  75. $sign = $this->_generateSign($module, $method, $params, $siteKey);
  76. if ($sign != $request['sign']) {
  77. throw new Cloud_Service_Server_RestfulException('Error Sign', 10);
  78. }
  79. if ($request['ptnId']) {
  80. if ($allowMethods = explode(',', $request['ptnMethods'])) {
  81. $manyouHelper = Cloud::loadClass('Service_ManyouHelper');
  82. if (!in_array($manyouHelper->getMethodCode($module, $method), $allowMethods)) {
  83. throw new Cloud_Service_Server_RestfulException('Method Not Allowed', 13);
  84. }
  85. }
  86. }
  87. $params = dunserialize($params);
  88. return $this->_callLocalMethod($module, $method, $params);
  89. }
  90. protected function _generateSign($module, $method, $params, $siteKey) {
  91. return md5($module . '|' . $method . '|' . $params . '|' . $siteKey);
  92. }
  93. protected function _callLocalMethod($module, $method, $params) {
  94. if ($module == 'Batch' && $method == 'run') {
  95. $response = array();
  96. foreach($params as $param) {
  97. try {
  98. $response[] = $this->_callLocalMethod($param['module'], $param['method'], $param['params']);
  99. } catch (Exception $e) {
  100. $response[] = new Cloud_Service_Server_ErrorResponse($e->getCode, $e->getMessage());
  101. }
  102. }
  103. return new Cloud_Service_Server_Response($response, 'Batch');
  104. } else {
  105. $methodName = $this->_getMethodName($module, $method);
  106. $className = sprintf('Cloud_Service_Server_%s', ucfirst($module));
  107. try {
  108. $class = Cloud::loadClass($className);
  109. } catch (Exception $e) {
  110. throw new Cloud_Service_Server_RestfulException('Class not implemented: ' . $className, 2);
  111. }
  112. if (method_exists($class, $methodName)) {
  113. $result = call_user_func_array(array(&$class, $methodName), (array)$params);
  114. if ($result instanceof Cloud_Service_Server_ErrorResponse) {
  115. return $result;
  116. }
  117. return new Cloud_Service_Server_Response($result);
  118. } else {
  119. throw new Cloud_Service_Server_RestfulException('Method not implemented: ' . $methodName, 2);
  120. }
  121. }
  122. }
  123. protected function _getMethodName($module, $method) {
  124. return 'on' . ucfirst($module) . ucfirst($method);
  125. }
  126. protected function _formatLocalResponse($data) {
  127. $utilService = Cloud::loadClass('Service_Util');
  128. $res = array(
  129. 'my_version' => $utilService->getApiVersion(),
  130. 'timezone' => $this->timezone,
  131. 'version' => $this->version,
  132. 'charset' => $this->charset,
  133. 'language' => $this->language
  134. );
  135. if ($data instanceof Cloud_Service_Server_Response) {
  136. if (is_array($data->result) && $data->getMode() == 'Batch') {
  137. foreach($data->result as $result) {
  138. if ($result instanceof Cloud_Service_Server_Response) {
  139. $res['result'][] = $result->getResult();
  140. } else {
  141. $res['result'][] = array(
  142. 'errno' => $result->getCode(),
  143. 'errmsg' => $result->getMessage()
  144. );
  145. }
  146. }
  147. } else {
  148. $res['result'] = $data->getResult();
  149. }
  150. } else {
  151. $res['errCode'] = $data->getCode();
  152. $res['errMessage'] = $data->getMessage();
  153. }
  154. return $res;
  155. }
  156. }