X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FRule.php;h=650f5e6883d9d246db389e074c1e18a4d20917ed;hb=35b63106af90ff25cfc1198363036acbd12a9529;hp=ff361257a1619df6ac08ab4f07852353c2efdc9f;hpb=22ca1f122e20a8a01412ab818f4a3e33bf2b4d00;p=civicrm-core.git diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index ff361257a1..650f5e6883 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -3,7 +3,7 @@ +--------------------------------------------------------------------+ | CiviCRM version 4.7 | +--------------------------------------------------------------------+ - | Copyright CiviCRM LLC (c) 2004-2015 | + | Copyright CiviCRM LLC (c) 2004-2016 | +--------------------------------------------------------------------+ | This file is a part of CiviCRM. | | | @@ -28,7 +28,7 @@ /** * * @package CRM - * @copyright CiviCRM LLC (c) 2004-2015 + * @copyright CiviCRM LLC (c) 2004-2016 */ require_once 'HTML/QuickForm/Rule/Email.php'; @@ -87,6 +87,65 @@ class CRM_Utils_Rule { return TRUE; } + /** + * Validate an acceptable column name for sorting results. + * + * @param $str + * + * @return bool + */ + public static function mysqlColumnName($str) { + // Check not empty. + if (empty($str)) { + return FALSE; + } + + // Ensure it only contains valid characters (alphanumeric and underscores). + // + // MySQL permits column names that don't match this (eg containing spaces), + // but CiviCRM won't create those ... + if (!preg_match('/^\w{1,64}(\.\w{1,64})?$/i', $str)) { + return FALSE; + } + + return TRUE; + } + + /** + * Validate that a string is ASC or DESC. + * + * Empty string should be treated as invalid and ignored => default = ASC. + * + * @param $str + * @return bool + */ + public static function mysqlOrderByDirection($str) { + if (!preg_match('/^(asc|desc)$/i', $str)) { + return FALSE; + } + + return TRUE; + } + + /** + * Validate that a string is valid order by clause. + * + * @param $str + * @return bool + */ + public static function mysqlOrderBy($str) { + // Making a regex for a comma separated list is quite hard and not readable + // at all, so we split and loop over. + $parts = explode(',', $str); + foreach ($parts as $part) { + if (!preg_match('/^((\w{1,64})((\.)(\w{1,64}))?( (asc|desc))?)$/i', trim($part))) { + return FALSE; + } + } + + return TRUE; + } + /** * @param $str * @@ -636,7 +695,7 @@ class CRM_Utils_Rule { * @param string $value * The value of the field we are checking. * @param array $options - * The daoName and fieldName (optional ). + * The daoName, fieldName (optional) and DomainID (optional). * * @return bool * true if object exists @@ -647,7 +706,7 @@ class CRM_Utils_Rule { $name = $options[2]; } - return CRM_Core_DAO::objectExists($value, CRM_Utils_Array::value(0, $options), CRM_Utils_Array::value(1, $options), CRM_Utils_Array::value(2, $options, $name)); + return CRM_Core_DAO::objectExists($value, CRM_Utils_Array::value(0, $options), CRM_Utils_Array::value(1, $options), CRM_Utils_Array::value(2, $options, $name), CRM_Utils_Array::value(3, $options)); } /** @@ -737,23 +796,6 @@ class CRM_Utils_Rule { return is_dir(Civi::paths()->getPath($path)); } - /** - * @param $value - * @param $options - * - * @return bool - */ - public static function autocomplete($value, $options) { - if ($value) { - $selectOption = CRM_Core_BAO_CustomOption::valuesByID($options['fieldID'], $options['optionGroupID']); - - if (!in_array($value, $selectOption)) { - return FALSE; - } - } - return TRUE; - } - /** * @param $value * @param null $actualElementValue @@ -829,4 +871,25 @@ class CRM_Utils_Rule { return ($key) ? CRM_Core_Key::valid($key) : FALSE; } + /** + * Check if the values in the date range are in correct chronological order. + * + * @param array $fields + * Fields of the form. + * @param $fieldName + * Name of date range field. + * @param $errors + * The error array. + * @param $title + * Title of the date range to be displayed in the error message. + */ + public static function validDateRange($fields, $fieldName, &$errors, $title) { + $lowDate = strtotime($fields[$fieldName . '_low']); + $highDate = strtotime($fields[$fieldName . '_high']); + + if ($lowDate > $highDate) { + $errors[$fieldName . '_range_error'] = ts('%1: Please check that your date range is in correct chronological order.', array(1 => $title)); + } + } + }