| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?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_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);
- }
- $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]));
- }
- }
|