function validateRegexp($expressions) { $validator = new CRegexValidator(['messageInvalid' => _('Regular expression must be a string'), 'messageRegex' => _('Incorrect regular expression "%1$s": "%2$s"')]); foreach ($expressions as $expression) { switch ($expression['expression_type']) { case EXPRESSION_TYPE_TRUE: case EXPRESSION_TYPE_FALSE: if (!$validator->validate($expression['expression'])) { throw new Exception($validator->getError()); } break; case EXPRESSION_TYPE_INCLUDED: case EXPRESSION_TYPE_NOT_INCLUDED: if ($expression['expression'] === '') { throw new Exception(_('Expression cannot be empty')); } break; case EXPRESSION_TYPE_ANY_INCLUDED: foreach (explode($expression['exp_delimiter'], $expression['expression']) as $string) { if ($expression['expression'] === '') { throw new Exception(_('Expression cannot be empty')); } } break; } } }
$page['title'] = _('Configuration of regular expressions'); $page['file'] = 'adm.regexps.php'; $page['type'] = detect_page_type(); require_once dirname(__FILE__) . '/include/page_header.php'; // VAR TYPE OPTIONAL FLAGS VALIDATION EXCEPTION $fields = ['regexpids' => [T_ZBX_INT, O_OPT, P_SYS, DB_ID, null], 'regexpid' => [T_ZBX_INT, O_OPT, P_SYS, DB_ID, 'isset({form}) && {form} == "update"'], 'name' => [T_ZBX_STR, O_OPT, null, NOT_EMPTY, 'isset({add}) || isset({update})', _('Name')], 'test_string' => [T_ZBX_STR, O_OPT, P_NO_TRIM, null, 'isset({add}) || isset({update})', _('Test string')], 'expressions' => [T_ZBX_STR, O_OPT, P_NO_TRIM, null, 'isset({add}) || isset({update})'], 'action' => [T_ZBX_STR, O_OPT, P_SYS | P_ACT, IN('"regexp.massdelete"'), null], 'add' => [T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null], 'update' => [T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null], 'form' => [T_ZBX_STR, O_OPT, P_SYS, null, null], 'form_refresh' => [T_ZBX_INT, O_OPT, null, null, null], 'output' => [T_ZBX_STR, O_OPT, P_ACT, null, null], 'ajaxaction' => [T_ZBX_STR, O_OPT, P_ACT, null, null], 'ajaxdata' => [T_ZBX_STR, O_OPT, P_ACT | P_NO_TRIM, null, null]]; check_fields($fields); /* * Ajax */ if (isset($_REQUEST['output']) && $_REQUEST['output'] == 'ajax') { $ajaxResponse = new CAjaxResponse(); $ajaxData = getRequest('ajaxdata', []); if (isset($_REQUEST['ajaxaction']) && $_REQUEST['ajaxaction'] == 'test') { $result = ['expressions' => [], 'errors' => [], 'final' => true]; $validator = new CRegexValidator(['messageInvalid' => _('Regular expression must be a string'), 'messageRegex' => _('Incorrect regular expression "%1$s": "%2$s"')]); foreach ($ajaxData['expressions'] as $id => $expression) { if (!in_array($expression['expression_type'], [EXPRESSION_TYPE_FALSE, EXPRESSION_TYPE_TRUE]) || $validator->validate($expression['expression'])) { $match = CGlobalRegexp::matchExpression($expression, $ajaxData['testString']); $result['expressions'][$id] = $match; } else { $match = false; $result['errors'][$id] = $validator->getError(); } $result['final'] = $result['final'] && $match; } $ajaxResponse->success($result); } $ajaxResponse->send(); require_once dirname(__FILE__) . '/include/page_footer.php'; exit;
function validateRegexp($expressions) { $validator = new CRegexValidator(array('messageInvalid' => _('Regular expression must be a string'), 'messageRegex' => _('Incorrect regular expression "%1$s": "%2$s"'))); foreach ($expressions as $expression) { if ($expression['expression_type'] == EXPRESSION_TYPE_TRUE || $expression['expression_type'] == EXPRESSION_TYPE_FALSE) { if (!$validator->validate($expression['expression'])) { throw new \Exception($validator->getError()); } } } }