#603: Show QoS informations
[platal.git] / include / xorg.misc.inc.php
CommitLineData
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
22function quoted_printable_encode($input, $line_max = 76) {
23 $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
24 $eol = "\n";
25 $linebreak = "=0D=0A=\n ";
26 $escape = "=";
27 $output = "";
28
29 foreach ($lines as $j => $line) {
30 $linlen = strlen($line);
31 $newline = "";
32 for($i = 0; $i < $linlen; $i++) {
33 $c = $line{$i};
34 $dec = ord($c);
35 if ( ($dec == 32) && ($i == ($linlen - 1)) ) {
36 // convert space at eol only
37 $c = "=20";
38 } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) {
39 // always encode "\t", which is *not* required
40 $c = $escape.strtoupper(sprintf("%02x",$dec));
41 }
42 if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
43 $output .= $newline.$escape.$eol;
44 $newline = " ";
45 }
46 $newline .= $c;
47 } // end of for
48 $output .= $newline;
49 if ($j<count($lines)-1) $output .= $linebreak;
50 }
51 return trim($output);
52}
53
54/** vérifie si une adresse email convient comme adresse de redirection
55 * @param $email l'adresse email a verifier
56 * @return BOOL
57 */
58function isvalid_email_redirection($email) {
59 return isvalid_email($email) &&
60 !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email);
61}
62
63/* Un soundex en français posté par Frédéric Bouchery
64 Voici une adaptation en PHP de la fonction soundex2 francisée de Frédéric BROUARD (http://sqlpro.developpez.com/Soundex/).
65 C'est une bonne démonstration de la force des expressions régulières compatible Perl.
66 trouvé sur http://expreg.com/voirsource.php?id=40&type=Chaines%20de%20caract%E8res */
67function soundex_fr($sIn)
68{
69 // Si il n'y a pas de mot, on sort immédiatement
70 if ( $sIn === '' ) return ' ';
71 // On met tout en minuscule
72 $sIn = strtoupper( $sIn );
73 // On supprime les accents
74 $sIn = strtr( $sIn, '\ 0ÀÇÈÉÊ˼ÎÏÔÖÙÛÜ', 'AAASEEEEEIIOOUUU' );
75 // On supprime tout ce qui n'est pas une lettre
76 $sIn = preg_replace( '`[^A-Z]`', '', $sIn );
77 // Si la chaîne ne fait qu'un seul caractère, on sort avec.
78 if ( strlen( $sIn ) === 1 ) return $sIn . ' ';
79 // on remplace les consonnances primaires
80 $convIn = array( 'GUI', 'GUE', 'GA', 'GO', 'GU', 'CA', 'CO', 'CU', 'Q', 'CC', 'CK' );
81 $convOut = array( 'KI', 'KE', 'KA', 'KO', 'K', 'KA', 'KO', 'KU', 'K', 'K', 'K' );
82 $sIn = str_replace( $convIn, $convOut, $sIn );
83 // on remplace les voyelles sauf le Y et sauf la première par A
84 $sIn = preg_replace( '`(?<!^)[EIOU]`', 'A', $sIn );
85 // on remplace les préfixes puis on conserve la première lettre
86 // et on fait les remplacements complémentaires
87 $convIn = array( '`^KN`', '`^(PH|PF)`', '`^MAC`', '`^SCH`', '`^ASA`', '`(?<!^)KN`', '`(?<!^)(PH|PF)`', '`(?<!^)MAC`', '`(?<!^)SCH`', '`(?<!^)ASA`' );
88 $convOut = array( 'NN', 'FF', 'MCC', 'SSS', 'AZA', 'NN', 'FF', 'MCC', 'SSS', 'AZA' );
89 $sIn = preg_replace( $convIn, $convOut, $sIn );
90 // suppression des H sauf CH ou SH
91 $sIn = preg_replace( '`(?<![CS])H`', '', $sIn );
92 // suppression des Y sauf précédés d'un A
93 $sIn = preg_replace( '`(?<!A)Y`', '', $sIn );
94 // on supprime les terminaisons A, T, D, S
95 $sIn = preg_replace( '`[ATDS]$`', '', $sIn );
96 // suppression de tous les A sauf en tête
97 $sIn = preg_replace( '`(?!^)A`', '', $sIn );
98 // on supprime les lettres répétitives
99 $sIn = preg_replace( '`(.)\1`', '$1', $sIn );
100 // on ne retient que 4 caractères ou on complète avec des blancs
101 return substr( $sIn . ' ', 0, 4);
102}
103
104function make_forlife($prenom,$nom,$promo) {
105 $prenomUS = replace_accent(trim($prenom));
106 $nomUS = replace_accent(trim($nom));
107
108 $forlife = strtolower($prenomUS.".".$nomUS.".".$promo);
109 $forlife = str_replace(" ","-",$forlife);
110 $forlife = str_replace("'","",$forlife);
111 return $forlife;
112}
5480a216 113
114function check_ip($level)
115{
116 $test = array();
117 switch ($level) {
118 case 'unsafe': $test[] = "state = 'unsafe'";
119 case 'dangerous': $test[] = "state = 'dangerous'";
120 case 'ban': $test[] = "state = 'ban'"; break;
121 default: return false;
122 }
123 $res = XDB::query("SELECT state
124 FROM ip_watch
125 WHERE ip = {?} AND (" . implode(' OR ', $test) . ')',
126 $_SERVER['REMOTE_ADDR']);
127 return $res->numRows();
128}
129
0d693e2f 130function check_email($email, $message)
131{
132 $res = XDB::query("SELECT state, description
133 FROM emails_watch
134 WHERE state != 'safe' AND email = {?}", $email);
135 if ($res->numRows()) {
136 send_warning_mail($message);
137 return true;
138 }
139 return false;
140}
141
ccdbc270 142function check_redirect($red = null)
143{
144 require_once 'emails.inc.php';
145 if (is_null($red)) {
146 $red = new Redirect(S::v('uid'));
147 }
148 $_SESSION['no_redirect'] = !$red->other_active('');
149 $_SESSION['mx_failures'] = $red->get_broken_mx();
150
151}
152
5480a216 153function send_warning_mail($title)
154{
155 $mailer = new PlMailer();
156 $mailer->setFrom("webmaster@polytechnique.org");
dbede442 157 $mailer->addTo("hotliners@staff.polytechnique.org");
2efe5355 158 $mailer->setSubject("[Plat/al Security Alert] $title");
5480a216 159 $mailer->setTxtBody("Identifiants de session :\n" . var_export($_SESSION, true) . "\n\n"
160 ."Identifiants de connexion :\n" . var_export($_SERVER, true));
161 $mailer->send();
162}
163
0337d704 164?>