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