]> git.mxchange.org Git - friendica.git/commitdiff
Remove $_SERVER['PHP_SELF'] usage
authorPhilipp Holzer <admin+github@philipp.info>
Tue, 17 Sep 2019 14:47:00 +0000 (16:47 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Tue, 17 Sep 2019 14:47:00 +0000 (16:47 +0200)
bin/auth_ejabberd.php
bin/console.php
bin/daemon.php
bin/worker.php
index.php
src/App/Mode.php
src/Factory/LoggerFactory.php
static/dependencies.config.php
tests/src/App/ModeTest.php

index 206e484470058bff0aa98c3625f2f47b4f29e0e4..5ccdd0174bb5068cea5d3716ca5903e6241e4825 100755 (executable)
@@ -36,6 +36,7 @@ use Dice\Dice;
 use Friendica\App\Mode;
 use Friendica\BaseObject;
 use Friendica\Util\ExAuth;
+use Psr\Log\LoggerInterface;
 
 if (sizeof($_SERVER["argv"]) == 0) {
        die();
@@ -54,6 +55,8 @@ chdir($directory);
 require dirname(__DIR__) . '/vendor/autoload.php';
 
 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
+$dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['auth_ejabberd']]);
+
 BaseObject::setDependencyInjection($dice);
 
 $appMode = $dice->create(Mode::class);
index 4c396854dc09c06564f5c4e86b01c645f49dfc5c..a6513a2e8f992bdd52d5a8bfea7ff81e5c7e94ef 100755 (executable)
@@ -2,9 +2,11 @@
 <?php
 
 use Dice\Dice;
+use Psr\Log\LoggerInterface;
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
+$dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['console']]);
 
 (new Friendica\Core\Console($dice, $argv))->execute();
index 8ea60fa9ac79b86f247f30325e222de011c8df5c..948829c1bf8ae02815af420267ce3c1f3f02e7d3 100755 (executable)
@@ -12,6 +12,7 @@ use Friendica\Core\Config;
 use Friendica\Core\Logger;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
+use Psr\Log\LoggerInterface;
 
 // Get options
 $shortopts = 'f';
@@ -33,6 +34,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
 require dirname(__DIR__) . '/vendor/autoload.php';
 
 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
+$dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['daemon']]);
 
 \Friendica\BaseObject::setDependencyInjection($dice);
 $a = \Friendica\BaseObject::getApp();
index f6b2d90a59313ab088f2653e8a61417a62e67465..469dcb001e6419accd062df13597abc705cfbf2d 100755 (executable)
@@ -11,6 +11,7 @@ use Friendica\BaseObject;
 use Friendica\Core\Config;
 use Friendica\Core\Update;
 use Friendica\Core\Worker;
+use Psr\Log\LoggerInterface;
 
 // Get options
 $shortopts = 'sn';
@@ -32,6 +33,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
 require dirname(__DIR__) . '/vendor/autoload.php';
 
 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
+$dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['worker']]);
 
 BaseObject::setDependencyInjection($dice);
 $a = BaseObject::getApp();
index 1e6439e0383cb22d20d80c91af4bbbeef393b116..5407532d494fdec91eefcf52b00c966ba8c26056 100644 (file)
--- a/index.php
+++ b/index.php
@@ -13,6 +13,7 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
 require __DIR__ . '/vendor/autoload.php';
 
 $dice = (new Dice())->addRules(include __DIR__ . '/static/dependencies.config.php');
+$dice = $dice->addRule(Friendica\App\Mode::class, ['call' => [['determineRunMode', [false, $_SERVER], Dice::CHAIN_CALL]]]);
 
 \Friendica\BaseObject::setDependencyInjection($dice);
 
index f3f269b87b9f8b3948556149397cb1e3613aeed1..0a5f98e24f2617d03abc4cbc9ca52d2cb7b80d09 100644 (file)
@@ -106,15 +106,16 @@ class Mode
        /**
         * Checks if the site is called via a backend process
         *
+        * @param bool         $isBackend    True, if the call is from a backend script (daemon, worker, ...)
         * @param Module       $module       The pre-loaded module (just name, not class!)
         * @param array        $server       The $_SERVER variable
         * @param MobileDetect $mobileDetect The mobile detection library
         *
         * @return Mode returns the determined mode
         */
-       public function determineRunMode(Module $module, array $server, MobileDetect $mobileDetect)
+       public function determineRunMode(bool $isBackend, Module $module, array $server, MobileDetect $mobileDetect)
        {
-               $isBackend = basename(($server['PHP_SELF'] ?? ''), '.php') !== 'index' ||
+               $isBackend = $isBackend ||
                             $module->isBackend();
                $isMobile  = $mobileDetect->isMobile();
                $isTablet  = $mobileDetect->isTablet();
index 0feb3b2f7842b787e04084e176075c63d0b818c4..55091a4879456c81d366b669b83f93b28d8ee6c6 100644 (file)
@@ -38,19 +38,17 @@ class LoggerFactory
                'Friendica\\Util\\Logger',
        ];
 
-       /**
-        * Retrieve the channel based on the __FILE__
-        *
-        * @return string
-        */
-       private function findChannel()
+       private $channel;
+
+       public function __construct(string $channel)
        {
-               return basename($_SERVER['PHP_SELF'], '.php');
+               $this->channel = $channel;
        }
 
        /**
         * Creates a new PSR-3 compliant logger instances
         *
+        * @param Database      $database The Friendica Database instance
         * @param Configuration $config   The config
         * @param Profiler      $profiler The profiler of the app
         *
@@ -59,7 +57,7 @@ class LoggerFactory
         * @throws \Exception
         * @throws InternalServerErrorException
         */
-       public function create(Database $database, Configuration $config, Profiler $profiler)
+       public function create( Database $database, Configuration $config, Profiler $profiler)
        {
                if (empty($config->get('system', 'debugging', false))) {
                        $logger = new VoidLogger();
@@ -76,7 +74,7 @@ class LoggerFactory
                                $loggerTimeZone = new \DateTimeZone('UTC');
                                Monolog\Logger::setTimezone($loggerTimeZone);
 
-                               $logger = new Monolog\Logger($this->findChannel());
+                               $logger = new Monolog\Logger($this->channel);
                                $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
                                $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
                                $logger->pushProcessor(new Monolog\Processor\UidProcessor());
@@ -91,7 +89,7 @@ class LoggerFactory
                                break;
 
                        case 'syslog':
-                               $logger = new SyslogLogger($this->findChannel(), $introspection, $loglevel);
+                               $logger = new SyslogLogger($this->channel, $introspection, $loglevel);
                                break;
 
                        case 'stream':
@@ -99,7 +97,7 @@ class LoggerFactory
                                $stream = $config->get('system', 'logfile');
                                // just add a stream in case it's either writable or not file
                                if (!is_file($stream) || is_writable($stream)) {
-                                       $logger = new StreamLogger($this->findChannel(), $stream, $introspection, $loglevel);
+                                       $logger = new StreamLogger($this->channel, $stream, $introspection, $loglevel);
                                } else {
                                        $logger = new VoidLogger();
                                }
index fc2da9c093fdb6c65d2092ebb8273f4519165e0f..59e9d56fea3b838ccfd8b5a8efb7d032ad4333e3 100644 (file)
@@ -62,7 +62,7 @@ return [
        ],
        App\Mode::class                 => [
                'call' => [
-                       ['determineRunMode', [$_SERVER], Dice::CHAIN_CALL],
+                       ['determineRunMode', [true, $_SERVER], Dice::CHAIN_CALL],
                        ['determine', [], Dice::CHAIN_CALL],
                ],
        ],
@@ -114,12 +114,18 @@ return [
         */
        LoggerInterface::class          => [
                'instanceOf' => Factory\LoggerFactory::class,
+               'constructParams' => [
+                       'index',
+               ],
                'call'       => [
-                       ['create', [], Dice::CHAIN_CALL],
+                       ['create', ['index'], Dice::CHAIN_CALL],
                ],
        ],
        '$devLogger'                    => [
                'instanceOf' => Factory\LoggerFactory::class,
+               'constructParams' => [
+                       'dev',
+               ],
                'call'       => [
                        ['createDev', [], Dice::CHAIN_CALL],
                ]
index fcd928e6ed2d6889b1486768a10e91681e269c68..fe65793f8e1b92e5699bf80aa447bd45d8a83a30 100644 (file)
@@ -183,13 +183,13 @@ class ModeTest extends MockedTest
        /**
         * Test if not called by index is backend
         */
-       public function testIsBackendNotIndex()
+       public function testIsBackendNotIsBackend()
        {
-               $server = ['PHP_SELF' => '/daemon.php'];
+               $server = [];
                $module = new Module();
                $mobileDetect = new MobileDetect();
 
-               $mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
+               $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
 
                $this->assertTrue($mode->isBackend());
        }
@@ -199,11 +199,11 @@ class ModeTest extends MockedTest
         */
        public function testIsBackendButIndex()
        {
-               $server = ['PHP_SELF' => '/index.php'];
+               $server = [];
                $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true);
                $mobileDetect = new MobileDetect();
 
-               $mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
+               $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
 
                $this->assertTrue($mode->isBackend());
        }
@@ -213,11 +213,11 @@ class ModeTest extends MockedTest
         */
        public function testIsNotBackend()
        {
-               $server = ['PHP_SELF' => '/index.php'];
+               $server = [];
                $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
                $mobileDetect = new MobileDetect();
 
-               $mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
+               $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
 
                $this->assertFalse($mode->isBackend());
        }
@@ -235,7 +235,7 @@ class ModeTest extends MockedTest
                $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
                $mobileDetect = new MobileDetect();
 
-               $mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
+               $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
 
                $this->assertTrue($mode->isAjax());
        }
@@ -249,7 +249,7 @@ class ModeTest extends MockedTest
                $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
                $mobileDetect = new MobileDetect();
 
-               $mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
+               $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
 
                $this->assertFalse($mode->isAjax());
        }
@@ -265,7 +265,7 @@ class ModeTest extends MockedTest
                $mobileDetect->shouldReceive('isMobile')->andReturn(true);
                $mobileDetect->shouldReceive('isTablet')->andReturn(true);
 
-               $mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
+               $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
 
                $this->assertTrue($mode->isMobile());
                $this->assertTrue($mode->isTablet());
@@ -283,7 +283,7 @@ class ModeTest extends MockedTest
                $mobileDetect->shouldReceive('isMobile')->andReturn(false);
                $mobileDetect->shouldReceive('isTablet')->andReturn(false);
 
-               $mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
+               $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
 
                $this->assertFalse($mode->isMobile());
                $this->assertFalse($mode->isTablet());