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 * Get the data_type for the field.
149 * @param array $fieldMetadata
150 * Metadata about the field.
154 public static function getDataTypeFromFieldMetadata($fieldMetadata) {
155 if (isset($fieldMetadata['data_type'])) {
156 return $fieldMetadata['data_type'];
158 if (empty($fieldMetadata['type'])) {
159 // I would prefer to throw an e-notice but there is some,
160 // probably unnecessary logic, that only retrieves activity fields
161 // if they are 'in the profile' and probably they are not 'in'
162 // until they are added - which might lead to ? who knows!
165 return self
::typeToString($fieldMetadata['type']);
169 * Helper function to call escape on arrays.
173 public static function escapeAll($data, $type, $abort = TRUE) {
174 foreach ($data as $key => $value) {
175 $data[$key] = CRM_Utils_Type
::escape($value, $type, $abort);
181 * Helper function to call validate on arrays
185 public static function validateAll($data, $type, $abort = TRUE) {
186 foreach ($data as $key => $value) {
187 $data[$key] = CRM_Utils_Type
::validate($value, $type, $abort);
193 * Verify that a variable is of a given type, and apply a bit of processing.
196 * The value to be verified/escaped.
197 * @param string $type
198 * The type to verify against.
200 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
203 * The data, escaped if necessary.
205 public static function escape($data, $type, $abort = TRUE) {
209 if (CRM_Utils_Rule
::integer($data)) {
215 if (CRM_Utils_Rule
::positiveInteger($data)) {
220 // CRM-8925 for custom fields of this type
222 case 'StateProvince':
223 // Handle multivalued data in delimited or array format
224 if (is_array($data) ||
(strpos($data, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE)) {
226 foreach (CRM_Utils_Array
::explodePadded($data) as $item) {
227 if (!CRM_Utils_Rule
::positiveInteger($item)) {
235 elseif (CRM_Utils_Rule
::positiveInteger($data)) {
241 if (CRM_Utils_Rule
::positiveInteger($data)) {
247 if (CRM_Utils_Rule
::url($data = trim($data))) {
253 if (CRM_Utils_Rule
::boolean($data)) {
260 if (CRM_Utils_Rule
::numeric($data)) {
268 return CRM_Core_DAO
::escapeString($data);
272 // a null date or timestamp is valid
273 if (strlen(trim($data)) == 0) {
277 if ((preg_match('/^\d{8}$/', $data) ||
278 preg_match('/^\d{14}$/', $data)
280 CRM_Utils_Rule
::mysqlDate($data)
286 case 'ContactReference':
287 if (strlen(trim($data)) == 0) {
291 if (CRM_Utils_Rule
::validContact($data)) {
296 case 'MysqlColumnNameOrAlias':
297 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
298 $data = str_replace('`', '', $data);
299 $parts = explode('.', $data);
300 $data = '`' . implode('`.`', $parts) . '`';
306 case 'MysqlOrderByDirection':
307 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
308 return strtolower($data);
313 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
314 $parts = explode(',', $data);
316 // The field() syntax is tricky here because it uses commas & when
317 // we separate by them we break it up. But we want to keep the clauses in order.
318 // so we just clumsily re-assemble it. Test cover exists.
319 $fieldClauseStart = NULL;
320 foreach ($parts as $index => &$part) {
321 if (substr($part, 0, 6) === 'field(') {
322 // Looking to escape a string like 'field(contribution_status_id,3,4,5) asc'
323 // to 'field(`contribution_status_id`,3,4,5) asc'
324 $fieldClauseStart = $index;
327 if ($fieldClauseStart !== NULL) {
328 // this is part of the list of field options. Concatenate it back on.
329 $parts[$fieldClauseStart] .= ',' . $part;
330 unset($parts[$index]);
331 if (!strstr($parts[$fieldClauseStart], ')')) {
332 // we have not reached the end of the list.
335 // We have the last piece of the field() clause, time to escape it.
336 $parts[$fieldClauseStart] = self
::mysqlOrderByFieldFunctionCallback($parts[$fieldClauseStart]);
337 $fieldClauseStart = NULL;
342 $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));
344 return implode(', ', $parts);
349 CRM_Core_Error
::fatal(
350 $type . " is not a recognised (camel cased) data type."
355 // @todo Use exceptions instead of CRM_Core_Error::fatal().
357 $data = htmlentities($data);
358 CRM_Core_Error
::fatal("$data is not of the type $type");
364 * Verify that a variable is of a given type.
367 * The value to validate.
368 * @param string $type
369 * The type to validate against.
371 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
373 * The name of the attribute
376 * The data, escaped if necessary
378 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
382 if (CRM_Utils_Rule
::integer($data)) {
388 if (CRM_Utils_Rule
::positiveInteger($data)) {
394 if (CRM_Utils_Rule
::boolean($data)) {
401 if (CRM_Utils_Rule
::numeric($data)) {
413 // a null date is valid
414 if (strlen(trim($data)) == 0) {
418 if (preg_match('/^\d{8}$/', $data) &&
419 CRM_Utils_Rule
::mysqlDate($data)
426 // a null timestamp is valid
427 if (strlen(trim($data)) == 0) {
431 if ((preg_match('/^\d{14}$/', $data) ||
432 preg_match('/^\d{8}$/', $data)
434 CRM_Utils_Rule
::mysqlDate($data)
440 case 'ContactReference':
442 if (strlen(trim($data)) == 0) {
446 if (CRM_Utils_Rule
::validContact($data)) {
451 case 'MysqlColumnNameOrAlias':
452 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
457 case 'MysqlOrderByDirection':
458 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
459 return strtolower($data);
464 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
470 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
475 $data = htmlentities($data);
476 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");
483 * Preg_replace_callback for mysqlOrderByFieldFunction escape.
485 * Add backticks around the field name.
487 * @param string $clause
491 public static function mysqlOrderByFieldFunctionCallback($clause) {
492 return preg_replace('/field\((\w*)/', 'field(`${1}`', $clause);
496 * preg_replace_callback for MysqlOrderBy escape.
498 public static function mysqlOrderByCallback($matches) {
500 $matches = str_replace('`', '', $matches);
503 if (isset($matches[1]) && $matches[1]) {
504 $output .= '`' . $matches[1] . '`.';
508 if (isset($matches[2]) && $matches[2]) {
509 $output .= '`' . $matches[2] . '`';
513 if (isset($matches[3]) && $matches[3]) {
514 $output .= ' ' . $matches[3];
521 * Get list of avaliable Data Tupes for Option Groups
525 public static function dataTypes() {
535 return array_combine($types, $types);