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