Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

519 lines
14 KiB

11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. /**
  3. *
  4. * Friendica
  5. *
  6. */
  7. /**
  8. *
  9. * bootstrap the application
  10. *
  11. */
  12. require_once('boot.php');
  13. require_once('object/BaseObject.php');
  14. $a = new App;
  15. BaseObject::set_app($a);
  16. // We assume that the index.php is called by a frontend process
  17. // The value is set to "true" by default in boot.php
  18. $a->backend = false;
  19. /**
  20. *
  21. * Load the configuration file which contains our DB credentials.
  22. * Ignore errors. If the file doesn't exist or is empty, we are running in installation mode.
  23. *
  24. */
  25. $install = ((file_exists('.htconfig.php') && filesize('.htconfig.php')) ? false : true);
  26. @include(".htconfig.php");
  27. /**
  28. *
  29. * Try to open the database;
  30. *
  31. */
  32. require_once("include/dba.php");
  33. if(!$install) {
  34. $db = new dba($db_host, $db_user, $db_pass, $db_data, $install);
  35. unset($db_host, $db_user, $db_pass, $db_data);
  36. /**
  37. * Load configs from db. Overwrite configs from .htconfig.php
  38. */
  39. load_config('config');
  40. load_config('system');
  41. if ($a->max_processes_reached() OR $a->maxload_reached()) {
  42. header($_SERVER["SERVER_PROTOCOL"].' 503 Service Temporarily Unavailable');
  43. header('Retry-After: 300');
  44. die("System is currently unavailable. Please try again later");
  45. }
  46. if (get_config('system','force_ssl') AND ($a->get_scheme() == "http") AND
  47. (intval(get_config('system','ssl_policy')) == SSL_POLICY_FULL) AND
  48. (substr($a->get_baseurl(), 0, 8) == "https://")) {
  49. header("HTTP/1.1 302 Moved Temporarily");
  50. header("Location: ".$a->get_baseurl()."/".$a->query_string);
  51. exit();
  52. }
  53. require_once("include/session.php");
  54. load_hooks();
  55. call_hooks('init_1');
  56. $maintenance = get_config('system', 'maintenance');
  57. }
  58. $lang = get_browser_language();
  59. load_translation_table($lang);
  60. /**
  61. *
  62. * Important stuff we always need to do.
  63. *
  64. * The order of these may be important so use caution if you think they're all
  65. * intertwingled with no logical order and decide to sort it out. Some of the
  66. * dependencies have changed, but at least at one time in the recent past - the
  67. * order was critical to everything working properly
  68. *
  69. */
  70. // Exclude the backend processes from the session management
  71. if (!$a->is_backend()) {
  72. $stamp1 = microtime(true);
  73. session_start();
  74. $a->save_timestamp($stamp1, "parser");
  75. }
  76. /**
  77. * Language was set earlier, but we can over-ride it in the session.
  78. * We have to do it here because the session was just now opened.
  79. */
  80. if (x($_SESSION,'authenticated') && !x($_SESSION,'language')) {
  81. // we didn't loaded user data yet, but we need user language
  82. $r = q("SELECT language FROM user WHERE uid=%d", intval($_SESSION['uid']));
  83. $_SESSION['language'] = $lang;
  84. if (count($r)>0) $_SESSION['language'] = $r[0]['language'];
  85. }
  86. if((x($_SESSION,'language')) && ($_SESSION['language'] !== $lang)) {
  87. $lang = $_SESSION['language'];
  88. load_translation_table($lang);
  89. }
  90. if((x($_GET,'zrl')) && (!$install && !$maintenance)) {
  91. // Only continue when the given profile link seems valid
  92. // Valid profile links contain a path with "/profile/" and no query parameters
  93. if ((parse_url($_GET['zrl'], PHP_URL_QUERY) == "") AND
  94. strstr(parse_url($_GET['zrl'], PHP_URL_PATH), "/profile/")) {
  95. $_SESSION['my_url'] = $_GET['zrl'];
  96. $a->query_string = preg_replace('/[\?&]zrl=(.*?)([\?&]|$)/is','',$a->query_string);
  97. zrl_init($a);
  98. } else {
  99. // Someone came with an invalid parameter, maybe as a DDoS attempt
  100. // We simply stop processing here
  101. logger("Invalid ZRL parameter ".$_GET['zrl'], LOGGER_DEBUG);
  102. header('HTTP/1.1 403 Forbidden');
  103. echo "<h1>403 Forbidden</h1>";
  104. killme();
  105. }
  106. }
  107. /**
  108. *
  109. * For Mozilla auth manager - still needs sorting, and this might conflict with LRDD header.
  110. * Apache/PHP lumps the Link: headers into one - and other services might not be able to parse it
  111. * this way. There's a PHP flag to link the headers because by default this will over-write any other
  112. * link header.
  113. *
  114. * What we really need to do is output the raw headers ourselves so we can keep them separate.
  115. *
  116. */
  117. // header('Link: <' . $a->get_baseurl() . '/amcd>; rel="acct-mgmt";');
  118. if(x($_COOKIE["Friendica"]) || (x($_SESSION,'authenticated')) || (x($_POST,'auth-params')) || ($a->module === 'login'))
  119. require("include/auth.php");
  120. if(! x($_SESSION,'authenticated'))
  121. header('X-Account-Management-Status: none');
  122. /* set up page['htmlhead'] and page['end'] for the modules to use */
  123. $a->page['htmlhead'] = '';
  124. $a->page['end'] = '';
  125. if(! x($_SESSION,'sysmsg'))
  126. $_SESSION['sysmsg'] = array();
  127. if(! x($_SESSION,'sysmsg_info'))
  128. $_SESSION['sysmsg_info'] = array();
  129. /*
  130. * check_config() is responsible for running update scripts. These automatically
  131. * update the DB schema whenever we push a new one out. It also checks to see if
  132. * any plugins have been added or removed and reacts accordingly.
  133. */
  134. // in install mode, any url loads install module
  135. // but we need "view" module for stylesheet
  136. if($install && $a->module!="view")
  137. $a->module = 'install';
  138. elseif($maintenance && $a->module!="view")
  139. $a->module = 'maintenance';
  140. else {
  141. check_url($a);
  142. check_db();
  143. check_plugins($a);
  144. }
  145. nav_set_selected('nothing');
  146. //Don't populate apps_menu if apps are private
  147. $privateapps = get_config('config','private_addons');
  148. if((local_user()) || (! $privateapps === "1"))
  149. {
  150. $arr = array('app_menu' => $a->apps);
  151. call_hooks('app_menu', $arr);
  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. if(strlen($a->module)) {
  173. /**
  174. *
  175. * We will always have a module name.
  176. * First see if we have a plugin which is masquerading as a module.
  177. *
  178. */
  179. // Compatibility with the Android Diaspora client
  180. if ($a->module == "stream")
  181. $a->module = "network";
  182. // Compatibility with the Firefox App
  183. if (($a->module == "users") AND ($a->cmd == "users/sign_in"))
  184. $a->module = "login";
  185. $privateapps = get_config('config','private_addons');
  186. if(is_array($a->plugins) && in_array($a->module,$a->plugins) && file_exists("addon/{$a->module}/{$a->module}.php")) {
  187. //Check if module is an app and if public access to apps is allowed or not
  188. if((!local_user()) && plugin_is_app($a->module) && $privateapps === "1") {
  189. info( t("You must be logged in to use addons. "));
  190. }
  191. else {
  192. include_once("addon/{$a->module}/{$a->module}.php");
  193. if(function_exists($a->module . '_module'))
  194. $a->module_loaded = true;
  195. }
  196. }
  197. /**
  198. * If not, next look for a 'standard' program module in the 'mod' directory
  199. */
  200. if((! $a->module_loaded) && (file_exists("mod/{$a->module}.php"))) {
  201. include_once("mod/{$a->module}.php");
  202. $a->module_loaded = true;
  203. }
  204. /**
  205. *
  206. * The URL provided does not resolve to a valid module.
  207. *
  208. * On Dreamhost sites, quite often things go wrong for no apparent reason and they send us to '/internal_error.html'.
  209. * We don't like doing this, but as it occasionally accounts for 10-20% or more of all site traffic -
  210. * 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
  211. * this will often succeed and eventually do the right thing.
  212. *
  213. * Otherwise we are going to emit a 404 not found.
  214. *
  215. */
  216. if(! $a->module_loaded) {
  217. // Stupid browser tried to pre-fetch our Javascript img template. Don't log the event or return anything - just quietly exit.
  218. if((x($_SERVER,'QUERY_STRING')) && preg_match('/{[0-9]}/',$_SERVER['QUERY_STRING']) !== 0) {
  219. killme();
  220. }
  221. if((x($_SERVER,'QUERY_STRING')) && ($_SERVER['QUERY_STRING'] === 'q=internal_error.html') && isset($dreamhost_error_hack)) {
  222. logger('index.php: dreamhost_error_hack invoked. Original URI =' . $_SERVER['REQUEST_URI']);
  223. goaway($a->get_baseurl() . $_SERVER['REQUEST_URI']);
  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. * load current theme info
  235. */
  236. $theme_info_file = "view/theme/".current_theme()."/theme.php";
  237. if (file_exists($theme_info_file)){
  238. require_once($theme_info_file);
  239. }
  240. /* initialise content region */
  241. if(! x($a->page,'content'))
  242. $a->page['content'] = '';
  243. if(!$install && !$maintenance)
  244. call_hooks('page_content_top',$a->page['content']);
  245. /**
  246. * Call module functions
  247. */
  248. if($a->module_loaded) {
  249. $a->page['page_title'] = $a->module;
  250. $placeholder = '';
  251. if(function_exists($a->module . '_init')) {
  252. call_hooks($a->module . '_mod_init', $placeholder);
  253. $func = $a->module . '_init';
  254. $func($a);
  255. }
  256. if(function_exists(str_replace('-','_',current_theme()) . '_init')) {
  257. $func = str_replace('-','_',current_theme()) . '_init';
  258. $func($a);
  259. }
  260. // elseif (x($a->theme_info,"extends") && file_exists("view/theme/".$a->theme_info["extends"]."/theme.php")) {
  261. // require_once("view/theme/".$a->theme_info["extends"]."/theme.php");
  262. // if(function_exists(str_replace('-','_',$a->theme_info["extends"]) . '_init')) {
  263. // $func = str_replace('-','_',$a->theme_info["extends"]) . '_init';
  264. // $func($a);
  265. // }
  266. // }
  267. if(($_SERVER['REQUEST_METHOD'] === 'POST') && (! $a->error)
  268. && (function_exists($a->module . '_post'))
  269. && (! x($_POST,'auth-params'))) {
  270. call_hooks($a->module . '_mod_post', $_POST);
  271. $func = $a->module . '_post';
  272. $func($a);
  273. }
  274. if((! $a->error) && (function_exists($a->module . '_afterpost'))) {
  275. call_hooks($a->module . '_mod_afterpost',$placeholder);
  276. $func = $a->module . '_afterpost';
  277. $func($a);
  278. }
  279. if((! $a->error) && (function_exists($a->module . '_content'))) {
  280. $arr = array('content' => $a->page['content']);
  281. call_hooks($a->module . '_mod_content', $arr);
  282. $a->page['content'] = $arr['content'];
  283. $func = $a->module . '_content';
  284. $arr = array('content' => $func($a));
  285. call_hooks($a->module . '_mod_aftercontent', $arr);
  286. $a->page['content'] .= $arr['content'];
  287. }
  288. if(function_exists(str_replace('-','_',current_theme()) . '_content_loaded')) {
  289. $func = str_replace('-','_',current_theme()) . '_content_loaded';
  290. $func($a);
  291. }
  292. }
  293. /*
  294. * Create the page head after setting the language
  295. * and getting any auth credentials
  296. *
  297. * Moved init_pagehead() and init_page_end() to after
  298. * all the module functions have executed so that all
  299. * theme choices made by the modules can take effect
  300. */
  301. $a->init_pagehead();
  302. /**
  303. * Build the page ending -- this is stuff that goes right before
  304. * the closing </body> tag
  305. */
  306. $a->init_page_end();
  307. // If you're just visiting, let javascript take you home
  308. if(x($_SESSION,'visitor_home'))
  309. $homebase = $_SESSION['visitor_home'];
  310. elseif(local_user())
  311. $homebase = 'profile/' . $a->user['nickname'];
  312. if(isset($homebase))
  313. $a->page['content'] .= '<script>var homebase="' . $homebase . '" ; </script>';
  314. // now that we've been through the module content, see if the page reported
  315. // a permission problem and if so, a 403 response would seem to be in order.
  316. if(stristr( implode("",$_SESSION['sysmsg']), t('Permission denied'))) {
  317. header($_SERVER["SERVER_PROTOCOL"] . ' 403 ' . t('Permission denied.'));
  318. }
  319. /**
  320. *
  321. * Report anything which needs to be communicated in the notification area (before the main body)
  322. *
  323. */
  324. /*if(x($_SESSION,'sysmsg')) {
  325. $a->page['content'] = "<div id=\"sysmsg\" class=\"error-message\">{$_SESSION['sysmsg']}</div>\r\n"
  326. . ((x($a->page,'content')) ? $a->page['content'] : '');
  327. $_SESSION['sysmsg']="";
  328. unset($_SESSION['sysmsg']);
  329. }
  330. if(x($_SESSION,'sysmsg_info')) {
  331. $a->page['content'] = "<div id=\"sysmsg_info\" class=\"info-message\">{$_SESSION['sysmsg_info']}</div>\r\n"
  332. . ((x($a->page,'content')) ? $a->page['content'] : '');
  333. $_SESSION['sysmsg_info']="";
  334. unset($_SESSION['sysmsg_info']);
  335. }*/
  336. call_hooks('page_end', $a->page['content']);
  337. /**
  338. *
  339. * Add the navigation (menu) template
  340. *
  341. */
  342. if($a->module != 'install' && $a->module != 'maintenance') {
  343. nav($a);
  344. }
  345. /**
  346. * Add a "toggle mobile" link if we're using a mobile device
  347. */
  348. if($a->is_mobile || $a->is_tablet) {
  349. if(isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
  350. $link = 'toggle_mobile?address=' . curPageURL();
  351. }
  352. else {
  353. $link = 'toggle_mobile?off=1&address=' . curPageURL();
  354. }
  355. $a->page['footer'] = replace_macros(get_markup_template("toggle_mobile_footer.tpl"), array(
  356. '$toggle_link' => $link,
  357. '$toggle_text' => t('toggle mobile')
  358. ));
  359. }
  360. /**
  361. * Build the page - now that we have all the components
  362. */
  363. if(!$a->theme['stylesheet'])
  364. $stylesheet = current_theme_url();
  365. else
  366. $stylesheet = $a->theme['stylesheet'];
  367. $a->page['htmlhead'] = str_replace('{{$stylesheet}}',$stylesheet,$a->page['htmlhead']);
  368. //$a->page['htmlhead'] = replace_macros($a->page['htmlhead'], array('$stylesheet' => $stylesheet));
  369. if (isset($_GET["mode"]) AND (($_GET["mode"] == "raw") OR ($_GET["mode"] == "minimal"))) {
  370. $doc = new DOMDocument();
  371. $target = new DOMDocument();
  372. $target->loadXML("<root></root>");
  373. $content = mb_convert_encoding($a->page["content"], 'HTML-ENTITIES', "UTF-8");
  374. @$doc->loadHTML($content);
  375. $xpath = new DomXPath($doc);
  376. $list = $xpath->query("//*[contains(@id,'tread-wrapper-')]"); /* */
  377. foreach ($list as $item) {
  378. $item = $target->importNode($item, true);
  379. // And then append it to the target
  380. $target->documentElement->appendChild($item);
  381. }
  382. }
  383. if (isset($_GET["mode"]) AND ($_GET["mode"] == "raw")) {
  384. header("Content-type: text/html; charset=utf-8");
  385. echo substr($target->saveHTML(), 6, -8);
  386. session_write_close();
  387. exit;
  388. }
  389. $page = $a->page;
  390. $profile = $a->profile;
  391. header("X-Friendica-Version: ".FRIENDICA_VERSION);
  392. header("Content-type: text/html; charset=utf-8");
  393. if (isset($_GET["mode"]) AND ($_GET["mode"] == "minimal")) {
  394. //$page['content'] = substr($target->saveHTML(), 6, -8)."\n\n".
  395. // '<div id="conversation-end"></div>'."\n\n";
  396. require "view/minimal.php";
  397. } else {
  398. $template = 'view/theme/' . current_theme() . '/'
  399. . ((x($a->page,'template')) ? $a->page['template'] : 'default' ) . '.php';
  400. if(file_exists($template))
  401. require_once($template);
  402. else
  403. require_once(str_replace('theme/' . current_theme() . '/', '', $template));
  404. }
  405. session_write_close();
  406. exit;