/[drupal]/contributions/modules/tribune/tribune.sleep.inc
ViewVC logotype

Contents of /contributions/modules/tribune/tribune.sleep.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Mon Nov 3 20:50:39 2008 UTC (12 months, 3 weeks ago) by seeschloss
Branch: MAIN
CVS Tags: DRUPAL-6--1-8, DRUPAL-6--2-0-BETA1, DRUPAL-6--2-0-BETA2, DRUPAL-6--1-7, DRUPAL-6--1-5, DRUPAL-6--1-6, HEAD
Branch point for: DRUPAL-6--2
Changes since 1.2: +18 -5 lines
File MIME type: text/x-php
Support iconv if mbstring is not present... if we have none, just avoid manipulating the string more than the strict necessary
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 "&" => "&amp;",
46 "<" => "&lt;",
47 ">" => "&gt;",
48 "'" => "&#039;",
49 '"' => "&quot;",
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 }

  ViewVC Help
Powered by ViewVC 1.1.2