1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/hubzilla_ynh.git synced 2024-09-03 19:26:21 +02:00
hubzilla_ynh/sources/Zotlabs/Web/Router.php

203 lines
6.7 KiB
PHP
Raw Normal View History

2016-03-20 08:06:33 +01:00
<?php
namespace Zotlabs\Web;
class Router {
function __construct(&$a) {
/**
*
2016-04-17 16:29:18 +02:00
* We have already parsed the server path into App::$argc and App::$argv
2016-03-20 08:06:33 +01:00
*
2016-04-17 16:29:18 +02:00
* App::$argv[0] is our module name. We will load the file mod/{App::$argv[0]}.php
2016-03-20 08:06:33 +01:00
* and use it for handling our URL request.
* The module file contains a few functions that we call in various circumstances
* and in the following order:
*
* "module"_init
* "module"_post (only called if there are $_POST variables)
* "module"_content - the string return of this function contains our page body
*
* Modules which emit other serialisations besides HTML (XML,JSON, etc.) should do
* so within the module init and/or post functions and then invoke killme() to terminate
* further processing.
*/
2016-04-17 16:29:18 +02:00
$module = \App::$module;
if(strlen($module)) {
2016-03-20 08:06:33 +01:00
/**
*
* We will always have a module name.
* First see if we have a plugin which is masquerading as a module.
*
*/
2016-04-17 16:29:18 +02:00
if(is_array(\App::$plugins) && in_array($module,\App::$plugins) && file_exists("addon/{$module}/{$module}.php")) {
include_once("addon/{$module}/{$module}.php");
if(function_exists($module . '_module'))
\App::$module_loaded = true;
2016-03-20 08:06:33 +01:00
}
2016-04-17 16:29:18 +02:00
if((strpos($module,'admin') === 0) && (! is_site_admin())) {
\App::$module_loaded = false;
2016-03-20 08:06:33 +01:00
notice( t('Permission denied.') . EOL);
goaway(z_root());
}
/**
* If the site has a custom module to over-ride the standard module, use it.
* Otherwise, look for the standard program module in the 'mod' directory
*/
2016-04-17 16:29:18 +02:00
if(! (\App::$module_loaded)) {
if(file_exists("mod/site/{$module}.php")) {
include_once("mod/site/{$module}.php");
\App::$module_loaded = true;
2016-03-20 08:06:33 +01:00
}
2016-04-17 16:29:18 +02:00
elseif(file_exists("mod/{$module}.php")) {
include_once("mod/{$module}.php");
\App::$module_loaded = true;
2016-03-20 08:06:33 +01:00
}
2016-04-17 16:29:18 +02:00
else logger("mod/{$module}.php not found.");
2016-03-20 08:06:33 +01:00
}
2016-04-17 16:29:18 +02:00
2016-03-20 08:06:33 +01:00
/**
* This provides a place for plugins to register module handlers which don't otherwise exist on the system.
* If the plugin sets 'installed' to true we won't throw a 404 error for the specified module even if
* there is no specific module file or matching plugin name.
* The plugin should catch at least one of the module hooks for this URL.
*/
2016-04-17 16:29:18 +02:00
$x = array('module' => $module, 'installed' => false);
2016-03-20 08:06:33 +01:00
call_hooks('module_loaded', $x);
if($x['installed'])
2016-04-17 16:29:18 +02:00
\App::$module_loaded = true;
2016-03-20 08:06:33 +01:00
/**
* The URL provided does not resolve to a valid module.
*
* On Dreamhost sites, quite often things go wrong for no apparent reason and they send us to '/internal_error.html'.
* We don't like doing this, but as it occasionally accounts for 10-20% or more of all site traffic -
* 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
* this will often succeed and eventually do the right thing.
*
* Otherwise we are going to emit a 404 not found.
*/
2016-04-17 16:29:18 +02:00
if(! (\App::$module_loaded)) {
2016-03-20 08:06:33 +01:00
// Stupid browser tried to pre-fetch our Javascript img template. Don't log the event or return anything - just quietly exit.
if((x($_SERVER, 'QUERY_STRING')) && preg_match('/{[0-9]}/', $_SERVER['QUERY_STRING']) !== 0) {
killme();
}
2016-04-17 16:29:18 +02:00
if((x($_SERVER, 'QUERY_STRING')) && ($_SERVER['QUERY_STRING'] === 'q=internal_error.html') && \App::$config['system']['dreamhost_error_hack']) {
2016-03-20 08:06:33 +01:00
logger('index.php: dreamhost_error_hack invoked. Original URI =' . $_SERVER['REQUEST_URI']);
2016-04-17 16:29:18 +02:00
goaway(z_root() . $_SERVER['REQUEST_URI']);
2016-03-20 08:06:33 +01:00
}
logger('index.php: page not found: ' . $_SERVER['REQUEST_URI'] . ' ADDRESS: ' . $_SERVER['REMOTE_ADDR'] . ' QUERY: ' . $_SERVER['QUERY_STRING'], LOGGER_DEBUG);
header($_SERVER['SERVER_PROTOCOL'] . ' 404 ' . t('Not Found'));
$tpl = get_markup_template('404.tpl');
2016-04-17 16:29:18 +02:00
\App::$page['content'] = replace_macros($tpl, array(
2016-03-20 08:06:33 +01:00
'$message' => t('Page not found.')
));
// pretend this is a module so it will initialise the theme
2016-04-17 16:29:18 +02:00
\App::$module = '404';
\App::$module_loaded = true;
2016-03-20 08:06:33 +01:00
}
}
}
function Dispatch(&$a) {
/**
* Call module functions
*/
2016-04-17 16:29:18 +02:00
if(\App::$module_loaded) {
\App::$page['page_title'] = \App::$module;
2016-03-20 08:06:33 +01:00
$placeholder = '';
/**
* No theme has been specified when calling the module_init functions
* For this reason, please restrict the use of templates to those which
* do not provide any presentation details - as themes will not be able
* to over-ride them.
*/
2016-04-17 16:29:18 +02:00
if(function_exists(\App::$module . '_init')) {
2016-03-20 08:06:33 +01:00
$arr = array('init' => true, 'replace' => false);
2016-04-17 16:29:18 +02:00
call_hooks(\App::$module . '_mod_init', $arr);
2016-03-20 08:06:33 +01:00
if(! $arr['replace']) {
2016-04-17 16:29:18 +02:00
$func = \App::$module . '_init';
2016-03-20 08:06:33 +01:00
$func($a);
}
}
/**
* Do all theme initialiasion here before calling any additional module functions.
* The module_init function may have changed the theme.
* Additionally any page with a Comanche template may alter the theme.
* So we'll check for those now.
*/
/**
* In case a page has overloaded a module, see if we already have a layout defined
* otherwise, if a PDL file exists for this module, use it
* The member may have also created a customised PDL that's stored in the config
*/
load_pdl($a);
/**
* load current theme info
*/
$theme_info_file = 'view/theme/' . current_theme() . '/php/theme.php';
if (file_exists($theme_info_file)){
require_once($theme_info_file);
}
if(function_exists(str_replace('-', '_', current_theme()) . '_init')) {
$func = str_replace('-', '_', current_theme()) . '_init';
$func($a);
}
2016-04-17 16:29:18 +02:00
elseif (x(\App::$theme_info, 'extends') && file_exists('view/theme/' . \App::$theme_info['extends'] . '/php/theme.php')) {
require_once('view/theme/' . \App::$theme_info['extends'] . '/php/theme.php');
if(function_exists(str_replace('-', '_', \App::$theme_info['extends']) . '_init')) {
$func = str_replace('-', '_', \App::$theme_info['extends']) . '_init';
2016-03-20 08:06:33 +01:00
$func($a);
}
}
2016-04-17 16:29:18 +02:00
if(($_SERVER['REQUEST_METHOD'] === 'POST') && (! \App::$error)
&& (function_exists(\App::$module . '_post'))
2016-03-20 08:06:33 +01:00
&& (! x($_POST, 'auth-params'))) {
2016-04-17 16:29:18 +02:00
call_hooks(\App::$module . '_mod_post', $_POST);
$func = \App::$module . '_post';
2016-03-20 08:06:33 +01:00
$func($a);
}
2016-04-17 16:29:18 +02:00
if((! \App::$error) && (function_exists(\App::$module . '_content'))) {
$arr = array('content' => \App::$page['content'], 'replace' => false);
call_hooks(\App::$module . '_mod_content', $arr);
\App::$page['content'] = $arr['content'];
2016-03-20 08:06:33 +01:00
if(! $arr['replace']) {
2016-04-17 16:29:18 +02:00
$func = \App::$module . '_content';
2016-03-20 08:06:33 +01:00
$arr = array('content' => $func($a));
}
2016-04-17 16:29:18 +02:00
call_hooks(\App::$module . '_mod_aftercontent', $arr);
\App::$page['content'] .= $arr['content'];
2016-03-20 08:06:33 +01:00
}
}
}
}