PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/content/code/trunk/administrator/components/com_artofcontent/libraries/jxtended/form/rules/email.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 97 lines | 48 code | 12 blank | 37 comment | 13 complexity | 4e7f43c452d8e5a02b6d204eaa49c605 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: email.php 484 2010-12-20 23:40:27Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Form
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License
  8. * @link http://www.theartofjoomla.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. juimport('jxtended.form.formrule');
  12. /**
  13. * Form Rule class for JXtended Libraries.
  14. *
  15. * @package JXtended.Libraries
  16. * @subpackage Form
  17. * @since 1.1
  18. */
  19. class JFormRuleEmail extends JFormRule
  20. {
  21. /**
  22. * The regular expression.
  23. *
  24. * @var string
  25. */
  26. protected $_regex = '[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}';
  27. /**
  28. * Method to test if an e-mail address is unique.
  29. *
  30. * @param object $field A reference to the form field.
  31. * @param mixed $values The values to test for validiaty.
  32. * @return mixed JException on invalid rule, true if the value is valid, false otherwise.
  33. * @since 1.6
  34. */
  35. public function test(&$field, &$values)
  36. {
  37. $return = false;
  38. $name = $field->attributes('name');
  39. $check = ($field->attributes('unique') == 'true' || $field->attributes('unique') == 'unique');
  40. // If the field is empty and not required, the field is valid.
  41. if ($field->attributes('required') != 'true')
  42. {
  43. // Get the data for the field.
  44. $value = array_key_exists($name, $values) ? $values[$name] : null;
  45. // If the data is empty, return valid.
  46. if ($value == null) {
  47. return true;
  48. }
  49. }
  50. // Check if we should test for uniqueness.
  51. if ($check)
  52. {
  53. $key = $field->attributes('field');
  54. $value = isset($values[$key]) ? $values[$key] : 0;
  55. // Check the rule.
  56. if (!$key) {
  57. return new JException('Invalid Form Rule :: '.get_class($this));
  58. }
  59. // Check if the username is unique.
  60. $db = &JFactory::getDbo();
  61. $db->setQuery(
  62. 'SELECT count(*) FROM `#__users`' .
  63. ' WHERE `email` = '.$db->Quote($values[$name]) .
  64. ' AND '.$db->nameQuote($key).' != '.$db->Quote($value)
  65. );
  66. $duplicate = (bool)$db->loadResult();
  67. // Check for a database error.
  68. if ($db->getErrorNum()) {
  69. return new JException('Database Error :: '.$db->getErrorMsg());
  70. }
  71. // Test the value against the regular expression.
  72. if (parent::test($field, $values) && !$duplicate) {
  73. $return = true;
  74. }
  75. }
  76. else
  77. {
  78. // Test the value against the regular expression.
  79. if (parent::test($field, $values)) {
  80. $return = true;
  81. }
  82. }
  83. return $return;
  84. }
  85. }