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