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