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 CControllerScriptUpdate extends CController { 23 24 protected function checkInput() { 25 $fields = [ 26 'scriptid' => 'fatal|required|db scripts.scriptid', 27 'name' => 'required|db scripts.name|not_empty', 28 'scope' => 'db scripts.scope| in '.implode(',', [ZBX_SCRIPT_SCOPE_ACTION, ZBX_SCRIPT_SCOPE_HOST, ZBX_SCRIPT_SCOPE_EVENT]), 29 '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]), 30 'execute_on' => 'db scripts.execute_on|in '.implode(',', [ZBX_SCRIPT_EXECUTE_ON_AGENT, ZBX_SCRIPT_EXECUTE_ON_SERVER, ZBX_SCRIPT_EXECUTE_ON_PROXY]), 31 'menu_path' => 'db scripts.menu_path', 32 'authtype' => 'db scripts.authtype|in '.implode(',', [ITEM_AUTHTYPE_PASSWORD, ITEM_AUTHTYPE_PUBLICKEY]), 33 'username' => 'db scripts.username', 34 'password' => 'db scripts.password', 35 'publickey' => 'db scripts.publickey', 36 'privatekey' => 'db scripts.privatekey', 37 'passphrase' => 'db scripts.password', 38 'port' => 'db scripts.port', 39 'command' => 'db scripts.command|flags '.P_CRLF, 40 'commandipmi' => 'db scripts.command|flags '.P_CRLF, 41 'parameters' => 'array', 42 'script' => 'db scripts.command|flags '.P_CRLF, 43 'timeout' => 'db scripts.timeout|time_unit '.implode(':', [1, SEC_PER_MIN]), 44 'description' => 'db scripts.description', 45 'host_access' => 'db scripts.host_access|in '.implode(',', [PERM_READ, PERM_READ_WRITE]), 46 'groupid' => 'db scripts.groupid', 47 'usrgrpid' => 'db scripts.usrgrpid', 48 'hgstype' => 'in 0,1', 49 'confirmation' => 'db scripts.confirmation|not_empty', 50 'enable_confirmation' => 'in 1', 51 'form_refresh' => 'int32' 52 ]; 53 54 $ret = $this->validateInput($fields); 55 56 if (!$ret) { 57 switch ($this->GetValidationError()) { 58 case self::VALIDATION_ERROR: 59 $response = new CControllerResponseRedirect('zabbix.php?action=script.edit'); 60 $response->setFormData($this->getInputAll()); 61 CMessageHelper::setErrorTitle(_('Cannot update script')); 62 $this->setResponse($response); 63 break; 64 65 case self::VALIDATION_FATAL_ERROR: 66 $this->setResponse(new CControllerResponseFatal()); 67 break; 68 } 69 } 70 71 return $ret; 72 } 73 74 protected function checkPermissions() { 75 if (!$this->checkAccess(CRoleHelper::UI_ADMINISTRATION_SCRIPTS)) { 76 return false; 77 } 78 79 return (bool) API::Script()->get([ 80 'output' => [], 81 'scriptids' => $this->getInput('scriptid'), 82 'editable' => true 83 ]); 84 } 85 86 protected function doAction() { 87 $script = []; 88 89 $this->getInputs($script, ['scriptid', 'name', 'description', 'groupid']); 90 $script['scope'] = $this->getInput('scope', ZBX_SCRIPT_SCOPE_ACTION); 91 $script['type'] = $this->getInput('type', ZBX_SCRIPT_TYPE_WEBHOOK); 92 93 if ($script['scope'] != ZBX_SCRIPT_SCOPE_ACTION) { 94 $script['menu_path'] = trimPath($this->getInput('menu_path', '')); 95 $script['host_access'] = $this->getInput('host_access', PERM_READ); 96 $script['confirmation'] = $this->getInput('confirmation', ''); 97 $script['usrgrpid'] = $this->getInput('usrgrpid', 0); 98 } 99 100 switch ($script['type']) { 101 case ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT: 102 $script['command'] = $this->getInput('command', ''); 103 $script['execute_on'] = $this->getInput('execute_on', ZBX_SCRIPT_EXECUTE_ON_PROXY); 104 break; 105 106 case ZBX_SCRIPT_TYPE_IPMI: 107 $script['command'] = $this->getInput('commandipmi', ''); 108 break; 109 110 case ZBX_SCRIPT_TYPE_SSH: 111 $script['command'] = $this->getInput('command', ''); 112 $script['username'] = $this->getInput('username', ''); 113 $script['port'] = $this->getInput('port', ''); 114 $script['authtype'] = $this->getInput('authtype', ITEM_AUTHTYPE_PASSWORD); 115 116 if ($script['authtype'] == ITEM_AUTHTYPE_PASSWORD) { 117 $script['password'] = $this->getInput('password', ''); 118 } 119 else { 120 $script['publickey'] = $this->getInput('publickey', ''); 121 $script['privatekey'] = $this->getInput('privatekey', ''); 122 $script['password'] = $this->getInput('passphrase', ''); 123 } 124 break; 125 126 case ZBX_SCRIPT_TYPE_TELNET: 127 $script['command'] = $this->getInput('command', ''); 128 $script['username'] = $this->getInput('username', ''); 129 $script['password'] = $this->getInput('password', ''); 130 $script['port'] = $this->getInput('port', ''); 131 break; 132 133 case ZBX_SCRIPT_TYPE_WEBHOOK: 134 $script['command'] = $this->getInput('script', ''); 135 $script['timeout'] = $this->getInput('timeout', DB::getDefault('scripts', 'timeout')); 136 $script['parameters'] = []; 137 $parameters = $this->getInput('parameters', []); 138 139 if (array_key_exists('name', $parameters) && array_key_exists('value', $parameters)) { 140 $script['parameters'] = array_map(function ($name, $value) { 141 return compact('name', 'value'); 142 }, 143 $parameters['name'], 144 $parameters['value'] 145 ); 146 } 147 break; 148 } 149 150 if ($this->getInput('hgstype', 1) == 0) { 151 $script['groupid'] = 0; 152 } 153 154 $result = (bool) API::Script()->update($script); 155 156 if ($result) { 157 $response = new CControllerResponseRedirect((new CUrl('zabbix.php')) 158 ->setArgument('action', 'script.list') 159 ->setArgument('page', CPagerHelper::loadPage('script.list', null)) 160 ); 161 $response->setFormData(['uncheck' => '1']); 162 CMessageHelper::setSuccessTitle(_('Script updated')); 163 } 164 else { 165 $response = new CControllerResponseRedirect((new CUrl('zabbix.php')) 166 ->setArgument('action', 'script.edit') 167 ->setArgument('scriptid', $this->getInput('scriptid')) 168 ); 169 $response->setFormData($this->getInputAll()); 170 CMessageHelper::setErrorTitle(_('Cannot update script')); 171 } 172 173 $this->setResponse($response); 174 } 175} 176