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.

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