]> git.mxchange.org Git - friendica.git/blob - boot.php
e70e2bf6b7c91a222064710c80807fad54f6a0e8
[friendica.git] / boot.php
1 <?php
2
3 set_time_limit(0);
4
5 define('EOL', "<br />\r\n");
6
7 define('REGISTER_CLOSED',  0);
8 define('REGISTER_APPROVE', 1);
9 define('REGISTER_OPEN',    2);
10
11 define ( 'DIRECTION_ANY',  0);
12 define ( 'DIRECTION_IN',   1);
13 define ( 'DIRECTION_OUT',  2);
14 define ( 'DIRECTION_BOTH', 3);
15
16 define ( 'NOTIFY_INTRO',   0x0001 );
17 define ( 'NOTIFY_CONFIRM', 0x0002 );
18 define ( 'NOTIFY_WALL',    0x0004 );
19 define ( 'NOTIFY_COMMENT', 0x0008 );
20 define ( 'NOTIFY_MAIL',    0x0010 );
21
22 define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' ); 
23
24
25 if(! class_exists('App')) {
26 class App {
27
28         public  $module_loaded = false;
29         public  $config;
30         public  $page;
31         public  $profile;
32         public  $user;
33         public  $cid;
34         public  $contact;
35         public  $content;
36         public  $data;
37         public  $error = false;
38         public  $cmd;
39         public  $argv;
40         public  $argc;
41         public  $module;
42         public  $pager;
43         public  $strings;   
44
45         private $scheme;
46         private $hostname;
47         private $path;
48         private $baseurl;
49         private $db;
50
51         function __construct() {
52
53                 $this->config = array();
54                 $this->page = array();
55                 $this->pager= array();
56
57                 $this->scheme = ((isset($_SERVER['HTTPS']) 
58                                 && ($_SERVER['HTTPS'])) ?  'https' : 'http' );
59                 $this->hostname = str_replace('www.','',
60                                 $_SERVER['SERVER_NAME']);
61                 set_include_path("include/$this->hostname" 
62                                 . PATH_SEPARATOR . 'include' 
63                                 . PATH_SEPARATOR . '.' );
64
65                 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
66                         $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
67                 $this->cmd = trim($_GET['q'],'/');
68
69
70                 $this->argv = explode('/',$this->cmd);
71                 $this->argc = count($this->argv);
72                 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
73                         $this->module = $this->argv[0];
74                 }
75                 else {
76                         $this->module = 'home';
77                 }
78
79                 if($this->cmd == '.well-known/host-meta')
80                         require_once('include/hostxrd.php');
81
82                 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
83                 $this->pager['itemspage'] = 50;
84                 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
85                 $this->pager['total'] = 0;
86         }
87
88         function get_baseurl($ssl = false) {
89                 if(strlen($this->baseurl))
90                         return $this->baseurl;
91
92                 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
93                         . ((isset($this->path) && strlen($this->path)) 
94                         ? '/' . $this->path : '' );
95                 return $this->baseurl;
96         }
97
98         function set_baseurl($url) {
99                 $this->baseurl = $url;
100         }
101
102         function get_hostname() {
103                 return $this->hostname;
104         }
105
106         function set_hostname($h) {
107                 $this->hostname = $h;
108         }
109
110         function set_path($p) {
111                 $this->path = ltrim(trim($p),'/');
112         } 
113
114         function get_path() {
115                 return $this->path;
116         }
117
118         function set_pager_total($n) {
119                 $this->pager['total'] = intval($n);
120         }
121
122         function set_pager_itemspage($n) {
123                 $this->pager['itemspage'] = intval($n);
124                 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
125
126         } 
127
128         function init_pagehead() {
129                 if(file_exists("view/head.tpl"))
130                         $s = file_get_contents("view/head.tpl");
131                 $this->page['htmlhead'] = replace_macros($s,array('$baseurl' => $this->get_baseurl()));
132         }
133
134 }}
135
136
137 if(! function_exists('x')) {
138 function x($s,$k = NULL) {
139         if($k != NULL) {
140                 if((is_array($s)) && (array_key_exists($k,$s))) {
141                         if($s[$k])
142                                 return (int) 1;
143                         return (int) 0;
144                 }
145                 return false;
146         }
147         else {          
148                 if(isset($s)) {
149                         if($s) {
150                                 return (int) 1;
151                         }
152                         return (int) 0;
153                 }
154                 return false;
155         }
156 }}
157
158 if(! function_exists('system_unavailable')) {
159 function system_unavailable() {
160         include('system_unavailable.php');
161         killme();
162 }}
163
164 if(! function_exists('replace_macros')) {  
165 function replace_macros($s,$r) {
166
167         $search = array();
168         $replace = array();
169
170         if(is_array($r) && count($r)) {
171                 foreach ($r as $k => $v ) {
172                         $search[] =  $k;
173                         $replace[] = $v;
174                 }
175         }
176         return str_replace($search,$replace,$s);
177 }}
178
179
180 if(! function_exists('load_translation_table')) {
181 function load_translation_table($lang) {
182         global $a;
183
184 }}
185
186 if(! function_exists('t')) {
187 function t($s) {
188         global $a;
189
190         if($a->strings[$s])
191                 return $a->strings[$s];
192         return $s;
193 }}
194
195 if(! function_exists('fetch_url')) {
196 function fetch_url($url,$binary = false) {
197         $ch = curl_init($url);
198         if(! $ch) return false;
199
200         curl_setopt($ch, CURLOPT_HEADER, 0);
201         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
202         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
203         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
204         if($binary)
205                 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
206
207         $s = curl_exec($ch);
208         curl_close($ch);
209         return($s);
210 }}
211
212
213 if(! function_exists('post_url')) {
214 function post_url($url,$params) {
215         $ch = curl_init($url);
216         if(! $ch) return false;
217
218         curl_setopt($ch, CURLOPT_HEADER, 0);
219         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
220         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
221         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
222         curl_setopt($ch, CURLOPT_POST,1);
223         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
224
225         $s = curl_exec($ch);
226         curl_close($ch);
227         return($s);
228 }}
229
230
231 if(! function_exists('random_string')) {
232 function random_string() {
233         return(hash('sha256',uniqid(rand(),true)));
234 }}
235
236 if(! function_exists('notags')) {
237 function notags($string) {
238         // protect against :<> with high-bit set
239         return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
240 }}
241
242 if(! function_exists('escape_tags')) {
243 function escape_tags($string) {
244
245         return(htmlspecialchars($string));
246 }}
247
248 if(! function_exists('login')) {
249 function login($register = false) {
250         $o = "";
251         $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
252
253
254         if(x($_SESSION,'authenticated')) {
255                 $o = file_get_contents("view/logout.tpl");
256         }
257         else {
258                 $o = file_get_contents("view/login.tpl");
259
260                 $o = replace_macros($o,array('$register_html' => $register_html ));
261         }
262         return $o;
263 }}
264
265
266 if(! function_exists('autoname')) {
267 function autoname($len) {
268
269         $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'); 
270         if(mt_rand(0,5) == 4)
271                 $vowels[] = 'y';
272
273         $cons = array(
274                         'b','bl','br',
275                         'c','ch','cl','cr',
276                         'd','dr',
277                         'f','fl','fr',
278                         'g','gh','gl','gr',
279                         'h',
280                         'j',
281                         'k','kh','kl','kr',
282                         'l',
283                         'm',
284                         'n',
285                         'p','ph','pl','pr',
286                         'qu',
287                         'r','rh',
288                         's','sc','sh','sm','sp','st',
289                         't','th','tr',
290                         'v',
291                         'w','wh',
292                         'x',
293                         'z','zh'
294                         );
295
296         $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
297                                 'nd','ng','nk','nt','rn','rp','rt');
298
299         $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
300                                 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
301
302         $start = mt_rand(0,2);
303         if($start == 0)
304                 $table = $vowels;
305         else
306                 $table = $cons;
307
308         $word = '';
309
310         for ($x = 0; $x < $len; $x ++) {
311                 $r = mt_rand(0,count($table) - 1);
312                 $word .= $table[$r];
313   
314                 if($table == $vowels)
315                         $table = array_merge($cons,$midcons);
316                 else
317                         $table = $vowels;
318
319         }
320
321         $word = substr($word,0,$len);
322
323         foreach($noend as $noe) {
324                 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
325                         $word = substr($word,0,-1);
326                         break;
327                 }
328         }
329         if(substr($word,-1) == 'q')
330                 $word = substr($word,0,-1);    
331         return $word;
332 }}
333
334 if(! function_exists('killme')) {
335 function killme() {
336         session_write_close();
337         exit;
338 }}
339
340 if(! function_exists('goaway')) {
341 function goaway($s) {
342         header("Location: $s");
343         killme();
344 }}
345
346
347 if(! function_exists('xml_status')) {
348 function xml_status($st) {
349         header( "Content-type: text/xml" );
350         echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
351         echo "<result><status>$st</status></result>\r\n";
352         killme();
353 }}
354
355 if(! function_exists('local_user')) {
356 function local_user() {
357         if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
358                 return $_SESSION['uid'];
359         return false;
360 }}
361
362 if(! function_exists('remote_user')) {
363 function remote_user() {
364         if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
365                 return $_SESSION['visitor_id'];
366         return false;
367 }}
368
369 function footer(&$a) {
370
371         $s = fetch_url("http://fortunemod.com/cookie.php?equal=1");
372         $a->page['content'] .= "<div class=\"fortune\" >$s</div>"; 
373 }
374
375 if(! function_exists('notice')) {
376 function notice($s) {
377
378         $_SESSION['sysmsg'] .= $s;
379
380 }}
381
382 if(! function_exists('get_max_import_size')) {
383 function get_max_import_size() {
384         global $a;
385         return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
386 }}
387
388 if(! function_exists('xmlify')) {
389 function xmlify($str) {
390         $buffer = '';
391         
392         for($x = 0; $x < strlen($str); $x ++) {
393                 $char = $str[$x];
394         
395                 switch( $char ) {
396
397                         case "\r" :
398                                 break;
399                         case "&" :
400                                 $buffer .= '&amp;';
401                                 break;
402                         case "'" :
403                                 $buffer .= '&apos;';
404                                 break;
405
406                         case "\"" :
407                                 $buffer .= '&quot;';
408                                 break;
409                         case '<' :
410                                 $buffer .= '&lt;';
411                                 break;
412                         case '>' :
413                                 $buffer .= '&gt;';
414                                 break;
415                         case "\n" :
416                                 $buffer .= ' ';
417                                 break;
418                         default :
419                                 $buffer .= $char;
420                                 break;
421                 }       
422         }
423         $buffer = trim($buffer);
424         return($buffer);
425 }}
426
427 if(! function_exists('unxmlify')) {
428 function unxmlify($s) {
429         $ret = str_replace('&amp;','&', $s);
430         $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
431         return $ret;    
432 }}
433
434 if(! function_exists('hex2bin')) {
435 function hex2bin($s) {
436         return(pack("H*",$s));
437 }}
438
439
440 if(! function_exists('paginate')) {
441 function paginate(&$a) {
442         $o = '';
443         $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
444         $stripped = str_replace('q=','',$stripped);
445         $stripped = trim($stripped,'/');
446         $url = $a->get_baseurl() . '/' . $stripped;
447
448
449           if($a->pager['total'] > $a->pager['itemspage']) {
450                 $o .= '<div class="pager">';
451                 if($a->pager['page'] != 1)
452                         $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
453
454                 $o .=  "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
455
456                 $numpages = $a->pager['total'] / $a->pager['itemspage'];
457
458                 $numstart = 1;
459                 $numstop = $numpages;
460
461                 if($numpages > 14) {
462                         $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
463                         $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
464                 }
465    
466                 for($i = $numstart; $i <= $numstop; $i++){
467                         if($i == $a->pager['page'])
468                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
469                         else
470                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
471                         $o .= '</span> ';
472                 }
473
474                 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
475                         if($i == $a->pager['page'])
476                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
477                         else
478                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
479                         $o .= '</span> ';
480                 }
481
482                 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
483                 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
484
485                 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
486                         $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
487                 $o .= '</div>'."\r\n";
488         }
489         return $o;
490 }}
491
492 if(! function_exists('expand_acl')) {
493 function expand_acl($s) {
494
495         if(strlen($s)) {
496                 $a = explode('<',$s);
497                 for($x = 0; $x < count($a); $x ++) {
498                         $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
499                 }
500                 return $a;
501         }
502         return array();
503 }}              
504
505 if(! function_exists('sanitise_acl')) {
506 function sanitise_acl(&$item) {
507         $item = '<' . intval(notags(trim($item))) . '>';
508 }}