]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Router.php
Merge pull request #12213 from Schnoop/bugfix/NodeInfo
[friendica.git] / src / App / Router.php
index 6e390a84d9b145f4fcf76a86b1b30ff22001dc9c..4e5f29521a822a9bf20a1bdd93914bc4b5994283 100644 (file)
@@ -34,6 +34,7 @@ use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Lock\Capability\ICanLock;
+use Friendica\Core\Session\Capability\IHandleUserSessions;
 use Friendica\LegacyModule;
 use Friendica\Module\HTTPException\MethodNotAllowed;
 use Friendica\Module\HTTPException\PageNotFound;
@@ -98,6 +99,9 @@ class Router
        /** @var LoggerInterface */
        private $logger;
 
+       /** @var bool */
+       private $isLocalUser;
+
        /** @var float */
        private $dice_profiler_threshold;
 
@@ -120,9 +124,10 @@ class Router
         * @param Arguments           $args
         * @param LoggerInterface     $logger
         * @param Dice                $dice
+        * @param IHandleUserSessions $userSession
         * @param RouteCollector|null $routeCollector
         */
-       public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, IManageConfigValues $config, Arguments $args, LoggerInterface $logger, Dice $dice, RouteCollector $routeCollector = null)
+       public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, IManageConfigValues $config, Arguments $args, LoggerInterface $logger, Dice $dice, IHandleUserSessions $userSession, RouteCollector $routeCollector = null)
        {
                $this->baseRoutesFilepath      = $baseRoutesFilepath;
                $this->l10n                    = $l10n;
@@ -133,6 +138,7 @@ class Router
                $this->dice                    = $dice;
                $this->server                  = $server;
                $this->logger                  = $logger;
+               $this->isLocalUser             = !empty($userSession->getLocalUserId());
                $this->dice_profiler_threshold = $config->get('system', 'dice_profiler_threshold', 0);
 
                $this->routeCollector = $routeCollector ?? new RouteCollector(new Std(), new GroupCountBased());
@@ -152,7 +158,7 @@ class Router
         *
         * @throws HTTPException\InternalServerErrorException In case of invalid configs
         */
-       public function loadRoutes(array $routes)
+       public function loadRoutes(array $routes): Router
        {
                $routeCollector = ($this->routeCollector ?? new RouteCollector(new Std(), new GroupCountBased()));
 
@@ -166,6 +172,13 @@ class Router
                return $this;
        }
 
+       /**
+        * Adds multiple routes to a route collector
+        *
+        * @param RouteCollector $routeCollector Route collector instance
+        * @param array $routes Multiple routes to be added
+        * @throws HTTPException\InternalServerErrorException If route was wrong (somehow)
+        */
        private function addRoutes(RouteCollector $routeCollector, array $routes)
        {
                foreach ($routes as $route => $config) {
@@ -221,7 +234,7 @@ class Router
         *
         * @return bool
         */
-       private function isRoute(array $config)
+       private function isRoute(array $config): bool
        {
                return
                        // The config array should at least have one entry
@@ -253,7 +266,7 @@ class Router
         * @throws HTTPException\MethodNotAllowedException    If a rule matched but the method didn't
         * @throws HTTPException\NotFoundException            If no rule matched
         */
-       private function getModuleClass()
+       private function getModuleClass(): string
        {
                $cmd = $this->args->getCommand();
                $cmd = '/' . ltrim($cmd, '/');
@@ -301,7 +314,7 @@ class Router
                        if (Addon::isEnabled($moduleName) && file_exists("addon/{$moduleName}/{$moduleName}.php")) {
                                //Check if module is an app and if public access to apps is allowed or not
                                $privateapps = $this->config->get('config', 'private_addons', false);
-                               if ((!local_user()) && Hook::isAddonApp($moduleName) && $privateapps) {
+                               if (!$this->isLocalUser && Hook::isAddonApp($moduleName) && $privateapps) {
                                        throw new MethodNotAllowedException($this->l10n->t("You must be logged in to use addons. "));
                                } else {
                                        include_once "addon/{$moduleName}/{$moduleName}.php";
@@ -332,7 +345,7 @@ class Router
                        if ($this->dice_profiler_threshold > 0) {
                                $dur = floatval(microtime(true) - $stamp);
                                if ($dur >= $this->dice_profiler_threshold) {
-                                       $this->logger->warning('Dice module creation lasts too long.', ['duration' => round($dur, 3), 'module' => $module_class, 'parameters' => $module_parameters]);
+                                       $this->logger->notice('Dice module creation lasts too long.', ['duration' => round($dur, 3), 'module' => $module_class, 'parameters' => $module_parameters]);
                                }
                        }
                }