X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fmisc.inc.php;h=ad37a92134433baa631ef45d3c352d0d783f9e31;hb=88541046480799a8d92eba186f22e22c0c64efb0;hp=aa3809b3a528d2b7235831de4b5d141554675c37;hpb=3ecd0a882aebbb539b4b0e507a360e961cd51c63;p=platal.git diff --git a/include/misc.inc.php b/include/misc.inc.php index aa3809b..ad37a92 100644 --- a/include/misc.inc.php +++ b/include/misc.inc.php @@ -1,6 +1,6 @@ $line) { - $linlen = strlen($line); - $newline = ""; - for($i = 0; $i < $linlen; $i++) { - $c = $line{$i}; - $dec = ord($c); - if ( ($dec == 32) && ($i == ($linlen - 1)) ) { - // convert space at eol only - $c = "=20"; - } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { - // always encode "\t", which is *not* required - $c = $escape.strtoupper(sprintf("%02x",$dec)); - } - if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted - $output .= $newline.$escape.$eol; - $newline = " "; - } - $newline .= $c; - } // end of for - $output .= $newline; - if ($j= PHP5.3) +if (!function_exists('quoted_printable_encode')) { + function quoted_printable_encode($input, $line_max = 76) + { + $lines = preg_split("/(?:\r\n|\r|\n)/", $input); + $eol = "\n"; + $linebreak = "=0D=0A=\n "; + $escape = "="; + $output = ""; + + foreach ($lines as $j => $line) { + $linlen = strlen($line); + $newline = ""; + for($i = 0; $i < $linlen; $i++) { + $c = $line{$i}; + $dec = ord($c); + if ( ($dec == 32) && ($i == ($linlen - 1)) ) { + // convert space at eol only + $c = "=20"; + } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { + // always encode "\t", which is *not* required + $c = $escape.strtoupper(sprintf("%02x",$dec)); + } + if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted + $output .= $newline.$escape.$eol; + $newline = " "; + } + $newline .= $c; + } // end of for + $output .= $newline; + if ($j $errors) { + return null; + } + return $d; + } catch (Exception $e) { + return null; + } + } +} + +/** Here to allow clean date formats instead of PHP's erroneous system... + * Format : + * %a: Mon...Sun + * %A: Monday...Sunday + * %d: day, two digits + * %e: day, space before single digits + * %j: day of year + * %u: day of week (1 for monday, 7 for sunday) + * %w: day of week (0 for sunday, 6 for saturday) + * + * //%U: week number (first week is that with the first sunday) + * //%V: week number (ISO 8601-1988: first week is that with at least 4 week days) + * %W: week number (first week is that with the first monday) + * + * %b: Jan...Dec + * %B: January...December + * %h: = %b + * %m: month, two digits + * + * %C: century, two digits + * %g: year, two digits (ISO 8601-1988) + * %G: %g with four digits + * %y: year, two digits + * %Y: year, four digits + * + * %H: hour, two digits, 24h format + * %h: hour, two digits, 12h format + * %l: hour, two digits, space before single, 12h format + * %M: minute, two digits + * %p: AM/PM + * %P: am/pm + * %r: %I:%M:%S %p + * %R: %H:%M + * %S: second, two digits + * %T: %H:%M:%S + * %z: timezone (offset) + * %Z: timezone (abbrev) + * + * %x: %e %B %Y + * %X: %T + * %s: unix timestamp + * %%: % + */ +function format_datetime($date, $format) +{ + $format = str_replace(array('%X', '%x', '%R', '%r', '%T', '%%'), + array('%T', '%e %B %Y', '%H:%M', '%I:%M:%S %p', '%H:%M:%S', '%%'), + $format); + + $date = make_datetime($date); + $yy = (int) $date->format('Y'); +// if ($yy > 1901 && $yy < 2038) { +// return strftime($format, $date->format('U')); +// } else { + $w = (int) $date->format('w'); + $u = $w; + if ($u == 0) { + $u = 7; + } + $weekday = new DateTime('2010-05-' . (10 + $u)); + $aa = strftime('%A', $weekday->format('U')); + $a = strftime('%a', $weekday->format('U')); + + $m = $date->format('m'); + $monthday = new DateTime('2010-' . $m . '-01'); + $bb = strftime('%B', $monthday->format('U')); + $b = strftime('%b', $monthday->format('U')); + $y = $date->format('y'); + + $j = $date->format('z'); // Day of year + $d = $date->format('d'); // Day of month, 2 digits + $e = $date->format('j'); // Day of month, leanding space + if (strlen($e) == 1) { + $e = ' ' . $e; + } + + $yy = "$yy"; + $cc = substr($yy, 0, 2); // Century + $ww = $date->format('W'); // Week number + + $hh = $date->format('H'); // Hour, 24h + $h = $date->format('h'); // Hour, 12h + $l = $date->format('g'); // Hour, 12h with leading space + if (strlen($l) == 1) { + $l = ' ' . $l; + } + + $mm = $date->format('i'); // Minutes + $p = $date->format('A'); // AM/PM + $pp = $date->format('a'); // am/pm + $ss = $date->format('s'); // Seconds + + $s = $date->format('U'); // Timestamp + $zz = $date->format('T'); // Timezone abbrev + $z = $date->format('Z'); // Timezone offset + + $txt = str_replace( + array('%a', '%A', '%d', '%e', '%j', '%u', '%w', + '%W', '%b', '%B', '%h', '%m', '%C', '%y', '%Y', + '%H', '%h', '%l', '%M', '%p', '%P', '%S', '%z', '%Z', + '%%'), + array($a, $aa, $d, $e, $j, $u, $w, + $ww, $b, $bb, $b, $m, $cc, $y, $yy, + $hh, $h, $l, $mm, $p, $pp, $ss, $z, $zz, + '%'), + $format); + + return $txt; +// } +} + +/** Get the first n characters of the string + */ +function left($string, $count) +{ + return substr($string, 0, $count); +} + +/** Get the last n characters of the string + */ +function right($string, $count) +{ + return substr($string, -$count); +} + +/** Check if a string is a prefix for another one. + */ +function starts_with($string, $prefix, $caseSensitive = true) +{ + $prefixLen = strlen($prefix); + if (strlen($string) < $prefixLen) { + return false; + } + $part = left($string, $prefixLen); + if ($caseSensitive) { + return strcmp($prefix, $part) === 0; + } else { + return strcasecmp($prefix, $part) === 0; + } +} + +/** Check if a string is a suffix for another one. + */ +function ends_with($string, $suffix, $caseSensitive = true) +{ + $suffixLen = strlen($suffix); + if (strlen($string) < $suffixLen) { + return false; + } + $part = right($string, $suffixLen); + if ($caseSensitive) { + return strcmp($suffix, $part) === 0; + } else { + return strcasecmp($suffix, $part) === 0; + } +} + +/** Check if the input data can be seen as an integer. + */ +function can_convert_to_integer($data) +{ + return is_int($data) || ctype_digit($data); +} + +/** Interpret the input data as an integer or return false. + */ +function to_integer($data) +{ + if (!can_convert_to_integer($data)) { + return false; + } + return intval($data); +} + + // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: ?>