/libraries/joomla/form/rule/url.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 98 lines · 52 code · 4 blank · 42 comment · 38 complexity · 6058f89171c7300cdae2b9d4ba51d3bb MD5 · raw file

  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. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Form Rule class for the Joomla Platform.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Form
  15. * @since 11.1
  16. */
  17. class JFormRuleUrl extends JFormRule
  18. {
  19. /**
  20. * Method to test an external url for a valid parts.
  21. *
  22. * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
  23. * @param mixed $value The form field value to validate.
  24. * @param string $group The field name group control value. This acts as as an array container for the field.
  25. * For example if the field has name="foo" and the group value is set to "bar" then the
  26. * full field name would end up being "bar[foo]".
  27. * @param JRegistry $input An optional JRegistry object with the entire data set to validate against the entire form.
  28. * @param JForm $form The form object for which the field is being tested.
  29. *
  30. * @return boolean True if the value is valid, false otherwise.
  31. *
  32. * @since 11.1
  33. * @link http://www.w3.org/Addressing/URL/url-spec.txt
  34. * @see Jstring
  35. */
  36. public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $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. if (!$required && empty($value))
  41. {
  42. return true;
  43. }
  44. $urlParts = JString::parse_url($value);
  45. // See http://www.w3.org/Addressing/URL/url-spec.txt
  46. // Use the full list or optionally specify a list of permitted schemes.
  47. if ($element['schemes'] == '')
  48. {
  49. $scheme = array('http', 'https', 'ftp', 'ftps', 'gopher', 'mailto', 'news', 'prospero', 'telnet', 'rlogin', 'tn3270', 'wais', 'url',
  50. 'mid', 'cid', 'nntp', 'tel', 'urn', 'ldap', 'file', 'fax', 'modem', 'git');
  51. }
  52. else
  53. {
  54. $scheme = explode(',', $element['schemes']);
  55. }
  56. /*
  57. * This rule is only for full URLs with schemes because parse_url does not parse
  58. * accurately without a scheme.
  59. * @see http://php.net/manual/en/function.parse-url.php
  60. */
  61. if ($urlParts && !array_key_exists('scheme', $urlParts))
  62. {
  63. return false;
  64. }
  65. $urlScheme = (string) $urlParts['scheme'];
  66. $urlScheme = strtolower($urlScheme);
  67. if (in_array($urlScheme, $scheme) == false)
  68. {
  69. return false;
  70. }
  71. // For some schemes here must be two slashes.
  72. if (($urlScheme == 'http' || $urlScheme == 'https' || $urlScheme == 'ftp' || $urlScheme == 'sftp' || $urlScheme == 'gopher'
  73. || $urlScheme == 'wais' || $urlScheme == 'gopher' || $urlScheme == 'prospero' || $urlScheme == 'telnet' || $urlScheme == 'git')
  74. && ((substr($value, strlen($urlScheme), 3)) !== '://'))
  75. {
  76. return false;
  77. }
  78. // The best we can do for the rest is make sure that the strings are valid UTF-8
  79. // and the port is an integer.
  80. if (array_key_exists('host', $urlParts) && !JString::valid((string) $urlParts['host']))
  81. {
  82. return false;
  83. }
  84. if (array_key_exists('port', $urlParts) && !is_int((int) $urlParts['port']))
  85. {
  86. return false;
  87. }
  88. if (array_key_exists('path', $urlParts) && !JString::valid((string) $urlParts['path']))
  89. {
  90. return false;
  91. }
  92. return true;
  93. }
  94. }