3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Utils_Type
{
69 * Convert Constant Data type to String
71 * @param $type integer datatype
73 * @return $string String datatype respective to integer datatype
78 static function typeToString($type) {
115 $string = 'Timestamp';
135 $string = 'Mediumblob';
139 return (isset($string)) ?
$string : "";
143 * Verify that a variable is of a given type
145 * @param mixed $data The variable
146 * @param string $type The type
147 * @param boolean $abort Should we abort if invalid
149 * @return mixed The data, escaped if necessary
153 public static function escape($data, $type, $abort = TRUE) {
157 if (CRM_Utils_Rule
::integer($data)) {
163 // the below 2 are for custom fields of this type
166 case 'StateProvince':
167 // Checked for multi valued state/country value
168 if (is_array($data)) {
170 foreach ($data as $data) {
171 if (CRM_Utils_Rule
::positiveInteger($data) || CRM_Core_DAO
::escapeString($data)) {
182 elseif (!is_numeric($data) && CRM_Core_DAO
::escapeString($data)) {
185 elseif (CRM_Utils_Rule
::positiveInteger($data)) {
191 if (CRM_Utils_Rule
::url($data = trim($data))) {
197 if (CRM_Utils_Rule
::boolean($data)) {
204 if (CRM_Utils_Rule
::numeric($data)) {
212 return CRM_Core_DAO
::escapeString($data);
216 // a null date or timestamp is valid
217 if (strlen(trim($data)) == 0) {
221 if ((preg_match('/^\d{8}$/', $data) ||
222 preg_match('/^\d{14}$/', $data)
224 CRM_Utils_Rule
::mysqlDate($data)
230 case 'ContactReference':
231 if (strlen(trim($data)) == 0) {
235 if (CRM_Utils_Rule
::validContact($data)) {
241 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
246 $data = htmlentities($data);
247 CRM_Core_Error
::fatal("$data is not of the type $type");
253 * Verify that a variable is of a given type
255 * @param mixed $data The variable
256 * @param string $type The type
257 * @param boolean $abort Should we abort if invalid
258 * @name string $name The name of the attribute
260 * @return mixed The data, escaped if necessary
264 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
268 if (CRM_Utils_Rule
::integer($data)) {
274 if (CRM_Utils_Rule
::positiveInteger($data)) {
280 if (CRM_Utils_Rule
::boolean($data)) {
287 if (CRM_Utils_Rule
::numeric($data)) {
299 // a null date is valid
300 if (strlen(trim($data)) == 0) {
304 if (preg_match('/^\d{8}$/', $data) &&
305 CRM_Utils_Rule
::mysqlDate($data)
312 // a null timestamp is valid
313 if (strlen(trim($data)) == 0) {
317 if ((preg_match('/^\d{14}$/', $data) ||
318 preg_match('/^\d{8}$/', $data)
320 CRM_Utils_Rule
::mysqlDate($data)
326 case 'ContactReference':
328 if (strlen(trim($data)) == 0) {
332 if (CRM_Utils_Rule
::validContact($data)) {
338 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
343 $data = htmlentities($data);
344 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");