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