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