You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

375 lines
9.6 KiB

12 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
10 years ago
  1. <?php
  2. use HeyUpdate\Emoji\Emoji;
  3. use HeyUpdate\Emoji\EmojiIndex;
  4. /**
  5. * @desc A singleton wrapper for the Emoji library
  6. */
  7. class MovimEmoji
  8. {
  9. protected static $instance = null;
  10. private $_emoji;
  11. private $_theme;
  12. protected function __construct()
  13. {
  14. $cd = new \Modl\ConfigDAO();
  15. $config = $cd->get();
  16. $this->_theme = $config->theme;
  17. $this->_emoji = new Emoji(new EmojiIndex(), $this->getPath());
  18. }
  19. public function replace($string, $large = false)
  20. {
  21. $this->_emoji->setAssetUrlFormat($this->getPath());
  22. $string = $this->_emoji->replaceEmojiWithImages($string);
  23. $this->_emoji->setAssetUrlFormat($this->getPath());
  24. return $string;
  25. }
  26. private function getPath($large = false)
  27. {
  28. return BASE_URI . 'themes/' . $this->_theme . '/img/emojis/svg/%s.svg';
  29. }
  30. public static function getInstance()
  31. {
  32. if (!isset(static::$instance)) {
  33. static::$instance = new MovimEmoji;
  34. }
  35. return static::$instance;
  36. }
  37. }
  38. function addUrls($string, $preview = false) {
  39. // Add missing links
  40. return preg_replace_callback(
  41. "/([\w\"'>]+\:\/\/[\w-?'&;#+,%:~=\.\/\@]+)/u", function ($match) use($preview) {
  42. if(!in_array(substr($match[0], 0, 1), array('>', '"', '\''))) {
  43. $content = $match[0];
  44. if($preview) {
  45. try {
  46. $embed = Embed\Embed::create($match[0]);
  47. if($embed->type == 'photo'
  48. && $embed->images[0]['width'] <= 1024
  49. && $embed->images[0]['height'] <= 1024) {
  50. $content = '<img src="'.$match[0].'"/>';
  51. } elseif($embed->type == 'link') {
  52. $content .= ' - '. $embed->title . ' - ' . $embed->providerName;
  53. }
  54. } catch(Exception $e) {
  55. error_log($e->getMessage());
  56. }
  57. }
  58. return stripslashes('<a href=\"'.$match[0].'\" target=\"_blank\">'.$content.'</a>');
  59. } else {
  60. return $match[0];
  61. }
  62. }, $string
  63. );
  64. }
  65. function addHFR($string) {
  66. // HFR EasterEgg
  67. return preg_replace_callback(
  68. '/\[:([\w\s-]+)([:\d])*\]/', function ($match) {
  69. $num = '';
  70. if(count($match) == 3)
  71. $num = $match[2].'/';
  72. return '<img class="hfr" title="'.$match[0].'" alt="'.$match[0].'" src="http://forum-images.hardware.fr/images/perso/'.$num.$match[1].'.gif">';
  73. }, $string
  74. );
  75. }
  76. /**
  77. * @desc Prepare the string (add the a to the links and show the smileys)
  78. *
  79. * @param string $string
  80. * @param boolean display large emojis
  81. * @param check the links and convert them to pictures (heavy)
  82. * @return string
  83. */
  84. function prepareString($string, $large = false, $preview = false) {
  85. $string = addUrls($string, $preview);
  86. // We remove all the style attributes
  87. $string = preg_replace_callback(
  88. '/(<[^>]+) style=".*?"/i', function($match) {
  89. return $match[1];
  90. }, $string
  91. );
  92. // Twitter hashtags
  93. $string = preg_replace_callback(
  94. "/ #[a-zA-Z0-9_-]{3,}/", function ($match) { return ' <a class="twitter hastag" href="http://twitter.com/search?q='. urlencode(trim($match[0])). '&src=hash" target="_blank">'. trim($match[0]). '</a>'; }, ' ' . $string);
  95. $string = preg_replace_callback(
  96. "/ @[a-zA-Z0-9_-]{3,}/", function ($match) {
  97. return
  98. ' <a class="twitter at" href="http://twitter.com/'.
  99. trim($match[0]).
  100. '" target="_blank">'.
  101. trim($match[0]).
  102. '</a>';
  103. }, ' ' . $string
  104. );
  105. //remove all scripts
  106. $string = preg_replace_callback(
  107. '#<[/]?script[^>]*>#is', function ($match) {
  108. return '';
  109. }, ' ' . $string
  110. );
  111. //remove all iframe
  112. $string = preg_replace_callback(
  113. '#<[/]?iframe[^>]*>#is', function ($match) {
  114. return '';
  115. }, ' ' . $string
  116. );
  117. //remove all iframe
  118. $string = preg_replace_callback(
  119. '#<[/]?ss[^>]*>#is', function ($match) {
  120. return '';
  121. }, ' ' . $string
  122. );
  123. // We add some smileys...
  124. $emoji = MovimEmoji::getInstance();
  125. $string = $emoji->replace($string, $large);
  126. return trim($string);
  127. }
  128. /**
  129. * Fix self-closing tags
  130. */
  131. function fixSelfClosing($string) {
  132. return preg_replace_callback('/<([^\s<]+)\/>/',
  133. function($match) {
  134. return '<'.$match[1].'></'.$match[1].'>';
  135. }
  136. , $string);
  137. }
  138. /**
  139. * Remove the content, body and html tags
  140. */
  141. function cleanHTMLTags($string) {
  142. return str_replace(
  143. array(
  144. '<content type="html">',
  145. '<html xmlns="http://jabber.org/protocol/xhtml-im">',
  146. '<body xmlns="http://www.w3.org/1999/xhtml">',
  147. '</body>',
  148. '</html>',
  149. '</content>'),
  150. '',
  151. $string);
  152. }
  153. /**
  154. * Return an array of informations from a XMPP uri
  155. */
  156. function explodeURI($uri) {
  157. $arr = parse_url(urldecode($uri));
  158. $result = array();
  159. if(isset($arr['query'])) {
  160. $query = explode(';', $arr['query']);
  161. foreach($query as $elt) {
  162. if($elt != '') {
  163. list($key, $val) = explode('=', $elt);
  164. $result[$key] = $val;
  165. }
  166. }
  167. $arr = array_merge($arr, $result);
  168. }
  169. return $arr;
  170. }
  171. /*
  172. * Echap the JID
  173. */
  174. function echapJid($jid)
  175. {
  176. return str_replace(' ', '\40', $jid);
  177. }
  178. /*
  179. * Echap the anti-slashs for Javascript
  180. */
  181. function echapJS($string)
  182. {
  183. return str_replace("\\", "\\\\", $string);
  184. }
  185. /*
  186. * Clean the resource of a jid
  187. */
  188. function cleanJid($jid)
  189. {
  190. $explode = explode('/', $jid);
  191. return reset($explode);
  192. }
  193. /*
  194. * Explode JID
  195. */
  196. function explodeJid($jid)
  197. {
  198. $arr = explode('/', $jid);
  199. $jid = $arr[0];
  200. if(isset($arr[1])) $resource = $arr[1];
  201. else $resource = null;
  202. list($username, $server) = explode('@', $jid);
  203. return array(
  204. 'username' => $username,
  205. 'server' => $server,
  206. 'resource' => $resource
  207. );
  208. }
  209. /**
  210. * Return a URIfied string
  211. * @param string
  212. * @return string
  213. */
  214. function stringToUri($url) {
  215. $url = utf8_decode($url);
  216. $url = strtolower(strtr($url, utf8_decode('ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ()[]\'"~$&%*@ç!?;,:/\^¨€{}<>|+- .'), 'aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn -- c --- e --_'));
  217. $url = str_replace(' ', '', $url);
  218. $url = str_replace('---', '-', $url);
  219. $url = str_replace('--', '-', $url);
  220. $url = trim($url,'-');
  221. return $url;
  222. }
  223. /**
  224. * Return a human readable filesize
  225. * @param string size in bytes
  226. * @return string
  227. */
  228. function sizeToCleanSize($size)
  229. {
  230. $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  231. $power = $size > 0 ? floor(log($size, 1024)) : 0;
  232. return number_format($size / pow(1024, $power), 7, '.', ',') . ' ' . $units[$power];
  233. }
  234. /**
  235. * Return a colored string in the console
  236. * @param string
  237. * @param color
  238. * @return string
  239. */
  240. function colorize($string, $color) {
  241. $colors = array(
  242. 'black' => 30,
  243. 'red' => 31,
  244. 'green' => 32,
  245. 'yellow' => 33,
  246. 'blue' => 34,
  247. 'purple' => 35,
  248. 'turquoise' => 36,
  249. 'white' => 37
  250. );
  251. return "\033[".$colors[$color]."m".$string."\033[0m";
  252. }
  253. /**
  254. * Return a color generated from the string
  255. * @param string
  256. * @return string
  257. */
  258. function stringToColor($string) {
  259. $colors = array(
  260. 0 => 'red',
  261. 1 => 'purple',
  262. 2 => 'indigo',
  263. 3 => 'blue',
  264. 4 => 'green',
  265. 5 => 'orange',
  266. 6 => 'yellow',
  267. 7 => 'brown');
  268. $s = substr(base_convert(sha1($string), 15, 10), 0, 10);
  269. if($colors[$s%8]) {
  270. return $colors[$s%8];
  271. } else {
  272. return 'orange';
  273. }
  274. }
  275. /**
  276. * Strip tags and add a whitespace
  277. * @param string
  278. * @return string
  279. */
  280. function stripTags($string)
  281. {
  282. return strip_tags(preg_replace('/(<\/[^>]+?>)(<[^>\/][^>]*?>)/', '$1 $2', $string));
  283. }
  284. /**
  285. * Purify a string
  286. * @param string
  287. * @return string
  288. */
  289. function purifyHTML($string)
  290. {
  291. $config = \HTMLPurifier_Config::createDefault();
  292. $config->set('HTML.Doctype', 'HTML 4.01 Transitional');
  293. $config->set('Cache.SerializerPath', '/tmp');
  294. $config->set('HTML.DefinitionID', 'html5-definitions');
  295. $config->set('HTML.DefinitionRev', 1);
  296. if ($def = $config->maybeGetRawHTMLDefinition()) {
  297. $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', array(
  298. 'src' => 'URI',
  299. 'type' => 'Text',
  300. 'width' => 'Length',
  301. 'height' => 'Length',
  302. 'poster' => 'URI',
  303. 'preload' => 'Enum#auto,metadata,none',
  304. 'controls' => 'Bool',
  305. ));
  306. $def->addElement('audio', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', array(
  307. 'src' => 'URI',
  308. 'preload' => 'Enum#auto,metadata,none',
  309. 'muted' => 'Bool',
  310. 'controls' => 'Bool',
  311. ));
  312. $def->addElement('source', 'Block', 'Flow', 'Common', array(
  313. 'src' => 'URI',
  314. 'type' => 'Text',
  315. ));
  316. }
  317. $purifier = new \HTMLPurifier($config);
  318. return $purifier->purify($string);
  319. }
  320. /**
  321. * Return the first two letters of a string
  322. * @param string
  323. * @return string
  324. */
  325. function firstLetterCapitalize($string) {
  326. return ucfirst(strtolower(mb_substr($string, 0, 2)));
  327. }