]> git.mxchange.org Git - friendica.git/blob - src/App/Module.php
Remove deprecated method to find duplicates (issue from 2013)
[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\HTTPException\MethodNotAllowed;
11 use Friendica\Module\HTTPException\PageNotFound;
12 use Friendica\Network\HTTPException\MethodNotAllowedException;
13 use Friendica\Network\HTTPException\NotFoundException;
14 use Psr\Log\LoggerInterface;
15
16 /**
17  * Holds the common context of the current, loaded module
18  */
19 class Module
20 {
21         const DEFAULT       = 'home';
22         const DEFAULT_CLASS = Home::class;
23         /**
24          * A list of modules, which are backend methods
25          *
26          * @var array
27          */
28         const BACKEND_MODULES = [
29                 '_well_known',
30                 'api',
31                 'dfrn_notify',
32                 'feed',
33                 'fetch',
34                 'followers',
35                 'following',
36                 'hcard',
37                 'hostxrd',
38                 'inbox',
39                 'manifest',
40                 'nodeinfo',
41                 'noscrape',
42                 'objects',
43                 'outbox',
44                 'poco',
45                 'post',
46                 'proxy',
47                 'pubsub',
48                 'pubsubhubbub',
49                 'receive',
50                 'rsd_xml',
51                 'salmon',
52                 'statistics_json',
53                 'xrd',
54         ];
55
56         /**
57          * @var string The module name
58          */
59         private $module;
60
61         /**
62          * @var BaseObject The module class
63          */
64         private $module_class;
65
66         /**
67          * @var bool true, if the module is a backend module
68          */
69         private $isBackend;
70
71         /**
72          * @var bool true, if the loaded addon is private, so we have to print out not allowed
73          */
74         private $printNotAllowedAddon;
75
76         /**
77          * @return string
78          */
79         public function getName()
80         {
81                 return $this->module;
82         }
83
84         /**
85          * @return string The base class name
86          */
87         public function getClassName()
88         {
89                 return $this->module_class;
90         }
91
92         /**
93          * @return bool True, if the current module is a backend module
94          * @see Module::BACKEND_MODULES for a list
95          */
96         public function isBackend()
97         {
98                 return $this->isBackend;
99         }
100
101         public function __construct(string $module = self::DEFAULT, string $moduleClass = self::DEFAULT_CLASS, bool $isBackend = false, bool $printNotAllowedAddon = false)
102         {
103                 $this->module               = $module;
104                 $this->module_class         = $moduleClass;
105                 $this->isBackend            = $isBackend;
106                 $this->printNotAllowedAddon = $printNotAllowedAddon;
107         }
108
109         /**
110          * Determines the current module based on the App arguments and the server variable
111          *
112          * @param Arguments $args   The Friendica arguments
113          *
114          * @return Module The module with the determined module
115          */
116         public function determineModule(Arguments $args)
117         {
118                 if ($args->getArgc() > 0) {
119                         $module = str_replace('.', '_', $args->get(0));
120                         $module = str_replace('-', '_', $module);
121                 } else {
122                         $module = self::DEFAULT;
123                 }
124
125                 // Compatibility with the Firefox App
126                 if (($module == "users") && ($args->getCommand() == "users/sign_in")) {
127                         $module = "login";
128                 }
129
130                 $isBackend = in_array($module, Module::BACKEND_MODULES);;
131
132                 return new Module($module, $this->module_class, $isBackend, $this->printNotAllowedAddon);
133         }
134
135         /**
136          * Determine the class of the current module
137          *
138          * @param Arguments                 $args   The Friendica execution arguments
139          * @param Router                    $router The Friendica routing instance
140          * @param Core\Config\Configuration $config The Friendica Configuration
141          *
142          * @return Module The determined module of this call
143          *
144          * @throws \Exception
145          */
146         public function determineClass(Arguments $args, Router $router, Core\Config\Configuration $config)
147         {
148                 $printNotAllowedAddon = false;
149
150                 $module_class = null;
151                 /**
152                  * ROUTING
153                  *
154                  * From the request URL, routing consists of obtaining the name of a BaseModule-extending class of which the
155                  * post() and/or content() static methods can be respectively called to produce a data change or an output.
156                  **/
157                 try {
158                         $module_class = $router->getModuleClass($args->getCommand());
159                 } catch (MethodNotAllowedException $e) {
160                         $module_class = MethodNotAllowed::class;
161                 } catch (NotFoundException $e) {
162                         // Then we try addon-provided modules that we wrap in the LegacyModule class
163                         if (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;
168                                 } else {
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;
173                                         }
174                                 }
175                         }
176
177                         /* Finally, we look for a 'standard' program module in the 'mod' directory
178                          * We emulate a Module class through the LegacyModule class
179                          */
180                         if (!$module_class && file_exists("mod/{$this->module}.php")) {
181                                 LegacyModule::setModuleFile("mod/{$this->module}.php");
182                                 $module_class = LegacyModule::class;
183                         }
184
185                         $module_class = $module_class ?: PageNotFound::class;
186                 }
187
188                 return new Module($this->module, $module_class, $this->isBackend, $printNotAllowedAddon);
189         }
190
191         /**
192          * Run the determined module class and calls all hooks applied to
193          *
194          * @param Core\L10n\L10n  $l10n         The L10n instance
195          * @param App             $app          The whole Friendica app (for method arguments)
196          * @param LoggerInterface $logger       The Friendica logger
197          * @param array           $server       The $_SERVER variable
198          * @param array           $post         The $_POST variables
199          *
200          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
201          */
202         public function run(Core\L10n\L10n $l10n, App $app, LoggerInterface $logger, array $server, array $post)
203         {
204                 if ($this->printNotAllowedAddon) {
205                         info($l10n->t("You must be logged in to use addons. "));
206                 }
207
208                 /* The URL provided does not resolve to a valid module.
209                  *
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.
214                  *
215                  * Otherwise we are going to emit a 404 not found.
216                  */
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) {
221                                 exit();
222                         }
223
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']);
227                         }
228
229                         $logger->debug('index.php: page not found.', ['request_uri' => $server['REQUEST_URI'], 'address' => $server['REMOTE_ADDR'], 'query' => $server['QUERY_STRING']]);
230                 }
231
232                 $placeholder = '';
233
234                 Core\Hook::callAll($this->module . '_mod_init', $placeholder);
235
236                 call_user_func([$this->module_class, 'init']);
237
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']);
241
242                 if ($server['REQUEST_METHOD'] === 'POST') {
243                         Core\Hook::callAll($this->module . '_mod_post', $post);
244                         call_user_func([$this->module_class, 'post']);
245                 }
246
247                 Core\Hook::callAll($this->module . '_mod_afterpost', $placeholder);
248                 call_user_func([$this->module_class, 'afterpost']);
249         }
250 }