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 JFormRuleEmail extends JFormRule
20 {
21 /**
22 * The regular expression to use in testing a form field value.
23 *
24 * @var string
25 * @since 11.1
26 * @link http://www.w3.org/TR/html-markup/input.email.html
27 */
28 protected $regex = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$";
29
30 /**
31 * Method to test the email address and optionally check for uniqueness.
32 *
33 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
34 * @param mixed $value The form field value to validate.
35 * @param string $group The field name group control value. This acts as an array container for the field.
36 * For example if the field has name="foo" and the group value is set to "bar" then the
37 * full field name would end up being "bar[foo]".
38 * @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
39 * @param JForm $form The form object for which the field is being tested.
40 *
41 * @return boolean True if the value is valid, false otherwise.
42 *
43 * @since 11.1
44 */
45 public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
46 {
47 // If the field is empty and not required, the field is valid.
48 $required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
49
50 if (!$required && empty($value))
51 {
52 return true;
53 }
54
55 // If the tld attribute is present, change the regular expression to require at least 2 characters for it.
56 $tld = ((string) $element['tld'] == 'tld' || (string) $element['tld'] == 'required');
57
58 if ($tld)
59 {
60 $this->regex = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])"
61 . '?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$';
62 }
63
64 // Determine if the multiple attribute is present
65 $multiple = ((string) $element['multiple'] == 'true' || (string) $element['multiple'] == 'multiple');
66
67 if (!$multiple)
68 {
69 // Handle idn email addresses by converting to punycode.
70 $value = JStringPunycode::emailToPunycode($value);
71
72 // Test the value against the regular expression.
73 if (!parent::test($element, $value, $group, $input, $form))
74 {
75 return false;
76 }
77 }
78 else
79 {
80 $values = explode(',', $value);
81
82 foreach ($values as $value)
83 {
84 // Handle idn email addresses by converting to punycode.
85 $value = JStringPunycode::emailToPunycode($value);
86
87 // Test the value against the regular expression.
88 if (!parent::test($element, $value, $group, $input, $form))
89 {
90 return false;
91 }
92 }
93 }
94
95 // Check if we should test for uniqueness. This only can be used if multiple is not true
96 $unique = ((string) $element['unique'] == 'true' || (string) $element['unique'] == 'unique');
97
98 if ($unique && !$multiple)
99 {
100 // Get the database object and a new query object.
101 $db = JFactory::getDbo();
102 $query = $db->getQuery(true);
103
104 // Build the query.
105 $query->select('COUNT(*)')
106 ->from('#__users')
107 ->where('email = ' . $db->quote($value));
108
109 // Get the extra field check attribute.
110 $userId = ($form instanceof JForm) ? $form->getValue('id') : '';
111 $query->where($db->quoteName('id') . ' <> ' . (int) $userId);
112
113 // Set and query the database.
114 $db->setQuery($query);
115 $duplicate = (bool) $db->loadResult();
116
117 if ($duplicate)
118 {
119 return false;
120 }
121 }
122
123 return true;
124 }
125 }
126