control.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * The control file of webhook module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(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 Sun Guangming<sunguangming@cnezsoft.com>
  8. * @package webhook
  9. * @version $Id$
  10. * @link https://www.zentao.net
  11. */
  12. class webhook extends control
  13. {
  14. /**
  15. * Construct
  16. *
  17. * @param string $moduleName
  18. * @param string $methodName
  19. * @access public
  20. * @return void
  21. */
  22. public function __construct($moduleName = '', $methodName = '')
  23. {
  24. parent::__construct($moduleName, $methodName);
  25. $this->loadModel('message');
  26. }
  27. /**
  28. * Webhook 列表。
  29. * Browse webhooks.
  30. *
  31. * @param string $orderBy
  32. * @param int $recTotal
  33. * @param int $recPerPage
  34. * @param int $pageID
  35. * @access public
  36. * @return void
  37. */
  38. public function browse($orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  39. {
  40. $this->app->loadClass('pager', true);
  41. $pager = new pager($recTotal, $recPerPage, $pageID);
  42. /* Unset selectedDepts cookie. */
  43. helper::setcookie('selectedDepts', '', 0, $this->config->webRoot, '', $this->config->cookieSecure, true);
  44. $this->view->title = $this->lang->webhook->api . $this->lang->hyphen . $this->lang->webhook->list;
  45. $this->view->webhooks = $this->webhook->getList($orderBy, $pager);
  46. $this->view->orderBy = $orderBy;
  47. $this->view->pager = $pager;
  48. $this->display();
  49. }
  50. /**
  51. * 创建 Webhook。
  52. * Create a webhook.
  53. *
  54. * @access public
  55. * @return void
  56. */
  57. public function create()
  58. {
  59. if($_POST)
  60. {
  61. $webhook = form::data($this->config->webhook->form->create)->get();
  62. $webhookID = $this->webhook->create($webhook);
  63. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  64. if($this->viewType == 'json') return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'id' => $webhookID));
  65. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => inlink('browse')));
  66. }
  67. $this->app->loadLang('action');
  68. $this->view->title = $this->lang->webhook->api . $this->lang->hyphen . $this->lang->webhook->create;
  69. $this->view->products = $this->loadModel('product')->getPairs();
  70. $this->view->executions = $this->loadModel('execution')->getPairs(0, 'all', 'withobject,multiple');
  71. $this->display();
  72. }
  73. /**
  74. * 编辑 Webhook。
  75. * Edit a webhook.
  76. *
  77. * @param int $id
  78. * @access public
  79. * @return void
  80. */
  81. public function edit($id)
  82. {
  83. if($_POST)
  84. {
  85. $webhook = form::data($this->config->webhook->form->edit)->get();
  86. $this->webhook->update($id, $webhook);
  87. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  88. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => inlink('browse')));
  89. }
  90. $webhook = $this->webhook->getByID($id);
  91. $this->app->loadLang('action');
  92. $this->view->title = $this->lang->webhook->edit . $this->lang->hyphen . $webhook->name;
  93. $this->view->products = $this->loadModel('product')->getPairs();
  94. $this->view->executions = $this->loadModel('execution')->getPairs(0, 'all', 'withobject,multiple');
  95. $this->view->webhook = $webhook;
  96. $this->display();
  97. }
  98. /**
  99. * 删除 Webhook。
  100. * Delete a webhook.
  101. *
  102. * @param int $id
  103. * @access public
  104. * @return void
  105. */
  106. public function delete($id)
  107. {
  108. $this->webhook->delete(TABLE_WEBHOOK, $id);
  109. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  110. return $this->sendSuccess(array('load' => true));
  111. }
  112. /**
  113. * 查看 Webhook 的日志。
  114. * Browse logs of a webhook.
  115. *
  116. * @param int $id
  117. * @param string $orderBy
  118. * @param int $recTotal
  119. * @param int $recPerPage
  120. * @param int $pageID
  121. * @access public
  122. * @return void
  123. */
  124. public function log($id, $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  125. {
  126. /* Save session. */
  127. $this->app->loadClass('pager', true);
  128. $pager = new pager($recTotal, $recPerPage, $pageID);
  129. $webhook = $this->webhook->getByID($id);
  130. $this->view->title = $this->lang->webhook->log . $this->lang->hyphen . $webhook->name;
  131. $this->view->logs = $this->webhook->getLogList($id, $orderBy, $pager);
  132. $this->view->webhook = $webhook;
  133. $this->view->orderBy = $orderBy;
  134. $this->view->pager = $pager;
  135. $this->display();
  136. }
  137. /**
  138. * 绑定钉钉、企业微信、飞书的用户。
  139. * Bind dingtalk, wechat, feishu user.
  140. *
  141. * @param int $id
  142. * @param int $recTotal
  143. * @param int $recPerPage
  144. * @param int $pageID
  145. * @access public
  146. * @return void
  147. */
  148. public function bind($id, $recTotal = 0, $recPerPage = 15, $pageID = 1)
  149. {
  150. if($_POST)
  151. {
  152. $this->webhook->bind($id);
  153. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  154. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->createLink('webhook', 'browse')));
  155. }
  156. $webhook = $this->webhook->getById($id);
  157. if(!in_array($webhook->type, array('dinguser', 'wechatuser', 'feishuuser'))) return $this->send(array('result' => 'fail', 'load' => array('alert' => $this->lang->webhook->note->bind, 'load' => $this->createLink('webhook', 'browse'))));
  158. $webhook->secret = json_decode($webhook->secret);
  159. /* Get selected depts. */
  160. if($this->get->selectedDepts)
  161. {
  162. helper::setcookie('selectedDepts', $this->get->selectedDepts, 0, $this->config->webRoot, '', $this->config->cookieSecure, true);
  163. $_COOKIE['selectedDepts'] = $this->get->selectedDepts;
  164. }
  165. $response = $this->webhookZen->getResponse($webhook);
  166. if($response['result'] == 'fail')
  167. {
  168. if($response['message'] == 'nodept') return $this->send(array('result' => 'fail', 'load' => array('alert' => $this->lang->webhook->error->noDept, 'load' => $this->createLink('webhook', 'chooseDept', "id={$id}"))));
  169. if(is_array($response['message'])) $response['message'] = implode(',', $response['message']);
  170. return $this->send(array('result' => 'fail', 'load' => array('alert' => $response['message'], 'load' => $this->createLink('webhook', 'browse'))));
  171. }
  172. $oauthUsers = $response['data'];
  173. $this->app->loadClass('pager', true);
  174. $pager = new pager($recTotal, $recPerPage, $pageID);
  175. $users = $this->loadModel('user')->getByQuery('inside', '', $pager);
  176. $boundUsers = $this->webhook->getBoundUsers($id);
  177. $boundUseridPairs = $this->webhookZen->getBoundUseridPairs($webhook, $users, $boundUsers, $oauthUsers);
  178. $this->view->title = $this->lang->webhook->bind;
  179. $this->view->webhook = $webhook;
  180. $this->view->oauthUsers = $oauthUsers;
  181. $this->view->useridPairs = array_flip($oauthUsers);
  182. $this->view->users = $users;
  183. $this->view->pager = $pager;
  184. $this->view->boundUsers = $boundUsers;
  185. $this->view->boundUseridPairs = $boundUseridPairs;
  186. $this->display();
  187. }
  188. /**
  189. * 选择部门,用于钉钉、企业微信、飞书的用户绑定。
  190. * Choose depts for dingtalk, wechat, feishu user bind.
  191. *
  192. * @param int $id
  193. * @access public
  194. * @return void
  195. */
  196. public function chooseDept($id)
  197. {
  198. $webhook = $this->webhook->getById($id);
  199. if(!in_array($webhook->type, array('dinguser', 'wechatuser', 'feishuuser'))) return $this->send(array('result' => 'fail', 'message' => $this->lang->webhook->note->bind, 'load' => $this->createLink('webhook', 'browse')));
  200. $webhook->secret = json_decode($webhook->secret);
  201. if($webhook->type == 'dinguser')
  202. {
  203. $this->app->loadClass('dingapi', true);
  204. $dingapi = new dingapi($webhook->secret->appKey, $webhook->secret->appSecret, $webhook->secret->agentId);
  205. $response = $dingapi->getDeptTree();
  206. }
  207. if($webhook->type == 'feishuuser') $response = array('result' => 'success', 'data' => array());
  208. if($response['result'] == 'fail')
  209. {
  210. $message = $response['message'];
  211. if(is_array($message) || is_object($message))
  212. {
  213. $message = array();
  214. array_walk_recursive($response['message'], function($item, $key) use(&$message) {$message[] = "$key: $item";});
  215. $message = implode(",", $message);
  216. }
  217. $response = array('result' => 'fail', 'message' => $message, 'load' => $this->createLink('webhook', 'browse'));
  218. return $this->send($response);
  219. }
  220. if($response['result'] == 'selected')
  221. {
  222. $locateLink = $this->createLink('webhook', 'bind', "id={$id}");
  223. $locateLink .= strpos($locateLink, '?') !== false ? '&' : '?';
  224. $locateLink .= 'selectedDepts=' . join(',', $response['data']);
  225. return $this->send(array('result' => 'success', 'load' => $locateLink));
  226. }
  227. $this->view->title = $this->lang->webhook->chooseDept;
  228. $this->view->webhookType = $webhook->type;
  229. $this->view->deptTree = $response['data'];
  230. $this->view->webhookID = $id;
  231. $this->display();
  232. }
  233. /**
  234. * 获取飞书部门列表,用于飞书的用户绑定。
  235. * Get feishu dept list for feishu user bind.
  236. *
  237. * @param int $id
  238. * @access public
  239. * @return void
  240. * @param int $webhookID
  241. */
  242. public function ajaxGetFeishuDeptList($webhookID)
  243. {
  244. $webhook = $this->webhook->getById($webhookID);
  245. $webhook->secret = json_decode($webhook->secret);
  246. if($_POST)
  247. {
  248. $this->app->loadClass('feishuapi', true);
  249. $feishuApi = new feishuapi($webhook->secret->appId, $webhook->secret->appSecret);
  250. $departmentID = $_POST['departmentID'] ? $_POST['departmentID'] : '';
  251. $depts = $feishuApi->getChildDeptTree($departmentID);
  252. echo json_encode($depts);
  253. }
  254. else
  255. {
  256. $this->app->loadClass('feishuapi', true);
  257. $feishuApi = new feishuapi($webhook->secret->appId, $webhook->secret->appSecret);
  258. $depts = $feishuApi->getDeptTree();
  259. echo json_encode($depts);
  260. }
  261. }
  262. /**
  263. * 异步发送数据。
  264. * Send data by async.
  265. *
  266. * @access public
  267. * @return void
  268. */
  269. public function asyncSend()
  270. {
  271. $webhooks = $this->webhook->getList('id_desc', null, false);
  272. if(empty($webhooks))
  273. {
  274. echo "NO WEBHOOK EXIST.\n";
  275. return false;
  276. }
  277. $dataList = $this->webhook->getDataList();
  278. if(empty($dataList))
  279. {
  280. echo "OK\n";
  281. return true;
  282. }
  283. $this->webhook->setSentStatus(array_keys($dataList), 'senting');
  284. $now = helper::now();
  285. $diff = 0;
  286. foreach($dataList as $data)
  287. {
  288. $webhook = zget($webhooks, $data->objectID, '');
  289. if($webhook)
  290. {
  291. /* if connect time is out then ignore it.*/
  292. if($diff < 29)
  293. {
  294. $time = time();
  295. $result = $this->webhook->fetchHook($webhook, $data->data, $data->action);
  296. $diff = time() - $time;
  297. }
  298. $this->webhook->saveLog($webhook, $data->action, $data->data, $result);
  299. $this->webhook->setSentStatus($data->id, 'sended', $now);
  300. }
  301. else
  302. {
  303. $this->webhook->setSentStatus($data->id, 'fail', $now);
  304. }
  305. }
  306. $this->dao->delete()->from(TABLE_NOTIFY)->where('status')->eq('sended')->exec();
  307. echo "OK\n";
  308. return true;
  309. }
  310. }