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.
 
 
 
 
 
 

1279 lines
46 KiB

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