123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- define('DIRECTIONDETECTORPLUGIN_VERSION', '0.2.0');
- class DirectionDetectorPlugin extends Plugin {
-
- 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;
- }
-
- 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)
- return true;
- elseif($rtl==0)
-
- return self::startsWithRTLCharacter($words[0]);
- return false;
- }
-
- 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)
- return true;
- if($cc>=65136 && $cc<=65279)
- return true;
- if($cc>=64336 && $cc<=65023)
- return true;
- if($cc>=1424 && $cc<=1535)
- return true;
- if($cc>=64256 && $cc<=64335)
- return true;
- if($cc>=1792 && $cc<=1871)
- return true;
- if($cc>=1920 && $cc<=1983)
- return true;
- if($cc>=1984 && $cc<=2047)
- return true;
- if($cc>=11568 && $cc<=11647)
- return true;
- return false;
- }
-
- private static function getClearText($str){
- $str = preg_replace('/@[^ ]+|![^ ]+|#[^ ]+/u', '', $str);
- $str = preg_replace('/^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]/u', '', $str);
- $str = preg_replace("/[ \r\t\n]+/", ' ', trim($str));
- return $str;
- }
-
- function onEndShowScripts($action){
- if (common_logged_in()) {
- $action->script($this->path('jquery.DirectionDetector.js'));
- }
- }
-
- private static function utf8ToUnicode($str){
- $mState = 0;
-
- $mUcs4 = 0;
- $mBytes = 1;
- $out = array();
- $len = strlen($str);
- for($i = 0; $i < $len; $i++) {
- $in = ord($str{$i});
- if (0 == $mState) {
-
-
- if (0 == (0x80 & ($in))) {
-
- $out[] = $in;
- $mBytes = 1;
- } elseif (0xC0 == (0xE0 & ($in))) {
-
- $mUcs4 = ($in);
- $mUcs4 = ($mUcs4 & 0x1F) << 6;
- $mState = 1;
- $mBytes = 2;
- } elseif (0xE0 == (0xF0 & ($in))) {
-
- $mUcs4 = ($in);
- $mUcs4 = ($mUcs4 & 0x0F) << 12;
- $mState = 2;
- $mBytes = 3;
- } elseif (0xF0 == (0xF8 & ($in))) {
-
- $mUcs4 = ($in);
- $mUcs4 = ($mUcs4 & 0x07) << 18;
- $mState = 3;
- $mBytes = 4;
- } elseif (0xF8 == (0xFC & ($in))) {
-
- $mUcs4 = ($in);
- $mUcs4 = ($mUcs4 & 0x03) << 24;
- $mState = 4;
- $mBytes = 5;
- } elseif (0xFC == (0xFE & ($in))) {
-
- $mUcs4 = ($in);
- $mUcs4 = ($mUcs4 & 1) << 30;
- $mState = 5;
- $mBytes = 6;
- } else {
-
- return false;
- }
- } else {
-
-
- if (0x80 == (0xC0 & ($in))) {
-
- $shift = ($mState - 1) * 6;
- $tmp = $in;
- $tmp = ($tmp & 0x0000003F) << $shift;
- $mUcs4 |= $tmp;
- if (0 == --$mState) {
-
-
- if (
- ((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
- ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
- ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
- (4 < $mBytes) ||
-
- (($mUcs4 & 0xFFFFF800) == 0xD800) ||
-
- ($mUcs4 > 0x10FFFF)
- ){
- return false;
- }
- if (0xFEFF != $mUcs4) {
- $out[] = $mUcs4;
- }
-
- $mState = 0;
- $mUcs4 = 0;
- $mBytes = 1;
- }
- } else {
-
- return false;
- }
- }
- }
- return $out;
- }
-
- function onPluginVersion(array &$versions){
- $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/DirectionDetector';
- $versions[] = array(
- 'name' => 'Direction detector',
- 'version' => DIRECTIONDETECTORPLUGIN_VERSION,
- 'author' => 'Behrooz Shabani',
- 'homepage' => $url,
-
- 'rawdescription' => _m('Shows notices with right-to-left content in correct direction.')
- );
- return true;
- }
- }
|