[CSS] +fix page header and title color
[lhc/web/www.git] / www / plugins / verifier / verifier / url.php
1 <?php
2
3 // Sécurité
4 if (!defined('_ECRIRE_INC_VERSION')) {
5 return;
6 }
7
8 /**
9 * Vérification d'une URL
10 *
11 * Si auncune option n'est définie, vérifie uniquement si un protocole de type web est défini
12 *
13 * @param string $valeur
14 * La valeur à vérifier.
15 * @param array $options
16 * mode : protocole_seul, php_filter, complet
17 * type_protocole : tous, web (http ou https), mail (imap, pop3, smtp), ftp (ftp ou sftp), exact
18 * protocole : nom du protocole (si type_protocole=exact)
19 * @return string
20 * Retourne une chaine vide si c'est valide, sinon une chaine expliquant l'erreur.
21 */
22 function verifier_url_dist($valeur, $options = array()) {
23 if (!is_string($valeur)) {
24 return _T('erreur_inconnue_generique');
25 }
26
27 // Choix du mode de verification de la syntaxe des url
28 if (!$options['mode'] or !in_array($options['mode'], array('protocole_seul','php_filter','complet'))) {
29 $mode = 'protocole_seul';
30 } else {
31 $mode = $options['mode'];
32 }
33
34 // Choix du type de protocole à vérifier
35 if (!$options['type_protocole'] or !in_array($options['type_protocole'], array('tous','web','mail','ftp','webcal_souple','webcal_strict','exact'))) {
36 $type_protocole = 'web';
37 $protocole = '';
38 } else {
39 $type_protocole = $options['type_protocole'];
40 $protocole = '' ;
41 if ($type_protocole=='exact' && $options['protocole']) {
42 $protocole = $options['protocole'];
43 }
44 }
45
46 $fonctions_disponibles = array('protocole_seul' => 'verifier_url_protocole', 'php_filter' => 'verifier_php_filter', 'complet' => 'verifier_url_complet');
47 $fonction_verif = $fonctions_disponibles[$mode];
48 return $fonction_verif($valeur,$type_protocole,$protocole) ;
49 }
50
51 /**
52 * Vérifier uniquement la présence d'un protocole
53 *
54 * @param string $valeur La valeur à vérifier
55 * @param string $type_protocole : tous, web (http ou https), mail (imap, pop3, smtp), ftp (ftp ou sftp), webcal (webcal, http, https), exact
56 * @param string $protocole : nom du protocole (si type_protocole=exact)
57 * @return boolean Retourne true uniquement lorsque l'url est valide
58 */
59 function verifier_url_protocole($url, $type_protocole, $protocole) {
60
61 $urlregex = array(
62 'tous' => '#^([a-z0-9]*)\:\/\/.*$# i',
63 'web' => '#^(https?)\:\/\/.*$# i',
64 'ftp' => '#^(s?ftp)\:\/\/.*$# i',
65 'mail' => '#^(pop3|smtp|imap)\:\/\/.*$# i',
66 'webcal' => '#^(webcal|https?)\:\/\/.*$# i',
67 'exact' => '#^(".$protocole.")\:\/\/.*$# i'
68 );
69
70 $msg_erreur = array(
71 'tous' => '',
72 'web' => 'http://, https://',
73 'ftp' => '^ftp://, sftp://',
74 'mail' => 'pop3://, smtp://, imap://',
75 'webcal' => 'webcal://, http://, https://',
76 'exact' => $protocole.'://'
77 );
78
79
80 if (!preg_match($urlregex[$type_protocole], $url)) {
81 if ($type_protocole == 'tous') {
82 return _T('verifier:erreur_url_protocole_exact', array('url' => echapper_tags($url)));
83 } else {
84 return _T('verifier:erreur_url_protocole', array('url' => echapper_tags($url),'protocole' => $msg_erreur[$type_protocole]));
85 }
86 }
87 return '';
88 }
89
90 /**
91 * Vérifier la présence d'un protocole,
92 * puis utilise le filtre FILTER_VALIDATE_URL de PHP pour s'assurer que l'url est complète
93 *
94 * @param string $valeur La valeur à vérifier
95 * @param string $type_protocole : tous, web (http ou https), mail (imap, pop3, smtp), ftp (ftp ou sftp), webcal (webcal, http, https), exact
96 * @param string $protocole : nom du protocole (si type_protocole=exact)
97 * @return boolean Retourne true uniquement lorsque l'url est valide
98 */
99 function verifier_php_filter($url, $type_protocole, $protocole) {
100 if ($msg = verifier_url_protocole($url, $type_protocole, $protocole)) {
101 return $msg;
102 }
103 if (!filter_var($url, FILTER_VALIDATE_URL)) {
104 return _T('verifier:erreur_url', array('url' => echapper_tags($valeur)));
105 }
106 return '';
107 }
108
109 /**
110 * Vérifier la présence d'un protocole et de la bonne syntaxe du reste de l'url
111 *
112 * http://phpcentral.com/208-url-validation-in-php.html
113 * <http[s]|ftp> :// [user[:pass]@] hostname [port] [/path] [?getquery] [anchor]
114 *
115 * @param string $valeur La valeur à vérifier
116 * @param string $type_protocole : web (http ou https), mail (imap, pop3, smtp), ftp (ftp ou sftp), webcal (webcal, http, https), exact
117 * @param string $protocole : nom du protocole (si type_protocole=exact)
118 * @return boolean Retourne true uniquement lorsque l'url est valide
119 */
120 function verifier_url_complet($url, $type_protocole, $protocole) {
121
122 if ($msg = verifier_url_protocole($url, $type_protocole, $protocole)) {
123 return $msg;
124 }
125 // SCHEME
126 $urlregex = '#^(.*)\:\/\/';
127
128 // USER AND PASS (optional)
129 $urlregex .= '([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?';
130
131 // HOSTNAME OR IP
132 $urlregex .= '[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*'; // http://x = allowed (ex. http://localhost, http://routerlogin)
133 //$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // http://x.x = minimum
134 //$urlregex .= "([a-z0-9+\$_-]+\.)*[a-z0-9+\$_-]{2,3}"; // http://x.xx(x) = minimum
135 //use only one of the above
136
137 // PORT (optional)
138 $urlregex .= '(\:[0-9]{2,5})?';
139 // PATH (optional)
140 $urlregex .= '(\/([a-z0-9+\$_%,-]\.?)+)*\/?';
141 // GET Query (optional)
142 $urlregex .= '(\?[a-z+&\$_.-][a-z0-9;:@\&%=+\$_.-]*)?';
143 // ANCHOR (optional)
144 $urlregex .= '(\#[a-z_.-][a-z0-9+\$_.-]*)?\$# i';
145
146 if (!preg_match($urlregex, $url)) {
147 return _T('verifier:erreur_url', array('url' => echapper_tags($url)));
148 }
149 return '';
150 }