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