]> git.mxchange.org Git - friendica.git/blob - src/App/Module.php
9a24c55544199635cde4a956577070491ce6519f
[friendica.git] / src / App / Module.php
1 <?php
2
3 namespace Friendica\App;
4
5 use Friendica\App;
6 use Friendica\BaseObject;
7 use Friendica\Core;
8 use Friendica\LegacyModule;
9 use Friendica\Module\Home;
10 use Friendica\Module\PageNotFound;
11 use Psr\Log\LoggerInterface;
12
13 /**
14  * Holds the common context of the current, loaded module
15  */
16 class Module
17 {
18         const DEFAULT       = 'home';
19         const DEFAULT_CLASS = Home::class;
20         /**
21          * A list of modules, which are backend methods
22          *
23          * @var array
24          */
25         const BACKEND_MODULES = [
26                 '_well_known',
27                 'api',
28                 'dfrn_notify',
29                 'feed',
30                 'fetch',
31                 'followers',
32                 'following',
33                 'hcard',
34                 'hostxrd',
35                 'inbox',
36                 'manifest',
37                 'nodeinfo',
38                 'noscrape',
39                 'objects',
40                 'outbox',
41                 'poco',
42                 'post',
43                 'proxy',
44                 'pubsub',
45                 'pubsubhubbub',
46                 'receive',
47                 'rsd_xml',
48                 'salmon',
49                 'statistics_json',
50                 'xrd',
51         ];
52
53         /**
54          * @var string The module name
55          */
56         private $module;
57
58         /**
59          * @var BaseObject The module class
60          */
61         private $module_class;
62
63         /**
64          * @var bool true, if the module is a backend module
65          */
66         private $isBackend;
67
68         /**
69          * @var bool true, if the loaded addon is private, so we have to print out not allowed
70          */
71         private $printNotAllowedAddon;
72
73         /**
74          * @return string
75          */
76         public function getName()
77         {
78                 return $this->module;
79         }
80
81         /**
82          * @return string The base class name
83          */
84         public function getClassName()
85         {
86                 return $this->module_class;
87         }
88
89         /**
90          * @return bool True, if the current module is a backend module
91          * @see Module::BACKEND_MODULES for a list
92          */
93         public function isBackend()
94         {
95                 return $this->isBackend;
96         }
97
98         public function __construct(string $module = self::DEFAULT, string $moduleClass = self::DEFAULT_CLASS, bool $isBackend = false, bool $printNotAllowedAddon = false)
99         {
100                 $this->module               = $module;
101                 $this->module_class         = $moduleClass;
102                 $this->isBackend            = $isBackend;
103                 $this->printNotAllowedAddon = $printNotAllowedAddon;
104         }
105
106         /**
107          * Determines the current module based on the App arguments and the server variable
108          *
109          * @param Arguments $args   The Friendica arguments
110          *
111          * @return Module The module with the determined module
112          */
113         public function determineModule(Arguments $args)
114         {
115                 if ($args->getArgc() > 0) {
116                         $module = str_replace('.', '_', $args->get(0));
117                         $module = str_replace('-', '_', $module);
118                 } else {
119                         $module = self::DEFAULT;
120                 }
121
122                 // Compatibility with the Firefox App
123                 if (($module == "users") && ($args->getCommand() == "users/sign_in")) {
124                         $module = "login";
125                 }
126
127                 $isBackend = in_array($module, Module::BACKEND_MODULES);;
128
129                 return new Module($module, $this->module_class, $isBackend, $this->printNotAllowedAddon);
130         }
131
132         /**
133          * Determine the class of the current module
134          *
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
138          *
139          * @return Module The determined module of this call
140          *
141          * @throws \Exception
142          */
143         public function determineClass(Arguments $args, Router $router, Core\Config\Configuration $config)
144         {
145                 $printNotAllowedAddon = false;
146
147                 /**
148                  * ROUTING
149                  *
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.
152                  **/
153                 $module_class = $router->getModuleClass($args->getCommand());
154
155                 // Then we try addon-provided modules that we wrap in the LegacyModule class
156                 if (!$module_class && Core\Addon::isEnabled($this->module) && file_exists("addon/{$this->module}/{$this->module}.php")) {
157                         //Check if module is an app and if public access to apps is allowed or not
158                         $privateapps = $config->get('config', 'private_addons', false);
159                         if ((!local_user()) && Core\Hook::isAddonApp($this->module) && $privateapps) {
160                                 $printNotAllowedAddon = true;
161                         } else {
162                                 include_once "addon/{$this->module}/{$this->module}.php";
163                                 if (function_exists($this->module . '_module')) {
164                                         LegacyModule::setModuleFile("addon/{$this->module}/{$this->module}.php");
165                                         $module_class = LegacyModule::class;
166                                 }
167                         }
168                 }
169
170                 /* Finally, we look for a 'standard' program module in the 'mod' directory
171                  * We emulate a Module class through the LegacyModule class
172                  */
173                 if (!$module_class && file_exists("mod/{$this->module}.php")) {
174                         LegacyModule::setModuleFile("mod/{$this->module}.php");
175                         $module_class = LegacyModule::class;
176                 }
177
178                 $module_class = !isset($module_class) ? PageNotFound::class : $module_class;
179
180                 return new Module($this->module, $module_class, $this->isBackend, $printNotAllowedAddon);
181         }
182
183         /**
184          * Run the determined module class and calls all hooks applied to
185          *
186          * @param Core\L10n\L10n  $l10n         The L10n instance
187          * @param App             $app          The whole Friendica app (for method arguments)
188          * @param LoggerInterface $logger       The Friendica logger
189          * @param array           $server       The $_SERVER variable
190          * @param array           $post         The $_POST variables
191          *
192          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
193          */
194         public function run(Core\L10n\L10n $l10n, App $app, LoggerInterface $logger, array $server, array $post)
195         {
196                 if ($this->printNotAllowedAddon) {
197                         info($l10n->t("You must be logged in to use addons. "));
198                 }
199
200                 /* The URL provided does not resolve to a valid module.
201                  *
202                  * On Dreamhost sites, quite often things go wrong for no apparent reason and they send us to '/internal_error.html'.
203                  * We don't like doing this, but as it occasionally accounts for 10-20% or more of all site traffic -
204                  * 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
205                  * this will often succeed and eventually do the right thing.
206                  *
207                  * Otherwise we are going to emit a 404 not found.
208                  */
209                 if ($this->module_class === PageNotFound::class) {
210                         $queryString = $server['QUERY_STRING'];
211                         // Stupid browser tried to pre-fetch our Javascript img template. Don't log the event or return anything - just quietly exit.
212                         if (!empty($queryString) && preg_match('/{[0-9]}/', $queryString) !== 0) {
213                                 exit();
214                         }
215
216                         if (!empty($queryString) && ($queryString === 'q=internal_error.html') && isset($dreamhost_error_hack)) {
217                                 $logger->info('index.php: dreamhost_error_hack invoked.', ['Original URI' => $server['REQUEST_URI']]);
218                                 $app->internalRedirect($server['REQUEST_URI']);
219                         }
220
221                         $logger->debug('index.php: page not found.', ['request_uri' => $server['REQUEST_URI'], 'address' => $server['REMOTE_ADDR'], 'query' => $server['QUERY_STRING']]);
222                 }
223
224                 $placeholder = '';
225
226                 Core\Hook::callAll($this->module . '_mod_init', $placeholder);
227
228                 call_user_func([$this->module_class, 'init']);
229
230                 // "rawContent" is especially meant for technical endpoints.
231                 // This endpoint doesn't need any theme initialization or other comparable stuff.
232                 call_user_func([$this->module_class, 'rawContent']);
233
234                 if ($server['REQUEST_METHOD'] === 'POST') {
235                         Core\Hook::callAll($this->module . '_mod_post', $post);
236                         call_user_func([$this->module_class, 'post']);
237                 }
238
239                 Core\Hook::callAll($this->module . '_mod_afterpost', $placeholder);
240                 call_user_func([$this->module_class, 'afterpost']);
241         }
242 }