3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
33 class CRM_Utils_Type
{
52 // @TODO What's the point of these constants? Backwards compatibility?
54 // These are used for field size (<input type=text size=2>), but redundant TWO=2
55 // usages are rare and should be eliminated. See CRM-18810.
71 * Gets the string representation for a data type.
74 * Integer number identifying the data type.
77 * String identifying the data type, e.g. 'Int' or 'String'.
79 public static function typeToString($type) {
80 // @todo Use constants in the case statements, e.g. "case T_INT:".
81 // @todo return directly, instead of assigning a value.
82 // @todo Use a lookup array, as a property or as a local variable.
119 $string = 'Timestamp';
139 $string = 'Mediumblob';
143 return (isset($string)) ?
$string : "";
147 * Helper function to call escape on arrays
151 public static function escapeAll($data, $type, $abort = TRUE) {
152 foreach ($data as $key => $value) {
153 $data[$key] = CRM_Utils_Type
::escape($value, $type, $abort);
159 * Helper function to call validate on arrays
163 public static function validateAll($data, $type, $abort = TRUE) {
164 foreach ($data as $key => $value) {
165 $data[$key] = CRM_Utils_Type
::validate($value, $type, $abort);
171 * Verify that a variable is of a given type, and apply a bit of processing.
174 * The value to be verified/escaped.
175 * @param string $type
176 * The type to verify against.
178 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
181 * The data, escaped if necessary.
183 public static function escape($data, $type, $abort = TRUE) {
187 if (CRM_Utils_Rule
::integer($data)) {
193 if (CRM_Utils_Rule
::positiveInteger($data)) {
198 // CRM-8925 for custom fields of this type
200 case 'StateProvince':
201 // Handle multivalued data in delimited or array format
202 if (is_array($data) ||
(strpos($data, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE)) {
204 foreach (CRM_Utils_Array
::explodePadded($data) as $item) {
205 if (!CRM_Utils_Rule
::positiveInteger($item)) {
213 elseif (CRM_Utils_Rule
::positiveInteger($data)) {
219 if (CRM_Utils_Rule
::positiveInteger($data)) {
225 if (CRM_Utils_Rule
::url($data = trim($data))) {
231 if (CRM_Utils_Rule
::boolean($data)) {
238 if (CRM_Utils_Rule
::numeric($data)) {
246 return CRM_Core_DAO
::escapeString($data);
250 // a null date or timestamp is valid
251 if (strlen(trim($data)) == 0) {
255 if ((preg_match('/^\d{8}$/', $data) ||
256 preg_match('/^\d{14}$/', $data)
258 CRM_Utils_Rule
::mysqlDate($data)
264 case 'ContactReference':
265 if (strlen(trim($data)) == 0) {
269 if (CRM_Utils_Rule
::validContact($data)) {
274 case 'MysqlColumnNameOrAlias':
275 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
276 $data = str_replace('`', '', $data);
277 $parts = explode('.', $data);
278 $data = '`' . implode('`.`', $parts) . '`';
284 case 'MysqlOrderByDirection':
285 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
286 return strtolower($data);
291 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
292 $parts = explode(',', $data);
293 foreach ($parts as &$part) {
294 $part = preg_replace_callback('/^(?:(?:((?:`[\w-]{1,64}`|[\w-]{1,64}))(?:\.))?(`[\w-]{1,64}`|[\w-]{1,64})(?: (asc|desc))?)$/i', array('CRM_Utils_Type', 'mysqlOrderByCallback'), trim($part));
296 return implode(', ', $parts);
301 CRM_Core_Error
::fatal(
302 $type . " is not a recognised (camel cased) data type."
307 // @todo Use exceptions instead of CRM_Core_Error::fatal().
309 $data = htmlentities($data);
310 CRM_Core_Error
::fatal("$data is not of the type $type");
316 * Verify that a variable is of a given type.
319 * The value to validate.
320 * @param string $type
321 * The type to validate against.
323 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
325 * The name of the attribute
328 * The data, escaped if necessary
330 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
334 if (CRM_Utils_Rule
::integer($data)) {
340 if (CRM_Utils_Rule
::positiveInteger($data)) {
346 if (CRM_Utils_Rule
::boolean($data)) {
353 if (CRM_Utils_Rule
::numeric($data)) {
365 // a null date is valid
366 if (strlen(trim($data)) == 0) {
370 if (preg_match('/^\d{8}$/', $data) &&
371 CRM_Utils_Rule
::mysqlDate($data)
378 // a null timestamp is valid
379 if (strlen(trim($data)) == 0) {
383 if ((preg_match('/^\d{14}$/', $data) ||
384 preg_match('/^\d{8}$/', $data)
386 CRM_Utils_Rule
::mysqlDate($data)
392 case 'ContactReference':
394 if (strlen(trim($data)) == 0) {
398 if (CRM_Utils_Rule
::validContact($data)) {
403 case 'MysqlColumnNameOrAlias':
404 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
409 case 'MysqlOrderByDirection':
410 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
411 return strtolower($data);
416 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
422 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
427 $data = htmlentities($data);
428 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");
435 * preg_replace_callback for MysqlOrderBy escape.
437 public static function mysqlOrderByCallback($matches) {
439 $matches = str_replace('`', '', $matches);
442 if (isset($matches[1]) && $matches[1]) {
443 $output .= '`' . $matches[1] . '`.';
447 if (isset($matches[2]) && $matches[2]) {
448 $output .= '`' . $matches[2] . '`';
452 if (isset($matches[3]) && $matches[3]) {
453 $output .= ' ' . $matches[3];
460 * Get list of avaliable Data Tupes for Option Groups
464 public static function dataTypes() {
474 return array_combine($types, $types);