3 * DirectionDetector plugin, detects notices with RTL content & sets RTL
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.
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.
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/>.
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
27 if (!defined('STATUSNET')) {
31 define('DIRECTIONDETECTORPLUGIN_VERSION', '0.2.0');
33 class DirectionDetectorPlugin extends Plugin {
35 * SN plugin API, here we will make changes on rendered column
37 * @param object $notice notice is going to be saved
39 public function onStartNoticeSave(Notice $notice) {
40 if(!preg_match('/<span class="rtl">/', $notice->rendered) && self::isRTL($notice->content))
41 $notice->rendered = '<span class="rtl">'.$notice->rendered.'</span>';
46 * is passed string a rtl content or not
48 * @param string $content
51 public static function isRTL($content) {
52 $content = self::getClearText($content);
53 $words = explode(' ', $content);
55 foreach($words as $str)
56 if(self::startsWithRTLCharacter($str))
60 if($rtl>0)// if number of rtl words is more than ltr words so it's a rtl content
63 // check first word again
64 return self::startsWithRTLCharacter($words[0]);
69 * checks that passed string starts with a RTL language or not
74 public static function startsWithRTLCharacter($str ){
75 if (strlen($str) < 1) {
78 if( is_array($cc = self::utf8ToUnicode(mb_substr($str, 0, 1, 'utf-8'))) )
82 if($cc>=1536 && $cc<=1791) // arabic, persian, urdu, kurdish, ...
84 if($cc>=65136 && $cc<=65279) // arabic peresent 2
86 if($cc>=64336 && $cc<=65023) // arabic peresent 1
88 if($cc>=1424 && $cc<=1535) // hebrew
90 if($cc>=64256 && $cc<=64335) // hebrew peresent
92 if($cc>=1792 && $cc<=1871) // Syriac
94 if($cc>=1920 && $cc<=1983) // Thaana
96 if($cc>=1984 && $cc<=2047) // NKo
98 if($cc>=11568 && $cc<=11647) // Tifinagh
104 * clears text from replys, tags, groups, reteets & whitespaces
109 private static function getClearText($str) {
110 $str = preg_replace('/@[^ ]+|![^ ]+|#[^ ]+/u', '', $str); // reply, tag, group
111 $str = preg_replace('/^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]/u', '', $str); // redent, retweet
112 $str = preg_replace("/[ \r\t\n]+/", ' ', trim($str)); // remove spaces
117 * adds javascript to do same thing on input textarea
119 * @param Action $action
121 function onEndShowScripts(Action $action){
122 if (common_logged_in()) {
123 $action->script($this->path('jquery.DirectionDetector.js'));
128 * Takes an UTF-8 string and returns an array of ints representing the
129 * Unicode characters. Astral planes are supported ie. the ints in the
130 * output can be > 0xFFFF. O$ccurrances of the BOM are ignored. Surrogates
134 * @return mixed array of ints, or false on invalid input
136 private static function utf8ToUnicode($str){
137 $mState = 0; // cached expected number of octets after the current octet
138 // until the beginning of the next UTF8 character sequence
139 $mUcs4 = 0; // cached Unicode character
140 $mBytes = 1; // cached expected number of octets in the current sequence
144 for($i = 0; $i < $len; $i++) {
147 // When mState is zero we expect either a US-ASCII character or a
148 // multi-octet sequence.
149 if (0 == (0x80 & ($in))) {
150 // US-ASCII, pass straight through.
153 } elseif (0xC0 == (0xE0 & ($in))) {
154 // First octet of 2 octet sequence
156 $mUcs4 = ($mUcs4 & 0x1F) << 6;
159 } elseif (0xE0 == (0xF0 & ($in))) {
160 // First octet of 3 octet sequence
162 $mUcs4 = ($mUcs4 & 0x0F) << 12;
165 } elseif (0xF0 == (0xF8 & ($in))) {
166 // First octet of 4 octet sequence
168 $mUcs4 = ($mUcs4 & 0x07) << 18;
171 } elseif (0xF8 == (0xFC & ($in))) {
172 /* First octet of 5 octet sequence.
174 * This is illegal because the encoded codepoint must be either
175 * (a) not the shortest form or
176 * (b) outside the Unicode range of 0-0x10FFFF.
177 * Rather than trying to resynchronize, we will carry on until the end
178 * of the sequence and let the later error handling code catch it.
181 $mUcs4 = ($mUcs4 & 0x03) << 24;
184 } elseif (0xFC == (0xFE & ($in))) {
185 // First octet of 6 octet sequence, see comments for 5 octet sequence.
187 $mUcs4 = ($mUcs4 & 1) << 30;
191 /* Current octet is neither in the US-ASCII range nor a legal first
192 * octet of a multi-octet sequence.
197 // When mState is non-zero, we expect a continuation of the multi-octet
199 if (0x80 == (0xC0 & ($in))) {
200 // Legal continuation.
201 $shift = ($mState - 1) * 6;
203 $tmp = ($tmp & 0x0000003F) << $shift;
205 if (0 == --$mState) {
206 /* End of the multi-octet sequence. mUcs4 now contains the final
207 * Unicode codepoint to be output
209 * Check for illegal sequences and codepoints.
211 // From Unicode 3.1, non-shortest form is illegal
213 ((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
214 ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
215 ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
217 // From Unicode 3.2, surrogate characters are illegal
218 (($mUcs4 & 0xFFFFF800) == 0xD800) ||
219 // Codepoints outside the Unicode range are illegal
224 if (0xFEFF != $mUcs4) {
227 //initialize UTF8 cache
233 /* ((0xC0 & (*in) != 0x80) && (mState != 0))
235 * Incomplete multi-octet sequence.
247 function onPluginVersion(array &$versions){
248 $url = 'http://status.net/wiki/Plugin:DirectionDetector';
251 'name' => 'Direction detector',
252 'version' => DIRECTIONDETECTORPLUGIN_VERSION,
253 'author' => 'Behrooz Shabani',
255 // TRANS: Plugin description.
256 'rawdescription' => _m('Shows notices with right-to-left content in correct direction.')
264 var_dump(DirectionDetectorPlugin::isRTL('RT @everplays ♺: دادگاه به دليل عدم حضور وکلای متهمان بنا بر اصل ١٣٥ قانون اساسی غير قانونی است')); // true