PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/form/rule/email.php

https://gitlab.com/vitaliylukin91/text
PHP | 124 lines | 59 code | 19 blank | 46 comment | 21 complexity | 4fed40f3dbaf24b0e4a6dec8536fa096 MD5 | raw file
  1<?php
  2/**
  3 * @package     Joomla.Platform
  4 * @subpackage  Form
  5 *
  6 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7 * @license     GNU General Public License version 2 or later; see LICENSE
  8 */
  9
 10defined('JPATH_PLATFORM') or die;
 11
 12use Joomla\Registry\Registry;
 13
 14/**
 15 * Form Rule class for the Joomla Platform.
 16 *
 17 * @since  11.1
 18 */
 19class 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	 * @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   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-]+)*$';
 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			// Get the database object and a new query object.
100			$db = JFactory::getDbo();
101			$query = $db->getQuery(true);
102
103			// Build the query.
104			$query->select('COUNT(*)')
105				->from('#__users')
106				->where('email = ' . $db->quote($value));
107
108			// Get the extra field check attribute.
109			$userId = ($form instanceof JForm) ? $form->getValue('id') : '';
110			$query->where($db->quoteName('id') . ' <> ' . (int) $userId);
111
112			// Set and query the database.
113			$db->setQuery($query);
114			$duplicate = (bool) $db->loadResult();
115
116			if ($duplicate)
117			{
118				return false;
119			}
120		}
121
122		return true;
123	}
124}