3 namespace Friendica\App;
6 use Friendica\BaseObject;
8 use Friendica\LegacyModule;
9 use Friendica\Module\Home;
10 use Friendica\Module\PageNotFound;
11 use Psr\Log\LoggerInterface;
14 * Holds the common context of the current, loaded module
18 const DEFAULT = 'home';
19 const DEFAULT_CLASS = Home::class;
21 * A list of modules, which are backend methods
25 const BACKEND_MODULES = [
54 * @var string The module name
59 * @var BaseObject The module class
61 private $module_class;
64 * @var bool true, if the module is a backend module
69 * @var bool true, if the loaded addon is private, so we have to print out not allowed
71 private $printNotAllowedAddon;
76 public function getName()
82 * @return string The base class name
84 public function getClassName()
86 return $this->module_class;
90 * @return bool True, if the current module is a backend module
91 * @see Module::BACKEND_MODULES for a list
93 public function isBackend()
95 return $this->isBackend;
98 public function __construct(string $module = self::DEFAULT, string $moduleClass = self::DEFAULT_CLASS, bool $isBackend = false, bool $printNotAllowedAddon = false)
100 $this->module = $module;
101 $this->module_class = $moduleClass;
102 $this->isBackend = $isBackend;
103 $this->printNotAllowedAddon = $printNotAllowedAddon;
107 * Determines the current module based on the App arguments and the server variable
109 * @param Arguments $args The Friendica arguments
111 * @return Module The module with the determined module
113 public function determineModule(Arguments $args)
115 if ($args->getArgc() > 0) {
116 $module = str_replace('.', '_', $args->get(0));
117 $module = str_replace('-', '_', $module);
119 $module = self::DEFAULT;
122 // Compatibility with the Firefox App
123 if (($module == "users") && ($args->getCommand() == "users/sign_in")) {
127 $isBackend = in_array($module, Module::BACKEND_MODULES);;
129 return new Module($module, $this->module_class, $isBackend, $this->printNotAllowedAddon);
133 * Determine the class of the current module
135 * @param Arguments $args The Friendica execution arguments
136 * @param Router $router The Friendica routing instance
137 * @param Core\Config\Configuration $config The Friendica Configuration
139 * @return Module The determined module of this call
141 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
143 public function determineClass(Arguments $args, Router $router, Core\Config\Configuration $config)
145 $printNotAllowedAddon = false;
150 * From the request URL, routing consists of obtaining the name of a BaseModule-extending class of which the
151 * post() and/or content() static methods can be respectively called to produce a data change or an output.
154 // First we try explicit routes defined in App\Router
155 $router->collectRoutes();
157 $data = $router->getRouteCollector();
158 Core\Hook::callAll('route_collection', $data);
160 $module_class = $router->getModuleClass($args->getCommand());
162 // Then we try addon-provided modules that we wrap in the LegacyModule class
163 if (!$module_class && Core\Addon::isEnabled($this->module) && file_exists("addon/{$this->module}/{$this->module}.php")) {
164 //Check if module is an app and if public access to apps is allowed or not
165 $privateapps = $config->get('config', 'private_addons', false);
166 if ((!local_user()) && Core\Hook::isAddonApp($this->module) && $privateapps) {
167 $printNotAllowedAddon = true;
169 include_once "addon/{$this->module}/{$this->module}.php";
170 if (function_exists($this->module . '_module')) {
171 LegacyModule::setModuleFile("addon/{$this->module}/{$this->module}.php");
172 $module_class = LegacyModule::class;
177 /* Finally, we look for a 'standard' program module in the 'mod' directory
178 * We emulate a Module class through the LegacyModule class
180 if (!$module_class && file_exists("mod/{$this->module}.php")) {
181 LegacyModule::setModuleFile("mod/{$this->module}.php");
182 $module_class = LegacyModule::class;
185 $module_class = !isset($module_class) ? PageNotFound::class : $module_class;
187 return new Module($this->module, $module_class, $this->isBackend, $printNotAllowedAddon);
191 * Run the determined module class and calls all hooks applied to
193 * @param Core\L10n\L10n $l10n The L10n instance
194 * @param App $app The whole Friendica app (for method arguments)
195 * @param LoggerInterface $logger The Friendica logger
196 * @param string $currentTheme The chosen theme
197 * @param array $server The $_SERVER variable
198 * @param array $post The $_POST variables
200 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
202 public function run(Core\L10n\L10n $l10n, App $app, LoggerInterface $logger, string $currentTheme, array $server, array $post)
204 if ($this->printNotAllowedAddon) {
205 info($l10n->t("You must be logged in to use addons. "));
208 /* The URL provided does not resolve to a valid module.
210 * On Dreamhost sites, quite often things go wrong for no apparent reason and they send us to '/internal_error.html'.
211 * We don't like doing this, but as it occasionally accounts for 10-20% or more of all site traffic -
212 * 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
213 * this will often succeed and eventually do the right thing.
215 * Otherwise we are going to emit a 404 not found.
217 if ($this->module_class === PageNotFound::class) {
218 $queryString = $server['QUERY_STRING'];
219 // Stupid browser tried to pre-fetch our Javascript img template. Don't log the event or return anything - just quietly exit.
220 if (!empty($queryString) && preg_match('/{[0-9]}/', $queryString) !== 0) {
224 if (!empty($queryString) && ($queryString === 'q=internal_error.html') && isset($dreamhost_error_hack)) {
225 $logger->info('index.php: dreamhost_error_hack invoked.', ['Original URI' => $server['REQUEST_URI']]);
226 $app->internalRedirect($server['REQUEST_URI']);
229 $logger->debug('index.php: page not found.', ['request_uri' => $server['REQUEST_URI'], 'address' => $server['REMOTE_ADDR'], 'query' => $server['QUERY_STRING']]);
234 Core\Hook::callAll($this->module . '_mod_init', $placeholder);
236 call_user_func([$this->module_class, 'init']);
238 // "rawContent" is especially meant for technical endpoints.
239 // This endpoint doesn't need any theme initialization or other comparable stuff.
240 call_user_func([$this->module_class, 'rawContent']);
242 // Load current theme info after module has been initialized as theme could have been set in module
243 $theme_info_file = 'view/theme/' . $currentTheme . '/theme.php';
244 if (file_exists($theme_info_file)) {
245 require_once $theme_info_file;
248 if (function_exists(str_replace('-', '_', $currentTheme) . '_init')) {
249 $func = str_replace('-', '_', $currentTheme) . '_init';
253 if ($server['REQUEST_METHOD'] === 'POST') {
254 Core\Hook::callAll($this->module . '_mod_post', $post);
255 call_user_func([$this->module_class, 'post']);
258 Core\Hook::callAll($this->module . '_mod_afterpost', $placeholder);
259 call_user_func([$this->module_class, 'afterpost']);