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