| 1 |
<?php
|
| 2 |
// vim:filetype=php expandtab tabstop=2 softtabstop=2 shiftwidth=2 autoindent smartindent
|
| 3 |
// $Id: tribune.sleep.inc,v 1.2 2008/08/30 22:51:38 seeschloss Exp $
|
| 4 |
|
| 5 |
// des trucs du slipounet de LiNuCe qui marchent bien comme ça
|
| 6 |
function str_ctrl_clean($s) {
|
| 7 |
$length = strlen($s);
|
| 8 |
$byte = 0 ;
|
| 9 |
|
| 10 |
for ($i=0 ; $i < $length ; $i++) {
|
| 11 |
$byte = ord($s[$i]);
|
| 12 |
if ($byte < 32 or $byte == 127) {
|
| 13 |
$s[$i] = ' ';
|
| 14 |
}
|
| 15 |
}
|
| 16 |
return trim($s);
|
| 17 |
}
|
| 18 |
|
| 19 |
function str_clean($s, $length) {
|
| 20 |
$clean = "";
|
| 21 |
if (function_exists("mb_detect_encoding")) {
|
| 22 |
// let us assume that mb_substr exists too then
|
| 23 |
if (strcasecmp(mb_detect_encoding($s, 'UTF-8', TRUE), 'UTF-8') == 0) {
|
| 24 |
$clean = mb_substr(str_ctrl_clean($s), 0, $length, 'UTF-8');
|
| 25 |
}
|
| 26 |
} else if (function_exists("iconv_substr")) {
|
| 27 |
// there is no iconv_detect_encoding, so let us assume that the string is UTF-8-encoded
|
| 28 |
$clean = iconv_substr(str_ctrl_clean($s), 0, $length, 'UTF-8');
|
| 29 |
} else {
|
| 30 |
// better than nothing?
|
| 31 |
//$clean = substr(str_ctrl_clean($s), 0, $length);
|
| 32 |
// actually, we'll just pass it as it is, rather than risking splitting a character in two
|
| 33 |
$clean = $s;
|
| 34 |
}
|
| 35 |
|
| 36 |
return $clean;
|
| 37 |
}
|
| 38 |
|
| 39 |
function tribune_sleep($message, $length = -1) {
|
| 40 |
$message = str_clean($message, $length);
|
| 41 |
|
| 42 |
$message = preg_replace_callback(':<(m|s|u|b|i|tt)>(.*?)</\1>:', 'tribune_sleep_replace', $message);
|
| 43 |
|
| 44 |
$replacement = array(
|
| 45 |
"&" => "&",
|
| 46 |
"<" => "<",
|
| 47 |
">" => ">",
|
| 48 |
"'" => "'",
|
| 49 |
'"' => """,
|
| 50 |
chr(26) => "<",
|
| 51 |
chr(27) => ">",
|
| 52 |
chr(28) => "'",
|
| 53 |
chr(29) => '"',
|
| 54 |
);
|
| 55 |
|
| 56 |
$message = str_replace(array_keys($replacement), array_values($replacement), $message);
|
| 57 |
|
| 58 |
return $message;
|
| 59 |
}
|
| 60 |
|
| 61 |
function tribune_sleep_replace($matches) {
|
| 62 |
$text = preg_replace_callback(':<(m|s|u|b|i|tt)>(.*?)</\1>:', 'tribune_sleep_replace', $matches[2]);
|
| 63 |
if ($matches[1] == 'm') {
|
| 64 |
$start = "====> ". chr(26) ."b". chr(27) ."Moment ";
|
| 65 |
$stop = chr(26) ."/b". chr(27) ." <====";
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
$start = chr(26) . $matches[1] . chr(27);
|
| 69 |
$stop = chr(26) ."/". $matches[1] . chr(27);
|
| 70 |
}
|
| 71 |
return $start . $text . $stop;
|
| 72 |
}
|
| 73 |
|
| 74 |
function tribune_sleep_mother($matches) {
|
| 75 |
$text = preg_replace_callback(':<(m|s|u|b|i|tt)>([^'. chr(26) .']*)(.*)$:', 'tribune_sleep_mother', $matches[2]);
|
| 76 |
if ($matches[1] == 'm') {
|
| 77 |
$start = "====> ". chr(26) ."b". chr(27) ."Moment ";
|
| 78 |
$stop = chr(26) ."/b". chr(27) ." <====";
|
| 79 |
}
|
| 80 |
else {
|
| 81 |
$start = chr(26) . $matches[1] . chr(27);
|
| 82 |
$stop = chr(26) ."/". $matches[1] . chr(27);
|
| 83 |
}
|
| 84 |
return $start . $text . $stop . $matches[3];
|
| 85 |
}
|