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