]> git.mxchange.org Git - friendica.git/blob - boot.php
eae44c672955a9c3c78c892bbf9523e36a567827
[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         $build = get_config('system','build');
174         if(! x($build))
175                 $build = set_config('system','build',BUILD_ID);
176
177         $url = get_config('system','url');
178         if(! x($url))
179                 $url = set_config('system','url',$a->get_baseurl());
180
181         if($build != BUILD_ID) {
182                 $stored = intval($build);
183                 $current = intval(BUILD_ID);
184                 if(($stored < $current) && file_exists('update.php')) {
185
186                         // We're reporting a different version than what is currently installed.
187                         // Run any existing update scripts to bring the database up to current.
188
189                         require_once('update.php');
190                         for($x = $stored; $x <= $current; $x ++) {
191                                 if(function_exists('update_' . $x)) {
192                                         $func = 'update_' . $x;
193                                         $func($a);
194                                 }
195                         }
196                         set_config('system','build', BUILD_ID);
197                 }
198         }
199         return;
200 }}
201
202
203
204 if(! function_exists('replace_macros')) {  
205 function replace_macros($s,$r) {
206
207         $search = array();
208         $replace = array();
209
210         if(is_array($r) && count($r)) {
211                 foreach ($r as $k => $v ) {
212                         $search[] =  $k;
213                         $replace[] = $v;
214                 }
215         }
216         return str_replace($search,$replace,$s);
217 }}
218
219
220 if(! function_exists('load_translation_table')) {
221 function load_translation_table($lang) {
222         global $a;
223
224 }}
225
226 if(! function_exists('t')) {
227 function t($s) {
228         global $a;
229
230         if($a->strings[$s])
231                 return $a->strings[$s];
232         return $s;
233 }}
234
235 if(! function_exists('fetch_url')) {
236 function fetch_url($url,$binary = false) {
237         $ch = curl_init($url);
238         if(! $ch) return false;
239
240         curl_setopt($ch, CURLOPT_HEADER, 0);
241         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
242         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
243         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
244         $prx = get_config('system','proxy');
245         if(strlen($prx)) {
246                 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
247                 curl_setopt($ch, CURLOPT_PROXY, $prx);
248                 $prxusr = get_config('system','proxyuser');
249                 if(strlen($prxusr))
250                         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
251         }
252         if($binary)
253                 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
254
255         $s = curl_exec($ch);
256         curl_close($ch);
257         return($s);
258 }}
259
260
261 if(! function_exists('post_url')) {
262 function post_url($url,$params) {
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         curl_setopt($ch, CURLOPT_POST,1);
271         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
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
281         $s = curl_exec($ch);
282         curl_close($ch);
283         return($s);
284 }}
285
286
287 if(! function_exists('random_string')) {
288 function random_string() {
289         return(hash('sha256',uniqid(rand(),true)));
290 }}
291
292 if(! function_exists('notags')) {
293 function notags($string) {
294         // protect against :<> with high-bit set
295         return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
296 }}
297
298 if(! function_exists('escape_tags')) {
299 function escape_tags($string) {
300
301         return(htmlspecialchars($string));
302 }}
303
304 if(! function_exists('login')) {
305 function login($register = false) {
306         $o = "";
307         $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
308
309
310         if(x($_SESSION,'authenticated')) {
311                 $o = file_get_contents("view/logout.tpl");
312         }
313         else {
314                 $o = file_get_contents("view/login.tpl");
315
316                 $o = replace_macros($o,array('$register_html' => $register_html ));
317         }
318         return $o;
319 }}
320
321
322 if(! function_exists('autoname')) {
323 function autoname($len) {
324
325         $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'); 
326         if(mt_rand(0,5) == 4)
327                 $vowels[] = 'y';
328
329         $cons = array(
330                         'b','bl','br',
331                         'c','ch','cl','cr',
332                         'd','dr',
333                         'f','fl','fr',
334                         'g','gh','gl','gr',
335                         'h',
336                         'j',
337                         'k','kh','kl','kr',
338                         'l',
339                         'm',
340                         'n',
341                         'p','ph','pl','pr',
342                         'qu',
343                         'r','rh',
344                         's','sc','sh','sm','sp','st',
345                         't','th','tr',
346                         'v',
347                         'w','wh',
348                         'x',
349                         'z','zh'
350                         );
351
352         $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
353                                 'nd','ng','nk','nt','rn','rp','rt');
354
355         $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
356                                 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
357
358         $start = mt_rand(0,2);
359         if($start == 0)
360                 $table = $vowels;
361         else
362                 $table = $cons;
363
364         $word = '';
365
366         for ($x = 0; $x < $len; $x ++) {
367                 $r = mt_rand(0,count($table) - 1);
368                 $word .= $table[$r];
369   
370                 if($table == $vowels)
371                         $table = array_merge($cons,$midcons);
372                 else
373                         $table = $vowels;
374
375         }
376
377         $word = substr($word,0,$len);
378
379         foreach($noend as $noe) {
380                 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
381                         $word = substr($word,0,-1);
382                         break;
383                 }
384         }
385         if(substr($word,-1) == 'q')
386                 $word = substr($word,0,-1);    
387         return $word;
388 }}
389
390 if(! function_exists('killme')) {
391 function killme() {
392         session_write_close();
393         exit;
394 }}
395
396 if(! function_exists('goaway')) {
397 function goaway($s) {
398         header("Location: $s");
399         killme();
400 }}
401
402
403 if(! function_exists('xml_status')) {
404 function xml_status($st) {
405         header( "Content-type: text/xml" );
406         echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
407         echo "<result><status>$st</status></result>\r\n";
408         killme();
409 }}
410
411 if(! function_exists('local_user')) {
412 function local_user() {
413         if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
414                 return $_SESSION['uid'];
415         return false;
416 }}
417
418 if(! function_exists('remote_user')) {
419 function remote_user() {
420         if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
421                 return $_SESSION['visitor_id'];
422         return false;
423 }}
424
425 if(! function_exists('notice')) {
426 function notice($s) {
427
428         $_SESSION['sysmsg'] .= $s;
429
430 }}
431
432 if(! function_exists('get_max_import_size')) {
433 function get_max_import_size() {
434         global $a;
435         return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
436 }}
437
438 if(! function_exists('xmlify')) {
439 function xmlify($str) {
440         $buffer = '';
441         
442         for($x = 0; $x < strlen($str); $x ++) {
443                 $char = $str[$x];
444         
445                 switch( $char ) {
446
447                         case "\r" :
448                                 break;
449                         case "&" :
450                                 $buffer .= '&amp;';
451                                 break;
452                         case "'" :
453                                 $buffer .= '&apos;';
454                                 break;
455
456                         case "\"" :
457                                 $buffer .= '&quot;';
458                                 break;
459                         case '<' :
460                                 $buffer .= '&lt;';
461                                 break;
462                         case '>' :
463                                 $buffer .= '&gt;';
464                                 break;
465                         case "\n" :
466                                 $buffer .= ' ';
467                                 break;
468                         default :
469                                 $buffer .= $char;
470                                 break;
471                 }       
472         }
473         $buffer = trim($buffer);
474         return($buffer);
475 }}
476
477 if(! function_exists('unxmlify')) {
478 function unxmlify($s) {
479         $ret = str_replace('&amp;','&', $s);
480         $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
481         return $ret;    
482 }}
483
484 if(! function_exists('hex2bin')) {
485 function hex2bin($s) {
486         return(pack("H*",$s));
487 }}
488
489
490 if(! function_exists('paginate')) {
491 function paginate(&$a) {
492         $o = '';
493         $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
494         $stripped = str_replace('q=','',$stripped);
495         $stripped = trim($stripped,'/');
496         $url = $a->get_baseurl() . '/' . $stripped;
497
498
499           if($a->pager['total'] > $a->pager['itemspage']) {
500                 $o .= '<div class="pager">';
501                 if($a->pager['page'] != 1)
502                         $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
503
504                 $o .=  "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
505
506                 $numpages = $a->pager['total'] / $a->pager['itemspage'];
507
508                 $numstart = 1;
509                 $numstop = $numpages;
510
511                 if($numpages > 14) {
512                         $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
513                         $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
514                 }
515    
516                 for($i = $numstart; $i <= $numstop; $i++){
517                         if($i == $a->pager['page'])
518                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
519                         else
520                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
521                         $o .= '</span> ';
522                 }
523
524                 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
525                         if($i == $a->pager['page'])
526                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
527                         else
528                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
529                         $o .= '</span> ';
530                 }
531
532                 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
533                 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
534
535                 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
536                         $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
537                 $o .= '</div>'."\r\n";
538         }
539         return $o;
540 }}
541
542 if(! function_exists('expand_acl')) {
543 function expand_acl($s) {
544
545         if(strlen($s)) {
546                 $a = explode('<',$s);
547                 for($x = 0; $x < count($a); $x ++) {
548                         $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
549                 }
550                 return $a;
551         }
552         return array();
553 }}              
554
555 if(! function_exists('sanitise_acl')) {
556 function sanitise_acl(&$item) {
557         if(intval($item))
558                 $item = '<' . intval(notags(trim($item))) . '>';
559         else
560                 unset($item);
561 }}
562
563 if(! function_exists('get_config')) {
564 function get_config($family,$key) {
565         $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
566                 dbesc($family),
567                 dbesc($key)
568         );
569         if(count($ret))
570                 return $ret[0]['v'];
571         return false;
572 }}
573
574 if(! function_exists('set_config')) {
575 function set_config($family,$key,$value) {
576         if(get_config($family,$key) === false) {
577                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
578                         dbesc($family),
579                         dbesc($key),
580                         dbesc($value)
581                 );
582                 if($ret) 
583                         return $value;
584                 return $ret;
585         }
586         $ret = q("SUPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
587                 dbesc($value),
588                 dbesc($family),
589                 dbesc($key)
590         );
591         if($ret)
592                 return $value;
593         return $ret;
594 }}