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