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.

index.php 16KB

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