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 String datatype respective to integer datatype@access public
76 static function typeToString($type) {
113 $string = 'Timestamp';
133 $string = 'Mediumblob';
137 return (isset($string)) ?
$string : "";
141 * Verify that a variable is of a given type
143 * @param mixed $data The variable
144 * @param string $type The type
145 * @param boolean $abort Should we abort if invalid
147 * @return mixed The data, escaped if necessary
151 public static function escape($data, $type, $abort = TRUE) {
155 if (CRM_Utils_Rule
::integer($data)) {
161 // the below 2 are for custom fields of this type
164 case 'StateProvince':
165 // Checked for multi valued state/country value
166 if (is_array($data)) {
168 foreach ($data as $data) {
169 if (CRM_Utils_Rule
::positiveInteger($data) || CRM_Core_DAO
::escapeString($data)) {
180 elseif (!is_numeric($data) && CRM_Core_DAO
::escapeString($data)) {
183 elseif (CRM_Utils_Rule
::positiveInteger($data)) {
189 if (CRM_Utils_Rule
::url($data = trim($data))) {
195 if (CRM_Utils_Rule
::boolean($data)) {
202 if (CRM_Utils_Rule
::numeric($data)) {
210 return CRM_Core_DAO
::escapeString($data);
214 // a null date or timestamp is valid
215 if (strlen(trim($data)) == 0) {
219 if ((preg_match('/^\d{8}$/', $data) ||
220 preg_match('/^\d{14}$/', $data)
222 CRM_Utils_Rule
::mysqlDate($data)
228 case 'ContactReference':
229 if (strlen(trim($data)) == 0) {
233 if (CRM_Utils_Rule
::validContact($data)) {
239 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
244 $data = htmlentities($data);
245 CRM_Core_Error
::fatal("$data is not of the type $type");
251 * Verify that a variable is of a given type
253 * @param mixed $data The variable
254 * @param string $type The type
255 * @param boolean $abort Should we abort if invalid
256 * @name string $name The name of the attribute
258 * @return mixed The data, escaped if necessary
262 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
266 if (CRM_Utils_Rule
::integer($data)) {
272 if (CRM_Utils_Rule
::positiveInteger($data)) {
278 if (CRM_Utils_Rule
::boolean($data)) {
285 if (CRM_Utils_Rule
::numeric($data)) {
297 // a null date is valid
298 if (strlen(trim($data)) == 0) {
302 if (preg_match('/^\d{8}$/', $data) &&
303 CRM_Utils_Rule
::mysqlDate($data)
310 // a null timestamp is valid
311 if (strlen(trim($data)) == 0) {
315 if ((preg_match('/^\d{14}$/', $data) ||
316 preg_match('/^\d{8}$/', $data)
318 CRM_Utils_Rule
::mysqlDate($data)
324 case 'ContactReference':
326 if (strlen(trim($data)) == 0) {
330 if (CRM_Utils_Rule
::validContact($data)) {
336 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
341 $data = htmlentities($data);
342 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");