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.

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