]> git.mxchange.org Git - friendica.git/blob - boot.php
ee1bba1cdd341f0cc1b14aba1387d826bd709f89
[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_ANY',  0);
14 define ( 'DIRECTION_IN',   1);
15 define ( 'DIRECTION_OUT',  2);
16 define ( 'DIRECTION_BOTH', 3);
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('$baseurl' => $this->get_baseurl()));
135         }
136
137 }}
138
139
140 if(! function_exists('x')) {
141 function x($s,$k = NULL) {
142         if($k != NULL) {
143                 if((is_array($s)) && (array_key_exists($k,$s))) {
144                         if($s[$k])
145                                 return (int) 1;
146                         return (int) 0;
147                 }
148                 return false;
149         }
150         else {          
151                 if(isset($s)) {
152                         if($s) {
153                                 return (int) 1;
154                         }
155                         return (int) 0;
156                 }
157                 return false;
158         }
159 }}
160
161 if(! function_exists('system_unavailable')) {
162 function system_unavailable() {
163         include('system_unavailable.php');
164         killme();
165 }}
166
167
168 if(! function_exists('check_config')) {
169 function check_config(&$a) {
170         $r = q("SELECT * FROM `config` WHERE `cat` = 'system' AND `k` IN ('url','build')");
171         if(! count($r)) {
172                 q("INSERT INTO `config` (`cat`,`k`,`v`) VALUES ( 'system', 'url', '%s' )",
173                         dbesc($a->get_baseurl())
174                 );
175                 q("INSERT INTO `config` (`cat`,`k`,`v`) VALUES ( 'system', 'build', '%s' )",
176                         dbesc(BUILD_ID)
177                 );
178                 return;
179         }
180         foreach($r as $rr) {
181                 if($rr['k'] == 'build') {
182                         $stored = intval($rr['v']);
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                                 q("UPDATE `config` SET `v` = '%s' WHERE `cat` = 'system' AND `k` = 'build' LIMIT 1",
197                                         dbesc(BUILD_ID)
198                                 );
199                         }
200                 }
201         }
202         return;
203 }}
204
205
206
207 if(! function_exists('replace_macros')) {  
208 function replace_macros($s,$r) {
209
210         $search = array();
211         $replace = array();
212
213         if(is_array($r) && count($r)) {
214                 foreach ($r as $k => $v ) {
215                         $search[] =  $k;
216                         $replace[] = $v;
217                 }
218         }
219         return str_replace($search,$replace,$s);
220 }}
221
222
223 if(! function_exists('load_translation_table')) {
224 function load_translation_table($lang) {
225         global $a;
226
227 }}
228
229 if(! function_exists('t')) {
230 function t($s) {
231         global $a;
232
233         if($a->strings[$s])
234                 return $a->strings[$s];
235         return $s;
236 }}
237
238 if(! function_exists('fetch_url')) {
239 function fetch_url($url,$binary = false) {
240         $ch = curl_init($url);
241         if(! $ch) return false;
242
243         curl_setopt($ch, CURLOPT_HEADER, 0);
244         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
245         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
246         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
247         if($binary)
248                 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
249
250         $s = curl_exec($ch);
251         curl_close($ch);
252         return($s);
253 }}
254
255
256 if(! function_exists('post_url')) {
257 function post_url($url,$params) {
258         $ch = curl_init($url);
259         if(! $ch) return false;
260
261         curl_setopt($ch, CURLOPT_HEADER, 0);
262         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
263         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
264         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
265         curl_setopt($ch, CURLOPT_POST,1);
266         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
267
268         $s = curl_exec($ch);
269         curl_close($ch);
270         return($s);
271 }}
272
273
274 if(! function_exists('random_string')) {
275 function random_string() {
276         return(hash('sha256',uniqid(rand(),true)));
277 }}
278
279 if(! function_exists('notags')) {
280 function notags($string) {
281         // protect against :<> with high-bit set
282         return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
283 }}
284
285 if(! function_exists('escape_tags')) {
286 function escape_tags($string) {
287
288         return(htmlspecialchars($string));
289 }}
290
291 if(! function_exists('login')) {
292 function login($register = false) {
293         $o = "";
294         $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
295
296
297         if(x($_SESSION,'authenticated')) {
298                 $o = file_get_contents("view/logout.tpl");
299         }
300         else {
301                 $o = file_get_contents("view/login.tpl");
302
303                 $o = replace_macros($o,array('$register_html' => $register_html ));
304         }
305         return $o;
306 }}
307
308
309 if(! function_exists('autoname')) {
310 function autoname($len) {
311
312         $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'); 
313         if(mt_rand(0,5) == 4)
314                 $vowels[] = 'y';
315
316         $cons = array(
317                         'b','bl','br',
318                         'c','ch','cl','cr',
319                         'd','dr',
320                         'f','fl','fr',
321                         'g','gh','gl','gr',
322                         'h',
323                         'j',
324                         'k','kh','kl','kr',
325                         'l',
326                         'm',
327                         'n',
328                         'p','ph','pl','pr',
329                         'qu',
330                         'r','rh',
331                         's','sc','sh','sm','sp','st',
332                         't','th','tr',
333                         'v',
334                         'w','wh',
335                         'x',
336                         'z','zh'
337                         );
338
339         $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
340                                 'nd','ng','nk','nt','rn','rp','rt');
341
342         $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
343                                 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
344
345         $start = mt_rand(0,2);
346         if($start == 0)
347                 $table = $vowels;
348         else
349                 $table = $cons;
350
351         $word = '';
352
353         for ($x = 0; $x < $len; $x ++) {
354                 $r = mt_rand(0,count($table) - 1);
355                 $word .= $table[$r];
356   
357                 if($table == $vowels)
358                         $table = array_merge($cons,$midcons);
359                 else
360                         $table = $vowels;
361
362         }
363
364         $word = substr($word,0,$len);
365
366         foreach($noend as $noe) {
367                 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
368                         $word = substr($word,0,-1);
369                         break;
370                 }
371         }
372         if(substr($word,-1) == 'q')
373                 $word = substr($word,0,-1);    
374         return $word;
375 }}
376
377 if(! function_exists('killme')) {
378 function killme() {
379         session_write_close();
380         exit;
381 }}
382
383 if(! function_exists('goaway')) {
384 function goaway($s) {
385         header("Location: $s");
386         killme();
387 }}
388
389
390 if(! function_exists('xml_status')) {
391 function xml_status($st) {
392         header( "Content-type: text/xml" );
393         echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
394         echo "<result><status>$st</status></result>\r\n";
395         killme();
396 }}
397
398 if(! function_exists('local_user')) {
399 function local_user() {
400         if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
401                 return $_SESSION['uid'];
402         return false;
403 }}
404
405 if(! function_exists('remote_user')) {
406 function remote_user() {
407         if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
408                 return $_SESSION['visitor_id'];
409         return false;
410 }}
411
412 if(! function_exists('notice')) {
413 function notice($s) {
414
415         $_SESSION['sysmsg'] .= $s;
416
417 }}
418
419 if(! function_exists('get_max_import_size')) {
420 function get_max_import_size() {
421         global $a;
422         return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
423 }}
424
425 if(! function_exists('xmlify')) {
426 function xmlify($str) {
427         $buffer = '';
428         
429         for($x = 0; $x < strlen($str); $x ++) {
430                 $char = $str[$x];
431         
432                 switch( $char ) {
433
434                         case "\r" :
435                                 break;
436                         case "&" :
437                                 $buffer .= '&amp;';
438                                 break;
439                         case "'" :
440                                 $buffer .= '&apos;';
441                                 break;
442
443                         case "\"" :
444                                 $buffer .= '&quot;';
445                                 break;
446                         case '<' :
447                                 $buffer .= '&lt;';
448                                 break;
449                         case '>' :
450                                 $buffer .= '&gt;';
451                                 break;
452                         case "\n" :
453                                 $buffer .= ' ';
454                                 break;
455                         default :
456                                 $buffer .= $char;
457                                 break;
458                 }       
459         }
460         $buffer = trim($buffer);
461         return($buffer);
462 }}
463
464 if(! function_exists('unxmlify')) {
465 function unxmlify($s) {
466         $ret = str_replace('&amp;','&', $s);
467         $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
468         return $ret;    
469 }}
470
471 if(! function_exists('hex2bin')) {
472 function hex2bin($s) {
473         return(pack("H*",$s));
474 }}
475
476
477 if(! function_exists('paginate')) {
478 function paginate(&$a) {
479         $o = '';
480         $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
481         $stripped = str_replace('q=','',$stripped);
482         $stripped = trim($stripped,'/');
483         $url = $a->get_baseurl() . '/' . $stripped;
484
485
486           if($a->pager['total'] > $a->pager['itemspage']) {
487                 $o .= '<div class="pager">';
488                 if($a->pager['page'] != 1)
489                         $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
490
491                 $o .=  "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
492
493                 $numpages = $a->pager['total'] / $a->pager['itemspage'];
494
495                 $numstart = 1;
496                 $numstop = $numpages;
497
498                 if($numpages > 14) {
499                         $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
500                         $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
501                 }
502    
503                 for($i = $numstart; $i <= $numstop; $i++){
504                         if($i == $a->pager['page'])
505                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
506                         else
507                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
508                         $o .= '</span> ';
509                 }
510
511                 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
512                         if($i == $a->pager['page'])
513                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
514                         else
515                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
516                         $o .= '</span> ';
517                 }
518
519                 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
520                 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
521
522                 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
523                         $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
524                 $o .= '</div>'."\r\n";
525         }
526         return $o;
527 }}
528
529 if(! function_exists('expand_acl')) {
530 function expand_acl($s) {
531
532         if(strlen($s)) {
533                 $a = explode('<',$s);
534                 for($x = 0; $x < count($a); $x ++) {
535                         $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
536                 }
537                 return $a;
538         }
539         return array();
540 }}              
541
542 if(! function_exists('sanitise_acl')) {
543 function sanitise_acl(&$item) {
544         if(intval($item))
545                 $item = '<' . intval(notags(trim($item))) . '>';
546         else
547                 unset($item);
548 }}