WecomCenterSave.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php declare(strict_types = 0);
  2. /*
  3. ** WeCom Center - POST controller. Dispatches actions and redirects back.
  4. */
  5. namespace Modules\WecomCenter\Actions;
  6. use CController;
  7. use CControllerResponseRedirect;
  8. use CUrl;
  9. use CWebUser;
  10. use Exception;
  11. use Modules\WecomCenter\Lib\Helper;
  12. use Modules\WecomCenter\Lib\I18n;
  13. class WecomCenterSave extends CController {
  14. protected function init() {
  15. $this->disableCsrfValidation();
  16. }
  17. protected function checkInput() {
  18. return true;
  19. }
  20. protected function checkPermissions() {
  21. return CWebUser::getType() == USER_TYPE_SUPER_ADMIN;
  22. }
  23. protected function doAction() {
  24. $mediatypeid = (string) ($_REQUEST['mediatypeid'] ?? '');
  25. $hostid = (string) ($_REQUEST['hostid'] ?? '');
  26. try {
  27. // One-click alert scheme presets: buttons named scheme_<key>.
  28. $applied = false;
  29. foreach (array_keys(Helper::SCHEMES) as $k) {
  30. if (isset($_REQUEST['scheme_'.$k])) {
  31. $this->applyScheme($mediatypeid, $k);
  32. $applied = true;
  33. break;
  34. }
  35. }
  36. if (!$applied) {
  37. $op = (string) ($_REQUEST['op'] ?? '');
  38. switch ($op) {
  39. case 'save_alert':
  40. $this->saveAlert($mediatypeid);
  41. break;
  42. case 'save_robot':
  43. $this->saveRobot($mediatypeid);
  44. break;
  45. case 'test_robot':
  46. $this->testRobot($mediatypeid);
  47. break;
  48. case 'import_template':
  49. $this->importTemplate();
  50. break;
  51. case 'save_map':
  52. $this->saveMap();
  53. break;
  54. default:
  55. error(I18n::t('msg.unknown_op'));
  56. }
  57. }
  58. }
  59. catch (Exception $e) {
  60. error($e->getMessage());
  61. }
  62. $url = (new CUrl('zabbix.php'))->setArgument('action', 'wecom.center.view');
  63. if ($mediatypeid !== '') {
  64. $url->setArgument('mediatypeid', $mediatypeid);
  65. }
  66. if ($hostid !== '') {
  67. $url->setArgument('hostid', $hostid);
  68. }
  69. $this->setResponse(new CControllerResponseRedirect($url));
  70. }
  71. private function userName(): string {
  72. $n = trim((string) ($_REQUEST['username'] ?? ''));
  73. return $n !== '' ? $n : 'wecomrobot';
  74. }
  75. private function applyScheme(string $mediatypeid, string $scheme): void {
  76. if (!array_key_exists($scheme, Helper::SCHEMES)) {
  77. throw new Exception(I18n::t('msg.bad_scheme'));
  78. }
  79. [$mask, $active] = Helper::SCHEMES[$scheme];
  80. $user = Helper::ensureRobotUser($this->userName());
  81. Helper::applyMedia($user['userid'], $mediatypeid, $mask, $active);
  82. info(I18n::t('msg.scheme_applied', [I18n::t('scheme.'.$scheme)]));
  83. }
  84. private function saveAlert(string $mediatypeid): void {
  85. $active = isset($_REQUEST['enabled']) ? 0 : 1; // media: 0=enabled, 1=disabled
  86. $indices = [];
  87. for ($i = 1; $i <= 6; $i++) {
  88. if (isset($_REQUEST['sev_'.$i])) {
  89. $indices[] = $i;
  90. }
  91. }
  92. $mask = Helper::severityMask($indices);
  93. if ($mask === 0 && $active === 0) {
  94. $mask = 2; // keep at least Information when enabled
  95. }
  96. $user = Helper::ensureRobotUser($this->userName());
  97. $sendto = (string) ($_REQUEST['sendto'] ?? 'WeCom');
  98. Helper::applyMedia($user['userid'], $mediatypeid, $mask, $active, $sendto);
  99. info(I18n::t('msg.saved'));
  100. }
  101. private function saveRobot(string $mediatypeid): void {
  102. $url = trim((string) ($_REQUEST['webhook_url'] ?? ''));
  103. if ($url !== '') {
  104. Helper::setWebhookUrl($mediatypeid, $url);
  105. }
  106. $user = Helper::ensureRobotUser($this->userName());
  107. $cur = Helper::currentMedia($user, $mediatypeid);
  108. $mask = $cur['severity'] ?? 48;
  109. $active = $cur['active'] ?? 0;
  110. $sendto = (string) ($_REQUEST['sendto'] ?? ($cur['sendto'] ?? 'WeCom'));
  111. Helper::applyMedia($user['userid'], $mediatypeid, $mask, $active, $sendto);
  112. info(I18n::t('msg.robot_saved'));
  113. }
  114. private function testRobot(string $mediatypeid): void {
  115. $mt = Helper::getMediatype($mediatypeid);
  116. if (!$mt) {
  117. throw new Exception(I18n::t('msg.no_mediatype'));
  118. }
  119. $url = Helper::webhookUrl($mt);
  120. if ($url === '') {
  121. throw new Exception(I18n::t('msg.no_url'));
  122. }
  123. $text = '['.I18n::t('page.title').'] Test OK - '.date('Y-m-d H:i:s');
  124. $res = Helper::testWebhook($url, $text);
  125. $body = (string) $res['body'];
  126. $ok = (strpos($body, '"errcode":0') !== false) || (strpos($body, '"errcode": 0') !== false);
  127. if ($ok) {
  128. info(I18n::t('msg.tested'));
  129. }
  130. else {
  131. error(I18n::t('msg.test_failed', [$res['status'], $body]));
  132. }
  133. }
  134. private function importTemplate(): void {
  135. $file = (string) ($_REQUEST['template_file'] ?? '');
  136. if ($file === '') {
  137. throw new Exception(I18n::t('msg.no_template'));
  138. }
  139. $module_dir = dirname(__DIR__);
  140. $res = Helper::importTemplate($module_dir, $file);
  141. $n = isset($res['templates']) && is_array($res['templates']) ? count($res['templates']) : 0;
  142. info(I18n::t('msg.imported', [$n]));
  143. }
  144. private function saveMap(): void {
  145. $hostid = (string) ($_REQUEST['hostid'] ?? '');
  146. $lat = trim((string) ($_REQUEST['lat'] ?? ''));
  147. $lon = trim((string) ($_REQUEST['lon'] ?? ''));
  148. if ($hostid === '') {
  149. throw new Exception(I18n::t('msg.no_host'));
  150. }
  151. Helper::setHostLocation($hostid, $lat, $lon);
  152. info(I18n::t('msg.map_saved'));
  153. }
  154. }