PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_easyblog/helpers/string.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 463 lines | 304 code | 61 blank | 98 comment | 47 complexity | aa28612ba9faa0081595abaec7c86407 MD5 | raw file
  1. <?php
  2. /**
  3. * @package EasyBlog
  4. * @copyright Copyright (C) 2010 Stack Ideas Private Limited. All rights reserved.
  5. * @license GNU/GPL, see LICENSE.php
  6. *
  7. * EasyBlog is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. defined('_JEXEC') or die('Restricted access');
  14. /*
  15. * String utilities class.
  16. *
  17. */
  18. class EasyBlogStringHelper
  19. {
  20. public static function getLangCode()
  21. {
  22. $lang = JFactory::getLanguage();
  23. $locale = $lang->getLocale();
  24. $langCode = null;
  25. if(empty($locale))
  26. {
  27. $langCode = 'en-GB';
  28. }
  29. else
  30. {
  31. $langTag = $locale[0];
  32. $langData = explode('.', $langTag);
  33. $langCode = JString::str_ireplace('_', '-', $langData[0]);
  34. }
  35. return $langCode;
  36. }
  37. public static function getNoun( $var , $count , $includeCount = false )
  38. {
  39. static $zeroIsPlural;
  40. if (!isset($zeroIsPlural))
  41. {
  42. $config = EasyBlogHelper::getConfig();
  43. $zeroIsPlural = $config->get( 'layout_zero_as_plural' );
  44. }
  45. $count = (int) $count;
  46. $var = ($count===1 || $count===-1 || ($count===0 && !$zeroIsPlural)) ? $var . '_SINGULAR' : $var . '_PLURAL';
  47. return ( $includeCount ) ? JText::sprintf( $var , $count ) : JText::_( $var );
  48. }
  49. /*
  50. * Convert string from ejax post into assoc-array
  51. * param - string
  52. * return - assc-array
  53. */
  54. public static function ejaxPostToArray($params)
  55. {
  56. $post = array();
  57. foreach($params as $item)
  58. {
  59. $pair = explode('=', $item);
  60. if( isset( $pair[ 0 ] ) && isset( $pair[ 1 ] ) )
  61. {
  62. $key = $pair[0];
  63. $value = EasyBlogStringHelper::ejaxUrlDecode( $pair[ 1 ] );
  64. if( JString::stristr( $key , '[]' ) !== false )
  65. {
  66. $key = JString::str_ireplace( '[]' , '' , $key );
  67. $post[ $key ][] = $value;
  68. }
  69. else
  70. {
  71. $post[ $key ] = $value;
  72. }
  73. }
  74. }
  75. return $post;
  76. }
  77. /*
  78. * decode the encoded url string
  79. * param - string
  80. * return - string
  81. */
  82. public static function ejaxUrlDecode($string)
  83. {
  84. $rawStr = urldecode( rawurldecode( $string ) );
  85. if( function_exists( 'html_entity_decode' ) )
  86. {
  87. return html_entity_decode($rawStr);
  88. }
  89. else
  90. {
  91. return EasyBlogStringHelper::unhtmlentities($rawStr);
  92. }
  93. }
  94. /**
  95. * A pior php 4.3.0 version of
  96. * html_entity_decode
  97. */
  98. public static function unhtmlentities($string)
  99. {
  100. // replace numeric entities
  101. $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
  102. $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
  103. // replace literal entities
  104. $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  105. $trans_tbl = array_flip($trans_tbl);
  106. return strtr($string, $trans_tbl);
  107. }
  108. public static function linkTweets( $source )
  109. {
  110. // Link hashes
  111. $pattern = '/\#(\w*)/i';
  112. $replace = '<a target="_blank" href="http://twitter.com/#!/search?q=$1" rel="nofollow">$0</a>';
  113. $source = preg_replace( $pattern , $replace , $source );
  114. // Link authors
  115. $pattern = '/\@(\w*)/i';
  116. $replace = '<a target="_blank" href="http://twitter.com/$1" rel="nofollow">$0</a>';
  117. $source = preg_replace( $pattern , $replace , $source );
  118. return $source;
  119. }
  120. public static function url2link( $string )
  121. {
  122. $newString = $string;
  123. preg_match('/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms', $newString, $matches);
  124. $patterns = array('/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms',
  125. "/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i",
  126. "/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i");
  127. $replace = array('[bbcode-url]',
  128. "<a target=\"_blank\" href=\"$1\" rel=\"nofollow\">$1</a>",
  129. "<a target=\"_blank\" href=\"http://$2\" rel=\"nofollow\">$2</a>");
  130. $newString = preg_replace($patterns, $replace, $newString);
  131. //now convert back again.
  132. if(count($matches) > 0)
  133. {
  134. $patterns = array('/\[bbcode\-url\]/ms');
  135. $replace = array($matches[0]);
  136. $newString = preg_replace($patterns, $replace, $newString);
  137. }
  138. return $newString;
  139. }
  140. public static function htmlAnchorLink( $url , $string )
  141. {
  142. if( !$string )
  143. {
  144. return $string;
  145. }
  146. if( JString::strpos( $url , 'http://' ) === false && JString::strpos( $url , 'https://' ) === false )
  147. {
  148. $url = 'http://' . $url;
  149. }
  150. $pattern = "/(((http[s]?:\/\/)|(www\.))(([a-z][-a-z0-9]+\.)?[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2,2})?)\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1})/is";
  151. $newString = preg_replace($pattern, '<a href="$1" target="_blank" rel="nofollow">' . $string. '</a>', $url);
  152. //this is not a link
  153. if( $newString == $url )
  154. {
  155. return $string;
  156. }
  157. return $newString;
  158. }
  159. public static function escape( $var )
  160. {
  161. return htmlspecialchars( $var, ENT_COMPAT, 'UTF-8' );
  162. }
  163. public static function tidyHTMLContent( $content )
  164. {
  165. require_once( JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_easyblog' . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'htmlawed' . DIRECTORY_SEPARATOR . 'htmlawed.php' );
  166. $htmLawedConfig = array( 'cdata' => 1,
  167. 'clean_ms_char' => 1,
  168. 'comment' => 1,
  169. 'safe' => 1,
  170. 'tidy' => 1,
  171. 'valid_xhtml' =>1,
  172. 'deny_attribute' => '* -title -href -target -alt',
  173. 'keep_bad' => 6,
  174. 'anti_link_spam' => array('`.`','')
  175. );
  176. //return htmLawed( $content, $htmLawedConfig);
  177. return htmLawed( $content );
  178. }
  179. // function convert2UTF8( $html )
  180. // {
  181. // $encoding = 'iso-8859-1';
  182. // $encoding = strtoupper( $encoding );
  183. //
  184. // $html = @mb_convert_encoding($html, 'UTF-8', $encoding);
  185. // return $html;
  186. // }
  187. /* reference: http://publicmind.in/blog/url-encoding/ */
  188. public static function encodeURL( $url )
  189. {
  190. $reserved = array(
  191. ":" => '!%3A!ui',
  192. "/" => '!%2F!ui',
  193. "?" => '!%3F!ui',
  194. "#" => '!%23!ui',
  195. "[" => '!%5B!ui',
  196. "]" => '!%5D!ui',
  197. "@" => '!%40!ui',
  198. "!" => '!%21!ui',
  199. "$" => '!%24!ui',
  200. "&" => '!%26!ui',
  201. "'" => '!%27!ui',
  202. "(" => '!%28!ui',
  203. ")" => '!%29!ui',
  204. "*" => '!%2A!ui',
  205. "+" => '!%2B!ui',
  206. "," => '!%2C!ui',
  207. ";" => '!%3B!ui',
  208. "=" => '!%3D!ui',
  209. "%" => '!%25!ui',
  210. );
  211. $url = str_replace(array('%09','%0A','%0B','%0D'),'',$url); // removes nasty whitespace
  212. $url = rawurlencode($url);
  213. $url = preg_replace(array_values($reserved), array_keys($reserved), $url);
  214. return $url;
  215. }
  216. public static function rel2abs($rel, $base)
  217. {
  218. /* return if already absolute URL */
  219. if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
  220. /* queries and anchors */
  221. if (@$rel[0]=='#' || @$rel[0]=='?') return $base.$rel;
  222. /* parse base URL and convert to local variables:
  223. $scheme, $host, $path */
  224. extract(parse_url($base));
  225. /* remove non-directory element from path */
  226. $path = preg_replace('#/[^/]*$#', '', $path);
  227. /* destroy path if relative url points to root */
  228. if ( @$rel[0] == '/') $path = '';
  229. /* dirty absolute URL */
  230. $abs = "$host$path/$rel";
  231. /* replace '//' or '/./' or '/foo/../' with '/' */
  232. $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
  233. for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
  234. /* absolute URL is ready! */
  235. return $scheme.'://'.$abs;
  236. }
  237. /**
  238. * @author "Sebastián Grignoli" <grignoli@framework2.com.ar>
  239. * @package forceUTF8
  240. * @version 1.1
  241. * @link http://www.framework2.com.ar/dzone/forceUTF8-es/
  242. * @example http://www.framework2.com.ar/dzone/forceUTF8-es/
  243. */
  244. public static function forceUTF8($text){
  245. /**
  246. * Function forceUTF8
  247. *
  248. * This function leaves UTF8 characters alone, while converting almost all non-UTF8 to UTF8.
  249. *
  250. * It may fail to convert characters to unicode if they fall into one of these scenarios:
  251. *
  252. * 1) when any of these characters: ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß
  253. * are followed by any of these: ("group B")
  254. * ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶•¸¹º»¼½¾¿
  255. * For example: %ABREPRESENT%C9%BB. «REPRESENTÉ»
  256. * The "«" (%AB) character will be converted, but the "É" followed by "»" (%C9%BB)
  257. * is also a valid unicode character, and will be left unchanged.
  258. *
  259. * 2) when any of these: àáâãäåæçèéêëìíîï are followed by TWO chars from group B,
  260. * 3) when any of these: ðñòó are followed by THREE chars from group B.
  261. *
  262. * @name forceUTF8
  263. * @param string $text Any string.
  264. * @return string The same string, UTF8 encoded
  265. *
  266. */
  267. if(is_array($text))
  268. {
  269. foreach($text as $k => $v)
  270. {
  271. $text[$k] = EasyBlogStringHelper::forceUTF8($v);
  272. }
  273. return $text;
  274. }
  275. $max = strlen($text);
  276. $buf = "";
  277. for($i = 0; $i < $max; $i++){
  278. $c1 = $text{$i};
  279. if($c1>="\xc0"){ //Should be converted to UTF8, if it's not UTF8 already
  280. $c2 = $i+1 >= $max? "\x00" : $text{$i+1};
  281. $c3 = $i+2 >= $max? "\x00" : $text{$i+2};
  282. $c4 = $i+3 >= $max? "\x00" : $text{$i+3};
  283. if($c1 >= "\xc0" & $c1 <= "\xdf"){ //looks like 2 bytes UTF8
  284. if($c2 >= "\x80" && $c2 <= "\xbf"){ //yeah, almost sure it's UTF8 already
  285. $buf .= $c1 . $c2;
  286. $i++;
  287. } else { //not valid UTF8. Convert it.
  288. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  289. $cc2 = ($c1 & "\x3f") | "\x80";
  290. $buf .= $cc1 . $cc2;
  291. }
  292. } elseif($c1 >= "\xe0" & $c1 <= "\xef"){ //looks like 3 bytes UTF8
  293. if($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf"){ //yeah, almost sure it's UTF8 already
  294. $buf .= $c1 . $c2 . $c3;
  295. $i = $i + 2;
  296. } else { //not valid UTF8. Convert it.
  297. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  298. $cc2 = ($c1 & "\x3f") | "\x80";
  299. $buf .= $cc1 . $cc2;
  300. }
  301. } elseif($c1 >= "\xf0" & $c1 <= "\xf7"){ //looks like 4 bytes UTF8
  302. if($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf" && $c4 >= "\x80" && $c4 <= "\xbf"){ //yeah, almost sure it's UTF8 already
  303. $buf .= $c1 . $c2 . $c3;
  304. $i = $i + 2;
  305. } else { //not valid UTF8. Convert it.
  306. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  307. $cc2 = ($c1 & "\x3f") | "\x80";
  308. $buf .= $cc1 . $cc2;
  309. }
  310. } else { //doesn't look like UTF8, but should be converted
  311. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  312. $cc2 = (($c1 & "\x3f") | "\x80");
  313. $buf .= $cc1 . $cc2;
  314. }
  315. } elseif(($c1 & "\xc0") == "\x80"){ // needs conversion
  316. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  317. $cc2 = (($c1 & "\x3f") | "\x80");
  318. $buf .= $cc1 . $cc2;
  319. } else { // it doesn't need convesion
  320. $buf .= $c1;
  321. }
  322. }
  323. return $buf;
  324. }
  325. public static function forceLatin1($text) {
  326. if(is_array($text)) {
  327. foreach($text as $k => $v) {
  328. $text[$k] = EasyBlogStringHelper::forceLatin1($v);
  329. }
  330. return $text;
  331. }
  332. return utf8_decode( EasyBlogStringHelper::forceUTF8($text) );
  333. }
  334. public static function fixUTF8($text){
  335. if(is_array($text)) {
  336. foreach($text as $k => $v) {
  337. $text[$k] = EasyBlogStringHelper::fixUTF8($v);
  338. }
  339. return $text;
  340. }
  341. $last = "";
  342. while($last <> $text){
  343. $last = $text;
  344. $text = EasyBlogStringHelper::forceUTF8( utf8_decode( EasyBlogStringHelper::forceUTF8($text) ) );
  345. }
  346. return $text;
  347. }
  348. /**
  349. * Returns an array of blocked words.
  350. *
  351. * @access public
  352. * @param null
  353. * @return array
  354. */
  355. public function getBlockedWords()
  356. {
  357. static $words = null;
  358. if( is_null( $words ) )
  359. {
  360. $config = EasyBlogHelper::getConfig();
  361. $words = trim( $config->get( 'main_blocked_words' ) , ',');
  362. if( !empty( $words ) )
  363. {
  364. $words = explode( ',' , $words );
  365. }
  366. else
  367. {
  368. $words = array();
  369. }
  370. }
  371. return $words;
  372. }
  373. /**
  374. * Determines if the text provided contains any blocked words
  375. *
  376. * @access public
  377. * @param string $text The text to lookup for
  378. * @return boolean True if contains blocked words, false otherwise.
  379. *
  380. */
  381. public function hasBlockedWords( $text )
  382. {
  383. $words = self::getBlockedWords();
  384. if( empty( $words ) || !$words )
  385. {
  386. return false;
  387. }
  388. foreach( $words as $word )
  389. {
  390. if( preg_match('/\b'.$word.'\b/i', $text) )
  391. {
  392. // Immediately exit the method since we now know that there's at least
  393. // 1 blocked word.
  394. return $word;
  395. }
  396. }
  397. return false;
  398. }
  399. }