| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (C) 2003-2004 Polytechnique.org |
| 4 | * http://opensource.polytechnique.org/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | require_once dirname(__FILE__).'/diogenes.mime.inc.php'; |
| 22 | |
| 23 | $lc_accent = "éèëêáàäâåãïîìíôöòóõøúùûüçñ"; |
| 24 | $lc_plain = "eeeeaaaaaaiiiioooooouuuucn"; |
| 25 | $uc_accent = "ÉÈËÊÁÀÄÂÅÃÏÎÌÍÔÖÒÓÕØÚÙÛÜÇÑ"; |
| 26 | $uc_plain = "EEEEAAAAAAIIIIOOOOOOUUUUCN"; |
| 27 | |
| 28 | |
| 29 | /** Returns the value of one or more request variables, catching |
| 30 | * the case where they do not exist. |
| 31 | * |
| 32 | * @param req name(s) of the request variables : a string or array strings |
| 33 | */ |
| 34 | function clean_request($req) { |
| 35 | if (is_array($req)) { |
| 36 | $out = array(); |
| 37 | foreach($req as $reqitem) |
| 38 | array_push($out,(isset($_REQUEST[$reqitem])) ? $_REQUEST[$reqitem] : ""); |
| 39 | return $out; |
| 40 | } else { |
| 41 | return (isset($_REQUEST[$req])) ? $_REQUEST[$req] : ""; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
| 46 | /** Strips slashes off a request var. |
| 47 | * |
| 48 | * @param req the request variable name |
| 49 | */ |
| 50 | function strip_request($req) { |
| 51 | return stripslashes(clean_request($req)); |
| 52 | } |
| 53 | |
| 54 | /** replaces accentuated characters in a string by their |
| 55 | * html counterpart |
| 56 | * |
| 57 | * @param $string the input string |
| 58 | * @return STRING the output string |
| 59 | */ |
| 60 | function html_accent($string) { |
| 61 | global $lc_accent,$uc_accent; |
| 62 | $lca = preg_split('//', $lc_accent, -1, PREG_SPLIT_NO_EMPTY); |
| 63 | $uca = preg_split('//', $uc_accent, -1, PREG_SPLIT_NO_EMPTY); |
| 64 | foreach($lca as $key=>$val) |
| 65 | $lch[$key] = htmlentities($val, ENT_COMPAT | ENT_HTML401, "ISO-8859-1"); |
| 66 | foreach($uca as $key=>$val) |
| 67 | $uch[$key] = htmlentities($val, ENT_COMPAT | ENT_HTML401, "ISO-8859-1"); |
| 68 | $newstring = str_replace($lca,$lch,$string); |
| 69 | $newstring = str_replace($uca,$uch,$newstring); |
| 70 | return $newstring; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** replaces accentuated characters in a string by their |
| 75 | * non-accentuaded counterpart |
| 76 | * |
| 77 | * @param $string the input string |
| 78 | * @return STRING the output string |
| 79 | */ |
| 80 | function replace_accent($string) { |
| 81 | global $lc_accent,$lc_plain,$uc_accent,$uc_plain; |
| 82 | |
| 83 | $newstring = strtr($string,$lc_accent,$lc_plain); |
| 84 | $newstring = strtr($newstring,$uc_accent,$uc_plain); |
| 85 | return $newstring; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** remplace les caractères accentués par la regexp (caractère accentué ou caractère non accentué) |
| 90 | * @param $string la chaîne de caractères |
| 91 | * @return STRING la nouvelle chaîne de caractères |
| 92 | * @see recherche.php |
| 93 | */ |
| 94 | function replace_accent_regexp($string) { |
| 95 | $classes_accent[] = "éèëêe"; |
| 96 | $classes_accent[] = "áàäâåãa"; |
| 97 | $classes_accent[] = "ïîìíi"; |
| 98 | $classes_accent[] = "ôöòóõøo"; |
| 99 | $classes_accent[] = "úùûüu"; |
| 100 | $classes_accent[] = "çc"; |
| 101 | $classes_accent[] = "ñn"; |
| 102 | $classes_accent[] = "ÉÈËÊE"; |
| 103 | $classes_accent[] = "ÁÀÄÂÅÃA"; |
| 104 | $classes_accent[] = "ÏÎÌÍI"; |
| 105 | $classes_accent[] = "ÔÖÒÓÕØO"; |
| 106 | $classes_accent[] = "ÚÙÛÜU"; |
| 107 | $classes_accent[] = "ÇC"; |
| 108 | $classes_accent[] = "ÑN"; |
| 109 | |
| 110 | for ($i=0;$i<count($classes_accent);$i++) |
| 111 | for ($j=0;$j<strlen($classes_accent[$i]);$j++) |
| 112 | $trans[$classes_accent[$i][$j]] = '['.$classes_accent[$i].']'; |
| 113 | $newstring = strtr($string,$trans); |
| 114 | return $newstring; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** capitalises the first letters of the elements of a name |
| 119 | * |
| 120 | * @param $name the name to capitalise |
| 121 | * |
| 122 | * @return STRING the capitalised name |
| 123 | */ |
| 124 | function make_name_case($name) { |
| 125 | $name = strtolower($name); |
| 126 | $pieces = explode('-',$name); |
| 127 | |
| 128 | foreach ($pieces as $piece) { |
| 129 | $subpieces = explode("'",$piece); |
| 130 | $usubpieces=""; |
| 131 | foreach ($subpieces as $subpiece) |
| 132 | $usubpieces[] = ucwords($subpiece); |
| 133 | $upieces[] = implode("'",$usubpieces); |
| 134 | } |
| 135 | return implode('-',$upieces); |
| 136 | } |
| 137 | |
| 138 | /** creates a username from a first and last name |
| 139 | * |
| 140 | * @param $prenom the firstname |
| 141 | * @param $nom the last name |
| 142 | * |
| 143 | * return STRING the corresponding username |
| 144 | */ |
| 145 | function make_username($prenom,$nom) { |
| 146 | /* on traite le prenom */ |
| 147 | $prenomUS=replace_accent(trim($prenom)); |
| 148 | $prenomUS=stripslashes($prenomUS); |
| 149 | |
| 150 | /* on traite le nom */ |
| 151 | $nomUS=replace_accent(trim($nom)); |
| 152 | $nomUS=stripslashes($nomUS); |
| 153 | |
| 154 | // calcul du login |
| 155 | $username = strtolower($prenomUS.".".$nomUS); |
| 156 | $username = str_replace(" ","-",$username); |
| 157 | $username = str_replace("'","",$username); |
| 158 | return $username; |
| 159 | } |
| 160 | |
| 161 | /** met les majuscules au debut de chaque atome du prénom |
| 162 | * @param $prenom le prénom à formater |
| 163 | * return STRING le prénom avec les majuscules |
| 164 | */ |
| 165 | function make_firstname_case($prenom) { |
| 166 | $prenom = strtolower($prenom); |
| 167 | $pieces = explode('-',$prenom); |
| 168 | |
| 169 | foreach ($pieces as $piece) { |
| 170 | $subpieces = explode("'",$piece); |
| 171 | $usubpieces=""; |
| 172 | foreach ($subpieces as $subpiece) |
| 173 | $usubpieces[] = ucwords($subpiece); |
| 174 | $upieces[] = implode("'",$usubpieces); |
| 175 | } |
| 176 | return implode('-',$upieces); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** vérifie si une adresse email est bien formatée |
| 181 | * ATTENTION, cette fonction ne doit pas être appelée sur une chaîne ayant subit un addslashes (car elle accepte le "'" qui serait alors un "\'" |
| 182 | * @param $email l'adresse email a verifier |
| 183 | * @return BOOL |
| 184 | */ |
| 185 | function isvalid_email($email) { |
| 186 | // la rfc2822 authorise les caractères "a-z", "0-9", "!", "#", "$", "%", "&", "'", "*", "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~" aussi bien dans la partie locale que dans le domaine. |
| 187 | // Pour la partie locale, on réduit cet ensemble car il n'est pas utilisé. |
| 188 | // Pour le domaine, le système DNS limite à [a-z0-9.-], on y ajoute le "_" car il est parfois utilisé. |
| 189 | return preg_match("/^[a-z0-9_.'+-]+@[a-z0-9._-]+\.[a-z]{2,4}$/i", $email); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** genere une chaine aleatoire de 22 caracteres ou moins |
| 194 | * @param $len longueur souhaitée, 22 par défaut |
| 195 | * @return la chaine aleatoire qui contient les caractères [A-Za-z0-9+/] |
| 196 | */ |
| 197 | function rand_token($len = 22) { |
| 198 | $len = max(2, $len); |
| 199 | $len = min(50, $len); |
| 200 | $fp = fopen('/dev/urandom', 'r'); |
| 201 | // $len * 2 is certainly an overkill, |
| 202 | // but HEY, reading 40 bytes from /dev/urandom is not that slow ! |
| 203 | $token = fread($fp, $len * 2); |
| 204 | fclose($fp); |
| 205 | $token = base64_encode($token); |
| 206 | $token = preg_replace("![Il10O+/]!", "", $token); |
| 207 | $token = substr($token,0,$len); |
| 208 | return $token; |
| 209 | } |
| 210 | |
| 211 | /** genere une chaine aleatoire convenable pour une url |
| 212 | * @param $len longueur souhaitée, 22 par défaut |
| 213 | * @return la chaine aleatoire |
| 214 | */ |
| 215 | function rand_url_id($len = 22) { |
| 216 | return rand_token($len); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /** genere une chaine aleatoire convenable pour un mot de passe |
| 221 | * @return la chaine aleatoire |
| 222 | */ |
| 223 | function rand_pass() { |
| 224 | return rand_token(8); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /** replacement for file_get_contents functions |
| 229 | */ |
| 230 | if (!function_exists("file_get_contents")) { |
| 231 | function file_get_contents($filename, $use_include_path = 0) { |
| 232 | $data = ""; // just to be safe. Dunno, if this is really needed |
| 233 | $file = @fopen($filename, "rb", $use_include_path); |
| 234 | if ($file) { |
| 235 | while (!feof($file)) |
| 236 | $data .= fread($file, 1024); |
| 237 | fclose($file); |
| 238 | } |
| 239 | return $data; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | ?> |