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.

1254 lines
46 KiB

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