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