Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

1289 lines
46 KiB

9 years ago
10 years ago
10 years ago
6 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
  1. <?php
  2. require_once("include/oembed.php");
  3. require_once('include/event.php');
  4. require_once('include/map.php');
  5. require_once('mod/proxy.php');
  6. function bb_PictureCacheExt($matches) {
  7. if (strpos($matches[3], "data:image/") === 0)
  8. return ($matches[0]);
  9. $matches[3] = proxy_url($matches[3]);
  10. return "[img=".$matches[1]."x".$matches[2]."]".$matches[3]."[/img]";
  11. }
  12. function bb_PictureCache($matches) {
  13. if (strpos($matches[1], "data:image/") === 0)
  14. return ($matches[0]);
  15. $matches[1] = proxy_url($matches[1]);
  16. return "[img]".$matches[1]."[/img]";
  17. }
  18. function bb_map_coords($match) {
  19. // the extra space in the following line is intentional
  20. return str_replace($match[0],'<div class="map" >' . generate_map(str_replace('/',' ',$match[1])) . '</div>', $match[0]);
  21. }
  22. function bb_map_location($match) {
  23. // the extra space in the following line is intentional
  24. return str_replace($match[0],'<div class="map" >' . generate_named_map($match[1]) . '</div>', $match[0]);
  25. }
  26. function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
  27. $Text = preg_replace_callback("/(.*?)\[attachment(.*?)\](.*?)\[\/attachment\]/ism",
  28. function ($match) use ($simplehtml, $tryoembed){
  29. $attributes = $match[2];
  30. $type = "";
  31. preg_match("/type='(.*?)'/ism", $attributes, $matches);
  32. if ($matches[1] != "")
  33. $type = strtolower($matches[1]);
  34. preg_match('/type="(.*?)"/ism', $attributes, $matches);
  35. if ($matches[1] != "")
  36. $type = strtolower($matches[1]);
  37. if ($type == "")
  38. return($match[0]);
  39. if (!in_array($type, array("link", "audio", "video")))
  40. return($match[0]);
  41. $url = "";
  42. preg_match("/url='(.*?)'/ism", $attributes, $matches);
  43. if ($matches[1] != "")
  44. $url = $matches[1];
  45. preg_match('/url="(.*?)"/ism', $attributes, $matches);
  46. if ($matches[1] != "")
  47. $url = $matches[1];
  48. $title = "";
  49. preg_match("/title='(.*?)'/ism", $attributes, $matches);
  50. if ($matches[1] != "")
  51. $title = $matches[1];
  52. preg_match('/title="(.*?)"/ism', $attributes, $matches);
  53. if ($matches[1] != "")
  54. $title = $matches[1];
  55. //$title = htmlentities($title, ENT_QUOTES, 'UTF-8', false);
  56. $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
  57. $title = str_replace(array("[", "]"), array("&#91;", "&#93;"), $title);
  58. $image = "";
  59. if ($type != "video") {
  60. preg_match("/image='(.*?)'/ism", $attributes, $matches);
  61. if ($matches[1] != "")
  62. $image = $matches[1];
  63. preg_match('/image="(.*?)"/ism', $attributes, $matches);
  64. if ($matches[1] != "")
  65. $image = $matches[1];
  66. }
  67. $preview = "";
  68. if ($type != "video") {
  69. preg_match("/preview='(.*?)'/ism", $attributes, $matches);
  70. if ($matches[1] != "")
  71. $preview = $matches[1];
  72. preg_match('/preview="(.*?)"/ism', $attributes, $matches);
  73. if ($matches[1] != "")
  74. $preview = $matches[1];
  75. }
  76. if (((strpos($match[1], "[img=") !== false) OR (strpos($match[1], "[img]") !== false)) AND ($image != "")) {
  77. $preview = $image;
  78. $image = "";
  79. }
  80. if ($simplehtml == 7) {
  81. $title2 = $title;
  82. $test1 = trim(html_entity_decode($match[1],ENT_QUOTES,'UTF-8'));
  83. $test2 = trim(html_entity_decode($title,ENT_QUOTES,'UTF-8'));
  84. // If the link description is similar to the text above then don't add the link description
  85. if (($title != "") AND ((strpos($test1,$test2) !== false) OR
  86. (similar_text($test1,$test2) / strlen($title)) > 0.9))
  87. $title2 = $url;
  88. $text = sprintf('<a href="%s" title="%s" class="attachment thumbnail" rel="nofollow external">%s</a><br />',
  89. $url, $title, $title2);
  90. } elseif (($simplehtml != 4) AND ($simplehtml != 0))
  91. $text = sprintf('<a href="%s" target="_blank">%s</a><br>', $url, $title);
  92. else {
  93. $text = sprintf('<span class="type-%s">', $type);
  94. $bookmark = array(sprintf('[bookmark=%s]%s[/bookmark]', $url, $title), $url, $title);
  95. if ($tryoembed)
  96. $oembed = tryoembed($bookmark);
  97. else
  98. $oembed = $bookmark[0];
  99. if (strstr(strtolower($oembed), "<iframe "))
  100. $text = $oembed;
  101. else {
  102. if (($image != "") AND !strstr(strtolower($oembed), "<img "))
  103. $text .= sprintf('<a href="%s" target="_blank"><img src="%s" alt="" title="%s" class="attachment-image" /></a><br />', $url, proxy_url($image), $title);
  104. elseif (($preview != "") AND !strstr(strtolower($oembed), "<img "))
  105. $text .= sprintf('<a href="%s" target="_blank"><img src="%s" alt="" title="%s" class="attachment-preview" /></a><br />', $url, proxy_url($preview), $title);
  106. $text .= $oembed;
  107. $text .= sprintf('<blockquote>%s</blockquote></span>', trim($match[3]));
  108. }
  109. }
  110. return($match[1].$text);
  111. },$Text);
  112. return($Text);
  113. }
  114. function bb_rearrange_share($shared) {
  115. if (!in_array(strtolower($shared[2]), array("type-link", "type-audio", "type-video")))
  116. return($shared[0]);
  117. if (!preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$shared[3], $bookmark))
  118. return($shared[0]);
  119. $type = substr(trim(strtolower($shared[2])), 5);
  120. $title = "";
  121. $url = "";
  122. $preview = "";
  123. $description = "";
  124. if (isset($bookmark[2][0]))
  125. $title = $bookmark[2][0];
  126. if (isset($bookmark[1][0]))
  127. $url = $bookmark[1][0];
  128. $cleanedshare = trim($shared[3]);
  129. $cleanedshare = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $cleanedshare);
  130. preg_match("/\[img\](.*?)\[\/img\]/ism", $cleanedshare, $matches);
  131. if ($matches)
  132. $preview = trim($matches[1]);
  133. preg_match("/\[quote\](.*?)\[\/quote\]/ism", $cleanedshare, $matches);
  134. if ($matches)
  135. $description = trim($matches[1]);
  136. $url = str_replace(array("[", "]"), array("&#91;", "&#93;"), htmlentities($url, ENT_QUOTES, 'UTF-8', false));
  137. $title = str_replace(array("[", "]"), array("&#91;", "&#93;"), htmlentities($title, ENT_QUOTES, 'UTF-8', false));
  138. $preview = str_replace(array("[", "]"), array("&#91;", "&#93;"), htmlentities($preview, ENT_QUOTES, 'UTF-8', false));
  139. $Text = trim($shared[1])."\n[attachment type='".$type."'";
  140. if ($url != "")
  141. $Text .= " url='".$url."'";
  142. if ($title != "")
  143. $Text .= " title='".$title."'";
  144. if ($preview != "") {
  145. require_once("include/Photo.php");
  146. $picturedata = get_photo_info($preview);
  147. if (count($picturedata) > 0) {
  148. // if the preview picture is larger than 500 pixels then show it in a larger mode
  149. // But only, if the picture isn't higher than large (To prevent huge posts)
  150. if (($picturedata[0] >= 500) AND ($picturedata[0] >= $picturedata[1]))
  151. $Text .= " image='".$preview."'";
  152. else
  153. $Text .= " preview='".$preview."'";
  154. } else
  155. $Text .= " preview='".$preview."'";
  156. }
  157. $Text .= "]".$description."[/attachment]";
  158. return($Text);
  159. }
  160. function bb_remove_share_information($Text, $plaintext = false, $nolink = false) {
  161. $Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism",
  162. function ($match) use ($plaintext, $nolink){
  163. return(bb_cleanup_share($match, $plaintext, $nolink));
  164. },$Text);
  165. return($Text);
  166. }
  167. function bb_cleanup_share($shared, $plaintext, $nolink) {
  168. $shared[1] = trim($shared[1]);
  169. if (!in_array($shared[2], array("type-link", "type-video")))
  170. return($shared[0]);
  171. if ($plaintext)
  172. $shared[3] = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism","[bookmark=$1]$1[/bookmark]", $shared[3]);
  173. if (!preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$shared[3], $bookmark))
  174. return($shared[0]);
  175. if ($nolink)
  176. return($shared[1]);
  177. $title = "";
  178. $link = "";
  179. if (isset($bookmark[2][0]))
  180. $title = $bookmark[2][0];
  181. if (isset($bookmark[1][0]))
  182. $link = $bookmark[1][0];
  183. if (($shared[1] != "") AND (strpos($title, $shared[1]) !== false))
  184. $shared[1] = $title;
  185. if (($title != "") AND ((strpos($shared[1],$title) !== false) OR
  186. (similar_text($shared[1],$title) / strlen($title)) > 0.9))
  187. $title = "";
  188. // if (strpos($shared[1],$link) !== false)
  189. // $link = "";
  190. $text = trim($shared[1]);
  191. if (($text == "") AND ($title != "") AND ($link == ""))
  192. $text .= "\n\n".trim($title);
  193. // If the link already is included in the post, don't add it again
  194. if (($link != "") AND strpos($text, $link))
  195. return(trim($text));
  196. if (($link != "") AND ($title != ""))
  197. $text .= "\n[url=".trim($link)."]".trim($title)."[/url]";
  198. elseif (($link != ""))
  199. $text .= "\n".trim($link);
  200. return(trim($text));
  201. }
  202. function bb_cleanstyle($st) {
  203. return "<span style=\"".cleancss($st[1]).";\">".$st[2]."</span>";
  204. }
  205. function bb_cleanclass($st) {
  206. return "<span class=\"".cleancss($st[1])."\">".$st[2]."</span>";
  207. }
  208. function cleancss($input) {
  209. $cleaned = "";
  210. $input = strtolower($input);
  211. for ($i = 0; $i < strlen($input); $i++) {
  212. $char = substr($input, $i, 1);
  213. if (($char >= "a") and ($char <= "z"))
  214. $cleaned .= $char;
  215. if (!(strpos(" #;:0123456789-_", $char) === false))
  216. $cleaned .= $char;
  217. }
  218. return($cleaned);
  219. }
  220. function stripcode_br_cb($s) {
  221. return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
  222. }
  223. function bb_onelinecode_cb($match) {
  224. if (strpos($match[1],"<br>")===false){
  225. return "<key>".$match[1]."</key>";
  226. }
  227. return "<code>".$match[1]."</code>";
  228. }
  229. function tryoembed($match){
  230. //$url = ((count($match)==2)?$match[1]:$match[2]);
  231. $url = $match[1];
  232. // Always embed the SSL version
  233. $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
  234. array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);
  235. //logger("tryoembed: $url");
  236. $o = oembed_fetch_url($url);
  237. if (isset($match[2]))
  238. $o->title = $match[2];
  239. if ($o->type=="error") return $match[0];
  240. $html = oembed_format_object($o);
  241. return $html; //oembed_iframe($html,$o->width,$o->height);
  242. }
  243. // [noparse][i]italic[/i][/noparse] turns into
  244. // [noparse][ i ]italic[ /i ][/noparse],
  245. // to hide them from parser.
  246. function bb_spacefy($st) {
  247. $whole_match = $st[0];
  248. $captured = $st[1];
  249. $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
  250. $new_str = str_replace($captured, $spacefied, $whole_match);
  251. return $new_str;
  252. }
  253. // The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
  254. // now turns back and the [noparse] tags are trimed
  255. // returning [i]italic[/i]
  256. function bb_unspacefy_and_trim($st) {
  257. $whole_match = $st[0];
  258. $captured = $st[1];
  259. $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
  260. return $unspacefied;
  261. }
  262. function bb_find_open_close($s, $open, $close, $occurance = 1) {
  263. if($occurance < 1)
  264. $occurance = 1;
  265. $start_pos = -1;
  266. for($i = 1; $i <= $occurance; $i++) {
  267. if( $start_pos !== false)
  268. $start_pos = strpos($s, $open, $start_pos + 1);
  269. }
  270. if( $start_pos === false)
  271. return false;
  272. $end_pos = strpos($s, $close, $start_pos);
  273. if( $end_pos === false)
  274. return false;
  275. $res = array( 'start' => $start_pos, 'end' => $end_pos );
  276. return $res;
  277. }
  278. function get_bb_tag_pos($s, $name, $occurance = 1) {
  279. if($occurance < 1)
  280. $occurance = 1;
  281. $start_open = -1;
  282. for($i = 1; $i <= $occurance; $i++) {
  283. if( $start_open !== false)
  284. $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
  285. }
  286. if( $start_open === false)
  287. return false;
  288. $start_equal = strpos($s, '=', $start_open);
  289. $start_close = strpos($s, ']', $start_open);
  290. if( $start_close === false)
  291. return false;
  292. $start_close++;
  293. $end_open = strpos($s, '[/' . $name . ']', $start_close);
  294. if( $end_open === false)
  295. return false;
  296. $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
  297. 'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
  298. if( $start_equal !== false)
  299. $res['start']['equal'] = $start_equal + 1;
  300. return $res;
  301. }
  302. function bb_tag_preg_replace($pattern, $replace, $name, $s) {
  303. $string = $s;
  304. $occurance = 1;
  305. $pos = get_bb_tag_pos($string, $name, $occurance);
  306. while($pos !== false && $occurance < 1000) {
  307. $start = substr($string, 0, $pos['start']['open']);
  308. $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
  309. $end = substr($string, $pos['end']['close']);
  310. if($end === false)
  311. $end = '';
  312. $subject = preg_replace($pattern, $replace, $subject);
  313. $string = $start . $subject . $end;
  314. $occurance++;
  315. $pos = get_bb_tag_pos($string, $name, $occurance);
  316. }
  317. return $string;
  318. }
  319. if(! function_exists('bb_extract_images')) {
  320. function bb_extract_images($body) {
  321. $saved_image = array();
  322. $orig_body = $body;
  323. $new_body = '';
  324. $cnt = 0;
  325. $img_start = strpos($orig_body, '[img');
  326. $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
  327. $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
  328. while(($img_st_close !== false) && ($img_end !== false)) {
  329. $img_st_close++; // make it point to AFTER the closing bracket
  330. $img_end += $img_start;
  331. if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
  332. // This is an embedded image
  333. $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
  334. $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
  335. $cnt++;
  336. }
  337. else
  338. $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
  339. $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
  340. if($orig_body === false) // in case the body ends on a closing image tag
  341. $orig_body = '';
  342. $img_start = strpos($orig_body, '[img');
  343. $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
  344. $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
  345. }
  346. $new_body = $new_body . $orig_body;
  347. return array('body' => $new_body, 'images' => $saved_image);
  348. }}
  349. if(! function_exists('bb_replace_images')) {
  350. function bb_replace_images($body, $images) {
  351. $newbody = $body;
  352. $cnt = 0;
  353. foreach($images as $image) {
  354. // We're depending on the property of 'foreach' (specified on the PHP website) that
  355. // it loops over the array starting from the first element and going sequentially
  356. // to the last element
  357. $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '<img src="' . proxy_url($image) .'" alt="' . t('Image/photo') . '" />', $newbody);
  358. $cnt++;
  359. }
  360. return $newbody;
  361. }}
  362. function bb_ShareAttributes($share, $simplehtml) {
  363. $attributes = $share[2];
  364. $author = "";
  365. preg_match("/author='(.*?)'/ism", $attributes, $matches);
  366. if ($matches[1] != "")
  367. $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
  368. preg_match('/author="(.*?)"/ism', $attributes, $matches);
  369. if ($matches[1] != "")
  370. $author = $matches[1];
  371. $profile = "";
  372. preg_match("/profile='(.*?)'/ism", $attributes, $matches);
  373. if ($matches[1] != "")
  374. $profile = $matches[1];
  375. preg_match('/profile="(.*?)"/ism', $attributes, $matches);
  376. if ($matches[1] != "")
  377. $profile = $matches[1];
  378. $avatar = "";
  379. preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
  380. if ($matches[1] != "")
  381. $avatar = $matches[1];
  382. preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
  383. if ($matches[1] != "")
  384. $avatar = $matches[1];
  385. $link = "";
  386. preg_match("/link='(.*?)'/ism", $attributes, $matches);
  387. if ($matches[1] != "")
  388. $link = $matches[1];
  389. preg_match('/link="(.*?)"/ism', $attributes, $matches);
  390. if ($matches[1] != "")
  391. $link = $matches[1];
  392. $posted = "";
  393. $itemcache = get_itemcachepath();
  394. // relative dates only make sense when they aren't cached
  395. if ($itemcache == "") {
  396. preg_match("/posted='(.*?)'/ism", $attributes, $matches);
  397. if ($matches[1] != "")
  398. $posted = $matches[1];
  399. preg_match('/posted="(.*?)"/ism', $attributes, $matches);
  400. if ($matches[1] != "")
  401. $posted = $matches[1];
  402. $reldate = (($posted) ? " " . relative_date($posted) : '');
  403. }
  404. $userid = GetProfileUsername($profile,$author, false);
  405. $userid_compact = GetProfileUsername($profile,$author, true);
  406. $preshare = trim($share[1]);
  407. if ($preshare != "")
  408. $preshare .= "<br /><br />";
  409. switch ($simplehtml) {
  410. case 1:
  411. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />»".$share[3]."«";
  412. break;
  413. case 2:
  414. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
  415. break;
  416. case 3: // Diaspora
  417. $headline .= '<b>'.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').$userid.':</b><br />';
  418. $text = trim($share[1]);
  419. if ($text != "")
  420. $text .= "<hr />";
  421. if (substr(normalise_link($link), 0, 19) != "http://twitter.com/") {
  422. $text .= $headline.'<blockquote>'.trim($share[3])."</blockquote><br />";
  423. if ($link != "")
  424. $text .= '<br /><a href="'.$link.'">[l]</a>';
  425. } else
  426. $text .= '<br /><a href="'.$link.'">'.$link.'</a>';
  427. break;
  428. case 4:
  429. $headline = '<div class="shared_header">';
  430. $headline .= '<span><b>'.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
  431. $headline .= sprintf(t('<a href="%1$s" target="_blank">%2$s</a> %3$s'), $link, $userid, $posted);
  432. $headline .= ":</b></span></div>";
  433. $text = trim($share[1]);
  434. if ($text != "")
  435. $text .= "<hr />";
  436. $text .= $headline.'<blockquote class="shared_content">'.trim($share[3])."</blockquote><br />";
  437. break;
  438. case 5:
  439. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
  440. break;
  441. case 6: // app.net
  442. $text = $preshare."&gt;&gt; @".$userid_compact.": <br />".$share[3];
  443. break;
  444. case 7: // statusnet/GNU Social
  445. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8')." @".$userid_compact.": ".$share[3];
  446. break;
  447. case 8: // twitter
  448. $text = $preshare."RT @".$userid_compact.": ".$share[3];
  449. break;
  450. case 9: // Google+/Facebook
  451. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
  452. if ($link != "")
  453. $text .= "<br /><br />".$link;
  454. break;
  455. default:
  456. $headline = trim($share[1]).'<div class="shared_header">';
  457. if ($avatar != "")
  458. $headline .= '<img src="'.proxy_url($avatar, false, PROXY_SIZE_MICRO).'" height="32" width="32" >';
  459. $headline .= sprintf(t('<span><a href="%s" target="_blank">%s</a> wrote the following <a href="%s" target="_blank">post</a>'.$reldate.':</span>'), $profile, $author, $link);
  460. $headline .= "</div>";
  461. $text = $headline.'<blockquote class="shared_content">'.trim($share[3])."</blockquote>";
  462. break;
  463. }
  464. return($text);
  465. }
  466. function GetProfileUsername($profile, $username, $compact = false, $getnetwork = false) {
  467. $twitter = preg_replace("=https?://twitter.com/(.*)=ism", "$1@twitter.com", $profile);
  468. if ($twitter != $profile) {
  469. if ($getnetwork)
  470. return(NETWORK_TWITTER);
  471. elseif ($compact)
  472. return($twitter);
  473. else
  474. return($username." (".$twitter.")");
  475. }
  476. $appnet = preg_replace("=https?://alpha.app.net/(.*)=ism", "$1@alpha.app.net", $profile);
  477. if ($appnet != $profile) {
  478. if ($getnetwork)
  479. return(NETWORK_APPNET);
  480. elseif ($compact)
  481. return($appnet);
  482. else
  483. return($username." (".$appnet.")");
  484. }
  485. $gplus = preg_replace("=https?://plus.google.com/(.*)=ism", "$1@plus.google.com", $profile);
  486. if ($gplus != $profile) {
  487. if ($getnetwork)
  488. return(NETWORK_GPLUS);
  489. elseif ($compact)
  490. return($gplususername." (".$username.")");
  491. else
  492. return($username." (".$gplus.")");
  493. }
  494. $friendica = preg_replace("=https?://(.*)/profile/(.*)=ism", "$2@$1", $profile);
  495. if ($friendica != $profile) {
  496. if ($getnetwork)
  497. return(NETWORK_DFRN);
  498. elseif ($compact)
  499. return($friendica);
  500. else
  501. return($username." (".$friendica.")");
  502. }
  503. $diaspora = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
  504. if ($diaspora != $profile) {
  505. if ($getnetwork)
  506. return(NETWORK_DIASPORA);
  507. elseif ($compact)
  508. return($diaspora);
  509. else
  510. return($username." (".$diaspora.")");
  511. }
  512. $red = preg_replace("=https?://(.*)/channel/(.*)=ism", "$2@$1", $profile);
  513. if ($red != $profile) {
  514. if ($getnetwork)
  515. // red is identified as Diaspora - friendica can't connect directly to it
  516. return(NETWORK_DIASPORA);
  517. elseif ($compact)
  518. return($red);
  519. else
  520. return($username." (".$red.")");
  521. }
  522. $StatusnetHost = preg_replace("=https?://(.*)/user/(.*)=ism", "$1", $profile);
  523. if ($StatusnetHost != $profile) {
  524. $StatusnetUser = preg_replace("=https?://(.*)/user/(.*)=ism", "$2", $profile);
  525. if ($StatusnetUser != $profile) {
  526. $UserData = fetch_url("http://".$StatusnetHost."/api/users/show.json?user_id=".$StatusnetUser);
  527. $user = json_decode($UserData);
  528. if ($user) {
  529. if ($getnetwork)
  530. return(NETWORK_STATUSNET);
  531. elseif ($compact)
  532. return($user->screen_name."@".$StatusnetHost);
  533. else
  534. return($username." (".$user->screen_name."@".$StatusnetHost.")");
  535. }
  536. }
  537. }
  538. // pumpio (http://host.name/user)
  539. $rest = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$3", $profile);
  540. if ($rest == "") {
  541. $pumpio = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$2@$1", $profile);
  542. if ($pumpio != $profile) {
  543. if ($getnetwork)
  544. return(NETWORK_PUMPIO);
  545. elseif ($compact)
  546. return($pumpio);
  547. else
  548. return($username." (".$pumpio.")");
  549. }
  550. }
  551. return($username);
  552. }
  553. function bb_DiasporaLinks($match) {
  554. $a = get_app();
  555. return "[url=".$a->get_baseurl()."/display/".$match[1]."]".$match[2]."[/url]";
  556. }
  557. function bb_RemovePictureLinks($match) {
  558. $text = Cache::get($match[1]);
  559. if(is_null($text)){
  560. $a = get_app();
  561. $stamp1 = microtime(true);
  562. $ch = @curl_init($match[1]);
  563. @curl_setopt($ch, CURLOPT_NOBODY, true);
  564. @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  565. @curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
  566. @curl_exec($ch);
  567. $curl_info = @curl_getinfo($ch);
  568. $a->save_timestamp($stamp1, "network");
  569. if (substr($curl_info["content_type"], 0, 6) == "image/")
  570. $text = "[url=".$match[1]."]".$match[1]."[/url]";
  571. else {
  572. $text = "[url=".$match[2]."]".$match[2]."[/url]";
  573. // if its not a picture then look if its a page that contains a picture link
  574. require_once("include/network.php");
  575. $body = fetch_url($match[1]);
  576. $doc = new DOMDocument();
  577. @$doc->loadHTML($body);
  578. $xpath = new DomXPath($doc);
  579. $list = $xpath->query("//meta[@name]");
  580. foreach ($list as $node) {
  581. $attr = array();
  582. if ($node->attributes->length)
  583. foreach ($node->attributes as $attribute)
  584. $attr[$attribute->name] = $attribute->value;
  585. if (strtolower($attr["name"]) == "twitter:image")
  586. $text = "[url=".$attr["content"]."]".$attr["content"]."[/url]";
  587. }
  588. }
  589. Cache::set($match[1],$text);
  590. }
  591. return($text);
  592. }
  593. function bb_expand_links($match) {
  594. if (($match[3] == "") OR ($match[2] == $match[3]) OR stristr($match[2], $match[3]))
  595. return ($match[1]."[url]".$match[2]."[/url]");
  596. else
  597. return ($match[1].$match[3]." [url]".$match[2]."[/url]");
  598. }
  599. function bb_CleanPictureLinksSub($match) {
  600. $text = Cache::get($match[1]);
  601. if(is_null($text)){
  602. $a = get_app();
  603. $stamp1 = microtime(true);
  604. $ch = @curl_init($match[1]);
  605. @curl_setopt($ch, CURLOPT_NOBODY, true);
  606. @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  607. @curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
  608. @curl_exec($ch);
  609. $curl_info = @curl_getinfo($ch);
  610. $a->save_timestamp($stamp1, "network");
  611. // if its a link to a picture then embed this picture
  612. if (substr($curl_info["content_type"], 0, 6) == "image/")
  613. $text = "[img]".$match[1]."[/img]";
  614. else {
  615. $text = "[img]".$match[2]."[/img]";
  616. // if its not a picture then look if its a page that contains a picture link
  617. require_once("include/network.php");
  618. $body = fetch_url($match[1]);
  619. $doc = new DOMDocument();
  620. @$doc->loadHTML($body);
  621. $xpath = new DomXPath($doc);
  622. $list = $xpath->query("//meta[@name]");
  623. foreach ($list as $node) {
  624. $attr = array();
  625. if ($node->attributes->length)
  626. foreach ($node->attributes as $attribute)
  627. $attr[$attribute->name] = $attribute->value;
  628. if (strtolower($attr["name"]) == "twitter:image")
  629. $text = "[img]".$attr["content"]."[/img]";
  630. }
  631. }
  632. Cache::set($match[1],$text);
  633. }
  634. return($text);
  635. }
  636. function bb_CleanPictureLinks($text) {
  637. $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_CleanPictureLinksSub', $text);
  638. return ($text);
  639. }
  640. // BBcode 2 HTML was written by WAY2WEB.net
  641. // extended to work with Mistpark/Friendica - Mike Macgirvin
  642. function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = false, $forplaintext = false) {
  643. $a = get_app();
  644. // Hide all [noparse] contained bbtags by spacefying them
  645. // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
  646. $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_spacefy',$Text);
  647. $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text);
  648. $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text);
  649. // Move all spaces out of the tags
  650. $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
  651. $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
  652. // Extract the private images which use data urls since preg has issues with
  653. // large data sizes. Stash them away while we do bbcode conversion, and then put them back
  654. // in after we've done all the regex matching. We cannot use any preg functions to do this.
  655. $extracted = bb_extract_images($Text);
  656. $Text = $extracted['body'];
  657. $saved_image = $extracted['images'];
  658. // If we find any event code, turn it into an event.
  659. // After we're finished processing the bbcode we'll
  660. // replace all of the event code with a reformatted version.
  661. $ev = bbtoevent($Text);
  662. // Replace any html brackets with HTML Entities to prevent executing HTML or script
  663. // Don't use strip_tags here because it breaks [url] search by replacing & with amp
  664. $Text = str_replace("<", "&lt;", $Text);
  665. $Text = str_replace(">", "&gt;", $Text);
  666. // remove some newlines before the general conversion
  667. $Text = preg_replace("/\s?\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","[share$1]$2[/share]",$Text);
  668. $Text = preg_replace("/\s?\[quote(.*?)\]\s?(.*?)\s?\[\/quote\]\s?/ism","[quote$1]$2[/quote]",$Text);
  669. $Text = preg_replace("/\n\[code\]/ism", "[code]", $Text);
  670. $Text = preg_replace("/\[\/code\]\n/ism", "[/code]", $Text);
  671. // Rearrange shares to attachments
  672. $Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism", "bb_rearrange_share",$Text);
  673. // when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
  674. if (!$tryoembed)
  675. $Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","\n[share$1$2]$3[/share]",$Text);
  676. // Convert new line chars to html <br /> tags
  677. // nlbr seems to be hopelessly messed up
  678. // $Text = nl2br($Text);
  679. // We'll emulate it.
  680. $Text = trim($Text);
  681. $Text = str_replace("\r\n","\n", $Text);
  682. // removing multiplicated newlines
  683. if (get_config("system", "remove_multiplicated_lines")) {
  684. $search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n", "\n\n[share ", "[/attachment]\n",
  685. "\n[h1]", "[/h1]\n", "\n[h2]", "[/h2]\n", "\n[h3]", "[/h3]\n", "\n[h4]", "[/h4]\n", "\n[h5]", "[/h5]\n", "\n[h6]", "[/h6]\n");
  686. $replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]", "\n[share ", "[/attachment]",
  687. "[h1]", "[/h1]", "[h2]", "[/h2]", "[h3]", "[/h3]", "[h4]", "[/h4]", "[h5]", "[/h5]", "[h6]", "[/h6]");
  688. do {
  689. $oldtext = $Text;
  690. $Text = str_replace($search, $replace, $Text);
  691. } while ($oldtext != $Text);
  692. }
  693. // Handle attached links or videos
  694. $Text = bb_attachment($Text, $simplehtml, $tryoembed);
  695. $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
  696. if($preserve_nl)
  697. $Text = str_replace(array("\n","\r"), array('',''),$Text);
  698. // Set up the parameters for a URL search string
  699. $URLSearchString = "^\[\]";
  700. // Set up the parameters for a MAIL search string
  701. $MAILSearchString = $URLSearchString;
  702. // Remove all hashtag addresses
  703. if ((!$tryoembed OR $simplehtml) AND !in_array($simplehtml, array(3, 7)))
  704. $Text = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $Text);
  705. elseif ($simplehtml == 3)
  706. $Text = preg_replace("/([@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
  707. '$1<a href="$2">$3</a>',
  708. $Text);
  709. elseif ($simplehtml == 7)
  710. $Text = preg_replace("/([@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
  711. '$1<span class="vcard"><a href="$2" class="url" title="$3"><span class="fn nickname mention">$3</span></a></span>',
  712. $Text);
  713. // Bookmarks in red - will be converted to bookmarks in friendica
  714. $Text = preg_replace("/#\^\[url\]([$URLSearchString]*)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $Text);
  715. $Text = preg_replace("/#\^\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $Text);
  716. $Text = preg_replace("/#\[url\=[$URLSearchString]*\]\^\[\/url\]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i",
  717. "[bookmark=$1]$2[/bookmark]", $Text);
  718. if (in_array($simplehtml, array(2, 6, 7, 8, 9))) {
  719. $Text = preg_replace_callback("/([^#@])\[url\=([^\]]*)\](.*?)\[\/url\]/ism","bb_expand_links",$Text);
  720. //$Text = preg_replace("/[^#@]\[url\=([^\]]*)\](.*?)\[\/url\]/ism",' $2 [url]$1[/url]',$Text);
  721. $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",' $2 [url]$1[/url]',$Text);
  722. }
  723. if ($simplehtml == 5)
  724. $Text = preg_replace("/[^#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $Text);
  725. // Perform URL Search
  726. if ($tryoembed)
  727. $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'tryoembed',$Text);
  728. if ($simplehtml == 5)
  729. $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url]$1[/url]',$Text);
  730. else
  731. $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
  732. // Handle Diaspora posts
  733. $Text = preg_replace_callback("&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi", 'bb_DiasporaLinks', $Text);
  734. // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
  735. if ($simplehtml != 7) {
  736. if (!$forplaintext)
  737. $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="_blank">$2</a>', $Text);
  738. else {
  739. $Text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism"," $1 ",$Text);
  740. $Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_RemovePictureLinks', $Text);
  741. }
  742. }
  743. if ($tryoembed)
  744. $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
  745. $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$1</a>', $Text);
  746. $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
  747. //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
  748. // Red compatibility, though the link can't be authenticated on Friendica
  749. $Text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
  750. // we may need to restrict this further if it picks up too many strays
  751. // link acct:user@host to a webfinger profile redirector
  752. $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "$1@$2"
  753. . '" target="extlink" >acct:' . "$1@$2$3" . '</a>',$Text);
  754. // Perform MAIL Search
  755. $Text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $Text);
  756. $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
  757. // leave open the posibility of [map=something]
  758. // this is replaced in prepare_body() which has knowledge of the item location
  759. if (strpos($Text,'[/map]') !== false) {
  760. $Text = preg_replace_callback("/\[map\](.*?)\[\/map\]/ism", 'bb_map_location', $Text);
  761. }
  762. if (strpos($Text,'[map=') !== false) {
  763. $Text = preg_replace_callback("/\[map=(.*?)\]/ism", 'bb_map_coords', $Text);
  764. }
  765. if (strpos($Text,'[map]') !== false) {
  766. $Text = preg_replace("/\[map\]/", '<div class="map"></div>', $Text);
  767. }
  768. // Check for headers
  769. $Text = preg_replace("(\[h1\](.*?)\[\/h1\])ism",'<h1>$1</h1>',$Text);
  770. $Text = preg_replace("(\[h2\](.*?)\[\/h2\])ism",'<h2>$1</h2>',$Text);
  771. $Text = preg_replace("(\[h3\](.*?)\[\/h3\])ism",'<h3>$1</h3>',$Text);
  772. $Text = preg_replace("(\[h4\](.*?)\[\/h4\])ism",'<h4>$1</h4>',$Text);
  773. $Text = preg_replace("(\[h5\](.*?)\[\/h5\])ism",'<h5>$1</h5>',$Text);
  774. $Text = preg_replace("(\[h6\](.*?)\[\/h6\])ism",'<h6>$1</h6>',$Text);
  775. // Check for bold text
  776. $Text = preg_replace("(\[b\](.*?)\[\/b\])ism",'<strong>$1</strong>',$Text);
  777. // Check for Italics text
  778. $Text = preg_replace("(\[i\](.*?)\[\/i\])ism",'<em>$1</em>',$Text);
  779. // Check for Underline text
  780. $Text = preg_replace("(\[u\](.*?)\[\/u\])ism",'<u>$1</u>',$Text);
  781. // Check for strike-through text
  782. $Text = preg_replace("(\[s\](.*?)\[\/s\])ism",'<strike>$1</strike>',$Text);
  783. // Check for over-line text
  784. $Text = preg_replace("(\[o\](.*?)\[\/o\])ism",'<span class="overline">$1</span>',$Text);
  785. // Check for colored text
  786. $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism","<span style=\"color: $1;\">$2</span>",$Text);
  787. // Check for sized text
  788. // [size=50] --> font-size: 50px (with the unit).
  789. $Text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1px; line-height: initial;\">$2</span>",$Text);
  790. $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1; line-height: initial;\">$2</span>",$Text);
  791. // Check for centered text
  792. $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text);
  793. // Check for list text
  794. $Text = str_replace("[*]", "<li>", $Text);
  795. // Check for style sheet commands
  796. $Text = preg_replace_callback("(\[style=(.*?)\](.*?)\[\/style\])ism","bb_cleanstyle",$Text);
  797. // Check for CSS classes
  798. $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_cleanclass",$Text);
  799. // handle nested lists
  800. $endlessloop = 0;
  801. while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
  802. ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) ||
  803. ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) ||
  804. ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
  805. $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
  806. $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
  807. $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
  808. $Text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>' ,$Text);
  809. $Text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>' ,$Text);
  810. $Text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>' ,$Text);
  811. $Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
  812. $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
  813. $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
  814. $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
  815. }
  816. $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);
  817. $Text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>' ,$Text);
  818. $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>' ,$Text);
  819. $Text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>' ,$Text);
  820. $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>' ,$Text);
  821. $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>' ,$Text);
  822. $Text = str_replace('[hr]','<hr />', $Text);
  823. // This is actually executed in prepare_body()
  824. $Text = str_replace('[nosmile]','',$Text);
  825. // Check for font change text
  826. $Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm","<span style=\"font-family: $1;\">$2</span>",$Text);
  827. // Declare the format for [code] layout
  828. // $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
  829. $CodeLayout = '<code>$1</code>';
  830. // Check for [code] text
  831. $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism","$CodeLayout", $Text);
  832. // Declare the format for [spoiler] layout
  833. $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
  834. // Check for [spoiler] text
  835. // handle nested quotes
  836. $endlessloop = 0;
  837. while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
  838. $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism","$SpoilerLayout", $Text);
  839. // Check for [spoiler=Author] text
  840. $t_wrote = t('$1 wrote:');
  841. // handle nested quotes
  842. $endlessloop = 0;
  843. while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
  844. $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
  845. "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
  846. $Text);
  847. // Declare the format for [quote] layout
  848. $QuoteLayout = '<blockquote>$1</blockquote>';
  849. // Check for [quote] text
  850. // handle nested quotes
  851. $endlessloop = 0;
  852. while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
  853. $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism","$QuoteLayout", $Text);
  854. // Check for [quote=Author] text
  855. $t_wrote = t('$1 wrote:');
  856. // handle nested quotes
  857. $endlessloop = 0;
  858. while ((strpos($Text, "[/quote]")!== false) and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
  859. $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
  860. "<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
  861. $Text);
  862. // [img=widthxheight]image source[/img]
  863. $Text = preg_replace_callback("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", 'bb_PictureCacheExt', $Text);
  864. $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
  865. $Text = preg_replace("/\[zmg\=([0-9]*)x([0-9]*)\](.*?)\[\/zmg\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $Text);
  866. // Images
  867. // [img]pathtoimage[/img]
  868. $Text = preg_replace_callback("/\[img\](.*?)\[\/img\]/ism", 'bb_PictureCache', $Text);
  869. $Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
  870. $Text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
  871. // Shared content
  872. $Text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism",
  873. function ($match) use ($simplehtml){
  874. return(bb_ShareAttributes($match, $simplehtml));
  875. },$Text);
  876. $Text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br />', $Text);
  877. $Text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
  878. //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
  879. // Try to Oembed
  880. if ($tryoembed) {
  881. $Text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4))\[\/video\]/ism", '<video src="$1" controls="controls" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></video>', $Text);
  882. $Text = preg_replace("/\[audio\](.*?\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\[\/audio\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
  883. $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
  884. $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
  885. } else {
  886. $Text = preg_replace("/\[video\](.*?)\[\/video\]/",
  887. '<a href="$1" target="_blank">$1</a>', $Text);
  888. $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/",
  889. '<a href="$1" target="_blank">$1</a>', $Text);
  890. }
  891. // html5 video and audio
  892. if ($tryoembed)
  893. $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
  894. else
  895. $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
  896. // Youtube extensions
  897. if ($tryoembed) {
  898. $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
  899. $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
  900. $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text);
  901. }
  902. $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
  903. $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
  904. $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
  905. if ($tryoembed)
  906. $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
  907. else
  908. $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
  909. '<a href="https://www.youtube.com/watch?v=$1" target="_blank">https://www.youtube.com/watch?v=$1</a>', $Text);
  910. if ($tryoembed) {
  911. $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
  912. $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
  913. }
  914. $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
  915. $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
  916. if ($tryoembed)
  917. $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
  918. else
  919. $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
  920. '<a href="https://vimeo.com/$1" target="_blank">https://vimeo.com/$1</a>', $Text);
  921. // $Text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);
  922. // oembed tag
  923. $Text = oembed_bbcode2html($Text);
  924. // Avoid triple linefeeds through oembed
  925. $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
  926. // If we found an event earlier, strip out all the event code and replace with a reformatted version.
  927. // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
  928. // Summary (e.g. title) is required, earlier revisions only required description (in addition to
  929. // start which is always required). Allow desc with a missing summary for compatibility.
  930. if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
  931. $sub = format_event_html($ev, $simplehtml);
  932. $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
  933. $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
  934. $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism",$sub,$Text);
  935. $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism",'',$Text);
  936. $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism",'',$Text);
  937. $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism",'',$Text);
  938. }
  939. //replace oneliner <code> with <key>
  940. $Text = preg_replace_callback("|(?!<br[^>]*>)<code>([^<]*)</code>(?!<br[^>]*>)|ism", 'bb_onelinecode_cb', $Text);
  941. // Unhide all [noparse] contained bbtags unspacefying them
  942. // and triming the [noparse] tag.
  943. $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_unspacefy_and_trim',$Text);
  944. $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_unspacefy_and_trim',$Text);
  945. $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_unspacefy_and_trim',$Text);
  946. $Text = preg_replace('/\[\&amp\;([#a-z0-9]+)\;\]/','&$1;',$Text);
  947. $Text = preg_replace('/\&\#039\;/','\'',$Text);
  948. $Text = preg_replace('/\&quot\;/','"',$Text);
  949. // fix any escaped ampersands that may have been converted into links
  950. $Text = preg_replace("/\<([^>]*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
  951. $Text = preg_replace("/\<([^>]*?)(src|href)=\"(?!http|ftp|mailto|gopher|cid)(.*?)\>/ism",'<$1$2="">',$Text);
  952. if($saved_image)
  953. $Text = bb_replace_images($Text, $saved_image);
  954. // Clean up the HTML by loading and saving the HTML with the DOM.
  955. // Bad structured html can break a whole page.
  956. // For performance reasons do it only with ativated item cache or at export.
  957. if (!$tryoembed OR (get_itemcachepath() != "")) {
  958. $doc = new DOMDocument();
  959. $doc->preserveWhiteSpace = false;
  960. $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
  961. $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
  962. $encoding = '<?xml encoding="UTF-8">';
  963. @$doc->loadHTML($encoding.$doctype."<html><body>".$Text."</body></html>");
  964. $doc->encoding = 'UTF-8';
  965. $Text = $doc->saveHTML();
  966. $Text = str_replace(array("<html><body>", "</body></html>", $doctype, $encoding), array("", "", "", ""), $Text);
  967. $Text = str_replace('<br></li>','</li>', $Text);
  968. //$Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
  969. }
  970. // Clean up some useless linebreaks in lists
  971. //$Text = str_replace('<br /><ul','<ul ', $Text);
  972. //$Text = str_replace('</ul><br />','</ul>', $Text);
  973. //$Text = str_replace('</li><br />','</li>', $Text);
  974. //$Text = str_replace('<br /><li>','<li>', $Text);
  975. // $Text = str_replace('<br /><ul','<ul ', $Text);
  976. call_hooks('bbcode',$Text);
  977. return trim($Text);
  978. }
  979. ?>