]> git.mxchange.org Git - friendica.git/blob - boot.php
like, dislike, activity streams, etc.
[friendica.git] / boot.php
1 <?php
2
3 set_time_limit(0);
4
5 define ( 'BUILD_ID' , 1003 );
6
7 define ( 'EOL', "<br />\r\n");
8 define ( 'ATOM_TIME',  'Y-m-d\TH:i:s\Z' );
9
10 define ( 'REGISTER_CLOSED',  0);
11 define ( 'REGISTER_APPROVE', 1);
12 define ( 'REGISTER_OPEN',    2);
13
14 define ( 'DIRECTION_NONE', 0);                  // No relationship
15 define ( 'DIRECTION_IN',   1);                  // VIP e.g. has FAN
16 define ( 'DIRECTION_OUT',  2);                  // FAN to a VIP
17 define ( 'DIRECTION_BOTH', 3);                  // Mutual Friends
18
19 define ( 'REL_VIP',        1);
20 define ( 'REL_FAN',        2);
21 define ( 'REL_BUD',        3);
22
23 define ( 'NOTIFY_INTRO',   0x0001 );
24 define ( 'NOTIFY_CONFIRM', 0x0002 );
25 define ( 'NOTIFY_WALL',    0x0004 );
26 define ( 'NOTIFY_COMMENT', 0x0008 );
27 define ( 'NOTIFY_MAIL',    0x0010 );
28
29 define ( 'NAMESPACE_DFRN' ,           'http://purl.org/macgirvin/dfrn/1.0' ); 
30 define ( 'NAMESPACE_THREAD' ,         'http://purl.org/syndication/thread/1.0' );
31 define ( 'NAMESPACE_TOMB' ,           'http://purl.org/atompub/tombstones/1.0' );
32 define ( 'NAMESPACE_ACTIVITY',        'http://activitystrea.ms/spec/1.0/' );
33 define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/');
34
35 define ( 'ACTIVITY_LIKE',        NAMESPACE_ACTIVITY_SCHEMA . 'like' );
36 define ( 'ACTIVITY_DISLIKE',     NAMESPACE_DFRN            . '/dislike' );
37 define ( 'ACTIVITY_OBJ_HEART',   NAMESPACE_DFRN            . '/heart' );
38
39 define ( 'ACTIVITY_FRIEND',      NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
40 define ( 'ACTIVITY_POST',        NAMESPACE_ACTIVITY_SCHEMA . 'post' );
41 define ( 'ACTIVITY_UPDATE',      NAMESPACE_ACTIVITY_SCHEMA . 'update' );
42
43 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
44 define ( 'ACTIVITY_OBJ_NOTE',    NAMESPACE_ACTIVITY_SCHEMA . 'note' );
45 define ( 'ACTIVITY_OBJ_PERSON',  NAMESPACE_ACTIVITY_SCHEMA . 'person' );
46 define ( 'ACTIVITY_OBJ_PHOTO',   NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
47 define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
48 define ( 'ACTIVITY_OBJ_ALBUM',   NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
49
50 define ( 'GRAVITY_PARENT',       0);
51 define ( 'GRAVITY_LIKE',         3);
52 define ( 'GRAVITY_COMMENT',      6);
53
54
55 if(! class_exists('App')) {
56 class App {
57
58         public  $module_loaded = false;
59         public  $config;
60         public  $page;
61         public  $profile;
62         public  $user;
63         public  $cid;
64         public  $contact;
65         public  $content;
66         public  $data;
67         public  $error = false;
68         public  $cmd;
69         public  $argv;
70         public  $argc;
71         public  $module;
72         public  $pager;
73         public  $strings;   
74         public  $path;
75
76         private $scheme;
77         private $hostname;
78         private $baseurl;
79         private $db;
80
81         function __construct() {
82
83                 $this->config = array();
84                 $this->page = array();
85                 $this->pager= array();
86
87                 $this->scheme = ((isset($_SERVER['HTTPS']) 
88                                 && ($_SERVER['HTTPS'])) ?  'https' : 'http' );
89                 $this->hostname = str_replace('www.','',
90                                 $_SERVER['SERVER_NAME']);
91                 set_include_path("include/$this->hostname" 
92                                 . PATH_SEPARATOR . 'include' 
93                                 . PATH_SEPARATOR . '.' );
94
95                 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
96                         $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
97                 $this->cmd = trim($_GET['q'],'/');
98
99
100                 $this->argv = explode('/',$this->cmd);
101                 $this->argc = count($this->argv);
102                 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
103                         $this->module = $this->argv[0];
104                 }
105                 else {
106                         $this->module = 'home';
107                 }
108
109                 if($this->cmd == '.well-known/host-meta')
110                         require_once('include/hostxrd.php');
111
112                 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
113                 $this->pager['itemspage'] = 50;
114                 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
115                 $this->pager['total'] = 0;
116         }
117
118         function get_baseurl($ssl = false) {
119                 if(strlen($this->baseurl))
120                         return $this->baseurl;
121
122                 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
123                         . ((isset($this->path) && strlen($this->path)) 
124                         ? '/' . $this->path : '' );
125                 return $this->baseurl;
126         }
127
128         function set_baseurl($url) {
129                 $this->baseurl = $url;
130                 $this->hostname = basename($url);
131         }
132
133         function get_hostname() {
134                 return $this->hostname;
135         }
136
137         function set_hostname($h) {
138                 $this->hostname = $h;
139         }
140
141         function set_path($p) {
142                 $this->path = ltrim(trim($p),'/');
143         } 
144
145         function get_path() {
146                 return $this->path;
147         }
148
149         function set_pager_total($n) {
150                 $this->pager['total'] = intval($n);
151         }
152
153         function set_pager_itemspage($n) {
154                 $this->pager['itemspage'] = intval($n);
155                 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
156
157         } 
158
159         function init_pagehead() {
160                 if(file_exists("view/head.tpl"))
161                         $s = file_get_contents("view/head.tpl");
162                 $this->page['htmlhead'] = replace_macros($s,array(
163                         '$baseurl' => $this->get_baseurl()
164                 ));
165         }
166
167 }}
168
169
170 if(! function_exists('x')) {
171 function x($s,$k = NULL) {
172         if($k != NULL) {
173                 if((is_array($s)) && (array_key_exists($k,$s))) {
174                         if($s[$k])
175                                 return (int) 1;
176                         return (int) 0;
177                 }
178                 return false;
179         }
180         else {          
181                 if(isset($s)) {
182                         if($s) {
183                                 return (int) 1;
184                         }
185                         return (int) 0;
186                 }
187                 return false;
188         }
189 }}
190
191 if(! function_exists('system_unavailable')) {
192 function system_unavailable() {
193         include('system_unavailable.php');
194         killme();
195 }}
196
197
198 if(! function_exists('check_config')) {
199 function check_config(&$a) {
200
201         load_config('system');
202
203         $build = get_config('system','build');
204         if(! x($build))
205                 $build = set_config('system','build',BUILD_ID);
206
207         $url = get_config('system','url');
208         if(! x($url))
209                 $url = set_config('system','url',$a->get_baseurl());
210
211         if($build != BUILD_ID) {
212                 $stored = intval($build);
213                 $current = intval(BUILD_ID);
214                 if(($stored < $current) && file_exists('update.php')) {
215
216                         // We're reporting a different version than what is currently installed.
217                         // Run any existing update scripts to bring the database up to current.
218
219                         require_once('update.php');
220                         for($x = $stored; $x < $current; $x ++) {
221                                 if(function_exists('update_' . $x)) {
222                                         $func = 'update_' . $x;
223                                         $func($a);
224                                 }
225                         }
226                         set_config('system','build', BUILD_ID);
227                 }
228         }
229         return;
230 }}
231
232
233
234 if(! function_exists('replace_macros')) {  
235 function replace_macros($s,$r) {
236
237         $search = array();
238         $replace = array();
239
240         if(is_array($r) && count($r)) {
241                 foreach ($r as $k => $v ) {
242                         $search[] =  $k;
243                         $replace[] = $v;
244                 }
245         }
246         return str_replace($search,$replace,$s);
247 }}
248
249
250 if(! function_exists('load_translation_table')) {
251 function load_translation_table($lang) {
252         global $a;
253
254 }}
255
256 if(! function_exists('t')) {
257 function t($s) {
258         global $a;
259
260         if($a->strings[$s])
261                 return $a->strings[$s];
262         return $s;
263 }}
264
265 if(! function_exists('fetch_url')) {
266 function fetch_url($url,$binary = false) {
267         $ch = curl_init($url);
268         if(! $ch) return false;
269
270         curl_setopt($ch, CURLOPT_HEADER, 0);
271         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
272         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
273         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
274         $prx = get_config('system','proxy');
275         if(strlen($prx)) {
276                 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
277                 curl_setopt($ch, CURLOPT_PROXY, $prx);
278                 $prxusr = get_config('system','proxyuser');
279                 if(strlen($prxusr))
280                         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
281         }
282         if($binary)
283                 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
284
285         $s = curl_exec($ch);
286         curl_close($ch);
287         return($s);
288 }}
289
290
291 if(! function_exists('post_url')) {
292 function post_url($url,$params) {
293         $ch = curl_init($url);
294         if(! $ch) return false;
295
296         curl_setopt($ch, CURLOPT_HEADER, 0);
297         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
298         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
299         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
300         curl_setopt($ch, CURLOPT_POST,1);
301         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
302         $prx = get_config('system','proxy');
303         if(strlen($prx)) {
304                 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
305                 curl_setopt($ch, CURLOPT_PROXY, $prx);
306                 $prxusr = get_config('system','proxyuser');
307                 if(strlen($prxusr))
308                         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
309         }
310
311         $s = curl_exec($ch);
312         curl_close($ch);
313         return($s);
314 }}
315
316
317 if(! function_exists('random_string')) {
318 function random_string() {
319         return(hash('sha256',uniqid(rand(),true)));
320 }}
321
322 if(! function_exists('notags')) {
323 function notags($string) {
324         // protect against :<> with high-bit set
325         return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
326 }}
327
328 if(! function_exists('escape_tags')) {
329 function escape_tags($string) {
330
331         return(htmlspecialchars($string));
332 }}
333
334 if(! function_exists('login')) {
335 function login($register = false) {
336         $o = "";
337         $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
338
339
340         if(x($_SESSION,'authenticated')) {
341                 $o = file_get_contents("view/logout.tpl");
342         }
343         else {
344                 $o = file_get_contents("view/login.tpl");
345
346                 $o = replace_macros($o,array('$register_html' => $register_html ));
347         }
348         return $o;
349 }}
350
351
352 if(! function_exists('autoname')) {
353 function autoname($len) {
354
355         $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'); 
356         if(mt_rand(0,5) == 4)
357                 $vowels[] = 'y';
358
359         $cons = array(
360                         'b','bl','br',
361                         'c','ch','cl','cr',
362                         'd','dr',
363                         'f','fl','fr',
364                         'g','gh','gl','gr',
365                         'h',
366                         'j',
367                         'k','kh','kl','kr',
368                         'l',
369                         'm',
370                         'n',
371                         'p','ph','pl','pr',
372                         'qu',
373                         'r','rh',
374                         's','sc','sh','sm','sp','st',
375                         't','th','tr',
376                         'v',
377                         'w','wh',
378                         'x',
379                         'z','zh'
380                         );
381
382         $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
383                                 'nd','ng','nk','nt','rn','rp','rt');
384
385         $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
386                                 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
387
388         $start = mt_rand(0,2);
389         if($start == 0)
390                 $table = $vowels;
391         else
392                 $table = $cons;
393
394         $word = '';
395
396         for ($x = 0; $x < $len; $x ++) {
397                 $r = mt_rand(0,count($table) - 1);
398                 $word .= $table[$r];
399   
400                 if($table == $vowels)
401                         $table = array_merge($cons,$midcons);
402                 else
403                         $table = $vowels;
404
405         }
406
407         $word = substr($word,0,$len);
408
409         foreach($noend as $noe) {
410                 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
411                         $word = substr($word,0,-1);
412                         break;
413                 }
414         }
415         if(substr($word,-1) == 'q')
416                 $word = substr($word,0,-1);    
417         return $word;
418 }}
419
420 if(! function_exists('killme')) {
421 function killme() {
422         session_write_close();
423         exit;
424 }}
425
426 if(! function_exists('goaway')) {
427 function goaway($s) {
428         header("Location: $s");
429         killme();
430 }}
431
432
433 if(! function_exists('xml_status')) {
434 function xml_status($st) {
435         header( "Content-type: text/xml" );
436         echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
437         echo "<result><status>$st</status></result>\r\n";
438         killme();
439 }}
440
441 if(! function_exists('local_user')) {
442 function local_user() {
443         if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
444                 return $_SESSION['uid'];
445         return false;
446 }}
447
448 if(! function_exists('remote_user')) {
449 function remote_user() {
450         if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
451                 return $_SESSION['visitor_id'];
452         return false;
453 }}
454
455 if(! function_exists('notice')) {
456 function notice($s) {
457
458         $_SESSION['sysmsg'] .= $s;
459
460 }}
461
462 if(! function_exists('get_max_import_size')) {
463 function get_max_import_size() {
464         global $a;
465         return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
466 }}
467
468 if(! function_exists('xmlify')) {
469 function xmlify($str) {
470         $buffer = '';
471         
472         for($x = 0; $x < strlen($str); $x ++) {
473                 $char = $str[$x];
474         
475                 switch( $char ) {
476
477                         case "\r" :
478                                 break;
479                         case "&" :
480                                 $buffer .= '&amp;';
481                                 break;
482                         case "'" :
483                                 $buffer .= '&apos;';
484                                 break;
485
486                         case "\"" :
487                                 $buffer .= '&quot;';
488                                 break;
489                         case '<' :
490                                 $buffer .= '&lt;';
491                                 break;
492                         case '>' :
493                                 $buffer .= '&gt;';
494                                 break;
495                         case "\n" :
496                                 $buffer .= ' ';
497                                 break;
498                         default :
499                                 $buffer .= $char;
500                                 break;
501                 }       
502         }
503         $buffer = trim($buffer);
504         return($buffer);
505 }}
506
507 if(! function_exists('unxmlify')) {
508 function unxmlify($s) {
509         $ret = str_replace('&amp;','&', $s);
510         $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
511         return $ret;    
512 }}
513
514 if(! function_exists('hex2bin')) {
515 function hex2bin($s) {
516         return(pack("H*",$s));
517 }}
518
519
520 if(! function_exists('paginate')) {
521 function paginate(&$a) {
522         $o = '';
523         $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
524         $stripped = str_replace('q=','',$stripped);
525         $stripped = trim($stripped,'/');
526         $url = $a->get_baseurl() . '/' . $stripped;
527
528
529           if($a->pager['total'] > $a->pager['itemspage']) {
530                 $o .= '<div class="pager">';
531                 if($a->pager['page'] != 1)
532                         $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
533
534                 $o .=  "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
535
536                 $numpages = $a->pager['total'] / $a->pager['itemspage'];
537
538                 $numstart = 1;
539                 $numstop = $numpages;
540
541                 if($numpages > 14) {
542                         $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
543                         $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
544                 }
545    
546                 for($i = $numstart; $i <= $numstop; $i++){
547                         if($i == $a->pager['page'])
548                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
549                         else
550                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
551                         $o .= '</span> ';
552                 }
553
554                 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
555                         if($i == $a->pager['page'])
556                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
557                         else
558                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
559                         $o .= '</span> ';
560                 }
561
562                 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
563                 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
564
565                 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
566                         $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
567                 $o .= '</div>'."\r\n";
568         }
569         return $o;
570 }}
571
572 if(! function_exists('expand_acl')) {
573 function expand_acl($s) {
574
575         if(strlen($s)) {
576                 $a = explode('<',$s);
577                 for($x = 0; $x < count($a); $x ++) {
578                         $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
579                 }
580                 return $a;
581         }
582         return array();
583 }}              
584
585 if(! function_exists('sanitise_acl')) {
586 function sanitise_acl(&$item) {
587         if(intval($item))
588                 $item = '<' . intval(notags(trim($item))) . '>';
589         else
590                 unset($item);
591 }}
592
593 if(! function_exists('load_config')) {
594 function load_config($family) {
595         global $a;
596         $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
597                 dbesc($family)
598         );
599         if(count($r)) {
600                 foreach($r as $rr) {
601                         $k = $rr['k'];
602                         $a->config[$family][$k] = $rr['v'];
603                 }
604         }
605 }}
606
607
608 if(! function_exists('get_config')) {
609 function get_config($family, $key, $instore = false) {
610
611         global $a;
612         if(! $instore) {
613                 if(isset($a->config[$family][$key])) {
614                         if($a->config[$family][$key] == '!<unset>!')
615                                 return false;
616                         return $a->config[$family][$key];
617                 }
618         }
619         $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
620                 dbesc($family),
621                 dbesc($key)
622         );
623         if(count($ret)) {
624                 $a->config[$family][$key] = $ret[0]['v'];
625                 return $ret[0]['v'];
626         }
627         else {
628                 $a->config[$family][$key] = '!<unset>!';
629         }
630         return false;
631 }}
632
633 if(! function_exists('set_config')) {
634 function set_config($family,$key,$value) {
635
636         global $a;
637         $a->config[$family][$key] = $value;
638
639         if(get_config($family,$key,true) === false) {
640                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
641                         dbesc($family),
642                         dbesc($key),
643                         dbesc($value)
644                 );
645                 if($ret) 
646                         return $value;
647                 return $ret;
648         }
649         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
650                 dbesc($value),
651                 dbesc($family),
652                 dbesc($key)
653         );
654         if($ret)
655                 return $value;
656         return $ret;
657 }}
658
659 if(! function_exists('convert_xml_element_to_array')) {
660 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
661
662         // If we're getting too deep, bail out
663         if ($recursion_depth > 512) {
664                 return(null);
665         }
666
667         if (!is_string($xml_element) &&
668         !is_array($xml_element) &&
669         (get_class($xml_element) == 'SimpleXMLElement')) {
670                 $xml_element_copy = $xml_element;
671                 $xml_element = get_object_vars($xml_element);
672         }
673
674         if (is_array($xml_element)) {
675                 $result_array = array();
676                 if (count($xml_element) <= 0) {
677                         return (trim(strval($xml_element_copy)));
678                 }
679
680                 foreach($xml_element as $key=>$value) {
681
682                         $recursion_depth++;
683                         $result_array[strtolower($key)] =
684                 convert_xml_element_to_array($value, $recursion_depth);
685                         $recursion_depth--;
686                 }
687                 if ($recursion_depth == 0) {
688                         $temp_array = $result_array;
689                         $result_array = array(
690                                 strtolower($xml_element_copy->getName()) => $temp_array,
691                         );
692                 }
693
694                 return ($result_array);
695
696         } else {
697                 return (trim(strval($xml_element)));
698         }
699 }}
700
701
702 if(! function_exists('webfinger')) {
703 function webfinger($s) {
704         if(! strstr($s,'@')) {
705                 return $s;
706         }
707         $host = substr($s,strpos($s,'@') + 1);
708         $url = 'http://' . $host . '/.well-known/host-meta' ;
709         $xml = fetch_url($url);
710         if (! $xml)
711                 return '';
712         $h = simplexml_load_string($xml);
713         $arr = convert_xml_element_to_array($h);
714
715         if(! isset($arr['xrd']['link']))
716                 return '';
717
718         $link = $arr['xrd']['link'];
719         if(! isset($link[0]))
720                 $links = array($link);
721         else
722                 $links = $link;
723
724         foreach($links as $link)
725                 if($link['@attributes']['rel'] && $link['@attributes']['rel'] == 'lrdd')
726                         $tpl = $link['@attributes']['template'];
727         if((empty($tpl)) || (! strpos($tpl, '{uri}')))
728                 return '';
729
730         $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
731
732         $xml = fetch_url($pxrd);
733         if (! $xml)
734                 return '';
735         $h = simplexml_load_string($xml);
736         $arr = convert_xml_element_to_array($h);
737
738         if(! isset($arr['xrd']['link']))
739                 return '';
740
741         $link = $arr['xrd']['link'];
742         if(! isset($link[0]))
743                 $links = array($link);
744         else
745                 $links = $link;
746
747         foreach($links as $link)
748                 if($link['@attributes']['rel'] == NAMESPACE_DFRN)
749                         return $link['@attributes']['href'];
750         return '';
751 }}
752
753 if(! function_exists('perms2str')) {
754 function perms2str($p) {
755         $ret = '';
756         $tmp = $p;
757         if(is_array($tmp)) {
758                 array_walk($tmp,'sanitise_acl');
759                 $ret = implode('',$tmp);
760         }
761         return $ret;
762 }}
763
764 if(! function_exists('item_new_uri')) {
765 function item_new_uri($hostname,$uid) {
766
767         do {
768                 $dups = false;
769                 $hash = random_string();
770
771                 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
772
773                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
774                         dbesc($uri));
775                 if(count($r))
776                         $dups = true;
777         } while($dups == true);
778         return $uri;
779 }}
780
781 if(! function_exists('photo_new_resource')) {
782 function photo_new_resource() {
783
784         do {
785                 $found = false;
786                 $resource = hash('md5',uniqid(mt_rand(),true));
787                 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
788                         dbesc($resource)
789                 );
790                 if(count($r))
791                         $found = true;
792         } while($found == true);
793         return $resource;
794 }}
795
796
797
798 if(! function_exists('get_uid')) {
799 function get_uid() {
800         return ((x($_SESSION,'uid')) ? intval($_SESSION['uid']) : 0) ;
801 }}
802
803 if(! function_exists('validate_url')) {
804 function validate_url(&$url) {
805         if(substr($url,0,4) != 'http')
806                 $url = 'http://' . $url;
807         $h = parse_url($url);
808
809         if(! $h) {
810                 return false;
811         }
812         if(! checkdnsrr($h['host'], 'ANY')) {
813                 return false;
814         }
815         return true;
816 }}
817
818 if(! function_exists('allowed_url')) {
819 function allowed_url($url) {
820
821         $h = parse_url($url);
822
823         if(! $h) {
824                 return false;
825         }
826
827         $str_allowed = get_config('system','allowed_sites');
828         if(! $str_allowed)
829                 return true;
830
831         $found = false;
832
833         $host = strtolower($h['host']);
834
835         // always allow our own site
836
837         if($host == strtolower($_SERVER['SERVER_NAME']))
838                 return true;
839
840         $fnmatch = function_exists('fnmatch');
841         $allowed = explode(',',$str_allowed);
842
843         if(count($allowed)) {
844                 foreach($allowed as $a) {
845                         $pat = strtolower(trim($a));
846                         if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
847                                 $found = true; 
848                                 break;
849                         }
850                 }
851         }
852         return $found;
853 }}
854
855 if(! function_exists('format_like')) {
856 function format_like($cnt,$arr,$type,$id) {
857         if($cnt == 1)
858                 $o .= $arr[0] . (($type == 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
859         else {
860                 $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >' 
861                         . $cnt . ' ' . t('people') . '</span> ' . (($type == 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
862                 $total = count($arr);
863                 if($total >= 75)
864                         $arr = array_slice($arr,0,74);
865                 if($total < 75)
866                         $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
867                 $str = implode(', ', $arr);
868                 if($total >= 75)
869                         $str .= t(', and ') . $total - 75 . t(' other people');
870                 $str .= (($type == 'like') ? t(' like this.') : t(' don\'t like this.'));
871                 $o .= '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
872         }
873         return $o;
874 }}
875