4 if (!defined('_ECRIRE_INC_VERSION')) {
9 * Vérification d'une URL
11 * Si auncune option n'est définie, vérifie uniquement si un protocole de type web est défini
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)
20 * Retourne une chaine vide si c'est valide, sinon une chaine expliquant l'erreur.
22 function verifier_url_dist($valeur, $options = array()) {
23 if (!is_string($valeur)) {
24 return _T('erreur_inconnue_generique');
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';
31 $mode = $options['mode'];
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';
39 $type_protocole = $options['type_protocole'];
41 if ($type_protocole=='exact' && $options['protocole']) {
42 $protocole = $options['protocole'];
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) ;
52 * Vérifier uniquement la présence d'un protocole
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
59 function verifier_url_protocole($url, $type_protocole, $protocole) {
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'
72 'web' => 'http://, https://',
73 'ftp' => '^ftp://, sftp://',
74 'mail' => 'pop3://, smtp://, imap://',
75 'webcal' => 'webcal://, http://, https://',
76 'exact' => $protocole.'://'
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)));
84 return _T('verifier:erreur_url_protocole', array('url' => echapper_tags($url),'protocole' => $msg_erreur[$type_protocole]));
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
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
99 function verifier_php_filter($url, $type_protocole, $protocole) {
100 if ($msg = verifier_url_protocole($url, $type_protocole, $protocole)) {
103 if (!filter_var($url, FILTER_VALIDATE_URL
)) {
104 return _T('verifier:erreur_url', array('url' => echapper_tags($valeur)));
110 * Vérifier la présence d'un protocole et de la bonne syntaxe du reste de l'url
112 * http://phpcentral.com/208-url-validation-in-php.html
113 * <http[s]|ftp> :// [user[:pass]@] hostname [port] [/path] [?getquery] [anchor]
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
120 function verifier_url_complet($url, $type_protocole, $protocole) {
122 if ($msg = verifier_url_protocole($url, $type_protocole, $protocole)) {
126 $urlregex = '#^(.*)\:\/\/';
128 // USER AND PASS (optional)
129 $urlregex .= '([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?';
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
138 $urlregex .= '(\:[0-9]{2,5})?';
140 $urlregex .= '(\/([a-z0-9+\$_%,-]\.?)+)*\/?';
141 // GET Query (optional)
142 $urlregex .= '(\?[a-z+&\$_.-][a-z0-9;:@\&%=+\$_.-]*)?';
144 $urlregex .= '(\#[a-z_.-][a-z0-9+\$_.-]*)?\$# i';
146 if (!preg_match($urlregex, $url)) {
147 return _T('verifier:erreur_url', array('url' => echapper_tags($url)));