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