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\System;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Event;
19 use Friendica\Model\Item;
20 use Friendica\Render\FriendicaSmarty;
21 use Friendica\Util\DateTimeFormat;
22 use Friendica\Util\Map;
23 use Friendica\Util\Proxy as ProxyUtils;
25 require_once "include/conversation.php";
28 * This is our template processor
30 * @param string|FriendicaSmarty $s the string requiring macro substitution,
31 * or an instance of FriendicaSmarty
32 * @param array $r key value pairs (search => replace)
33 * @return string substituted string
35 function replace_macros($s, $r) {
37 $stamp1 = microtime(true);
41 // pass $baseurl to all templates
42 $r['$baseurl'] = System::baseUrl();
44 $t = $a->template_engine();
46 $output = $t->replaceMacros($s, $r);
47 } catch (Exception $e) {
48 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
52 $a->save_timestamp($stamp1, "rendering");
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);
266 * @brief Paginator function. Pushes relevant links in a pager array structure.
268 * Links are generated depending on the current page and the total number of items.
269 * Inactive links (like "first" and "prev" on page 1) are given the "disabled" class.
270 * Current page link is given the "active" CSS class
272 * @param App $a App instance
273 * @param int $count [optional] item count (used with minimal pager)
274 * @return Array data for pagination template
276 function paginate_data(App $a, $count = null) {
277 $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $a->query_string);
279 $stripped = str_replace('q=', '', $stripped);
280 $stripped = trim($stripped, '/');
281 $pagenum = $a->pager['page'];
283 if (($a->page_offset != '') && !preg_match('/[?&].offset=/', $stripped)) {
284 $stripped .= '&offset=' . urlencode($a->page_offset);
290 function _l(&$d, $name, $url, $text, $class = '') {
291 if (strpos($url, '?') === false && ($pos = strpos($url, '&')) !== false) {
292 $url = substr($url, 0, $pos) . '?' . substr($url, $pos + 1);
295 $d[$name] = ['url' => $url, 'text' => $text, 'class' => $class];
298 if (!is_null($count)) {
299 // minimal pager (newer / older)
300 $data['class'] = 'pager';
301 _l($data, 'prev', $url . '&page=' . ($a->pager['page'] - 1), L10n::t('newer'), 'previous' . ($a->pager['page'] == 1 ? ' disabled' : ''));
302 _l($data, 'next', $url . '&page=' . ($a->pager['page'] + 1), L10n::t('older'), 'next' . ($count <= 0 ? ' disabled' : ''));
304 // full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
305 $data['class'] = 'pagination';
306 if ($a->pager['total'] > $a->pager['itemspage']) {
307 _l($data, 'first', $url . '&page=1', L10n::t('first'), $a->pager['page'] == 1 ? 'disabled' : '');
308 _l($data, 'prev', $url . '&page=' . ($a->pager['page'] - 1), L10n::t('prev'), $a->pager['page'] == 1 ? 'disabled' : '');
310 $numpages = $a->pager['total'] / $a->pager['itemspage'];
313 $numstop = $numpages;
315 // Limit the number of displayed page number buttons.
317 $numstart = (($pagenum > 4) ? ($pagenum - 4) : 1);
318 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 8));
323 for ($i = $numstart; $i <= $numstop; $i++) {
324 if ($i == $a->pager['page']) {
325 _l($pages, $i, '#', $i, 'current active');
327 _l($pages, $i, $url . '&page='. $i, $i, 'n');
331 if (($a->pager['total'] % $a->pager['itemspage']) != 0) {
332 if ($i == $a->pager['page']) {
333 _l($pages, $i, '#', $i, 'current active');
335 _l($pages, $i, $url . '&page=' . $i, $i, 'n');
339 $data['pages'] = $pages;
341 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
342 _l($data, 'next', $url . '&page=' . ($a->pager['page'] + 1), L10n::t('next'), $a->pager['page'] == $lastpage ? 'disabled' : '');
343 _l($data, 'last', $url . '&page=' . $lastpage, L10n::t('last'), $a->pager['page'] == $lastpage ? 'disabled' : '');
352 * Automatic pagination.
354 * To use, get the count of total items.
355 * Then call $a->set_pager_total($number_items);
356 * Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
357 * Then call paginate($a) after the end of the display loop to insert the pager block on the page
358 * (assuming there are enough items to paginate).
359 * When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
360 * will limit the results to the correct items for the current page.
361 * The actual page handling is then accomplished at the application layer.
363 * @param App $a App instance
364 * @return string html for pagination #FIXME remove html
366 function paginate(App $a) {
368 $data = paginate_data($a);
369 $tpl = get_markup_template("paginate.tpl");
370 return replace_macros($tpl, ["pager" => $data]);
377 * @param App $a App instance
379 * @return string html for pagination #FIXME remove html
381 function alt_pager(App $a, $i) {
383 $data = paginate_data($a, $i);
384 $tpl = get_markup_template("paginate.tpl");
385 return replace_macros($tpl, ['pager' => $data]);
391 * Loader for infinite scrolling
392 * @return string html for loader
394 function scroll_loader() {
395 $tpl = get_markup_template("scroll_loader.tpl");
396 return replace_macros($tpl, [
397 'wait' => L10n::t('Loading more entries...'),
398 'end' => L10n::t('The end')
404 * Turn user/group ACLs stored as angle bracketed text into arrays
409 function expand_acl($s) {
410 // turn string array of angle-bracketed elements into numeric array
411 // e.g. "<1><2><3>" => array(1,2,3);
415 $t = str_replace('<', '', $s);
416 $a = explode('>', $t);
417 foreach ($a as $aa) {
419 $ret[] = intval($aa);
428 * Wrap ACL elements in angle brackets for storage
429 * @param string $item
431 function sanitise_acl(&$item) {
433 $item = '<' . intval(notags(trim($item))) . '>';
441 * Convert an ACL array to a storable string
443 * Normally ACL permissions will be an array.
444 * We'll also allow a comma-separated string.
446 * @param string|array $p
449 function perms2str($p) {
454 $tmp = explode(',', $p);
457 if (is_array($tmp)) {
458 array_walk($tmp, 'sanitise_acl');
459 $ret = implode('', $tmp);
468 * @param string $root
471 function get_markup_template($s, $root = '') {
472 $stamp1 = microtime(true);
475 $t = $a->template_engine();
477 $template = $t->getTemplateFile($s, $root);
478 } catch (Exception $e) {
479 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
483 $a->save_timestamp($stamp1, "file");
489 * for html,xml parsing - let's say you've got
490 * an attribute foobar="class1 class2 class3"
491 * and you want to find out if it contains 'class3'.
492 * you can't use a normal sub string search because you
493 * might match 'notclass3' and a regex to do the job is
494 * possible but a bit complicated.
495 * pass the attribute string as $attr and the attribute you
496 * are looking for as $s - returns true if found, otherwise false
498 * @param string $attr attribute value
499 * @param string $s string to search
500 * @return boolean True if found, False otherwise
502 function attribute_contains($attr, $s) {
503 $a = explode(' ', $attr);
504 return (count($a) && in_array($s,$a));
508 /* setup int->string log level map */
512 * @brief Logs the given message at the given log level
516 * LOGGER_INFO (default)
522 * @global array $LOGGER_LEVELS
526 function logger($msg, $level = LOGGER_INFO) {
528 global $LOGGER_LEVELS;
530 $debugging = Config::get('system', 'debugging');
531 $logfile = Config::get('system', 'logfile');
532 $loglevel = intval(Config::get('system', 'loglevel'));
537 || $level > $loglevel
542 if (count($LOGGER_LEVELS) == 0) {
543 foreach (get_defined_constants() as $k => $v) {
544 if (substr($k, 0, 7) == "LOGGER_") {
545 $LOGGER_LEVELS[$v] = substr($k, 7, 7);
550 $process_id = session_id();
552 if ($process_id == '') {
553 $process_id = get_app()->process_id;
556 $callers = debug_backtrace();
558 if (count($callers) > 1) {
559 $function = $callers[1]['function'];
564 $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
565 DateTimeFormat::utcNow(DateTimeFormat::ATOM),
567 $LOGGER_LEVELS[$level],
568 basename($callers[0]['file']),
574 $stamp1 = microtime(true);
575 @file_put_contents($logfile, $logline, FILE_APPEND);
576 $a->save_timestamp($stamp1, "file");
580 * @brief An alternative logger for development.
581 * Works largely as logger() but allows developers
582 * to isolate particular elements they are targetting
583 * personally without background noise
587 * LOGGER_INFO (default)
593 * @global array $LOGGER_LEVELS
597 function dlogger($msg, $level = LOGGER_INFO) {
600 $logfile = Config::get('system', 'dlogfile');
605 $dlogip = Config::get('system', 'dlogip');
606 if (!is_null($dlogip) && $_SERVER['REMOTE_ADDR'] != $dlogip) {
610 if (count($LOGGER_LEVELS) == 0) {
611 foreach (get_defined_constants() as $k => $v) {
612 if (substr($k, 0, 7) == "LOGGER_") {
613 $LOGGER_LEVELS[$v] = substr($k, 7, 7);
618 $process_id = session_id();
620 if ($process_id == '') {
621 $process_id = $a->process_id;
624 $callers = debug_backtrace();
625 $logline = sprintf("%s@\t%s:\t%s:\t%s\t%s\t%s\n",
626 DateTimeFormat::utcNow(),
628 basename($callers[0]['file']),
630 $callers[1]['function'],
634 $stamp1 = microtime(true);
635 @file_put_contents($logfile, $logline, FILE_APPEND);
636 $a->save_timestamp($stamp1, "file");
641 * Compare activity uri. Knows about activity namespace.
643 * @param string $haystack
644 * @param string $needle
647 function activity_match($haystack,$needle) {
648 return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
653 * @brief Pull out all #hashtags and @person tags from $string.
655 * We also get @person@domain.com - which would make
656 * the regex quite complicated as tags can also
657 * end a sentence. So we'll run through our results
658 * and strip the period from any tags which end with one.
659 * Returns array of tags found, or empty array.
661 * @param string $string Post content
662 * @return array List of tag and person names
664 function get_tags($string) {
667 // Convert hashtag links to hashtags
668 $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
670 // ignore anything in a code block
671 $string = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $string);
673 // Force line feeds at bbtags
674 $string = str_replace(['[', ']'], ["\n[", "]\n"], $string);
676 // ignore anything in a bbtag
677 $string = preg_replace('/\[(.*?)\]/sm', '', $string);
679 // Match full names against @tags including the space between first and last
680 // We will look these up afterward to see if they are full names or not recognisable.
682 if (preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/', $string, $matches)) {
683 foreach ($matches[1] as $match) {
684 if (strstr($match, ']')) {
685 // we might be inside a bbcode color tag - leave it alone
688 if (substr($match, -1, 1) === '.') {
689 $ret[] = substr($match, 0, -1);
696 // Otherwise pull out single word tags. These can be @nickname, @first_last
699 if (preg_match_all('/([!#@][^\^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/', $string, $matches)) {
700 foreach ($matches[1] as $match) {
701 if (strstr($match, ']')) {
702 // we might be inside a bbcode color tag - leave it alone
705 if (substr($match, -1, 1) === '.') {
706 $match = substr($match,0,-1);
708 // ignore strictly numeric tags like #1
709 if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
712 // try not to catch url fragments
713 if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
724 * quick and dirty quoted_printable encoding
730 return str_replace("%", "=", rawurlencode($s));
735 * Get html for contact block.
737 * @template contact_block.tpl
738 * @hook contact_block_end (contacts=>array, output=>string)
741 function contact_block() {
745 $shown = PConfig::get($a->profile['uid'], 'system', 'display_friend_count', 24);
750 if (!is_array($a->profile) || $a->profile['hide-friends']) {
753 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
754 WHERE `uid` = %d AND NOT `self` AND NOT `blocked`
755 AND NOT `pending` AND NOT `hidden` AND NOT `archive`
756 AND `network` IN ('%s', '%s', '%s')",
757 intval($a->profile['uid']),
758 DBA::escape(NETWORK_DFRN),
759 DBA::escape(NETWORK_OSTATUS),
760 DBA::escape(NETWORK_DIASPORA)
762 if (DBA::isResult($r)) {
763 $total = intval($r[0]['total']);
766 $contacts = L10n::t('No contacts');
769 // Splitting the query in two parts makes it much faster
770 $r = q("SELECT `id` FROM `contact`
771 WHERE `uid` = %d AND NOT `self` AND NOT `blocked`
772 AND NOT `pending` AND NOT `hidden` AND NOT `archive`
773 AND `network` IN ('%s', '%s', '%s')
774 ORDER BY RAND() LIMIT %d",
775 intval($a->profile['uid']),
776 DBA::escape(NETWORK_DFRN),
777 DBA::escape(NETWORK_OSTATUS),
778 DBA::escape(NETWORK_DIASPORA),
781 if (DBA::isResult($r)) {
783 foreach ($r AS $contact) {
784 $contacts[] = $contact["id"];
786 $r = q("SELECT `id`, `uid`, `addr`, `url`, `name`, `thumb`, `network` FROM `contact` WHERE `id` IN (%s)",
787 DBA::escape(implode(",", $contacts)));
789 if (DBA::isResult($r)) {
790 $contacts = L10n::tt('%d Contact', '%d Contacts', $total);
792 foreach ($r as $rr) {
793 $micropro[] = micropro($rr, true, 'mpfriend');
799 $tpl = get_markup_template('contact_block.tpl');
800 $o = replace_macros($tpl, [
801 '$contacts' => $contacts,
802 '$nickname' => $a->profile['nickname'],
803 '$viewcontacts' => L10n::t('View Contacts'),
804 '$micropro' => $micropro,
807 $arr = ['contacts' => $r, 'output' => $o];
809 Addon::callHooks('contact_block_end', $arr);
816 * @brief Format contacts as picture links or as texxt links
818 * @param array $contact Array with contacts which contains an array with
819 * int 'id' => The ID of the contact
820 * int 'uid' => The user ID of the user who owns this data
821 * string 'name' => The name of the contact
822 * string 'url' => The url to the profile page of the contact
823 * string 'addr' => The webbie of the contact (e.g.) username@friendica.com
824 * string 'network' => The network to which the contact belongs to
825 * string 'thumb' => The contact picture
826 * string 'click' => js code which is performed when clicking on the contact
827 * @param boolean $redirect If true try to use the redir url if it's possible
828 * @param string $class CSS class for the
829 * @param boolean $textmode If true display the contacts as text links
830 * if false display the contacts as picture links
832 * @return string Formatted html
834 function micropro($contact, $redirect = false, $class = '', $textmode = false) {
836 // Use the contact URL if no address is available
837 if (!x($contact, "addr")) {
838 $contact["addr"] = $contact["url"];
841 $url = $contact['url'];
846 $url = Contact::magicLink($contact['url']);
847 if (strpos($url, 'redir/') === 0) {
848 $sparkle = ' sparkle';
852 // If there is some js available we don't need the url
853 if (x($contact, 'click')) {
857 return replace_macros(get_markup_template(($textmode)?'micropro_txt.tpl':'micropro_img.tpl'),[
858 '$click' => defaults($contact, 'click', ''),
861 '$photo' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
862 '$name' => $contact['name'],
863 'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
864 '$parkle' => $sparkle,
873 * @param string $s Search query.
874 * @param string $id HTML id
875 * @param string $url Search url.
876 * @param bool $save Show save search button.
877 * @param bool $aside Display the search widgit aside.
879 * @return string Formatted HTML.
881 function search($s, $id = 'search-box', $url = 'search', $save = false, $aside = true)
885 if (strpos($s, '#') === 0) {
888 $save_label = $mode === 'text' ? L10n::t('Save') : L10n::t('Follow');
891 '$s' => htmlspecialchars($s),
893 '$action_url' => $url,
894 '$search_label' => L10n::t('Search'),
895 '$save_label' => $save_label,
896 '$savedsearch' => local_user() && Feature::isEnabled(local_user(),'savedsearch'),
897 '$search_hint' => L10n::t('@name, !forum, #tags, content'),
902 $values['$searchoption'] = [
903 L10n::t("Full Text"),
905 L10n::t("Contacts")];
907 if (Config::get('system','poco_local_search')) {
908 $values['$searchoption'][] = L10n::t("Forums");
912 return replace_macros(get_markup_template('searchbox.tpl'), $values);
916 * @brief Check for a valid email string
918 * @param string $email_address
921 function valid_email($email_address)
923 return preg_match('/^[_a-zA-Z0-9\-\+]+(\.[_a-zA-Z0-9\-\+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/', $email_address);
928 * Replace naked text hyperlink with HTML formatted hyperlink
932 function linkify($s) {
933 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="_blank">$1</a>', $s);
934 $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$s);
942 * @return array index is present tense verb
943 * value is array containing past tense verb, translation of present, translation of past
944 * @hook poke_verbs pokes array
946 function get_poke_verbs() {
948 // index is present tense verb
949 // value is array containing past tense verb, translation of present, translation of past
952 'poke' => ['poked', L10n::t('poke'), L10n::t('poked')],
953 'ping' => ['pinged', L10n::t('ping'), L10n::t('pinged')],
954 'prod' => ['prodded', L10n::t('prod'), L10n::t('prodded')],
955 'slap' => ['slapped', L10n::t('slap'), L10n::t('slapped')],
956 'finger' => ['fingered', L10n::t('finger'), L10n::t('fingered')],
957 'rebuff' => ['rebuffed', L10n::t('rebuff'), L10n::t('rebuffed')],
959 Addon::callHooks('poke_verbs', $arr);
964 * @brief Translate days and months names.
966 * @param string $s String with day or month name.
967 * @return string Translated string.
969 function day_translate($s) {
970 $ret = str_replace(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
971 [L10n::t('Monday'), L10n::t('Tuesday'), L10n::t('Wednesday'), L10n::t('Thursday'), L10n::t('Friday'), L10n::t('Saturday'), L10n::t('Sunday')],
974 $ret = str_replace(['January','February','March','April','May','June','July','August','September','October','November','December'],
975 [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')],
982 * @brief Translate short days and months names.
984 * @param string $s String with short day or month name.
985 * @return string Translated string.
987 function day_short_translate($s) {
988 $ret = str_replace(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
989 [L10n::t('Mon'), L10n::t('Tue'), L10n::t('Wed'), L10n::t('Thu'), L10n::t('Fri'), L10n::t('Sat'), L10n::t('Sun')],
991 $ret = str_replace(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov','Dec'],
992 [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')],
1001 * @param string $url
1004 function normalise_link($url) {
1005 $ret = str_replace(['https:', '//www.'], ['http:', '//'], $url);
1006 return rtrim($ret,'/');
1011 * Compare two URLs to see if they are the same, but ignore
1012 * slight but hopefully insignificant differences such as if one
1013 * is https and the other isn't, or if one is www.something and
1014 * the other isn't - and also ignore case differences.
1016 * @param string $a first url
1017 * @param string $b second url
1018 * @return boolean True if the URLs match, otherwise False
1021 function link_compare($a, $b) {
1022 return (strcasecmp(normalise_link($a), normalise_link($b)) === 0);
1027 * @brief Find any non-embedded images in private items and add redir links to them
1030 * @param array &$item The field array of an item row
1032 function redir_private_images($a, &$item)
1035 $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
1037 foreach ($matches as $mtch) {
1038 if (strpos($mtch[1], '/redir') !== false) {
1042 if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) {
1043 $img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
1044 $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
1051 * Sets the "rendered-html" field of the provided item
1053 * Body is preserved to avoid side-effects as we modify it just-in-time for spoilers and private image links
1055 * @param array $item
1056 * @param bool $update
1058 * @todo Remove reference, simply return "rendered-html" and "rendered-hash"
1060 function put_item_in_cache(&$item, $update = false)
1062 $body = $item["body"];
1064 $rendered_hash = defaults($item, 'rendered-hash', '');
1065 $rendered_html = defaults($item, 'rendered-html', '');
1067 if ($rendered_hash == ''
1068 || $rendered_html == ""
1069 || $rendered_hash != hash("md5", $item["body"])
1070 || Config::get("system", "ignore_cache")
1073 redir_private_images($a, $item);
1075 $item["rendered-html"] = prepare_text($item["body"]);
1076 $item["rendered-hash"] = hash("md5", $item["body"]);
1078 $hook_data = ['item' => $item, 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];
1079 Addon::callHooks('put_item_in_cache', $hook_data);
1080 $item['rendered-html'] = $hook_data['rendered-html'];
1081 $item['rendered-hash'] = $hook_data['rendered-hash'];
1084 // Force an update if the generated values differ from the existing ones
1085 if ($rendered_hash != $item["rendered-hash"]) {
1089 // Only compare the HTML when we forcefully ignore the cache
1090 if (Config::get("system", "ignore_cache") && ($rendered_html != $item["rendered-html"])) {
1094 if ($update && !empty($item["id"])) {
1095 Item::update(['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]],
1096 ['id' => $item["id"]]);
1100 $item["body"] = $body;
1104 * @brief Given an item array, convert the body element from bbcode to html and add smilie icons.
1105 * If attach is true, also add icons for item attachments.
1107 * @param array $item
1108 * @param boolean $attach
1109 * @param boolean $is_preview
1110 * @return string item body html
1111 * @hook prepare_body_init item array before any work
1112 * @hook prepare_body_content_filter ('item'=>item array, 'filter_reasons'=>string array) before first bbcode to html
1113 * @hook prepare_body ('item'=>item array, 'html'=>body string, 'is_preview'=>boolean, 'filter_reasons'=>string array) after first bbcode to html
1114 * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
1116 function prepare_body(array &$item, $attach = false, $is_preview = false)
1119 Addon::callHooks('prepare_body_init', $item);
1121 // In order to provide theme developers more possibilities, event items
1122 // are treated differently.
1123 if ($item['object-type'] === ACTIVITY_OBJ_EVENT && isset($item['event-id'])) {
1124 $ev = Event::getItemHTML($item);
1128 $tags = \Friendica\Model\Term::populateTagsFromItem($item);
1130 $item['tags'] = $tags['tags'];
1131 $item['hashtags'] = $tags['hashtags'];
1132 $item['mentions'] = $tags['mentions'];
1134 // Compile eventual content filter reasons
1135 $filter_reasons = [];
1136 if (!$is_preview && public_contact() != $item['author-id']) {
1137 if (!empty($item['content-warning']) && (!local_user() || !PConfig::get(local_user(), 'system', 'disable_cw', false))) {
1138 $filter_reasons[] = L10n::t('Content warning: %s', $item['content-warning']);
1143 'filter_reasons' => $filter_reasons
1145 Addon::callHooks('prepare_body_content_filter', $hook_data);
1146 $filter_reasons = $hook_data['filter_reasons'];
1150 // Update the cached values if there is no "zrl=..." on the links.
1151 $update = (!local_user() && !remote_user() && ($item["uid"] == 0));
1153 // Or update it if the current viewer is the intented viewer.
1154 if (($item["uid"] == local_user()) && ($item["uid"] != 0)) {
1158 put_item_in_cache($item, $update);
1159 $s = $item["rendered-html"];
1164 'preview' => $is_preview,
1165 'filter_reasons' => $filter_reasons
1167 Addon::callHooks('prepare_body', $hook_data);
1168 $s = $hook_data['html'];
1172 // Replace the blockquotes with quotes that are used in mails.
1173 $mailquote = '<blockquote type="cite" class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">';
1174 $s = str_replace(['<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'], [$mailquote, $mailquote, $mailquote], $s);
1181 preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $item['attach'], $matches, PREG_SET_ORDER);
1182 foreach ($matches as $mtch) {
1185 $the_url = Contact::magicLinkById($item['author-id'], $mtch[1]);
1187 if (strpos($mime, 'video') !== false) {
1190 $a->page['htmlhead'] .= replace_macros(get_markup_template('videos_head.tpl'), [
1191 '$baseurl' => System::baseUrl(),
1193 $a->page['end'] .= replace_macros(get_markup_template('videos_end.tpl'), [
1194 '$baseurl' => System::baseUrl(),
1198 $url_parts = explode('/', $the_url);
1199 $id = end($url_parts);
1200 $as .= replace_macros(get_markup_template('video_top.tpl'), [
1203 'title' => L10n::t('View Video'),
1210 $filetype = strtolower(substr($mime, 0, strpos($mime, '/')));
1212 $filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1));
1213 $filesubtype = str_replace('.', '-', $filesubtype);
1216 $filesubtype = 'unkn';
1219 $title = escape_tags(trim(!empty($mtch[4]) ? $mtch[4] : $mtch[1]));
1220 $title .= ' ' . $mtch[2] . ' ' . L10n::t('bytes');
1222 $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
1223 $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
1227 $s .= '<div class="body-attach">'.$as.'<div class="clear"></div></div>';
1231 if (strpos($s, '<div class="map">') !== false && x($item, 'coord')) {
1232 $x = Map::byCoordinates(trim($item['coord']));
1234 $s = preg_replace('/\<div class\=\"map\"\>/', '$0' . $x, $s);
1239 // Look for spoiler.
1240 $spoilersearch = '<blockquote class="spoiler">';
1242 // Remove line breaks before the spoiler.
1243 while ((strpos($s, "\n" . $spoilersearch) !== false)) {
1244 $s = str_replace("\n" . $spoilersearch, $spoilersearch, $s);
1246 while ((strpos($s, "<br />" . $spoilersearch) !== false)) {
1247 $s = str_replace("<br />" . $spoilersearch, $spoilersearch, $s);
1250 while ((strpos($s, $spoilersearch) !== false)) {
1251 $pos = strpos($s, $spoilersearch);
1252 $rnd = random_string(8);
1253 $spoilerreplace = '<br /> <span id="spoiler-wrap-' . $rnd . '" class="spoiler-wrap fakelink" onclick="openClose(\'spoiler-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
1254 '<blockquote class="spoiler" id="spoiler-' . $rnd . '" style="display: none;">';
1255 $s = substr($s, 0, $pos) . $spoilerreplace . substr($s, $pos + strlen($spoilersearch));
1258 // Look for quote with author.
1259 $authorsearch = '<blockquote class="author">';
1261 while ((strpos($s, $authorsearch) !== false)) {
1262 $pos = strpos($s, $authorsearch);
1263 $rnd = random_string(8);
1264 $authorreplace = '<br /> <span id="author-wrap-' . $rnd . '" class="author-wrap fakelink" onclick="openClose(\'author-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
1265 '<blockquote class="author" id="author-' . $rnd . '" style="display: block;">';
1266 $s = substr($s, 0, $pos) . $authorreplace . substr($s, $pos + strlen($authorsearch));
1269 // Replace friendica image url size with theme preference.
1270 if (x($a->theme_info, 'item_image_size')){
1271 $ps = $a->theme_info['item_image_size'];
1272 $s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s);
1275 $s = apply_content_filter($s, $filter_reasons);
1277 $hook_data = ['item' => $item, 'html' => $s];
1278 Addon::callHooks('prepare_body_final', $hook_data);
1280 return $hook_data['html'];
1284 * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
1286 * Reasons are expected to have been translated already.
1288 * @param string $html
1289 * @param array $reasons
1292 function apply_content_filter($html, array $reasons)
1294 if (count($reasons)) {
1295 $tpl = get_markup_template('wall/content_filter.tpl');
1296 $html = replace_macros($tpl, [
1297 '$reasons' => $reasons,
1298 '$rnd' => random_string(8),
1299 '$openclose' => L10n::t('Click to open/close'),
1308 * @brief Given a text string, convert from bbcode to html and add smilie icons.
1310 * @param string $text String with bbcode.
1311 * @return string Formattet HTML.
1313 function prepare_text($text) {
1314 if (stristr($text, '[nosmile]')) {
1315 $s = BBCode::convert($text);
1317 $s = Smilies::replace(BBCode::convert($text));
1324 * return array with details for categories and folders for an item
1326 * @param array $item
1330 * [ // categories array
1332 * 'name': 'category name',
1333 * 'removeurl': 'url to remove this category',
1334 * 'first': 'is the first in this array? true/false',
1335 * 'last': 'is the last in this array? true/false',
1341 * 'name': 'folder name',
1342 * 'removeurl': 'url to remove this folder',
1343 * 'first': 'is the first in this array? true/false',
1344 * 'last': 'is the last in this array? true/false',
1350 function get_cats_and_terms($item)
1357 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
1359 foreach ($matches as $mtch) {
1361 'name' => xmlify(file_tag_decode($mtch[1])),
1363 'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . xmlify(file_tag_decode($mtch[1])):""),
1371 if (count($categories)) {
1372 $categories[count($categories) - 1]['last'] = true;
1375 if (local_user() == $item['uid']) {
1378 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
1380 foreach ($matches as $mtch) {
1382 'name' => xmlify(file_tag_decode($mtch[1])),
1384 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . xmlify(file_tag_decode($mtch[1])) : ""),
1393 if (count($folders)) {
1394 $folders[count($folders) - 1]['last'] = true;
1397 return [$categories, $folders];
1402 * get private link for item
1403 * @param array $item
1404 * @return boolean|array False if item has not plink, otherwise array('href'=>plink url, 'title'=>translated title)
1406 function get_plink($item) {
1409 if ($a->user['nickname'] != "") {
1411 //'href' => "display/" . $a->user['nickname'] . "/" . $item['id'],
1412 'href' => "display/" . $item['guid'],
1413 'orig' => "display/" . $item['guid'],
1414 'title' => L10n::t('View on separate page'),
1415 'orig_title' => L10n::t('view on separate page'),
1418 if (x($item, 'plink')) {
1419 $ret["href"] = $a->remove_baseurl($item['plink']);
1420 $ret["title"] = L10n::t('link to source');
1423 } elseif (x($item, 'plink') && ($item['private'] != 1)) {
1425 'href' => $item['plink'],
1426 'orig' => $item['plink'],
1427 'title' => L10n::t('link to source'),
1438 * replace html amp entity with amp char
1442 function unamp($s) {
1443 return str_replace('&', '&', $s);
1448 * return number of bytes in size (K, M, G)
1449 * @param string $size_str
1452 function return_bytes($size_str) {
1453 switch (substr ($size_str, -1)) {
1454 case 'M': case 'm': return (int)$size_str * 1048576;
1455 case 'K': case 'k': return (int)$size_str * 1024;
1456 case 'G': case 'g': return (int)$size_str * 1073741824;
1457 default: return $size_str;
1465 function generate_user_guid() {
1468 $guid = System::createGUID(32);
1469 $x = q("SELECT `uid` FROM `user` WHERE `guid` = '%s' LIMIT 1",
1472 if (!DBA::isResult($x)) {
1475 } while ($found == true);
1483 * @param boolean $strip_padding
1486 function base64url_encode($s, $strip_padding = false) {
1488 $s = strtr(base64_encode($s), '+/', '-_');
1490 if ($strip_padding) {
1491 $s = str_replace('=','',$s);
1501 function base64url_decode($s) {
1504 logger('base64url_decode: illegal input: ' . print_r(debug_backtrace(), true));
1509 * // Placeholder for new rev of salmon which strips base64 padding.
1510 * // PHP base64_decode handles the un-padded input without requiring this step
1511 * // Uncomment if you find you need it.
1514 * if (!strpos($s,'=')) {
1524 return base64_decode(strtr($s,'-_','+/'));
1529 * return div element with class 'clear'
1533 function cleardiv() {
1534 return '<div class="clear"></div>';
1538 function bb_translate_video($s) {
1541 $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
1543 foreach ($matches as $mtch) {
1544 if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
1545 $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
1546 } elseif (stristr($mtch[1], 'vimeo')) {
1547 $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
1554 function html2bb_video($s) {
1556 $s = preg_replace('#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
1557 '[youtube]$2[/youtube]', $s);
1559 $s = preg_replace('#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
1560 '[youtube]$2[/youtube]', $s);
1562 $s = preg_replace('#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
1563 '[vimeo]$2[/vimeo]', $s);
1569 * apply xmlify() to all values of array $val, recursively
1573 function array_xmlify($val){
1574 if (is_bool($val)) {
1575 return $val?"true":"false";
1576 } elseif (is_array($val)) {
1577 return array_map('array_xmlify', $val);
1579 return xmlify((string) $val);
1584 * transform link href and img src from relative to absolute
1586 * @param string $text
1587 * @param string $base base url
1590 function reltoabs($text, $base) {
1595 $base = rtrim($base,'/');
1597 $base2 = $base . "/";
1600 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
1601 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
1602 $text = preg_replace($pattern, $replace, $text);
1604 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
1605 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
1606 $text = preg_replace($pattern, $replace, $text);
1609 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
1610 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
1611 $text = preg_replace($pattern, $replace, $text);
1613 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
1614 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
1615 $text = preg_replace($pattern, $replace, $text);
1623 * get translated item type
1625 * @param array $itme
1628 function item_post_type($item) {
1629 if (!empty($item['event-id'])) {
1630 return L10n::t('event');
1631 } elseif (!empty($item['resource-id'])) {
1632 return L10n::t('photo');
1633 } elseif (!empty($item['verb']) && $item['verb'] !== ACTIVITY_POST) {
1634 return L10n::t('activity');
1635 } elseif ($item['id'] != $item['parent']) {
1636 return L10n::t('comment');
1639 return L10n::t('post');
1642 // post categories and "save to file" use the same item.file table for storage.
1643 // We will differentiate the different uses by wrapping categories in angle brackets
1644 // and save to file categories in square brackets.
1645 // To do this we need to escape these characters if they appear in our tag.
1647 function file_tag_encode($s) {
1648 return str_replace(['<','>','[',']'],['%3c','%3e','%5b','%5d'],$s);
1651 function file_tag_decode($s) {
1652 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
1655 function file_tag_file_query($table,$s,$type = 'file') {
1657 if ($type == 'file') {
1658 $str = preg_quote('[' . str_replace('%', '%%', file_tag_encode($s)) . ']');
1660 $str = preg_quote('<' . str_replace('%', '%%', file_tag_encode($s)) . '>');
1662 return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
1665 // ex. given music,video return <music><video> or [music][video]
1666 function file_tag_list_to_file($list, $type = 'file') {
1668 if (strlen($list)) {
1669 $list_array = explode(",",$list);
1670 if ($type == 'file') {
1678 foreach ($list_array as $item) {
1679 if (strlen($item)) {
1680 $tag_list .= $lbracket . file_tag_encode(trim($item)) . $rbracket;
1687 // ex. given <music><video>[friends], return music,video or friends
1688 function file_tag_file_to_list($file, $type = 'file') {
1691 if ($type == 'file') {
1692 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
1694 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
1697 foreach ($matches as $mtch) {
1698 if (strlen($list)) {
1701 $list .= file_tag_decode($mtch[1]);
1708 function file_tag_update_pconfig($uid, $file_old, $file_new, $type = 'file') {
1709 // $file_old - categories previously associated with an item
1710 // $file_new - new list of categories for an item
1712 if (!intval($uid)) {
1714 } elseif ($file_old == $file_new) {
1718 $saved = PConfig::get($uid, 'system', 'filetags');
1719 if (strlen($saved)) {
1720 if ($type == 'file') {
1723 $termtype = TERM_FILE;
1727 $termtype = TERM_CATEGORY;
1730 $filetags_updated = $saved;
1732 // check for new tags to be added as filetags in pconfig
1734 $check_new_tags = explode(",",file_tag_file_to_list($file_new,$type));
1736 foreach ($check_new_tags as $tag) {
1737 if (!stristr($saved,$lbracket . file_tag_encode($tag) . $rbracket)) {
1742 $filetags_updated .= file_tag_list_to_file(implode(",",$new_tags),$type);
1744 // check for deleted tags to be removed from filetags in pconfig
1746 $check_deleted_tags = explode(",",file_tag_file_to_list($file_old,$type));
1748 foreach ($check_deleted_tags as $tag) {
1749 if (!stristr($file_new,$lbracket . file_tag_encode($tag) . $rbracket)) {
1750 $deleted_tags[] = $tag;
1754 foreach ($deleted_tags as $key => $tag) {
1755 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
1757 intval(TERM_OBJ_POST),
1761 if (DBA::isResult($r)) {
1762 unset($deleted_tags[$key]);
1764 $filetags_updated = str_replace($lbracket . file_tag_encode($tag) . $rbracket,'',$filetags_updated);
1768 if ($saved != $filetags_updated) {
1769 PConfig::set($uid, 'system', 'filetags', $filetags_updated);
1772 } elseif (strlen($file_new)) {
1773 PConfig::set($uid, 'system', 'filetags', $file_new);
1778 function file_tag_save_file($uid, $item_id, $file)
1780 if (!intval($uid)) {
1784 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
1785 if (DBA::isResult($item)) {
1786 if (!stristr($item['file'],'[' . file_tag_encode($file) . ']')) {
1787 $fields = ['file' => $item['file'] . '[' . file_tag_encode($file) . ']'];
1788 Item::update($fields, ['id' => $item_id]);
1790 $saved = PConfig::get($uid, 'system', 'filetags');
1791 if (!strlen($saved) || !stristr($saved, '[' . file_tag_encode($file) . ']')) {
1792 PConfig::set($uid, 'system', 'filetags', $saved . '[' . file_tag_encode($file) . ']');
1794 info(L10n::t('Item filed'));
1799 function file_tag_unsave_file($uid, $item_id, $file, $cat = false)
1801 if (!intval($uid)) {
1806 $pattern = '<' . file_tag_encode($file) . '>' ;
1807 $termtype = TERM_CATEGORY;
1809 $pattern = '[' . file_tag_encode($file) . ']' ;
1810 $termtype = TERM_FILE;
1813 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
1814 if (!DBA::isResult($item)) {
1818 $fields = ['file' => str_replace($pattern,'',$item['file'])];
1819 Item::update($fields, ['id' => $item_id]);
1821 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
1823 intval(TERM_OBJ_POST),
1827 if (!DBA::isResult($r)) {
1828 $saved = PConfig::get($uid, 'system', 'filetags');
1829 PConfig::set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));
1835 function normalise_openid($s) {
1836 return trim(str_replace(['http://', 'https://'], ['', ''], $s), '/');
1840 function undo_post_tagging($s) {
1842 $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
1844 foreach ($matches as $mtch) {
1845 if (in_array($mtch[1], ['!', '@'])) {
1846 $contact = Contact::getDetailsByURL($mtch[2]);
1847 $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
1849 $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
1855 function protect_sprintf($s) {
1856 return str_replace('%', '%%', $s);
1859 /// @TODO Rewrite this
1860 function is_a_date_arg($s) {
1866 if ($i <= $y + 1 && strpos($s, '-') == 4) {
1867 $m = intval(substr($s, 5));
1869 if ($m > 0 && $m <= 12) {
1879 * remove intentation from a text
1881 function deindent($text, $chr = "[\t ]", $count = NULL) {
1882 $lines = explode("\n", $text);
1884 if (is_null($count)) {
1887 while ($k < count($lines) && strlen($lines[$k]) == 0) {
1890 preg_match("|^" . $chr . "*|", $lines[$k], $m);
1891 $count = strlen($m[0]);
1894 for ($k = 0; $k < count($lines); $k++) {
1895 $lines[$k] = preg_replace("|^" . $chr . "{" . $count . "}|", "", $lines[$k]);
1898 return implode("\n", $lines);
1901 function formatBytes($bytes, $precision = 2) {
1902 $units = ['B', 'KB', 'MB', 'GB', 'TB'];
1904 $bytes = max($bytes, 0);
1905 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
1906 $pow = min($pow, count($units) - 1);
1908 $bytes /= pow(1024, $pow);
1910 return round($bytes, $precision) . ' ' . $units[$pow];
1914 * @brief translate and format the networkname of a contact
1916 * @param string $network
1917 * Networkname of the contact (e.g. dfrn, rss and so on)
1922 function format_network_name($network, $url = 0) {
1923 if ($network != "") {
1925 $network_name = '<a href="'.$url.'">'.ContactSelector::networkToName($network, $url)."</a>";
1927 $network_name = ContactSelector::networkToName($network);
1930 return $network_name;
1935 * @brief Syntax based code highlighting for popular languages.
1936 * @param string $s Code block
1937 * @param string $lang Programming language
1938 * @return string Formated html
1940 function text_highlight($s, $lang) {
1941 if ($lang === 'js') {
1942 $lang = 'javascript';
1945 if ($lang === 'bash') {
1949 // @TODO: Replace Text_Highlighter_Renderer_Html by scrivo/highlight.php
1951 // Autoload the library to make constants available
1952 class_exists('Text_Highlighter_Renderer_Html');
1955 'numbers' => HL_NUMBERS_LI,
1960 $s = trim(html_entity_decode($s, ENT_COMPAT));
1961 $s = str_replace(' ', "\t", $s);
1964 * The highlighter library insists on an opening php tag for php code blocks. If
1965 * it isn't present, nothing is highlighted. So we're going to see if it's present.
1966 * If not, we'll add it, and then quietly remove it after we get the processed output back.
1968 if ($lang === 'php' && strpos($s, '<?php') !== 0) {
1969 $s = '<?php' . "\n" . $s;
1973 $renderer = new Text_Highlighter_Renderer_Html($options);
1974 $factory = new Text_Highlighter();
1975 $hl = $factory->factory($lang);
1976 $hl->setRenderer($renderer);
1977 $o = $hl->highlight($s);
1978 $o = str_replace("\n", '', $o);
1981 $b = substr($o, 0, strpos($o, '<li>'));
1982 $e = substr($o, strpos($o, '</li>'));
1986 return '<code>' . $o . '</code>';