1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22class CControllerScriptCreate extends CController { 23 24 protected function checkInput() { 25 $fields = [ 26 'name' => 'required|db scripts.name|not_empty', 27 'scope' => 'db scripts.scope| in '.implode(',', [ZBX_SCRIPT_SCOPE_ACTION, ZBX_SCRIPT_SCOPE_HOST, ZBX_SCRIPT_SCOPE_EVENT]), 28 'type' => 'required|db scripts.type|in '.implode(',', [ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT, ZBX_SCRIPT_TYPE_IPMI, ZBX_SCRIPT_TYPE_SSH, ZBX_SCRIPT_TYPE_TELNET, ZBX_SCRIPT_TYPE_WEBHOOK]), 29 'execute_on' => 'db scripts.execute_on|in '.implode(',', [ZBX_SCRIPT_EXECUTE_ON_AGENT, ZBX_SCRIPT_EXECUTE_ON_SERVER, ZBX_SCRIPT_EXECUTE_ON_PROXY]), 30 'menu_path' => 'db scripts.menu_path', 31 'authtype' => 'db scripts.authtype|in '.implode(',', [ITEM_AUTHTYPE_PASSWORD, ITEM_AUTHTYPE_PUBLICKEY]), 32 'username' => 'db scripts.username', 33 'password' => 'db scripts.password', 34 'publickey' => 'db scripts.publickey', 35 'privatekey' => 'db scripts.privatekey', 36 'passphrase' => 'db scripts.password', 37 'port' => 'db scripts.port', 38 'command' => 'db scripts.command|flags '.P_CRLF, 39 'commandipmi' => 'db scripts.command|flags '.P_CRLF, 40 'parameters' => 'array', 41 'script' => 'db scripts.command|flags '.P_CRLF, 42 'timeout' => 'db scripts.timeout|time_unit '.implode(':', [1, SEC_PER_MIN]), 43 'description' => 'db scripts.description', 44 'host_access' => 'db scripts.host_access|in '.implode(',', [PERM_READ, PERM_READ_WRITE]), 45 'groupid' => 'db scripts.groupid', 46 'usrgrpid' => 'db scripts.usrgrpid', 47 'hgstype' => 'in 0,1', 48 'confirmation' => 'db scripts.confirmation|not_empty', 49 'enable_confirmation' => 'in 1', 50 'form_refresh' => 'int32' 51 ]; 52 53 $ret = $this->validateInput($fields); 54 55 if (!$ret) { 56 switch ($this->GetValidationError()) { 57 case self::VALIDATION_ERROR: 58 $response = new CControllerResponseRedirect('zabbix.php?action=script.edit'); 59 $response->setFormData($this->getInputAll()); 60 CMessageHelper::setErrorTitle(_('Cannot add script')); 61 $this->setResponse($response); 62 break; 63 64 case self::VALIDATION_FATAL_ERROR: 65 $this->setResponse(new CControllerResponseFatal()); 66 break; 67 } 68 } 69 70 return $ret; 71 } 72 73 protected function checkPermissions() { 74 return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_SCRIPTS); 75 } 76 77 protected function doAction() { 78 $script = []; 79 80 $this->getInputs($script, ['name', 'description', 'groupid']); 81 $script['scope'] = $this->getInput('scope', ZBX_SCRIPT_SCOPE_ACTION); 82 $script['type'] = $this->getInput('type', ZBX_SCRIPT_TYPE_WEBHOOK); 83 84 if ($script['scope'] != ZBX_SCRIPT_SCOPE_ACTION) { 85 $script['menu_path'] = trimPath($this->getInput('menu_path', '')); 86 $script['host_access'] = $this->getInput('host_access', PERM_READ); 87 $script['confirmation'] = $this->getInput('confirmation', ''); 88 $script['usrgrpid'] = $this->getInput('usrgrpid', 0); 89 } 90 91 switch ($script['type']) { 92 case ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT: 93 $script['command'] = $this->getInput('command', ''); 94 $script['execute_on'] = $this->getInput('execute_on', ZBX_SCRIPT_EXECUTE_ON_PROXY); 95 break; 96 97 case ZBX_SCRIPT_TYPE_IPMI: 98 $script['command'] = $this->getInput('commandipmi', ''); 99 break; 100 101 case ZBX_SCRIPT_TYPE_SSH: 102 $script['command'] = $this->getInput('command', ''); 103 $script['username'] = $this->getInput('username', ''); 104 $script['port'] = $this->getInput('port', ''); 105 $script['authtype'] = $this->getInput('authtype', ITEM_AUTHTYPE_PASSWORD); 106 107 if ($script['authtype'] == ITEM_AUTHTYPE_PASSWORD) { 108 $script['password'] = $this->getInput('password', ''); 109 } 110 else { 111 $script['publickey'] = $this->getInput('publickey', ''); 112 $script['privatekey'] = $this->getInput('privatekey', ''); 113 $script['password'] = $this->getInput('passphrase', ''); 114 } 115 break; 116 117 case ZBX_SCRIPT_TYPE_TELNET: 118 $script['command'] = $this->getInput('command', ''); 119 $script['username'] = $this->getInput('username', ''); 120 $script['password'] = $this->getInput('password', ''); 121 $script['port'] = $this->getInput('port', ''); 122 break; 123 124 case ZBX_SCRIPT_TYPE_WEBHOOK: 125 $script['command'] = $this->getInput('script', ''); 126 $script['timeout'] = $this->getInput('timeout', DB::getDefault('scripts', 'timeout')); 127 $parameters = $this->getInput('parameters', []); 128 129 if (array_key_exists('name', $parameters) && array_key_exists('value', $parameters)) { 130 $script['parameters'] = array_map(function ($name, $value) { 131 return compact('name', 'value'); 132 }, $parameters['name'], $parameters['value'] 133 ); 134 } 135 break; 136 } 137 138 if ($this->getInput('hgstype', 1) == 0) { 139 $script['groupid'] = 0; 140 } 141 142 $result = (bool) API::Script()->create($script); 143 144 if ($result) { 145 $response = new CControllerResponseRedirect((new CUrl('zabbix.php')) 146 ->setArgument('action', 'script.list') 147 ->setArgument('page', CPagerHelper::loadPage('script.list', null)) 148 ); 149 $response->setFormData(['uncheck' => '1']); 150 CMessageHelper::setSuccessTitle(_('Script added')); 151 } 152 else { 153 $response = new CControllerResponseRedirect((new CUrl('zabbix.php'))->setArgument('action', 'script.edit')); 154 $response->setFormData($this->getInputAll()); 155 CMessageHelper::setErrorTitle(_('Cannot add script')); 156 } 157 158 $this->setResponse($response); 159 } 160} 161