| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?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'] ?? '');
- $hostid = (string) ($_REQUEST['hostid'] ?? '');
- try {
- // One-click alert scheme presets: buttons named scheme_<key>.
- $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_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::setWebhookUrl($mediatypeid, $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 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'));
- }
- }
|