2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 function quoted_printable_encode($input, $line_max = 76)
24 $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
26 $linebreak = "=0D=0A=\n ";
30 foreach ($lines as $j => $line) {
31 $linlen = strlen($line);
33 for($i = 0; $i < $linlen; $i++
) {
36 if ( ($dec == 32) && ($i == ($linlen - 1)) ) {
37 // convert space at eol only
39 } elseif ( ($dec == 61) ||
($dec < 32 ) ||
($dec > 126) ) {
40 // always encode "\t", which is *not* required
41 $c = $escape.strtoupper(sprintf("%02x",$dec));
43 if ( (strlen($newline) +
strlen($c)) >= $line_max ) { // CRLF is not counted
44 $output .= $newline.$escape.$eol;
50 if ($j<count($lines)-1) $output .= $linebreak;
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+/]
59 function rand_token($len = 22)
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);
68 $token = base64_encode($token);
69 $token = preg_replace("![Il10O+/]!", "", $token);
70 $token = substr($token,0,$len);
74 /** genere une chaine aleatoire convenable pour une url
75 * @param $len longueur souhaitée, 22 par défaut
76 * @return la chaine aleatoire
78 function rand_url_id($len = 22)
80 return rand_token($len);
84 /** genere une chaine aleatoire convenable pour un mot de passe
85 * @return la chaine aleatoire
92 /** Remove accent from a string and replace them by the nearest letter
94 global $lc_convert, $uc_convert;
95 $lc_convert = array('é' => 'e', 'è' => 'e', 'ë' => 'e', 'ê' => 'e',
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');
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',
106 'Ç' => 'C', 'Ñ' => 'N');
108 function replace_accent($string)
110 global $lc_convert, $uc_convert;
111 $string = strtr($string, $lc_convert);
112 return strtr($string, $uc_convert);
115 /* Un soundex en français posté par Frédéric Bouchery
116 Voici une adaptation en PHP de la fonction soundex2 francisée de Frédéric BROUARD (http://sqlpro.developpez.com/Soundex/).
117 C'est une bonne démonstration de la force des expressions régulières compatible Perl.
118 trouvé sur http://expreg.com/voirsource.php?id=40&type=Chaines%20de%20caract%E8res */
119 function soundex_fr($sIn)
121 static $convVIn, $convVOut, $convGuIn, $convGuOut, $accents;
122 if (!isset($convGuIn)) {
123 global $uc_convert, $lc_convert;
124 $convGuIn = array( 'GUI', 'GUE', 'GA', 'GO', 'GU', 'SCI', 'SCE', 'SC', 'CA', 'CO',
125 'CU', 'QU', 'Q', 'CC', 'CK', 'G', 'ST', 'PH');
126 $convGuOut = array( 'KI', 'KE', 'KA', 'KO', 'K', 'SI', 'SE', 'SK', 'KA', 'KO',
127 'KU', 'K', 'K', 'K', 'K', 'J', 'T', 'F');
128 $convVIn = array( '/E?(AU)/', '/([EA])?[UI]([NM])([^EAIOUY]|$)/', '/[AE]O?[NM]([^AEIOUY]|$)/',
129 '/[EA][IY]([NM]?[^NM]|$)/', '/(^|[^OEUIA])(OEU|OE|EU)([^OEUIA]|$)/', '/OI/',
130 '/(ILLE?|I)/', '/O(U|W)/', '/O[NM]($|[^EAOUIY])/', '/(SC|S|C)H/',
131 '/([^AEIOUY1])[^AEIOUYLKTPNR]([UAO])([^AEIOUY])/', '/([^AEIOUY]|^)([AUO])[^AEIOUYLKTP]([^AEIOUY1])/', '/^KN/',
132 '/^PF/', '/C([^AEIOUY]|$)/',
133 '/C/', '/Z$/', '/(?<!^)Z+/', '/ER$/', '/H/', '/W/');
134 $convVOut = array( 'O', '1\3', 'A\1',
136 'Y', 'U', 'O\1', '9',
137 '\1\2\3', '\1\2\3', 'N',
139 'S', 'SE', 'S', 'E', '', 'V');
140 $accents = $uc_convert +
$lc_convert;
144 // Si il n'y a pas de mot, on sort immédiatement
145 if ( $sIn === '' ) return ' ';
146 // On supprime les accents
147 $sIn = strtr( $sIn, $accents);
148 // On met tout en minuscule
149 $sIn = strtoupper( $sIn );
150 // On supprime tout ce qui n'est pas une lettre
151 $sIn = preg_replace( '`[^A-Z]`', '', $sIn );
152 // Si la chaîne ne fait qu'un seul caractère, on sort avec.
153 if ( strlen( $sIn ) === 1 ) return $sIn . ' ';
154 // on remplace les consonnances primaires
155 $sIn = str_replace( $convGuIn, $convGuOut, $sIn );
156 // on supprime les lettres répétitives
157 $sIn = preg_replace( '`(.)\1`', '$1', $sIn );
158 // on réinterprète les voyelles
159 $sIn = preg_replace( $convVIn, $convVOut, $sIn);
160 // on supprime les terminaisons T, D, S, X (et le L qui précède si existe)
161 $sIn = preg_replace( '`L?[TDX]S?$`', '', $sIn );
162 // on supprime les E, A et Y qui ne sont pas en première position
163 $sIn = preg_replace( '`(?!^)Y([^AEOU]|$)`', '\1', $sIn);
164 $sIn = preg_replace( '`(?!^)[EA]`', '', $sIn);
165 return substr( $sIn . ' ', 0, 4);
168 /** Convert ip to uint (to store it in a database)
170 function ip_to_uint($ip)
172 $part = explode('.', $ip);
173 if (count($part) != 4) {
178 for ($i = 0 ; $i < 4 ; ++
$i) {
179 $v +
= $fact * $part[$i];
185 /** Convert uint to ip (to build a human understandable ip)
187 function uint_to_ip($uint)
189 return long2ip($uint);
192 /** Converts DateTime / string / timestamp to DateTime object
194 function make_datetime($date)
196 if ($date instanceof DateTime
) {
198 } elseif (preg_match('/^\d{14}$/', $date) ||
preg_match('/^\d{8}$/', $date)) {
199 return new DateTime($date);
200 } elseif (is_int($date) ||
is_numeric($date)) {
201 return new DateTime("@$date");
204 $d = new DateTime($date);
206 } catch (Exception
$e) {
212 /** Here to allow clean date formats instead of PHP's erroneous system...
215 * %A: Monday...Sunday
216 * %d: day, two digits
217 * %e: day, space before single digits
219 * %u: day of week (1 for monday, 7 for sunday)
220 * %w: day of week (0 for sunday, 6 for saturday)
222 * //%U: week number (first week is that with the first sunday)
223 * //%V: week number (ISO 8601-1988: first week is that with at least 4 week days)
224 * %W: week number (first week is that with the first monday)
227 * %B: January...December
229 * %m: month, two digits
231 * %C: century, two digits
232 * %g: year, two digits (ISO 8601-1988)
233 * %G: %g with four digits
234 * %y: year, two digits
235 * %Y: year, four digits
237 * %H: hour, two digits, 24h format
238 * %h: hour, two digits, 12h format
239 * %l: hour, two digits, space before single, 12h format
240 * %M: minute, two digits
245 * %S: second, two digits
247 * %z: timezone (offset)
248 * %Z: timezone (abbrev)
255 function format_datetime($date, $format)
257 $format = str_replace(array('%X', '%x', '%R', '%r', '%T', '%%'),
258 array('%T', '%e %B %Y', '%H:%M', '%I:%M:%S %p', '%H:%M:%S', '%%'),
261 $date = make_datetime($date);
262 $yy = (int) $date->format('Y');
263 // if ($yy > 1901 && $yy < 2038) {
264 // return strftime($format, $date->format('U'));
266 $w = (int) $date->format('w');
271 $weekday = new DateTime('2010-05-' . (10 +
$u));
272 $aa = strftime('%A', $weekday->format('U'));
273 $a = strftime('%a', $weekday->format('U'));
275 $m = $date->format('m');
276 $monthday = new DateTime('2010-' . $m . '-01');
277 $bb = strftime('%B', $monthday->format('U'));
278 $b = strftime('%b', $monthday->format('U'));
279 $y = $date->format('y');
281 $j = $date->format('z'); // Day of year
282 $d = $date->format('d'); // Day of month, 2 digits
283 $e = $date->format('j'); // Day of month, leanding space
284 if (strlen($e) == 1) {
289 $cc = substr($yy, 0, 2); // Century
290 $ww = $date->format('W'); // Week number
292 $hh = $date->format('H'); // Hour, 24h
293 $h = $date->format('h'); // Hour, 12h
294 $l = $date->format('g'); // Hour, 12h with leading space
295 if (strlen($l) == 1) {
299 $mm = $date->format('i'); // Minutes
300 $p = $date->format('A'); // AM/PM
301 $pp = $date->format('a'); // am/pm
302 $ss = $date->format('s'); // Seconds
304 $s = $date->format('U'); // Timestamp
305 $zz = $date->format('T'); // Timezone abbrev
306 $z = $date->format('Z'); // Timezone offset
309 array('%a', '%A', '%d', '%e', '%j', '%u', '%w',
310 '%W', '%b', '%B', '%h', '%m', '%C', '%y', '%Y',
311 '%H', '%h', '%l', '%M', '%p', '%P', '%S', '%z', '%Z',
313 array($a, $aa, $d, $e, $j, $u, $w,
314 $ww, $b, $bb, $b, $m, $cc, $y, $yy,
315 $hh, $h, $l, $mm, $p, $pp, $ss, $z, $zz,
323 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: