| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php declare(strict_types = 0);
- /*
- ** WeCom Center - POST controller. Dispatches actions and redirects back.
- */
- namespace Modules\WecomCenter\Actions;
- use CController;
- use CControllerResponseRedirect;
- use CUrl;
- use CWebUser;
- use Exception;
- use Modules\WecomCenter\Lib\Helper;
- use Modules\WecomCenter\Lib\I18n;
- class WecomCenterSave extends CController {
- protected function init() {
- $this->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=<preset>.
- 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_class_sev': $this->saveClassSeverity(); 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;
- case 'init_env': $this->initEnv(); 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);
- if ($tab === 'maint') {
- $ts = (string) ($_REQUEST['trig_search'] ?? '');
- if ($ts !== '') {
- $url->setArgument('trig_search', $ts);
- }
- }
- }
- $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'));
- }
- $changed = 0;
- $sev_changed = 0;
- foreach ($ids as $id) {
- $id = (int) $id;
- if ($id <= 0) {
- continue;
- }
- $status = isset($_REQUEST['trig_'.$id]) ? 0 : 1; // checkbox = enabled
- $priority = null;
- if (isset($_REQUEST['pse_'.$id]) && $_REQUEST['pse_'.$id] !== '') {
- $priority = (int) $_REQUEST['pse_'.$id];
- $sev_changed++;
- }
- Helper::setTriggerState((string) $id, $priority, $status);
- $changed++;
- }
- info(I18n::t('msg.triggers_saved', [$changed]));
- }
- private function saveClassSeverity(): void {
- $ck = (string) ($_REQUEST['class_key'] ?? '');
- $p = (int) ($_REQUEST['class_priority'] ?? -1);
- if ($ck === '' || $p < 0 || $p > 5) {
- throw new Exception(I18n::t('msg.bad_class_sev'));
- }
- $r = Helper::setClassSeverity($ck, $p);
- info(I18n::t('msg.class_sev_saved', [$r['trigger'] + $r['prototype'], $r['discovered']]));
- }
- 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]));
- }
- private function initEnv(): void {
- $key = trim((string) ($_REQUEST['key'] ?? $_REQUEST['webhook_url'] ?? ''));
- $proto = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
- $host = (string) ($_SERVER['HTTP_HOST'] ?? '');
- $zbxUrl = $host !== '' ? $proto.'://'.$host : '';
- $r = Helper::initializeEnvironment($key, $zbxUrl);
- $parts = [];
- $parts[] = $r['created_mediatype'] ? I18n::t('msg.init_mediatype_new') : I18n::t('msg.init_mediatype_ok');
- $parts[] = $r['created_user'] ? I18n::t('msg.init_user_new') : I18n::t('msg.init_user_ok');
- $parts[] = $r['created_action'] ? I18n::t('msg.init_action_new') : I18n::t('msg.init_action_ok');
- if ($r['key_set']) {
- $parts[] = I18n::t('msg.init_key_set');
- }
- info(I18n::t('msg.init_done', [implode(' · ', $parts)]));
- }
- }
|