| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2011 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., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | // Use native function if it's available (>= PHP5.3) |
| 23 | if (!function_exists('quoted_printable_encode')) { |
| 24 | function quoted_printable_encode($input, $line_max = 76) |
| 25 | { |
| 26 | $lines = preg_split("/(?:\r\n|\r|\n)/", $input); |
| 27 | $eol = "\n"; |
| 28 | $linebreak = "=0D=0A=\n "; |
| 29 | $escape = "="; |
| 30 | $output = ""; |
| 31 | |
| 32 | foreach ($lines as $j => $line) { |
| 33 | $linlen = strlen($line); |
| 34 | $newline = ""; |
| 35 | for($i = 0; $i < $linlen; $i++) { |
| 36 | $c = $line{$i}; |
| 37 | $dec = ord($c); |
| 38 | if ( ($dec == 32) && ($i == ($linlen - 1)) ) { |
| 39 | // convert space at eol only |
| 40 | $c = "=20"; |
| 41 | } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { |
| 42 | // always encode "\t", which is *not* required |
| 43 | $c = $escape.strtoupper(sprintf("%02x",$dec)); |
| 44 | } |
| 45 | if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted |
| 46 | $output .= $newline.$escape.$eol; |
| 47 | $newline = " "; |
| 48 | } |
| 49 | $newline .= $c; |
| 50 | } // end of for |
| 51 | $output .= $newline; |
| 52 | if ($j<count($lines)-1) $output .= $linebreak; |
| 53 | } |
| 54 | return trim($output); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** genere une chaine aleatoire de 22 caracteres ou moins |
| 59 | * @param $len longueur souhaitée, 22 par défaut |
| 60 | * @return la chaine aleatoire qui contient les caractères [A-Za-z0-9+/] |
| 61 | */ |
| 62 | function rand_token($len = 22) |
| 63 | { |
| 64 | $len = max(2, $len); |
| 65 | $len = min(50, $len); |
| 66 | $fp = fopen('/dev/urandom', 'r'); |
| 67 | // $len * 2 is certainly an overkill, |
| 68 | // but HEY, reading 40 bytes from /dev/urandom is not that slow ! |
| 69 | $token = fread($fp, $len * 2); |
| 70 | fclose($fp); |
| 71 | $token = base64_encode($token); |
| 72 | $token = preg_replace("![Il10O+/]!", "", $token); |
| 73 | $token = substr($token,0,$len); |
| 74 | return $token; |
| 75 | } |
| 76 | |
| 77 | /** genere une chaine aleatoire convenable pour une url |
| 78 | * @param $len longueur souhaitée, 22 par défaut |
| 79 | * @return la chaine aleatoire |
| 80 | */ |
| 81 | function rand_url_id($len = 22) |
| 82 | { |
| 83 | return rand_token($len); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** genere une chaine aleatoire convenable pour un mot de passe |
| 88 | * @return la chaine aleatoire |
| 89 | */ |
| 90 | function rand_pass() |
| 91 | { |
| 92 | return rand_token(8); |
| 93 | } |
| 94 | |
| 95 | /** Remove accent from a string and replace them by the nearest letter |
| 96 | */ |
| 97 | global $lc_convert, $uc_convert; |
| 98 | $lc_convert = array('é' => 'e', 'è' => 'e', 'ë' => 'e', 'ê' => 'e', |
| 99 | 'á' => 'a', 'à' => 'a', 'ä' => 'a', 'â' => 'a', 'å' => 'a', 'ã' => 'a', |
| 100 | 'ï' => 'i', 'î' => 'i', 'ì' => 'i', 'í' => 'i', |
| 101 | 'ô' => 'o', 'ö' => 'o', 'ò' => 'o', 'ó' => 'o', 'õ' => 'o', 'ø' => 'o', |
| 102 | 'ú' => 'u', 'ù' => 'u', 'û' => 'u', 'ü' => 'u', |
| 103 | 'ç' => 'c', 'ñ' => 'n'); |
| 104 | $uc_convert = array('É' => 'E', 'È' => 'E', 'Ë' => 'E', 'Ê' => 'E', |
| 105 | 'Á' => 'A', 'À' => 'A', 'Ä' => 'A', 'Â' => 'A', 'Å' => 'A', 'Ã' => 'A', |
| 106 | 'Ï' => 'I', 'Î' => 'I', 'Ì' => 'I', 'Í' => 'I', |
| 107 | 'Ô' => 'O', 'Ö' => 'O', 'Ò' => 'O', 'Ó' => 'O', 'Õ' => 'O', 'Ø' => 'O', |
| 108 | 'Ú' => 'U', 'Ù' => 'U', 'Û' => 'U', 'Ü' => 'U', |
| 109 | 'Ç' => 'C', 'Ñ' => 'N'); |
| 110 | |
| 111 | function replace_accent($string) |
| 112 | { |
| 113 | global $lc_convert, $uc_convert; |
| 114 | $string = strtr($string, $lc_convert); |
| 115 | return strtr($string, $uc_convert); |
| 116 | } |
| 117 | |
| 118 | /* Un soundex en français posté par Frédéric Bouchery |
| 119 | Voici une adaptation en PHP de la fonction soundex2 francisée de Frédéric BROUARD (http://sqlpro.developpez.com/Soundex/). |
| 120 | C'est une bonne démonstration de la force des expressions régulières compatible Perl. |
| 121 | trouvé sur http://expreg.com/voirsource.php?id=40&type=Chaines%20de%20caract%E8res */ |
| 122 | function soundex_fr($sIn) |
| 123 | { |
| 124 | static $convVIn, $convVOut, $convGuIn, $convGuOut, $accents; |
| 125 | if (!isset($convGuIn)) { |
| 126 | global $uc_convert, $lc_convert; |
| 127 | $convGuIn = array( 'GUI', 'GUE', 'GA', 'GO', 'GU', 'SCI', 'SCE', 'SC', 'CA', 'CO', |
| 128 | 'CU', 'QU', 'Q', 'CC', 'CK', 'G', 'ST', 'PH'); |
| 129 | $convGuOut = array( 'KI', 'KE', 'KA', 'KO', 'K', 'SI', 'SE', 'SK', 'KA', 'KO', |
| 130 | 'KU', 'K', 'K', 'K', 'K', 'J', 'T', 'F'); |
| 131 | $convVIn = array( '/E?(AU)/', '/([EA])?[UI]([NM])([^EAIOUY]|$)/', '/[AE]O?[NM]([^AEIOUY]|$)/', |
| 132 | '/[EA][IY]([NM]?[^NM]|$)/', '/(^|[^OEUIA])(OEU|OE|EU)([^OEUIA]|$)/', '/OI/', |
| 133 | '/(ILLE?|I)/', '/O(U|W)/', '/O[NM]($|[^EAOUIY])/', '/(SC|S|C)H/', |
| 134 | '/([^AEIOUY1])[^AEIOUYLKTPNR]([UAO])([^AEIOUY])/', '/([^AEIOUY]|^)([AUO])[^AEIOUYLKTP]([^AEIOUY1])/', '/^KN/', |
| 135 | '/^PF/', '/C([^AEIOUY]|$)/', |
| 136 | '/C/', '/Z$/', '/(?<!^)Z+/', '/ER$/', '/H/', '/W/'); |
| 137 | $convVOut = array( 'O', '1\3', 'A\1', |
| 138 | 'E\1', '\1E\3', 'O', |
| 139 | 'Y', 'U', 'O\1', '9', |
| 140 | '\1\2\3', '\1\2\3', 'N', |
| 141 | 'F', 'K\1', |
| 142 | 'S', 'SE', 'S', 'E', '', 'V'); |
| 143 | $accents = $uc_convert + $lc_convert; |
| 144 | $accents['Ç'] = 'S'; |
| 145 | $accents['¿'] = 'E'; |
| 146 | } |
| 147 | // Si il n'y a pas de mot, on sort immédiatement |
| 148 | if ( $sIn === '' ) return ' '; |
| 149 | // On supprime les accents |
| 150 | $sIn = strtr( $sIn, $accents); |
| 151 | // On met tout en minuscule |
| 152 | $sIn = strtoupper( $sIn ); |
| 153 | // On supprime tout ce qui n'est pas une lettre |
| 154 | $sIn = preg_replace( '`[^A-Z]`', '', $sIn ); |
| 155 | // Si la chaîne ne fait qu'un seul caractère, on sort avec. |
| 156 | if ( strlen( $sIn ) === 1 ) return $sIn . ' '; |
| 157 | // on remplace les consonnances primaires |
| 158 | $sIn = str_replace( $convGuIn, $convGuOut, $sIn ); |
| 159 | // on supprime les lettres répétitives |
| 160 | $sIn = preg_replace( '`(.)\1`', '$1', $sIn ); |
| 161 | // on réinterprète les voyelles |
| 162 | $sIn = preg_replace( $convVIn, $convVOut, $sIn); |
| 163 | // on supprime les terminaisons T, D, S, X (et le L qui précède si existe) |
| 164 | $sIn = preg_replace( '`L?[TDX]S?$`', '', $sIn ); |
| 165 | // on supprime les E, A et Y qui ne sont pas en première position |
| 166 | $sIn = preg_replace( '`(?!^)Y([^AEOU]|$)`', '\1', $sIn); |
| 167 | $sIn = preg_replace( '`(?!^)[EA]`', '', $sIn); |
| 168 | return substr( $sIn . ' ', 0, 4); |
| 169 | } |
| 170 | |
| 171 | /** Convert ip to uint (to store it in a database) |
| 172 | */ |
| 173 | function ip_to_uint($ip) |
| 174 | { |
| 175 | $part = explode('.', $ip); |
| 176 | if (count($part) != 4) { |
| 177 | return null; |
| 178 | } |
| 179 | $v = 0; |
| 180 | $fact = 0x1000000; |
| 181 | for ($i = 0 ; $i < 4 ; ++$i) { |
| 182 | $v += $fact * $part[$i]; |
| 183 | $fact >>= 8; |
| 184 | } |
| 185 | return $v; |
| 186 | } |
| 187 | |
| 188 | /** Convert uint to ip (to build a human understandable ip) |
| 189 | */ |
| 190 | function uint_to_ip($uint) |
| 191 | { |
| 192 | return long2ip($uint); |
| 193 | } |
| 194 | |
| 195 | /** Converts DateTime / string / timestamp to DateTime object |
| 196 | */ |
| 197 | function make_datetime($date) |
| 198 | { |
| 199 | if ($date instanceof DateTime) { |
| 200 | return $date; |
| 201 | } elseif (preg_match('/^\d{14}$/', $date) || preg_match('/^\d{8}$/', $date)) { |
| 202 | return new DateTime($date); |
| 203 | } elseif (is_int($date) || is_numeric($date)) { |
| 204 | return new DateTime("@$date"); |
| 205 | } else { |
| 206 | try { |
| 207 | $list = explode('/', $date); |
| 208 | if (count($list) == 3) { |
| 209 | $date = $list[1] . '/' . $list[0] . '/' . $list[2]; |
| 210 | } |
| 211 | // FIXME: On PHP < 5.3, parsing error are reported using an error, |
| 212 | // not an exception. Thus count the number of error to detect |
| 213 | // errors. |
| 214 | $errors = @count($GLOBALS['pl_errors']); |
| 215 | $d = new DateTime($date); |
| 216 | if (@count($GLOBALS['pl_errors']) > $errors) { |
| 217 | return null; |
| 218 | } |
| 219 | return $d; |
| 220 | } catch (Exception $e) { |
| 221 | return null; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** Here to allow clean date formats instead of PHP's erroneous system... |
| 227 | * Format : |
| 228 | * %a: Mon...Sun |
| 229 | * %A: Monday...Sunday |
| 230 | * %d: day, two digits |
| 231 | * %e: day, space before single digits |
| 232 | * %j: day of year |
| 233 | * %u: day of week (1 for monday, 7 for sunday) |
| 234 | * %w: day of week (0 for sunday, 6 for saturday) |
| 235 | * |
| 236 | * //%U: week number (first week is that with the first sunday) |
| 237 | * //%V: week number (ISO 8601-1988: first week is that with at least 4 week days) |
| 238 | * %W: week number (first week is that with the first monday) |
| 239 | * |
| 240 | * %b: Jan...Dec |
| 241 | * %B: January...December |
| 242 | * %h: = %b |
| 243 | * %m: month, two digits |
| 244 | * |
| 245 | * %C: century, two digits |
| 246 | * %g: year, two digits (ISO 8601-1988) |
| 247 | * %G: %g with four digits |
| 248 | * %y: year, two digits |
| 249 | * %Y: year, four digits |
| 250 | * |
| 251 | * %H: hour, two digits, 24h format |
| 252 | * %h: hour, two digits, 12h format |
| 253 | * %l: hour, two digits, space before single, 12h format |
| 254 | * %M: minute, two digits |
| 255 | * %p: AM/PM |
| 256 | * %P: am/pm |
| 257 | * %r: %I:%M:%S %p |
| 258 | * %R: %H:%M |
| 259 | * %S: second, two digits |
| 260 | * %T: %H:%M:%S |
| 261 | * %z: timezone (offset) |
| 262 | * %Z: timezone (abbrev) |
| 263 | * |
| 264 | * %x: %e %B %Y |
| 265 | * %X: %T |
| 266 | * %s: unix timestamp |
| 267 | * %%: % |
| 268 | */ |
| 269 | function format_datetime($date, $format) |
| 270 | { |
| 271 | $format = str_replace(array('%X', '%x', '%R', '%r', '%T', '%%'), |
| 272 | array('%T', '%e %B %Y', '%H:%M', '%I:%M:%S %p', '%H:%M:%S', '%%'), |
| 273 | $format); |
| 274 | |
| 275 | $date = make_datetime($date); |
| 276 | $yy = (int) $date->format('Y'); |
| 277 | // if ($yy > 1901 && $yy < 2038) { |
| 278 | // return strftime($format, $date->format('U')); |
| 279 | // } else { |
| 280 | $w = (int) $date->format('w'); |
| 281 | $u = $w; |
| 282 | if ($u == 0) { |
| 283 | $u = 7; |
| 284 | } |
| 285 | $weekday = new DateTime('2010-05-' . (10 + $u)); |
| 286 | $aa = strftime('%A', $weekday->format('U')); |
| 287 | $a = strftime('%a', $weekday->format('U')); |
| 288 | |
| 289 | $m = $date->format('m'); |
| 290 | $monthday = new DateTime('2010-' . $m . '-01'); |
| 291 | $bb = strftime('%B', $monthday->format('U')); |
| 292 | $b = strftime('%b', $monthday->format('U')); |
| 293 | $y = $date->format('y'); |
| 294 | |
| 295 | $j = $date->format('z'); // Day of year |
| 296 | $d = $date->format('d'); // Day of month, 2 digits |
| 297 | $e = $date->format('j'); // Day of month, leanding space |
| 298 | if (strlen($e) == 1) { |
| 299 | $e = ' ' . $e; |
| 300 | } |
| 301 | |
| 302 | $yy = "$yy"; |
| 303 | $cc = substr($yy, 0, 2); // Century |
| 304 | $ww = $date->format('W'); // Week number |
| 305 | |
| 306 | $hh = $date->format('H'); // Hour, 24h |
| 307 | $h = $date->format('h'); // Hour, 12h |
| 308 | $l = $date->format('g'); // Hour, 12h with leading space |
| 309 | if (strlen($l) == 1) { |
| 310 | $l = ' ' . $l; |
| 311 | } |
| 312 | |
| 313 | $mm = $date->format('i'); // Minutes |
| 314 | $p = $date->format('A'); // AM/PM |
| 315 | $pp = $date->format('a'); // am/pm |
| 316 | $ss = $date->format('s'); // Seconds |
| 317 | |
| 318 | $s = $date->format('U'); // Timestamp |
| 319 | $zz = $date->format('T'); // Timezone abbrev |
| 320 | $z = $date->format('Z'); // Timezone offset |
| 321 | |
| 322 | $txt = str_replace( |
| 323 | array('%a', '%A', '%d', '%e', '%j', '%u', '%w', |
| 324 | '%W', '%b', '%B', '%h', '%m', '%C', '%y', '%Y', |
| 325 | '%H', '%h', '%l', '%M', '%p', '%P', '%S', '%z', '%Z', |
| 326 | '%%'), |
| 327 | array($a, $aa, $d, $e, $j, $u, $w, |
| 328 | $ww, $b, $bb, $b, $m, $cc, $y, $yy, |
| 329 | $hh, $h, $l, $mm, $p, $pp, $ss, $z, $zz, |
| 330 | '%'), |
| 331 | $format); |
| 332 | |
| 333 | return $txt; |
| 334 | // } |
| 335 | } |
| 336 | |
| 337 | /** Get the first n characters of the string |
| 338 | */ |
| 339 | function left($string, $count) |
| 340 | { |
| 341 | return substr($string, 0, $count); |
| 342 | } |
| 343 | |
| 344 | /** Get the last n characters of the string |
| 345 | */ |
| 346 | function right($string, $count) |
| 347 | { |
| 348 | return substr($string, -$count); |
| 349 | } |
| 350 | |
| 351 | /** Check if a string is a prefix for another one. |
| 352 | */ |
| 353 | function starts_with($string, $prefix, $caseSensitive = true) |
| 354 | { |
| 355 | $prefixLen = strlen($prefix); |
| 356 | if (strlen($string) < $prefixLen) { |
| 357 | return false; |
| 358 | } |
| 359 | $part = left($string, $prefixLen); |
| 360 | if ($caseSensitive) { |
| 361 | return strcmp($prefix, $part) === 0; |
| 362 | } else { |
| 363 | return strcasecmp($prefix, $part) === 0; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** Check if a string is a suffix for another one. |
| 368 | */ |
| 369 | function ends_with($string, $suffix, $caseSensitive = true) |
| 370 | { |
| 371 | $suffixLen = strlen($suffix); |
| 372 | if (strlen($string) < $suffixLen) { |
| 373 | return false; |
| 374 | } |
| 375 | $part = right($string, $suffixLen); |
| 376 | if ($caseSensitive) { |
| 377 | return strcmp($suffix, $part) === 0; |
| 378 | } else { |
| 379 | return strcasecmp($suffix, $part) === 0; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** Check if the input data can be seen as an integer. |
| 384 | */ |
| 385 | function can_convert_to_integer($data) |
| 386 | { |
| 387 | return is_int($data) || ctype_digit($data) || ($data[0] == '-' && ctype_digit(substr($data, 1))); |
| 388 | } |
| 389 | |
| 390 | /** Interpret the input data as an integer or return false. |
| 391 | */ |
| 392 | function to_integer($data) |
| 393 | { |
| 394 | if (!can_convert_to_integer($data)) { |
| 395 | return false; |
| 396 | } |
| 397 | return intval($data); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: |
| 402 | ?> |