convert validate/check functions
Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
449
lib/Froxlor/Validate/Validate.php
Normal file
449
lib/Froxlor/Validate/Validate.php
Normal file
@@ -0,0 +1,449 @@
|
||||
<?php
|
||||
namespace Froxlor\Validate;
|
||||
|
||||
use Froxlor\Settings;
|
||||
|
||||
class Validate
|
||||
{
|
||||
|
||||
/**
|
||||
* Validates the given string by matching against the pattern, prints an error on failure and exits
|
||||
*
|
||||
* @param string $str
|
||||
* the string to be tested (user input)
|
||||
* @param
|
||||
* string the $fieldname to be used in error messages
|
||||
* @param string $pattern
|
||||
* the regular expression to be used for testing
|
||||
* @param
|
||||
* string language id for the error
|
||||
* @return string the clean string
|
||||
*
|
||||
* If the default pattern is used and the string does not match, we try to replace the
|
||||
* 'bad' values and log the action.
|
||||
*
|
||||
*/
|
||||
public static function validate($str, $fieldname, $pattern = '', $lng = '', $emptydefault = array(), $throw_exception = false)
|
||||
{
|
||||
global $log;
|
||||
|
||||
if (! is_array($emptydefault)) {
|
||||
$emptydefault_array = array(
|
||||
$emptydefault
|
||||
);
|
||||
unset($emptydefault);
|
||||
$emptydefault = $emptydefault_array;
|
||||
unset($emptydefault_array);
|
||||
}
|
||||
|
||||
// Check if the $str is one of the values which represent the default for an 'empty' value
|
||||
if (is_array($emptydefault) && ! empty($emptydefault) && in_array($str, $emptydefault) && isset($emptydefault[0])) {
|
||||
return $emptydefault[0];
|
||||
}
|
||||
|
||||
if ($pattern == '') {
|
||||
|
||||
$pattern = '/^[^\r\n\t\f\0]*$/D';
|
||||
|
||||
if (! preg_match($pattern, $str)) {
|
||||
// Allows letters a-z, digits, space (\\040), hyphen (\\-), underscore (\\_) and backslash (\\\\),
|
||||
// everything else is removed from the string.
|
||||
$allowed = "/[^a-z0-9\\040\\.\\-\\_\\\\]/i";
|
||||
$str = preg_replace($allowed, "", $str);
|
||||
$log->logAction(USR_ACTION, LOG_WARNING, "cleaned bad formatted string (" . $str . ")");
|
||||
}
|
||||
}
|
||||
|
||||
if (preg_match($pattern, $str)) {
|
||||
return $str;
|
||||
}
|
||||
|
||||
if ($lng == '') {
|
||||
$lng = 'stringformaterror';
|
||||
}
|
||||
|
||||
standard_error($lng, $fieldname, $throw_exception);
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether it is a valid ip
|
||||
*
|
||||
* @param string $ip
|
||||
* ip-address to check
|
||||
* @param bool $return_bool
|
||||
* whether to return bool or call standard_error()
|
||||
* @param string $lng
|
||||
* index for error-message (if $return_bool is false)
|
||||
* @param bool $allow_localhost
|
||||
* whether to allow 127.0.0.1
|
||||
* @param bool $allow_priv
|
||||
* whether to allow private network addresses
|
||||
* @param bool $allow_cidr
|
||||
* whether to allow CIDR values e.g. 10.10.10.10/16
|
||||
*
|
||||
* @return string|bool ip address on success, false on failure
|
||||
*/
|
||||
public static function validate_ip2($ip, $return_bool = false, $lng = 'invalidip', $allow_localhost = false, $allow_priv = false, $allow_cidr = false, $throw_exception = false)
|
||||
{
|
||||
$cidr = "";
|
||||
if ($allow_cidr) {
|
||||
$org_ip = $ip;
|
||||
$ip_cidr = explode("/", $ip);
|
||||
if (count($ip_cidr) == 2) {
|
||||
$ip = $ip_cidr[0];
|
||||
$cidr = "/" . $ip_cidr[1];
|
||||
} else {
|
||||
$ip = $org_ip;
|
||||
}
|
||||
} elseif (strpos($ip, "/") !== false) {
|
||||
if ($return_bool) {
|
||||
return false;
|
||||
} else {
|
||||
standard_error($lng, $ip, $throw_exception);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
$filter_lan = $allow_priv ? FILTER_FLAG_NO_RES_RANGE : (FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE);
|
||||
|
||||
if ((filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) && filter_var($ip, FILTER_VALIDATE_IP, $filter_lan)) {
|
||||
return $ip . $cidr;
|
||||
}
|
||||
|
||||
// special case where localhost ip is allowed (mysql-access-hosts for example)
|
||||
if ($allow_localhost && $ip == '127.0.0.1') {
|
||||
return $ip . $cidr;
|
||||
}
|
||||
|
||||
if ($return_bool) {
|
||||
return false;
|
||||
} else {
|
||||
standard_error($lng, $ip, $throw_exception);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the submitted string is a valid domainname
|
||||
*
|
||||
* @param string $domainname
|
||||
* The domainname which should be checked.
|
||||
* @param bool $allow_underscore
|
||||
* optional if true, allowes the underscore character in a domain label (DKIM etc.)
|
||||
*
|
||||
* @return string|boolean the domain-name if the domain is valid, false otherwise
|
||||
*/
|
||||
public static function validateDomain($domainname, $allow_underscore = false)
|
||||
{
|
||||
if (is_string($domainname)) {
|
||||
$char_validation = '([a-z\d](-*[a-z\d])*)(\.?([a-z\d](-*[a-z\d])*))*\.([a-z\d])+';
|
||||
if ($allow_underscore) {
|
||||
$char_validation = '([a-z\d\_](-*[a-z\d\_])*)(\.([a-z\d\_](-*[a-z\d])*))*(\.?([a-z\d](-*[a-z\d])*))+\.([a-z\d])+';
|
||||
}
|
||||
|
||||
if (preg_match("/^" . $char_validation . "$/i", $domainname) && // valid chars check
|
||||
preg_match("/^.{1,253}$/", $domainname) && // overall length check
|
||||
preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domainname)) // length of each label
|
||||
{
|
||||
return $domainname;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function checkHostname($fieldname, $fielddata, $newfieldvalue, $allnewfieldvalues)
|
||||
{
|
||||
if (0 == strlen(trim($newfieldvalue)) || self::validateDomain($newfieldvalue) === false) {
|
||||
return array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_ERROR,
|
||||
'invalidhostname'
|
||||
);
|
||||
} else {
|
||||
return array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_OK
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* validate a local-hostname by regex
|
||||
*
|
||||
* @param string $hostname
|
||||
*
|
||||
* @return string|boolean hostname on success, else false
|
||||
*/
|
||||
public static function validateLocalHostname($hostname)
|
||||
{
|
||||
$pattern = '/^([a-zA-Z0-9\-])+$/i';
|
||||
if (preg_match($pattern, $hostname)) {
|
||||
return $hostname;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* check whether an email account is to be deleted
|
||||
* reference: #1519
|
||||
*
|
||||
* @return bool true if the domain is to be deleted, false otherwise
|
||||
*
|
||||
*/
|
||||
public static function checkMailAccDeletionState($email_addr = null)
|
||||
{
|
||||
// example data of task 7: a:2:{s:9:"loginname";s:4:"webX";s:5:"email";s:20:"deleteme@example.tld";}
|
||||
|
||||
// check for task
|
||||
$result_tasks_stmt = \Froxlor\Database\Database::prepare("
|
||||
SELECT * FROM `" . TABLE_PANEL_TASKS . "` WHERE `type` = '7' AND `data` LIKE :emailaddr
|
||||
");
|
||||
\Froxlor\Database\Database::pexecute($result_tasks_stmt, array(
|
||||
'emailaddr' => "%" . $email_addr . "%"
|
||||
));
|
||||
$num_results = \Froxlor\Database\Database::num_rows();
|
||||
|
||||
// is there a task for deleting this email account?
|
||||
if ($num_results > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if an emailaddress is in correct format or not
|
||||
*
|
||||
* @param string $email
|
||||
* The email address to check
|
||||
* @return bool Correct or not
|
||||
*/
|
||||
public static function validateEmail($email)
|
||||
{
|
||||
$email = strtolower($email);
|
||||
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
|
||||
public static function checkUsername($fieldname, $fielddata, $newfieldvalue, $allnewfieldvalues)
|
||||
{
|
||||
if (! isset($allnewfieldvalues['customer_mysqlprefix'])) {
|
||||
$allnewfieldvalues['customer_mysqlprefix'] = Settings::Get('customer.mysqlprefix');
|
||||
}
|
||||
|
||||
$returnvalue = array();
|
||||
if (validateUsername($newfieldvalue, Settings::Get('panel.unix_names'), 14 - strlen($allnewfieldvalues['customer_mysqlprefix'])) === true) {
|
||||
$returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_OK
|
||||
);
|
||||
} else {
|
||||
$errmsg = 'accountprefixiswrong';
|
||||
if ($fieldname == 'customer_mysqlprefix') {
|
||||
$errmsg = 'mysqlprefixiswrong';
|
||||
}
|
||||
$returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_ERROR,
|
||||
$errmsg
|
||||
);
|
||||
}
|
||||
return $returnvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if an username is in correct format or not.
|
||||
*
|
||||
* @param
|
||||
* string The username to check
|
||||
* @return bool Correct or not
|
||||
* @author Michael Duergner <michael@duergner.com>
|
||||
*
|
||||
*/
|
||||
public static function validateUsername($username, $unix_names = 1, $mysql_max = '')
|
||||
{
|
||||
if ($unix_names == 0) {
|
||||
if (strpos($username, '--') === false) {
|
||||
return (preg_match('/^[a-z][a-z0-9\-_]{0,' . (int) ($mysql_max - 1) . '}[a-z0-9]{1}$/Di', $username) != false);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return (preg_match('/^[a-z][a-z0-9]{0,' . $mysql_max . '}$/Di', $username) != false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* validates a given regex
|
||||
*
|
||||
* @param string $regex
|
||||
* regex to validate
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function checkValidRegEx($regex = null)
|
||||
{
|
||||
if ($regex == null || $regex == '') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static function checkPathConflicts($fieldname, $fielddata, $newfieldvalue, $allnewfieldvalues)
|
||||
{
|
||||
if ((int) Settings::Get('system.mod_fcgid') == 1) {
|
||||
// fcgid-configdir has changed -> check against customer-doc-prefix
|
||||
if ($fieldname == "system_mod_fcgid_configdir") {
|
||||
$newdir = \Froxlor\FileDir::makeCorrectDir($newfieldvalue);
|
||||
$cdir = \Froxlor\FileDir::makeCorrectDir(Settings::Get('system.documentroot_prefix'));
|
||||
} // customer-doc-prefix has changed -> check against fcgid-configdir
|
||||
elseif ($fieldname == "system_documentroot_prefix") {
|
||||
$newdir = \Froxlor\FileDir::makeCorrectDir($newfieldvalue);
|
||||
$cdir = \Froxlor\FileDir::makeCorrectDir(Settings::Get('system.mod_fcgid_configdir'));
|
||||
}
|
||||
|
||||
// neither dir can be within the other nor can they be equal
|
||||
if (substr($newdir, 0, strlen($cdir)) == $cdir || substr($cdir, 0, strlen($newdir)) == $newdir || $newdir == $cdir) {
|
||||
$returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_ERROR,
|
||||
'fcgidpathcannotbeincustomerdoc'
|
||||
);
|
||||
} else {
|
||||
$returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_OK
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_OK
|
||||
);
|
||||
}
|
||||
|
||||
return $returnvalue;
|
||||
}
|
||||
|
||||
public static function checkPhpInterfaceSetting($fieldname, $fielddata, $newfieldvalue, $allnewfieldvalues)
|
||||
{
|
||||
$returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_OK
|
||||
);
|
||||
|
||||
if ((int) Settings::Get('system.mod_fcgid') == 1) {
|
||||
// fcgid only works for apache and lighttpd
|
||||
if (strtolower($newfieldvalue) != 'apache2' && strtolower($newfieldvalue) != 'lighttpd') {
|
||||
$returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_ERROR,
|
||||
'fcgidstillenableddeadlock'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $returnvalue;
|
||||
}
|
||||
|
||||
public static function checkFcgidPhpFpm($fieldname, $fielddata, $newfieldvalue, $allnewfieldvalues)
|
||||
{
|
||||
$returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_OK
|
||||
);
|
||||
|
||||
$check_array = array(
|
||||
'system_mod_fcgid_enabled' => array(
|
||||
'other_post_field' => 'system_phpfpm_enabled',
|
||||
'other_enabled' => 'phpfpm.enabled',
|
||||
'other_enabled_lng' => 'phpfpmstillenabled',
|
||||
'deactivate' => array(
|
||||
'phpfpm.enabled_ownvhost' => 0
|
||||
)
|
||||
),
|
||||
'system_phpfpm_enabled' => array(
|
||||
'other_post_field' => 'system_mod_fcgid_enabled',
|
||||
'other_enabled' => 'system.mod_fcgid',
|
||||
'other_enabled_lng' => 'fcgidstillenabled',
|
||||
'deactivate' => array(
|
||||
'system.mod_fcgid_ownvhost' => 0
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// interface is to be enabled
|
||||
if ((int) $newfieldvalue == 1) {
|
||||
// check for POST value of the other field == 1 (active)
|
||||
if (isset($_POST[$check_array[$fieldname]['other_post_field']]) && (int) $_POST[$check_array[$fieldname]['other_post_field']] == 1) {
|
||||
// the other interface is activated already and STAYS activated
|
||||
if ((int) Settings::Get($check_array[$fieldname]['other_enabled']) == 1) {
|
||||
$returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_ERROR,
|
||||
$check_array[$fieldname]['other_enabled_lng']
|
||||
);
|
||||
} else {
|
||||
// fcgid is being validated before fpm -> "ask" fpm about its state
|
||||
if ($fieldname == 'system_mod_fcgid_enabled') {
|
||||
$returnvalue = checkFcgidPhpFpm('system_phpfpm_enabled', null, $check_array[$fieldname]['other_post_field'], null);
|
||||
} else {
|
||||
// not, bot are nogo
|
||||
$returnvalue = $returnvalue = array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_ERROR,
|
||||
'fcgidandphpfpmnogoodtogether'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (in_array(FORMFIELDS_PLAUSIBILITY_CHECK_OK, $returnvalue)) {
|
||||
// be sure to deactivate the other one for the froxlor-vhost
|
||||
// to avoid having a settings-deadlock
|
||||
foreach ($check_array[$fieldname]['deactivate'] as $setting => $value) {
|
||||
Settings::Set($setting, $value, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $returnvalue;
|
||||
}
|
||||
|
||||
public function checkMysqlAccessHost($fieldname, $fielddata, $newfieldvalue, $allnewfieldvalues)
|
||||
{
|
||||
$mysql_access_host_array = array_map('trim', explode(',', $newfieldvalue));
|
||||
|
||||
foreach ($mysql_access_host_array as $host_entry) {
|
||||
|
||||
if (self::validate_ip2($host_entry, true, 'invalidip', true, true) == false && self::validateDomain($host_entry) == false && self::validateLocalHostname($host_entry) == false && $host_entry != '%') {
|
||||
return array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_ERROR,
|
||||
'invalidmysqlhost',
|
||||
$host_entry
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
FORMFIELDS_PLAUSIBILITY_CHECK_OK
|
||||
);
|
||||
}
|
||||
|
||||
public static function validateSqlInterval($interval = null)
|
||||
{
|
||||
if (! $interval === null || $interval != '') {
|
||||
if (strstr($interval, ' ') !== false) {
|
||||
/*
|
||||
* [0] = ([0-9]+)
|
||||
* [1] = valid SQL-Interval expression
|
||||
*/
|
||||
$valid_expr = array(
|
||||
'SECOND',
|
||||
'MINUTE',
|
||||
'HOUR',
|
||||
'DAY',
|
||||
'WEEK',
|
||||
'MONTH',
|
||||
'YEAR'
|
||||
);
|
||||
|
||||
$interval_parts = explode(' ', $interval);
|
||||
|
||||
if (is_array($interval_parts) && isset($interval_parts[0]) && isset($interval_parts[1])) {
|
||||
if (preg_match('/([0-9]+)/i', $interval_parts[0])) {
|
||||
if (in_array(strtoupper($interval_parts[1]), $valid_expr)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user