1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
abstract class CController {
const VALIDATION_OK = 0;
const VALIDATION_ERROR = 1;
const VALIDATION_FATAL_ERROR = 2;
/**
* Action name, so that controller knows what action he is executing.
*
* @var string
*/
private $action;
/**
* Response oject generated by controller.
*
* @var string
*/
private $response;
/**
* Result of input validation, one of VALIDATION_OK, VALIDATION_ERROR, VALIDATION_FATAL_ERROR.
*
* @var string
*/
private $validationResult;
/**
* Input parameters retrieved from global $_REQUEST after validation.
*
* @var string
*/
public $input = [];
/**
* SID validation flag, if true SID must be validated.
*
* @var bool
*/
private $validateSID = true;
public function __construct() {
CSession::start();
$this->init();
}
/**
* Initialization function that can be overridden later.
*
* @return var
*/
protected function init() {
}
/**
* Set controller action name.
*
* @return var
*/
public function setAction($action) {
$this->action = $action;
}
/**
* Set controller response.
*
* @return var
*/
public function setResponse($response) {
$this->response = $response;
}
/**
* Return controller response object.
*
* @return var
*/
public function getResponse() {
return $this->response;
}
/**
* Return controller action name.
*
* @return var
*/
public function getAction() {
return $this->action;
}
/**
* Return debug mode.
*
* @return var
*/
public function getDebugMode() {
return CWebUser::getDebugMode();
}
/**
* Return user type.
*
* @return var
*/
public function getUserType() {
return CWebUser::getType();
}
/**
* Return user SID, first 16 bytes of session ID.
*
* @return var
*/
public function getUserSID() {
$sessionid = CWebUser::getSessionCookie();
if ($sessionid === null || strlen($sessionid) < 16) {
return null;
}
return substr($sessionid, 16, 16);
}
/**
* Validate input parameters.
*
* @return var
*/
public function validateInput($validationRules) {
if (CSession::keyExists('formData')) {
$input = array_merge($_REQUEST, CSession::getValue('formData'));
CSession::unsetValue(['formData']);
}
else {
$input = $_REQUEST;
}
$validator = new CNewValidator($input, $validationRules);
foreach ($validator->getAllErrors() as $error) {
info($error);
}
if ($validator->isErrorFatal()) {
$this->validationResult = self::VALIDATION_FATAL_ERROR;
}
else if ($validator->isError()) {
$this->input = $validator->getValidInput();
$this->validationResult = self::VALIDATION_ERROR;
}
else {
$this->input = $validator->getValidInput();
$this->validationResult = self::VALIDATION_OK;
}
return ($this->validationResult == self::VALIDATION_OK);
}
/**
* Validate "from" and "to" parameters for allowed period.
*
* @return bool
*/
public function validateTimeSelectorPeriod() {
if (!$this->hasInput('from') || !$this->hasInput('to')) {
return true;
}
$ts = [];
$range_time_parser = new CRangeTimeParser();
foreach (['from', 'to'] as $field) {
$range_time_parser->parse($this->getInput($field));
$ts[$field] = $range_time_parser->getDateTime($field === 'from')->getTimestamp();
}
$period = $ts['to'] - $ts['from'] + 1;
if ($period < ZBX_MIN_PERIOD) {
info(_n('Minimum time period to display is %1$s minute.',
'Minimum time period to display is %1$s minutes.', (int) ZBX_MIN_PERIOD / SEC_PER_MIN
));
return false;
}
elseif ($period > ZBX_MAX_PERIOD) {
info(_n('Maximum time period to display is %1$s day.',
'Maximum time period to display is %1$s days.', (int) ZBX_MAX_PERIOD / SEC_PER_DAY
));
return false;
}
return true;
}
/**
* Return validation result.
*
* @return var
*/
public function getValidationError() {
return $this->validationResult;
}
/**
* Check if input parameter exists.
*
* @return var
*/
public function hasInput($var) {
return array_key_exists($var, $this->input);
}
/**
* Get single input parameter.
*
* @return var
*/
public function getInput($var, $default = null) {
if ($default === null) {
return $this->input[$var];
}
else {
return array_key_exists($var, $this->input) ? $this->input[$var] : $default;
}
}
/**
* Get several input parameters.
*
* @return var
*/
public function getInputs(&$var, $names) {
foreach ($names as $name) {
if ($this->hasInput($name)) {
$var[$name] = $this->getInput($name);
}
}
}
/**
* Return all input parameters.
*
* @return var
*/
public function getInputAll() {
return $this->input;
}
/**
* Check user permissions.
*
* @abstract
*
* @return var
*/
abstract protected function checkPermissions();
/**
* Validate input parameters.
*
* @abstract
*
* @return var
*/
abstract protected function checkInput();
/**
* Validate session ID (SID).
*
* @return var
*/
public function disableSIDvalidation() {
$this->validateSID = false;
}
/**
* Validate session ID (SID).
*
* @return var
*/
protected function checkSID() {
$sessionid = CWebUser::getSessionCookie();
if ($sessionid === null || !isset($_REQUEST['sid'])) {
return false;
}
return ($_REQUEST['sid'] === substr($sessionid, 16, 16));
}
/**
* Execute action and generate response object.
*
* @abstract
*
* @return var
*/
abstract protected function doAction();
/**
* Main controller processing routine. Returns response object: data, redirect or fatal redirect.
*
* @return var
*/
final public function run() {
if ($this->validateSID && !$this->checkSID()) {
access_deny(ACCESS_DENY_PAGE);
}
if ($this->checkInput()) {
if ($this->checkPermissions() !== true) {
access_deny(ACCESS_DENY_PAGE);
}
$this->doAction();
}
if (CProfile::isModified()) {
DBstart();
$result = CProfile::flush();
DBend($result);
}
return $this->getResponse();
}
}
|