zen.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * The zen file of api module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Shujie Tian<tianshujie@easycorp.ltd>
  8. * @package api
  9. * @link https://www.zentao.net
  10. */
  11. class apiZen extends api
  12. {
  13. /**
  14. * 解析cookie里的docSpaceParam值。
  15. * Parse docSpaceParam cookie.
  16. *
  17. * @param array $libs
  18. * @param int $libID
  19. * @param string $type
  20. * @param int $objectID
  21. * @param int $moduleID
  22. * @param string $spaceType
  23. * @param int $release
  24. * @access protected
  25. * @return void
  26. */
  27. protected function parseDocSpaceParam($libs, $libID, $type, $objectID, $moduleID, $spaceType, $release)
  28. {
  29. if($this->cookie->docSpaceParam) $docParam = json_decode($this->cookie->docSpaceParam);
  30. if(isset($docParam) && !(in_array($docParam->type, array('product', 'project')) && $docParam->objectID == 0))
  31. {
  32. $type = $docParam->type;
  33. $objectID = $docParam->objectID;
  34. $libID = $docParam->libID;
  35. $moduleID = $docParam->moduleID;
  36. $browseType = $docParam->browseType;
  37. $param = $docParam->param;
  38. $spaceType = $docParam->type;
  39. list($libs, $libID, $object, $objectID, $objectDropdown) = $this->doc->setMenuByType($type, $objectID, $libID);
  40. $libTree = $this->doc->getLibTree($libID, $libs, $type, $moduleID, $objectID, $browseType, $param);
  41. }
  42. else
  43. {
  44. $objectDropdown = $this->generateLibsDropMenu($libs[$libID], $release);
  45. $libTree = $this->doc->getLibTree($libID, $libs, $spaceType, $moduleID);
  46. }
  47. $this->view->type = $type;
  48. $this->view->objectType = $type;
  49. $this->view->objectID = $objectID;
  50. $this->view->libID = $libID;
  51. $this->view->moduleID = $moduleID;
  52. $this->view->libTree = $libTree;
  53. $this->view->objectDropdown = $objectDropdown;
  54. $this->view->spaceType = $spaceType;
  55. }
  56. /**
  57. * 组装页面左上角下拉菜单的数据。
  58. * Generate api doc index page dropMenu
  59. *
  60. * @param object $lib
  61. * @param int $version
  62. * @access public
  63. * @return array
  64. */
  65. protected function generateLibsDropMenu($lib, $version = 0)
  66. {
  67. if(empty($lib)) return '';
  68. $objectTitle = $this->lang->api->noLinked;
  69. $objectType = 'nolink';
  70. $objectID = 0;
  71. if($lib->product)
  72. {
  73. $objectType = 'product';
  74. $objectID = $lib->product;
  75. $product = $this->loadModel('product')->getByID($objectID);
  76. $objectTitle = zget($product, 'name', '');
  77. }
  78. elseif($lib->project)
  79. {
  80. $objectType = 'project';
  81. $objectID = $lib->project;
  82. $project = $this->loadModel('project')->getByID($objectID);
  83. $objectTitle = zget($project, 'name', '');
  84. }
  85. $objectDropdown['text'] = $objectTitle;
  86. $objectDropdown['link'] = helper::createLink('api', 'ajaxGetDropMenu', "objectType=$objectType&objectID=$objectID&libID=$lib->id&version=$version");
  87. return $objectDropdown;
  88. }
  89. /**
  90. * 解析请求地获得请求的详细信息。
  91. * Get the details of the method by file path.
  92. *
  93. * @param string $filePath
  94. * @param string $ext
  95. * @access protected
  96. * @return object
  97. */
  98. protected function getMethod($filePath, $ext = '')
  99. {
  100. $fileName = dirname($filePath);
  101. $className = basename(dirname(dirname($filePath)));
  102. $methodName = basename($filePath);
  103. if(!class_exists($className)) helper::import($fileName);
  104. $method = new ReflectionMethod($className . $ext, $methodName);
  105. $data = new stdClass();
  106. $data->startLine = $method->getStartLine();
  107. $data->endLine = $method->getEndLine();
  108. $data->comment = $method->getDocComment();
  109. $data->parameters = $method->getParameters();
  110. $data->className = $className;
  111. $data->methodName = $methodName;
  112. $data->fileName = $fileName;
  113. $data->post = false;
  114. $file = file($fileName);
  115. for($i = $data->startLine - 1; $i <= $data->endLine; $i++)
  116. {
  117. if(strpos($file[$i], '$this->post') or strpos($file[$i], 'fixer::input') or strpos($file[$i], '$_POST'))
  118. {
  119. $data->post = true;
  120. }
  121. }
  122. return $data;
  123. }
  124. /**
  125. * 对指定模块下的指定方法进行调用并返回请求结果。
  126. * Request the api.
  127. *
  128. * @param string $moduleName
  129. * @param string $methodName
  130. * @param string $action extendModel | extendControl
  131. * @access protected
  132. * @return array
  133. */
  134. protected function request($moduleName, $methodName, $action)
  135. {
  136. $host = common::getSysURL();
  137. $param = '';
  138. if($action == 'extendModel')
  139. {
  140. /* 对model的函数进行调用。 */
  141. if(!isset($_POST['noparam']))
  142. {
  143. foreach($_POST as $key => $value) $param .= ',' . $key . '=' . $value;
  144. $param = ltrim($param, ',');
  145. }
  146. $url = rtrim($host, '/') . inlink('getModel', "moduleName=$moduleName&methodName=$methodName&params=$param", 'json');
  147. $url .= strpos($url, '?') === false ? '?' : '&';
  148. $url .= $this->config->sessionVar . '=' . session_id();
  149. }
  150. else
  151. {
  152. /* 对control的函数进行调用。 */
  153. if(!isset($_POST['noparam']))
  154. {
  155. foreach($_POST as $key => $value) $param .= '&' . $key . '=' . $value;
  156. $param = ltrim($param, '&');
  157. }
  158. $url = rtrim($host, '/') . helper::createLink($moduleName, $methodName, $param, 'json');
  159. $url .= strpos($url, '?') === false ? '?' : '&';
  160. $url .= $this->config->sessionVar . '=' . session_id();
  161. }
  162. /* Unlock session. After new request, restart session. */
  163. session_write_close();
  164. $content = file_get_contents($url);
  165. session_start();
  166. return array('url' => $url, 'content' => $content);
  167. }
  168. }