zentao.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /**
  3. * This is the PHP-SDK class of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2015 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author
  8. * @package api
  9. * @version v1.0.0
  10. * @link http://www.zentao.net
  11. */
  12. class zentao
  13. {
  14. const ztURL = ''; // ZenTaoPMS deploys domain names.
  15. const ztAccount = ''; // ZenTaoPMS login account.
  16. const ztPassword = ''; // ZenTaoPMS login password.
  17. const ztAccessMode = ''; // Parameter request method. [GET|PATH_INFO]
  18. public $sessionRand = 0; // Session random number for some encryption and verification.
  19. public $tokenAuth = ''; // Session authentication.
  20. public $requestMethod = ''; // Set request method.
  21. public $params = array(); // Interface request parameter.
  22. public $returnResult = array('status' => 0, 'msg' => 'error', 'result' => array()); // Return result.
  23. /**
  24. * Get the session ID required for the session.
  25. *
  26. * @access public
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. $this->login();
  32. }
  33. /**
  34. * User login verification.
  35. *
  36. * @access public
  37. * @return void
  38. */
  39. public function login()
  40. {
  41. /* Get token. */
  42. $result = $this->setParams(array('m' => 'api', 'f' => 'getSessionID'))
  43. ->setRequestMethod('get')
  44. ->sendRequest(array('get' => self::ztURL), true);
  45. $result = json_decode($result->data);
  46. $this->tokenAuth = $result->sessionName . '=' . $result->sessionID;
  47. /* User authentication login. */
  48. $this->setParams(array('account' => self::ztAccount, 'password' => self::ztPassword))
  49. ->setRequestMethod('post')
  50. ->sendRequest(array('get' => self::ztURL . '?m=user&f=login&t=json', 'path_info' => self::ztURL . '/user-login.json'));
  51. }
  52. /**
  53. * Get a list of departments.
  54. *
  55. * @param array $optionalParams
  56. * @param array $extraFields
  57. * @access public
  58. * @return string
  59. */
  60. public function getDeptList($optionalParams = array(), $extraFields = array())
  61. {
  62. $result = $this->setParams(array('m' => 'dept', 'f' => 'browse'), $optionalParams)
  63. ->setRequestMethod('get')
  64. ->sendRequest(array('get' => self::ztURL), true);
  65. $extraFields = array_merge(array('title', 'deptID', 'parentDepts', 'sons', 'tree'), $extraFields);
  66. $returnResult = $this->getResultData($result, $extraFields);
  67. return $returnResult;
  68. }
  69. /**
  70. * Add a new department.
  71. *
  72. * @param array $optionalParams
  73. * @access public
  74. * @return string
  75. */
  76. public function addDept($optionalParams = array())
  77. {
  78. $result = $this->setParams(array(), $optionalParams)
  79. ->setRequestMethod('post')
  80. ->sendRequest(array('get' => self::ztURL . '?m=dept&f=manageChild&t=json', 'path_info' => self::ztURL . '/dept-manageChild.json'));
  81. $returnResult = $this->returnResult;
  82. if (strpos($result, 'reload')) $returnResult = array('status' => 1, 'msg' => 'success', 'result' => array());
  83. return json_encode($returnResult, JSON_UNESCAPED_UNICODE);
  84. }
  85. /**
  86. * Get user list.
  87. *
  88. * @param array $optionalParams
  89. * @param array $extraFields
  90. * @access public
  91. * @return string
  92. */
  93. public function getUserList($optionalParams = array(), $extraFields = array())
  94. {
  95. $optionalParams = array_merge(array('param'=> 0, 'type'=> 'bydept', 'orderBy'=> 'id', 'recTotal'=> 999999, 'recPerPage' => 999999), $optionalParams);
  96. $result = $this->setParams(array('m' => 'company', 'f' => 'browse'), $optionalParams)
  97. ->setRequestMethod('get')
  98. ->sendRequest(array('get' => self::ztURL), true);
  99. $extraFields = array_merge(array('title', 'users'), $extraFields);
  100. $returnResult = $this->getResultData($result, $extraFields);
  101. return $returnResult;
  102. }
  103. /**
  104. * Add user optional information.
  105. *
  106. * @param array $optionalParams
  107. * @param array $extraFields
  108. * @access public
  109. * @return string
  110. */
  111. public function getUserCreateParams($optionalParams = array(), $extraFields = array())
  112. {
  113. $result = $this->setParams(array('m' => 'user', 'f' => 'create'), $optionalParams)
  114. ->setRequestMethod('get')
  115. ->sendRequest(array('get' => self::ztURL), true);
  116. $extraFields = array_merge(array('title', 'depts', 'groupList', 'roleGroup'), $extraFields);
  117. $returnResult = $this->getResultData($result, $extraFields);
  118. $result = json_decode($result->data);
  119. $this->sessionRand = $result->rand;
  120. return $returnResult;
  121. }
  122. /**
  123. * New users.
  124. *
  125. * @param array $optionalParams
  126. * @access public
  127. * @return string
  128. */
  129. public function addUser($optionalParams = array())
  130. {
  131. // Get the random number required for encryption.
  132. $this->getUserCreateParams();
  133. $optionalParams['password1'] = md5($optionalParams['password1']) . $this->sessionRand;
  134. $optionalParams['password2'] = md5($optionalParams['password2']) . $this->sessionRand;
  135. $optionalParams['verifyPassword'] = md5(md5(self::ztPassword) . $this->sessionRand);
  136. $requestURL['get'] = self::ztURL . '?m=user&f=create&dept=' . $optionalParams['dept'] . '&t=json';
  137. $requestURL['path_info'] = self::ztURL . '/user-create-' . $optionalParams['dept'] . '.json';
  138. $responseData = $this->setParams(array(), $optionalParams)
  139. ->setRequestMethod('post')
  140. ->sendRequest($requestURL, true);
  141. $returnResult = $this->getResultData($responseData);
  142. return $returnResult;
  143. }
  144. /**
  145. * Get product list.
  146. *
  147. * @param array $optionalParams
  148. * @param array $extraFields
  149. * @access public
  150. * @return string
  151. */
  152. public function getProductList($optionalParams = array(), $extraFields = array())
  153. {
  154. $optionalParams = array_merge(array('productID ' => 0, 'line' => 0, 'status' => 'all', 'orderBy' => 'order_desc', 'recTotal' => 999999, 'recPerPage' => 999999), $optionalParams);
  155. $result = $this->setParams(array('m' => 'product', 'f' => 'all'), $optionalParams)
  156. ->setRequestMethod('get')
  157. ->sendRequest(array('get' => self::ztURL), true);
  158. $extraFields = array_merge(array('title', 'products', 'productStats'), $extraFields);
  159. $returnResult = $this->getResultData($result, $extraFields);
  160. return $returnResult;
  161. }
  162. /**
  163. * Get added product optional information.
  164. *
  165. * @param array $optionalParams
  166. * @param array $extraFields
  167. * @access public
  168. * @return string
  169. */
  170. public function getProductCreateParams($optionalParams = array(), $extraFields = array())
  171. {
  172. $result = $this->setParams(array('m' => 'product', 'f' => 'create'), $optionalParams)
  173. ->setRequestMethod('get')
  174. ->sendRequest(array('get' => self::ztURL), true);
  175. $extraFields = array_merge(array('title', 'products', 'lines', 'poUsers', 'qdUsers', 'rdUsers', 'groups'), $extraFields);
  176. $returnResult = $this->getResultData($result, $extraFields);
  177. return $returnResult;
  178. }
  179. /**
  180. * Add a single product.
  181. *
  182. * @param array $optionalParams
  183. * @access public
  184. * @return string
  185. */
  186. public function addProduct($optionalParams = array())
  187. {
  188. $requestURL['get'] = self::ztURL . '?m=product&f=create&t=json';
  189. $requestURL['path_info'] = self::ztURL . '/product-create.json';
  190. $responseData = $this->setParams(array(), $optionalParams)
  191. ->setRequestMethod('post')
  192. ->sendRequest($requestURL, true);
  193. $returnResult = $this->getResultData($responseData);
  194. return $returnResult;
  195. }
  196. /**
  197. * Get project list.
  198. *
  199. * @param array $optionalParams
  200. * @param array $extraFields
  201. * @access public
  202. * @return string
  203. */
  204. public function getProjectList($optionalParams = array(), $extraFields = array())
  205. {
  206. $optionalParams = array_merge(array('status' => 'all', 'projectID' => 0, 'orderBy' => 'order_desc', 'productID' => 0, 'recTotal' => 999999, 'recPerPage' => 999999), $optionalParams);
  207. $result = $this->setParams(array('m' => 'project', 'f' => 'all'), $optionalParams)
  208. ->setRequestMethod('get')
  209. ->sendRequest(array('get' => self::ztURL), true);
  210. $extraFields = array_merge(array('title', 'projects', 'projectStats', 'teamMembers', 'users'), $extraFields);
  211. $returnResult = $this->getResultData($result, $extraFields);
  212. return $returnResult;
  213. }
  214. /**
  215. * Get optional information for adding items.
  216. *
  217. * @param array $optionalParams
  218. * @param array $extraFields
  219. * @access public
  220. * @return string
  221. */
  222. public function getProjectCreateParams($optionalParams = array(), $extraFields = array())
  223. {
  224. $result = $this->setParams(array('m' => 'project', 'f' => 'create'), $optionalParams)
  225. ->setRequestMethod('get')
  226. ->sendRequest(array('get' => self::ztURL), true);
  227. $extraFields = array_merge(array('title', 'projects', 'groups', 'allProducts'), $extraFields);
  228. $returnResult = $this->getResultData($result, $extraFields);
  229. return $returnResult;
  230. }
  231. /**
  232. * Add a single item.
  233. *
  234. * @param array $optionalParams
  235. * @access public
  236. * @return string
  237. */
  238. public function addProject($optionalParams = array())
  239. {
  240. $requestURL['get'] = self::ztURL . '?m=project&f=create&t=json';
  241. $requestURL['path_info'] = self::ztURL . '/project-create.json';
  242. $responseData = $this->setParams(array(), $optionalParams)
  243. ->setRequestMethod('post')
  244. ->sendRequest($requestURL, true);
  245. $returnResult = $this->getResultData($responseData);
  246. return $returnResult;
  247. }
  248. /**
  249. * Get task list.
  250. *
  251. * @param array $optionalParams
  252. * @param array $extraFields
  253. * @access public
  254. * @return string
  255. */
  256. public function getTaskList($optionalParams = array(), $extraFields = array())
  257. {
  258. $optionalParams = array_merge(array('projectID' => 0, 'status' => 'all', 'param' => 0, 'orderBy' => '', 'recTotal' => 999999, 'recPerPage' => 999999), $optionalParams);
  259. $result = $this->setParams(array('m' => 'project', 'f' => 'task'), $optionalParams)
  260. ->setRequestMethod('get')
  261. ->sendRequest(array('get' => self::ztURL), true);
  262. $extraFields = array_merge(array('title', 'projects', 'project', 'products', 'tasks'), $extraFields);
  263. $returnResult = $this->getResultData($result, $extraFields);
  264. return $returnResult;
  265. }
  266. /**
  267. * Add task optional information.
  268. *
  269. * @param array $optionalParams
  270. * @param array $extraFields
  271. * @access public
  272. * @return string
  273. */
  274. public function getTaskCreateParams($optionalParams = array(), $extraFields = array())
  275. {
  276. $result = $this->setParams(array('m' => 'task', 'f' => 'create'), $optionalParams)
  277. ->setRequestMethod('get')
  278. ->sendRequest(array('get' => self::ztURL), true);
  279. $extraFields = array_merge(array('title', 'projects', 'users', 'stories', 'moduleOptionMenu', 'project'), $extraFields);
  280. $returnResult = $this->getResultData($result, $extraFields);
  281. return $returnResult;
  282. }
  283. /**
  284. * Add a single task.
  285. *
  286. * @param array $optionalParams
  287. * @access public
  288. * @return string
  289. */
  290. public function addTask($optionalParams = array())
  291. {
  292. $requestURL['get'] = self::ztURL . '?m=task&f=create&project=' . $optionalParams['project'] . '&t=json';
  293. $requestURL['path_info'] = self::ztURL . '/task-create-' . $optionalParams['project'] . '.json';
  294. $responseData = $this->setParams(array('status' => 'wait', 'after' => 'toTaskList'), $optionalParams)
  295. ->setRequestMethod('post')
  296. ->sendRequest($requestURL, true);
  297. $returnResult = $this->getResultData($responseData);
  298. return $returnResult;
  299. }
  300. /**
  301. * Optional information for completing a single task.
  302. *
  303. * @param array $optionalParams
  304. * @param array $extraFields
  305. * @access public
  306. * @return string
  307. */
  308. public function getTaskFinishParams($optionalParams = array(), $extraFields = array())
  309. {
  310. $result = $this->setParams(array('m' => 'task', 'f' => 'finish'), $optionalParams)
  311. ->setRequestMethod('get')
  312. ->sendRequest(array('get' => self::ztURL), true);
  313. $extraFields = array_merge(array('title', 'users', 'task', 'project', 'actions'), $extraFields);
  314. $returnResult = $this->getResultData($result, $extraFields);
  315. return $returnResult;
  316. }
  317. /**
  318. * Complete a single task.
  319. *
  320. * @param array $optionalParams
  321. * @access public
  322. * @return string
  323. */
  324. public function addTaskFinish($optionalParams = array())
  325. {
  326. $taskID = $optionalParams['taskID'];
  327. unset($optionalParams['taskID']);
  328. $requestURL['get'] = self::ztURL . '?m=task&f=finish&taskID=' . $taskID . '&t=json';
  329. $requestURL['path_info'] = self::ztURL . '/task-finish-' . $taskID . '.json';
  330. $result = $this->setParams(array('status' => 'done'), $optionalParams)
  331. ->setRequestMethod('post')
  332. ->sendRequest($requestURL, false);
  333. $returnResult = $this->returnResult;
  334. if (strpos($result, 'task-view-' . $taskID . '.json') || strpos($result, 'taskID=' . $taskID)) $returnResult = array('status' => 1, 'msg' => 'success', 'result' => array());
  335. return json_encode($returnResult, JSON_UNESCAPED_UNICODE);
  336. }
  337. /**
  338. * Get BUG List.
  339. *
  340. * @param array $optionalParams
  341. * @param array $extraFields
  342. * @access public
  343. * @return string
  344. */
  345. public function getBugList($optionalParams = array(), $extraFields = array())
  346. {
  347. $optionalParams = array_merge(array('productID' => 0, 'branch' => 0, 'browseType' => 'all', 'param' => 0, 'orderBy' => '', 'recTotal' => 999999, 'recPerPage' => 999999), $optionalParams);
  348. $result = $this->setParams(array('m' => 'bug', 'f' => 'browse'), $optionalParams)
  349. ->setRequestMethod('get')
  350. ->sendRequest(array('get' => self::ztURL), true);
  351. $extraFields = array_merge(array('title', 'products', 'productID', 'productName', 'product', 'moduleName', 'modules', 'browseType', 'bugs'), $extraFields);
  352. $returnResult = $this->getResultData($result, $extraFields);
  353. return $returnResult;
  354. }
  355. /**
  356. * Add single BUG optional information.
  357. *
  358. * @param array $optionalParams
  359. * @param array $extraFields
  360. * @access public
  361. * @return string
  362. */
  363. public function getBugCreateParams($optionalParams = array(), $extraFields = array())
  364. {
  365. $result = $this->setParams(array('m' => 'bug', 'f' => 'create'), $optionalParams)
  366. ->setRequestMethod('get')
  367. ->sendRequest(array('get' => self::ztURL), true);
  368. $extraFields = array_merge(array('title', 'productID', 'productName', 'projects', 'moduleOptionMenu', 'users', 'stories', 'builds'), $extraFields);
  369. $returnResult = $this->getResultData($result, $extraFields);
  370. return $returnResult;
  371. }
  372. /**
  373. * Add a single bug.
  374. *
  375. * @param array $optionalParams
  376. * @access public
  377. * @return string
  378. */
  379. public function addBug($optionalParams = array())
  380. {
  381. $requestURL['get'] = self::ztURL . '?m=bug&f=create&productID=' . $optionalParams['product'] . '&t=json';
  382. $requestURL['path_info'] = self::ztURL . '/bug-create-' . $optionalParams['product'] . '.json';
  383. $responseData = $this->setParams(array('status' => 'active'), $optionalParams)
  384. ->setRequestMethod('post')
  385. ->sendRequest($requestURL, true);
  386. $returnResult = $this->getResultData($responseData);
  387. return $returnResult;
  388. }
  389. /**
  390. * Optional information for solving a single bug.
  391. *
  392. * @param array $optionalParams
  393. * @param array $extraFields
  394. * @access public
  395. * @return string
  396. */
  397. public function getBugResolveParams($optionalParams = array(), $extraFields = array())
  398. {
  399. $result = $this->setParams(array('m' => 'bug', 'f' => 'resolve'), $optionalParams)
  400. ->setRequestMethod('get')
  401. ->sendRequest(array('get' => self::ztURL), true);
  402. $extraFields = array_merge(array('title', 'products', 'bug', 'users', 'builds', 'actions'), $extraFields);
  403. $returnResult = $this->getResultData($result, $extraFields);
  404. return $returnResult;
  405. }
  406. /**
  407. * Solve a single bug.
  408. *
  409. * @param array $optionalParams
  410. * @access public
  411. * @return string
  412. */
  413. public function addBugResolve($optionalParams = array())
  414. {
  415. $bugID = $optionalParams['bugID'];
  416. $requestURL['get'] = self::ztURL . '?m=bug&f=resolve&bugID=' . $bugID . '&t=json';
  417. $requestURL['path_info'] = self::ztURL . '/bug-resolve-' . $bugID . '.json';
  418. unset($optionalParams['bugID']);
  419. $result = $this->setParams(array('status' => 'resolved'), $optionalParams)
  420. ->setRequestMethod('post')
  421. ->sendRequest($requestURL);
  422. $returnResult = $this->returnResult;
  423. if (strpos($result, 'bug-view-' . $bugID . '.json') || strpos($result, 'bugID=' . $bugID)) $returnResult = array('status' => 1, 'msg' => 'success', 'result' => array());
  424. return json_encode($returnResult, JSON_UNESCAPED_UNICODE);
  425. }
  426. /**
  427. * Send a get request.
  428. *
  429. * @param string $url
  430. * @access public
  431. * @return string
  432. */
  433. public function getUrl($url)
  434. {
  435. $ch = curl_init();
  436. if (self::ztAccessMode == 'GET')
  437. {
  438. $this->params = array_merge($this->params, array('t' => 'json'));
  439. $url .= strpos($url, '?') ? http_build_query($this->params) : '?' . http_build_query($this->params);
  440. }
  441. elseif (self::ztAccessMode == 'PATH_INFO')
  442. {
  443. $params = implode('-', $this->params);
  444. $url = $url . '/' . $params . '.json';
  445. }
  446. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  447. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  448. curl_setopt($ch, CURLOPT_COOKIE, $this->tokenAuth);
  449. curl_setopt($ch, CURLOPT_REFERER, self::ztURL);
  450. curl_setopt($ch, CURLOPT_URL, $url);
  451. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  452. $result = curl_exec($ch);
  453. curl_close($ch);
  454. return $result;
  455. }
  456. /**
  457. * Send a post request.
  458. *
  459. * @param string $url
  460. * @access public
  461. * @return string
  462. */
  463. public function postUrl($url)
  464. {
  465. $ch = curl_init();
  466. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  467. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  468. curl_setopt($ch, CURLOPT_COOKIE, $this->tokenAuth);
  469. curl_setopt($ch, CURLOPT_REFERER, self::ztURL);
  470. curl_setopt($ch, CURLOPT_URL, $url);
  471. if (count($this->params)) curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->params));
  472. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  473. curl_setopt($ch, CURLOPT_POST, true);
  474. $result = curl_exec($ch);
  475. curl_close($ch);
  476. return $result;
  477. }
  478. /**
  479. * Processing request parameters.
  480. *
  481. * @param array $requiredParams
  482. * @param array $optionalParams
  483. * @access public
  484. * @return $this
  485. */
  486. public function setParams($requiredParams = array(), $optionalParams = array())
  487. {
  488. $this->params = array();
  489. $this->params = array_merge($requiredParams, $optionalParams);
  490. return $this;
  491. }
  492. /**
  493. * Set request method.
  494. *
  495. * @param string $requestMethod
  496. * @access public
  497. * @return $this
  498. */
  499. public function setRequestMethod($requestMethod = 'get')
  500. {
  501. $this->requestMethod = strcmp($requestMethod, 'get') === 0 ? 'get' : 'post';
  502. return $this;
  503. }
  504. /**
  505. * Send a request for response results.
  506. *
  507. * @param array $requestURL
  508. * @param bool $isDecode
  509. * @access public
  510. * @return string $result
  511. */
  512. public function sendRequest($requestURL = array(), $isDecode = false)
  513. {
  514. if ($this->requestMethod == 'get') $result = $this->getUrl($requestURL['get']);
  515. if ($this->requestMethod == 'post') $result = self::ztAccessMode == 'GET' ? $this->postUrl($requestURL['get']) : $this->postUrl($requestURL['path_info']);
  516. $result = $isDecode ? json_decode($result) : $result;
  517. return $result;
  518. }
  519. /**
  520. * Get the specified result.
  521. *
  522. * @param string $responseData
  523. * @param array $extraFields
  524. * @access public
  525. * @return string $result
  526. */
  527. public function getResultData($responseData = '', $extraFields = array())
  528. {
  529. $returnResult = $this->returnResult;
  530. if (!empty($responseData->status) && strcmp($responseData->status, 'success') === 0)
  531. {
  532. $returnResult = array('status' => 1, 'msg' => 'success');
  533. $responseData = json_decode($responseData->data);
  534. foreach ($extraFields as $k => $v)
  535. {
  536. isset($responseData->$v) ? $returnResult['result'][$v] = $responseData->$v : $returnResult['result'][$v] = array();
  537. }
  538. if (count($extraFields) == 0) $returnResult['result'] = $responseData;
  539. }
  540. elseif (!empty($responseData->result))
  541. {
  542. if (strcmp($responseData->result, 'success') === 0) $returnResult = array('status' => 1, 'msg' => 'success');
  543. $returnResult['result'] = $responseData->message;
  544. }
  545. return json_encode($returnResult, JSON_UNESCAPED_UNICODE);
  546. }
  547. }