3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 * Maximum size of a MySQL BLOB or TEXT column in bytes.
73 const BLOB_SIZE
= 65535;
76 * Maximum value of a MySQL signed INT column.
78 const INT_MAX
= 2147483647;
81 * Gets the string representation for a data type.
84 * Integer number identifying the data type.
87 * String identifying the data type, e.g. 'Int' or 'String'.
89 public static function typeToString($type) {
90 // @todo Use constants in the case statements, e.g. "case T_INT:".
91 // @todo return directly, instead of assigning a value.
92 // @todo Use a lookup array, as a property or as a local variable.
129 $string = 'Timestamp';
149 $string = 'Mediumblob';
153 return (isset($string)) ?
$string : "";
158 * An array of type in the form 'type name' => 'int representing type'
160 public static function getValidTypes() {
162 'Int' => self
::T_INT
,
163 'String' => self
::T_STRING
,
164 'Enum' => self
::T_ENUM
,
165 'Date' => self
::T_DATE
,
166 'Time' => self
::T_TIME
,
167 'Boolean' => self
::T_BOOLEAN
,
168 'Text' => self
::T_TEXT
,
169 'Blob' => self
::T_BLOB
,
170 'Timestamp' => self
::T_TIMESTAMP
,
171 'Float' => self
::T_FLOAT
,
172 'Money' => self
::T_MONEY
,
173 'Email' => self
::T_EMAIL
,
174 'Mediumblob' => self
::T_MEDIUMBLOB
,
179 * Get the data_type for the field.
181 * @param array $fieldMetadata
182 * Metadata about the field.
186 public static function getDataTypeFromFieldMetadata($fieldMetadata) {
187 if (isset($fieldMetadata['data_type'])) {
188 return $fieldMetadata['data_type'];
190 if (empty($fieldMetadata['type'])) {
191 // I would prefer to throw an e-notice but there is some,
192 // probably unnecessary logic, that only retrieves activity fields
193 // if they are 'in the profile' and probably they are not 'in'
194 // until they are added - which might lead to ? who knows!
197 return self
::typeToString($fieldMetadata['type']);
201 * Helper function to call escape on arrays.
205 public static function escapeAll($data, $type, $abort = TRUE) {
206 foreach ($data as $key => $value) {
207 $data[$key] = CRM_Utils_Type
::escape($value, $type, $abort);
213 * Helper function to call validate on arrays
217 public static function validateAll($data, $type, $abort = TRUE) {
218 foreach ($data as $key => $value) {
219 $data[$key] = CRM_Utils_Type
::validate($value, $type, $abort);
225 * Verify that a variable is of a given type, and apply a bit of processing.
228 * The value to be verified/escaped.
229 * @param string $type
230 * The type to verify against.
232 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
235 * The data, escaped if necessary.
237 public static function escape($data, $type, $abort = TRUE) {
241 if (CRM_Utils_Rule
::integer($data)) {
247 if (CRM_Utils_Rule
::positiveInteger($data)) {
252 // CRM-8925 for custom fields of this type
254 case 'StateProvince':
255 // Handle multivalued data in delimited or array format
256 if (is_array($data) ||
(strpos($data, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE)) {
258 foreach (CRM_Utils_Array
::explodePadded($data) as $item) {
259 if (!CRM_Utils_Rule
::positiveInteger($item)) {
267 elseif (CRM_Utils_Rule
::positiveInteger($data)) {
273 if (CRM_Utils_Rule
::positiveInteger($data)) {
279 if (CRM_Utils_Rule
::url($data = trim($data))) {
285 if (CRM_Utils_Rule
::boolean($data)) {
292 if (CRM_Utils_Rule
::numeric($data)) {
300 return CRM_Core_DAO
::escapeString($data);
304 // a null date or timestamp is valid
305 if (strlen(trim($data)) == 0) {
309 if ((preg_match('/^\d{8}$/', $data) ||
310 preg_match('/^\d{14}$/', $data)
312 CRM_Utils_Rule
::mysqlDate($data)
318 case 'ContactReference':
319 if (strlen(trim($data)) == 0) {
323 if (CRM_Utils_Rule
::validContact($data)) {
328 case 'MysqlColumnNameOrAlias':
329 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
330 $data = str_replace('`', '', $data);
331 $parts = explode('.', $data);
332 $data = '`' . implode('`.`', $parts) . '`';
338 case 'MysqlOrderByDirection':
339 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
340 return strtolower($data);
345 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
346 $parts = explode(',', $data);
348 // The field() syntax is tricky here because it uses commas & when
349 // we separate by them we break it up. But we want to keep the clauses in order.
350 // so we just clumsily re-assemble it. Test cover exists.
351 $fieldClauseStart = NULL;
352 foreach ($parts as $index => &$part) {
353 if (substr($part, 0, 6) === 'field(') {
354 // Looking to escape a string like 'field(contribution_status_id,3,4,5) asc'
355 // to 'field(`contribution_status_id`,3,4,5) asc'
356 $fieldClauseStart = $index;
359 if ($fieldClauseStart !== NULL) {
360 // this is part of the list of field options. Concatenate it back on.
361 $parts[$fieldClauseStart] .= ',' . $part;
362 unset($parts[$index]);
363 if (!strstr($parts[$fieldClauseStart], ')')) {
364 // we have not reached the end of the list.
367 // We have the last piece of the field() clause, time to escape it.
368 $parts[$fieldClauseStart] = self
::mysqlOrderByFieldFunctionCallback($parts[$fieldClauseStart]);
369 $fieldClauseStart = NULL;
374 $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));
376 return implode(', ', $parts);
381 CRM_Core_Error
::fatal(
382 $type . " is not a recognised (camel cased) data type."
387 // @todo Use exceptions instead of CRM_Core_Error::fatal().
389 $data = htmlentities($data);
390 CRM_Core_Error
::fatal("$data is not of the type $type");
396 * Verify that a variable is of a given type.
399 * The value to validate.
400 * @param string $type
401 * The type to validate against.
403 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
404 * @param string $name
405 * The name of the attribute
406 * @param bool $isThrowException
407 * Should an exception be thrown rather than a using a deprecated fatal error.
410 * The data, escaped if necessary
412 * @throws \CRM_Core_Exception
414 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ', $isThrowException = FALSE) {
416 $possibleTypes = array(
420 'CommaSeparatedIntegers',
431 'MysqlColumnNameOrAlias',
432 'MysqlOrderByDirection',
438 if (!in_array($type, $possibleTypes)) {
439 if ($isThrowException) {
440 throw new CRM_Core_Exception(ts('Invalid type, must be one of : ' . implode($possibleTypes)));
442 CRM_Core_Error
::fatal(ts('Invalid type, must be one of : ' . implode($possibleTypes)));
447 if (CRM_Utils_Rule
::integer($data)) {
453 if (CRM_Utils_Rule
::positiveInteger($data)) {
458 case 'CommaSeparatedIntegers':
459 if (CRM_Utils_Rule
::commaSeparatedIntegers($data)) {
465 if (CRM_Utils_Rule
::boolean($data)) {
472 if (CRM_Utils_Rule
::numeric($data)) {
484 // a null date is valid
485 if (strlen(trim($data)) == 0) {
489 if (preg_match('/^\d{8}$/', $data) &&
490 CRM_Utils_Rule
::mysqlDate($data)
497 // a null timestamp is valid
498 if (strlen(trim($data)) == 0) {
502 if ((preg_match('/^\d{14}$/', $data) ||
503 preg_match('/^\d{8}$/', $data)
505 CRM_Utils_Rule
::mysqlDate($data)
511 case 'ContactReference':
513 if (strlen(trim($data)) == 0) {
517 if (CRM_Utils_Rule
::validContact($data)) {
522 case 'MysqlColumnNameOrAlias':
523 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
528 case 'MysqlOrderByDirection':
529 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
530 return strtolower($data);
535 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
541 if (CRM_Utils_Rule
::checkExtensionKeyIsValid($data)) {
547 if (CRM_Utils_Rule
::json($data)) {
553 if (CRM_Utils_Rule
::alphanumeric($data)) {
560 $data = htmlentities($data);
561 if ($isThrowException) {
562 throw new CRM_Core_Exception("$name (value: $data) is not of the type $type");
564 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");
571 * Preg_replace_callback for mysqlOrderByFieldFunction escape.
573 * Add backticks around the field name.
575 * @param string $clause
579 public static function mysqlOrderByFieldFunctionCallback($clause) {
580 return preg_replace('/field\((\w*)/', 'field(`${1}`', $clause);
584 * preg_replace_callback for MysqlOrderBy escape.
586 public static function mysqlOrderByCallback($matches) {
588 $matches = str_replace('`', '', $matches);
591 if (isset($matches[1]) && $matches[1]) {
592 $output .= '`' . $matches[1] . '`.';
596 if (isset($matches[2]) && $matches[2]) {
597 $output .= '`' . $matches[2] . '`';
601 if (isset($matches[3]) && $matches[3]) {
602 $output .= ' ' . $matches[3];
609 * Get list of avaliable Data Types for Option Groups
613 public static function dataTypes() {
623 return array_combine($types, $types);