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