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.

1409 lines
50KB

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