3 * @file include/text.php
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Feature;
9 use Friendica\Content\Smilies;
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Core\Addon;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Protocol;
16 use Friendica\Core\System;
17 use Friendica\Database\DBA;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Event;
20 use Friendica\Model\Item;
21 use Friendica\Render\FriendicaSmarty;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\Map;
24 use Friendica\Util\Proxy as ProxyUtils;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Renderer;
28 use Friendica\Model\FileTag;
30 require_once "include/conversation.php";
33 * This is our template processor
35 * @param string|FriendicaSmarty $s the string requiring macro substitution,
36 * or an instance of FriendicaSmarty
37 * @param array $r key value pairs (search => replace)
38 * @return string substituted string
40 function replace_macros($s, $r)
42 return Renderer::replaceMacros($s, $r);
52 function get_markup_template($s, $root = '')
54 return Renderer::getMarkupTemplate($s, $root);
58 * @brief Generates a pseudo-random string of hexadecimal characters
63 function random_string($size = 64)
65 $byte_size = ceil($size / 2);
67 $bytes = random_bytes($byte_size);
69 $return = substr(bin2hex($bytes), 0, $size);
75 * This is our primary input filter.
77 * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
78 * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
79 * after cleansing, and angle chars with the high bit set could get through as markup.
81 * This is now disabled because it was interfering with some legitimate unicode sequences
82 * and hopefully there aren't a lot of those browsers left.
84 * Use this on any text input where angle chars are not valid or permitted
85 * They will be replaced with safer brackets. This may be filtered further
86 * if these are not allowed either.
88 * @param string $string Input string
89 * @return string Filtered string
91 function notags($string) {
92 return str_replace(["<", ">"], ['[', ']'], $string);
94 // High-bit filter no longer used
95 // return str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string);
100 * use this on "body" or "content" input where angle chars shouldn't be removed,
101 * and allow them to be safely displayed.
102 * @param string $string
105 function escape_tags($string) {
106 return htmlspecialchars($string, ENT_COMPAT, 'UTF-8', false);
111 * generate a string that's random, but usually pronounceable.
112 * used to generate initial passwords
116 function autoname($len) {
122 $vowels = ['a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'];
123 if (mt_rand(0, 5) == 4) {
142 's','sc','sh','sm','sp','st',
150 $midcons = ['ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
151 'nd','ng','nk','nt','rn','rp','rt'];
153 $noend = ['bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
154 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh','q'];
156 $start = mt_rand(0,2);
165 for ($x = 0; $x < $len; $x ++) {
166 $r = mt_rand(0,count($table) - 1);
169 if ($table == $vowels) {
170 $table = array_merge($cons,$midcons);
177 $word = substr($word,0,$len);
179 foreach ($noend as $noe) {
180 $noelen = strlen($noe);
181 if ((strlen($word) > $noelen) && (substr($word, -$noelen) == $noe)) {
182 $word = autoname($len);
192 * escape text ($str) for XML transport
194 * @return string Escaped text.
196 function xmlify($str) {
197 /// @TODO deprecated code found?
200 $len = mb_strlen($str);
201 for ($x = 0; $x < $len; $x ++) {
202 $char = mb_substr($str,$x,1);
232 $buffer = mb_ereg_replace("&", "&", $str);
233 $buffer = mb_ereg_replace("'", "'", $buffer);
234 $buffer = mb_ereg_replace('"', """, $buffer);
235 $buffer = mb_ereg_replace("<", "<", $buffer);
236 $buffer = mb_ereg_replace(">", ">", $buffer);
238 $buffer = htmlspecialchars($str, ENT_QUOTES, "UTF-8");
239 $buffer = trim($buffer);
247 * @param string $s xml escaped text
248 * @return string unescaped text
250 function unxmlify($s) {
251 /// @TODO deprecated code found?
252 // $ret = str_replace('&','&', $s);
253 // $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
254 /*$ret = mb_ereg_replace('&', '&', $s);
255 $ret = mb_ereg_replace(''', "'", $ret);
256 $ret = mb_ereg_replace('"', '"', $ret);
257 $ret = mb_ereg_replace('<', "<", $ret);
258 $ret = mb_ereg_replace('>', ">", $ret);
260 $ret = htmlspecialchars_decode($s, ENT_QUOTES);
265 * Loader for infinite scrolling
266 * @return string html for loader
268 function scroll_loader() {
269 $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl");
270 return Renderer::replaceMacros($tpl, [
271 'wait' => L10n::t('Loading more entries...'),
272 'end' => L10n::t('The end')
278 * Turn user/group ACLs stored as angle bracketed text into arrays
283 function expand_acl($s) {
284 // turn string array of angle-bracketed elements into numeric array
285 // e.g. "<1><2><3>" => array(1,2,3);
289 $t = str_replace('<', '', $s);
290 $a = explode('>', $t);
291 foreach ($a as $aa) {
293 $ret[] = intval($aa);
302 * Wrap ACL elements in angle brackets for storage
303 * @param string $item
305 function sanitise_acl(&$item) {
307 $item = '<' . intval(notags(trim($item))) . '>';
315 * Convert an ACL array to a storable string
317 * Normally ACL permissions will be an array.
318 * We'll also allow a comma-separated string.
320 * @param string|array $p
323 function perms2str($p) {
328 $tmp = explode(',', $p);
331 if (is_array($tmp)) {
332 array_walk($tmp, 'sanitise_acl');
333 $ret = implode('', $tmp);
339 * for html,xml parsing - let's say you've got
340 * an attribute foobar="class1 class2 class3"
341 * and you want to find out if it contains 'class3'.
342 * you can't use a normal sub string search because you
343 * might match 'notclass3' and a regex to do the job is
344 * possible but a bit complicated.
345 * pass the attribute string as $attr and the attribute you
346 * are looking for as $s - returns true if found, otherwise false
348 * @param string $attr attribute value
349 * @param string $s string to search
350 * @return boolean True if found, False otherwise
352 function attribute_contains($attr, $s) {
353 $a = explode(' ', $attr);
354 return (count($a) && in_array($s,$a));
358 * Compare activity uri. Knows about activity namespace.
360 * @param string $haystack
361 * @param string $needle
364 function activity_match($haystack,$needle) {
365 return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
370 * @brief Pull out all #hashtags and @person tags from $string.
372 * We also get @person@domain.com - which would make
373 * the regex quite complicated as tags can also
374 * end a sentence. So we'll run through our results
375 * and strip the period from any tags which end with one.
376 * Returns array of tags found, or empty array.
378 * @param string $string Post content
379 * @return array List of tag and person names
381 function get_tags($string) {
384 // Convert hashtag links to hashtags
385 $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
387 // ignore anything in a code block
388 $string = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $string);
390 // Force line feeds at bbtags
391 $string = str_replace(['[', ']'], ["\n[", "]\n"], $string);
393 // ignore anything in a bbtag
394 $string = preg_replace('/\[(.*?)\]/sm', '', $string);
396 // Match full names against @tags including the space between first and last
397 // We will look these up afterward to see if they are full names or not recognisable.
399 if (preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/', $string, $matches)) {
400 foreach ($matches[1] as $match) {
401 if (strstr($match, ']')) {
402 // we might be inside a bbcode color tag - leave it alone
405 if (substr($match, -1, 1) === '.') {
406 $ret[] = substr($match, 0, -1);
413 // Otherwise pull out single word tags. These can be @nickname, @first_last
416 if (preg_match_all('/([!#@][^\^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/', $string, $matches)) {
417 foreach ($matches[1] as $match) {
418 if (strstr($match, ']')) {
419 // we might be inside a bbcode color tag - leave it alone
422 if (substr($match, -1, 1) === '.') {
423 $match = substr($match,0,-1);
425 // ignore strictly numeric tags like #1
426 if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
429 // try not to catch url fragments
430 if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
441 * quick and dirty quoted_printable encoding
447 return str_replace("%", "=", rawurlencode($s));
452 * Get html for contact block.
454 * @template contact_block.tpl
455 * @hook contact_block_end (contacts=>array, output=>string)
458 function contact_block() {
462 $shown = PConfig::get($a->profile['uid'], 'system', 'display_friend_count', 24);
467 if (!is_array($a->profile) || $a->profile['hide-friends']) {
470 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
471 WHERE `uid` = %d AND NOT `self` AND NOT `blocked`
472 AND NOT `pending` AND NOT `hidden` AND NOT `archive`
473 AND `network` IN ('%s', '%s', '%s')",
474 intval($a->profile['uid']),
475 DBA::escape(Protocol::DFRN),
476 DBA::escape(Protocol::OSTATUS),
477 DBA::escape(Protocol::DIASPORA)
479 if (DBA::isResult($r)) {
480 $total = intval($r[0]['total']);
483 $contacts = L10n::t('No contacts');
486 // Splitting the query in two parts makes it much faster
487 $r = q("SELECT `id` FROM `contact`
488 WHERE `uid` = %d AND NOT `self` AND NOT `blocked`
489 AND NOT `pending` AND NOT `hidden` AND NOT `archive`
490 AND `network` IN ('%s', '%s', '%s')
491 ORDER BY RAND() LIMIT %d",
492 intval($a->profile['uid']),
493 DBA::escape(Protocol::DFRN),
494 DBA::escape(Protocol::OSTATUS),
495 DBA::escape(Protocol::DIASPORA),
498 if (DBA::isResult($r)) {
500 foreach ($r AS $contact) {
501 $contacts[] = $contact["id"];
503 $r = q("SELECT `id`, `uid`, `addr`, `url`, `name`, `thumb`, `network` FROM `contact` WHERE `id` IN (%s)",
504 DBA::escape(implode(",", $contacts)));
506 if (DBA::isResult($r)) {
507 $contacts = L10n::tt('%d Contact', '%d Contacts', $total);
509 foreach ($r as $rr) {
510 $micropro[] = micropro($rr, true, 'mpfriend');
516 $tpl = Renderer::getMarkupTemplate('contact_block.tpl');
517 $o = Renderer::replaceMacros($tpl, [
518 '$contacts' => $contacts,
519 '$nickname' => $a->profile['nickname'],
520 '$viewcontacts' => L10n::t('View Contacts'),
521 '$micropro' => $micropro,
524 $arr = ['contacts' => $r, 'output' => $o];
526 Addon::callHooks('contact_block_end', $arr);
533 * @brief Format contacts as picture links or as texxt links
535 * @param array $contact Array with contacts which contains an array with
536 * int 'id' => The ID of the contact
537 * int 'uid' => The user ID of the user who owns this data
538 * string 'name' => The name of the contact
539 * string 'url' => The url to the profile page of the contact
540 * string 'addr' => The webbie of the contact (e.g.) username@friendica.com
541 * string 'network' => The network to which the contact belongs to
542 * string 'thumb' => The contact picture
543 * string 'click' => js code which is performed when clicking on the contact
544 * @param boolean $redirect If true try to use the redir url if it's possible
545 * @param string $class CSS class for the
546 * @param boolean $textmode If true display the contacts as text links
547 * if false display the contacts as picture links
549 * @return string Formatted html
551 function micropro($contact, $redirect = false, $class = '', $textmode = false) {
553 // Use the contact URL if no address is available
554 if (!x($contact, "addr")) {
555 $contact["addr"] = $contact["url"];
558 $url = $contact['url'];
563 $url = Contact::magicLink($contact['url']);
564 if (strpos($url, 'redir/') === 0) {
565 $sparkle = ' sparkle';
569 // If there is some js available we don't need the url
570 if (x($contact, 'click')) {
574 return Renderer::replaceMacros(Renderer::getMarkupTemplate(($textmode)?'micropro_txt.tpl':'micropro_img.tpl'),[
575 '$click' => defaults($contact, 'click', ''),
578 '$photo' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
579 '$name' => $contact['name'],
580 'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
581 '$parkle' => $sparkle,
590 * @param string $s Search query.
591 * @param string $id HTML id
592 * @param string $url Search url.
593 * @param bool $save Show save search button.
594 * @param bool $aside Display the search widgit aside.
596 * @return string Formatted HTML.
598 function search($s, $id = 'search-box', $url = 'search', $save = false, $aside = true)
602 if (strpos($s, '#') === 0) {
605 $save_label = $mode === 'text' ? L10n::t('Save') : L10n::t('Follow');
608 '$s' => htmlspecialchars($s),
610 '$action_url' => $url,
611 '$search_label' => L10n::t('Search'),
612 '$save_label' => $save_label,
613 '$savedsearch' => local_user() && Feature::isEnabled(local_user(),'savedsearch'),
614 '$search_hint' => L10n::t('@name, !forum, #tags, content'),
619 $values['$searchoption'] = [
620 L10n::t("Full Text"),
622 L10n::t("Contacts")];
624 if (Config::get('system','poco_local_search')) {
625 $values['$searchoption'][] = L10n::t("Forums");
629 return Renderer::replaceMacros(Renderer::getMarkupTemplate('searchbox.tpl'), $values);
633 * @brief Check for a valid email string
635 * @param string $email_address
638 function valid_email($email_address)
640 return preg_match('/^[_a-zA-Z0-9\-\+]+(\.[_a-zA-Z0-9\-\+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/', $email_address);
645 * Replace naked text hyperlink with HTML formatted hyperlink
649 function linkify($s) {
650 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="_blank">$1</a>', $s);
651 $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$s);
659 * @return array index is present tense verb
660 * value is array containing past tense verb, translation of present, translation of past
661 * @hook poke_verbs pokes array
663 function get_poke_verbs() {
665 // index is present tense verb
666 // value is array containing past tense verb, translation of present, translation of past
669 'poke' => ['poked', L10n::t('poke'), L10n::t('poked')],
670 'ping' => ['pinged', L10n::t('ping'), L10n::t('pinged')],
671 'prod' => ['prodded', L10n::t('prod'), L10n::t('prodded')],
672 'slap' => ['slapped', L10n::t('slap'), L10n::t('slapped')],
673 'finger' => ['fingered', L10n::t('finger'), L10n::t('fingered')],
674 'rebuff' => ['rebuffed', L10n::t('rebuff'), L10n::t('rebuffed')],
676 Addon::callHooks('poke_verbs', $arr);
681 * @brief Translate days and months names.
683 * @param string $s String with day or month name.
684 * @return string Translated string.
686 function day_translate($s) {
687 $ret = str_replace(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
688 [L10n::t('Monday'), L10n::t('Tuesday'), L10n::t('Wednesday'), L10n::t('Thursday'), L10n::t('Friday'), L10n::t('Saturday'), L10n::t('Sunday')],
691 $ret = str_replace(['January','February','March','April','May','June','July','August','September','October','November','December'],
692 [L10n::t('January'), L10n::t('February'), L10n::t('March'), L10n::t('April'), L10n::t('May'), L10n::t('June'), L10n::t('July'), L10n::t('August'), L10n::t('September'), L10n::t('October'), L10n::t('November'), L10n::t('December')],
699 * @brief Translate short days and months names.
701 * @param string $s String with short day or month name.
702 * @return string Translated string.
704 function day_short_translate($s) {
705 $ret = str_replace(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
706 [L10n::t('Mon'), L10n::t('Tue'), L10n::t('Wed'), L10n::t('Thu'), L10n::t('Fri'), L10n::t('Sat'), L10n::t('Sun')],
708 $ret = str_replace(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov','Dec'],
709 [L10n::t('Jan'), L10n::t('Feb'), L10n::t('Mar'), L10n::t('Apr'), L10n::t('May'), ('Jun'), L10n::t('Jul'), L10n::t('Aug'), L10n::t('Sep'), L10n::t('Oct'), L10n::t('Nov'), L10n::t('Dec')],
721 function normalise_link($url) {
722 $ret = str_replace(['https:', '//www.'], ['http:', '//'], $url);
723 return rtrim($ret,'/');
728 * Compare two URLs to see if they are the same, but ignore
729 * slight but hopefully insignificant differences such as if one
730 * is https and the other isn't, or if one is www.something and
731 * the other isn't - and also ignore case differences.
733 * @param string $a first url
734 * @param string $b second url
735 * @return boolean True if the URLs match, otherwise False
738 function link_compare($a, $b) {
739 return (strcasecmp(normalise_link($a), normalise_link($b)) === 0);
744 * @brief Find any non-embedded images in private items and add redir links to them
747 * @param array &$item The field array of an item row
749 function redir_private_images($a, &$item)
752 $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
754 foreach ($matches as $mtch) {
755 if (strpos($mtch[1], '/redir') !== false) {
759 if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == Protocol::DFRN)) {
760 $img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
761 $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
768 * Sets the "rendered-html" field of the provided item
770 * Body is preserved to avoid side-effects as we modify it just-in-time for spoilers and private image links
773 * @param bool $update
775 * @todo Remove reference, simply return "rendered-html" and "rendered-hash"
777 function put_item_in_cache(&$item, $update = false)
779 $body = $item["body"];
781 $rendered_hash = defaults($item, 'rendered-hash', '');
782 $rendered_html = defaults($item, 'rendered-html', '');
784 if ($rendered_hash == ''
785 || $rendered_html == ""
786 || $rendered_hash != hash("md5", $item["body"])
787 || Config::get("system", "ignore_cache")
790 redir_private_images($a, $item);
792 $item["rendered-html"] = prepare_text($item["body"]);
793 $item["rendered-hash"] = hash("md5", $item["body"]);
795 $hook_data = ['item' => $item, 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];
796 Addon::callHooks('put_item_in_cache', $hook_data);
797 $item['rendered-html'] = $hook_data['rendered-html'];
798 $item['rendered-hash'] = $hook_data['rendered-hash'];
801 // Force an update if the generated values differ from the existing ones
802 if ($rendered_hash != $item["rendered-hash"]) {
806 // Only compare the HTML when we forcefully ignore the cache
807 if (Config::get("system", "ignore_cache") && ($rendered_html != $item["rendered-html"])) {
811 if ($update && !empty($item["id"])) {
812 Item::update(['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]],
813 ['id' => $item["id"]]);
817 $item["body"] = $body;
821 * @brief Given an item array, convert the body element from bbcode to html and add smilie icons.
822 * If attach is true, also add icons for item attachments.
825 * @param boolean $attach
826 * @param boolean $is_preview
827 * @return string item body html
828 * @hook prepare_body_init item array before any work
829 * @hook prepare_body_content_filter ('item'=>item array, 'filter_reasons'=>string array) before first bbcode to html
830 * @hook prepare_body ('item'=>item array, 'html'=>body string, 'is_preview'=>boolean, 'filter_reasons'=>string array) after first bbcode to html
831 * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
833 function prepare_body(array &$item, $attach = false, $is_preview = false)
836 Addon::callHooks('prepare_body_init', $item);
838 // In order to provide theme developers more possibilities, event items
839 // are treated differently.
840 if ($item['object-type'] === ACTIVITY_OBJ_EVENT && isset($item['event-id'])) {
841 $ev = Event::getItemHTML($item);
845 $tags = \Friendica\Model\Term::populateTagsFromItem($item);
847 $item['tags'] = $tags['tags'];
848 $item['hashtags'] = $tags['hashtags'];
849 $item['mentions'] = $tags['mentions'];
851 // Compile eventual content filter reasons
852 $filter_reasons = [];
853 if (!$is_preview && public_contact() != $item['author-id']) {
854 if (!empty($item['content-warning']) && (!local_user() || !PConfig::get(local_user(), 'system', 'disable_cw', false))) {
855 $filter_reasons[] = L10n::t('Content warning: %s', $item['content-warning']);
860 'filter_reasons' => $filter_reasons
862 Addon::callHooks('prepare_body_content_filter', $hook_data);
863 $filter_reasons = $hook_data['filter_reasons'];
867 // Update the cached values if there is no "zrl=..." on the links.
868 $update = (!local_user() && !remote_user() && ($item["uid"] == 0));
870 // Or update it if the current viewer is the intented viewer.
871 if (($item["uid"] == local_user()) && ($item["uid"] != 0)) {
875 put_item_in_cache($item, $update);
876 $s = $item["rendered-html"];
881 'preview' => $is_preview,
882 'filter_reasons' => $filter_reasons
884 Addon::callHooks('prepare_body', $hook_data);
885 $s = $hook_data['html'];
889 // Replace the blockquotes with quotes that are used in mails.
890 $mailquote = '<blockquote type="cite" class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">';
891 $s = str_replace(['<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'], [$mailquote, $mailquote, $mailquote], $s);
898 preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $item['attach'], $matches, PREG_SET_ORDER);
899 foreach ($matches as $mtch) {
902 $the_url = Contact::magicLinkById($item['author-id'], $mtch[1]);
904 if (strpos($mime, 'video') !== false) {
907 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('videos_head.tpl'), [
908 '$baseurl' => System::baseUrl(),
912 $url_parts = explode('/', $the_url);
913 $id = end($url_parts);
914 $as .= Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [
917 'title' => L10n::t('View Video'),
924 $filetype = strtolower(substr($mime, 0, strpos($mime, '/')));
926 $filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1));
927 $filesubtype = str_replace('.', '-', $filesubtype);
930 $filesubtype = 'unkn';
933 $title = escape_tags(trim(!empty($mtch[4]) ? $mtch[4] : $mtch[1]));
934 $title .= ' ' . $mtch[2] . ' ' . L10n::t('bytes');
936 $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
937 $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
941 $s .= '<div class="body-attach">'.$as.'<div class="clear"></div></div>';
945 if (strpos($s, '<div class="map">') !== false && x($item, 'coord')) {
946 $x = Map::byCoordinates(trim($item['coord']));
948 $s = preg_replace('/\<div class\=\"map\"\>/', '$0' . $x, $s);
954 $spoilersearch = '<blockquote class="spoiler">';
956 // Remove line breaks before the spoiler.
957 while ((strpos($s, "\n" . $spoilersearch) !== false)) {
958 $s = str_replace("\n" . $spoilersearch, $spoilersearch, $s);
960 while ((strpos($s, "<br />" . $spoilersearch) !== false)) {
961 $s = str_replace("<br />" . $spoilersearch, $spoilersearch, $s);
964 while ((strpos($s, $spoilersearch) !== false)) {
965 $pos = strpos($s, $spoilersearch);
966 $rnd = random_string(8);
967 $spoilerreplace = '<br /> <span id="spoiler-wrap-' . $rnd . '" class="spoiler-wrap fakelink" onclick="openClose(\'spoiler-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
968 '<blockquote class="spoiler" id="spoiler-' . $rnd . '" style="display: none;">';
969 $s = substr($s, 0, $pos) . $spoilerreplace . substr($s, $pos + strlen($spoilersearch));
972 // Look for quote with author.
973 $authorsearch = '<blockquote class="author">';
975 while ((strpos($s, $authorsearch) !== false)) {
976 $pos = strpos($s, $authorsearch);
977 $rnd = random_string(8);
978 $authorreplace = '<br /> <span id="author-wrap-' . $rnd . '" class="author-wrap fakelink" onclick="openClose(\'author-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
979 '<blockquote class="author" id="author-' . $rnd . '" style="display: block;">';
980 $s = substr($s, 0, $pos) . $authorreplace . substr($s, $pos + strlen($authorsearch));
983 // Replace friendica image url size with theme preference.
984 if (x($a->theme_info, 'item_image_size')){
985 $ps = $a->theme_info['item_image_size'];
986 $s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s);
989 $s = apply_content_filter($s, $filter_reasons);
991 $hook_data = ['item' => $item, 'html' => $s];
992 Addon::callHooks('prepare_body_final', $hook_data);
994 return $hook_data['html'];
998 * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
1000 * Reasons are expected to have been translated already.
1002 * @param string $html
1003 * @param array $reasons
1006 function apply_content_filter($html, array $reasons)
1008 if (count($reasons)) {
1009 $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl');
1010 $html = Renderer::replaceMacros($tpl, [
1011 '$reasons' => $reasons,
1012 '$rnd' => random_string(8),
1013 '$openclose' => L10n::t('Click to open/close'),
1022 * @brief Given a text string, convert from bbcode to html and add smilie icons.
1024 * @param string $text String with bbcode.
1025 * @return string Formattet HTML.
1027 function prepare_text($text) {
1028 if (stristr($text, '[nosmile]')) {
1029 $s = BBCode::convert($text);
1031 $s = Smilies::replace(BBCode::convert($text));
1038 * return array with details for categories and folders for an item
1040 * @param array $item
1044 * [ // categories array
1046 * 'name': 'category name',
1047 * 'removeurl': 'url to remove this category',
1048 * 'first': 'is the first in this array? true/false',
1049 * 'last': 'is the last in this array? true/false',
1055 * 'name': 'folder name',
1056 * 'removeurl': 'url to remove this folder',
1057 * 'first': 'is the first in this array? true/false',
1058 * 'last': 'is the last in this array? true/false',
1064 function get_cats_and_terms($item)
1071 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
1073 foreach ($matches as $mtch) {
1075 'name' => xmlify(FileTag::decode($mtch[1])),
1077 'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . xmlify(FileTag::decode($mtch[1])):""),
1085 if (count($categories)) {
1086 $categories[count($categories) - 1]['last'] = true;
1089 if (local_user() == $item['uid']) {
1092 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
1094 foreach ($matches as $mtch) {
1096 'name' => xmlify(FileTag::decode($mtch[1])),
1098 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . xmlify(FileTag::decode($mtch[1])) : ""),
1107 if (count($folders)) {
1108 $folders[count($folders) - 1]['last'] = true;
1111 return [$categories, $folders];
1116 * get private link for item
1117 * @param array $item
1118 * @return boolean|array False if item has not plink, otherwise array('href'=>plink url, 'title'=>translated title)
1120 function get_plink($item) {
1123 if ($a->user['nickname'] != "") {
1125 //'href' => "display/" . $a->user['nickname'] . "/" . $item['id'],
1126 'href' => "display/" . $item['guid'],
1127 'orig' => "display/" . $item['guid'],
1128 'title' => L10n::t('View on separate page'),
1129 'orig_title' => L10n::t('view on separate page'),
1132 if (x($item, 'plink')) {
1133 $ret["href"] = $a->removeBaseURL($item['plink']);
1134 $ret["title"] = L10n::t('link to source');
1137 } elseif (x($item, 'plink') && ($item['private'] != 1)) {
1139 'href' => $item['plink'],
1140 'orig' => $item['plink'],
1141 'title' => L10n::t('link to source'),
1152 * replace html amp entity with amp char
1156 function unamp($s) {
1157 return str_replace('&', '&', $s);
1162 * return number of bytes in size (K, M, G)
1163 * @param string $size_str
1166 function return_bytes($size_str) {
1167 switch (substr ($size_str, -1)) {
1168 case 'M': case 'm': return (int)$size_str * 1048576;
1169 case 'K': case 'k': return (int)$size_str * 1024;
1170 case 'G': case 'g': return (int)$size_str * 1073741824;
1171 default: return $size_str;
1177 * @param boolean $strip_padding
1180 function base64url_encode($s, $strip_padding = false) {
1182 $s = strtr(base64_encode($s), '+/', '-_');
1184 if ($strip_padding) {
1185 $s = str_replace('=','',$s);
1195 function base64url_decode($s) {
1198 Logger::log('base64url_decode: illegal input: ' . print_r(debug_backtrace(), true));
1203 * // Placeholder for new rev of salmon which strips base64 padding.
1204 * // PHP base64_decode handles the un-padded input without requiring this step
1205 * // Uncomment if you find you need it.
1208 * if (!strpos($s,'=')) {
1218 return base64_decode(strtr($s,'-_','+/'));
1223 * return div element with class 'clear'
1227 function cleardiv() {
1228 return '<div class="clear"></div>';
1232 function bb_translate_video($s) {
1235 $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
1237 foreach ($matches as $mtch) {
1238 if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
1239 $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
1240 } elseif (stristr($mtch[1], 'vimeo')) {
1241 $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
1248 function html2bb_video($s) {
1250 $s = preg_replace('#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
1251 '[youtube]$2[/youtube]', $s);
1253 $s = preg_replace('#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
1254 '[youtube]$2[/youtube]', $s);
1256 $s = preg_replace('#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
1257 '[vimeo]$2[/vimeo]', $s);
1263 * apply xmlify() to all values of array $val, recursively
1267 function array_xmlify($val){
1268 if (is_bool($val)) {
1269 return $val?"true":"false";
1270 } elseif (is_array($val)) {
1271 return array_map('array_xmlify', $val);
1273 return xmlify((string) $val);
1278 * transform link href and img src from relative to absolute
1280 * @param string $text
1281 * @param string $base base url
1284 function reltoabs($text, $base) {
1289 $base = rtrim($base,'/');
1291 $base2 = $base . "/";
1294 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
1295 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
1296 $text = preg_replace($pattern, $replace, $text);
1298 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
1299 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
1300 $text = preg_replace($pattern, $replace, $text);
1303 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
1304 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
1305 $text = preg_replace($pattern, $replace, $text);
1307 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
1308 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
1309 $text = preg_replace($pattern, $replace, $text);
1317 * get translated item type
1319 * @param array $itme
1322 function item_post_type($item) {
1323 if (!empty($item['event-id'])) {
1324 return L10n::t('event');
1325 } elseif (!empty($item['resource-id'])) {
1326 return L10n::t('photo');
1327 } elseif (!empty($item['verb']) && $item['verb'] !== ACTIVITY_POST) {
1328 return L10n::t('activity');
1329 } elseif ($item['id'] != $item['parent']) {
1330 return L10n::t('comment');
1333 return L10n::t('post');
1336 function normalise_openid($s) {
1337 return trim(str_replace(['http://', 'https://'], ['', ''], $s), '/');
1341 function undo_post_tagging($s) {
1343 $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
1345 foreach ($matches as $mtch) {
1346 if (in_array($mtch[1], ['!', '@'])) {
1347 $contact = Contact::getDetailsByURL($mtch[2]);
1348 $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
1350 $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
1356 function protect_sprintf($s) {
1357 return str_replace('%', '%%', $s);
1360 /// @TODO Rewrite this
1361 function is_a_date_arg($s) {
1367 if ($i <= $y + 1 && strpos($s, '-') == 4) {
1368 $m = intval(substr($s, 5));
1370 if ($m > 0 && $m <= 12) {
1380 * remove intentation from a text
1382 function deindent($text, $chr = "[\t ]", $count = NULL) {
1383 $lines = explode("\n", $text);
1385 if (is_null($count)) {
1388 while ($k < count($lines) && strlen($lines[$k]) == 0) {
1391 preg_match("|^" . $chr . "*|", $lines[$k], $m);
1392 $count = strlen($m[0]);
1395 for ($k = 0; $k < count($lines); $k++) {
1396 $lines[$k] = preg_replace("|^" . $chr . "{" . $count . "}|", "", $lines[$k]);
1399 return implode("\n", $lines);
1402 function formatBytes($bytes, $precision = 2) {
1403 $units = ['B', 'KB', 'MB', 'GB', 'TB'];
1405 $bytes = max($bytes, 0);
1406 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
1407 $pow = min($pow, count($units) - 1);
1409 $bytes /= pow(1024, $pow);
1411 return round($bytes, $precision) . ' ' . $units[$pow];
1415 * @brief translate and format the networkname of a contact
1417 * @param string $network
1418 * Networkname of the contact (e.g. dfrn, rss and so on)
1423 function format_network_name($network, $url = 0) {
1424 if ($network != "") {
1426 $network_name = '<a href="'.$url.'">'.ContactSelector::networkToName($network, $url)."</a>";
1428 $network_name = ContactSelector::networkToName($network);
1431 return $network_name;