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\DBM;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Event;
19 use Friendica\Model\Item;
20 use Friendica\Model\Profile;
21 use Friendica\Render\FriendicaSmarty;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\Map;
25 require_once "mod/proxy.php";
26 require_once "include/conversation.php";
29 * This is our template processor
31 * @param string|FriendicaSmarty $s the string requiring macro substitution,
32 * or an instance of FriendicaSmarty
33 * @param array $r key value pairs (search => replace)
34 * @return string substituted string
36 function replace_macros($s, $r) {
38 $stamp1 = microtime(true);
42 // pass $baseurl to all templates
43 $r['$baseurl'] = System::baseUrl();
45 $t = $a->template_engine();
47 $output = $t->replaceMacros($s, $r);
48 } catch (Exception $e) {
49 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
53 $a->save_timestamp($stamp1, "rendering");
59 * @brief Generates a pseudo-random string of hexadecimal characters
64 function random_string($size = 64)
66 $byte_size = ceil($size / 2);
68 $bytes = random_bytes($byte_size);
70 $return = substr(bin2hex($bytes), 0, $size);
76 * This is our primary input filter.
78 * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
79 * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
80 * after cleansing, and angle chars with the high bit set could get through as markup.
82 * This is now disabled because it was interfering with some legitimate unicode sequences
83 * and hopefully there aren't a lot of those browsers left.
85 * Use this on any text input where angle chars are not valid or permitted
86 * They will be replaced with safer brackets. This may be filtered further
87 * if these are not allowed either.
89 * @param string $string Input string
90 * @return string Filtered string
92 function notags($string) {
93 return str_replace(["<", ">"], ['[', ']'], $string);
95 // High-bit filter no longer used
96 // return str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string);
101 * use this on "body" or "content" input where angle chars shouldn't be removed,
102 * and allow them to be safely displayed.
103 * @param string $string
106 function escape_tags($string) {
107 return htmlspecialchars($string, ENT_COMPAT, 'UTF-8', false);
112 * generate a string that's random, but usually pronounceable.
113 * used to generate initial passwords
117 function autoname($len) {
123 $vowels = ['a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'];
124 if (mt_rand(0, 5) == 4) {
143 's','sc','sh','sm','sp','st',
151 $midcons = ['ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
152 'nd','ng','nk','nt','rn','rp','rt'];
154 $noend = ['bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
155 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh','q'];
157 $start = mt_rand(0,2);
166 for ($x = 0; $x < $len; $x ++) {
167 $r = mt_rand(0,count($table) - 1);
170 if ($table == $vowels) {
171 $table = array_merge($cons,$midcons);
178 $word = substr($word,0,$len);
180 foreach ($noend as $noe) {
181 $noelen = strlen($noe);
182 if ((strlen($word) > $noelen) && (substr($word, -$noelen) == $noe)) {
183 $word = autoname($len);
193 * escape text ($str) for XML transport
195 * @return string Escaped text.
197 function xmlify($str) {
198 /// @TODO deprecated code found?
201 $len = mb_strlen($str);
202 for ($x = 0; $x < $len; $x ++) {
203 $char = mb_substr($str,$x,1);
233 $buffer = mb_ereg_replace("&", "&", $str);
234 $buffer = mb_ereg_replace("'", "'", $buffer);
235 $buffer = mb_ereg_replace('"', """, $buffer);
236 $buffer = mb_ereg_replace("<", "<", $buffer);
237 $buffer = mb_ereg_replace(">", ">", $buffer);
239 $buffer = htmlspecialchars($str, ENT_QUOTES, "UTF-8");
240 $buffer = trim($buffer);
248 * @param string $s xml escaped text
249 * @return string unescaped text
251 function unxmlify($s) {
252 /// @TODO deprecated code found?
253 // $ret = str_replace('&','&', $s);
254 // $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
255 /*$ret = mb_ereg_replace('&', '&', $s);
256 $ret = mb_ereg_replace(''', "'", $ret);
257 $ret = mb_ereg_replace('"', '"', $ret);
258 $ret = mb_ereg_replace('<', "<", $ret);
259 $ret = mb_ereg_replace('>', ">", $ret);
261 $ret = htmlspecialchars_decode($s, ENT_QUOTES);
267 * @brief Paginator function. Pushes relevant links in a pager array structure.
269 * Links are generated depending on the current page and the total number of items.
270 * Inactive links (like "first" and "prev" on page 1) are given the "disabled" class.
271 * Current page link is given the "active" CSS class
273 * @param App $a App instance
274 * @param int $count [optional] item count (used with minimal pager)
275 * @return Array data for pagination template
277 function paginate_data(App $a, $count = null) {
278 $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $a->query_string);
280 $stripped = str_replace('q=', '', $stripped);
281 $stripped = trim($stripped, '/');
282 $pagenum = $a->pager['page'];
284 if (($a->page_offset != '') && !preg_match('/[?&].offset=/', $stripped)) {
285 $stripped .= '&offset=' . urlencode($a->page_offset);
291 function _l(&$d, $name, $url, $text, $class = '') {
292 if (strpos($url, '?') === false && ($pos = strpos($url, '&')) !== false) {
293 $url = substr($url, 0, $pos) . '?' . substr($url, $pos + 1);
296 $d[$name] = ['url' => $url, 'text' => $text, 'class' => $class];
299 if (!is_null($count)) {
300 // minimal pager (newer / older)
301 $data['class'] = 'pager';
302 _l($data, 'prev', $url . '&page=' . ($a->pager['page'] - 1), L10n::t('newer'), 'previous' . ($a->pager['page'] == 1 ? ' disabled' : ''));
303 _l($data, 'next', $url . '&page=' . ($a->pager['page'] + 1), L10n::t('older'), 'next' . ($count <= 0 ? ' disabled' : ''));
305 // full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
306 $data['class'] = 'pagination';
307 if ($a->pager['total'] > $a->pager['itemspage']) {
308 _l($data, 'first', $url . '&page=1', L10n::t('first'), $a->pager['page'] == 1 ? 'disabled' : '');
309 _l($data, 'prev', $url . '&page=' . ($a->pager['page'] - 1), L10n::t('prev'), $a->pager['page'] == 1 ? 'disabled' : '');
311 $numpages = $a->pager['total'] / $a->pager['itemspage'];
314 $numstop = $numpages;
316 // Limit the number of displayed page number buttons.
318 $numstart = (($pagenum > 4) ? ($pagenum - 4) : 1);
319 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 8));
324 for ($i = $numstart; $i <= $numstop; $i++) {
325 if ($i == $a->pager['page']) {
326 _l($pages, $i, '#', $i, 'current active');
328 _l($pages, $i, $url . '&page='. $i, $i, 'n');
332 if (($a->pager['total'] % $a->pager['itemspage']) != 0) {
333 if ($i == $a->pager['page']) {
334 _l($pages, $i, '#', $i, 'current active');
336 _l($pages, $i, $url . '&page=' . $i, $i, 'n');
340 $data['pages'] = $pages;
342 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
343 _l($data, 'next', $url . '&page=' . ($a->pager['page'] + 1), L10n::t('next'), $a->pager['page'] == $lastpage ? 'disabled' : '');
344 _l($data, 'last', $url . '&page=' . $lastpage, L10n::t('last'), $a->pager['page'] == $lastpage ? 'disabled' : '');
353 * Automatic pagination.
355 * To use, get the count of total items.
356 * Then call $a->set_pager_total($number_items);
357 * Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
358 * Then call paginate($a) after the end of the display loop to insert the pager block on the page
359 * (assuming there are enough items to paginate).
360 * When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
361 * will limit the results to the correct items for the current page.
362 * The actual page handling is then accomplished at the application layer.
364 * @param App $a App instance
365 * @return string html for pagination #FIXME remove html
367 function paginate(App $a) {
369 $data = paginate_data($a);
370 $tpl = get_markup_template("paginate.tpl");
371 return replace_macros($tpl, ["pager" => $data]);
378 * @param App $a App instance
380 * @return string html for pagination #FIXME remove html
382 function alt_pager(App $a, $i) {
384 $data = paginate_data($a, $i);
385 $tpl = get_markup_template("paginate.tpl");
386 return replace_macros($tpl, ['pager' => $data]);
392 * Loader for infinite scrolling
393 * @return string html for loader
395 function scroll_loader() {
396 $tpl = get_markup_template("scroll_loader.tpl");
397 return replace_macros($tpl, [
398 'wait' => L10n::t('Loading more entries...'),
399 'end' => L10n::t('The end')
405 * Turn user/group ACLs stored as angle bracketed text into arrays
410 function expand_acl($s) {
411 // turn string array of angle-bracketed elements into numeric array
412 // e.g. "<1><2><3>" => array(1,2,3);
416 $t = str_replace('<', '', $s);
417 $a = explode('>', $t);
418 foreach ($a as $aa) {
420 $ret[] = intval($aa);
429 * Wrap ACL elements in angle brackets for storage
430 * @param string $item
432 function sanitise_acl(&$item) {
434 $item = '<' . intval(notags(trim($item))) . '>';
442 * Convert an ACL array to a storable string
444 * Normally ACL permissions will be an array.
445 * We'll also allow a comma-separated string.
447 * @param string|array $p
450 function perms2str($p) {
455 $tmp = explode(',', $p);
458 if (is_array($tmp)) {
459 array_walk($tmp, 'sanitise_acl');
460 $ret = implode('', $tmp);
467 * wrapper to load a view template, checking for alternate
468 * languages before falling back to the default
470 * @global string $lang
472 * @param string $s view name
475 function load_view_file($s) {
482 if (file_exists("$d/$lang/$b")) {
483 $stamp1 = microtime(true);
484 $content = file_get_contents("$d/$lang/$b");
485 $a->save_timestamp($stamp1, "file");
489 $theme = $a->getCurrentTheme();
491 if (file_exists("$d/theme/$theme/$b")) {
492 $stamp1 = microtime(true);
493 $content = file_get_contents("$d/theme/$theme/$b");
494 $a->save_timestamp($stamp1, "file");
498 $stamp1 = microtime(true);
499 $content = file_get_contents($s);
500 $a->save_timestamp($stamp1, "file");
506 * load a view template, checking for alternate
507 * languages before falling back to the default
509 * @global string $lang
510 * @param string $s view path
513 function get_intltext_template($s) {
518 if ($a->theme['template_engine'] === 'smarty3') {
519 $engine = "/smarty3";
526 if (file_exists("view/lang/$lang$engine/$s")) {
527 $stamp1 = microtime(true);
528 $content = file_get_contents("view/lang/$lang$engine/$s");
529 $a->save_timestamp($stamp1, "file");
531 } elseif (file_exists("view/lang/en$engine/$s")) {
532 $stamp1 = microtime(true);
533 $content = file_get_contents("view/lang/en$engine/$s");
534 $a->save_timestamp($stamp1, "file");
537 $stamp1 = microtime(true);
538 $content = file_get_contents("view$engine/$s");
539 $a->save_timestamp($stamp1, "file");
549 * @param string $root
552 function get_markup_template($s, $root = '') {
553 $stamp1 = microtime(true);
556 $t = $a->template_engine();
558 $template = $t->getTemplateFile($s, $root);
559 } catch (Exception $e) {
560 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
564 $a->save_timestamp($stamp1, "file");
570 * for html,xml parsing - let's say you've got
571 * an attribute foobar="class1 class2 class3"
572 * and you want to find out if it contains 'class3'.
573 * you can't use a normal sub string search because you
574 * might match 'notclass3' and a regex to do the job is
575 * possible but a bit complicated.
576 * pass the attribute string as $attr and the attribute you
577 * are looking for as $s - returns true if found, otherwise false
579 * @param string $attr attribute value
580 * @param string $s string to search
581 * @return boolean True if found, False otherwise
583 function attribute_contains($attr, $s) {
584 $a = explode(' ', $attr);
585 return (count($a) && in_array($s,$a));
589 /* setup int->string log level map */
593 * @brief Logs the given message at the given log level
596 * LOGGER_NORMAL (default)
603 * @global array $LOGGER_LEVELS
607 function logger($msg, $level = 0) {
609 global $LOGGER_LEVELS;
611 // turn off logger in install mode
613 $a->mode == App::MODE_INSTALL
619 $debugging = Config::get('system','debugging');
620 $logfile = Config::get('system','logfile');
621 $loglevel = intval(Config::get('system','loglevel'));
626 || $level > $loglevel
631 if (count($LOGGER_LEVELS) == 0) {
632 foreach (get_defined_constants() as $k => $v) {
633 if (substr($k, 0, 7) == "LOGGER_") {
634 $LOGGER_LEVELS[$v] = substr($k, 7, 7);
639 $process_id = session_id();
641 if ($process_id == '') {
642 $process_id = get_app()->process_id;
645 $callers = debug_backtrace();
647 if (count($callers) > 1) {
648 $function = $callers[1]['function'];
653 $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
654 DateTimeFormat::utcNow(DateTimeFormat::ATOM),
656 $LOGGER_LEVELS[$level],
657 basename($callers[0]['file']),
663 $stamp1 = microtime(true);
664 @file_put_contents($logfile, $logline, FILE_APPEND);
665 $a->save_timestamp($stamp1, "file");
669 * @brief An alternative logger for development.
670 * Works largely as logger() but allows developers
671 * to isolate particular elements they are targetting
672 * personally without background noise
675 * LOGGER_NORMAL (default)
682 * @global array $LOGGER_LEVELS
687 function dlogger($msg, $level = 0) {
690 // turn off logger in install mode
692 $a->mode == App::MODE_INSTALL
698 $logfile = Config::get('system', 'dlogfile');
703 $dlogip = Config::get('system', 'dlogip');
704 if (!is_null($dlogip) && $_SERVER['REMOTE_ADDR'] != $dlogip) {
708 if (count($LOGGER_LEVELS) == 0) {
709 foreach (get_defined_constants() as $k => $v) {
710 if (substr($k, 0, 7) == "LOGGER_") {
711 $LOGGER_LEVELS[$v] = substr($k, 7, 7);
716 $process_id = session_id();
718 if ($process_id == '') {
719 $process_id = get_app()->process_id;
722 $callers = debug_backtrace();
723 $logline = sprintf("%s@\t%s:\t%s:\t%s\t%s\t%s\n",
724 DateTimeFormat::utcNow(),
726 basename($callers[0]['file']),
728 $callers[1]['function'],
732 $stamp1 = microtime(true);
733 @file_put_contents($logfile, $logline, FILE_APPEND);
734 $a->save_timestamp($stamp1, "file");
739 * Compare activity uri. Knows about activity namespace.
741 * @param string $haystack
742 * @param string $needle
745 function activity_match($haystack,$needle) {
746 return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
751 * @brief Pull out all #hashtags and @person tags from $string.
753 * We also get @person@domain.com - which would make
754 * the regex quite complicated as tags can also
755 * end a sentence. So we'll run through our results
756 * and strip the period from any tags which end with one.
757 * Returns array of tags found, or empty array.
759 * @param string $string Post content
760 * @return array List of tag and person names
762 function get_tags($string) {
765 // Convert hashtag links to hashtags
766 $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
768 // ignore anything in a code block
769 $string = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $string);
771 // Force line feeds at bbtags
772 $string = str_replace(['[', ']'], ["\n[", "]\n"], $string);
774 // ignore anything in a bbtag
775 $string = preg_replace('/\[(.*?)\]/sm', '', $string);
777 // Match full names against @tags including the space between first and last
778 // We will look these up afterward to see if they are full names or not recognisable.
780 if (preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/', $string, $matches)) {
781 foreach ($matches[1] as $match) {
782 if (strstr($match, ']')) {
783 // we might be inside a bbcode color tag - leave it alone
786 if (substr($match, -1, 1) === '.') {
787 $ret[] = substr($match, 0, -1);
794 // Otherwise pull out single word tags. These can be @nickname, @first_last
797 if (preg_match_all('/([!#@][^\^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/', $string, $matches)) {
798 foreach ($matches[1] as $match) {
799 if (strstr($match, ']')) {
800 // we might be inside a bbcode color tag - leave it alone
803 if (substr($match, -1, 1) === '.') {
804 $match = substr($match,0,-1);
806 // ignore strictly numeric tags like #1
807 if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
810 // try not to catch url fragments
811 if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
822 * quick and dirty quoted_printable encoding
828 return str_replace("%", "=", rawurlencode($s));
833 * Get html for contact block.
835 * @template contact_block.tpl
836 * @hook contact_block_end (contacts=>array, output=>string)
839 function contact_block() {
843 $shown = PConfig::get($a->profile['uid'], 'system', 'display_friend_count', 24);
848 if (!is_array($a->profile) || $a->profile['hide-friends']) {
851 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
852 WHERE `uid` = %d AND NOT `self` AND NOT `blocked`
853 AND NOT `pending` AND NOT `hidden` AND NOT `archive`
854 AND `network` IN ('%s', '%s', '%s')",
855 intval($a->profile['uid']),
857 dbesc(NETWORK_OSTATUS),
858 dbesc(NETWORK_DIASPORA)
860 if (DBM::is_result($r)) {
861 $total = intval($r[0]['total']);
864 $contacts = L10n::t('No contacts');
867 // Splitting the query in two parts makes it much faster
868 $r = q("SELECT `id` FROM `contact`
869 WHERE `uid` = %d AND NOT `self` AND NOT `blocked`
870 AND NOT `pending` AND NOT `hidden` AND NOT `archive`
871 AND `network` IN ('%s', '%s', '%s')
872 ORDER BY RAND() LIMIT %d",
873 intval($a->profile['uid']),
875 dbesc(NETWORK_OSTATUS),
876 dbesc(NETWORK_DIASPORA),
879 if (DBM::is_result($r)) {
881 foreach ($r AS $contact) {
882 $contacts[] = $contact["id"];
884 $r = q("SELECT `id`, `uid`, `addr`, `url`, `name`, `thumb`, `network` FROM `contact` WHERE `id` IN (%s)",
885 dbesc(implode(",", $contacts)));
887 if (DBM::is_result($r)) {
888 $contacts = L10n::tt('%d Contact', '%d Contacts', $total);
890 foreach ($r as $rr) {
891 $micropro[] = micropro($rr, true, 'mpfriend');
897 $tpl = get_markup_template('contact_block.tpl');
898 $o = replace_macros($tpl, [
899 '$contacts' => $contacts,
900 '$nickname' => $a->profile['nickname'],
901 '$viewcontacts' => L10n::t('View Contacts'),
902 '$micropro' => $micropro,
905 $arr = ['contacts' => $r, 'output' => $o];
907 Addon::callHooks('contact_block_end', $arr);
914 * @brief Format contacts as picture links or as texxt links
916 * @param array $contact Array with contacts which contains an array with
917 * int 'id' => The ID of the contact
918 * int 'uid' => The user ID of the user who owns this data
919 * string 'name' => The name of the contact
920 * string 'url' => The url to the profile page of the contact
921 * string 'addr' => The webbie of the contact (e.g.) username@friendica.com
922 * string 'network' => The network to which the contact belongs to
923 * string 'thumb' => The contact picture
924 * string 'click' => js code which is performed when clicking on the contact
925 * @param boolean $redirect If true try to use the redir url if it's possible
926 * @param string $class CSS class for the
927 * @param boolean $textmode If true display the contacts as text links
928 * if false display the contacts as picture links
930 * @return string Formatted html
932 function micropro($contact, $redirect = false, $class = '', $textmode = false) {
934 // Use the contact URL if no address is available
935 if (!x($contact, "addr")) {
936 $contact["addr"] = $contact["url"];
939 $url = $contact['url'];
944 $url = Contact::magicLink($contact['url']);
945 if (strpos($url, 'redir/') === 0) {
946 $sparkle = ' sparkle';
950 // If there is some js available we don't need the url
951 if (x($contact, 'click')) {
955 return replace_macros(get_markup_template(($textmode)?'micropro_txt.tpl':'micropro_img.tpl'),[
956 '$click' => defaults($contact, 'click', ''),
959 '$photo' => proxy_url($contact['thumb'], false, PROXY_SIZE_THUMB),
960 '$name' => $contact['name'],
961 'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
962 '$parkle' => $sparkle,
971 * @param string $s Search query.
972 * @param string $id HTML id
973 * @param string $url Search url.
974 * @param bool $save Show save search button.
975 * @param bool $aside Display the search widgit aside.
977 * @return string Formatted HTML.
979 function search($s, $id = 'search-box', $url = 'search', $save = false, $aside = true)
983 if (strpos($s, '#') === 0) {
986 $save_label = $mode === 'text' ? L10n::t('Save') : L10n::t('Follow');
989 '$s' => htmlspecialchars($s),
991 '$action_url' => $url,
992 '$search_label' => L10n::t('Search'),
993 '$save_label' => $save_label,
994 '$savedsearch' => local_user() && Feature::isEnabled(local_user(),'savedsearch'),
995 '$search_hint' => L10n::t('@name, !forum, #tags, content'),
1000 $values['$searchoption'] = [
1001 L10n::t("Full Text"),
1003 L10n::t("Contacts")];
1005 if (Config::get('system','poco_local_search')) {
1006 $values['$searchoption'][] = L10n::t("Forums");
1010 return replace_macros(get_markup_template('searchbox.tpl'), $values);
1014 * @brief Check for a valid email string
1016 * @param string $email_address
1019 function valid_email($email_address)
1021 return preg_match('/^[_a-zA-Z0-9\-\+]+(\.[_a-zA-Z0-9\-\+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/', $email_address);
1026 * Replace naked text hyperlink with HTML formatted hyperlink
1030 function linkify($s) {
1031 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="_blank">$1</a>', $s);
1032 $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$s);
1040 * @return array index is present tense verb
1041 * value is array containing past tense verb, translation of present, translation of past
1042 * @hook poke_verbs pokes array
1044 function get_poke_verbs() {
1046 // index is present tense verb
1047 // value is array containing past tense verb, translation of present, translation of past
1050 'poke' => ['poked', L10n::t('poke'), L10n::t('poked')],
1051 'ping' => ['pinged', L10n::t('ping'), L10n::t('pinged')],
1052 'prod' => ['prodded', L10n::t('prod'), L10n::t('prodded')],
1053 'slap' => ['slapped', L10n::t('slap'), L10n::t('slapped')],
1054 'finger' => ['fingered', L10n::t('finger'), L10n::t('fingered')],
1055 'rebuff' => ['rebuffed', L10n::t('rebuff'), L10n::t('rebuffed')],
1057 Addon::callHooks('poke_verbs', $arr);
1062 * @brief Translate days and months names.
1064 * @param string $s String with day or month name.
1065 * @return string Translated string.
1067 function day_translate($s) {
1068 $ret = str_replace(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
1069 [L10n::t('Monday'), L10n::t('Tuesday'), L10n::t('Wednesday'), L10n::t('Thursday'), L10n::t('Friday'), L10n::t('Saturday'), L10n::t('Sunday')],
1072 $ret = str_replace(['January','February','March','April','May','June','July','August','September','October','November','December'],
1073 [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')],
1080 * @brief Translate short days and months names.
1082 * @param string $s String with short day or month name.
1083 * @return string Translated string.
1085 function day_short_translate($s) {
1086 $ret = str_replace(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
1087 [L10n::t('Mon'), L10n::t('Tue'), L10n::t('Wed'), L10n::t('Thu'), L10n::t('Fri'), L10n::t('Sat'), L10n::t('Sun')],
1089 $ret = str_replace(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov','Dec'],
1090 [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')],
1099 * @param string $url
1102 function normalise_link($url) {
1103 $ret = str_replace(['https:', '//www.'], ['http:', '//'], $url);
1104 return rtrim($ret,'/');
1109 * Compare two URLs to see if they are the same, but ignore
1110 * slight but hopefully insignificant differences such as if one
1111 * is https and the other isn't, or if one is www.something and
1112 * the other isn't - and also ignore case differences.
1114 * @param string $a first url
1115 * @param string $b second url
1116 * @return boolean True if the URLs match, otherwise False
1119 function link_compare($a, $b) {
1120 return (strcasecmp(normalise_link($a), normalise_link($b)) === 0);
1125 * @brief Find any non-embedded images in private items and add redir links to them
1128 * @param array &$item The field array of an item row
1130 function redir_private_images($a, &$item)
1133 $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
1135 foreach ($matches as $mtch) {
1136 if (strpos($mtch[1], '/redir') !== false) {
1140 if ((local_user() == $item['uid']) && ($item['private'] != 0) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) {
1141 $img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
1142 $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
1149 * Sets the "rendered-html" field of the provided item
1151 * Body is preserved to avoid side-effects as we modify it just-in-time for spoilers and private image links
1153 * @param array $item
1154 * @param bool $update
1156 * @todo Remove reference, simply return "rendered-html" and "rendered-hash"
1158 function put_item_in_cache(&$item, $update = false)
1160 $body = $item["body"];
1162 $rendered_hash = defaults($item, 'rendered-hash', '');
1163 $rendered_html = defaults($item, 'rendered-html', '');
1165 if ($rendered_hash == ''
1166 || $rendered_html == ""
1167 || $rendered_hash != hash("md5", $item["body"])
1168 || Config::get("system", "ignore_cache")
1171 redir_private_images($a, $item);
1173 $item["rendered-html"] = prepare_text($item["body"]);
1174 $item["rendered-hash"] = hash("md5", $item["body"]);
1176 // Force an update if the generated values differ from the existing ones
1177 if ($rendered_hash != $item["rendered-hash"]) {
1181 // Only compare the HTML when we forcefully ignore the cache
1182 if (Config::get("system", "ignore_cache") && ($rendered_html != $item["rendered-html"])) {
1186 if ($update && !empty($item["id"])) {
1187 Item::update(['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]],
1188 ['id' => $item["id"]]);
1192 $item["body"] = $body;
1196 * @brief Given an item array, convert the body element from bbcode to html and add smilie icons.
1197 * If attach is true, also add icons for item attachments.
1199 * @param array $item
1200 * @param boolean $attach
1201 * @param boolean $is_preview
1202 * @return string item body html
1203 * @hook prepare_body_init item array before any work
1204 * @hook prepare_body_content_filter ('item'=>item array, 'filter_reasons'=>string array) before first bbcode to html
1205 * @hook prepare_body ('item'=>item array, 'html'=>body string, 'is_preview'=>boolean, 'filter_reasons'=>string array) after first bbcode to html
1206 * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
1208 function prepare_body(array &$item, $attach = false, $is_preview = false)
1211 Addon::callHooks('prepare_body_init', $item);
1213 // In order to provide theme developers more possibilities, event items
1214 // are treated differently.
1215 if ($item['object-type'] === ACTIVITY_OBJ_EVENT && isset($item['event-id'])) {
1216 $ev = Event::getItemHTML($item);
1220 $tags = \Friendica\Model\Term::populateTagsFromItem($item);
1222 $item['tags'] = $tags['tags'];
1223 $item['hashtags'] = $tags['hashtags'];
1224 $item['mentions'] = $tags['mentions'];
1226 // Compile eventual content filter reasons
1227 $filter_reasons = [];
1228 if (!$is_preview && public_contact() != $item['author-id']) {
1229 if (!empty($item['content-warning']) && (!local_user() || !PConfig::get(local_user(), 'system', 'disable_cw', false))) {
1230 $filter_reasons[] = L10n::t('Content warning: %s', $item['content-warning']);
1235 'filter_reasons' => $filter_reasons
1237 Addon::callHooks('prepare_body_content_filter', $hook_data);
1238 $filter_reasons = $hook_data['filter_reasons'];
1242 // Update the cached values if there is no "zrl=..." on the links.
1243 $update = (!local_user() && !remote_user() && ($item["uid"] == 0));
1245 // Or update it if the current viewer is the intented viewer.
1246 if (($item["uid"] == local_user()) && ($item["uid"] != 0)) {
1250 put_item_in_cache($item, $update);
1251 $s = $item["rendered-html"];
1256 'preview' => $is_preview,
1257 'filter_reasons' => $filter_reasons
1259 Addon::callHooks('prepare_body', $hook_data);
1260 $s = $hook_data['html'];
1264 // Replace the blockquotes with quotes that are used in mails.
1265 $mailquote = '<blockquote type="cite" class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">';
1266 $s = str_replace(['<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'], [$mailquote, $mailquote, $mailquote], $s);
1273 preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $item['attach'], $matches, PREG_SET_ORDER);
1274 foreach ($matches as $mtch) {
1277 $the_url = Contact::magicLinkById($item['author-id'], $mtch[1]);
1279 if (strpos($mime, 'video') !== false) {
1282 $a->page['htmlhead'] .= replace_macros(get_markup_template('videos_head.tpl'), [
1283 '$baseurl' => System::baseUrl(),
1285 $a->page['end'] .= replace_macros(get_markup_template('videos_end.tpl'), [
1286 '$baseurl' => System::baseUrl(),
1290 $url_parts = explode('/', $the_url);
1291 $id = end($url_parts);
1292 $as .= replace_macros(get_markup_template('video_top.tpl'), [
1295 'title' => L10n::t('View Video'),
1302 $filetype = strtolower(substr($mime, 0, strpos($mime, '/')));
1304 $filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1));
1305 $filesubtype = str_replace('.', '-', $filesubtype);
1308 $filesubtype = 'unkn';
1311 $title = escape_tags(trim(!empty($mtch[4]) ? $mtch[4] : $mtch[1]));
1312 $title .= ' ' . $mtch[2] . ' ' . L10n::t('bytes');
1314 $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
1315 $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
1319 $s .= '<div class="body-attach">'.$as.'<div class="clear"></div></div>';
1323 if (strpos($s, '<div class="map">') !== false && x($item, 'coord')) {
1324 $x = Map::byCoordinates(trim($item['coord']));
1326 $s = preg_replace('/\<div class\=\"map\"\>/', '$0' . $x, $s);
1331 // Look for spoiler.
1332 $spoilersearch = '<blockquote class="spoiler">';
1334 // Remove line breaks before the spoiler.
1335 while ((strpos($s, "\n" . $spoilersearch) !== false)) {
1336 $s = str_replace("\n" . $spoilersearch, $spoilersearch, $s);
1338 while ((strpos($s, "<br />" . $spoilersearch) !== false)) {
1339 $s = str_replace("<br />" . $spoilersearch, $spoilersearch, $s);
1342 while ((strpos($s, $spoilersearch) !== false)) {
1343 $pos = strpos($s, $spoilersearch);
1344 $rnd = random_string(8);
1345 $spoilerreplace = '<br /> <span id="spoiler-wrap-' . $rnd . '" class="spoiler-wrap fakelink" onclick="openClose(\'spoiler-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
1346 '<blockquote class="spoiler" id="spoiler-' . $rnd . '" style="display: none;">';
1347 $s = substr($s, 0, $pos) . $spoilerreplace . substr($s, $pos + strlen($spoilersearch));
1350 // Look for quote with author.
1351 $authorsearch = '<blockquote class="author">';
1353 while ((strpos($s, $authorsearch) !== false)) {
1354 $pos = strpos($s, $authorsearch);
1355 $rnd = random_string(8);
1356 $authorreplace = '<br /> <span id="author-wrap-' . $rnd . '" class="author-wrap fakelink" onclick="openClose(\'author-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
1357 '<blockquote class="author" id="author-' . $rnd . '" style="display: block;">';
1358 $s = substr($s, 0, $pos) . $authorreplace . substr($s, $pos + strlen($authorsearch));
1361 // Replace friendica image url size with theme preference.
1362 if (x($a->theme_info, 'item_image_size')){
1363 $ps = $a->theme_info['item_image_size'];
1364 $s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s);
1367 $s = apply_content_filter($s, $filter_reasons);
1369 $hook_data = ['item' => $item, 'html' => $s];
1370 Addon::callHooks('prepare_body_final', $hook_data);
1372 return $hook_data['html'];
1376 * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
1378 * Reasons are expected to have been translated already.
1380 * @param string $html
1381 * @param array $reasons
1384 function apply_content_filter($html, array $reasons)
1386 if (count($reasons)) {
1387 $tpl = get_markup_template('wall/content_filter.tpl');
1388 $html = replace_macros($tpl, [
1389 '$reasons' => $reasons,
1390 '$rnd' => random_string(8),
1391 '$openclose' => L10n::t('Click to open/close'),
1400 * @brief Given a text string, convert from bbcode to html and add smilie icons.
1402 * @param string $text String with bbcode.
1403 * @return string Formattet HTML.
1405 function prepare_text($text) {
1406 if (stristr($text, '[nosmile]')) {
1407 $s = BBCode::convert($text);
1409 $s = Smilies::replace(BBCode::convert($text));
1416 * return array with details for categories and folders for an item
1418 * @param array $item
1422 * [ // categories array
1424 * 'name': 'category name',
1425 * 'removeurl': 'url to remove this category',
1426 * 'first': 'is the first in this array? true/false',
1427 * 'last': 'is the last in this array? true/false',
1433 * 'name': 'folder name',
1434 * 'removeurl': 'url to remove this folder',
1435 * 'first': 'is the first in this array? true/false',
1436 * 'last': 'is the last in this array? true/false',
1442 function get_cats_and_terms($item)
1449 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
1451 foreach ($matches as $mtch) {
1453 'name' => xmlify(file_tag_decode($mtch[1])),
1455 'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . xmlify(file_tag_decode($mtch[1])):""),
1463 if (count($categories)) {
1464 $categories[count($categories) - 1]['last'] = true;
1467 if (local_user() == $item['uid']) {
1470 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
1472 foreach ($matches as $mtch) {
1474 'name' => xmlify(file_tag_decode($mtch[1])),
1476 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . xmlify(file_tag_decode($mtch[1])) : ""),
1485 if (count($folders)) {
1486 $folders[count($folders) - 1]['last'] = true;
1489 return [$categories, $folders];
1494 * get private link for item
1495 * @param array $item
1496 * @return boolean|array False if item has not plink, otherwise array('href'=>plink url, 'title'=>translated title)
1498 function get_plink($item) {
1501 if ($a->user['nickname'] != "") {
1503 //'href' => "display/" . $a->user['nickname'] . "/" . $item['id'],
1504 'href' => "display/" . $item['guid'],
1505 'orig' => "display/" . $item['guid'],
1506 'title' => L10n::t('View on separate page'),
1507 'orig_title' => L10n::t('view on separate page'),
1510 if (x($item, 'plink')) {
1511 $ret["href"] = $a->remove_baseurl($item['plink']);
1512 $ret["title"] = L10n::t('link to source');
1515 } elseif (x($item, 'plink') && ($item['private'] != 1)) {
1517 'href' => $item['plink'],
1518 'orig' => $item['plink'],
1519 'title' => L10n::t('link to source'),
1530 * replace html amp entity with amp char
1534 function unamp($s) {
1535 return str_replace('&', '&', $s);
1540 * return number of bytes in size (K, M, G)
1541 * @param string $size_str
1544 function return_bytes($size_str) {
1545 switch (substr ($size_str, -1)) {
1546 case 'M': case 'm': return (int)$size_str * 1048576;
1547 case 'K': case 'k': return (int)$size_str * 1024;
1548 case 'G': case 'g': return (int)$size_str * 1073741824;
1549 default: return $size_str;
1557 function generate_user_guid() {
1560 $guid = System::createGUID(32);
1561 $x = q("SELECT `uid` FROM `user` WHERE `guid` = '%s' LIMIT 1",
1564 if (!DBM::is_result($x)) {
1567 } while ($found == true);
1575 * @param boolean $strip_padding
1578 function base64url_encode($s, $strip_padding = false) {
1580 $s = strtr(base64_encode($s), '+/', '-_');
1582 if ($strip_padding) {
1583 $s = str_replace('=','',$s);
1593 function base64url_decode($s) {
1596 logger('base64url_decode: illegal input: ' . print_r(debug_backtrace(), true));
1601 * // Placeholder for new rev of salmon which strips base64 padding.
1602 * // PHP base64_decode handles the un-padded input without requiring this step
1603 * // Uncomment if you find you need it.
1606 * if (!strpos($s,'=')) {
1616 return base64_decode(strtr($s,'-_','+/'));
1621 * return div element with class 'clear'
1625 function cleardiv() {
1626 return '<div class="clear"></div>';
1630 function bb_translate_video($s) {
1633 $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
1635 foreach ($matches as $mtch) {
1636 if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
1637 $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
1638 } elseif (stristr($mtch[1], 'vimeo')) {
1639 $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
1646 function html2bb_video($s) {
1648 $s = preg_replace('#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
1649 '[youtube]$2[/youtube]', $s);
1651 $s = preg_replace('#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
1652 '[youtube]$2[/youtube]', $s);
1654 $s = preg_replace('#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
1655 '[vimeo]$2[/vimeo]', $s);
1661 * apply xmlify() to all values of array $val, recursively
1665 function array_xmlify($val){
1666 if (is_bool($val)) {
1667 return $val?"true":"false";
1668 } elseif (is_array($val)) {
1669 return array_map('array_xmlify', $val);
1671 return xmlify((string) $val);
1676 * transform link href and img src from relative to absolute
1678 * @param string $text
1679 * @param string $base base url
1682 function reltoabs($text, $base) {
1687 $base = rtrim($base,'/');
1689 $base2 = $base . "/";
1692 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
1693 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
1694 $text = preg_replace($pattern, $replace, $text);
1696 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
1697 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
1698 $text = preg_replace($pattern, $replace, $text);
1701 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
1702 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
1703 $text = preg_replace($pattern, $replace, $text);
1705 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
1706 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
1707 $text = preg_replace($pattern, $replace, $text);
1715 * get translated item type
1717 * @param array $itme
1720 function item_post_type($item) {
1721 if (!empty($item['event-id'])) {
1722 return L10n::t('event');
1723 } elseif (!empty($item['resource-id'])) {
1724 return L10n::t('photo');
1725 } elseif (!empty($item['verb']) && $item['verb'] !== ACTIVITY_POST) {
1726 return L10n::t('activity');
1727 } elseif ($item['id'] != $item['parent']) {
1728 return L10n::t('comment');
1731 return L10n::t('post');
1734 // post categories and "save to file" use the same item.file table for storage.
1735 // We will differentiate the different uses by wrapping categories in angle brackets
1736 // and save to file categories in square brackets.
1737 // To do this we need to escape these characters if they appear in our tag.
1739 function file_tag_encode($s) {
1740 return str_replace(['<','>','[',']'],['%3c','%3e','%5b','%5d'],$s);
1743 function file_tag_decode($s) {
1744 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
1747 function file_tag_file_query($table,$s,$type = 'file') {
1749 if ($type == 'file') {
1750 $str = preg_quote('[' . str_replace('%', '%%', file_tag_encode($s)) . ']');
1752 $str = preg_quote('<' . str_replace('%', '%%', file_tag_encode($s)) . '>');
1754 return " AND " . (($table) ? dbesc($table) . '.' : '') . "file regexp '" . dbesc($str) . "' ";
1757 // ex. given music,video return <music><video> or [music][video]
1758 function file_tag_list_to_file($list, $type = 'file') {
1760 if (strlen($list)) {
1761 $list_array = explode(",",$list);
1762 if ($type == 'file') {
1770 foreach ($list_array as $item) {
1771 if (strlen($item)) {
1772 $tag_list .= $lbracket . file_tag_encode(trim($item)) . $rbracket;
1779 // ex. given <music><video>[friends], return music,video or friends
1780 function file_tag_file_to_list($file, $type = 'file') {
1783 if ($type == 'file') {
1784 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
1786 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
1789 foreach ($matches as $mtch) {
1790 if (strlen($list)) {
1793 $list .= file_tag_decode($mtch[1]);
1800 function file_tag_update_pconfig($uid, $file_old, $file_new, $type = 'file') {
1801 // $file_old - categories previously associated with an item
1802 // $file_new - new list of categories for an item
1804 if (!intval($uid)) {
1806 } elseif ($file_old == $file_new) {
1810 $saved = PConfig::get($uid, 'system', 'filetags');
1811 if (strlen($saved)) {
1812 if ($type == 'file') {
1815 $termtype = TERM_FILE;
1819 $termtype = TERM_CATEGORY;
1822 $filetags_updated = $saved;
1824 // check for new tags to be added as filetags in pconfig
1826 $check_new_tags = explode(",",file_tag_file_to_list($file_new,$type));
1828 foreach ($check_new_tags as $tag) {
1829 if (!stristr($saved,$lbracket . file_tag_encode($tag) . $rbracket)) {
1834 $filetags_updated .= file_tag_list_to_file(implode(",",$new_tags),$type);
1836 // check for deleted tags to be removed from filetags in pconfig
1838 $check_deleted_tags = explode(",",file_tag_file_to_list($file_old,$type));
1840 foreach ($check_deleted_tags as $tag) {
1841 if (!stristr($file_new,$lbracket . file_tag_encode($tag) . $rbracket)) {
1842 $deleted_tags[] = $tag;
1846 foreach ($deleted_tags as $key => $tag) {
1847 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
1849 intval(TERM_OBJ_POST),
1853 if (DBM::is_result($r)) {
1854 unset($deleted_tags[$key]);
1856 $filetags_updated = str_replace($lbracket . file_tag_encode($tag) . $rbracket,'',$filetags_updated);
1860 if ($saved != $filetags_updated) {
1861 PConfig::set($uid, 'system', 'filetags', $filetags_updated);
1864 } elseif (strlen($file_new)) {
1865 PConfig::set($uid, 'system', 'filetags', $file_new);
1870 function file_tag_save_file($uid, $item_id, $file)
1872 if (!intval($uid)) {
1876 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
1877 if (DBM::is_result($item)) {
1878 if (!stristr($item['file'],'[' . file_tag_encode($file) . ']')) {
1879 $fields = ['file' => $item['file'] . '[' . file_tag_encode($file) . ']'];
1880 Item::update($fields, ['id' => $item_id]);
1882 $saved = PConfig::get($uid, 'system', 'filetags');
1883 if (!strlen($saved) || !stristr($saved, '[' . file_tag_encode($file) . ']')) {
1884 PConfig::set($uid, 'system', 'filetags', $saved . '[' . file_tag_encode($file) . ']');
1886 info(L10n::t('Item filed'));
1891 function file_tag_unsave_file($uid, $item_id, $file, $cat = false)
1893 if (!intval($uid)) {
1898 $pattern = '<' . file_tag_encode($file) . '>' ;
1899 $termtype = TERM_CATEGORY;
1901 $pattern = '[' . file_tag_encode($file) . ']' ;
1902 $termtype = TERM_FILE;
1905 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
1906 if (!DBM::is_result($item)) {
1910 $fields = ['file' => str_replace($pattern,'',$item['file'])];
1911 Item::update($fields, ['id' => $item_id]);
1913 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
1915 intval(TERM_OBJ_POST),
1919 if (!DBM::is_result($r)) {
1920 $saved = PConfig::get($uid, 'system', 'filetags');
1921 PConfig::set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));
1927 function normalise_openid($s) {
1928 return trim(str_replace(['http://', 'https://'], ['', ''], $s), '/');
1932 function undo_post_tagging($s) {
1934 $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
1936 foreach ($matches as $mtch) {
1937 if (in_array($mtch[1], ['!', '@'])) {
1938 $contact = Contact::getDetailsByURL($mtch[2]);
1939 $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
1941 $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
1947 function protect_sprintf($s) {
1948 return str_replace('%', '%%', $s);
1951 /// @TODO Rewrite this
1952 function is_a_date_arg($s) {
1958 if ($i <= $y + 1 && strpos($s, '-') == 4) {
1959 $m = intval(substr($s, 5));
1961 if ($m > 0 && $m <= 12) {
1971 * remove intentation from a text
1973 function deindent($text, $chr = "[\t ]", $count = NULL) {
1974 $lines = explode("\n", $text);
1976 if (is_null($count)) {
1979 while ($k < count($lines) && strlen($lines[$k]) == 0) {
1982 preg_match("|^" . $chr . "*|", $lines[$k], $m);
1983 $count = strlen($m[0]);
1986 for ($k = 0; $k < count($lines); $k++) {
1987 $lines[$k] = preg_replace("|^" . $chr . "{" . $count . "}|", "", $lines[$k]);
1990 return implode("\n", $lines);
1993 function formatBytes($bytes, $precision = 2) {
1994 $units = ['B', 'KB', 'MB', 'GB', 'TB'];
1996 $bytes = max($bytes, 0);
1997 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
1998 $pow = min($pow, count($units) - 1);
2000 $bytes /= pow(1024, $pow);
2002 return round($bytes, $precision) . ' ' . $units[$pow];
2006 * @brief translate and format the networkname of a contact
2008 * @param string $network
2009 * Networkname of the contact (e.g. dfrn, rss and so on)
2014 function format_network_name($network, $url = 0) {
2015 if ($network != "") {
2017 $network_name = '<a href="'.$url.'">'.ContactSelector::networkToName($network, $url)."</a>";
2019 $network_name = ContactSelector::networkToName($network);
2022 return $network_name;
2027 * @brief Syntax based code highlighting for popular languages.
2028 * @param string $s Code block
2029 * @param string $lang Programming language
2030 * @return string Formated html
2032 function text_highlight($s, $lang) {
2033 if ($lang === 'js') {
2034 $lang = 'javascript';
2037 if ($lang === 'bash') {
2041 // @TODO: Replace Text_Highlighter_Renderer_Html by scrivo/highlight.php
2043 // Autoload the library to make constants available
2044 class_exists('Text_Highlighter_Renderer_Html');
2047 'numbers' => HL_NUMBERS_LI,
2052 $s = trim(html_entity_decode($s, ENT_COMPAT));
2053 $s = str_replace(' ', "\t", $s);
2056 * The highlighter library insists on an opening php tag for php code blocks. If
2057 * it isn't present, nothing is highlighted. So we're going to see if it's present.
2058 * If not, we'll add it, and then quietly remove it after we get the processed output back.
2060 if ($lang === 'php' && strpos($s, '<?php') !== 0) {
2061 $s = '<?php' . "\n" . $s;
2065 $renderer = new Text_Highlighter_Renderer_Html($options);
2066 $factory = new Text_Highlighter();
2067 $hl = $factory->factory($lang);
2068 $hl->setRenderer($renderer);
2069 $o = $hl->highlight($s);
2070 $o = str_replace("\n", '', $o);
2073 $b = substr($o, 0, strpos($o, '<li>'));
2074 $e = substr($o, strpos($o, '</li>'));
2078 return '<code>' . $o . '</code>';