+ /**
+ * SN plugin API, here we will make changes on rendered column
+ *
+ * @param object $notice notice is going to be saved
+ */
+ public function onStartNoticeSave($notice){
+ if(!preg_match('/<span class="rtl">/', $notice->rendered) && self::isRTL($notice->content))
+ $notice->rendered = '<span class="rtl">'.$notice->rendered.'</span>';
+ return true;
+ }
+
+ /**
+ * SN plugin API, here we will add css needed for modifiyed rendered
+ *
+ * @param Action $xml
+ */
+ public function onEndShowStatusNetStyles($xml){
+ $xml->element('style', array('type' => 'text/css'), 'span.rtl {display:block;direction:rtl;text-align:right;float:right;} .notice .author {float:left}');
+ }
+
+ /**
+ * is passed string a rtl content or not
+ *
+ * @param string $content
+ * @return boolean
+ */
+ public static function isRTL($content){
+ $content = self::getClearText($content);
+ $words = explode(' ', $content);
+ $rtl = 0;
+ foreach($words as $str)
+ if(self::startsWithRTLCharacter($str))
+ $rtl++;
+ else
+ $rtl--;
+ if($rtl>0)// if number of rtl words is more than ltr words so it's a rtl content
+ return true;
+ elseif($rtl==0)
+ // check first word again
+ return self::startsWithRTLCharacter($words[0]);
+ return false;
+ }
+
+ /**
+ * checks that passed string starts with a RTL language or not
+ *
+ * @param string $str
+ * @return boolean
+ */
+ public static function startsWithRTLCharacter($str){
+ if (strlen($str) < 1) {
+ return false;
+ }
+ if( is_array($cc = self::utf8ToUnicode(mb_substr($str, 0, 1, 'utf-8'))) )
+ $cc = $cc[0];
+ else
+ return false;
+ if($cc>=1536 && $cc<=1791) // arabic, persian, urdu, kurdish, ...
+ return true;
+ if($cc>=65136 && $cc<=65279) // arabic peresent 2
+ return true;
+ if($cc>=64336 && $cc<=65023) // arabic peresent 1
+ return true;
+ if($cc>=1424 && $cc<=1535) // hebrew
+ return true;
+ if($cc>=64256 && $cc<=64335) // hebrew peresent
+ return true;
+ if($cc>=1792 && $cc<=1871) // Syriac
+ return true;
+ if($cc>=1920 && $cc<=1983) // Thaana
+ return true;
+ if($cc>=1984 && $cc<=2047) // NKo
+ return true;
+ if($cc>=11568 && $cc<=11647) // Tifinagh
+ return true;
+ return false;
+ }
+
+ /**
+ * clears text from replys, tags, groups, reteets & whitespaces
+ *
+ * @param string $str
+ * @return string
+ */
+ private static function getClearText($str){
+ $str = preg_replace('/@[^ ]+|![^ ]+|#[^ ]+/u', '', $str); // reply, tag, group
+ $str = preg_replace('/^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]/u', '', $str); // redent, retweet
+ $str = preg_replace("/[ \r\t\n]+/", ' ', trim($str)); // remove spaces
+ return $str;
+ }