]> git.mxchange.org Git - friendica.git/blob - index.php
4c98cd6e02c0428eab7429d31c121822f03e0c83
[friendica.git] / index.php
1 <?php
2
3
4 /**
5  *
6  * Friendica
7  *
8  */
9
10 /**
11  *
12  * bootstrap the application
13  *
14  */
15
16 require_once('boot.php');
17 require_once('object/BaseObject.php');
18
19 $a = new App;
20 BaseObject::set_app($a);
21
22 /**
23  *
24  * Load the configuration file which contains our DB credentials.
25  * Ignore errors. If the file doesn't exist or is empty, we are running in installation mode.
26  *
27  */
28
29 $install = ((file_exists('.htconfig.php') && filesize('.htconfig.php')) ? false : true);
30
31 @include(".htconfig.php");
32
33
34
35 $lang = get_browser_language();
36         
37 load_translation_table($lang);
38
39 /**
40  *
41  * Try to open the database;
42  *
43  */
44
45 require_once("include/dba.php");
46
47 if(!$install) {
48         $db = new dba($db_host, $db_user, $db_pass, $db_data, $install);
49             unset($db_host, $db_user, $db_pass, $db_data);
50
51         /**
52          * Load configs from db. Overwrite configs from .htconfig.php
53          */
54
55         load_config('config');
56         load_config('system');
57
58         require_once("include/session.php");
59         load_hooks();
60         call_hooks('init_1');
61 }
62
63 $maintenance = get_config('system', 'maintenance');
64
65
66 /**
67  *
68  * Important stuff we always need to do.
69  *
70  * The order of these may be important so use caution if you think they're all
71  * intertwingled with no logical order and decide to sort it out. Some of the
72  * dependencies have changed, but at least at one time in the recent past - the 
73  * order was critical to everything working properly
74  *
75  */
76
77 session_start();
78
79 /**
80  * Language was set earlier, but we can over-ride it in the session.
81  * We have to do it here because the session was just now opened.
82  */
83
84 if(array_key_exists('system_language',$_POST)) {
85         if(strlen($_POST['system_language']))
86                 $_SESSION['language'] = $_POST['system_language'];
87         else
88                 unset($_SESSION['language']);
89 }
90 if((x($_SESSION,'language')) && ($_SESSION['language'] !== $lang)) {
91         $lang = $_SESSION['language'];
92         load_translation_table($lang);
93 }
94
95 if((x($_GET,'zrl')) && (!$install && !$maintenance)) {
96         $_SESSION['my_url'] = $_GET['zrl'];
97         $a->query_string = preg_replace('/[\?&]zrl=(.*?)([\?&]|$)/is','',$a->query_string);
98         zrl_init($a);
99 }
100
101 /**
102  *
103  * For Mozilla auth manager - still needs sorting, and this might conflict with LRDD header.
104  * Apache/PHP lumps the Link: headers into one - and other services might not be able to parse it
105  * this way. There's a PHP flag to link the headers because by default this will over-write any other 
106  * link header. 
107  *
108  * What we really need to do is output the raw headers ourselves so we can keep them separate.
109  *
110  */
111  
112 // header('Link: <' . $a->get_baseurl() . '/amcd>; rel="acct-mgmt";');
113
114 if((x($_SESSION,'authenticated')) || (x($_POST,'auth-params')) || ($a->module === 'login'))
115         require("include/auth.php");
116
117 if(! x($_SESSION,'authenticated'))
118         header('X-Account-Management-Status: none');
119
120
121 /* set up page['htmlhead'] and page['end'] for the modules to use */
122 $a->page['htmlhead'] = '';
123 $a->page['end'] = '';
124
125
126 if(! x($_SESSION,'sysmsg'))
127         $_SESSION['sysmsg'] = array();
128
129 if(! x($_SESSION,'sysmsg_info'))
130         $_SESSION['sysmsg_info'] = array();
131
132 /*
133  * check_config() is responsible for running update scripts. These automatically 
134  * update the DB schema whenever we push a new one out. It also checks to see if
135  * any plugins have been added or removed and reacts accordingly. 
136  */
137
138
139 if($install)
140         $a->module = 'install';
141 elseif($maintenance)
142         $a->module = 'maintenance';
143 else
144         proc_run('php', 'include/dbupdate.php');
145
146 nav_set_selected('nothing');
147
148 $arr = array('app_menu' => $a->apps);
149
150 call_hooks('app_menu', $arr);
151
152 $a->apps = $arr['app_menu'];
153
154 /**
155  *
156  * We have already parsed the server path into $a->argc and $a->argv
157  *
158  * $a->argv[0] is our module name. We will load the file mod/{$a->argv[0]}.php
159  * and use it for handling our URL request.
160  * The module file contains a few functions that we call in various circumstances
161  * and in the following order:
162  * 
163  * "module"_init
164  * "module"_post (only called if there are $_POST variables)
165  * "module"_afterpost
166  * "module"_content - the string return of this function contains our page body
167  *
168  * Modules which emit other serialisations besides HTML (XML,JSON, etc.) should do 
169  * so within the module init and/or post functions and then invoke killme() to terminate
170  * further processing.
171  */
172
173 if(strlen($a->module)) {
174
175         /**
176          *
177          * We will always have a module name.
178          * First see if we have a plugin which is masquerading as a module.
179          *
180          */
181
182         if(is_array($a->plugins) && in_array($a->module,$a->plugins) && file_exists("addon/{$a->module}/{$a->module}.php")) {
183                 include_once("addon/{$a->module}/{$a->module}.php");
184                 if(function_exists($a->module . '_module'))
185                         $a->module_loaded = true;
186         }
187
188         /**
189          * If not, next look for a 'standard' program module in the 'mod' directory
190          */
191
192         if((! $a->module_loaded) && (file_exists("mod/{$a->module}.php"))) {
193                 include_once("mod/{$a->module}.php");
194                 $a->module_loaded = true;
195         }
196
197         /**
198          *
199          * The URL provided does not resolve to a valid module.
200          *
201          * On Dreamhost sites, quite often things go wrong for no apparent reason and they send us to '/internal_error.html'. 
202          * We don't like doing this, but as it occasionally accounts for 10-20% or more of all site traffic - 
203          * we are going to trap this and redirect back to the requested page. As long as you don't have a critical error on your page
204          * this will often succeed and eventually do the right thing.
205          *
206          * Otherwise we are going to emit a 404 not found.
207          *
208          */
209
210         if(! $a->module_loaded) {
211
212                 // Stupid browser tried to pre-fetch our Javascript img template. Don't log the event or return anything - just quietly exit.
213                 if((x($_SERVER,'QUERY_STRING')) && preg_match('/{[0-9]}/',$_SERVER['QUERY_STRING']) !== 0) {
214                         killme();
215                 }
216
217                 if((x($_SERVER,'QUERY_STRING')) && ($_SERVER['QUERY_STRING'] === 'q=internal_error.html') && isset($dreamhost_error_hack)) {
218                         logger('index.php: dreamhost_error_hack invoked. Original URI =' . $_SERVER['REQUEST_URI']);
219                         goaway($a->get_baseurl() . $_SERVER['REQUEST_URI']);
220                 }
221
222                 logger('index.php: page not found: ' . $_SERVER['REQUEST_URI'] . ' ADDRESS: ' . $_SERVER['REMOTE_ADDR'] . ' QUERY: ' . $_SERVER['QUERY_STRING'], LOGGER_DEBUG);
223                 header($_SERVER["SERVER_PROTOCOL"] . ' 404 ' . t('Not Found'));
224                 $tpl = get_markup_template("404.tpl");
225                 $a->page['content'] = replace_macros($tpl, array(
226                         '$message' =>  t('Page not found.' )
227                 ));
228         }
229 }
230
231 /**
232  * load current theme info
233  */
234 $theme_info_file = "view/theme/".current_theme()."/theme.php";
235 if (file_exists($theme_info_file)){
236         require_once($theme_info_file);
237 }
238
239
240 /* initialise content region */
241
242 if(! x($a->page,'content'))
243         $a->page['content'] = '';
244
245 if(!$install && !$maintenance)
246         call_hooks('page_content_top',$a->page['content']);
247
248 /**
249  * Call module functions
250  */
251
252 if($a->module_loaded) {
253         $a->page['page_title'] = $a->module;
254         $placeholder = '';
255
256         if(function_exists($a->module . '_init')) {
257                 call_hooks($a->module . '_mod_init', $placeholder);
258                 $func = $a->module . '_init';
259                 $func($a);
260         }
261
262         if(function_exists(str_replace('-','_',current_theme()) . '_init')) {
263                 $func = str_replace('-','_',current_theme()) . '_init';
264                 $func($a);
265         }
266 //      elseif (x($a->theme_info,"extends") && file_exists("view/theme/".$a->theme_info["extends"]."/theme.php")) {
267 //              require_once("view/theme/".$a->theme_info["extends"]."/theme.php");
268 //              if(function_exists(str_replace('-','_',$a->theme_info["extends"]) . '_init')) {
269 //                      $func = str_replace('-','_',$a->theme_info["extends"]) . '_init';
270 //                      $func($a);
271 //              }
272 //      }
273
274         if(($_SERVER['REQUEST_METHOD'] === 'POST') && (! $a->error)
275                 && (function_exists($a->module . '_post'))
276                 && (! x($_POST,'auth-params'))) {
277                 call_hooks($a->module . '_mod_post', $_POST);
278                 $func = $a->module . '_post';
279                 $func($a);
280         }
281
282         if((! $a->error) && (function_exists($a->module . '_afterpost'))) {
283                 call_hooks($a->module . '_mod_afterpost',$placeholder);
284                 $func = $a->module . '_afterpost';
285                 $func($a);
286         }
287
288         if((! $a->error) && (function_exists($a->module . '_content'))) {
289                 $arr = array('content' => $a->page['content']);
290                 call_hooks($a->module . '_mod_content', $arr);
291                 $a->page['content'] = $arr['content'];
292                 $func = $a->module . '_content';
293                 $arr = array('content' => $func($a));
294                 call_hooks($a->module . '_mod_aftercontent', $arr);
295                 $a->page['content'] .= $arr['content'];
296         }
297
298         if(function_exists(str_replace('-','_',current_theme()) . '_content_loaded')) {
299                 $func = str_replace('-','_',current_theme()) . '_content_loaded';
300                 $func($a);
301         }
302
303 }
304
305
306 /*
307  * Create the page head after setting the language
308  * and getting any auth credentials
309  *
310  * Moved init_pagehead() and init_page_end() to after
311  * all the module functions have executed so that all
312  * theme choices made by the modules can take effect
313  */
314
315 $a->init_pagehead();
316
317 /**
318  * Build the page ending -- this is stuff that goes right before
319  * the closing </body> tag
320  */
321
322 $a->init_page_end();
323
324 // If you're just visiting, let javascript take you home
325
326 if(x($_SESSION,'visitor_home'))
327         $homebase = $_SESSION['visitor_home'];
328 elseif(local_user())
329         $homebase = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
330
331 if(isset($homebase))
332         $a->page['content'] .= '<script>var homebase="' . $homebase . '" ; </script>';
333
334 // now that we've been through the module content, see if the page reported
335 // a permission problem and if so, a 403 response would seem to be in order.
336
337 if(stristr( implode("",$_SESSION['sysmsg']), t('Permission denied'))) {
338         header($_SERVER["SERVER_PROTOCOL"] . ' 403 ' . t('Permission denied.'));
339 }
340
341 /**
342  *
343  * Report anything which needs to be communicated in the notification area (before the main body)
344  *
345  */
346         
347 /*if(x($_SESSION,'sysmsg')) {
348         $a->page['content'] = "<div id=\"sysmsg\" class=\"error-message\">{$_SESSION['sysmsg']}</div>\r\n"
349                 . ((x($a->page,'content')) ? $a->page['content'] : '');
350         $_SESSION['sysmsg']="";
351         unset($_SESSION['sysmsg']);
352 }
353 if(x($_SESSION,'sysmsg_info')) {
354         $a->page['content'] = "<div id=\"sysmsg_info\" class=\"info-message\">{$_SESSION['sysmsg_info']}</div>\r\n"
355                 . ((x($a->page,'content')) ? $a->page['content'] : '');
356         $_SESSION['sysmsg_info']="";
357         unset($_SESSION['sysmsg_info']);
358 }*/
359
360
361
362 call_hooks('page_end', $a->page['content']);
363
364
365 /**
366  *
367  * Add a place for the pause/resume Ajax indicator
368  *
369  */
370
371 $a->page['content'] .=  '<div id="pause"></div>';
372
373
374 /**
375  *
376  * Add the navigation (menu) template
377  *
378  */
379
380 if($a->module != 'install' && $a->module != 'maintenance') {
381         nav($a);
382 }
383
384 /**
385  * Add a "toggle mobile" link if we're using a mobile device
386  */
387
388 if($a->is_mobile || $a->is_tablet) {
389         if(isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
390                 $link = $a->get_baseurl() . '/toggle_mobile?address=' . curPageURL();
391         }
392         else {
393                 $link = $a->get_baseurl() . '/toggle_mobile?off=1&address=' . curPageURL();
394         }
395         $a->page['footer'] = replace_macros(get_markup_template("toggle_mobile_footer.tpl"), array(
396                                 '$toggle_link' => $link,
397                                 '$toggle_text' => t('toggle mobile')
398                          ));
399 }
400
401 /**
402  * Build the page - now that we have all the components
403  */
404
405 if(!$a->theme['stylesheet'])
406         $stylesheet = current_theme_url();
407 else
408         $stylesheet = $a->theme['stylesheet'];
409 $a->page['htmlhead'] = replace_macros($a->page['htmlhead'], array('$stylesheet' => $stylesheet));
410
411 $page    = $a->page;
412 $profile = $a->profile;
413
414 header("Content-type: text/html; charset=utf-8");
415
416 $template = 'view/theme/' . current_theme() . '/' 
417         . ((x($a->page,'template')) ? $a->page['template'] : 'default' ) . '.php';
418
419 if(file_exists($template))
420         require_once($template);
421 else
422         require_once(str_replace('theme/' . current_theme() . '/', '', $template));
423
424 session_write_close();
425 exit;