DirectionDetectorPlugin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. if (!defined('STATUSNET')) {
  27. exit(1);
  28. }
  29. define('DIRECTIONDETECTORPLUGIN_VERSION', '0.2.0');
  30. class DirectionDetectorPlugin extends Plugin {
  31. /**
  32. * SN plugin API, here we will make changes on rendered column
  33. *
  34. * @param object $notice notice is going to be saved
  35. */
  36. public function onStartNoticeSave($notice){
  37. // don't use getRendered() here since it's not saved yet and thus can't ->update in case that would happen
  38. if(!preg_match('/<span class="rtl">/', $notice->rendered) && self::isRTL($notice->content))
  39. $notice->rendered = '<span class="rtl">'.$notice->rendered.'</span>';
  40. return true;
  41. }
  42. /**
  43. * is passed string a rtl content or not
  44. *
  45. * @param string $content
  46. * @return boolean
  47. */
  48. public static function isRTL($content){
  49. $content = self::getClearText($content);
  50. $words = explode(' ', $content);
  51. $rtl = 0;
  52. foreach($words as $str)
  53. if(self::startsWithRTLCharacter($str))
  54. $rtl++;
  55. else
  56. $rtl--;
  57. if($rtl>0)// if number of rtl words is more than ltr words so it's a rtl content
  58. return true;
  59. elseif($rtl==0)
  60. // check first word again
  61. return self::startsWithRTLCharacter($words[0]);
  62. return false;
  63. }
  64. /**
  65. * checks that passed string starts with a RTL language or not
  66. *
  67. * @param string $str
  68. * @return boolean
  69. */
  70. public static function startsWithRTLCharacter($str){
  71. if (strlen($str) < 1) {
  72. return false;
  73. }
  74. if( is_array($cc = self::utf8ToUnicode(mb_substr($str, 0, 1, 'utf-8'))) )
  75. $cc = $cc[0];
  76. else
  77. return false;
  78. if($cc>=1536 && $cc<=1791) // arabic, persian, urdu, kurdish, ...
  79. return true;
  80. if($cc>=65136 && $cc<=65279) // arabic peresent 2
  81. return true;
  82. if($cc>=64336 && $cc<=65023) // arabic peresent 1
  83. return true;
  84. if($cc>=1424 && $cc<=1535) // hebrew
  85. return true;
  86. if($cc>=64256 && $cc<=64335) // hebrew peresent
  87. return true;
  88. if($cc>=1792 && $cc<=1871) // Syriac
  89. return true;
  90. if($cc>=1920 && $cc<=1983) // Thaana
  91. return true;
  92. if($cc>=1984 && $cc<=2047) // NKo
  93. return true;
  94. if($cc>=11568 && $cc<=11647) // Tifinagh
  95. return true;
  96. return false;
  97. }
  98. /**
  99. * clears text from replys, tags, groups, reteets & whitespaces
  100. *
  101. * @param string $str
  102. * @return string
  103. */
  104. private static function getClearText($str){
  105. $str = preg_replace('/@[^ ]+|![^ ]+|#[^ ]+/u', '', $str); // reply, tag, group
  106. $str = preg_replace('/^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]/u', '', $str); // redent, retweet
  107. $str = preg_replace("/[ \r\t\n]+/", ' ', trim($str)); // remove spaces
  108. return $str;
  109. }
  110. /**
  111. * adds javascript to do same thing on input textarea
  112. *
  113. * @param Action $action
  114. */
  115. function onEndShowScripts($action){
  116. if (common_logged_in()) {
  117. $action->script($this->path('jquery.DirectionDetector.js'));
  118. }
  119. }
  120. /**
  121. * Takes an UTF-8 string and returns an array of ints representing the
  122. * Unicode characters. Astral planes are supported ie. the ints in the
  123. * output can be > 0xFFFF. O$ccurrances of the BOM are ignored. Surrogates
  124. * are not allowed.
  125. *
  126. * @param string $str
  127. * @return mixed array of ints, or false on invalid input
  128. */
  129. private static function utf8ToUnicode($str){
  130. $mState = 0; // cached expected number of octets after the current octet
  131. // until the beginning of the next UTF8 character sequence
  132. $mUcs4 = 0; // cached Unicode character
  133. $mBytes = 1; // cached expected number of octets in the current sequence
  134. $out = array();
  135. $len = strlen($str);
  136. for($i = 0; $i < $len; $i++) {
  137. $in = ord($str{$i});
  138. if (0 == $mState) {
  139. // When mState is zero we expect either a US-ASCII character or a
  140. // multi-octet sequence.
  141. if (0 == (0x80 & ($in))) {
  142. // US-ASCII, pass straight through.
  143. $out[] = $in;
  144. $mBytes = 1;
  145. } elseif (0xC0 == (0xE0 & ($in))) {
  146. // First octet of 2 octet sequence
  147. $mUcs4 = ($in);
  148. $mUcs4 = ($mUcs4 & 0x1F) << 6;
  149. $mState = 1;
  150. $mBytes = 2;
  151. } elseif (0xE0 == (0xF0 & ($in))) {
  152. // First octet of 3 octet sequence
  153. $mUcs4 = ($in);
  154. $mUcs4 = ($mUcs4 & 0x0F) << 12;
  155. $mState = 2;
  156. $mBytes = 3;
  157. } elseif (0xF0 == (0xF8 & ($in))) {
  158. // First octet of 4 octet sequence
  159. $mUcs4 = ($in);
  160. $mUcs4 = ($mUcs4 & 0x07) << 18;
  161. $mState = 3;
  162. $mBytes = 4;
  163. } elseif (0xF8 == (0xFC & ($in))) {
  164. /* First octet of 5 octet sequence.
  165. *
  166. * This is illegal because the encoded codepoint must be either
  167. * (a) not the shortest form or
  168. * (b) outside the Unicode range of 0-0x10FFFF.
  169. * Rather than trying to resynchronize, we will carry on until the end
  170. * of the sequence and let the later error handling code catch it.
  171. */
  172. $mUcs4 = ($in);
  173. $mUcs4 = ($mUcs4 & 0x03) << 24;
  174. $mState = 4;
  175. $mBytes = 5;
  176. } elseif (0xFC == (0xFE & ($in))) {
  177. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  178. $mUcs4 = ($in);
  179. $mUcs4 = ($mUcs4 & 1) << 30;
  180. $mState = 5;
  181. $mBytes = 6;
  182. } else {
  183. /* Current octet is neither in the US-ASCII range nor a legal first
  184. * octet of a multi-octet sequence.
  185. */
  186. return false;
  187. }
  188. } else {
  189. // When mState is non-zero, we expect a continuation of the multi-octet
  190. // sequence
  191. if (0x80 == (0xC0 & ($in))) {
  192. // Legal continuation.
  193. $shift = ($mState - 1) * 6;
  194. $tmp = $in;
  195. $tmp = ($tmp & 0x0000003F) << $shift;
  196. $mUcs4 |= $tmp;
  197. if (0 == --$mState) {
  198. /* End of the multi-octet sequence. mUcs4 now contains the final
  199. * Unicode codepoint to be output
  200. *
  201. * Check for illegal sequences and codepoints.
  202. */
  203. // From Unicode 3.1, non-shortest form is illegal
  204. if (
  205. ((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
  206. ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
  207. ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
  208. (4 < $mBytes) ||
  209. // From Unicode 3.2, surrogate characters are illegal
  210. (($mUcs4 & 0xFFFFF800) == 0xD800) ||
  211. // Codepoints outside the Unicode range are illegal
  212. ($mUcs4 > 0x10FFFF)
  213. ){
  214. return false;
  215. }
  216. if (0xFEFF != $mUcs4) {
  217. $out[] = $mUcs4;
  218. }
  219. //initialize UTF8 cache
  220. $mState = 0;
  221. $mUcs4 = 0;
  222. $mBytes = 1;
  223. }
  224. } else {
  225. /* ((0xC0 & (*in) != 0x80) && (mState != 0))
  226. *
  227. * Incomplete multi-octet sequence.
  228. */
  229. return false;
  230. }
  231. }
  232. }
  233. return $out;
  234. }
  235. /**
  236. * plugin details
  237. */
  238. function onPluginVersion(array &$versions){
  239. $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/DirectionDetector';
  240. $versions[] = array(
  241. 'name' => 'Direction detector',
  242. 'version' => DIRECTIONDETECTORPLUGIN_VERSION,
  243. 'author' => 'Behrooz Shabani',
  244. 'homepage' => $url,
  245. // TRANS: Plugin description.
  246. 'rawdescription' => _m('Shows notices with right-to-left content in correct direction.')
  247. );
  248. return true;
  249. }
  250. }
  251. /*
  252. // Example:
  253. var_dump(DirectionDetectorPlugin::isRTL('RT @everplays ♺: دادگاه به دليل عدم حضور وکلای متهمان بنا بر اصل ١٣٥ قانون اساسی غير قانونی است')); // true
  254. */