| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
| 32 | */ |
| 33 | |
| 34 | require_once 'HTML/QuickForm/Rule/Email.php'; |
| 35 | |
| 36 | /** |
| 37 | * Class CRM_Utils_Rule |
| 38 | */ |
| 39 | class CRM_Utils_Rule { |
| 40 | |
| 41 | /** |
| 42 | * @param $str |
| 43 | * @param int $maxLength |
| 44 | * |
| 45 | * @return bool |
| 46 | */ |
| 47 | public static function title($str, $maxLength = 127) { |
| 48 | |
| 49 | // check length etc |
| 50 | if (empty($str) || strlen($str) > $maxLength) { |
| 51 | return FALSE; |
| 52 | } |
| 53 | |
| 54 | // Make sure it include valid characters, alpha numeric and underscores |
| 55 | if (!preg_match('/^\w[\w\s\'\&\,\$\#\-\.\"\?\!]+$/i', $str)) { |
| 56 | return FALSE; |
| 57 | } |
| 58 | |
| 59 | return TRUE; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param $str |
| 64 | * |
| 65 | * @return bool |
| 66 | */ |
| 67 | public static function longTitle($str) { |
| 68 | return self::title($str, 255); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param $str |
| 73 | * |
| 74 | * @return bool |
| 75 | */ |
| 76 | public static function variable($str) { |
| 77 | // check length etc |
| 78 | if (empty($str) || strlen($str) > 31) { |
| 79 | return FALSE; |
| 80 | } |
| 81 | |
| 82 | // make sure it includes valid characters, alpha numeric and underscores |
| 83 | if (!preg_match('/^[\w]+$/i', $str)) { |
| 84 | return FALSE; |
| 85 | } |
| 86 | |
| 87 | return TRUE; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Validate that a string is a valid MySQL column name or alias. |
| 92 | * |
| 93 | * @param $str |
| 94 | * |
| 95 | * @return bool |
| 96 | */ |
| 97 | public static function mysqlColumnNameOrAlias($str) { |
| 98 | // Check not empty. |
| 99 | if (empty($str)) { |
| 100 | return FALSE; |
| 101 | } |
| 102 | |
| 103 | // Ensure $str conforms to expected format. Not a complete expression of |
| 104 | // what MySQL permits; this should permit the formats CiviCRM generates. |
| 105 | // |
| 106 | // * Table name prefix is optional. |
| 107 | // * Table & column names & aliases: |
| 108 | // * Composed of alphanumeric chars, underscore and hyphens. |
| 109 | // * Maximum length of 64 chars. |
| 110 | // * Optionally surrounded by backticks, in which case spaces also OK. |
| 111 | if (!preg_match('/^((`[\w- ]{1,64}`|[\w-]{1,64})\.)?(`[\w- ]{1,64}`|[\w-]{1,64})$/i', $str)) { |
| 112 | return FALSE; |
| 113 | } |
| 114 | |
| 115 | return TRUE; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Validate that a string is ASC or DESC. |
| 120 | * |
| 121 | * Empty string should be treated as invalid and ignored => default = ASC. |
| 122 | * |
| 123 | * @param $str |
| 124 | * @return bool |
| 125 | */ |
| 126 | public static function mysqlOrderByDirection($str) { |
| 127 | if (!preg_match('/^(asc|desc)$/i', $str)) { |
| 128 | return FALSE; |
| 129 | } |
| 130 | |
| 131 | return TRUE; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Validate that a string is valid order by clause. |
| 136 | * |
| 137 | * @param $str |
| 138 | * @return bool |
| 139 | */ |
| 140 | public static function mysqlOrderBy($str) { |
| 141 | $matches = array(); |
| 142 | // Using the field function in order by is valid. |
| 143 | // Look for a string like field(contribution_status_id,3,4,6). |
| 144 | // or field(civicrm_contribution.contribution_status_id,3,4,6) |
| 145 | if (preg_match('/field\([a-z_.]+,[0-9,]+\)/', $str, $matches)) { |
| 146 | // We have checked these. Remove them as they will fail the next lot. |
| 147 | // Our check currently only permits numbers & no back ticks. If we get a |
| 148 | // need for strings or backticks we can add. |
| 149 | $str = str_replace($matches, '', $str); |
| 150 | } |
| 151 | $str = trim($str); |
| 152 | if (!empty($matches) && empty($str)) { |
| 153 | // nothing left to check after the field check. |
| 154 | return TRUE; |
| 155 | } |
| 156 | // Making a regex for a comma separated list is quite hard and not readable |
| 157 | // at all, so we split and loop over. |
| 158 | $parts = explode(',', $str); |
| 159 | foreach ($parts as $part) { |
| 160 | if (!preg_match('/^((`[\w-]{1,64}`|[\w-]{1,64})\.)?(`[\w-]{1,64}`|[\w-]{1,64})( (asc|desc))?$/i', trim($part))) { |
| 161 | return FALSE; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return TRUE; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param $str |
| 170 | * |
| 171 | * @return bool |
| 172 | */ |
| 173 | public static function qfVariable($str) { |
| 174 | // check length etc |
| 175 | //if ( empty( $str ) || strlen( $str ) > 31 ) { |
| 176 | if (strlen(trim($str)) == 0 || strlen($str) > 31) { |
| 177 | return FALSE; |
| 178 | } |
| 179 | |
| 180 | // make sure it includes valid characters, alpha numeric and underscores |
| 181 | // added (. and ,) option (CRM-1336) |
| 182 | if (!preg_match('/^[\w\s\.\,]+$/i', $str)) { |
| 183 | return FALSE; |
| 184 | } |
| 185 | |
| 186 | return TRUE; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param $phone |
| 191 | * |
| 192 | * @return bool |
| 193 | */ |
| 194 | public static function phone($phone) { |
| 195 | // check length etc |
| 196 | if (empty($phone) || strlen($phone) > 16) { |
| 197 | return FALSE; |
| 198 | } |
| 199 | |
| 200 | // make sure it includes valid characters, (, \s and numeric |
| 201 | if (preg_match('/^[\d\(\)\-\.\s]+$/', $phone)) { |
| 202 | return TRUE; |
| 203 | } |
| 204 | return FALSE; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @param $query |
| 209 | * |
| 210 | * @return bool |
| 211 | */ |
| 212 | public static function query($query) { |
| 213 | // check length etc |
| 214 | if (empty($query) || strlen($query) < 3 || strlen($query) > 127) { |
| 215 | return FALSE; |
| 216 | } |
| 217 | |
| 218 | // make sure it includes valid characters, alpha numeric and underscores |
| 219 | if (!preg_match('/^[\w\s\%\'\&\,\$\#]+$/i', $query)) { |
| 220 | return FALSE; |
| 221 | } |
| 222 | |
| 223 | return TRUE; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @param $url |
| 228 | * |
| 229 | * @return bool |
| 230 | */ |
| 231 | public static function url($url) { |
| 232 | if (preg_match('/^\//', $url)) { |
| 233 | // allow relative URL's (CRM-15598) |
| 234 | $url = 'http://' . $_SERVER['HTTP_HOST'] . $url; |
| 235 | } |
| 236 | return (bool) filter_var($url, FILTER_VALIDATE_URL); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @param $url |
| 241 | * |
| 242 | * @return bool |
| 243 | */ |
| 244 | public static function urlish($url) { |
| 245 | if (empty($url)) { |
| 246 | return TRUE; |
| 247 | } |
| 248 | $url = Civi::paths()->getUrl($url, 'absolute'); |
| 249 | return (bool) filter_var($url, FILTER_VALIDATE_URL); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * @param $string |
| 254 | * |
| 255 | * @return bool |
| 256 | */ |
| 257 | public static function wikiURL($string) { |
| 258 | $items = explode(' ', trim($string), 2); |
| 259 | return self::url($items[0]); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @param $domain |
| 264 | * |
| 265 | * @return bool |
| 266 | */ |
| 267 | public static function domain($domain) { |
| 268 | // not perfect, but better than the previous one; see CRM-1502 |
| 269 | if (!preg_match('/^[A-Za-z0-9]([A-Za-z0-9\.\-]*[A-Za-z0-9])?$/', $domain)) { |
| 270 | return FALSE; |
| 271 | } |
| 272 | return TRUE; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @param $value |
| 277 | * @param null $default |
| 278 | * |
| 279 | * @return null |
| 280 | */ |
| 281 | public static function date($value, $default = NULL) { |
| 282 | if (is_string($value) && |
| 283 | preg_match('/^\d\d\d\d-?\d\d-?\d\d$/', $value) |
| 284 | ) { |
| 285 | return $value; |
| 286 | } |
| 287 | return $default; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @param $value |
| 292 | * @param null $default |
| 293 | * |
| 294 | * @return null|string |
| 295 | */ |
| 296 | public static function dateTime($value, $default = NULL) { |
| 297 | $result = $default; |
| 298 | if (is_string($value) && |
| 299 | preg_match('/^\d\d\d\d-?\d\d-?\d\d(\s\d\d:\d\d(:\d\d)?|\d\d\d\d(\d\d)?)?$/', $value) |
| 300 | ) { |
| 301 | $result = $value; |
| 302 | } |
| 303 | |
| 304 | return $result; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Check the validity of the date (in qf format) |
| 309 | * note that only a year is valid, or a mon-year is |
| 310 | * also valid in addition to day-mon-year. The date |
| 311 | * specified has to be beyond today. (i.e today or later) |
| 312 | * |
| 313 | * @param array $date |
| 314 | * @param bool $monthRequired |
| 315 | * Check whether month is mandatory. |
| 316 | * |
| 317 | * @return bool |
| 318 | * true if valid date |
| 319 | */ |
| 320 | public static function currentDate($date, $monthRequired = TRUE) { |
| 321 | $config = CRM_Core_Config::singleton(); |
| 322 | |
| 323 | $d = CRM_Utils_Array::value('d', $date); |
| 324 | $m = CRM_Utils_Array::value('M', $date); |
| 325 | $y = CRM_Utils_Array::value('Y', $date); |
| 326 | |
| 327 | if (!$d && !$m && !$y) { |
| 328 | return TRUE; |
| 329 | } |
| 330 | |
| 331 | // CRM-9017 CiviContribute/CiviMember form with expiration date format 'm Y' |
| 332 | if (!$m && !empty($date['m'])) { |
| 333 | $m = CRM_Utils_Array::value('m', $date); |
| 334 | } |
| 335 | |
| 336 | $day = $mon = 1; |
| 337 | $year = 0; |
| 338 | if ($d) { |
| 339 | $day = $d; |
| 340 | } |
| 341 | if ($m) { |
| 342 | $mon = $m; |
| 343 | } |
| 344 | if ($y) { |
| 345 | $year = $y; |
| 346 | } |
| 347 | |
| 348 | // if we have day we need mon, and if we have mon we need year |
| 349 | if (($d && !$m) || |
| 350 | ($d && !$y) || |
| 351 | ($m && !$y) |
| 352 | ) { |
| 353 | return FALSE; |
| 354 | } |
| 355 | |
| 356 | $result = FALSE; |
| 357 | if (!empty($day) || !empty($mon) || !empty($year)) { |
| 358 | $result = checkdate($mon, $day, $year); |
| 359 | } |
| 360 | |
| 361 | if (!$result) { |
| 362 | return FALSE; |
| 363 | } |
| 364 | |
| 365 | // ensure we have month if required |
| 366 | if ($monthRequired && !$m) { |
| 367 | return FALSE; |
| 368 | } |
| 369 | |
| 370 | // now make sure this date is greater that today |
| 371 | $currentDate = getdate(); |
| 372 | if ($year > $currentDate['year']) { |
| 373 | return TRUE; |
| 374 | } |
| 375 | elseif ($year < $currentDate['year']) { |
| 376 | return FALSE; |
| 377 | } |
| 378 | |
| 379 | if ($m) { |
| 380 | if ($mon > $currentDate['mon']) { |
| 381 | return TRUE; |
| 382 | } |
| 383 | elseif ($mon < $currentDate['mon']) { |
| 384 | return FALSE; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if ($d) { |
| 389 | if ($day > $currentDate['mday']) { |
| 390 | return TRUE; |
| 391 | } |
| 392 | elseif ($day < $currentDate['mday']) { |
| 393 | return FALSE; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return TRUE; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Check the validity of a date or datetime (timestamp) |
| 402 | * value which is in YYYYMMDD or YYYYMMDDHHMMSS format |
| 403 | * |
| 404 | * Uses PHP checkdate() - params are ( int $month, int $day, int $year ) |
| 405 | * |
| 406 | * @param string $date |
| 407 | * |
| 408 | * @return bool |
| 409 | * true if valid date |
| 410 | */ |
| 411 | public static function mysqlDate($date) { |
| 412 | // allow date to be null |
| 413 | if ($date == NULL) { |
| 414 | return TRUE; |
| 415 | } |
| 416 | |
| 417 | if (checkdate(substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) { |
| 418 | return TRUE; |
| 419 | } |
| 420 | |
| 421 | return FALSE; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * @param $value |
| 426 | * |
| 427 | * @return bool |
| 428 | */ |
| 429 | public static function integer($value) { |
| 430 | if (is_int($value)) { |
| 431 | return TRUE; |
| 432 | } |
| 433 | |
| 434 | // CRM-13460 |
| 435 | // ensure number passed is always a string numeral |
| 436 | if (!is_numeric($value)) { |
| 437 | return FALSE; |
| 438 | } |
| 439 | |
| 440 | // note that is_int matches only integer type |
| 441 | // and not strings which are only integers |
| 442 | // hence we do this here |
| 443 | if (preg_match('/^\d+$/', $value)) { |
| 444 | return TRUE; |
| 445 | } |
| 446 | |
| 447 | if ($value < 0) { |
| 448 | $negValue = -1 * $value; |
| 449 | if (is_int($negValue)) { |
| 450 | return TRUE; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | return FALSE; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * @param $value |
| 459 | * |
| 460 | * @return bool |
| 461 | */ |
| 462 | public static function positiveInteger($value) { |
| 463 | if (is_int($value)) { |
| 464 | return ($value < 0) ? FALSE : TRUE; |
| 465 | } |
| 466 | |
| 467 | // CRM-13460 |
| 468 | // ensure number passed is always a string numeral |
| 469 | if (!is_numeric($value)) { |
| 470 | return FALSE; |
| 471 | } |
| 472 | |
| 473 | if (preg_match('/^\d+$/', $value)) { |
| 474 | return TRUE; |
| 475 | } |
| 476 | |
| 477 | return FALSE; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * @param $value |
| 482 | * |
| 483 | * @return bool |
| 484 | */ |
| 485 | public static function numeric($value) { |
| 486 | // lets use a php gatekeeper to ensure this is numeric |
| 487 | if (!is_numeric($value)) { |
| 488 | return FALSE; |
| 489 | } |
| 490 | |
| 491 | return preg_match('/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/', $value) ? TRUE : FALSE; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * @param $value |
| 496 | * @param $noOfDigit |
| 497 | * |
| 498 | * @return bool |
| 499 | */ |
| 500 | public static function numberOfDigit($value, $noOfDigit) { |
| 501 | return preg_match('/^\d{' . $noOfDigit . '}$/', $value) ? TRUE : FALSE; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * @param $value |
| 506 | * |
| 507 | * @return mixed |
| 508 | */ |
| 509 | public static function cleanMoney($value) { |
| 510 | // first remove all white space |
| 511 | $value = str_replace(array(' ', "\t", "\n"), '', $value); |
| 512 | |
| 513 | $config = CRM_Core_Config::singleton(); |
| 514 | |
| 515 | //CRM-14868 |
| 516 | $currencySymbols = CRM_Core_PseudoConstant::get( |
| 517 | 'CRM_Contribute_DAO_Contribution', |
| 518 | 'currency', array( |
| 519 | 'keyColumn' => 'name', |
| 520 | 'labelColumn' => 'symbol', |
| 521 | ) |
| 522 | ); |
| 523 | $value = str_replace($currencySymbols, '', $value); |
| 524 | |
| 525 | if ($config->monetaryThousandSeparator) { |
| 526 | $mon_thousands_sep = $config->monetaryThousandSeparator; |
| 527 | } |
| 528 | else { |
| 529 | $mon_thousands_sep = ','; |
| 530 | } |
| 531 | |
| 532 | // ugly fix for CRM-6391: do not drop the thousand separator if |
| 533 | // it looks like it’s separating decimal part (because a given |
| 534 | // value undergoes a second cleanMoney() call, for example) |
| 535 | // CRM-15835 - in case the amount/value contains 0 after decimal |
| 536 | // eg 150.5 the following if condition will pass |
| 537 | if ($mon_thousands_sep != '.' or (substr($value, -3, 1) != '.' && substr($value, -2, 1) != '.')) { |
| 538 | $value = str_replace($mon_thousands_sep, '', $value); |
| 539 | } |
| 540 | |
| 541 | if ($config->monetaryDecimalPoint) { |
| 542 | $mon_decimal_point = $config->monetaryDecimalPoint; |
| 543 | } |
| 544 | else { |
| 545 | $mon_decimal_point = '.'; |
| 546 | } |
| 547 | $value = str_replace($mon_decimal_point, '.', $value); |
| 548 | |
| 549 | return $value; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * @param $value |
| 554 | * |
| 555 | * @return bool |
| 556 | */ |
| 557 | public static function money($value) { |
| 558 | $config = CRM_Core_Config::singleton(); |
| 559 | |
| 560 | // only edge case when we have a decimal point in the input money |
| 561 | // field and not defined in the decimal Point in config settings |
| 562 | if ($config->monetaryDecimalPoint && |
| 563 | $config->monetaryDecimalPoint != '.' && |
| 564 | // CRM-7122 also check for Thousands Separator in config settings |
| 565 | $config->monetaryThousandSeparator != '.' && |
| 566 | substr_count($value, '.') |
| 567 | ) { |
| 568 | return FALSE; |
| 569 | } |
| 570 | |
| 571 | $value = self::cleanMoney($value); |
| 572 | |
| 573 | if (self::integer($value)) { |
| 574 | return TRUE; |
| 575 | } |
| 576 | |
| 577 | return preg_match('/(^-?\d+\.\d?\d?$)|(^-?\.\d\d?$)/', $value) ? TRUE : FALSE; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * @param $value |
| 582 | * @param int $maxLength |
| 583 | * |
| 584 | * @return bool |
| 585 | */ |
| 586 | public static function string($value, $maxLength = 0) { |
| 587 | if (is_string($value) && |
| 588 | ($maxLength === 0 || strlen($value) <= $maxLength) |
| 589 | ) { |
| 590 | return TRUE; |
| 591 | } |
| 592 | return FALSE; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * @param $value |
| 597 | * |
| 598 | * @return bool |
| 599 | */ |
| 600 | public static function boolean($value) { |
| 601 | return preg_match( |
| 602 | '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value |
| 603 | ) ? TRUE : FALSE; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * @param $value |
| 608 | * |
| 609 | * @return bool |
| 610 | */ |
| 611 | public static function email($value) { |
| 612 | return (bool) filter_var($value, FILTER_VALIDATE_EMAIL); |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * @param $list |
| 617 | * |
| 618 | * @return bool |
| 619 | */ |
| 620 | public static function emailList($list) { |
| 621 | $emails = explode(',', $list); |
| 622 | foreach ($emails as $email) { |
| 623 | $email = trim($email); |
| 624 | if (!self::email($email)) { |
| 625 | return FALSE; |
| 626 | } |
| 627 | } |
| 628 | return TRUE; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or |
| 633 | * if u disregard the first 0, 4 (thanx excel!) |
| 634 | * FIXME: we need to figure out how to localize such rules |
| 635 | * @param $value |
| 636 | * |
| 637 | * @return bool |
| 638 | */ |
| 639 | public static function postalCode($value) { |
| 640 | if (preg_match('/^\d{4,6}(-\d{4})?$/', $value)) { |
| 641 | return TRUE; |
| 642 | } |
| 643 | return FALSE; |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * See how file rules are written in HTML/QuickForm/file.php |
| 648 | * Checks to make sure the uploaded file is ascii |
| 649 | * |
| 650 | * @param string $elementValue |
| 651 | * |
| 652 | * @return bool |
| 653 | * True if file has been uploaded, false otherwise |
| 654 | */ |
| 655 | public static function asciiFile($elementValue) { |
| 656 | if ((isset($elementValue['error']) && $elementValue['error'] == 0) || |
| 657 | (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none') |
| 658 | ) { |
| 659 | return CRM_Utils_File::isAscii($elementValue['tmp_name']); |
| 660 | } |
| 661 | return FALSE; |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * Checks to make sure the uploaded file is in UTF-8, recodes if it's not |
| 666 | * |
| 667 | * @param array $elementValue |
| 668 | * |
| 669 | * @return bool |
| 670 | * Whether file has been uploaded properly and is now in UTF-8. |
| 671 | */ |
| 672 | public static function utf8File($elementValue) { |
| 673 | $success = FALSE; |
| 674 | |
| 675 | if ((isset($elementValue['error']) && $elementValue['error'] == 0) || |
| 676 | (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none') |
| 677 | ) { |
| 678 | |
| 679 | $success = CRM_Utils_File::isAscii($elementValue['tmp_name']); |
| 680 | |
| 681 | // if it's a file, but not UTF-8, let's try and recode it |
| 682 | // and then make sure it's an UTF-8 file in the end |
| 683 | if (!$success) { |
| 684 | $success = CRM_Utils_File::toUtf8($elementValue['tmp_name']); |
| 685 | if ($success) { |
| 686 | $success = CRM_Utils_File::isAscii($elementValue['tmp_name']); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | return $success; |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * See how file rules are written in HTML/QuickForm/file.php |
| 695 | * Checks to make sure the uploaded file is html |
| 696 | * |
| 697 | * @param array $elementValue |
| 698 | * |
| 699 | * @return bool |
| 700 | * True if file has been uploaded, false otherwise |
| 701 | */ |
| 702 | public static function htmlFile($elementValue) { |
| 703 | if ((isset($elementValue['error']) && $elementValue['error'] == 0) || |
| 704 | (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none') |
| 705 | ) { |
| 706 | return CRM_Utils_File::isHtmlFile($elementValue['tmp_name']); |
| 707 | } |
| 708 | return FALSE; |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Check if there is a record with the same name in the db. |
| 713 | * |
| 714 | * @param string $value |
| 715 | * The value of the field we are checking. |
| 716 | * @param array $options |
| 717 | * The daoName, fieldName (optional) and DomainID (optional). |
| 718 | * |
| 719 | * @return bool |
| 720 | * true if object exists |
| 721 | */ |
| 722 | public static function objectExists($value, $options) { |
| 723 | $name = 'name'; |
| 724 | if (isset($options[2])) { |
| 725 | $name = $options[2]; |
| 726 | } |
| 727 | |
| 728 | return CRM_Core_DAO::objectExists($value, CRM_Utils_Array::value(0, $options), CRM_Utils_Array::value(1, $options), CRM_Utils_Array::value(2, $options, $name), CRM_Utils_Array::value(3, $options)); |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * @param $value |
| 733 | * @param $options |
| 734 | * |
| 735 | * @return bool |
| 736 | */ |
| 737 | public static function optionExists($value, $options) { |
| 738 | return CRM_Core_OptionValue::optionExists($value, $options[0], $options[1], $options[2], CRM_Utils_Array::value(3, $options, 'name'), CRM_Utils_Array::value(4, $options, FALSE)); |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * @param $value |
| 743 | * @param $type |
| 744 | * |
| 745 | * @return bool |
| 746 | */ |
| 747 | public static function creditCardNumber($value, $type) { |
| 748 | return Validate_Finance_CreditCard::number($value, $type); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * @param $value |
| 753 | * @param $type |
| 754 | * |
| 755 | * @return bool |
| 756 | */ |
| 757 | public static function cvv($value, $type) { |
| 758 | return Validate_Finance_CreditCard::cvv($value, $type); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * @param $value |
| 763 | * |
| 764 | * @return bool |
| 765 | */ |
| 766 | public static function currencyCode($value) { |
| 767 | static $currencyCodes = NULL; |
| 768 | if (!$currencyCodes) { |
| 769 | $currencyCodes = CRM_Core_PseudoConstant::currencyCode(); |
| 770 | } |
| 771 | if (in_array($value, $currencyCodes)) { |
| 772 | return TRUE; |
| 773 | } |
| 774 | return FALSE; |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * @param $value |
| 779 | * |
| 780 | * @return bool |
| 781 | */ |
| 782 | public static function xssString($value) { |
| 783 | if (is_string($value)) { |
| 784 | return preg_match('!<(vb)?script[^>]*>.*</(vb)?script.*>!ims', |
| 785 | $value |
| 786 | ) ? FALSE : TRUE; |
| 787 | } |
| 788 | else { |
| 789 | return TRUE; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * @param $path |
| 795 | * |
| 796 | * @return bool |
| 797 | */ |
| 798 | public static function fileExists($path) { |
| 799 | return file_exists($path); |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * Determine whether the value contains a valid reference to a directory. |
| 804 | * |
| 805 | * Paths stored in the setting system may be absolute -- or may be |
| 806 | * relative to the default data directory. |
| 807 | * |
| 808 | * @param string $path |
| 809 | * @return bool |
| 810 | */ |
| 811 | public static function settingPath($path) { |
| 812 | return is_dir(Civi::paths()->getPath($path)); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * @param $value |
| 817 | * @param null $actualElementValue |
| 818 | * |
| 819 | * @return bool |
| 820 | */ |
| 821 | public static function validContact($value, $actualElementValue = NULL) { |
| 822 | if ($actualElementValue) { |
| 823 | $value = $actualElementValue; |
| 824 | } |
| 825 | |
| 826 | return CRM_Utils_Rule::positiveInteger($value); |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Check the validity of the date (in qf format) |
| 831 | * note that only a year is valid, or a mon-year is |
| 832 | * also valid in addition to day-mon-year |
| 833 | * |
| 834 | * @param array $date |
| 835 | * |
| 836 | * @return bool |
| 837 | * true if valid date |
| 838 | */ |
| 839 | public static function qfDate($date) { |
| 840 | $config = CRM_Core_Config::singleton(); |
| 841 | |
| 842 | $d = CRM_Utils_Array::value('d', $date); |
| 843 | $m = CRM_Utils_Array::value('M', $date); |
| 844 | $y = CRM_Utils_Array::value('Y', $date); |
| 845 | if (isset($date['h']) || |
| 846 | isset($date['g']) |
| 847 | ) { |
| 848 | $m = CRM_Utils_Array::value('M', $date); |
| 849 | } |
| 850 | |
| 851 | if (!$d && !$m && !$y) { |
| 852 | return TRUE; |
| 853 | } |
| 854 | |
| 855 | $day = $mon = 1; |
| 856 | $year = 0; |
| 857 | if ($d) { |
| 858 | $day = $d; |
| 859 | } |
| 860 | if ($m) { |
| 861 | $mon = $m; |
| 862 | } |
| 863 | if ($y) { |
| 864 | $year = $y; |
| 865 | } |
| 866 | |
| 867 | // if we have day we need mon, and if we have mon we need year |
| 868 | if (($d && !$m) || |
| 869 | ($d && !$y) || |
| 870 | ($m && !$y) |
| 871 | ) { |
| 872 | return FALSE; |
| 873 | } |
| 874 | |
| 875 | if (!empty($day) || !empty($mon) || !empty($year)) { |
| 876 | return checkdate($mon, $day, $year); |
| 877 | } |
| 878 | return FALSE; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * @param $key |
| 883 | * |
| 884 | * @return bool |
| 885 | */ |
| 886 | public static function qfKey($key) { |
| 887 | return ($key) ? CRM_Core_Key::valid($key) : FALSE; |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * Check if the values in the date range are in correct chronological order. |
| 892 | * |
| 893 | * @param array $fields |
| 894 | * Fields of the form. |
| 895 | * @param $fieldName |
| 896 | * Name of date range field. |
| 897 | * @param $errors |
| 898 | * The error array. |
| 899 | * @param $title |
| 900 | * Title of the date range to be displayed in the error message. |
| 901 | */ |
| 902 | public static function validDateRange($fields, $fieldName, &$errors, $title) { |
| 903 | $lowDate = strtotime($fields[$fieldName . '_low']); |
| 904 | $highDate = strtotime($fields[$fieldName . '_high']); |
| 905 | |
| 906 | if ($lowDate > $highDate) { |
| 907 | $errors[$fieldName . '_range_error'] = ts('%1: Please check that your date range is in correct chronological order.', array(1 => $title)); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | } |