disableCsrfValidation(); } protected function checkInput() { return true; } protected function checkPermissions() { return CWebUser::getType() == USER_TYPE_SUPER_ADMIN; } protected function doAction() { $mediatypeid = (string) ($_REQUEST['mediatypeid'] ?? ''); try { // Strategy apply buttons are submitted as name="apply_strategy" value=. if (isset($_REQUEST['apply_strategy'])) { $this->applyStrategy($mediatypeid, (string) $_REQUEST['apply_strategy']); } else { $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; case 'apply_switch': $this->applySwitch(); 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); } $tab = (string) ($_REQUEST['tab'] ?? ''); if (in_array($tab, ['alert', 'opt', 'maint'], true)) { $url->setArgument('tab', $tab); } $this->setResponse(new CControllerResponseRedirect($url)); } private function applyStrategy(string $mediatypeid, string $key): void { if (!isset(Helper::STRATEGY_PRESETS[$key])) { throw new Exception(I18n::t('msg.bad_scheme')); } $tid = Helper::templateHostid(); if ($tid === null) { throw new Exception(I18n::t('msg.no_template_host')); } $preset = Helper::STRATEGY_PRESETS[$key]; // 1) threshold on the media (global "how severe still notifies") if ($mediatypeid !== '') { $user = Helper::ensureRobotUser('wecomrobot'); $mask = Helper::severityMaskFromMin((int) $preset['min']); Helper::applyMedia($user['userid'], $mediatypeid, $mask, 0); } // 2) classify + write severities $res = Helper::applyStrategy($tid, $preset['targets']); Helper::saveStrategyKey($key); info(I18n::t('msg.strategy_applied', [ I18n::t($preset['label']), $res['trigger'], $res['prototype'], $res['discovered'] ])); } 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; } $user = Helper::ensureRobotUser('wecomrobot'); Helper::applyMedia($user['userid'], $mediatypeid, $mask, $active); 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('wecomrobot'); $cur = Helper::currentMedia($user, $mediatypeid); $mask = $cur['severity'] ?? 48; $active = $cur['active'] ?? 0; Helper::applyMedia($user['userid'], $mediatypeid, $mask, $active); 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; } $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')); } private function applySwitch(): void { $src = (string) ($_REQUEST['switch_src'] ?? ''); $dst = (string) ($_REQUEST['switch_dst'] ?? ''); if ($src === '' || $dst === '') { throw new Exception(I18n::t('msg.need_both')); } if ($src === $dst) { throw new Exception(I18n::t('msg.same_tpl')); } $n = Helper::switchApply($src, $dst); info(I18n::t('msg.switch_applied', [$n])); } }