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.

1239 lines
45 KiB

9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
  1. <?php
  2. require_once("include/oembed.php");
  3. require_once('include/event.php');
  4. require_once('include/map.php');
  5. require_once('mod/proxy.php');
  6. require_once('include/Contact.php');
  7. require_once('include/plaintext.php');
  8. function bb_PictureCacheExt($matches) {
  9. if (strpos($matches[3], "data:image/") === 0)
  10. return ($matches[0]);
  11. $matches[3] = proxy_url($matches[3]);
  12. return "[img=".$matches[1]."x".$matches[2]."]".$matches[3]."[/img]";
  13. }
  14. function bb_PictureCache($matches) {
  15. if (strpos($matches[1], "data:image/") === 0)
  16. return ($matches[0]);
  17. $matches[1] = proxy_url($matches[1]);
  18. return "[img]".$matches[1]."[/img]";
  19. }
  20. function bb_map_coords($match) {
  21. // the extra space in the following line is intentional
  22. return str_replace($match[0],'<div class="map" >' . generate_map(str_replace('/',' ',$match[1])) . '</div>', $match[0]);
  23. }
  24. function bb_map_location($match) {
  25. // the extra space in the following line is intentional
  26. return str_replace($match[0],'<div class="map" >' . generate_named_map($match[1]) . '</div>', $match[0]);
  27. }
  28. function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
  29. $data = get_attachment_data($Text);
  30. if (!$data)
  31. return $Text;
  32. if (isset($data["title"])) {
  33. $data["title"] = strip_tags($data["title"]);
  34. $data["title"] = str_replace(array("http://", "https://"), "", $data["title"]);
  35. }
  36. if (((strpos($data["text"], "[img=") !== false) OR (strpos($data["text"], "[img]") !== false)) AND ($data["image"] != "")) {
  37. $data["preview"] = $data["image"];
  38. $data["image"] = "";
  39. }
  40. if ($simplehtml == 7) {
  41. $title2 = $data["title"];
  42. $test1 = trim(html_entity_decode($data["text"],ENT_QUOTES,'UTF-8'));
  43. $test2 = trim(html_entity_decode($data["title"],ENT_QUOTES,'UTF-8'));
  44. // If the link description is similar to the text above then don't add the link description
  45. if (($data["title"] != "") AND ((strpos($test1,$test2) !== false) OR
  46. (similar_text($test1,$test2) / strlen($data["title"])) > 0.9))
  47. $title2 = $data["url"];
  48. $text = sprintf('<a href="%s" title="%s" class="attachment thumbnail" rel="nofollow external">%s</a><br />',
  49. $data["url"], $data["title"], $title2);
  50. } elseif (($simplehtml != 4) AND ($simplehtml != 0))
  51. $text = sprintf('<a href="%s" target="_blank">%s</a><br>', $data["url"], $data["title"]);
  52. else {
  53. $text = sprintf('<span class="type-%s">', $data["type"]);
  54. $bookmark = array(sprintf('[bookmark=%s]%s[/bookmark]', $data["url"], $data["title"]), $data["url"], $data["title"]);
  55. if ($tryoembed)
  56. $oembed = tryoembed($bookmark);
  57. else
  58. $oembed = $bookmark[0];
  59. if (strstr(strtolower($oembed), "<iframe "))
  60. $text = $oembed;
  61. else {
  62. if (($data["image"] != "") AND !strstr(strtolower($oembed), "<img "))
  63. $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"]);
  64. elseif (($data["preview"] != "") AND !strstr(strtolower($oembed), "<img "))
  65. $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"]);
  66. if (($data["type"] == "photo") AND ($data["url"] != "") AND ($data["image"] != ""))
  67. $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"]);
  68. else
  69. $text .= $oembed;
  70. if (trim($data["description"]) != "")
  71. $text .= sprintf('<blockquote>%s</blockquote></span>', trim($data["description"]));
  72. }
  73. }
  74. return $data["text"].$text.$data["after"];
  75. }
  76. function bb_remove_share_information($Text, $plaintext = false, $nolink = false) {
  77. $data = get_attachment_data($Text);
  78. if (!$data)
  79. return $Text;
  80. if ($nolink)
  81. return $data["text"].$data["after"];
  82. $title = htmlentities($data["title"], ENT_QUOTES, 'UTF-8', false);
  83. $text = htmlentities($data["text"], ENT_QUOTES, 'UTF-8', false);
  84. if ($plaintext OR (($title != "") AND strstr($text, $title)))
  85. $data["title"] = $data["url"];
  86. elseif (($text != "") AND strstr($title, $text)) {
  87. $data["text"] = $data["title"];
  88. $data["title"] = $data["url"];
  89. }
  90. if (($data["text"] == "") AND ($data["title"] != "") AND ($data["url"] == ""))
  91. return $data["title"].$data["after"];
  92. // If the link already is included in the post, don't add it again
  93. if (($data["url"] != "") AND strpos($data["text"], $data["url"]))
  94. return $data["text"].$data["after"];
  95. $text = $data["text"];
  96. if (($data["url"] != "") AND ($data["title"] != ""))
  97. $text .= "\n[url=".$data["url"]."]".$data["title"]."[/url]";
  98. elseif (($data["url"] != ""))
  99. $text .= "\n".$data["url"];
  100. return $text."\n".$data["after"];
  101. }
  102. function bb_cleanstyle($st) {
  103. return "<span style=\"".cleancss($st[1]).";\">".$st[2]."</span>";
  104. }
  105. function bb_cleanclass($st) {
  106. return "<span class=\"".cleancss($st[1])."\">".$st[2]."</span>";
  107. }
  108. function cleancss($input) {
  109. $cleaned = "";
  110. $input = strtolower($input);
  111. for ($i = 0; $i < strlen($input); $i++) {
  112. $char = substr($input, $i, 1);
  113. if (($char >= "a") and ($char <= "z"))
  114. $cleaned .= $char;
  115. if (!(strpos(" #;:0123456789-_", $char) === false))
  116. $cleaned .= $char;
  117. }
  118. return($cleaned);
  119. }
  120. function stripcode_br_cb($s) {
  121. return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
  122. }
  123. function bb_onelinecode_cb($match) {
  124. if (strpos($match[1],"<br>")===false){
  125. return "<key>".$match[1]."</key>";
  126. }
  127. return "<code>".$match[1]."</code>";
  128. }
  129. function tryoembed($match){
  130. $url = $match[1];
  131. // Always embed the SSL version
  132. $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
  133. array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);
  134. $o = oembed_fetch_url($url);
  135. if (!is_object($o))
  136. return $match[0];
  137. if (isset($match[2]))
  138. $o->title = $match[2];
  139. if ($o->type=="error") return $match[0];
  140. $html = oembed_format_object($o);
  141. return $html;
  142. }
  143. // [noparse][i]italic[/i][/noparse] turns into
  144. // [noparse][ i ]italic[ /i ][/noparse],
  145. // to hide them from parser.
  146. function bb_spacefy($st) {
  147. $whole_match = $st[0];
  148. $captured = $st[1];
  149. $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
  150. $new_str = str_replace($captured, $spacefied, $whole_match);
  151. return $new_str;
  152. }
  153. // The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
  154. // now turns back and the [noparse] tags are trimed
  155. // returning [i]italic[/i]
  156. function bb_unspacefy_and_trim($st) {
  157. $whole_match = $st[0];
  158. $captured = $st[1];
  159. $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
  160. return $unspacefied;
  161. }
  162. function bb_find_open_close($s, $open, $close, $occurance = 1) {
  163. if($occurance < 1)
  164. $occurance = 1;
  165. $start_pos = -1;
  166. for($i = 1; $i <= $occurance; $i++) {
  167. if( $start_pos !== false)
  168. $start_pos = strpos($s, $open, $start_pos + 1);
  169. }
  170. if( $start_pos === false)
  171. return false;
  172. $end_pos = strpos($s, $close, $start_pos);
  173. if( $end_pos === false)
  174. return false;
  175. $res = array( 'start' => $start_pos, 'end' => $end_pos );
  176. return $res;
  177. }
  178. function get_bb_tag_pos($s, $name, $occurance = 1) {
  179. if($occurance < 1)
  180. $occurance = 1;
  181. $start_open = -1;
  182. for($i = 1; $i <= $occurance; $i++) {
  183. if( $start_open !== false)
  184. $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
  185. }
  186. if( $start_open === false)
  187. return false;
  188. $start_equal = strpos($s, '=', $start_open);
  189. $start_close = strpos($s, ']', $start_open);
  190. if( $start_close === false)
  191. return false;
  192. $start_close++;
  193. $end_open = strpos($s, '[/' . $name . ']', $start_close);
  194. if( $end_open === false)
  195. return false;
  196. $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
  197. 'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
  198. if( $start_equal !== false)
  199. $res['start']['equal'] = $start_equal + 1;
  200. return $res;
  201. }
  202. function bb_tag_preg_replace($pattern, $replace, $name, $s) {
  203. $string = $s;
  204. $occurance = 1;
  205. $pos = get_bb_tag_pos($string, $name, $occurance);
  206. while($pos !== false && $occurance < 1000) {
  207. $start = substr($string, 0, $pos['start']['open']);
  208. $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
  209. $end = substr($string, $pos['end']['close']);
  210. if($end === false)
  211. $end = '';
  212. $subject = preg_replace($pattern, $replace, $subject);
  213. $string = $start . $subject . $end;
  214. $occurance++;
  215. $pos = get_bb_tag_pos($string, $name, $occurance);
  216. }
  217. return $string;
  218. }
  219. if(! function_exists('bb_extract_images')) {
  220. function bb_extract_images($body) {
  221. $saved_image = array();
  222. $orig_body = $body;
  223. $new_body = '';
  224. $cnt = 0;
  225. $img_start = strpos($orig_body, '[img');
  226. $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
  227. $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
  228. while(($img_st_close !== false) && ($img_end !== false)) {
  229. $img_st_close++; // make it point to AFTER the closing bracket
  230. $img_end += $img_start;
  231. if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
  232. // This is an embedded image
  233. $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
  234. $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
  235. $cnt++;
  236. }
  237. else
  238. $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
  239. $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
  240. if($orig_body === false) // in case the body ends on a closing image tag
  241. $orig_body = '';
  242. $img_start = strpos($orig_body, '[img');
  243. $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
  244. $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
  245. }
  246. $new_body = $new_body . $orig_body;
  247. return array('body' => $new_body, 'images' => $saved_image);
  248. }}
  249. if(! function_exists('bb_replace_images')) {
  250. function bb_replace_images($body, $images) {
  251. $newbody = $body;
  252. $cnt = 0;
  253. foreach($images as $image) {
  254. // We're depending on the property of 'foreach' (specified on the PHP website) that
  255. // it loops over the array starting from the first element and going sequentially
  256. // to the last element
  257. $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '<img src="' . proxy_url($image) .'" alt="' . t('Image/photo') . '" />', $newbody);
  258. $cnt++;
  259. }
  260. return $newbody;
  261. }}
  262. function bb_ShareAttributes($share, $simplehtml) {
  263. $attributes = $share[2];
  264. $author = "";
  265. preg_match("/author='(.*?)'/ism", $attributes, $matches);
  266. if ($matches[1] != "")
  267. $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
  268. preg_match('/author="(.*?)"/ism', $attributes, $matches);
  269. if ($matches[1] != "")
  270. $author = $matches[1];
  271. $profile = "";
  272. preg_match("/profile='(.*?)'/ism", $attributes, $matches);
  273. if ($matches[1] != "")
  274. $profile = $matches[1];
  275. preg_match('/profile="(.*?)"/ism', $attributes, $matches);
  276. if ($matches[1] != "")
  277. $profile = $matches[1];
  278. $avatar = "";
  279. preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
  280. if ($matches[1] != "")
  281. $avatar = $matches[1];
  282. preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
  283. if ($matches[1] != "")
  284. $avatar = $matches[1];
  285. $link = "";
  286. preg_match("/link='(.*?)'/ism", $attributes, $matches);
  287. if ($matches[1] != "")
  288. $link = $matches[1];
  289. preg_match('/link="(.*?)"/ism', $attributes, $matches);
  290. if ($matches[1] != "")
  291. $link = $matches[1];
  292. $posted = "";
  293. $itemcache = get_itemcachepath();
  294. preg_match("/posted='(.*?)'/ism", $attributes, $matches);
  295. if ($matches[1] != "")
  296. $posted = $matches[1];
  297. preg_match('/posted="(.*?)"/ism', $attributes, $matches);
  298. if ($matches[1] != "")
  299. $posted = $matches[1];
  300. // relative dates only make sense when they aren't cached
  301. if ($itemcache == "")
  302. $reldate = (($posted) ? " " . relative_date($posted) : '');
  303. // We only call this so that a previously unknown contact can be added.
  304. // This is important for the function "get_contact_details_by_url".
  305. // This function then can fetch an entry from the contact table.
  306. get_contact($profile, 0);
  307. $data = get_contact_details_by_url($profile);
  308. if (isset($data["name"]) AND isset($data["addr"]))
  309. $userid_compact = $data["name"]." (".$data["addr"].")";
  310. else
  311. $userid_compact = GetProfileUsername($profile,$author, true);
  312. if (isset($data["addr"]))
  313. $userid = $data["addr"];
  314. else
  315. $userid = GetProfileUsername($profile,$author, false);
  316. if (isset($data["name"]))
  317. $author = $data["name"];
  318. if (isset($data["micro"]))
  319. $avatar = $data["micro"];
  320. $preshare = trim($share[1]);
  321. if ($preshare != "")
  322. $preshare .= "<br /><br />";
  323. switch ($simplehtml) {
  324. case 1:
  325. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />»".$share[3]."«";
  326. break;
  327. case 2:
  328. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
  329. break;
  330. case 3: // Diaspora
  331. $headline .= '<b>'.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').$userid.':</b><br />';
  332. $text = trim($share[1]);
  333. if ($text != "")
  334. $text .= "<hr />";
  335. if (substr(normalise_link($link), 0, 19) != "http://twitter.com/") {
  336. $text .= $headline.'<blockquote>'.trim($share[3])."</blockquote><br />";
  337. if ($link != "")
  338. $text .= '<br /><a href="'.$link.'">[l]</a>';
  339. } else
  340. $text .= '<br /><a href="'.$link.'">'.$link.'</a>';
  341. break;
  342. case 4:
  343. $headline = '<div class="shared_header">';
  344. $headline .= '<span><b>'.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
  345. $headline .= sprintf(t('<a href="%1$s" target="_blank">%2$s</a> %3$s'), $link, $userid, $posted);
  346. $headline .= ":</b></span></div>";
  347. $text = trim($share[1]);
  348. if ($text != "")
  349. $text .= "<hr />";
  350. $text .= $headline.'<blockquote class="shared_content">'.trim($share[3])."</blockquote><br />";
  351. break;
  352. case 5:
  353. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
  354. break;
  355. case 6: // app.net
  356. $text = $preshare."&gt;&gt; @".$userid_compact.": <br />".$share[3];
  357. break;
  358. case 7: // statusnet/GNU Social
  359. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8')." @".$userid_compact.": ".$share[3];
  360. break;
  361. case 8: // twitter
  362. $text = $preshare."RT @".$userid_compact.": ".$share[3];
  363. break;
  364. case 9: // Google+/Facebook
  365. $text = $preshare.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
  366. if ($link != "")
  367. $text .= "<br /><br />".$link;
  368. break;
  369. default:
  370. $text = trim($share[1])."\n";
  371. $avatar = proxy_url($avatar, false, PROXY_SIZE_THUMB);
  372. $tpl = get_markup_template('shared_content.tpl');
  373. $text .= replace_macros($tpl,
  374. array(
  375. '$profile' => $profile,
  376. '$avatar' => $avatar,
  377. '$author' => $author,
  378. '$link' => $link,
  379. '$posted' => $posted,
  380. '$reldate' => $reldate,
  381. '$content' => trim($share[3])
  382. )
  383. );
  384. break;
  385. }
  386. return($text);
  387. }
  388. function GetProfileUsername($profile, $username, $compact = false, $getnetwork = false) {
  389. $twitter = preg_replace("=https?://twitter.com/(.*)=ism", "$1@twitter.com", $profile);
  390. if ($twitter != $profile) {
  391. if ($getnetwork)
  392. return(NETWORK_TWITTER);
  393. elseif ($compact)
  394. return($twitter);
  395. else
  396. return($username." (".$twitter.")");
  397. }
  398. $appnet = preg_replace("=https?://alpha.app.net/(.*)=ism", "$1@alpha.app.net", $profile);
  399. if ($appnet != $profile) {
  400. if ($getnetwork)
  401. return(NETWORK_APPNET);
  402. elseif ($compact)
  403. return($appnet);
  404. else
  405. return($username." (".$appnet.")");
  406. }
  407. $gplus = preg_replace("=https?://plus.google.com/(.*)=ism", "$1@plus.google.com", $profile);
  408. if ($gplus != $profile) {
  409. if ($getnetwork)
  410. return(NETWORK_GPLUS);
  411. elseif ($compact)
  412. return($gplususername." (".$username.")");
  413. else
  414. return($username." (".$gplus.")");
  415. }
  416. $friendica = preg_replace("=https?://(.*)/profile/(.*)=ism", "$2@$1", $profile);
  417. if ($friendica != $profile) {
  418. if ($getnetwork)
  419. return(NETWORK_DFRN);
  420. elseif ($compact)
  421. return($friendica);
  422. else
  423. return($username." (".$friendica.")");
  424. }
  425. $diaspora = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
  426. if ($diaspora != $profile) {
  427. if ($getnetwork)
  428. return(NETWORK_DIASPORA);
  429. elseif ($compact)
  430. return($diaspora);
  431. else
  432. return($username." (".$diaspora.")");
  433. }
  434. $red = preg_replace("=https?://(.*)/channel/(.*)=ism", "$2@$1", $profile);
  435. if ($red != $profile) {
  436. if ($getnetwork)
  437. // red is identified as Diaspora - friendica can't connect directly to it
  438. return(NETWORK_DIASPORA);
  439. elseif ($compact)
  440. return($red);
  441. else
  442. return($username." (".$red.")");
  443. }
  444. $StatusnetHost = preg_replace("=https?://(.*)/user/(.*)=ism", "$1", $profile);
  445. if ($StatusnetHost != $profile) {
  446. $StatusnetUser = preg_replace("=https?://(.*)/user/(.*)=ism", "$2", $profile);
  447. if ($StatusnetUser != $profile) {
  448. $UserData = fetch_url("http://".$StatusnetHost."/api/users/show.json?user_id=".$StatusnetUser);
  449. $user = json_decode($UserData);
  450. if ($user) {
  451. if ($getnetwork)
  452. return(NETWORK_STATUSNET);
  453. elseif ($compact)
  454. return($user->screen_name."@".$StatusnetHost);
  455. else
  456. return($username." (".$user->screen_name."@".$StatusnetHost.")");
  457. }
  458. }
  459. }
  460. // pumpio (http://host.name/user)
  461. $rest = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$3", $profile);
  462. if ($rest == "") {
  463. $pumpio = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$2@$1", $profile);
  464. if ($pumpio != $profile) {
  465. if ($getnetwork)
  466. return(NETWORK_PUMPIO);
  467. elseif ($compact)
  468. return($pumpio);
  469. else
  470. return($username." (".$pumpio.")");
  471. }
  472. }
  473. return($username);
  474. }
  475. function bb_DiasporaLinks($match) {
  476. $a = get_app();
  477. return "[url=".$a->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 href="' . $a->get_baseurl() . '/acctlink?addr=' . "$1@$2"
  692. . '" target="extlink" >acct:' . "$1@$2$3" . '</a>',$Text);
  693. // Perform MAIL Search
  694. $Text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $Text);
  695. $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
  696. // leave open the posibility of [map=something]
  697. // this is replaced in prepare_body() which has knowledge of the item location
  698. if (strpos($Text,'[/map]') !== false) {
  699. $Text = preg_replace_callback("/\[map\](.*?)\[\/map\]/ism", 'bb_map_location', $Text);
  700. }
  701. if (strpos($Text,'[map=') !== false) {
  702. $Text = preg_replace_callback("/\[map=(.*?)\]/ism", 'bb_map_coords', $Text);
  703. }
  704. if (strpos($Text,'[map]') !== false) {
  705. $Text = preg_replace("/\[map\]/", '<div class="map"></div>', $Text);
  706. }
  707. // Check for headers
  708. $Text = preg_replace("(\[h1\](.*?)\[\/h1\])ism",'<h1>$1</h1>',$Text);
  709. $Text = preg_replace("(\[h2\](.*?)\[\/h2\])ism",'<h2>$1</h2>',$Text);
  710. $Text = preg_replace("(\[h3\](.*?)\[\/h3\])ism",'<h3>$1</h3>',$Text);
  711. $Text = preg_replace("(\[h4\](.*?)\[\/h4\])ism",'<h4>$1</h4>',$Text);
  712. $Text = preg_replace("(\[h5\](.*?)\[\/h5\])ism",'<h5>$1</h5>',$Text);
  713. $Text = preg_replace("(\[h6\](.*?)\[\/h6\])ism",'<h6>$1</h6>',$Text);
  714. // Check for bold text
  715. $Text = preg_replace("(\[b\](.*?)\[\/b\])ism",'<strong>$1</strong>',$Text);
  716. // Check for Italics text
  717. $Text = preg_replace("(\[i\](.*?)\[\/i\])ism",'<em>$1</em>',$Text);
  718. // Check for Underline text
  719. $Text = preg_replace("(\[u\](.*?)\[\/u\])ism",'<u>$1</u>',$Text);
  720. // Check for strike-through text
  721. $Text = preg_replace("(\[s\](.*?)\[\/s\])ism",'<strike>$1</strike>',$Text);
  722. // Check for over-line text
  723. $Text = preg_replace("(\[o\](.*?)\[\/o\])ism",'<span class="overline">$1</span>',$Text);
  724. // Check for colored text
  725. $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism","<span style=\"color: $1;\">$2</span>",$Text);
  726. // Check for sized text
  727. // [size=50] --> font-size: 50px (with the unit).
  728. $Text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1px; line-height: initial;\">$2</span>",$Text);
  729. $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1; line-height: initial;\">$2</span>",$Text);
  730. // Check for centered text
  731. $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text);
  732. // Check for list text
  733. $Text = str_replace("[*]", "<li>", $Text);
  734. // Check for style sheet commands
  735. $Text = preg_replace_callback("(\[style=(.*?)\](.*?)\[\/style\])ism","bb_cleanstyle",$Text);
  736. // Check for CSS classes
  737. $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_cleanclass",$Text);
  738. // handle nested lists
  739. $endlessloop = 0;
  740. while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
  741. ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) ||
  742. ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) ||
  743. ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
  744. $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
  745. $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
  746. $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
  747. $Text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>' ,$Text);
  748. $Text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>' ,$Text);
  749. $Text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>' ,$Text);
  750. $Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
  751. $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
  752. $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
  753. $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
  754. }
  755. $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);
  756. $Text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>' ,$Text);
  757. $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>' ,$Text);
  758. $Text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>' ,$Text);
  759. $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>' ,$Text);
  760. $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>' ,$Text);
  761. $Text = str_replace('[hr]','<hr />', $Text);
  762. // This is actually executed in prepare_body()
  763. $Text = str_replace('[nosmile]','',$Text);
  764. // Check for font change text
  765. $Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm","<span style=\"font-family: $1;\">$2</span>",$Text);
  766. // Declare the format for [code] layout
  767. // $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
  768. $CodeLayout = '<code>$1</code>';
  769. // Check for [code] text
  770. $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism","$CodeLayout", $Text);
  771. // Declare the format for [spoiler] layout
  772. $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
  773. // Check for [spoiler] text
  774. // handle nested quotes
  775. $endlessloop = 0;
  776. while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
  777. $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism","$SpoilerLayout", $Text);
  778. // Check for [spoiler=Author] text
  779. $t_wrote = t('$1 wrote:');
  780. // handle nested quotes
  781. $endlessloop = 0;
  782. while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
  783. $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
  784. "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
  785. $Text);
  786. // Declare the format for [quote] layout
  787. $QuoteLayout = '<blockquote>$1</blockquote>';
  788. // Check for [quote] text
  789. // handle nested quotes
  790. $endlessloop = 0;
  791. while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
  792. $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism","$QuoteLayout", $Text);
  793. // Check for [quote=Author] text
  794. $t_wrote = t('$1 wrote:');
  795. // handle nested quotes
  796. $endlessloop = 0;
  797. while ((strpos($Text, "[/quote]")!== false) and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
  798. $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
  799. "<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
  800. $Text);
  801. // [img=widthxheight]image source[/img]
  802. $Text = preg_replace_callback("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", 'bb_PictureCacheExt', $Text);
  803. $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
  804. $Text = preg_replace("/\[zmg\=([0-9]*)x([0-9]*)\](.*?)\[\/zmg\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $Text);
  805. // Images
  806. // [img]pathtoimage[/img]
  807. $Text = preg_replace_callback("/\[img\](.*?)\[\/img\]/ism", 'bb_PictureCache', $Text);
  808. $Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
  809. $Text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
  810. // Shared content
  811. $Text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism",
  812. function ($match) use ($simplehtml){
  813. return(bb_ShareAttributes($match, $simplehtml));
  814. },$Text);
  815. $Text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br />', $Text);
  816. $Text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
  817. //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
  818. // Try to Oembed
  819. if ($tryoembed) {
  820. $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);
  821. $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);
  822. $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
  823. $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
  824. } else {
  825. $Text = preg_replace("/\[video\](.*?)\[\/video\]/",
  826. '<a href="$1" target="_blank">$1</a>', $Text);
  827. $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/",
  828. '<a href="$1" target="_blank">$1</a>', $Text);
  829. }
  830. // html5 video and audio
  831. if ($tryoembed)
  832. $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
  833. else
  834. $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
  835. // Youtube extensions
  836. if ($tryoembed) {
  837. $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
  838. $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
  839. $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text);
  840. }
  841. $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
  842. $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
  843. $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
  844. if ($tryoembed)
  845. $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);
  846. else
  847. $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
  848. '<a href="https://www.youtube.com/watch?v=$1" target="_blank">https://www.youtube.com/watch?v=$1</a>', $Text);
  849. if ($tryoembed) {
  850. $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
  851. $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
  852. }
  853. $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
  854. $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
  855. if ($tryoembed)
  856. $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);
  857. else
  858. $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
  859. '<a href="https://vimeo.com/$1" target="_blank">https://vimeo.com/$1</a>', $Text);
  860. // $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);
  861. // oembed tag
  862. $Text = oembed_bbcode2html($Text);
  863. // Avoid triple linefeeds through oembed
  864. $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
  865. // If we found an event earlier, strip out all the event code and replace with a reformatted version.
  866. // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
  867. // Summary (e.g. title) is required, earlier revisions only required description (in addition to
  868. // start which is always required). Allow desc with a missing summary for compatibility.
  869. if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
  870. $sub = format_event_html($ev, $simplehtml);
  871. $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
  872. $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
  873. $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism",$sub,$Text);
  874. $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism",'',$Text);
  875. $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism",'',$Text);
  876. $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism",'',$Text);
  877. }
  878. //replace oneliner <code> with <key>
  879. $Text = preg_replace_callback("|(?!<br[^>]*>)<code>([^<]*)</code>(?!<br[^>]*>)|ism", 'bb_onelinecode_cb', $Text);
  880. // Unhide all [noparse] contained bbtags unspacefying them
  881. // and triming the [noparse] tag.
  882. $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_unspacefy_and_trim',$Text);
  883. $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_unspacefy_and_trim',$Text);
  884. $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_unspacefy_and_trim',$Text);
  885. $Text = preg_replace('/\[\&amp\;([#a-z0-9]+)\;\]/','&$1;',$Text);
  886. $Text = preg_replace('/\&\#039\;/','\'',$Text);
  887. $Text = preg_replace('/\&quot\;/','"',$Text);
  888. // fix any escaped ampersands that may have been converted into links
  889. $Text = preg_replace("/\<([^>]*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
  890. $Text = preg_replace("/\<([^>]*?)(src|href)=\"(?!http|ftp|mailto|gopher|cid)(.*?)\>/ism",'<$1$2="">',$Text);
  891. if($saved_image)
  892. $Text = bb_replace_images($Text, $saved_image);
  893. // Clean up the HTML by loading and saving the HTML with the DOM.
  894. // Bad structured html can break a whole page.
  895. // For performance reasons do it only with ativated item cache or at export.
  896. if (!$tryoembed OR (get_itemcachepath() != "")) {
  897. $doc = new DOMDocument();
  898. $doc->preserveWhiteSpace = false;
  899. $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
  900. $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
  901. $encoding = '<?xml encoding="UTF-8">';
  902. @$doc->loadHTML($encoding.$doctype."<html><body>".$Text."</body></html>");
  903. $doc->encoding = 'UTF-8';
  904. $Text = $doc->saveHTML();
  905. $Text = str_replace(array("<html><body>", "</body></html>", $doctype, $encoding), array("", "", "", ""), $Text);
  906. $Text = str_replace('<br></li>','</li>', $Text);
  907. //$Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
  908. }
  909. // Clean up some useless linebreaks in lists
  910. //$Text = str_replace('<br /><ul','<ul ', $Text);
  911. //$Text = str_replace('</ul><br />','</ul>', $Text);
  912. //$Text = str_replace('</li><br />','</li>', $Text);
  913. //$Text = str_replace('<br /><li>','<li>', $Text);
  914. // $Text = str_replace('<br /><ul','<ul ', $Text);
  915. call_hooks('bbcode',$Text);
  916. return trim($Text);
  917. }
  918. /**
  919. * @brief Removes the "abstract" element from the text
  920. *
  921. * @param string $text The text with BBCode
  922. * @return string The same text - but without "abstract" element
  923. */
  924. function remove_abstract($text) {
  925. $text = preg_replace("/[\s|\n]*\[abstract\].*?\[\/abstract\][\s|\n]*/ism", '', $text);
  926. $text = preg_replace("/[\s|\n]*\[abstract=.*?\].*?\[\/abstract][\s|\n]*/ism", '', $text);
  927. return $text;
  928. }
  929. /**
  930. * @brief Returns the value of the "abstract" element
  931. *
  932. * @param string $text The text that maybe contains the element
  933. * @param string $addon The addon for which the abstract is meant for
  934. * @return string The abstract
  935. */
  936. function fetch_abstract($text, $addon = "") {
  937. $abstract = "";
  938. $abstracts = array();
  939. $addon = strtolower($addon);
  940. if (preg_match_all("/\[abstract=(.*?)\](.*?)\[\/abstract\]/ism",$text, $results, PREG_SET_ORDER))
  941. foreach ($results AS $result)
  942. $abstracts[strtolower($result[1])] = $result[2];
  943. if (isset($abstracts[$addon]))
  944. $abstract = $abstracts[$addon];
  945. if ($abstract == "")
  946. if (preg_match("/\[abstract\](.*?)\[\/abstract\]/ism",$text, $result))
  947. $abstract = $result[1];
  948. return $abstract;
  949. }
  950. ?>