]> git.mxchange.org Git - friendica.git/blob - boot.php
doc
[friendica.git] / boot.php
1 <?php
2
3 set_time_limit(0);
4
5 define('EOL', '<br />');
6
7 define('REGISTER_CLOSED',  0);
8 define('REGISTER_APPROVE', 1);
9 define('REGISTER_OPEN',    2);
10
11
12 if(! class_exists('App')) {
13 class App {
14
15         public  $module_loaded = false;
16         public  $config;
17         public  $page;
18         public  $profile;
19         public  $user;
20         public  $content;
21         public  $error = false;
22         public  $cmd;
23         public  $argv;
24         public  $argc;
25         public  $module;
26
27         private $scheme;
28         private $hostname;
29         private $path;
30         private $db;
31
32         function __construct() {
33
34                 $this->config = array();
35                 $this->page = array();
36
37                 $this->scheme = ((isset($_SERVER['HTTPS']) 
38                                 && ($_SERVER['HTTPS'])) ?  'https' : 'http' );
39                 $this->hostname = str_replace('www.','',
40                                 $_SERVER['SERVER_NAME']);
41                 set_include_path("include/$this->hostname" 
42                                 . PATH_SEPARATOR . 'include' 
43                                 . PATH_SEPARATOR . '.' );
44
45                 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
46                         $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
47 //              $this->cmd = trim($_SERVER['QUERY_STRING'],'/');
48                 $this->cmd = trim($_GET['q'],'/');
49
50                 $this->argv = explode('/',$this->cmd);
51                 $this->argc = count($this->argv);
52                 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
53                         $this->module = $this->argv[0];
54                 }
55                 else {
56                         $this->module = 'home';
57                 }
58         }
59
60         function get_baseurl($ssl = false) {
61                 
62                 return (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
63                         . ((isset($this->path) && strlen($this->path)) 
64                         ? '/' . $this->path : '' );
65         }
66
67         function set_path($p) {
68                 $this->path = ltrim(trim($p),'/');
69         } 
70
71         function init_pagehead() {
72                 if(file_exists("view/head.tpl"))
73                         $s = file_get_contents("view/head.tpl");
74                 $this->page['htmlhead'] = replace_macros($s,array('$baseurl' => $this->get_baseurl()));
75         }
76
77 }}
78
79
80 if(! function_exists('x')) {
81 function x($s,$k = NULL) {
82         if($k != NULL) {
83                 if((is_array($s)) && (array_key_exists($k,$s))) {
84                         if($s[$k])
85                                 return (int) 1;
86                         return (int) 0;
87                 }
88                 return false;
89         }
90         else {          
91                 if(isset($s)) {
92                         if($s) {
93                                 return (int) 1;
94                         }
95                         return (int) 0;
96                 }
97                 return false;
98         }
99 }}
100
101 if(! function_exists('system_unavailable')) {
102 function system_unavailable() {
103         include('system_unavailable.php');
104         killme();
105 }}
106
107 if(! function_exists('replace_macros')) {  
108 function replace_macros($s,$r) {
109
110         $search = array();
111         $replace = array();
112
113         if(is_array($r) && count($r)) {
114                 foreach ($r as $k => $v ) {
115                         $search[] =  $k;
116                         $replace[] = $v;
117                 }
118         }
119         return str_replace($search,$replace,$s);
120 }}
121
122
123
124 if(! function_exists('fetch_url')) {
125 function fetch_url($url,$binary = false) {
126         $ch = curl_init($url);
127         if(! $ch) return false;
128
129         curl_setopt($ch, CURLOPT_HEADER, 0);
130         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
131         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
132         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
133         if($binary)
134                 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
135
136         $s = curl_exec($ch);
137         curl_close($ch);
138         return($s);
139 }}
140
141
142 if(! function_exists('post_url')) {
143 function post_url($url,$params) {
144         $ch = curl_init($url);
145         if(! $ch) return false;
146
147         curl_setopt($ch, CURLOPT_HEADER, 0);
148         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
149         curl_setopt($ch, CURLOPT_MAXREDIRS,8);
150         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
151         curl_setopt($ch, CURLOPT_POST,1);
152         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
153
154         $s = curl_exec($ch);
155         curl_close($ch);
156         return($s);
157 }}
158
159
160 if(! function_exists('random_string')) {
161 function random_string() {
162         return(hash('sha256',uniqid(rand(),true)));
163 }}
164
165 if(! function_exists('notags')) {
166 function notags($string) {
167         // protect against :<> with high-bit set
168         return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
169 }}
170
171 // The PHP built-in tag escape function has traditionally been buggy
172 if(! function_exists('escape_tags')) {
173 function escape_tags($string) {
174         return(str_replace(array("<",">","&"), array('&lt;','&gt;','&amp;'), $string));
175 }}
176
177 if(! function_exists('login')) {
178 function login($register = false) {
179         $o = "";
180         $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
181
182
183         if(x($_SESSION,'authenticated')) {
184                 $o = file_get_contents("view/logout.tpl");
185         }
186         else {
187                 $o = file_get_contents("view/login.tpl");
188
189                 $o = replace_macros($o,array('$register_html' => $register_html ));
190         }
191         return $o;
192 }}
193
194
195 if(! function_exists('autoname')) {
196 function autoname($len) {
197
198         $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'); 
199         if(mt_rand(0,5) == 4)
200                 $vowels[] = 'y';
201
202         $cons = array(
203                         'b','bl','br',
204                         'c','ch','cl','cr',
205                         'd','dr',
206                         'f','fl','fr',
207                         'g','gh','gl','gr',
208                         'h',
209                         'j',
210                         'k','kh','kl','kr',
211                         'l',
212                         'm',
213                         'n',
214                         'p','ph','pl','pr',
215                         'qu',
216                         'r','rh',
217                         's','sc','sh','sm','sp','st',
218                         't','th','tr',
219                         'v',
220                         'w','wh',
221                         'x',
222                         'z','zh'
223                         );
224
225         $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
226                                 'nd','ng','nk','nt','rn','rp','rt');
227
228         $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
229                                 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
230
231         $start = mt_rand(0,2);
232         if($start == 0)
233                 $table = $vowels;
234         else
235                 $table = $cons;
236
237         $word = '';
238
239         for ($x = 0; $x < $len; $x ++) {
240                 $r = mt_rand(0,count($table) - 1);
241                 $word .= $table[$r];
242   
243                 if($table == $vowels)
244                         $table = array_merge($cons,$midcons);
245                 else
246                         $table = $vowels;
247
248         }
249
250         $word = substr($word,0,$len);
251
252         foreach($noend as $noe) {
253                 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
254                         $word = substr($word,0,-1);
255                         break;
256                 }
257         }
258         if(substr($word,-1) == 'q')
259                 $word = substr($word,0,-1);    
260         return $word;
261 }}
262
263 if(! function_exists('killme')) {
264 function killme() {
265         session_write_close();
266         exit;
267 }}
268
269 if(! function_exists('goaway')) {
270 function goaway($s) {
271         header("Location: $s");
272         killme();
273 }}
274
275
276 if(! function_exists('xml_status')) {
277 function xml_status($st) {
278         header( "Content-type: text/xml");
279         echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
280         echo "<result><status>$st</status></result>\r\n";
281         killme();
282 }}
283
284 if(! function_exists('local_user')) {
285 function local_user() {
286         if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
287                 return $_SESSION['uid'];
288         return false;
289 }}
290
291 if(! function_exists('remote_user')) {
292 function remote_user() {
293         if((x($_SESSION,'authenticated')) && (x($_SESSION,'cid')))
294                 return $_SESSION['cid'];
295         return false;
296 }}
297
298 function footer(&$a) {
299
300         $s = fetch_url("http://fortunemod.com/cookie.php?equal=1");
301         $a->page['content'] .= "<div class=\"fortune\" >$s</div>"; 
302 }