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);
315 foreach ($parts as &$part) {
316 $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));
318 return implode(', ', $parts);
323 CRM_Core_Error
::fatal(
324 $type . " is not a recognised (camel cased) data type."
329 // @todo Use exceptions instead of CRM_Core_Error::fatal().
331 $data = htmlentities($data);
332 CRM_Core_Error
::fatal("$data is not of the type $type");
338 * Verify that a variable is of a given type.
341 * The value to validate.
342 * @param string $type
343 * The type to validate against.
345 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
347 * The name of the attribute
350 * The data, escaped if necessary
352 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
356 if (CRM_Utils_Rule
::integer($data)) {
362 if (CRM_Utils_Rule
::positiveInteger($data)) {
368 if (CRM_Utils_Rule
::boolean($data)) {
375 if (CRM_Utils_Rule
::numeric($data)) {
387 // a null date is valid
388 if (strlen(trim($data)) == 0) {
392 if (preg_match('/^\d{8}$/', $data) &&
393 CRM_Utils_Rule
::mysqlDate($data)
400 // a null timestamp is valid
401 if (strlen(trim($data)) == 0) {
405 if ((preg_match('/^\d{14}$/', $data) ||
406 preg_match('/^\d{8}$/', $data)
408 CRM_Utils_Rule
::mysqlDate($data)
414 case 'ContactReference':
416 if (strlen(trim($data)) == 0) {
420 if (CRM_Utils_Rule
::validContact($data)) {
425 case 'MysqlColumnNameOrAlias':
426 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
431 case 'MysqlOrderByDirection':
432 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
433 return strtolower($data);
438 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
444 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
449 $data = htmlentities($data);
450 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");
457 * preg_replace_callback for MysqlOrderBy escape.
459 public static function mysqlOrderByCallback($matches) {
461 $matches = str_replace('`', '', $matches);
464 if (isset($matches[1]) && $matches[1]) {
465 $output .= '`' . $matches[1] . '`.';
469 if (isset($matches[2]) && $matches[2]) {
470 $output .= '`' . $matches[2] . '`';
474 if (isset($matches[3]) && $matches[3]) {
475 $output .= ' ' . $matches[3];
482 * Get list of avaliable Data Tupes for Option Groups
486 public static function dataTypes() {
496 return array_combine($types, $types);