3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
33 class CRM_Utils_Type
{
52 // @todo What's the point of these constants? Backwards compatibility?
68 * Gets the string representation for a data type.
71 * Integer number identifying the data type.
74 * String identifying the data type, e.g. 'Int' or 'String'.
76 public static function typeToString($type) {
77 // @todo Use constants in the case statements, e.g. "case T_INT:".
78 // @todo return directly, instead of assigning a value.
79 // @todo Use a lookup array, as a property or as a local variable.
116 $string = 'Timestamp';
136 $string = 'Mediumblob';
140 return (isset($string)) ?
$string : "";
144 * Helper function to call escape on arrays
148 public static function escapeAll($data, $type, $abort = TRUE) {
149 foreach ($data as $key => $value) {
150 $data[$key] = CRM_Utils_Type
::escape($value, $type, $abort);
156 * Helper function to call validate on arrays
160 public static function validateAll($data, $type, $abort = TRUE) {
161 foreach ($data as $key => $value) {
162 $data[$key] = CRM_Utils_Type
::validate($value, $type, $abort);
168 * Verify that a variable is of a given type, and apply a bit of processing.
171 * The value to be verified/escaped.
172 * @param string $type
173 * The type to verify against.
175 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
178 * The data, escaped if necessary.
180 public static function escape($data, $type, $abort = TRUE) {
184 if (CRM_Utils_Rule
::integer($data)) {
190 if (CRM_Utils_Rule
::positiveInteger($data)) {
195 // CRM-8925 for custom fields of this type
197 case 'StateProvince':
198 // Handle multivalued data in delimited or array format
199 if (is_array($data) ||
(strpos($data, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE)) {
201 foreach (CRM_Utils_Array
::explodePadded($data) as $item) {
202 if (!CRM_Utils_Rule
::positiveInteger($item)) {
210 elseif (CRM_Utils_Rule
::positiveInteger($data)) {
216 if (CRM_Utils_Rule
::positiveInteger($data)) {
222 if (CRM_Utils_Rule
::url($data = trim($data))) {
228 if (CRM_Utils_Rule
::boolean($data)) {
235 if (CRM_Utils_Rule
::numeric($data)) {
243 return CRM_Core_DAO
::escapeString($data);
247 // a null date or timestamp is valid
248 if (strlen(trim($data)) == 0) {
252 if ((preg_match('/^\d{8}$/', $data) ||
253 preg_match('/^\d{14}$/', $data)
255 CRM_Utils_Rule
::mysqlDate($data)
261 case 'ContactReference':
262 if (strlen(trim($data)) == 0) {
266 if (CRM_Utils_Rule
::validContact($data)) {
271 case 'MysqlColumnNameOrAlias':
272 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
273 $data = str_replace('`', '', $data);
274 $parts = explode('.', $data);
275 $data = '`' . implode('`.`', $parts) . '`';
281 case 'MysqlOrderByDirection':
282 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
283 return strtolower($data);
288 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
289 $parts = explode(',', $data);
290 foreach ($parts as &$part) {
291 $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));
293 return implode(', ', $parts);
298 CRM_Core_Error
::fatal(
299 $type . " is not a recognised (camel cased) data type."
304 // @todo Use exceptions instead of CRM_Core_Error::fatal().
306 $data = htmlentities($data);
307 CRM_Core_Error
::fatal("$data is not of the type $type");
313 * Verify that a variable is of a given type.
316 * The value to validate.
317 * @param string $type
318 * The type to validate against.
320 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
322 * The name of the attribute
325 * The data, escaped if necessary
327 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
331 if (CRM_Utils_Rule
::integer($data)) {
337 if (CRM_Utils_Rule
::positiveInteger($data)) {
343 if (CRM_Utils_Rule
::boolean($data)) {
350 if (CRM_Utils_Rule
::numeric($data)) {
362 // a null date is valid
363 if (strlen(trim($data)) == 0) {
367 if (preg_match('/^\d{8}$/', $data) &&
368 CRM_Utils_Rule
::mysqlDate($data)
375 // a null timestamp is valid
376 if (strlen(trim($data)) == 0) {
380 if ((preg_match('/^\d{14}$/', $data) ||
381 preg_match('/^\d{8}$/', $data)
383 CRM_Utils_Rule
::mysqlDate($data)
389 case 'ContactReference':
391 if (strlen(trim($data)) == 0) {
395 if (CRM_Utils_Rule
::validContact($data)) {
400 case 'MysqlColumnNameOrAlias':
401 if (CRM_Utils_Rule
::mysqlColumnNameOrAlias($data)) {
406 case 'MysqlOrderByDirection':
407 if (CRM_Utils_Rule
::mysqlOrderByDirection($data)) {
408 return strtolower($data);
413 if (CRM_Utils_Rule
::mysqlOrderBy($data)) {
419 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
424 $data = htmlentities($data);
425 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");
432 * preg_replace_callback for MysqlOrderBy escape.
434 public static function mysqlOrderByCallback($matches) {
436 $matches = str_replace('`', '', $matches);
439 if (isset($matches[1]) && $matches[1]) {
440 $output .= '`' . $matches[1] . '`.';
444 if (isset($matches[2]) && $matches[2]) {
445 $output .= '`' . $matches[2] . '`';
449 if (isset($matches[3]) && $matches[3]) {
450 $output .= ' ' . $matches[3];