streams/Zotlabs/Web/WebServer.php

210 lines
5.2 KiB
PHP
Raw Normal View History

2019-05-24 05:51:39 +00:00
<?php
2016-05-24 23:36:55 +00:00
namespace Zotlabs\Web;
2019-05-24 05:51:39 +00:00
use App;
2016-05-24 23:36:55 +00:00
class WebServer {
public function run() {
/*
* Bootstrap the application, load configuration, load modules, load theme, etc.
*/
require_once('boot.php');
sys_boot();
2016-05-24 23:36:55 +00:00
2019-05-24 05:51:39 +00:00
App::$language = get_best_language();
load_translation_table(App::$language,App::$install);
2016-05-24 23:36:55 +00:00
/**
*
* Important stuff we always need to do.
*
* The order of these may be important so use caution if you think they're all
* intertwingled with no logical order and decide to sort it out. Some of the
* dependencies have changed, but at least at one time in the recent past - the
* order was critical to everything working properly
*
*/
2019-05-24 05:51:39 +00:00
if (App::$session) {
App::$session->start();
2016-05-24 23:36:55 +00:00
}
else {
session_start();
register_shutdown_function('session_write_close');
}
/**
* Language was set earlier, but we can over-ride it in the session.
* We have to do it here because the session was just now opened.
*/
2019-05-24 05:51:39 +00:00
if (array_key_exists('system_language',$_REQUEST)) {
if (strlen($_REQUEST['system_language'])) {
$_SESSION['language'] = $_REQUEST['system_language'];
2019-05-24 05:51:39 +00:00
}
else {
2016-05-24 23:36:55 +00:00
unset($_SESSION['language']);
2019-05-24 05:51:39 +00:00
}
2016-05-24 23:36:55 +00:00
}
2019-05-24 05:51:39 +00:00
if ((x($_SESSION, 'language')) && ($_SESSION['language'] !== $lang)) {
App::$language = $_SESSION['language'];
load_translation_table(App::$language);
2016-05-24 23:36:55 +00:00
}
2019-05-24 05:51:39 +00:00
if ((x($_GET,'zid')) && (! App::$install)) {
App::$query_string = strip_zids(App::$query_string);
if (! local_channel()) {
if ($_SESSION['my_address'] !== $_GET['zid']) {
$_SESSION['my_address'] = $_GET['zid'];
$_SESSION['authenticated'] = 0;
}
2016-07-15 02:43:47 +00:00
zid_init();
}
}
2019-05-24 05:51:39 +00:00
if ((x($_GET,'zat')) && (! App::$install)) {
App::$query_string = strip_zats(App::$query_string);
if (! local_channel()) {
2016-07-15 02:43:47 +00:00
zat_init();
2016-05-24 23:36:55 +00:00
}
}
2019-05-24 05:51:39 +00:00
if ((x($_REQUEST,'owt')) && (! App::$install)) {
2017-09-08 00:56:02 +00:00
$token = $_REQUEST['owt'];
2019-05-24 05:51:39 +00:00
App::$query_string = strip_query_param(App::$query_string,'owt');
2017-09-08 00:56:02 +00:00
owt_init($token);
}
2019-05-24 05:51:39 +00:00
if ((x($_SESSION, 'authenticated')) || (x($_POST, 'auth-params')) || (App::$module === 'login')) {
2016-05-24 23:36:55 +00:00
require('include/auth.php');
2019-05-24 05:51:39 +00:00
}
2016-05-24 23:36:55 +00:00
2019-05-24 05:51:39 +00:00
if (! x($_SESSION, 'sysmsg')) {
$_SESSION['sysmsg'] = [];
}
2016-05-24 23:36:55 +00:00
2019-05-24 05:51:39 +00:00
if (! x($_SESSION, 'sysmsg_info')) {
$_SESSION['sysmsg_info'] = [];
}
2016-05-24 23:36:55 +00:00
2019-05-24 05:51:39 +00:00
if (App::$install) {
2016-05-24 23:36:55 +00:00
/* Allow an exception for the view module so that pcss will be interpreted during installation */
2019-05-24 05:51:39 +00:00
if (App::$module !== 'view')
App::$module = 'setup';
2016-05-24 23:36:55 +00:00
}
2017-03-24 04:49:20 +00:00
else {
/*
* check_config() is responsible for running update scripts. These automatically
* update the DB schema whenever we push a new one out. It also checks to see if
* any plugins have been added or removed and reacts accordingly.
*/
check_config();
}
2016-05-24 23:36:55 +00:00
$this->create_channel_links();
2016-05-24 23:36:55 +00:00
$Router = new Router();
$this->initialise_content();
$Router->Dispatch();
$this->set_homebase();
// now that we've been through the module content, see if the page reported
// a permission problem and if so, a 403 response would seem to be in order.
2019-05-24 05:51:39 +00:00
if (is_array($_SESSION['sysmsg']) && stristr(implode("", $_SESSION['sysmsg']), t('Permission denied'))) {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 ' . t('Permission denied.'));
}
2019-05-24 05:51:39 +00:00
call_hooks('page_end', App::$page['content']);
construct_page();
killme();
}
private function initialise_content() {
/* initialise content region */
2019-05-24 05:51:39 +00:00
if (! x(App::$page, 'content')) {
App::$page['content'] = EMPTY_STR;
}
2019-05-24 05:51:39 +00:00
call_hooks('page_content_top', App::$page['content']);
}
private function create_channel_links() {
2016-05-24 23:36:55 +00:00
/* Initialise the Link: response header if this is a channel page.
* This cannot be done inside the channel module because some protocol
* addons over-ride the module functions and these links are common
* to all protocol drivers; thus doing it here avoids duplication.
*/
2019-05-24 05:51:39 +00:00
if (( App::$module === 'channel' ) && argc() > 1) {
App::$channel_links = [
[
'rel' => 'jrd',
'type' => 'application/jrd+json',
2019-05-24 05:51:39 +00:00
'url' => z_root() . '/.well-known/webfinger?f=&resource=acct%3A' . argv(1) . '%40' . App::get_hostname()
],
[
'rel' => 'zot',
'type' => 'application/x-zot+json',
'url' => z_root() . '/channel/' . argv(1)
],
];
2018-10-04 02:10:52 +00:00
if(! defined('NOMADIC')) {
2019-05-24 05:51:39 +00:00
App::$channel_links[] =
2018-10-04 02:10:52 +00:00
[
'rel' => 'self',
'type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'href' => z_root() . '/channel/' . argv(1)
];
2019-05-24 05:51:39 +00:00
App::$channel_links[] =
2018-10-04 02:10:52 +00:00
[
'rel' => 'self',
'type' => 'application/activity+json',
'href' => z_root() . '/channel/' . argv(1)
];
}
2019-05-24 05:51:39 +00:00
$x = [ 'channel_address' => argv(1), 'channel_links' => App::$channel_links ];
call_hooks('channel_links', $x );
2019-05-24 05:51:39 +00:00
App::$channel_links = $x['channel_links'];
header('Link: ' . App::get_channel_links());
}
}
private function set_homebase() {
2016-05-24 23:36:55 +00:00
// If you're just visiting, let javascript take you home
2019-05-24 05:51:39 +00:00
if (x($_SESSION, 'visitor_home')) {
2016-05-24 23:36:55 +00:00
$homebase = $_SESSION['visitor_home'];
}
2019-05-24 05:51:39 +00:00
elseif (local_channel()) {
$homebase = z_root() . '/channel/' . App::$channel['channel_address'];
2016-05-24 23:36:55 +00:00
}
2019-05-24 05:51:39 +00:00
if (isset($homebase)) {
App::$page['content'] .= '<script>var homebase = "' . $homebase . '";</script>';
2016-05-24 23:36:55 +00:00
}
}
}