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