disableCsrfValidation(); } protected function checkInput() { return true; } protected function checkPermissions() { return CWebUser::getType() == USER_TYPE_SUPER_ADMIN; } protected function doAction() { $mediatypeid = (string) ($_REQUEST['mediatypeid'] ?? ''); $hostid = (string) ($_REQUEST['hostid'] ?? ''); try { // One-click alert scheme presets: buttons named scheme_. $applied = false; foreach (array_keys(Helper::SCHEMES) as $k) { if (isset($_REQUEST['scheme_'.$k])) { $this->applyScheme($mediatypeid, $k); $applied = true; break; } } if (!$applied) { $op = (string) ($_REQUEST['op'] ?? ''); switch ($op) { case 'save_alert': $this->saveAlert($mediatypeid); break; case 'save_triggers': $this->saveTriggers(); break; case 'save_robot': $this->saveRobot($mediatypeid); break; case 'test_robot': $this->testRobot($mediatypeid); break; case 'import_template': $this->importTemplate(); break; case 'save_map': $this->saveMap(); break; default: error(I18n::t('msg.unknown_op')); } } } catch (Exception $e) { error($e->getMessage()); } $url = (new CUrl('zabbix.php'))->setArgument('action', 'wecom.center.view'); if ($mediatypeid !== '') { $url->setArgument('mediatypeid', $mediatypeid); } if ($hostid !== '') { $url->setArgument('hostid', $hostid); } $this->setResponse(new CControllerResponseRedirect($url)); } private function userName(): string { $n = trim((string) ($_REQUEST['username'] ?? '')); return $n !== '' ? $n : 'wecomrobot'; } private function applyScheme(string $mediatypeid, string $scheme): void { if (!array_key_exists($scheme, Helper::SCHEMES)) { throw new Exception(I18n::t('msg.bad_scheme')); } [$mask, $active] = Helper::SCHEMES[$scheme]; $user = Helper::ensureRobotUser($this->userName()); Helper::applyMedia($user['userid'], $mediatypeid, $mask, $active); info(I18n::t('msg.scheme_applied', [I18n::t('scheme.'.$scheme)])); } private function saveAlert(string $mediatypeid): void { $active = isset($_REQUEST['enabled']) ? 0 : 1; // media: 0=enabled, 1=disabled $indices = []; for ($i = 1; $i <= 6; $i++) { if (isset($_REQUEST['sev_'.$i])) { $indices[] = $i; } } $mask = Helper::severityMask($indices); if ($mask === 0 && $active === 0) { $mask = 2; // keep at least Information when enabled } $user = Helper::ensureRobotUser($this->userName()); $sendto = (string) ($_REQUEST['sendto'] ?? 'WeCom'); Helper::applyMedia($user['userid'], $mediatypeid, $mask, $active, $sendto); info(I18n::t('msg.saved')); } private function saveRobot(string $mediatypeid): void { $url = trim((string) ($_REQUEST['webhook_url'] ?? '')); if ($url !== '') { Helper::setWebhookKey($url); } $user = Helper::ensureRobotUser($this->userName()); $cur = Helper::currentMedia($user, $mediatypeid); $mask = $cur['severity'] ?? 48; $active = $cur['active'] ?? 0; $sendto = (string) ($_REQUEST['sendto'] ?? ($cur['sendto'] ?? 'WeCom')); Helper::applyMedia($user['userid'], $mediatypeid, $mask, $active, $sendto); info(I18n::t('msg.robot_saved')); } private function testRobot(string $mediatypeid): void { $mt = Helper::getMediatype($mediatypeid); if (!$mt) { throw new Exception(I18n::t('msg.no_mediatype')); } $url = Helper::webhookUrl($mt); if ($url === '') { throw new Exception(I18n::t('msg.no_url')); } $text = '['.I18n::t('page.title').'] Test OK - '.date('Y-m-d H:i:s'); $res = Helper::testWebhook($url, $text); $body = (string) $res['body']; $ok = (strpos($body, '"errcode":0') !== false) || (strpos($body, '"errcode": 0') !== false); if ($ok) { info(I18n::t('msg.tested')); } else { error(I18n::t('msg.test_failed', [$res['status'], $body])); } } private function importTemplate(): void { $file = (string) ($_REQUEST['template_file'] ?? ''); if ($file === '') { throw new Exception(I18n::t('msg.no_template')); } $module_dir = dirname(__DIR__); $res = Helper::importTemplate($module_dir, $file); $n = isset($res['templates']) && is_array($res['templates']) ? count($res['templates']) : 0; info(I18n::t('msg.imported', [$n])); } private function saveTriggers(): void { $ids = (string) ($_REQUEST['trig_ids'] ?? ''); $ids = array_filter(array_map('trim', explode(',', $ids))); if (!$ids) { throw new Exception(I18n::t('msg.no_triggers')); } $map = []; foreach ($ids as $id) { $id = (string) (int) $id; if ($id === '' || $id === '0') { continue; } // checked => alerting enabled => trigger status 0; unchecked => disabled => 1 $map[$id] = isset($_REQUEST['trig_'.$id]) ? 0 : 1; } $n = Helper::setTriggersStatus($map); info(I18n::t('msg.triggers_saved', [$n])); } private function saveMap(): void { $hostid = (string) ($_REQUEST['hostid'] ?? ''); $lat = trim((string) ($_REQUEST['lat'] ?? '')); $lon = trim((string) ($_REQUEST['lon'] ?? '')); if ($hostid === '') { throw new Exception(I18n::t('msg.no_host')); } Helper::setHostLocation($hostid, $lat, $lon); info(I18n::t('msg.map_saved')); } }