Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
8.0KB

  1. <?php
  2. namespace Friendica\App;
  3. use Friendica\App;
  4. use Friendica\BaseObject;
  5. use Friendica\Core;
  6. use Friendica\LegacyModule;
  7. use Friendica\Module\Home;
  8. use Friendica\Module\HTTPException\MethodNotAllowed;
  9. use Friendica\Module\HTTPException\PageNotFound;
  10. use Friendica\Network\HTTPException\MethodNotAllowedException;
  11. use Friendica\Network\HTTPException\NotFoundException;
  12. use Psr\Log\LoggerInterface;
  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. * @var string The module name
  54. */
  55. private $module;
  56. /**
  57. * @var BaseObject The module class
  58. */
  59. private $module_class;
  60. /**
  61. * @var array The module parameters
  62. */
  63. private $module_parameters;
  64. /**
  65. * @var bool true, if the module is a backend module
  66. */
  67. private $isBackend;
  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. * @return string
  74. */
  75. public function getName()
  76. {
  77. return $this->module;
  78. }
  79. /**
  80. * @return string The base class name
  81. */
  82. public function getClassName()
  83. {
  84. return $this->module_class;
  85. }
  86. /**
  87. * @return array The module parameters extracted from the route
  88. */
  89. public function getParameters()
  90. {
  91. return $this->module_parameters;
  92. }
  93. /**
  94. * @return bool True, if the current module is a backend module
  95. * @see Module::BACKEND_MODULES for a list
  96. */
  97. public function isBackend()
  98. {
  99. return $this->isBackend;
  100. }
  101. public function __construct(string $module = self::DEFAULT, string $moduleClass = self::DEFAULT_CLASS, array $moduleParameters = [], bool $isBackend = false, bool $printNotAllowedAddon = false)
  102. {
  103. $this->module = $module;
  104. $this->module_class = $moduleClass;
  105. $this->module_parameters = $moduleParameters;
  106. $this->isBackend = $isBackend;
  107. $this->printNotAllowedAddon = $printNotAllowedAddon;
  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. // Compatibility with the Firefox App
  125. if (($module == "users") && ($args->getCommand() == "users/sign_in")) {
  126. $module = "login";
  127. }
  128. $isBackend = in_array($module, Module::BACKEND_MODULES);;
  129. return new Module($module, $this->module_class, [], $isBackend, $this->printNotAllowedAddon);
  130. }
  131. /**
  132. * Determine the class of the current module
  133. *
  134. * @param Arguments $args The Friendica execution arguments
  135. * @param Router $router The Friendica routing instance
  136. * @param Core\Config\Configuration $config The Friendica Configuration
  137. *
  138. * @return Module The determined module of this call
  139. *
  140. * @throws \Exception
  141. */
  142. public function determineClass(Arguments $args, Router $router, Core\Config\Configuration $config)
  143. {
  144. $printNotAllowedAddon = false;
  145. $module_class = null;
  146. $module_parameters = [];
  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. try {
  154. $module_class = $router->getModuleClass($args->getCommand());
  155. $module_parameters = $router->getModuleParameters();
  156. } catch (MethodNotAllowedException $e) {
  157. $module_class = MethodNotAllowed::class;
  158. } catch (NotFoundException $e) {
  159. // Then we try addon-provided modules that we wrap in the LegacyModule class
  160. if (Core\Addon::isEnabled($this->module) && file_exists("addon/{$this->module}/{$this->module}.php")) {
  161. //Check if module is an app and if public access to apps is allowed or not
  162. $privateapps = $config->get('config', 'private_addons', false);
  163. if ((!local_user()) && Core\Hook::isAddonApp($this->module) && $privateapps) {
  164. $printNotAllowedAddon = true;
  165. } else {
  166. include_once "addon/{$this->module}/{$this->module}.php";
  167. if (function_exists($this->module . '_module')) {
  168. LegacyModule::setModuleFile("addon/{$this->module}/{$this->module}.php");
  169. $module_class = LegacyModule::class;
  170. }
  171. }
  172. }
  173. /* Finally, we look for a 'standard' program module in the 'mod' directory
  174. * We emulate a Module class through the LegacyModule class
  175. */
  176. if (!$module_class && file_exists("mod/{$this->module}.php")) {
  177. LegacyModule::setModuleFile("mod/{$this->module}.php");
  178. $module_class = LegacyModule::class;
  179. }
  180. $module_class = $module_class ?: PageNotFound::class;
  181. }
  182. return new Module($this->module, $module_class, $module_parameters, $this->isBackend, $printNotAllowedAddon);
  183. }
  184. /**
  185. * Run the determined module class and calls all hooks applied to
  186. *
  187. * @param Core\L10n\L10n $l10n The L10n instance
  188. * @param App $app The whole Friendica app (for method arguments)
  189. * @param LoggerInterface $logger The Friendica logger
  190. * @param array $server The $_SERVER variable
  191. * @param array $post The $_POST variables
  192. *
  193. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  194. */
  195. public function run(Core\L10n\L10n $l10n, App $app, LoggerInterface $logger, array $server, array $post)
  196. {
  197. if ($this->printNotAllowedAddon) {
  198. info($l10n->t("You must be logged in to use addons. "));
  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. if (!empty($queryString) && ($queryString === 'q=internal_error.html') && isset($dreamhost_error_hack)) {
  216. $logger->info('index.php: dreamhost_error_hack invoked.', ['Original URI' => $server['REQUEST_URI']]);
  217. $app->internalRedirect($server['REQUEST_URI']);
  218. }
  219. $logger->debug('index.php: page not found.', ['request_uri' => $server['REQUEST_URI'], 'address' => $server['REMOTE_ADDR'], 'query' => $server['QUERY_STRING']]);
  220. }
  221. $placeholder = '';
  222. Core\Hook::callAll($this->module . '_mod_init', $placeholder);
  223. call_user_func([$this->module_class, 'init'], $this->module_parameters);
  224. // "rawContent" is especially meant for technical endpoints.
  225. // This endpoint doesn't need any theme initialization or other comparable stuff.
  226. call_user_func([$this->module_class, 'rawContent'], $this->module_parameters);
  227. if ($server['REQUEST_METHOD'] === 'POST') {
  228. Core\Hook::callAll($this->module . '_mod_post', $post);
  229. call_user_func([$this->module_class, 'post'], $this->module_parameters);
  230. }
  231. Core\Hook::callAll($this->module . '_mod_afterpost', $placeholder);
  232. call_user_func([$this->module_class, 'afterpost'], $this->module_parameters);
  233. }
  234. }