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