Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
email.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Form
5  *
6  * @copyright Copyright (C) 2005 - 2013 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 /**
13  * Form Rule class for the Joomla Platform.
14  *
15  * @package Joomla.Platform
16  * @subpackage Form
17  * @since 11.1
18  */
20 {
21  /**
22  * The regular expression to use in testing a form field value.
23  *
24  * @var string
25  * @since 11.1
26  * @see 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 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 JRegistry $input An optional JRegistry 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, JRegistry $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-]{2,})$';
61  }
62 
63  // Determine if the multiple attribute is present
64  $multiple = ((string) $element['multiple'] == 'true' || (string) $element['multiple'] == 'multiple');
65 
66  if (!$multiple)
67  {
68  // Handle idn e-mail addresses by converting to punycode.
69  $value = JStringPunycode::emailToPunycode($value);
70 
71  // Test the value against the regular expression.
72  if (!parent::test($element, $value, $group, $input, $form))
73  {
74  return false;
75  }
76  }
77  else
78  {
79  $values = explode(',', $value);
80 
81  foreach ($values as $value)
82  {
83  // Handle idn e-mail addresses by converting to punycode.
84  $value = JStringPunycode::emailToPunycode($value);
85 
86  // Test the value against the regular expression.
87  if (!parent::test($element, $value, $group, $input, $form))
88  {
89  return false;
90  }
91  }
92  }
93 
94  // Check if we should test for uniqueness. This only can be used if multiple is not true
95  $unique = ((string) $element['unique'] == 'true' || (string) $element['unique'] == 'unique');
96 
97  if ($unique && !$multiple)
98  {
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 }