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;
29 use Friendica\Util\XML;
31 require_once "include/conversation.php";
34 * @brief Generates a pseudo-random string of hexadecimal characters
39 function random_string($size = 64)
41 $byte_size = ceil($size / 2);
43 $bytes = random_bytes($byte_size);
45 $return = substr(bin2hex($bytes), 0, $size);
51 * This is our primary input filter.
53 * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
54 * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
55 * after cleansing, and angle chars with the high bit set could get through as markup.
57 * This is now disabled because it was interfering with some legitimate unicode sequences
58 * and hopefully there aren't a lot of those browsers left.
60 * Use this on any text input where angle chars are not valid or permitted
61 * They will be replaced with safer brackets. This may be filtered further
62 * if these are not allowed either.
64 * @param string $string Input string
65 * @return string Filtered string
67 function notags($string) {
68 return str_replace(["<", ">"], ['[', ']'], $string);
70 // High-bit filter no longer used
71 // return str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string);
76 * use this on "body" or "content" input where angle chars shouldn't be removed,
77 * and allow them to be safely displayed.
78 * @param string $string
81 function escape_tags($string) {
82 return htmlspecialchars($string, ENT_COMPAT, 'UTF-8', false);
87 * generate a string that's random, but usually pronounceable.
88 * used to generate initial passwords
92 function autoname($len) {
98 $vowels = ['a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'];
99 if (mt_rand(0, 5) == 4) {
118 's','sc','sh','sm','sp','st',
126 $midcons = ['ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
127 'nd','ng','nk','nt','rn','rp','rt'];
129 $noend = ['bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
130 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh','q'];
132 $start = mt_rand(0,2);
141 for ($x = 0; $x < $len; $x ++) {
142 $r = mt_rand(0,count($table) - 1);
145 if ($table == $vowels) {
146 $table = array_merge($cons,$midcons);
153 $word = substr($word,0,$len);
155 foreach ($noend as $noe) {
156 $noelen = strlen($noe);
157 if ((strlen($word) > $noelen) && (substr($word, -$noelen) == $noe)) {
158 $word = autoname($len);
167 * Turn user/group ACLs stored as angle bracketed text into arrays
172 function expand_acl($s) {
173 // turn string array of angle-bracketed elements into numeric array
174 // e.g. "<1><2><3>" => array(1,2,3);
178 $t = str_replace('<', '', $s);
179 $a = explode('>', $t);
180 foreach ($a as $aa) {
182 $ret[] = intval($aa);
191 * Wrap ACL elements in angle brackets for storage
192 * @param string $item
194 function sanitise_acl(&$item) {
196 $item = '<' . intval(notags(trim($item))) . '>';
204 * Convert an ACL array to a storable string
206 * Normally ACL permissions will be an array.
207 * We'll also allow a comma-separated string.
209 * @param string|array $p
212 function perms2str($p) {
217 $tmp = explode(',', $p);
220 if (is_array($tmp)) {
221 array_walk($tmp, 'sanitise_acl');
222 $ret = implode('', $tmp);
228 * for html,xml parsing - let's say you've got
229 * an attribute foobar="class1 class2 class3"
230 * and you want to find out if it contains 'class3'.
231 * you can't use a normal sub string search because you
232 * might match 'notclass3' and a regex to do the job is
233 * possible but a bit complicated.
234 * pass the attribute string as $attr and the attribute you
235 * are looking for as $s - returns true if found, otherwise false
237 * @param string $attr attribute value
238 * @param string $s string to search
239 * @return boolean True if found, False otherwise
241 function attribute_contains($attr, $s) {
242 $a = explode(' ', $attr);
243 return (count($a) && in_array($s,$a));
247 * Compare activity uri. Knows about activity namespace.
249 * @param string $haystack
250 * @param string $needle
253 function activity_match($haystack,$needle) {
254 return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
259 * @brief Pull out all #hashtags and @person tags from $string.
261 * We also get @person@domain.com - which would make
262 * the regex quite complicated as tags can also
263 * end a sentence. So we'll run through our results
264 * and strip the period from any tags which end with one.
265 * Returns array of tags found, or empty array.
267 * @param string $string Post content
268 * @return array List of tag and person names
270 function get_tags($string) {
273 // Convert hashtag links to hashtags
274 $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
276 // ignore anything in a code block
277 $string = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $string);
279 // Force line feeds at bbtags
280 $string = str_replace(['[', ']'], ["\n[", "]\n"], $string);
282 // ignore anything in a bbtag
283 $string = preg_replace('/\[(.*?)\]/sm', '', $string);
285 // Match full names against @tags including the space between first and last
286 // We will look these up afterward to see if they are full names or not recognisable.
288 if (preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/', $string, $matches)) {
289 foreach ($matches[1] as $match) {
290 if (strstr($match, ']')) {
291 // we might be inside a bbcode color tag - leave it alone
294 if (substr($match, -1, 1) === '.') {
295 $ret[] = substr($match, 0, -1);
302 // Otherwise pull out single word tags. These can be @nickname, @first_last
305 if (preg_match_all('/([!#@][^\^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/', $string, $matches)) {
306 foreach ($matches[1] as $match) {
307 if (strstr($match, ']')) {
308 // we might be inside a bbcode color tag - leave it alone
311 if (substr($match, -1, 1) === '.') {
312 $match = substr($match,0,-1);
314 // ignore strictly numeric tags like #1
315 if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
318 // try not to catch url fragments
319 if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
330 * quick and dirty quoted_printable encoding
336 return str_replace("%", "=", rawurlencode($s));
340 * @brief Check for a valid email string
342 * @param string $email_address
345 function valid_email($email_address)
347 return preg_match('/^[_a-zA-Z0-9\-\+]+(\.[_a-zA-Z0-9\-\+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/', $email_address);
353 * @return array index is present tense verb
354 * value is array containing past tense verb, translation of present, translation of past
355 * @hook poke_verbs pokes array
357 function get_poke_verbs() {
359 // index is present tense verb
360 // value is array containing past tense verb, translation of present, translation of past
363 'poke' => ['poked', L10n::t('poke'), L10n::t('poked')],
364 'ping' => ['pinged', L10n::t('ping'), L10n::t('pinged')],
365 'prod' => ['prodded', L10n::t('prod'), L10n::t('prodded')],
366 'slap' => ['slapped', L10n::t('slap'), L10n::t('slapped')],
367 'finger' => ['fingered', L10n::t('finger'), L10n::t('fingered')],
368 'rebuff' => ['rebuffed', L10n::t('rebuff'), L10n::t('rebuffed')],
370 Addon::callHooks('poke_verbs', $arr);
375 * @brief Translate days and months names.
377 * @param string $s String with day or month name.
378 * @return string Translated string.
380 function day_translate($s) {
381 $ret = str_replace(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
382 [L10n::t('Monday'), L10n::t('Tuesday'), L10n::t('Wednesday'), L10n::t('Thursday'), L10n::t('Friday'), L10n::t('Saturday'), L10n::t('Sunday')],
385 $ret = str_replace(['January','February','March','April','May','June','July','August','September','October','November','December'],
386 [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')],
393 * @brief Translate short days and months names.
395 * @param string $s String with short day or month name.
396 * @return string Translated string.
398 function day_short_translate($s) {
399 $ret = str_replace(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
400 [L10n::t('Mon'), L10n::t('Tue'), L10n::t('Wed'), L10n::t('Thu'), L10n::t('Fri'), L10n::t('Sat'), L10n::t('Sun')],
402 $ret = str_replace(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov','Dec'],
403 [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')],
415 function normalise_link($url) {
416 $ret = str_replace(['https:', '//www.'], ['http:', '//'], $url);
417 return rtrim($ret,'/');
422 * Compare two URLs to see if they are the same, but ignore
423 * slight but hopefully insignificant differences such as if one
424 * is https and the other isn't, or if one is www.something and
425 * the other isn't - and also ignore case differences.
427 * @param string $a first url
428 * @param string $b second url
429 * @return boolean True if the URLs match, otherwise False
432 function link_compare($a, $b) {
433 return (strcasecmp(normalise_link($a), normalise_link($b)) === 0);
438 * @brief Find any non-embedded images in private items and add redir links to them
441 * @param array &$item The field array of an item row
443 function redir_private_images($a, &$item)
446 $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
448 foreach ($matches as $mtch) {
449 if (strpos($mtch[1], '/redir') !== false) {
453 if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == Protocol::DFRN)) {
454 $img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
455 $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
462 * Sets the "rendered-html" field of the provided item
464 * Body is preserved to avoid side-effects as we modify it just-in-time for spoilers and private image links
467 * @param bool $update
469 * @todo Remove reference, simply return "rendered-html" and "rendered-hash"
471 function put_item_in_cache(&$item, $update = false)
473 $body = $item["body"];
475 $rendered_hash = defaults($item, 'rendered-hash', '');
476 $rendered_html = defaults($item, 'rendered-html', '');
478 if ($rendered_hash == ''
479 || $rendered_html == ""
480 || $rendered_hash != hash("md5", $item["body"])
481 || Config::get("system", "ignore_cache")
484 redir_private_images($a, $item);
486 $item["rendered-html"] = prepare_text($item["body"]);
487 $item["rendered-hash"] = hash("md5", $item["body"]);
489 $hook_data = ['item' => $item, 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];
490 Addon::callHooks('put_item_in_cache', $hook_data);
491 $item['rendered-html'] = $hook_data['rendered-html'];
492 $item['rendered-hash'] = $hook_data['rendered-hash'];
495 // Force an update if the generated values differ from the existing ones
496 if ($rendered_hash != $item["rendered-hash"]) {
500 // Only compare the HTML when we forcefully ignore the cache
501 if (Config::get("system", "ignore_cache") && ($rendered_html != $item["rendered-html"])) {
505 if ($update && !empty($item["id"])) {
506 Item::update(['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]],
507 ['id' => $item["id"]]);
511 $item["body"] = $body;
515 * @brief Given an item array, convert the body element from bbcode to html and add smilie icons.
516 * If attach is true, also add icons for item attachments.
519 * @param boolean $attach
520 * @param boolean $is_preview
521 * @return string item body html
522 * @hook prepare_body_init item array before any work
523 * @hook prepare_body_content_filter ('item'=>item array, 'filter_reasons'=>string array) before first bbcode to html
524 * @hook prepare_body ('item'=>item array, 'html'=>body string, 'is_preview'=>boolean, 'filter_reasons'=>string array) after first bbcode to html
525 * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
527 function prepare_body(array &$item, $attach = false, $is_preview = false)
530 Addon::callHooks('prepare_body_init', $item);
532 // In order to provide theme developers more possibilities, event items
533 // are treated differently.
534 if ($item['object-type'] === ACTIVITY_OBJ_EVENT && isset($item['event-id'])) {
535 $ev = Event::getItemHTML($item);
539 $tags = \Friendica\Model\Term::populateTagsFromItem($item);
541 $item['tags'] = $tags['tags'];
542 $item['hashtags'] = $tags['hashtags'];
543 $item['mentions'] = $tags['mentions'];
545 // Compile eventual content filter reasons
546 $filter_reasons = [];
547 if (!$is_preview && public_contact() != $item['author-id']) {
548 if (!empty($item['content-warning']) && (!local_user() || !PConfig::get(local_user(), 'system', 'disable_cw', false))) {
549 $filter_reasons[] = L10n::t('Content warning: %s', $item['content-warning']);
554 'filter_reasons' => $filter_reasons
556 Addon::callHooks('prepare_body_content_filter', $hook_data);
557 $filter_reasons = $hook_data['filter_reasons'];
561 // Update the cached values if there is no "zrl=..." on the links.
562 $update = (!local_user() && !remote_user() && ($item["uid"] == 0));
564 // Or update it if the current viewer is the intented viewer.
565 if (($item["uid"] == local_user()) && ($item["uid"] != 0)) {
569 put_item_in_cache($item, $update);
570 $s = $item["rendered-html"];
575 'preview' => $is_preview,
576 'filter_reasons' => $filter_reasons
578 Addon::callHooks('prepare_body', $hook_data);
579 $s = $hook_data['html'];
583 // Replace the blockquotes with quotes that are used in mails.
584 $mailquote = '<blockquote type="cite" class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">';
585 $s = str_replace(['<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'], [$mailquote, $mailquote, $mailquote], $s);
592 preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $item['attach'], $matches, PREG_SET_ORDER);
593 foreach ($matches as $mtch) {
596 $the_url = Contact::magicLinkById($item['author-id'], $mtch[1]);
598 if (strpos($mime, 'video') !== false) {
601 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('videos_head.tpl'), [
602 '$baseurl' => System::baseUrl(),
606 $url_parts = explode('/', $the_url);
607 $id = end($url_parts);
608 $as .= Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [
611 'title' => L10n::t('View Video'),
618 $filetype = strtolower(substr($mime, 0, strpos($mime, '/')));
620 $filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1));
621 $filesubtype = str_replace('.', '-', $filesubtype);
624 $filesubtype = 'unkn';
627 $title = escape_tags(trim(!empty($mtch[4]) ? $mtch[4] : $mtch[1]));
628 $title .= ' ' . $mtch[2] . ' ' . L10n::t('bytes');
630 $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
631 $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
635 $s .= '<div class="body-attach">'.$as.'<div class="clear"></div></div>';
639 if (strpos($s, '<div class="map">') !== false && x($item, 'coord')) {
640 $x = Map::byCoordinates(trim($item['coord']));
642 $s = preg_replace('/\<div class\=\"map\"\>/', '$0' . $x, $s);
648 $spoilersearch = '<blockquote class="spoiler">';
650 // Remove line breaks before the spoiler.
651 while ((strpos($s, "\n" . $spoilersearch) !== false)) {
652 $s = str_replace("\n" . $spoilersearch, $spoilersearch, $s);
654 while ((strpos($s, "<br />" . $spoilersearch) !== false)) {
655 $s = str_replace("<br />" . $spoilersearch, $spoilersearch, $s);
658 while ((strpos($s, $spoilersearch) !== false)) {
659 $pos = strpos($s, $spoilersearch);
660 $rnd = random_string(8);
661 $spoilerreplace = '<br /> <span id="spoiler-wrap-' . $rnd . '" class="spoiler-wrap fakelink" onclick="openClose(\'spoiler-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
662 '<blockquote class="spoiler" id="spoiler-' . $rnd . '" style="display: none;">';
663 $s = substr($s, 0, $pos) . $spoilerreplace . substr($s, $pos + strlen($spoilersearch));
666 // Look for quote with author.
667 $authorsearch = '<blockquote class="author">';
669 while ((strpos($s, $authorsearch) !== false)) {
670 $pos = strpos($s, $authorsearch);
671 $rnd = random_string(8);
672 $authorreplace = '<br /> <span id="author-wrap-' . $rnd . '" class="author-wrap fakelink" onclick="openClose(\'author-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
673 '<blockquote class="author" id="author-' . $rnd . '" style="display: block;">';
674 $s = substr($s, 0, $pos) . $authorreplace . substr($s, $pos + strlen($authorsearch));
677 // Replace friendica image url size with theme preference.
678 if (x($a->theme_info, 'item_image_size')){
679 $ps = $a->theme_info['item_image_size'];
680 $s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s);
683 $s = HTML::applyContentFilter($s, $filter_reasons);
685 $hook_data = ['item' => $item, 'html' => $s];
686 Addon::callHooks('prepare_body_final', $hook_data);
688 return $hook_data['html'];
692 * @brief Given a text string, convert from bbcode to html and add smilie icons.
694 * @param string $text String with bbcode.
695 * @return string Formattet HTML.
697 function prepare_text($text) {
698 if (stristr($text, '[nosmile]')) {
699 $s = BBCode::convert($text);
701 $s = Smilies::replace(BBCode::convert($text));
708 * return array with details for categories and folders for an item
714 * [ // categories array
716 * 'name': 'category name',
717 * 'removeurl': 'url to remove this category',
718 * 'first': 'is the first in this array? true/false',
719 * 'last': 'is the last in this array? true/false',
725 * 'name': 'folder name',
726 * 'removeurl': 'url to remove this folder',
727 * 'first': 'is the first in this array? true/false',
728 * 'last': 'is the last in this array? true/false',
734 function get_cats_and_terms($item)
741 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
743 foreach ($matches as $mtch) {
745 'name' => XML::escape(FileTag::decode($mtch[1])),
747 'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . XML::escape(FileTag::decode($mtch[1])):""),
755 if (count($categories)) {
756 $categories[count($categories) - 1]['last'] = true;
759 if (local_user() == $item['uid']) {
762 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
764 foreach ($matches as $mtch) {
766 'name' => XML::escape(FileTag::decode($mtch[1])),
768 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . XML::escape(FileTag::decode($mtch[1])) : ""),
777 if (count($folders)) {
778 $folders[count($folders) - 1]['last'] = true;
781 return [$categories, $folders];
786 * get private link for item
788 * @return boolean|array False if item has not plink, otherwise array('href'=>plink url, 'title'=>translated title)
790 function get_plink($item) {
793 if ($a->user['nickname'] != "") {
795 //'href' => "display/" . $a->user['nickname'] . "/" . $item['id'],
796 'href' => "display/" . $item['guid'],
797 'orig' => "display/" . $item['guid'],
798 'title' => L10n::t('View on separate page'),
799 'orig_title' => L10n::t('view on separate page'),
802 if (x($item, 'plink')) {
803 $ret["href"] = $a->removeBaseURL($item['plink']);
804 $ret["title"] = L10n::t('link to source');
807 } elseif (x($item, 'plink') && ($item['private'] != 1)) {
809 'href' => $item['plink'],
810 'orig' => $item['plink'],
811 'title' => L10n::t('link to source'),
821 * return number of bytes in size (K, M, G)
822 * @param string $size_str
825 function return_bytes($size_str) {
826 switch (substr ($size_str, -1)) {
827 case 'M': case 'm': return (int)$size_str * 1048576;
828 case 'K': case 'k': return (int)$size_str * 1024;
829 case 'G': case 'g': return (int)$size_str * 1073741824;
830 default: return $size_str;
836 * @param boolean $strip_padding
839 function base64url_encode($s, $strip_padding = false) {
841 $s = strtr(base64_encode($s), '+/', '-_');
843 if ($strip_padding) {
844 $s = str_replace('=','',$s);
854 function base64url_decode($s) {
857 Logger::log('base64url_decode: illegal input: ' . print_r(debug_backtrace(), true));
862 * // Placeholder for new rev of salmon which strips base64 padding.
863 * // PHP base64_decode handles the un-padded input without requiring this step
864 * // Uncomment if you find you need it.
867 * if (!strpos($s,'=')) {
877 return base64_decode(strtr($s,'-_','+/'));
881 function bb_translate_video($s) {
884 $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
886 foreach ($matches as $mtch) {
887 if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
888 $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
889 } elseif (stristr($mtch[1], 'vimeo')) {
890 $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
898 * get translated item type
903 function item_post_type($item) {
904 if (!empty($item['event-id'])) {
905 return L10n::t('event');
906 } elseif (!empty($item['resource-id'])) {
907 return L10n::t('photo');
908 } elseif (!empty($item['verb']) && $item['verb'] !== ACTIVITY_POST) {
909 return L10n::t('activity');
910 } elseif ($item['id'] != $item['parent']) {
911 return L10n::t('comment');
914 return L10n::t('post');
917 function normalise_openid($s) {
918 return trim(str_replace(['http://', 'https://'], ['', ''], $s), '/');
922 function undo_post_tagging($s) {
924 $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
926 foreach ($matches as $mtch) {
927 if (in_array($mtch[1], ['!', '@'])) {
928 $contact = Contact::getDetailsByURL($mtch[2]);
929 $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
931 $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
937 function protect_sprintf($s) {
938 return str_replace('%', '%%', $s);
941 /// @TODO Rewrite this
942 function is_a_date_arg($s) {
948 if ($i <= $y + 1 && strpos($s, '-') == 4) {
949 $m = intval(substr($s, 5));
951 if ($m > 0 && $m <= 12) {
961 * remove intentation from a text
963 function deindent($text, $chr = "[\t ]", $count = NULL) {
964 $lines = explode("\n", $text);
966 if (is_null($count)) {
969 while ($k < count($lines) && strlen($lines[$k]) == 0) {
972 preg_match("|^" . $chr . "*|", $lines[$k], $m);
973 $count = strlen($m[0]);
976 for ($k = 0; $k < count($lines); $k++) {
977 $lines[$k] = preg_replace("|^" . $chr . "{" . $count . "}|", "", $lines[$k]);
980 return implode("\n", $lines);
983 function formatBytes($bytes, $precision = 2) {
984 $units = ['B', 'KB', 'MB', 'GB', 'TB'];
986 $bytes = max($bytes, 0);
987 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
988 $pow = min($pow, count($units) - 1);
990 $bytes /= pow(1024, $pow);
992 return round($bytes, $precision) . ' ' . $units[$pow];
996 * @brief translate and format the networkname of a contact
998 * @param string $network
999 * Networkname of the contact (e.g. dfrn, rss and so on)
1004 function format_network_name($network, $url = 0) {
1005 if ($network != "") {
1007 $network_name = '<a href="'.$url.'">'.ContactSelector::networkToName($network, $url)."</a>";
1009 $network_name = ContactSelector::networkToName($network);
1012 return $network_name;