]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DirectionDetector/DirectionDetectorPlugin.php
b1362b166fea669f1b6996eb969b87656ecb9cb5
[quix0rs-gnu-social.git] / plugins / DirectionDetector / DirectionDetectorPlugin.php
1 <?php
2 /**
3  * DirectionDetector plugin, detects notices with RTL content & sets RTL
4  * style for them.
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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  * @category     Plugin
20  * @package      StatusNet
21  * @author       Behrooz shabani (everplays) - <behrooz@rock.com>
22  * @copyright    2009-2010 Behrooz shabani
23  * @license      http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
24  *
25  */
26
27 if (!defined('STATUSNET')) {
28                 exit(1);
29 }
30
31 define('DIRECTIONDETECTORPLUGIN_VERSION', '0.1.2');
32
33 class DirectionDetectorPlugin extends Plugin {
34         /**
35          * SN plugin API, here we will make changes on rendered column
36          *
37          * @param object $notice notice is going to be saved
38          */
39         public function onStartNoticeSave(&$notice){
40                 if(!preg_match('/<span class="rtl">/', $notice->rendered) && self::isRTL($notice->content))
41                         $notice->rendered = '<span class="rtl">'.$notice->rendered.'</span>';
42                 return true;
43         }
44
45         /**
46          * SN plugin API, here we will add css needed for modifiyed rendered
47          *
48          * @param
49          */
50         public function onEndShowStatusNetStyles($xml){
51                 $xml->element('style', array('type' => 'text/css'), 'span.rtl {display:block;direction:rtl;text-align:right;float:right;width:490px;} .notice .author {float:left}');
52         }
53         /**
54          * checks that passed string is a RTL language or not
55          *
56          * @param string $str String to be checked
57          */
58         public static function isRTL($str){
59                 self::getClearText($str);
60                 if( is_array($cc = self::utf8ToUnicode(mb_substr($str, 0, 1, 'utf-8'))) )
61                         $cc = $cc[0];
62                 else
63                         return false;
64                 if($cc>=1536 && $cc<=1791) // Arabic, Persian, Urdu, Kurdish, ...
65                         return true;
66                 if($cc>=65136 && $cc<=65279) // Arabic peresent 2
67                         return true;
68                 if($cc>=64336 && $cc<=65023) // Arabic peresent 1
69                         return true;
70                 if($cc>=1424 && $cc<=1535) // Hebrew
71                         return true;
72                 if($cc>=64256 && $cc<=64335) // Hebrew peresent
73                         return true;
74                 if($cc>=1792 && $cc<=1871) // Syriac
75                         return true;
76                 if($cc>=1920 && $cc<=1983) // Thaana
77                         return true;
78                 if($cc>=1984 && $cc<=2047) // NKo
79                         return true;
80                 if($cc>=11568 && $cc<=11647) // Tifinagh
81                         return true;
82                 return false;
83         }
84
85         /**
86          * clears text from replies, tags, groups, repeats & whitespaces
87          *
88          * @param string &$str string to be cleared
89          */
90         private static function getClearText(&$str){
91                 $str = preg_replace('/@[^ ]+|![^ ]+|#[^ ]+/u', '', $str); // reply, tag, group
92                 $str = preg_replace('/^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]/u', '', $str); // redent, retweet
93                 $str = preg_replace("/[ \r\t\n]+/", ' ', trim($str)); // remove spaces
94         }
95
96         /**
97          * Takes a UTF-8 string and returns an array of ints representing the
98          * Unicode characters. Astral planes are supported i.e. the ints in the
99          * output can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
100          * are not allowed. ### modified ### returns first character code
101          *
102          * Returns false if the input string isn't a valid UTF-8 octet sequence.
103          */
104         private static function utf8ToUnicode($str){
105                 $mState = 0;       // cached expected number of octets after the current octet
106                                    // until the beginning of the next UTF8 character sequence
107                 $mUcs4  = 0;     // cached Unicode character
108                 $mBytes = 1;       // cached expected number of octets in the current sequence
109                 $out = array();
110                 $len = strlen($str);
111
112                 for($i = 0; $i < $len; $i++) {
113                         $in = ord($str{$i});
114                         if (0 == $mState) {
115                                 // When mState is zero we expect either a US-ASCII character or a
116                                 // multi-octet sequence.
117                                 if (0 == (0x80 & ($in))) {
118                                         // US-ASCII, pass straight through.
119                                         $out[] = $in;
120                                         $mBytes = 1;
121                                 } elseif (0xC0 == (0xE0 & ($in))) {
122                                         // First octet of 2 octet sequence
123                                         $mUcs4 = ($in);
124                                         $mUcs4 = ($mUcs4 & 0x1F) << 6;
125                                         $mState = 1;
126                                         $mBytes = 2;
127                                 } elseif (0xE0 == (0xF0 & ($in))) {
128                                         // First octet of 3 octet sequence
129                                         $mUcs4 = ($in);
130                                         $mUcs4 = ($mUcs4 & 0x0F) << 12;
131                                         $mState = 2;
132                                         $mBytes = 3;
133                                 } elseif (0xF0 == (0xF8 & ($in))) {
134                                         // First octet of 4 octet sequence
135                                         $mUcs4 = ($in);
136                                         $mUcs4 = ($mUcs4 & 0x07) << 18;
137                                         $mState = 3;
138                                         $mBytes = 4;
139                                 } elseif (0xF8 == (0xFC & ($in))) {
140                                         /* First octet of 5 octet sequence.
141                                          *
142                                          * This is illegal because the encoded codepoint must be either
143                                          * (a) not the shortest form or
144                                          * (b) outside the Unicode range of 0-0x10FFFF.
145                                          * Rather than trying to resynchronize, we will carry on until the end
146                                          * of the sequence and let the later error handling code catch it.
147                                          */
148                                         $mUcs4 = ($in);
149                                         $mUcs4 = ($mUcs4 & 0x03) << 24;
150                                         $mState = 4;
151                                         $mBytes = 5;
152                                 } elseif (0xFC == (0xFE & ($in))) {
153                                         // First octet of 6 octet sequence, see comments for 5 octet sequence.
154                                         $mUcs4 = ($in);
155                                         $mUcs4 = ($mUcs4 & 1) << 30;
156                                         $mState = 5;
157                                         $mBytes = 6;
158                                 } else {
159                                         /* Current octet is neither in the US-ASCII range nor a legal first
160                                          * octet of a multi-octet sequence.
161                                          */
162                                         return false;
163                                 }
164                         } else {
165                                 // When mState is non-zero, we expect a continuation of the multi-octet
166                                 // sequence
167                                 if (0x80 == (0xC0 & ($in))) {
168                                         // Legal continuation.
169                                         $shift = ($mState - 1) * 6;
170                                         $tmp = $in;
171                                         $tmp = ($tmp & 0x0000003F) << $shift;
172                                         $mUcs4 |= $tmp;
173                                         if (0 == --$mState) {
174                                                 /* End of the multi-octet sequence. mUcs4 now contains the final
175                                                  * Unicode codepoint to be output
176                                                  *
177                                                  * Check for illegal sequences and codepoints.
178                                                  */
179                                                 // From Unicode 3.1, non-shortest form is illegal
180                                                 if      (
181                                                                 ((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
182                                                                 ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
183                                                                 ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
184                                                                 (4 < $mBytes) ||
185                                                                 // From Unicode 3.2, surrogate characters are illegal
186                                                                 (($mUcs4 & 0xFFFFF800) == 0xD800) ||
187                                                                 // Codepoints outside the Unicode range are illegal
188                                                                 ($mUcs4 > 0x10FFFF)
189                                                         ){
190                                                         return false;
191                                                 }
192                                                 if (0xFEFF != $mUcs4) {
193                                                         $out[] = $mUcs4;
194                                                 }
195                                                 //initialize UTF8 cache
196                                                 $mState = 0;
197                                                 $mUcs4  = 0;
198                                                 $mBytes = 1;
199                                         }
200                                 } else {
201                                         /* ((0xC0 & (*in) != 0x80) && (mState != 0))
202                                          *
203                                          * Incomplete multi-octet sequence.
204                                          */
205                                         return false;
206                                 }
207                         }
208                 }
209                 return $out;
210         }
211
212         /**
213          * plugin details
214          */
215         function onPluginVersion(&$versions){
216                 $versions[] = array(
217                         'name' => 'Direction detector',
218                         'version' => DIRECTIONDETECTORPLUGIN_VERSION,
219                         'author' => 'Behrooz Shabani',
220                         // TRANS: Direction detector plugin description.
221                         'rawdescription' => _m('Shows notices with right-to-left content in correct direction.')
222                 );
223                 return true;
224         }
225 }
226
227 /*
228 // Example:
229 var_dump(DirectionDetectorPlugin::isRTL('RT @everplays ♺: دادگاه به دليل عدم حضور وکلای متهمان بنا بر اصل ١٣٥ قانون اساسی غير قانونی است')); // true
230 */