]> git.mxchange.org Git - friendica.git/blob - boot.php
mongo checkin, global directory, redir rework, location basics
[friendica.git] / boot.php
1 <?php
2
3 set_time_limit(0);
4
5 define ( 'BUILD_ID' , 1000 );
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
26
27 if(! class_exists('App')) {
28 class App {
29
30         public  $module_loaded = false;
31         public  $config;
32         public  $page;
33         public  $profile;
34         public  $user;
35         public  $cid;
36         public  $contact;
37         public  $content;
38         public  $data;
39         public  $error = false;
40         public  $cmd;
41         public  $argv;
42         public  $argc;
43         public  $module;
44         public  $pager;
45         public  $strings;   
46         public  $path;
47
48         private $scheme;
49         private $hostname;
50         private $baseurl;
51         private $db;
52
53         function __construct() {
54
55                 $this->config = array();
56                 $this->page = array();
57                 $this->pager= array();
58
59                 $this->scheme = ((isset($_SERVER['HTTPS']) 
60                                 && ($_SERVER['HTTPS'])) ?  'https' : 'http' );
61                 $this->hostname = str_replace('www.','',
62                                 $_SERVER['SERVER_NAME']);
63                 set_include_path("include/$this->hostname" 
64                                 . PATH_SEPARATOR . 'include' 
65                                 . PATH_SEPARATOR . '.' );
66
67                 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
68                         $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
69                 $this->cmd = trim($_GET['q'],'/');
70
71
72                 $this->argv = explode('/',$this->cmd);
73                 $this->argc = count($this->argv);
74                 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
75                         $this->module = $this->argv[0];
76                 }
77                 else {
78                         $this->module = 'home';
79                 }
80
81                 if($this->cmd == '.well-known/host-meta')
82                         require_once('include/hostxrd.php');
83
84                 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
85                 $this->pager['itemspage'] = 50;
86                 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
87                 $this->pager['total'] = 0;
88         }
89
90         function get_baseurl($ssl = false) {
91                 if(strlen($this->baseurl))
92                         return $this->baseurl;
93
94                 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
95                         . ((isset($this->path) && strlen($this->path)) 
96                         ? '/' . $this->path : '' );
97                 return $this->baseurl;
98         }
99
100         function set_baseurl($url) {
101                 $this->baseurl = $url;
102                 $this->hostname = basename($url);
103         }
104
105         function get_hostname() {
106                 return $this->hostname;
107         }
108
109         function set_hostname($h) {
110                 $this->hostname = $h;
111         }
112
113         function set_path($p) {
114                 $this->path = ltrim(trim($p),'/');
115         } 
116
117         function get_path() {
118                 return $this->path;
119         }
120
121         function set_pager_total($n) {
122                 $this->pager['total'] = intval($n);
123         }
124
125         function set_pager_itemspage($n) {
126                 $this->pager['itemspage'] = intval($n);
127                 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
128
129         } 
130
131         function init_pagehead() {
132                 if(file_exists("view/head.tpl"))
133                         $s = file_get_contents("view/head.tpl");
134                 $this->page['htmlhead'] = replace_macros($s,array(
135                         '$baseurl' => $this->get_baseurl()
136                 ));
137         }
138
139 }}
140
141
142 if(! function_exists('x')) {
143 function x($s,$k = NULL) {
144         if($k != NULL) {
145                 if((is_array($s)) && (array_key_exists($k,$s))) {
146                         if($s[$k])
147                                 return (int) 1;
148                         return (int) 0;
149                 }
150                 return false;
151         }
152         else {          
153                 if(isset($s)) {
154                         if($s) {
155                                 return (int) 1;
156                         }
157                         return (int) 0;
158                 }
159                 return false;
160         }
161 }}
162
163 if(! function_exists('system_unavailable')) {
164 function system_unavailable() {
165         include('system_unavailable.php');
166         killme();
167 }}
168
169
170 if(! function_exists('check_config')) {
171 function check_config(&$a) {
172
173         load_config('system');
174
175         $build = get_config('system','build');
176         if(! x($build))
177                 $build = set_config('system','build',BUILD_ID);
178
179         $url = get_config('system','url');
180         if(! x($url))
181                 $url = set_config('system','url',$a->get_baseurl());
182
183         if($build != BUILD_ID) {
184                 $stored = intval($build);
185                 $current = intval(BUILD_ID);
186                 if(($stored < $current) && file_exists('update.php')) {
187
188                         // We're reporting a different version than what is currently installed.
189                         // Run any existing update scripts to bring the database up to current.
190
191                         require_once('update.php');
192                         for($x = $stored; $x <= $current; $x ++) {
193                                 if(function_exists('update_' . $x)) {
194                                         $func = 'update_' . $x;
195                                         $func($a);
196                                 }
197                         }
198                         set_config('system','build', BUILD_ID);
199                 }
200         }
201         return;
202 }}
203
204
205
206 if(! function_exists('replace_macros')) {  
207 function replace_macros($s,$r) {
208
209         $search = array();
210         $replace = array();
211
212         if(is_array($r) && count($r)) {
213                 foreach ($r as $k => $v ) {
214                         $search[] =  $k;
215                         $replace[] = $v;
216                 }
217         }
218         return str_replace($search,$replace,$s);
219 }}
220
221
222 if(! function_exists('load_translation_table')) {
223 function load_translation_table($lang) {
224         global $a;
225
226 }}
227
228 if(! function_exists('t')) {
229 function t($s) {
230         global $a;
231
232         if($a->strings[$s])
233                 return $a->strings[$s];
234         return $s;
235 }}
236
237 if(! function_exists('fetch_url')) {
238 function fetch_url($url,$binary = false) {
239         $ch = curl_init($url);
240         if(! $ch) return false;
241
242         curl_setopt($ch, CURLOPT_HEADER, 0);
243         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
244         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
245         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
246         $prx = get_config('system','proxy');
247         if(strlen($prx)) {
248                 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
249                 curl_setopt($ch, CURLOPT_PROXY, $prx);
250                 $prxusr = get_config('system','proxyuser');
251                 if(strlen($prxusr))
252                         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
253         }
254         if($binary)
255                 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
256
257         $s = curl_exec($ch);
258         curl_close($ch);
259         return($s);
260 }}
261
262
263 if(! function_exists('post_url')) {
264 function post_url($url,$params) {
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         curl_setopt($ch, CURLOPT_POST,1);
273         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
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
283         $s = curl_exec($ch);
284         curl_close($ch);
285         return($s);
286 }}
287
288
289 if(! function_exists('random_string')) {
290 function random_string() {
291         return(hash('sha256',uniqid(rand(),true)));
292 }}
293
294 if(! function_exists('notags')) {
295 function notags($string) {
296         // protect against :<> with high-bit set
297         return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
298 }}
299
300 if(! function_exists('escape_tags')) {
301 function escape_tags($string) {
302
303         return(htmlspecialchars($string));
304 }}
305
306 if(! function_exists('login')) {
307 function login($register = false) {
308         $o = "";
309         $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
310
311
312         if(x($_SESSION,'authenticated')) {
313                 $o = file_get_contents("view/logout.tpl");
314         }
315         else {
316                 $o = file_get_contents("view/login.tpl");
317
318                 $o = replace_macros($o,array('$register_html' => $register_html ));
319         }
320         return $o;
321 }}
322
323
324 if(! function_exists('autoname')) {
325 function autoname($len) {
326
327         $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'); 
328         if(mt_rand(0,5) == 4)
329                 $vowels[] = 'y';
330
331         $cons = array(
332                         'b','bl','br',
333                         'c','ch','cl','cr',
334                         'd','dr',
335                         'f','fl','fr',
336                         'g','gh','gl','gr',
337                         'h',
338                         'j',
339                         'k','kh','kl','kr',
340                         'l',
341                         'm',
342                         'n',
343                         'p','ph','pl','pr',
344                         'qu',
345                         'r','rh',
346                         's','sc','sh','sm','sp','st',
347                         't','th','tr',
348                         'v',
349                         'w','wh',
350                         'x',
351                         'z','zh'
352                         );
353
354         $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
355                                 'nd','ng','nk','nt','rn','rp','rt');
356
357         $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
358                                 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
359
360         $start = mt_rand(0,2);
361         if($start == 0)
362                 $table = $vowels;
363         else
364                 $table = $cons;
365
366         $word = '';
367
368         for ($x = 0; $x < $len; $x ++) {
369                 $r = mt_rand(0,count($table) - 1);
370                 $word .= $table[$r];
371   
372                 if($table == $vowels)
373                         $table = array_merge($cons,$midcons);
374                 else
375                         $table = $vowels;
376
377         }
378
379         $word = substr($word,0,$len);
380
381         foreach($noend as $noe) {
382                 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
383                         $word = substr($word,0,-1);
384                         break;
385                 }
386         }
387         if(substr($word,-1) == 'q')
388                 $word = substr($word,0,-1);    
389         return $word;
390 }}
391
392 if(! function_exists('killme')) {
393 function killme() {
394         session_write_close();
395         exit;
396 }}
397
398 if(! function_exists('goaway')) {
399 function goaway($s) {
400         header("Location: $s");
401         killme();
402 }}
403
404
405 if(! function_exists('xml_status')) {
406 function xml_status($st) {
407         header( "Content-type: text/xml" );
408         echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
409         echo "<result><status>$st</status></result>\r\n";
410         killme();
411 }}
412
413 if(! function_exists('local_user')) {
414 function local_user() {
415         if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
416                 return $_SESSION['uid'];
417         return false;
418 }}
419
420 if(! function_exists('remote_user')) {
421 function remote_user() {
422         if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
423                 return $_SESSION['visitor_id'];
424         return false;
425 }}
426
427 if(! function_exists('notice')) {
428 function notice($s) {
429
430         $_SESSION['sysmsg'] .= $s;
431
432 }}
433
434 if(! function_exists('get_max_import_size')) {
435 function get_max_import_size() {
436         global $a;
437         return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
438 }}
439
440 if(! function_exists('xmlify')) {
441 function xmlify($str) {
442         $buffer = '';
443         
444         for($x = 0; $x < strlen($str); $x ++) {
445                 $char = $str[$x];
446         
447                 switch( $char ) {
448
449                         case "\r" :
450                                 break;
451                         case "&" :
452                                 $buffer .= '&amp;';
453                                 break;
454                         case "'" :
455                                 $buffer .= '&apos;';
456                                 break;
457
458                         case "\"" :
459                                 $buffer .= '&quot;';
460                                 break;
461                         case '<' :
462                                 $buffer .= '&lt;';
463                                 break;
464                         case '>' :
465                                 $buffer .= '&gt;';
466                                 break;
467                         case "\n" :
468                                 $buffer .= ' ';
469                                 break;
470                         default :
471                                 $buffer .= $char;
472                                 break;
473                 }       
474         }
475         $buffer = trim($buffer);
476         return($buffer);
477 }}
478
479 if(! function_exists('unxmlify')) {
480 function unxmlify($s) {
481         $ret = str_replace('&amp;','&', $s);
482         $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
483         return $ret;    
484 }}
485
486 if(! function_exists('hex2bin')) {
487 function hex2bin($s) {
488         return(pack("H*",$s));
489 }}
490
491
492 if(! function_exists('paginate')) {
493 function paginate(&$a) {
494         $o = '';
495         $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
496         $stripped = str_replace('q=','',$stripped);
497         $stripped = trim($stripped,'/');
498         $url = $a->get_baseurl() . '/' . $stripped;
499
500
501           if($a->pager['total'] > $a->pager['itemspage']) {
502                 $o .= '<div class="pager">';
503                 if($a->pager['page'] != 1)
504                         $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
505
506                 $o .=  "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
507
508                 $numpages = $a->pager['total'] / $a->pager['itemspage'];
509
510                 $numstart = 1;
511                 $numstop = $numpages;
512
513                 if($numpages > 14) {
514                         $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
515                         $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
516                 }
517    
518                 for($i = $numstart; $i <= $numstop; $i++){
519                         if($i == $a->pager['page'])
520                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
521                         else
522                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
523                         $o .= '</span> ';
524                 }
525
526                 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
527                         if($i == $a->pager['page'])
528                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
529                         else
530                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
531                         $o .= '</span> ';
532                 }
533
534                 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
535                 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
536
537                 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
538                         $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
539                 $o .= '</div>'."\r\n";
540         }
541         return $o;
542 }}
543
544 if(! function_exists('expand_acl')) {
545 function expand_acl($s) {
546
547         if(strlen($s)) {
548                 $a = explode('<',$s);
549                 for($x = 0; $x < count($a); $x ++) {
550                         $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
551                 }
552                 return $a;
553         }
554         return array();
555 }}              
556
557 if(! function_exists('sanitise_acl')) {
558 function sanitise_acl(&$item) {
559         if(intval($item))
560                 $item = '<' . intval(notags(trim($item))) . '>';
561         else
562                 unset($item);
563 }}
564
565 if(! function_exists('load_config')) {
566 function load_config($family) {
567         global $a;
568         $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
569                 dbesc($family)
570         );
571         if(count($r)) {
572                 foreach($r as $rr) {
573                         $k = $rr['k'];
574                         $a->config[$family][$k] = $rr['v'];
575                 }
576         }
577 }}
578
579
580
581 if(! function_exists('get_config')) {
582 function get_config($family,$key) {
583         global $a;
584         if(isset($a->config[$family][$key]))
585                 return $a->config[$family][$key];
586
587         $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
588                 dbesc($family),
589                 dbesc($key)
590         );
591         if(count($ret)) {
592                 $a->config[$family][$key] = $ret[0]['v'];
593                 return $ret[0]['v'];
594         }
595         return false;
596 }}
597
598 if(! function_exists('set_config')) {
599 function set_config($family,$key,$value) {
600
601         global $a;
602         $a->config[$family][$key] = $value;
603
604         if(get_config($family,$key) === false) {
605                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
606                         dbesc($family),
607                         dbesc($key),
608                         dbesc($value)
609                 );
610                 if($ret) 
611                         return $value;
612                 return $ret;
613         }
614         $ret = q("SUPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
615                 dbesc($value),
616                 dbesc($family),
617                 dbesc($key)
618         );
619         if($ret)
620                 return $value;
621         return $ret;
622 }}