3 require_once("include/template_processor.php");
4 require_once("include/friendica_smarty.php");
5 require_once("include/Smilies.php");
6 require_once("include/map.php");
7 require_once("mod/proxy.php");
10 if(! function_exists('replace_macros')) {
12 * This is our template processor
14 * @param string|FriendicaSmarty $s the string requiring macro substitution,
15 * or an instance of FriendicaSmarty
16 * @param array $r key value pairs (search => replace)
17 * @return string substituted string
19 function replace_macros($s,$r) {
21 $stamp1 = microtime(true);
25 // pass $baseurl to all templates
26 $r['$baseurl'] = $a->get_baseurl();
29 $t = $a->template_engine();
31 $output = $t->replace_macros($s,$r);
32 } catch (Exception $e) {
33 echo "<pre><b>".__function__."</b>: ".$e->getMessage()."</pre>"; killme();
36 $a->save_timestamp($stamp1, "rendering");
42 // random string, there are 86 characters max in text mode, 128 for hex
45 define('RANDOM_STRING_HEX', 0x00 );
46 define('RANDOM_STRING_TEXT', 0x01 );
48 if(! function_exists('random_string')) {
49 function random_string($size = 64,$type = RANDOM_STRING_HEX) {
50 // generate a bit of entropy and run it through the whirlpool
51 $s = hash('whirlpool', (string) rand() . uniqid(rand(),true) . (string) rand(),(($type == RANDOM_STRING_TEXT) ? true : false));
52 $s = (($type == RANDOM_STRING_TEXT) ? str_replace("\n","",base64url_encode($s,true)) : $s);
53 return(substr($s,0,$size));
56 if(! function_exists('notags')) {
58 * This is our primary input filter.
60 * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
61 * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
62 * after cleansing, and angle chars with the high bit set could get through as markup.
64 * This is now disabled because it was interfering with some legitimate unicode sequences
65 * and hopefully there aren't a lot of those browsers left.
67 * Use this on any text input where angle chars are not valid or permitted
68 * They will be replaced with safer brackets. This may be filtered further
69 * if these are not allowed either.
71 * @param string $string Input string
72 * @return string Filtered string
74 function notags($string) {
76 return(str_replace(array("<",">"), array('[',']'), $string));
78 // High-bit filter no longer used
79 // return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
84 if(! function_exists('escape_tags')) {
86 * use this on "body" or "content" input where angle chars shouldn't be removed,
87 * and allow them to be safely displayed.
88 * @param string $string
91 function escape_tags($string) {
93 return(htmlspecialchars($string, ENT_COMPAT, 'UTF-8', false));
97 // generate a string that's random, but usually pronounceable.
98 // used to generate initial passwords
100 if(! function_exists('autoname')) {
102 * generate a string that's random, but usually pronounceable.
103 * used to generate initial passwords
107 function autoname($len) {
112 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
113 if(mt_rand(0,5) == 4)
131 's','sc','sh','sm','sp','st',
139 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
140 'nd','ng','nk','nt','rn','rp','rt');
142 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
143 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
145 $start = mt_rand(0,2);
153 for ($x = 0; $x < $len; $x ++) {
154 $r = mt_rand(0,count($table) - 1);
157 if($table == $vowels)
158 $table = array_merge($cons,$midcons);
164 $word = substr($word,0,$len);
166 foreach($noend as $noe) {
167 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
168 $word = substr($word,0,-1);
172 if(substr($word,-1) == 'q')
173 $word = substr($word,0,-1);
178 // escape text ($str) for XML transport
179 // returns escaped text.
181 if(! function_exists('xmlify')) {
183 * escape text ($str) for XML transport
185 * @return string Escaped text.
187 function xmlify($str) {
190 $len = mb_strlen($str);
191 for($x = 0; $x < $len; $x ++) {
192 $char = mb_substr($str,$x,1);
222 $buffer = mb_ereg_replace("&", "&", $str);
223 $buffer = mb_ereg_replace("'", "'", $buffer);
224 $buffer = mb_ereg_replace('"', """, $buffer);
225 $buffer = mb_ereg_replace("<", "<", $buffer);
226 $buffer = mb_ereg_replace(">", ">", $buffer);
228 $buffer = htmlspecialchars($str, ENT_QUOTES, "UTF-8");
229 $buffer = trim($buffer);
234 if(! function_exists('unxmlify')) {
237 * @param string $s xml escaped text
238 * @return string unescaped text
240 function unxmlify($s) {
241 // $ret = str_replace('&','&', $s);
242 // $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
243 /*$ret = mb_ereg_replace('&', '&', $s);
244 $ret = mb_ereg_replace(''', "'", $ret);
245 $ret = mb_ereg_replace('"', '"', $ret);
246 $ret = mb_ereg_replace('<', "<", $ret);
247 $ret = mb_ereg_replace('>', ">", $ret);
249 $ret = htmlspecialchars_decode($s, ENT_QUOTES);
253 if(! function_exists('hex2bin')) {
255 * convenience wrapper, reverse the operation "bin2hex"
259 function hex2bin($s) {
260 if(! (is_string($s) && strlen($s)))
263 if(! ctype_xdigit($s)) {
267 return(pack("H*",$s));
271 if(! function_exists('paginate_data')) {
273 * Automatica pagination data.
275 * @param App $a App instance
276 * @param int $count [optional] item count (used with alt pager)
277 * @return Array data for pagination template
279 function paginate_data(&$a, $count=null) {
280 $stripped = preg_replace('/([&?]page=[0-9]*)/','',$a->query_string);
282 $stripped = str_replace('q=','',$stripped);
283 $stripped = trim($stripped,'/');
284 $pagenum = $a->pager['page'];
286 if (($a->page_offset != "") AND !preg_match('/[?&].offset=/', $stripped))
287 $stripped .= "&offset=".urlencode($a->page_offset);
292 function _l(&$d, $name, $url, $text, $class="") {
293 if (!strpos($url, "?")) {
294 if ($pos = strpos($url, "&"))
295 $url = substr($url, 0, $pos)."?".substr($url, $pos + 1);
298 $d[$name] = array('url'=>$url, 'text'=>$text, 'class'=>$class);
301 if (!is_null($count)){
303 if($a->pager['page']>1)
304 _l($data, "prev", $url.'&page='.($a->pager['page'] - 1), t('newer'));
306 _l($data, "next", $url.'&page='.($a->pager['page'] + 1), t('older'));
309 if($a->pager['total'] > $a->pager['itemspage']) {
310 if($a->pager['page'] != 1)
311 _l($data, "prev", $url.'&page='.($a->pager['page'] - 1), t('prev'));
313 _l($data, "first", $url."&page=1", t('first'));
316 $numpages = $a->pager['total'] / $a->pager['itemspage'];
319 $numstop = $numpages;
322 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
323 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
328 for($i = $numstart; $i <= $numstop; $i++){
329 if($i == $a->pager['page'])
330 _l($pages, $i, "#", $i, "current");
332 _l($pages, $i, $url."&page=$i", $i, "n");
335 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
336 if($i == $a->pager['page'])
337 _l($pages, $i, "#", $i, "current");
339 _l($pages, $i, $url."&page=$i", $i, "n");
342 $data['pages'] = $pages;
344 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
345 _l($data, "last", $url."&page=$lastpage", t('last'));
347 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
348 _l($data, "next", $url."&page=".($a->pager['page'] + 1), t('next'));
356 if(! function_exists('paginate')) {
358 * Automatic pagination.
360 * To use, get the count of total items.
361 * Then call $a->set_pager_total($number_items);
362 * Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
363 * Then call paginate($a) after the end of the display loop to insert the pager block on the page
364 * (assuming there are enough items to paginate).
365 * When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
366 * will limit the results to the correct items for the current page.
367 * The actual page handling is then accomplished at the application layer.
369 * @param App $a App instance
370 * @return string html for pagination #FIXME remove html
372 function paginate(&$a) {
374 $data = paginate_data($a);
375 $tpl = get_markup_template("paginate.tpl");
376 return replace_macros($tpl, array("pager" => $data));
380 if(! function_exists('alt_pager')) {
383 * @param App $a App instance
385 * @return string html for pagination #FIXME remove html
387 function alt_pager(&$a, $i) {
389 $data = paginate_data($a, $i);
390 $tpl = get_markup_template("paginate.tpl");
391 return replace_macros($tpl, array('pager' => $data));
395 if(! function_exists('scroll_loader')) {
397 * Loader for infinite scrolling
398 * @return string html for loader
400 function scroll_loader() {
401 $tpl = get_markup_template("scroll_loader.tpl");
402 return replace_macros($tpl, array(
403 'wait' => t('Loading more entries...'),
404 'end' => t('The end')
408 if(! function_exists('expand_acl')) {
410 * Turn user/group ACLs stored as angle bracketed text into arrays
415 function expand_acl($s) {
416 // turn string array of angle-bracketed elements into numeric array
417 // e.g. "<1><2><3>" => array(1,2,3);
421 $t = str_replace('<','',$s);
422 $a = explode('>',$t);
425 $ret[] = intval($aa);
431 if(! function_exists('sanitise_acl')) {
433 * Wrap ACL elements in angle brackets for storage
434 * @param string $item
436 function sanitise_acl(&$item) {
438 $item = '<' . intval(notags(trim($item))) . '>';
444 if(! function_exists('perms2str')) {
446 * Convert an ACL array to a storable string
448 * Normally ACL permissions will be an array.
449 * We'll also allow a comma-separated string.
451 * @param string|array $p
454 function perms2str($p) {
459 $tmp = explode(',',$p);
462 array_walk($tmp,'sanitise_acl');
463 $ret = implode('',$tmp);
469 if(! function_exists('item_new_uri')) {
471 * generate a guaranteed unique (for this domain) item ID for ATOM
472 * safe from birthday paradox
474 * @param string $hostname
478 function item_new_uri($hostname,$uid, $guid = "") {
484 $hash = get_guid(32);
490 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
492 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
496 } while($dups == true);
500 // Generate a guaranteed unique photo ID.
501 // safe from birthday paradox
503 if(! function_exists('photo_new_resource')) {
505 * Generate a guaranteed unique photo ID.
506 * safe from birthday paradox
510 function photo_new_resource() {
514 $resource = hash('md5',uniqid(mt_rand(),true));
515 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
520 } while($found == true);
525 if(! function_exists('load_view_file')) {
528 * wrapper to load a view template, checking for alternate
529 * languages before falling back to the default
531 * @global string $lang
533 * @param string $s view name
536 function load_view_file($s) {
542 if(file_exists("$d/$lang/$b")) {
543 $stamp1 = microtime(true);
544 $content = file_get_contents("$d/$lang/$b");
545 $a->save_timestamp($stamp1, "file");
549 $theme = current_theme();
551 if(file_exists("$d/theme/$theme/$b")) {
552 $stamp1 = microtime(true);
553 $content = file_get_contents("$d/theme/$theme/$b");
554 $a->save_timestamp($stamp1, "file");
558 $stamp1 = microtime(true);
559 $content = file_get_contents($s);
560 $a->save_timestamp($stamp1, "file");
564 if(! function_exists('get_intltext_template')) {
566 * load a view template, checking for alternate
567 * languages before falling back to the default
569 * @global string $lang
570 * @param string $s view path
573 function get_intltext_template($s) {
578 if($a->theme['template_engine'] === 'smarty3')
579 $engine = "/smarty3";
584 if(file_exists("view/$lang$engine/$s")) {
585 $stamp1 = microtime(true);
586 $content = file_get_contents("view/$lang$engine/$s");
587 $a->save_timestamp($stamp1, "file");
589 } elseif(file_exists("view/en$engine/$s")) {
590 $stamp1 = microtime(true);
591 $content = file_get_contents("view/en$engine/$s");
592 $a->save_timestamp($stamp1, "file");
595 $stamp1 = microtime(true);
596 $content = file_get_contents("view$engine/$s");
597 $a->save_timestamp($stamp1, "file");
602 if(! function_exists('get_markup_template')) {
607 * @param string $root
610 function get_markup_template($s, $root = '') {
611 $stamp1 = microtime(true);
614 $t = $a->template_engine();
616 $template = $t->get_template_file($s, $root);
617 } catch (Exception $e) {
618 echo "<pre><b>".__function__."</b>: ".$e->getMessage()."</pre>"; killme();
621 $a->save_timestamp($stamp1, "file");
626 if(! function_exists("get_template_file")) {
630 * @param string $filename
631 * @param string $root
634 function get_template_file($a, $filename, $root = '') {
635 $theme = current_theme();
637 // Make sure $root ends with a slash /
638 if($root !== '' && $root[strlen($root)-1] !== '/')
641 if(file_exists("{$root}view/theme/$theme/$filename"))
642 $template_file = "{$root}view/theme/$theme/$filename";
643 elseif (x($a->theme_info,"extends") && file_exists("{$root}view/theme/{$a->theme_info["extends"]}/$filename"))
644 $template_file = "{$root}view/theme/{$a->theme_info["extends"]}/$filename";
645 elseif (file_exists("{$root}/$filename"))
646 $template_file = "{$root}/$filename";
648 $template_file = "{$root}view/$filename";
650 return $template_file;
659 if(! function_exists('attribute_contains')) {
661 * for html,xml parsing - let's say you've got
662 * an attribute foobar="class1 class2 class3"
663 * and you want to find out if it contains 'class3'.
664 * you can't use a normal sub string search because you
665 * might match 'notclass3' and a regex to do the job is
666 * possible but a bit complicated.
667 * pass the attribute string as $attr and the attribute you
668 * are looking for as $s - returns true if found, otherwise false
670 * @param string $attr attribute value
671 * @param string $s string to search
672 * @return boolean True if found, False otherwise
674 function attribute_contains($attr,$s) {
675 $a = explode(' ', $attr);
676 if(count($a) && in_array($s,$a))
681 if(! function_exists('logger')) {
682 /* setup int->string log level map */
683 $LOGGER_LEVELS = array();
687 * LOGGER_NORMAL (default)
698 function logger($msg,$level = 0) {
699 // turn off logger in install mode
702 global $LOGGER_LEVELS;
704 if(($a->module == 'install') || (! ($db && $db->connected))) return;
706 if (count($LOGGER_LEVELS)==0){
707 foreach (get_defined_constants() as $k=>$v){
708 if (substr($k,0,7)=="LOGGER_")
709 $LOGGER_LEVELS[$v] = substr($k,7,7);
713 $debugging = get_config('system','debugging');
714 $loglevel = intval(get_config('system','loglevel'));
715 $logfile = get_config('system','logfile');
717 if((! $debugging) || (! $logfile) || ($level > $loglevel))
720 $callers = debug_backtrace();
721 $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
724 $LOGGER_LEVELS[$level],
725 basename($callers[0]['file']),
727 $callers[1]['function'],
731 $stamp1 = microtime(true);
732 @file_put_contents($logfile, $logline, FILE_APPEND);
733 $a->save_timestamp($stamp1, "file");
738 if(! function_exists('activity_match')) {
740 * Compare activity uri. Knows about activity namespace.
742 * @param string $haystack
743 * @param string $needle
746 function activity_match($haystack,$needle) {
747 if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA)))
753 if(! function_exists('get_tags')) {
755 * Pull out all #hashtags and @person tags from $s;
756 * We also get @person@domain.com - which would make
757 * the regex quite complicated as tags can also
758 * end a sentence. So we'll run through our results
759 * and strip the period from any tags which end with one.
760 * Returns array of tags found, or empty array.
765 function get_tags($s) {
768 // Convert hashtag links to hashtags
769 $s = preg_replace("/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism", "#$2", $s);
771 // ignore anything in a code block
772 $s = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$s);
774 // Force line feeds at bbtags
775 $s = str_replace(array("[", "]"), array("\n[", "]\n"), $s);
777 // ignore anything in a bbtag
778 $s = preg_replace('/\[(.*?)\]/sm','',$s);
780 // Match full names against @tags including the space between first and last
781 // We will look these up afterward to see if they are full names or not recognisable.
783 if(preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/',$s,$match)) {
784 foreach($match[1] as $mtch) {
785 if(strstr($mtch,"]")) {
786 // we might be inside a bbcode color tag - leave it alone
789 if(substr($mtch,-1,1) === '.')
790 $ret[] = substr($mtch,0,-1);
796 // Otherwise pull out single word tags. These can be @nickname, @first_last
799 if(preg_match_all('/([!#@][^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/',$s,$match)) {
800 foreach($match[1] as $mtch) {
801 if(strstr($mtch,"]")) {
802 // we might be inside a bbcode color tag - leave it alone
805 if(substr($mtch,-1,1) === '.')
806 $mtch = substr($mtch,0,-1);
807 // ignore strictly numeric tags like #1
808 if((strpos($mtch,'#') === 0) && ctype_digit(substr($mtch,1)))
810 // try not to catch url fragments
811 if(strpos($s,$mtch) && preg_match('/[a-zA-z0-9\/]/',substr($s,strpos($s,$mtch)-1,1)))
822 if(! function_exists('qp')) {
824 * quick and dirty quoted_printable encoding
830 return str_replace ("%","=",rawurlencode($s));
833 if(! function_exists('contact_block')) {
835 * Get html for contact block.
837 * @template contact_block.tpl
838 * @hook contact_block_end (contacts=>array, output=>string)
841 function contact_block() {
845 $shown = get_pconfig($a->profile['uid'],'system','display_friend_count');
851 if((! is_array($a->profile)) || ($a->profile['hide-friends']))
853 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
854 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0
855 AND `hidden` = 0 AND `archive` = 0
856 AND `network` IN ('%s', '%s', '%s')",
857 intval($a->profile['uid']),
859 dbesc(NETWORK_OSTATUS),
860 dbesc(NETWORK_DIASPORA)
863 $total = intval($r[0]['total']);
866 $contacts = t('No contacts');
870 $r = q("SELECT `id`, `uid`, `addr`, `url`, `name`, `micro`, `network` FROM `contact`
871 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending`
872 AND NOT `hidden` AND NOT `archive`
873 AND `network` IN ('%s', '%s', '%s') ORDER BY RAND() LIMIT %d",
874 intval($a->profile['uid']),
876 dbesc(NETWORK_OSTATUS),
877 dbesc(NETWORK_DIASPORA),
881 $contacts = sprintf( tt('%d Contact','%d Contacts', $total),$total);
884 $micropro[] = micropro($rr,true,'mpfriend');
889 $tpl = get_markup_template('contact_block.tpl');
890 $o = replace_macros($tpl, array(
891 '$contacts' => $contacts,
892 '$nickname' => $a->profile['nickname'],
893 '$viewcontacts' => t('View Contacts'),
894 '$micropro' => $micropro,
897 $arr = array('contacts' => $r, 'output' => $o);
899 call_hooks('contact_block_end', $arr);
904 if(! function_exists('micropro')) {
907 * @param array $contact
908 * @param boolean $redirect
909 * @param string $class
910 * @param boolean $textmode
911 * @return string #FIXME: remove html
913 function micropro($contact, $redirect = false, $class = '', $textmode = false) {
916 $class = ' ' . $class;
918 if ($contact["addr"] == "")
919 $contact["addr"] = $contact["url"];
921 $url = $contact['url'];
927 $redirect_url = 'redir/' . $contact['id'];
928 if(local_user() && ($contact['uid'] == local_user()) && ($contact['network'] === NETWORK_DFRN)) {
930 $url = $redirect_url;
931 $sparkle = ' sparkle';
936 $click = ((x($contact,'click')) ? ' onclick="' . $contact['click'] . '" ' : '');
940 return '<div class="contact-block-textdiv' . $class . '"><a class="contact-block-link' . $class . $sparkle
941 . (($click) ? ' fakelink' : '') . '" '
942 . (($redir) ? ' target="redir" ' : '')
943 . (($url) ? ' href="' . $url . '"' : '') . $click
944 . '" title="' . $contact['name'] . ' [' . $contact['addr'] . ']" alt="' . $contact['name']
945 . '" >'. $contact['name'] . '</a></div>' . "\r\n";
948 return '<div class="contact-block-div' . $class . '"><a class="contact-block-link' . $class . $sparkle
949 . (($click) ? ' fakelink' : '') . '" '
950 . (($redir) ? ' target="redir" ' : '')
951 . (($url) ? ' href="' . $url . '"' : '') . $click . ' ><img class="contact-block-img' . $class . $sparkle . '" src="'
952 . proxy_url($contact['micro'], false, PROXY_SIZE_THUMB) . '" title="' . $contact['name'] . ' [' . $contact['addr'] . ']" alt="' . $contact['name']
953 . '" /></a></div>' . "\r\n";
959 if(! function_exists('search')) {
963 * @param string $s search query
964 * @param string $id html id
965 * @param string $url search url
966 * @param boolean $savedsearch show save search button
968 function search($s,$id='search-box',$url='search',$save = false, $aside = true) {
974 '$action_url' => $url,
975 '$search_label' => t('Search'),
976 '$save_label' => t('Save'),
977 '$savedsearch' => feature_enabled(local_user(),'savedsearch'),
981 $values['$searchoption'] = array(
986 if (get_config('system','poco_local_search'))
987 $values['$searchoption'][] = t("Forums");
990 return replace_macros(get_markup_template('searchbox.tpl'), $values);
993 if(! function_exists('valid_email')) {
995 * Check if $x is a valid email string
1000 function valid_email($x){
1002 // Removed because Fabio told me so.
1003 //if(get_config('system','disable_email_validation'))
1006 if(preg_match('/^[_a-zA-Z0-9\-\+]+(\.[_a-zA-Z0-9\-\+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))
1012 if(! function_exists('linkify')) {
1014 * Replace naked text hyperlink with HTML formatted hyperlink
1018 function linkify($s) {
1019 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="_blank">$1</a>', $s);
1020 $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$s);
1028 * @return array index is present tense verb
1029 value is array containing past tense verb, translation of present, translation of past
1030 * @hook poke_verbs pokes array
1032 function get_poke_verbs() {
1034 // index is present tense verb
1035 // value is array containing past tense verb, translation of present, translation of past
1038 'poke' => array( 'poked', t('poke'), t('poked')),
1039 'ping' => array( 'pinged', t('ping'), t('pinged')),
1040 'prod' => array( 'prodded', t('prod'), t('prodded')),
1041 'slap' => array( 'slapped', t('slap'), t('slapped')),
1042 'finger' => array( 'fingered', t('finger'), t('fingered')),
1043 'rebuff' => array( 'rebuffed', t('rebuff'), t('rebuffed')),
1045 call_hooks('poke_verbs', $arr);
1051 * @return array index is mood, value is translated mood
1052 * @hook mood_verbs moods array
1054 function get_mood_verbs() {
1057 'happy' => t('happy'),
1059 'mellow' => t('mellow'),
1060 'tired' => t('tired'),
1061 'perky' => t('perky'),
1062 'angry' => t('angry'),
1063 'stupefied' => t('stupified'),
1064 'puzzled' => t('puzzled'),
1065 'interested' => t('interested'),
1066 'bitter' => t('bitter'),
1067 'cheerful' => t('cheerful'),
1068 'alive' => t('alive'),
1069 'annoyed' => t('annoyed'),
1070 'anxious' => t('anxious'),
1071 'cranky' => t('cranky'),
1072 'disturbed' => t('disturbed'),
1073 'frustrated' => t('frustrated'),
1074 'motivated' => t('motivated'),
1075 'relaxed' => t('relaxed'),
1076 'surprised' => t('surprised'),
1079 call_hooks('mood_verbs', $arr);
1083 if(! function_exists('day_translate')) {
1085 * Translate days and months names
1090 function day_translate($s) {
1091 $ret = str_replace(array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
1092 array( t('Monday'), t('Tuesday'), t('Wednesday'), t('Thursday'), t('Friday'), t('Saturday'), t('Sunday')),
1095 $ret = str_replace(array('January','February','March','April','May','June','July','August','September','October','November','December'),
1096 array( t('January'), t('February'), t('March'), t('April'), t('May'), t('June'), t('July'), t('August'), t('September'), t('October'), t('November'), t('December')),
1103 if(! function_exists('normalise_link')) {
1107 * @param string $url
1110 function normalise_link($url) {
1111 $ret = str_replace(array('https:','//www.'), array('http:','//'), $url);
1112 return(rtrim($ret,'/'));
1117 if(! function_exists('link_compare')) {
1119 * Compare two URLs to see if they are the same, but ignore
1120 * slight but hopefully insignificant differences such as if one
1121 * is https and the other isn't, or if one is www.something and
1122 * the other isn't - and also ignore case differences.
1124 * @param string $a first url
1125 * @param string $b second url
1126 * @return boolean True if the URLs match, otherwise False
1129 function link_compare($a,$b) {
1130 if(strcasecmp(normalise_link($a),normalise_link($b)) === 0)
1136 if(! function_exists('redir_private_images')) {
1138 * Find any non-embedded images in private items and add redir links to them
1141 * @param array $item
1143 function redir_private_images($a, &$item) {
1146 $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
1148 //logger("redir_private_images: matches = " . print_r($matches, true));
1149 foreach($matches as $mtch) {
1150 if(strpos($mtch[1], '/redir') !== false)
1153 if((local_user() == $item['uid']) && ($item['private'] != 0) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) {
1154 //logger("redir_private_images: redir");
1155 $img_url = 'redir?f=1&quiet=1&url=' . $mtch[1] . '&conurl=' . $item['author-link'];
1156 $item['body'] = str_replace($mtch[0], "[img]".$img_url."[/img]", $item['body']);
1163 function put_item_in_cache(&$item, $update = false) {
1165 if (($item["rendered-hash"] != hash("md5", $item["body"])) OR ($item["rendered-hash"] == "") OR
1166 ($item["rendered-html"] == "") OR get_config("system", "ignore_cache")) {
1168 // The function "redir_private_images" changes the body.
1169 // I'm not sure if we should store it permanently, so we save the old value.
1170 $body = $item["body"];
1173 redir_private_images($a, $item);
1175 $item["rendered-html"] = prepare_text($item["body"]);
1176 $item["rendered-hash"] = hash("md5", $item["body"]);
1177 $item["body"] = $body;
1179 if ($update AND ($item["id"] != 0)) {
1180 q("UPDATE `item` SET `rendered-html` = '%s', `rendered-hash` = '%s' WHERE `id` = %d",
1181 dbesc($item["rendered-html"]), dbesc($item["rendered-hash"]), intval($item["id"]));
1186 // Given an item array, convert the body element from bbcode to html and add smilie icons.
1187 // If attach is true, also add icons for item attachments
1189 if(! function_exists('prepare_body')) {
1191 * Given an item array, convert the body element from bbcode to html and add smilie icons.
1192 * If attach is true, also add icons for item attachments
1194 * @param array $item
1195 * @param boolean $attach
1196 * @return string item body html
1197 * @hook prepare_body_init item array before any work
1198 * @hook prepare_body ('item'=>item array, 'html'=>body string) after first bbcode to html
1199 * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
1201 function prepare_body(&$item,$attach = false, $preview = false) {
1204 call_hooks('prepare_body_init', $item);
1206 $searchpath = z_root()."/search?tag=";
1209 $hashtags = array();
1210 $mentions = array();
1212 if (!get_config('system','suppress_tags')) {
1213 $taglist = q("SELECT `type`, `term`, `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d) ORDER BY `tid`",
1214 intval(TERM_OBJ_POST), intval($item['id']), intval(TERM_HASHTAG), intval(TERM_MENTION));
1216 foreach($taglist as $tag) {
1218 if ($tag["url"] == "")
1219 $tag["url"] = $searchpath.strtolower($tag["term"]);
1221 if ($tag["type"] == TERM_HASHTAG) {
1222 $hashtags[] = "#<a href=\"".$tag["url"]."\" target=\"_blank\">".$tag["term"]."</a>";
1224 } elseif ($tag["type"] == TERM_MENTION) {
1225 $mentions[] = "@<a href=\"".$tag["url"]."\" target=\"_blank\">".$tag["term"]."</a>";
1228 $tags[] = $prefix."<a href=\"".$tag["url"]."\" target=\"_blank\">".$tag["term"]."</a>";
1232 $item['tags'] = $tags;
1233 $item['hashtags'] = $hashtags;
1234 $item['mentions'] = $mentions;
1236 // Update the cached values if there is no "zrl=..." on the links
1237 $update = (!local_user() and !remote_user() and ($item["uid"] == 0));
1239 // Or update it if the current viewer is the intented viewer
1240 if (($item["uid"] == local_user()) AND ($item["uid"] != 0))
1243 put_item_in_cache($item, $update);
1244 $s = $item["rendered-html"];
1246 $prep_arr = array('item' => $item, 'html' => $s, 'preview' => $preview);
1247 call_hooks('prepare_body', $prep_arr);
1248 $s = $prep_arr['html'];
1251 // Replace the blockquotes with quotes that are used in mails
1252 $mailquote = '<blockquote type="cite" class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">';
1253 $s = str_replace(array('<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'), array($mailquote, $mailquote, $mailquote), $s);
1259 $arr = explode('[/attach],',$item['attach']);
1261 $as .= '<div class="body-attach">';
1262 foreach($arr as $r) {
1265 $cnt = preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches, PREG_SET_ORDER);
1267 foreach($matches as $mtch) {
1270 if((local_user() == $item['uid']) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN))
1271 $the_url = 'redir/' . $item['contact-id'] . '?f=1&url=' . $mtch[1];
1273 $the_url = $mtch[1];
1275 if(strpos($mime, 'video') !== false) {
1278 $a->page['htmlhead'] .= replace_macros(get_markup_template('videos_head.tpl'), array(
1279 '$baseurl' => z_root(),
1281 $a->page['end'] .= replace_macros(get_markup_template('videos_end.tpl'), array(
1282 '$baseurl' => z_root(),
1286 $id = end(explode('/', $the_url));
1287 $as .= replace_macros(get_markup_template('video_top.tpl'), array(
1290 'title' => t('View Video'),
1297 $filetype = strtolower(substr( $mime, 0, strpos($mime,'/') ));
1299 $filesubtype = strtolower(substr( $mime, strpos($mime,'/') + 1 ));
1300 $filesubtype = str_replace('.', '-', $filesubtype);
1304 $filesubtype = 'unkn';
1307 $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
1308 /*$icontype = strtolower(substr($mtch[3],0,strpos($mtch[3],'/')));
1314 $icon = '<div class="attachtype icon s22 type-' . $icontype . '"></div>';
1317 $icon = '<div class="attachtype icon s22 type-unkn"></div>';
1321 $title = ((strlen(trim($mtch[4]))) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1]));
1322 $title .= ' ' . $mtch[2] . ' ' . t('bytes');
1324 $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
1328 $as .= '<div class="clear"></div></div>';
1333 if(strpos($s,'<div class="map">') !== false && $item['coord']) {
1334 $x = generate_map(trim($item['coord']));
1336 $s = preg_replace('/\<div class\=\"map\"\>/','$0' . $x,$s);
1342 $spoilersearch = '<blockquote class="spoiler">';
1344 // Remove line breaks before the spoiler
1345 while ((strpos($s, "\n".$spoilersearch) !== false))
1346 $s = str_replace("\n".$spoilersearch, $spoilersearch, $s);
1347 while ((strpos($s, "<br />".$spoilersearch) !== false))
1348 $s = str_replace("<br />".$spoilersearch, $spoilersearch, $s);
1350 while ((strpos($s, $spoilersearch) !== false)) {
1352 $pos = strpos($s, $spoilersearch);
1353 $rnd = random_string(8);
1354 $spoilerreplace = '<br /> <span id="spoiler-wrap-'.$rnd.'" class="spoiler-wrap fakelink" onclick="openClose(\'spoiler-'.$rnd.'\');">'.sprintf(t('Click to open/close')).'</span>'.
1355 '<blockquote class="spoiler" id="spoiler-'.$rnd.'" style="display: none;">';
1356 $s = substr($s, 0, $pos).$spoilerreplace.substr($s, $pos+strlen($spoilersearch));
1359 // Look for quote with author
1360 $authorsearch = '<blockquote class="author">';
1362 while ((strpos($s, $authorsearch) !== false)) {
1364 $pos = strpos($s, $authorsearch);
1365 $rnd = random_string(8);
1366 $authorreplace = '<br /> <span id="author-wrap-'.$rnd.'" class="author-wrap fakelink" onclick="openClose(\'author-'.$rnd.'\');">'.sprintf(t('Click to open/close')).'</span>'.
1367 '<blockquote class="author" id="author-'.$rnd.'" style="display: block;">';
1368 $s = substr($s, 0, $pos).$authorreplace.substr($s, $pos+strlen($authorsearch));
1371 // replace friendica image url size with theme preference
1372 if (x($a->theme_info,'item_image_size')){
1373 $ps = $a->theme_info['item_image_size'];
1375 $s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|',"$1-".$ps, $s);
1378 $prep_arr = array('item' => $item, 'html' => $s);
1379 call_hooks('prepare_body_final', $prep_arr);
1381 return $prep_arr['html'];
1385 if(! function_exists('prepare_text')) {
1387 * Given a text string, convert from bbcode to html and add smilie icons.
1389 * @param string $text
1392 function prepare_text($text) {
1394 require_once('include/bbcode.php');
1396 if(stristr($text,'[nosmile]'))
1399 $s = Smilies::replace(bbcode($text));
1407 * return array with details for categories and folders for an item
1409 * @param array $item
1413 * [ // categories array
1415 * 'name': 'category name',
1416 * 'removeurl': 'url to remove this category',
1417 * 'first': 'is the first in this array? true/false',
1418 * 'last': 'is the last in this array? true/false',
1424 * 'name': 'folder name',
1425 * 'removeurl': 'url to remove this folder',
1426 * 'first': 'is the first in this array? true/false',
1427 * 'last': 'is the last in this array? true/false',
1433 function get_cats_and_terms($item) {
1436 $categories = array();
1439 $matches = false; $first = true;
1440 $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
1442 foreach($matches as $mtch) {
1443 $categories[] = array(
1444 'name' => xmlify(file_tag_decode($mtch[1])),
1446 'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . xmlify(file_tag_decode($mtch[1])):""),
1453 if (count($categories)) $categories[count($categories)-1]['last'] = true;
1456 if(local_user() == $item['uid']) {
1457 $matches = false; $first = true;
1458 $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
1460 foreach($matches as $mtch) {
1462 'name' => xmlify(file_tag_decode($mtch[1])),
1464 'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&term=' . xmlify(file_tag_decode($mtch[1])):""),
1473 if (count($folders)) $folders[count($folders)-1]['last'] = true;
1475 return array($categories, $folders);
1478 if(! function_exists('get_plink')) {
1480 * get private link for item
1481 * @param array $item
1482 * @return boolean|array False if item has not plink, otherwise array('href'=>plink url, 'title'=>translated title)
1484 function get_plink($item) {
1487 if ($a->user['nickname'] != "") {
1489 //'href' => "display/".$a->user['nickname']."/".$item['id'],
1490 'href' => "display/".$item['guid'],
1491 'orig' => "display/".$item['guid'],
1492 'title' => t('View on separate page'),
1493 'orig_title' => t('view on separate page'),
1496 if (x($item,'plink')) {
1497 $ret["href"] = $a->remove_baseurl($item['plink']);
1498 $ret["title"] = t('link to source');
1501 } elseif (x($item,'plink') && ($item['private'] != 1))
1503 'href' => $item['plink'],
1504 'orig' => $item['plink'],
1505 'title' => t('link to source'),
1510 //if (x($item,'plink') && ($item['private'] != 1))
1515 if(! function_exists('unamp')) {
1517 * replace html amp entity with amp char
1521 function unamp($s) {
1522 return str_replace('&', '&', $s);
1526 if(! function_exists('return_bytes')) {
1528 * return number of bytes in size (K, M, G)
1529 * @param string $size_str
1532 function return_bytes ($size_str) {
1533 switch (substr ($size_str, -1)) {
1534 case 'M': case 'm': return (int)$size_str * 1048576;
1535 case 'K': case 'k': return (int)$size_str * 1024;
1536 case 'G': case 'g': return (int)$size_str * 1073741824;
1537 default: return $size_str;
1544 function generate_user_guid() {
1547 $guid = get_guid(32);
1548 $x = q("SELECT `uid` FROM `user` WHERE `guid` = '%s' LIMIT 1",
1553 } while ($found == true );
1560 * @param boolean $strip_padding
1563 function base64url_encode($s, $strip_padding = false) {
1565 $s = strtr(base64_encode($s),'+/','-_');
1568 $s = str_replace('=','',$s);
1577 function base64url_decode($s) {
1580 logger('base64url_decode: illegal input: ' . print_r(debug_backtrace(), true));
1585 * // Placeholder for new rev of salmon which strips base64 padding.
1586 * // PHP base64_decode handles the un-padded input without requiring this step
1587 * // Uncomment if you find you need it.
1590 * if(! strpos($s,'=')) {
1600 return base64_decode(strtr($s,'-_','+/'));
1604 if (!function_exists('str_getcsv')) {
1608 * @param string $input
1609 * @param string $delimiter
1610 * @param string $enclosure
1611 * @param string $escape
1612 * @param string $eol
1613 * @return boolean|array False on error, otherwise array[row][column]
1615 function str_getcsv($input, $delimiter = ',', $enclosure = '"', $escape = '\\', $eol = '\n') {
1616 if (is_string($input) && !empty($input)) {
1618 $tmp = preg_split("/".$eol."/",$input);
1619 if (is_array($tmp) && !empty($tmp)) {
1620 while (list($line_num, $line) = each($tmp)) {
1621 if (preg_match("/".$escape.$enclosure."/",$line)) {
1622 while ($strlen = strlen($line)) {
1623 $pos_delimiter = strpos($line,$delimiter);
1624 $pos_enclosure_start = strpos($line,$enclosure);
1626 is_int($pos_delimiter) && is_int($pos_enclosure_start)
1627 && ($pos_enclosure_start < $pos_delimiter)
1629 $enclosed_str = substr($line,1);
1630 $pos_enclosure_end = strpos($enclosed_str,$enclosure);
1631 $enclosed_str = substr($enclosed_str,0,$pos_enclosure_end);
1632 $output[$line_num][] = $enclosed_str;
1633 $offset = $pos_enclosure_end+3;
1635 if (empty($pos_delimiter) && empty($pos_enclosure_start)) {
1636 $output[$line_num][] = substr($line,0);
1637 $offset = strlen($line);
1639 $output[$line_num][] = substr($line,0,$pos_delimiter);
1641 !empty($pos_enclosure_start)
1642 && ($pos_enclosure_start < $pos_delimiter)
1644 ?$pos_enclosure_start
1648 $line = substr($line,$offset);
1651 $line = preg_split("/".$delimiter."/",$line);
1654 * Validating against pesky extra line breaks creating false rows.
1656 if (is_array($line) && !empty($line[0])) {
1657 $output[$line_num] = $line;
1672 * return div element with class 'clear'
1676 function cleardiv() {
1677 return '<div class="clear"></div>';
1681 function bb_translate_video($s) {
1684 $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
1686 foreach($matches as $mtch) {
1687 if((stristr($mtch[1],'youtube')) || (stristr($mtch[1],'youtu.be')))
1688 $s = str_replace($mtch[0],'[youtube]' . $mtch[1] . '[/youtube]',$s);
1689 elseif(stristr($mtch[1],'vimeo'))
1690 $s = str_replace($mtch[0],'[vimeo]' . $mtch[1] . '[/vimeo]',$s);
1696 function html2bb_video($s) {
1698 $s = preg_replace('#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
1699 '[youtube]$2[/youtube]', $s);
1701 $s = preg_replace('#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
1702 '[youtube]$2[/youtube]', $s);
1704 $s = preg_replace('#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
1705 '[vimeo]$2[/vimeo]', $s);
1711 * apply xmlify() to all values of array $val, recursively
1715 function array_xmlify($val){
1716 if (is_bool($val)) return $val?"true":"false";
1717 if (is_array($val)) return array_map('array_xmlify', $val);
1718 return xmlify((string) $val);
1723 * transorm link href and img src from relative to absolute
1725 * @param string $text
1726 * @param string $base base url
1729 function reltoabs($text, $base) {
1733 $base = rtrim($base,'/');
1735 $base2 = $base . "/";
1738 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
1739 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
1740 $text = preg_replace($pattern, $replace, $text);
1742 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
1743 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
1744 $text = preg_replace($pattern, $replace, $text);
1747 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
1748 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
1749 $text = preg_replace($pattern, $replace, $text);
1751 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
1752 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
1753 $text = preg_replace($pattern, $replace, $text);
1761 * get translated item type
1763 * @param array $itme
1766 function item_post_type($item) {
1767 if(intval($item['event-id']))
1769 if(strlen($item['resource-id']))
1771 if(strlen($item['verb']) && $item['verb'] !== ACTIVITY_POST)
1772 return t('activity');
1773 if($item['id'] != $item['parent'])
1774 return t('comment');
1778 // post categories and "save to file" use the same item.file table for storage.
1779 // We will differentiate the different uses by wrapping categories in angle brackets
1780 // and save to file categories in square brackets.
1781 // To do this we need to escape these characters if they appear in our tag.
1783 function file_tag_encode($s) {
1784 return str_replace(array('<','>','[',']'),array('%3c','%3e','%5b','%5d'),$s);
1787 function file_tag_decode($s) {
1788 return str_replace(array('%3c','%3e','%5b','%5d'),array('<','>','[',']'),$s);
1791 function file_tag_file_query($table,$s,$type = 'file') {
1794 $str = preg_quote( '[' . str_replace('%','%%',file_tag_encode($s)) . ']' );
1796 $str = preg_quote( '<' . str_replace('%','%%',file_tag_encode($s)) . '>' );
1797 return " AND " . (($table) ? dbesc($table) . '.' : '') . "file regexp '" . dbesc($str) . "' ";
1800 // ex. given music,video return <music><video> or [music][video]
1801 function file_tag_list_to_file($list,$type = 'file') {
1804 $list_array = explode(",",$list);
1805 if($type == 'file') {
1814 foreach($list_array as $item) {
1816 $tag_list .= $lbracket . file_tag_encode(trim($item)) . $rbracket;
1823 // ex. given <music><video>[friends], return music,video or friends
1824 function file_tag_file_to_list($file,$type = 'file') {
1827 if($type == 'file') {
1828 $cnt = preg_match_all('/\[(.*?)\]/',$file,$matches,PREG_SET_ORDER);
1831 $cnt = preg_match_all('/<(.*?)>/',$file,$matches,PREG_SET_ORDER);
1834 foreach($matches as $mtch) {
1837 $list .= file_tag_decode($mtch[1]);
1844 function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') {
1845 // $file_old - categories previously associated with an item
1846 // $file_new - new list of categories for an item
1851 if($file_old == $file_new)
1854 $saved = get_pconfig($uid,'system','filetags');
1855 if(strlen($saved)) {
1856 if($type == 'file') {
1859 $termtype = TERM_FILE;
1864 $termtype = TERM_CATEGORY;
1867 $filetags_updated = $saved;
1869 // check for new tags to be added as filetags in pconfig
1870 $new_tags = array();
1871 $check_new_tags = explode(",",file_tag_file_to_list($file_new,$type));
1873 foreach($check_new_tags as $tag) {
1874 if(! stristr($saved,$lbracket . file_tag_encode($tag) . $rbracket))
1878 $filetags_updated .= file_tag_list_to_file(implode(",",$new_tags),$type);
1880 // check for deleted tags to be removed from filetags in pconfig
1881 $deleted_tags = array();
1882 $check_deleted_tags = explode(",",file_tag_file_to_list($file_old,$type));
1884 foreach($check_deleted_tags as $tag) {
1885 if(! stristr($file_new,$lbracket . file_tag_encode($tag) . $rbracket))
1886 $deleted_tags[] = $tag;
1889 foreach($deleted_tags as $key => $tag) {
1890 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
1892 intval(TERM_OBJ_POST),
1896 //$r = q("select file from item where uid = %d " . file_tag_file_query('item',$tag,$type),
1901 unset($deleted_tags[$key]);
1904 $filetags_updated = str_replace($lbracket . file_tag_encode($tag) . $rbracket,'',$filetags_updated);
1908 if($saved != $filetags_updated) {
1909 set_pconfig($uid,'system','filetags', $filetags_updated);
1914 if(strlen($file_new)) {
1915 set_pconfig($uid,'system','filetags', $file_new);
1920 function file_tag_save_file($uid,$item,$file) {
1921 require_once("include/files.php");
1926 $r = q("SELECT `file` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1931 if(! stristr($r[0]['file'],'[' . file_tag_encode($file) . ']'))
1932 q("UPDATE `item` SET `file` = '%s' WHERE `id` = %d AND `uid` = %d",
1933 dbesc($r[0]['file'] . '[' . file_tag_encode($file) . ']'),
1938 create_files_from_item($item);
1940 $saved = get_pconfig($uid,'system','filetags');
1941 if((! strlen($saved)) || (! stristr($saved,'[' . file_tag_encode($file) . ']')))
1942 set_pconfig($uid,'system','filetags',$saved . '[' . file_tag_encode($file) . ']');
1943 info( t('Item filed') );
1948 function file_tag_unsave_file($uid,$item,$file,$cat = false) {
1949 require_once("include/files.php");
1956 $pattern = '<' . file_tag_encode($file) . '>' ;
1957 $termtype = TERM_CATEGORY;
1959 $pattern = '[' . file_tag_encode($file) . ']' ;
1960 $termtype = TERM_FILE;
1964 $r = q("SELECT `file` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1971 q("UPDATE `item` SET `file` = '%s' WHERE `id` = %d AND `uid` = %d",
1972 dbesc(str_replace($pattern,'',$r[0]['file'])),
1977 create_files_from_item($item);
1979 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
1981 intval(TERM_OBJ_POST),
1985 //$r = q("select file from item where uid = %d and deleted = 0 " . file_tag_file_query('item',$file,(($cat) ? 'category' : 'file')),
1989 $saved = get_pconfig($uid,'system','filetags');
1990 set_pconfig($uid,'system','filetags',str_replace($pattern,'',$saved));
1996 function normalise_openid($s) {
1997 return trim(str_replace(array('http://','https://'),array('',''),$s),'/');
2001 function undo_post_tagging($s) {
2003 $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism',$s,$matches,PREG_SET_ORDER);
2005 foreach($matches as $mtch) {
2006 $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
2012 function fix_mce_lf($s) {
2013 $s = str_replace("\r\n","\n",$s);
2014 // $s = str_replace("\n\n","\n",$s);
2019 function protect_sprintf($s) {
2020 return(str_replace('%','%%',$s));
2024 function is_a_date_arg($s) {
2028 if($i <= $y+1 && strpos($s,'-') == 4) {
2029 $m = intval(substr($s,5));
2030 if($m > 0 && $m <= 12)
2038 * remove intentation from a text
2040 function deindent($text, $chr="[\t ]", $count=NULL) {
2041 $text = fix_mce_lf($text);
2042 $lines = explode("\n", $text);
2043 if (is_null($count)) {
2045 $k=0; while($k<count($lines) && strlen($lines[$k])==0) $k++;
2046 preg_match("|^".$chr."*|", $lines[$k], $m);
2047 $count = strlen($m[0]);
2049 for ($k=0; $k<count($lines); $k++){
2050 $lines[$k] = preg_replace("|^".$chr."{".$count."}|", "", $lines[$k]);
2053 return implode("\n", $lines);
2056 function formatBytes($bytes, $precision = 2) {
2057 $units = array('B', 'KB', 'MB', 'GB', 'TB');
2059 $bytes = max($bytes, 0);
2060 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
2061 $pow = min($pow, count($units) - 1);
2063 $bytes /= pow(1024, $pow);
2065 return round($bytes, $precision) . ' ' . $units[$pow];
2069 * @brief translate and format the networkname of a contact
2071 * @param string $network
2072 * Networkname of the contact (e.g. dfrn, rss and so on)
2077 function format_network_name($network, $url = 0) {
2078 if ($network != "") {
2079 require_once('include/contact_selectors.php');
2081 $network_name = '<a href="'.$url.'">'.network_to_name($network, $url)."</a>";
2083 $network_name = network_to_name($network);
2085 return $network_name;