]> git.mxchange.org Git - friendica.git/blob - index.php
Merge branch 'master' of https://github.com/friendica/friendica
[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         $maintenance = get_config('system', 'maintenance');
63 }
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         check_url($a);
145         check_db();
146         check_plugins($a);
147 }
148
149 nav_set_selected('nothing');
150
151 $arr = array('app_menu' => $a->apps);
152
153 call_hooks('app_menu', $arr);
154
155 $a->apps = $arr['app_menu'];
156
157 /**
158  *
159  * We have already parsed the server path into $a->argc and $a->argv
160  *
161  * $a->argv[0] is our module name. We will load the file mod/{$a->argv[0]}.php
162  * and use it for handling our URL request.
163  * The module file contains a few functions that we call in various circumstances
164  * and in the following order:
165  * 
166  * "module"_init
167  * "module"_post (only called if there are $_POST variables)
168  * "module"_afterpost
169  * "module"_content - the string return of this function contains our page body
170  *
171  * Modules which emit other serialisations besides HTML (XML,JSON, etc.) should do 
172  * so within the module init and/or post functions and then invoke killme() to terminate
173  * further processing.
174  */
175
176 if(strlen($a->module)) {
177
178         /**
179          *
180          * We will always have a module name.
181          * First see if we have a plugin which is masquerading as a module.
182          *
183          */
184
185         if(is_array($a->plugins) && in_array($a->module,$a->plugins) && file_exists("addon/{$a->module}/{$a->module}.php")) {
186                 include_once("addon/{$a->module}/{$a->module}.php");
187                 if(function_exists($a->module . '_module'))
188                         $a->module_loaded = true;
189         }
190
191         /**
192          * If not, next look for a 'standard' program module in the 'mod' directory
193          */
194
195         if((! $a->module_loaded) && (file_exists("mod/{$a->module}.php"))) {
196                 include_once("mod/{$a->module}.php");
197                 $a->module_loaded = true;
198         }
199
200         /**
201          *
202          * The URL provided does not resolve to a valid module.
203          *
204          * On Dreamhost sites, quite often things go wrong for no apparent reason and they send us to '/internal_error.html'. 
205          * We don't like doing this, but as it occasionally accounts for 10-20% or more of all site traffic - 
206          * 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
207          * this will often succeed and eventually do the right thing.
208          *
209          * Otherwise we are going to emit a 404 not found.
210          *
211          */
212
213         if(! $a->module_loaded) {
214
215                 // Stupid browser tried to pre-fetch our Javascript img template. Don't log the event or return anything - just quietly exit.
216                 if((x($_SERVER,'QUERY_STRING')) && preg_match('/{[0-9]}/',$_SERVER['QUERY_STRING']) !== 0) {
217                         killme();
218                 }
219
220                 if((x($_SERVER,'QUERY_STRING')) && ($_SERVER['QUERY_STRING'] === 'q=internal_error.html') && isset($dreamhost_error_hack)) {
221                         logger('index.php: dreamhost_error_hack invoked. Original URI =' . $_SERVER['REQUEST_URI']);
222                         goaway($a->get_baseurl() . $_SERVER['REQUEST_URI']);
223                 }
224
225                 logger('index.php: page not found: ' . $_SERVER['REQUEST_URI'] . ' ADDRESS: ' . $_SERVER['REMOTE_ADDR'] . ' QUERY: ' . $_SERVER['QUERY_STRING'], LOGGER_DEBUG);
226                 header($_SERVER["SERVER_PROTOCOL"] . ' 404 ' . t('Not Found'));
227                 $tpl = get_markup_template("404.tpl");
228                 $a->page['content'] = replace_macros($tpl, array(
229                         '$message' =>  t('Page not found.' )
230                 ));
231         }
232 }
233
234 /**
235  * load current theme info
236  */
237 $theme_info_file = "view/theme/".current_theme()."/theme.php";
238 if (file_exists($theme_info_file)){
239         require_once($theme_info_file);
240 }
241
242
243 /* initialise content region */
244
245 if(! x($a->page,'content'))
246         $a->page['content'] = '';
247
248 if(!$install && !$maintenance)
249         call_hooks('page_content_top',$a->page['content']);
250
251 /**
252  * Call module functions
253  */
254
255 if($a->module_loaded) {
256         $a->page['page_title'] = $a->module;
257         $placeholder = '';
258
259         if(function_exists($a->module . '_init')) {
260                 call_hooks($a->module . '_mod_init', $placeholder);
261                 $func = $a->module . '_init';
262                 $func($a);
263         }
264
265         if(function_exists(str_replace('-','_',current_theme()) . '_init')) {
266                 $func = str_replace('-','_',current_theme()) . '_init';
267                 $func($a);
268         }
269 //      elseif (x($a->theme_info,"extends") && file_exists("view/theme/".$a->theme_info["extends"]."/theme.php")) {
270 //              require_once("view/theme/".$a->theme_info["extends"]."/theme.php");
271 //              if(function_exists(str_replace('-','_',$a->theme_info["extends"]) . '_init')) {
272 //                      $func = str_replace('-','_',$a->theme_info["extends"]) . '_init';
273 //                      $func($a);
274 //              }
275 //      }
276
277         if(($_SERVER['REQUEST_METHOD'] === 'POST') && (! $a->error)
278                 && (function_exists($a->module . '_post'))
279                 && (! x($_POST,'auth-params'))) {
280                 call_hooks($a->module . '_mod_post', $_POST);
281                 $func = $a->module . '_post';
282                 $func($a);
283         }
284
285         if((! $a->error) && (function_exists($a->module . '_afterpost'))) {
286                 call_hooks($a->module . '_mod_afterpost',$placeholder);
287                 $func = $a->module . '_afterpost';
288                 $func($a);
289         }
290
291         if((! $a->error) && (function_exists($a->module . '_content'))) {
292                 $arr = array('content' => $a->page['content']);
293                 call_hooks($a->module . '_mod_content', $arr);
294                 $a->page['content'] = $arr['content'];
295                 $func = $a->module . '_content';
296                 $arr = array('content' => $func($a));
297                 call_hooks($a->module . '_mod_aftercontent', $arr);
298                 $a->page['content'] .= $arr['content'];
299         }
300
301         if(function_exists(str_replace('-','_',current_theme()) . '_content_loaded')) {
302                 $func = str_replace('-','_',current_theme()) . '_content_loaded';
303                 $func($a);
304         }
305
306 }
307
308
309 /*
310  * Create the page head after setting the language
311  * and getting any auth credentials
312  *
313  * Moved init_pagehead() and init_page_end() to after
314  * all the module functions have executed so that all
315  * theme choices made by the modules can take effect
316  */
317
318 $a->init_pagehead();
319
320 /**
321  * Build the page ending -- this is stuff that goes right before
322  * the closing </body> tag
323  */
324
325 $a->init_page_end();
326
327 // If you're just visiting, let javascript take you home
328
329 if(x($_SESSION,'visitor_home'))
330         $homebase = $_SESSION['visitor_home'];
331 elseif(local_user())
332         $homebase = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
333
334 if(isset($homebase))
335         $a->page['content'] .= '<script>var homebase="' . $homebase . '" ; </script>';
336
337 // now that we've been through the module content, see if the page reported
338 // a permission problem and if so, a 403 response would seem to be in order.
339
340 if(stristr( implode("",$_SESSION['sysmsg']), t('Permission denied'))) {
341         header($_SERVER["SERVER_PROTOCOL"] . ' 403 ' . t('Permission denied.'));
342 }
343
344 /**
345  *
346  * Report anything which needs to be communicated in the notification area (before the main body)
347  *
348  */
349         
350 /*if(x($_SESSION,'sysmsg')) {
351         $a->page['content'] = "<div id=\"sysmsg\" class=\"error-message\">{$_SESSION['sysmsg']}</div>\r\n"
352                 . ((x($a->page,'content')) ? $a->page['content'] : '');
353         $_SESSION['sysmsg']="";
354         unset($_SESSION['sysmsg']);
355 }
356 if(x($_SESSION,'sysmsg_info')) {
357         $a->page['content'] = "<div id=\"sysmsg_info\" class=\"info-message\">{$_SESSION['sysmsg_info']}</div>\r\n"
358                 . ((x($a->page,'content')) ? $a->page['content'] : '');
359         $_SESSION['sysmsg_info']="";
360         unset($_SESSION['sysmsg_info']);
361 }*/
362
363
364
365 call_hooks('page_end', $a->page['content']);
366
367
368 /**
369  *
370  * Add a place for the pause/resume Ajax indicator
371  *
372  */
373
374 $a->page['content'] .=  '<div id="pause"></div>';
375
376
377 /**
378  *
379  * Add the navigation (menu) template
380  *
381  */
382
383 if($a->module != 'install' && $a->module != 'maintenance') {
384         nav($a);
385 }
386
387 /**
388  * Add a "toggle mobile" link if we're using a mobile device
389  */
390
391 if($a->is_mobile || $a->is_tablet) {
392         if(isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
393                 $link = $a->get_baseurl() . '/toggle_mobile?address=' . curPageURL();
394         }
395         else {
396                 $link = $a->get_baseurl() . '/toggle_mobile?off=1&address=' . curPageURL();
397         }
398         $a->page['footer'] = replace_macros(get_markup_template("toggle_mobile_footer.tpl"), array(
399                                 '$toggle_link' => $link,
400                                 '$toggle_text' => t('toggle mobile')
401                          ));
402 }
403
404 /**
405  * Build the page - now that we have all the components
406  */
407
408 if(!$a->theme['stylesheet'])
409         $stylesheet = current_theme_url();
410 else
411         $stylesheet = $a->theme['stylesheet'];
412 $a->page['htmlhead'] = replace_macros($a->page['htmlhead'], array('$stylesheet' => $stylesheet));
413
414 $page    = $a->page;
415 $profile = $a->profile;
416
417 header("Content-type: text/html; charset=utf-8");
418
419 $template = 'view/theme/' . current_theme() . '/' 
420         . ((x($a->page,'template')) ? $a->page['template'] : 'default' ) . '.php';
421
422 if(file_exists($template))
423         require_once($template);
424 else
425         require_once(str_replace('theme/' . current_theme() . '/', '', $template));
426
427 session_write_close();
428 exit;