0337d704 |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 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) { |
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; |
51 | } |
52 | return trim($output); |
53 | } |
54 | |
a14159bf |
55 | /** vérifie si une adresse email est bien formatée |
56 | * ATTENTION, cette fonction ne doit pas être appelée sur une chaîne ayant subit un addslashes (car elle accepte le "'" qui rait alors un "\'" |
57 | * @param $email l'adresse email a verifier |
58 | * @return BOOL |
59 | */ |
60 | function isvalid_email($email) { |
61 | // la rfc2822 authorise les caractères "a-z", "0-9", "!", "#", "$", "%", "&", "'", "*", "+", "-", "/", "=", "?", "^", "_", `", "{", "|", "}", "~" aussi bien dans la partie locale que dans le domaine. |
62 | // Pour la partie locale, on réduit cet ensemble car il n'est pas utilisé. |
63 | // Pour le domaine, le système DNS limite à [a-z0-9.-], on y ajoute le "_" car il est parfois utilisé. |
64 | return preg_match("/^[a-z0-9_.'+-]+@[a-z0-9._-]+\.[a-z]{2,4}$/i", $email); |
65 | } |
66 | |
a7de4ef7 |
67 | /** vérifie si une adresse email convient comme adresse de redirection |
0337d704 |
68 | * @param $email l'adresse email a verifier |
69 | * @return BOOL |
70 | */ |
71 | function isvalid_email_redirection($email) { |
72 | return isvalid_email($email) && |
73 | !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email); |
74 | } |
75 | |
db3bd146 |
76 | /** Check if the string is utf8 |
77 | */ |
78 | function is_utf8($s) |
79 | { |
80 | return @iconv('utf-8', 'utf-8', $s) == $s; |
81 | } |
82 | |
83 | |
a14159bf |
84 | /** Remove accent from a string and replace them by the nearest letter |
85 | */ |
86 | global $lc_convert, $uc_convert; |
87 | $lc_convert = array('é' => 'e', 'è' => 'e', 'ë' => 'e', 'ê' => 'e', |
88 | 'á' => 'a', 'à' => 'a', 'ä' => 'a', 'â' => 'a', 'å' => 'a', 'ã' => 'a', |
89 | 'ï' => 'i', 'î' => 'i', 'ì' => 'i', 'í' => 'i', |
90 | 'ô' => 'o', 'ö' => 'o', 'ò' => 'o', 'ó' => 'o', 'õ' => 'o', 'ø' => 'o', |
91 | 'ú' => 'u', 'ù' => 'u', 'û' => 'u', 'ü' => 'u', |
92 | 'ç' => 'c', 'ñ' => 'n'); |
93 | $uc_convert = array('É' => 'E', 'È' => 'E', 'Ë' => 'E', 'Ê' => 'E', |
94 | 'Á' => 'A', 'À' => 'A', 'Ä' => 'A', 'Â' => 'A', 'Å' => 'A', 'Ã' => 'A', |
95 | 'Ï' => 'I', 'Î' => 'I', 'Ì' => 'I', 'Í' => 'I', |
96 | 'Ô' => 'O', 'Ö' => 'O', 'Ò' => 'O', 'Ó' => 'O', 'Õ' => 'O', 'Ø' => 'O', |
97 | 'Ú' => 'U', 'Ù' => 'U', 'Û' => 'U', 'Ü' => 'U', |
98 | 'Ç' => 'C', 'Ñ' => 'N'); |
99 | |
100 | function replace_accent($string) |
101 | { |
102 | global $lc_convert, $uc_convert; |
103 | $string = strtr($string, $lc_convert); |
104 | return strtr($string, $uc_convert); |
105 | } |
106 | |
a7de4ef7 |
107 | /* Un soundex en français posté par Frédéric Bouchery |
108 | Voici une adaptation en PHP de la fonction soundex2 francisée de Frédéric BROUARD (http://sqlpro.developpez.com/Soundex/). |
109 | C'est une bonne démonstration de la force des expressions régulières compatible Perl. |
110 | trouvé sur http://expreg.com/voirsource.php?id=40&type=Chaines%20de%20caract%E8res */ |
0337d704 |
111 | function soundex_fr($sIn) |
112 | { |
a7de4ef7 |
113 | // Si il n'y a pas de mot, on sort immédiatement |
0337d704 |
114 | if ( $sIn === '' ) return ' '; |
115 | // On met tout en minuscule |
116 | $sIn = strtoupper( $sIn ); |
117 | // On supprime les accents |
a14159bf |
118 | global $uc_convert; |
119 | $accents = $uc_convert; |
120 | $accents['Ç'] = 'S'; |
121 | $accents['¿'] = 'E'; |
122 | $sIn = strtr( $sIn, $accents); |
0337d704 |
123 | // On supprime tout ce qui n'est pas une lettre |
124 | $sIn = preg_replace( '`[^A-Z]`', '', $sIn ); |
a7de4ef7 |
125 | // Si la chaîne ne fait qu'un seul caractère, on sort avec. |
0337d704 |
126 | if ( strlen( $sIn ) === 1 ) return $sIn . ' '; |
127 | // on remplace les consonnances primaires |
128 | $convIn = array( 'GUI', 'GUE', 'GA', 'GO', 'GU', 'CA', 'CO', 'CU', 'Q', 'CC', 'CK' ); |
129 | $convOut = array( 'KI', 'KE', 'KA', 'KO', 'K', 'KA', 'KO', 'KU', 'K', 'K', 'K' ); |
130 | $sIn = str_replace( $convIn, $convOut, $sIn ); |
a7de4ef7 |
131 | // on remplace les voyelles sauf le Y et sauf la première par A |
0337d704 |
132 | $sIn = preg_replace( '`(?<!^)[EIOU]`', 'A', $sIn ); |
a7de4ef7 |
133 | // on remplace les préfixes puis on conserve la première lettre |
134 | // et on fait les remplacements complémentaires |
0337d704 |
135 | $convIn = array( '`^KN`', '`^(PH|PF)`', '`^MAC`', '`^SCH`', '`^ASA`', '`(?<!^)KN`', '`(?<!^)(PH|PF)`', '`(?<!^)MAC`', '`(?<!^)SCH`', '`(?<!^)ASA`' ); |
136 | $convOut = array( 'NN', 'FF', 'MCC', 'SSS', 'AZA', 'NN', 'FF', 'MCC', 'SSS', 'AZA' ); |
137 | $sIn = preg_replace( $convIn, $convOut, $sIn ); |
138 | // suppression des H sauf CH ou SH |
139 | $sIn = preg_replace( '`(?<![CS])H`', '', $sIn ); |
a7de4ef7 |
140 | // suppression des Y sauf précédés d'un A |
0337d704 |
141 | $sIn = preg_replace( '`(?<!A)Y`', '', $sIn ); |
142 | // on supprime les terminaisons A, T, D, S |
143 | $sIn = preg_replace( '`[ATDS]$`', '', $sIn ); |
a7de4ef7 |
144 | // suppression de tous les A sauf en tête |
0337d704 |
145 | $sIn = preg_replace( '`(?!^)A`', '', $sIn ); |
a7de4ef7 |
146 | // on supprime les lettres répétitives |
a14159bf |
147 | $sIn = preg_replace( '`(.)\1`u', '$1', $sIn ); |
a7de4ef7 |
148 | // on ne retient que 4 caractères ou on complète avec des blancs |
0337d704 |
149 | return substr( $sIn . ' ', 0, 4); |
150 | } |
151 | |
e2fcbef1 |
152 | /** met les majuscules au debut de chaque atome du prénom |
153 | * @param $prenom le prénom à formater |
154 | * return STRING le prénom avec les majuscules |
155 | */ |
156 | function make_firstname_case($prenom) { |
157 | $prenom = strtolower($prenom); |
158 | $pieces = explode('-',$prenom); |
159 | |
160 | foreach ($pieces as $piece) { |
161 | $subpieces = explode("'",$piece); |
162 | $usubpieces=""; |
163 | foreach ($subpieces as $subpiece) |
164 | $usubpieces[] = ucwords($subpiece); |
165 | $upieces[] = implode("'",$usubpieces); |
166 | } |
167 | return implode('-',$upieces); |
168 | } |
169 | |
170 | |
0337d704 |
171 | function make_forlife($prenom,$nom,$promo) { |
172 | $prenomUS = replace_accent(trim($prenom)); |
173 | $nomUS = replace_accent(trim($nom)); |
174 | |
175 | $forlife = strtolower($prenomUS.".".$nomUS.".".$promo); |
176 | $forlife = str_replace(" ","-",$forlife); |
177 | $forlife = str_replace("'","",$forlife); |
178 | return $forlife; |
179 | } |
5480a216 |
180 | |
181 | function check_ip($level) |
a2446af5 |
182 | { |
183 | if (empty($_SERVER['REMOTE_ADDR'])) { |
e76421d8 |
184 | return false; |
8f61b4d5 |
185 | } |
186 | if (empty($_SESSION['check_ip'])) { |
e76421d8 |
187 | $ips = array(); |
188 | if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
189 | $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); |
190 | } |
191 | $ips[] = $_SERVER['REMOTE_ADDR']; |
192 | foreach ($ips as &$ip) { |
193 | $ip = "ip='$ip'"; |
194 | } |
195 | $res = XDB::query('SELECT state |
196 | FROM ip_watch |
197 | WHERE ' . implode(' OR ', $ips) . ' |
198 | ORDER BY state DESC'); |
8f61b4d5 |
199 | if ($res->numRows()) { |
200 | $_SESSION['check_ip'] = $res->fetchOneCell(); |
201 | } else { |
202 | $_SESSION['check_ip'] = 'safe'; |
203 | } |
204 | } |
5480a216 |
205 | $test = array(); |
206 | switch ($level) { |
8f61b4d5 |
207 | case 'unsafe': $test[] = 'unsafe'; |
208 | case 'dangerous': $test[] = 'dangerous'; |
209 | case 'ban': $test[] = 'ban'; break; |
5480a216 |
210 | default: return false; |
211 | } |
8f61b4d5 |
212 | return in_array($_SESSION['check_ip'], $test); |
5480a216 |
213 | } |
214 | |
0d693e2f |
215 | function check_email($email, $message) |
216 | { |
217 | $res = XDB::query("SELECT state, description |
218 | FROM emails_watch |
219 | WHERE state != 'safe' AND email = {?}", $email); |
220 | if ($res->numRows()) { |
221 | send_warning_mail($message); |
222 | return true; |
223 | } |
224 | return false; |
225 | } |
226 | |
0be07aa6 |
227 | function check_account() |
228 | { |
229 | return S::v('watch'); |
230 | } |
231 | |
ccdbc270 |
232 | function check_redirect($red = null) |
233 | { |
234 | require_once 'emails.inc.php'; |
235 | if (is_null($red)) { |
236 | $red = new Redirect(S::v('uid')); |
237 | } |
238 | $_SESSION['no_redirect'] = !$red->other_active(''); |
0be07aa6 |
239 | $_SESSION['mx_failures'] = $red->get_broken_mx(); |
ccdbc270 |
240 | } |
241 | |
5480a216 |
242 | function send_warning_mail($title) |
243 | { |
244 | $mailer = new PlMailer(); |
245 | $mailer->setFrom("webmaster@polytechnique.org"); |
dbede442 |
246 | $mailer->addTo("hotliners@staff.polytechnique.org"); |
2efe5355 |
247 | $mailer->setSubject("[Plat/al Security Alert] $title"); |
5480a216 |
248 | $mailer->setTxtBody("Identifiants de session :\n" . var_export($_SESSION, true) . "\n\n" |
249 | ."Identifiants de connexion :\n" . var_export($_SERVER, true)); |
250 | $mailer->send(); |
251 | } |
252 | |
a7de4ef7 |
253 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 |
254 | ?> |