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