3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 class CRM_Utils_Type
{
54 // @todo What's the point of these constants? Backwards compatibility?
70 * Gets the string representation for a data type.
73 * Integer number identifying the data type.
76 * String identifying the data type, e.g. 'Int' or 'String'.
78 public static function typeToString($type) {
79 // @todo Use constants in the case statements, e.g. "case T_INT:".
80 // @todo return directly, instead of assigning a value.
81 // @todo Use a lookup array, as a property or as a local variable.
118 $string = 'Timestamp';
138 $string = 'Mediumblob';
142 return (isset($string)) ?
$string : "";
146 * Helper function to call escape on arrays
150 public static function escapeAll($data, $type, $abort = TRUE) {
151 foreach ($data as $key => $value) {
152 $data[$key] = CRM_Utils_Type
::escape($value, $type, $abort);
158 * Verify that a variable is of a given type, and apply a bit of processing.
161 * The value to be verified/escaped.
162 * @param string $type
163 * The type to verify against.
165 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
168 * The data, escaped if necessary.
170 public static function escape($data, $type, $abort = TRUE) {
174 if (CRM_Utils_Rule
::integer($data)) {
180 if (CRM_Utils_Rule
::positiveInteger($data)) {
185 // CRM-8925 for custom fields of this type
187 case 'StateProvince':
188 // Handle multivalued data in delimited or array format
189 if (is_array($data) ||
(strpos($data, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE)) {
191 foreach (CRM_Utils_Array
::explodePadded($data) as $item) {
192 if (!CRM_Utils_Rule
::positiveInteger($item)) {
200 elseif (CRM_Utils_Rule
::positiveInteger($data)) {
206 if (CRM_Utils_Rule
::positiveInteger($data)) {
212 if (CRM_Utils_Rule
::url($data = trim($data))) {
218 if (CRM_Utils_Rule
::boolean($data)) {
225 if (CRM_Utils_Rule
::numeric($data)) {
233 return CRM_Core_DAO
::escapeString($data);
237 // a null date or timestamp is valid
238 if (strlen(trim($data)) == 0) {
242 if ((preg_match('/^\d{8}$/', $data) ||
243 preg_match('/^\d{14}$/', $data)
245 CRM_Utils_Rule
::mysqlDate($data)
251 case 'ContactReference':
252 if (strlen(trim($data)) == 0) {
256 if (CRM_Utils_Rule
::validContact($data)) {
261 case 'MysqlColumnNameOrAlias':
262 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
263 $data = str_replace('`', '', $data);
264 $parts = explode('.', $data);
265 $data = '`' . implode('`.`', $parts) . '`';
271 case 'MysqlOrderByDirection':
272 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
273 return strtolower($data);
278 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
279 $parts = explode(',', $data);
280 foreach ($parts as &$part) {
281 $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));
283 return implode(', ', $parts);
288 CRM_Core_Error
::fatal(
289 $type . " is not a recognised (camel cased) data type."
294 // @todo Use exceptions instead of CRM_Core_Error::fatal().
296 $data = htmlentities($data);
297 CRM_Core_Error
::fatal("$data is not of the type $type");
303 * Helper function to call validate on arrays
307 public static function validateAll($data, $type, $abort = TRUE) {
308 foreach ($data as $key => $value) {
309 $data[$key] = CRM_Utils_Type
::validate($value, $type, $abort);
315 * Verify that a variable is of a given type.
318 * The value to validate.
319 * @param string $type
320 * The type to validate against.
322 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
324 * The name of the attribute
327 * The data, escaped if necessary
329 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
333 if (CRM_Utils_Rule
::integer($data)) {
339 if (CRM_Utils_Rule
::positiveInteger($data)) {
345 if (CRM_Utils_Rule
::boolean($data)) {
352 if (CRM_Utils_Rule
::numeric($data)) {
364 // a null date is valid
365 if (strlen(trim($data)) == 0) {
369 if (preg_match('/^\d{8}$/', $data) &&
370 CRM_Utils_Rule
::mysqlDate($data)
377 // a null timestamp is valid
378 if (strlen(trim($data)) == 0) {
382 if ((preg_match('/^\d{14}$/', $data) ||
383 preg_match('/^\d{8}$/', $data)
385 CRM_Utils_Rule
::mysqlDate($data)
391 case 'ContactReference':
393 if (strlen(trim($data)) == 0) {
397 if (CRM_Utils_Rule
::validContact($data)) {
402 case 'MysqlColumnNameOrAlias':
403 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
408 case 'MysqlOrderByDirection':
409 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
410 return strtolower($data);
415 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
421 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
426 $data = htmlentities($data);
427 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");
434 * preg_replace_callback for MysqlOrderBy escape.
436 public static function mysqlOrderByCallback($matches) {
438 $matches = str_replace('`', '', $matches);
440 if (isset($matches[1]) && $matches[1]) {
441 $output .= '`' . $matches[1] . '`.';
444 if (isset($matches[2]) && $matches[2]) {
445 $output .= '`' . $matches[2] . '`';
448 if (isset($matches[3]) && $matches[3]) {
449 $output .= ' ' . $matches[3];