Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
179afa7f | 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
0337d704 | 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., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
db3bd146 | 22 | function quoted_printable_encode($input, $line_max = 76) |
23 | { | |
0337d704 | 24 | $lines = preg_split("/(?:\r\n|\r|\n)/", $input); |
25 | $eol = "\n"; | |
26 | $linebreak = "=0D=0A=\n "; | |
27 | $escape = "="; | |
28 | $output = ""; | |
29 | ||
30 | foreach ($lines as $j => $line) { | |
d36e55a1 | 31 | $linlen = strlen($line); |
32 | $newline = ""; | |
33 | for($i = 0; $i < $linlen; $i++) { | |
34 | $c = $line{$i}; | |
35 | $dec = ord($c); | |
36 | if ( ($dec == 32) && ($i == ($linlen - 1)) ) { | |
37 | // convert space at eol only | |
38 | $c = "=20"; | |
39 | } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { | |
40 | // always encode "\t", which is *not* required | |
41 | $c = $escape.strtoupper(sprintf("%02x",$dec)); | |
42 | } | |
43 | if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted | |
44 | $output .= $newline.$escape.$eol; | |
45 | $newline = " "; | |
46 | } | |
47 | $newline .= $c; | |
48 | } // end of for | |
49 | $output .= $newline; | |
50 | if ($j<count($lines)-1) $output .= $linebreak; | |
0337d704 | 51 | } |
52 | return trim($output); | |
53 | } | |
54 | ||
d0631eab | 55 | /** genere une chaine aleatoire de 22 caracteres ou moins |
56 | * @param $len longueur souhaitée, 22 par défaut | |
57 | * @return la chaine aleatoire qui contient les caractères [A-Za-z0-9+/] | |
58 | */ | |
59 | function rand_token($len = 22) | |
60 | { | |
61 | $len = max(2, $len); | |
62 | $len = min(50, $len); | |
63 | $fp = fopen('/dev/urandom', 'r'); | |
64 | // $len * 2 is certainly an overkill, | |
65 | // but HEY, reading 40 bytes from /dev/urandom is not that slow ! | |
66 | $token = fread($fp, $len * 2); | |
67 | fclose($fp); | |
68 | $token = base64_encode($token); | |
69 | $token = preg_replace("![Il10O+/]!", "", $token); | |
70 | $token = substr($token,0,$len); | |
71 | return $token; | |
72 | } | |
73 | ||
74 | /** genere une chaine aleatoire convenable pour une url | |
75 | * @param $len longueur souhaitée, 22 par défaut | |
eaf30d86 | 76 | * @return la chaine aleatoire |
d0631eab | 77 | */ |
78 | function rand_url_id($len = 22) | |
79 | { | |
80 | return rand_token($len); | |
81 | } | |
82 | ||
83 | ||
84 | /** genere une chaine aleatoire convenable pour un mot de passe | |
85 | * @return la chaine aleatoire | |
86 | */ | |
87 | function rand_pass() | |
88 | { | |
89 | return rand_token(8); | |
90 | } | |
91 | ||
a14159bf | 92 | /** Remove accent from a string and replace them by the nearest letter |
93 | */ | |
94 | global $lc_convert, $uc_convert; | |
95 | $lc_convert = array('é' => 'e', 'è' => 'e', 'ë' => 'e', 'ê' => 'e', | |
d36e55a1 | 96 | 'á' => 'a', 'à' => 'a', 'ä' => 'a', 'â' => 'a', 'å' => 'a', 'ã' => 'a', |
97 | 'ï' => 'i', 'î' => 'i', 'ì' => 'i', 'í' => 'i', | |
98 | 'ô' => 'o', 'ö' => 'o', 'ò' => 'o', 'ó' => 'o', 'õ' => 'o', 'ø' => 'o', | |
99 | 'ú' => 'u', 'ù' => 'u', 'û' => 'u', 'ü' => 'u', | |
100 | 'ç' => 'c', 'ñ' => 'n'); | |
eaf30d86 PH |
101 | $uc_convert = array('É' => 'E', 'È' => 'E', 'Ë' => 'E', 'Ê' => 'E', |
102 | 'Á' => 'A', 'À' => 'A', 'Ä' => 'A', 'Â' => 'A', 'Å' => 'A', 'Ã' => 'A', | |
103 | 'Ï' => 'I', 'Î' => 'I', 'Ì' => 'I', 'Í' => 'I', | |
104 | 'Ô' => 'O', 'Ö' => 'O', 'Ò' => 'O', 'Ó' => 'O', 'Õ' => 'O', 'Ø' => 'O', | |
105 | 'Ú' => 'U', 'Ù' => 'U', 'Û' => 'U', 'Ü' => 'U', | |
d36e55a1 | 106 | 'Ç' => 'C', 'Ñ' => 'N'); |
a14159bf | 107 | |
108 | function replace_accent($string) | |
109 | { | |
110 | global $lc_convert, $uc_convert; | |
111 | $string = strtr($string, $lc_convert); | |
112 | return strtr($string, $uc_convert); | |
113 | } | |
114 | ||
a11b5424 | 115 | /** creates a username from a first and last name |
d36e55a1 | 116 | * |
117 | * @param $prenom the firstname | |
118 | * @param $nom the last name | |
119 | * | |
120 | * return STRING the corresponding username | |
121 | */ | |
122 | function make_username($prenom,$nom) | |
123 | { | |
a11b5424 | 124 | /* on traite le prenom */ |
125 | $prenomUS=replace_accent(trim($prenom)); | |
126 | $prenomUS=stripslashes($prenomUS); | |
127 | ||
128 | /* on traite le nom */ | |
129 | $nomUS=replace_accent(trim($nom)); | |
130 | $nomUS=stripslashes($nomUS); | |
131 | ||
132 | // calcul du login | |
133 | $username = strtolower($prenomUS.".".$nomUS); | |
134 | $username = str_replace(" ","-",$username); | |
135 | $username = str_replace("'","",$username); | |
136 | return $username; | |
137 | } | |
138 | ||
a7de4ef7 | 139 | /* Un soundex en français posté par Frédéric Bouchery |
140 | Voici une adaptation en PHP de la fonction soundex2 francisée de Frédéric BROUARD (http://sqlpro.developpez.com/Soundex/). | |
141 | C'est une bonne démonstration de la force des expressions régulières compatible Perl. | |
d36e55a1 | 142 | trouvé sur http://expreg.com/voirsource.php?id=40&type=Chaines%20de%20caract%E8res */ |
0337d704 | 143 | function soundex_fr($sIn) |
94f6c381 | 144 | { |
145 | static $convVIn, $convVOut, $convGuIn, $convGuOut, $accents; | |
146 | if (!isset($convGuIn)) { | |
d36e55a1 | 147 | global $uc_convert, $lc_convert; |
33a55e8d FB |
148 | $convGuIn = array( 'GUI', 'GUE', 'GA', 'GO', 'GU', 'SCI', 'SCE', 'SC', 'CA', 'CO', |
149 | 'CU', 'QU', 'Q', 'CC', 'CK', 'G', 'ST', 'PH'); | |
150 | $convGuOut = array( 'KI', 'KE', 'KA', 'KO', 'K', 'SI', 'SE', 'SK', 'KA', 'KO', | |
151 | 'KU', 'K', 'K', 'K', 'K', 'J', 'T', 'F'); | |
393137f9 | 152 | $convVIn = array( '/E?(AU)/', '/([EA])?[UI]([NM])([^EAIOUY]|$)/', '/[AE]O?[NM]([^AEIOUY]|$)/', |
d36e55a1 | 153 | '/[EA][IY]([NM]?[^NM]|$)/', '/(^|[^OEUIA])(OEU|OE|EU)([^OEUIA]|$)/', '/OI/', |
154 | '/(ILLE?|I)/', '/O(U|W)/', '/O[NM]($|[^EAOUIY])/', '/(SC|S|C)H/', | |
33a55e8d | 155 | '/([^AEIOUY1])[^AEIOUYLKTPNR]([UAO])([^AEIOUY])/', '/([^AEIOUY]|^)([AUO])[^AEIOUYLKTP]([^AEIOUY1])/', '/^KN/', |
d36e55a1 | 156 | '/^PF/', '/C([^AEIOUY]|$)/', |
1c428347 | 157 | '/C/', '/Z$/', '/(?<!^)Z+/', '/ER$/', '/H/', '/W/'); |
94f6c381 | 158 | $convVOut = array( 'O', '1\3', 'A\1', |
d36e55a1 | 159 | 'E\1', '\1E\3', 'O', |
160 | 'Y', 'U', 'O\1', '9', | |
161 | '\1\2\3', '\1\2\3', 'N', | |
162 | 'F', 'K\1', | |
1c428347 | 163 | 'S', 'SE', 'S', 'E', '', 'V'); |
d36e55a1 | 164 | $accents = $uc_convert + $lc_convert; |
94f6c381 | 165 | $accents['Ç'] = 'S'; |
166 | $accents['¿'] = 'E'; | |
167 | } | |
eaf30d86 PH |
168 | // Si il n'y a pas de mot, on sort immédiatement |
169 | if ( $sIn === '' ) return ' '; | |
170 | // On supprime les accents | |
d36e55a1 | 171 | $sIn = strtr( $sIn, $accents); |
eaf30d86 PH |
172 | // On met tout en minuscule |
173 | $sIn = strtoupper( $sIn ); | |
174 | // On supprime tout ce qui n'est pas une lettre | |
175 | $sIn = preg_replace( '`[^A-Z]`', '', $sIn ); | |
176 | // Si la chaîne ne fait qu'un seul caractère, on sort avec. | |
177 | if ( strlen( $sIn ) === 1 ) return $sIn . ' '; | |
178 | // on remplace les consonnances primaires | |
94f6c381 | 179 | $sIn = str_replace( $convGuIn, $convGuOut, $sIn ); |
180 | // on supprime les lettres répétitives | |
181 | $sIn = preg_replace( '`(.)\1`', '$1', $sIn ); | |
182 | // on réinterprète les voyelles | |
183 | $sIn = preg_replace( $convVIn, $convVOut, $sIn); | |
eaf30d86 | 184 | // on supprime les terminaisons T, D, S, X (et le L qui précède si existe) |
33a55e8d | 185 | $sIn = preg_replace( '`L?[TDX]S?$`', '', $sIn ); |
94f6c381 | 186 | // on supprime les E, A et Y qui ne sont pas en première position |
187 | $sIn = preg_replace( '`(?!^)Y([^AEOU]|$)`', '\1', $sIn); | |
393137f9 | 188 | $sIn = preg_replace( '`(?!^)[EA]`', '', $sIn); |
eaf30d86 | 189 | return substr( $sIn . ' ', 0, 4); |
0337d704 | 190 | } |
191 | ||
e2fcbef1 | 192 | /** met les majuscules au debut de chaque atome du prénom |
193 | * @param $prenom le prénom à formater | |
194 | * return STRING le prénom avec les majuscules | |
195 | */ | |
94f6c381 | 196 | function make_firstname_case($prenom) |
197 | { | |
d36e55a1 | 198 | $prenom = strtolower($prenom); |
199 | $pieces = explode('-',$prenom); | |
e2fcbef1 | 200 | |
d36e55a1 | 201 | foreach ($pieces as $piece) { |
202 | $subpieces = explode("'",$piece); | |
203 | $usubpieces=""; | |
204 | foreach ($subpieces as $subpiece) | |
205 | $usubpieces[] = ucwords($subpiece); | |
206 | $upieces[] = implode("'",$usubpieces); | |
207 | } | |
208 | return implode('-',$upieces); | |
e2fcbef1 | 209 | } |
210 | ||
211 | ||
d36e55a1 | 212 | function make_forlife($prenom, $nom, $promo) |
213 | { | |
0337d704 | 214 | $prenomUS = replace_accent(trim($prenom)); |
215 | $nomUS = replace_accent(trim($nom)); | |
216 | ||
217 | $forlife = strtolower($prenomUS.".".$nomUS.".".$promo); | |
218 | $forlife = str_replace(" ","-",$forlife); | |
219 | $forlife = str_replace("'","",$forlife); | |
220 | return $forlife; | |
221 | } | |
5480a216 | 222 | |
9797734d FB |
223 | /** Convert ip to uint (to store it in a database) |
224 | */ | |
225 | function ip_to_uint($ip) | |
226 | { | |
61c98f4b | 227 | $part = explode('.', $ip); |
ba34dc61 FB |
228 | if (count($part) != 4) { |
229 | return null; | |
230 | } | |
61c98f4b FB |
231 | $v = 0; |
232 | $fact = 0x1000000; | |
233 | for ($i = 0 ; $i < 4 ; ++$i) { | |
234 | $v += $fact * $part[$i]; | |
235 | $fact >>= 8; | |
236 | } | |
237 | return $v; | |
9797734d FB |
238 | } |
239 | ||
240 | /** Convert uint to ip (to build a human understandable ip) | |
241 | */ | |
242 | function uint_to_ip($uint) | |
243 | { | |
bcf05105 | 244 | return long2ip($uint); |
9797734d FB |
245 | } |
246 | ||
a7de4ef7 | 247 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 248 | ?> |