1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 /**
15 * Form Rule class for the Joomla Platform
16 *
17 * @since 11.1
18 */
19 class JFormRuleTel extends JFormRule
20 {
21 /**
22 * Method to test the url for a valid parts.
23 *
24 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
25 * @param mixed $value The form field value to validate.
26 * @param string $group The field name group control value. This acts as an array container for the field.
27 * For example if the field has name="foo" and the group value is set to "bar" then the
28 * full field name would end up being "bar[foo]".
29 * @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
30 * @param JForm $form The form object for which the field is being tested.
31 *
32 * @return boolean True if the value is valid, false otherwise.
33 *
34 * @since 11.1
35 */
36 public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
37 {
38 // If the field is empty and not required, the field is valid.
39 $required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
40
41 if (!$required && empty($value))
42 {
43 return true;
44 }
45
46 /*
47 * @link http://www.nanpa.com/
48 * @link http://tools.ietf.org/html/rfc4933
49 * @link http://www.itu.int/rec/T-REC-E.164/en
50 *
51 * Regex by Steve Levithan
52 * @link http://blog.stevenlevithan.com/archives/validate-phone-number
53 * @note that valid ITU-T and EPP must begin with +.
54 */
55 $regexarray = array(
56 'NANP' => '/^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/',
57 'ITU-T' => '/^\+(?:[0-9] ?){6,14}[0-9]$/',
58 'EPP' => '/^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/',
59 );
60
61 if (isset($element['plan']))
62 {
63 $plan = (string) $element['plan'];
64
65 if ($plan == 'northamerica' || $plan == 'us')
66 {
67 $plan = 'NANP';
68 }
69 elseif ($plan == 'International' || $plan == 'int' || $plan == 'missdn' || !$plan)
70 {
71 $plan = 'ITU-T';
72 }
73 elseif ($plan == 'IETF')
74 {
75 $plan = 'EPP';
76 }
77
78 $regex = $regexarray[$plan];
79
80 // Test the value against the regular expression.
81 if (preg_match($regex, $value) == false)
82 {
83 return false;
84 }
85 }
86 else
87 {
88 /*
89 * If the rule is set but no plan is selected just check that there are between
90 * 7 and 15 digits inclusive and no illegal characters (but common number separators
91 * are allowed).
92 */
93 $cleanvalue = preg_replace('/[+. \-(\)]/', '', $value);
94 $regex = '/^[0-9]{7,15}?$/';
95
96 if (preg_match($regex, $cleanvalue) == true)
97 {
98 return true;
99 }
100 else
101 {
102 return false;
103 }
104 }
105
106 return true;
107 }
108 }
109