]> git.mxchange.org Git - friendica.git/blob - boot.php
0633a828f6760d9f1c5084ef5384ea4d5cf24f5c
[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(
194                 array('&', '"', "'", '<', '>'), 
195                 array('&amp;', '&quot;', '&#39;', '&lt;', '&gt;'), $string));
196 }}
197
198 if(! function_exists('login')) {
199 function login($register = false) {
200         $o = "";
201         $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
202
203
204         if(x($_SESSION,'authenticated')) {
205                 $o = file_get_contents("view/logout.tpl");
206         }
207         else {
208                 $o = file_get_contents("view/login.tpl");
209
210                 $o = replace_macros($o,array('$register_html' => $register_html ));
211         }
212         return $o;
213 }}
214
215
216 if(! function_exists('autoname')) {
217 function autoname($len) {
218
219         $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'); 
220         if(mt_rand(0,5) == 4)
221                 $vowels[] = 'y';
222
223         $cons = array(
224                         'b','bl','br',
225                         'c','ch','cl','cr',
226                         'd','dr',
227                         'f','fl','fr',
228                         'g','gh','gl','gr',
229                         'h',
230                         'j',
231                         'k','kh','kl','kr',
232                         'l',
233                         'm',
234                         'n',
235                         'p','ph','pl','pr',
236                         'qu',
237                         'r','rh',
238                         's','sc','sh','sm','sp','st',
239                         't','th','tr',
240                         'v',
241                         'w','wh',
242                         'x',
243                         'z','zh'
244                         );
245
246         $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
247                                 'nd','ng','nk','nt','rn','rp','rt');
248
249         $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
250                                 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
251
252         $start = mt_rand(0,2);
253         if($start == 0)
254                 $table = $vowels;
255         else
256                 $table = $cons;
257
258         $word = '';
259
260         for ($x = 0; $x < $len; $x ++) {
261                 $r = mt_rand(0,count($table) - 1);
262                 $word .= $table[$r];
263   
264                 if($table == $vowels)
265                         $table = array_merge($cons,$midcons);
266                 else
267                         $table = $vowels;
268
269         }
270
271         $word = substr($word,0,$len);
272
273         foreach($noend as $noe) {
274                 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
275                         $word = substr($word,0,-1);
276                         break;
277                 }
278         }
279         if(substr($word,-1) == 'q')
280                 $word = substr($word,0,-1);    
281         return $word;
282 }}
283
284 if(! function_exists('killme')) {
285 function killme() {
286         session_write_close();
287         exit;
288 }}
289
290 if(! function_exists('goaway')) {
291 function goaway($s) {
292         header("Location: $s");
293         killme();
294 }}
295
296
297 if(! function_exists('xml_status')) {
298 function xml_status($st) {
299         header( "Content-type: text/xml");
300         echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
301         echo "<result><status>$st</status></result>\r\n";
302         killme();
303 }}
304
305 if(! function_exists('local_user')) {
306 function local_user() {
307         if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
308                 return $_SESSION['uid'];
309         return false;
310 }}
311
312 if(! function_exists('remote_user')) {
313 function remote_user() {
314         if((x($_SESSION,'authenticated')) && (x($_SESSION,'cid')))
315                 return $_SESSION['cid'];
316         return false;
317 }}
318
319 function footer(&$a) {
320
321         $s = fetch_url("http://fortunemod.com/cookie.php?equal=1");
322         $a->page['content'] .= "<div class=\"fortune\" >$s</div>"; 
323 }
324
325 if(! function_exists('notice')) {
326 function notice($s) {
327
328         $_SESSION['sysmsg'] .= $s;
329
330 }}
331
332
333 if(! function_exists('xmlify')) {
334 function xmlify($str) {
335         $buffer = '';
336         
337         for($x = 0; $x < strlen($str); $x ++) {
338                 $char = $str[$x];
339         
340                 switch( $char ) {
341
342                         case "\r" :
343                                 break;
344                         case "&" :
345                                 $buffer .= '&amp;';
346                                 break;
347                         case "'" :
348                                 $buffer .= '&apos;';
349                                 break;
350
351                         case "\"" :
352                                 $buffer .= '&quot;';
353                                 break;
354                         case '<' :
355                                 $buffer .= '&lt;';
356                                 break;
357                         case '>' :
358                                 $buffer .= '&gt;';
359                                 break;
360                         case "\n" :
361                                 $buffer .= ' ';
362                                 break;
363                         default :
364                                 $buffer .= $char;
365                                 break;
366                 }       
367         }
368         $buffer = trim($buffer);
369         return($buffer);
370 }}
371
372
373 function hex2bin($s) {
374         return(pack("H*",$s));
375 }
376
377
378 function paginate(&$a) {
379         $o = '';
380         $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
381         $stripped = str_replace('q=','',$stripped);
382         $stripped = trim($stripped,'/');
383         $url = $a->get_baseurl() . '/' . $stripped;
384
385
386           if($a->pager['total'] > $a->pager['itemspage']) {
387                 $o .= '<div class="pager">';
388                 if($a->pager['page'] != 1)
389                         $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">prev</a></span> ';
390
391                 $o .=  "<span class=\"pager_first\"><a href=\"$url"."&page=1\">first</a></span> ";
392
393                 $numpages = $a->pager['total'] / $a->pager['itemspage'];
394
395                 $numstart = 1;
396                 $numstop = $numpages;
397
398                 if($numpages > 14) {
399                         $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
400                         $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
401                 }
402    
403                 for($i = $numstart; $i <= $numstop; $i++){
404                         if($i == $a->pager['page'])
405                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
406                         else
407                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
408                         $o .= '</span> ';
409                 }
410
411                 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
412                         if($i == $a->pager['page'])
413                                 $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
414                         else
415                                 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
416                         $o .= '</span> ';
417                 }
418
419                 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
420                 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">last</a></span> ";
421
422                 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
423                         $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">next</a></span>';
424                 $o .= '</div>'."\r\n";
425         }
426         return $o;
427 }