Browse Source

Remove $_SERVER['PHP_SELF'] usage

tags/2019.09
Philipp Holzer 1 month ago
parent
commit
b13dbb3dcb
No account linked to committer's email address

+ 3
- 0
bin/auth_ejabberd.php View File

@@ -36,6 +36,7 @@ use Dice\Dice;
36 36
 use Friendica\App\Mode;
37 37
 use Friendica\BaseObject;
38 38
 use Friendica\Util\ExAuth;
39
+use Psr\Log\LoggerInterface;
39 40
 
40 41
 if (sizeof($_SERVER["argv"]) == 0) {
41 42
 	die();
@@ -54,6 +55,8 @@ chdir($directory);
54 55
 require dirname(__DIR__) . '/vendor/autoload.php';
55 56
 
56 57
 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
58
+$dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['auth_ejabberd']]);
59
+
57 60
 BaseObject::setDependencyInjection($dice);
58 61
 
59 62
 $appMode = $dice->create(Mode::class);

+ 2
- 0
bin/console.php View File

@@ -2,9 +2,11 @@
2 2
 <?php
3 3
 
4 4
 use Dice\Dice;
5
+use Psr\Log\LoggerInterface;
5 6
 
6 7
 require dirname(__DIR__) . '/vendor/autoload.php';
7 8
 
8 9
 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
10
+$dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['console']]);
9 11
 
10 12
 (new Friendica\Core\Console($dice, $argv))->execute();

+ 2
- 0
bin/daemon.php View File

@@ -12,6 +12,7 @@ use Friendica\Core\Config;
12 12
 use Friendica\Core\Logger;
13 13
 use Friendica\Core\Worker;
14 14
 use Friendica\Database\DBA;
15
+use Psr\Log\LoggerInterface;
15 16
 
16 17
 // Get options
17 18
 $shortopts = 'f';
@@ -33,6 +34,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
33 34
 require dirname(__DIR__) . '/vendor/autoload.php';
34 35
 
35 36
 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
37
+$dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['daemon']]);
36 38
 
37 39
 \Friendica\BaseObject::setDependencyInjection($dice);
38 40
 $a = \Friendica\BaseObject::getApp();

+ 2
- 0
bin/worker.php View File

@@ -11,6 +11,7 @@ use Friendica\BaseObject;
11 11
 use Friendica\Core\Config;
12 12
 use Friendica\Core\Update;
13 13
 use Friendica\Core\Worker;
14
+use Psr\Log\LoggerInterface;
14 15
 
15 16
 // Get options
16 17
 $shortopts = 'sn';
@@ -32,6 +33,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
32 33
 require dirname(__DIR__) . '/vendor/autoload.php';
33 34
 
34 35
 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
36
+$dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['worker']]);
35 37
 
36 38
 BaseObject::setDependencyInjection($dice);
37 39
 $a = BaseObject::getApp();

+ 1
- 0
index.php View File

@@ -13,6 +13,7 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
13 13
 require __DIR__ . '/vendor/autoload.php';
14 14
 
15 15
 $dice = (new Dice())->addRules(include __DIR__ . '/static/dependencies.config.php');
16
+$dice = $dice->addRule(Friendica\App\Mode::class, ['call' => [['determineRunMode', [false, $_SERVER], Dice::CHAIN_CALL]]]);
16 17
 
17 18
 \Friendica\BaseObject::setDependencyInjection($dice);
18 19
 

+ 3
- 2
src/App/Mode.php View File

@@ -106,15 +106,16 @@ class Mode
106 106
 	/**
107 107
 	 * Checks if the site is called via a backend process
108 108
 	 *
109
+	 * @param bool         $isBackend    True, if the call is from a backend script (daemon, worker, ...)
109 110
 	 * @param Module       $module       The pre-loaded module (just name, not class!)
110 111
 	 * @param array        $server       The $_SERVER variable
111 112
 	 * @param MobileDetect $mobileDetect The mobile detection library
112 113
 	 *
113 114
 	 * @return Mode returns the determined mode
114 115
 	 */
115
-	public function determineRunMode(Module $module, array $server, MobileDetect $mobileDetect)
116
+	public function determineRunMode(bool $isBackend, Module $module, array $server, MobileDetect $mobileDetect)
116 117
 	{
117
-		$isBackend = basename(($server['PHP_SELF'] ?? ''), '.php') !== 'index' ||
118
+		$isBackend = $isBackend ||
118 119
 		             $module->isBackend();
119 120
 		$isMobile  = $mobileDetect->isMobile();
120 121
 		$isTablet  = $mobileDetect->isTablet();

+ 9
- 11
src/Factory/LoggerFactory.php View File

@@ -38,19 +38,17 @@ class LoggerFactory
38 38
 		'Friendica\\Util\\Logger',
39 39
 	];
40 40
 
41
-	/**
42
-	 * Retrieve the channel based on the __FILE__
43
-	 *
44
-	 * @return string
45
-	 */
46
-	private function findChannel()
41
+	private $channel;
42
+
43
+	public function __construct(string $channel)
47 44
 	{
48
-		return basename($_SERVER['PHP_SELF'], '.php');
45
+		$this->channel = $channel;
49 46
 	}
50 47
 
51 48
 	/**
52 49
 	 * Creates a new PSR-3 compliant logger instances
53 50
 	 *
51
+	 * @param Database      $database The Friendica Database instance
54 52
 	 * @param Configuration $config   The config
55 53
 	 * @param Profiler      $profiler The profiler of the app
56 54
 	 *
@@ -59,7 +57,7 @@ class LoggerFactory
59 57
 	 * @throws \Exception
60 58
 	 * @throws InternalServerErrorException
61 59
 	 */
62
-	public function create(Database $database, Configuration $config, Profiler $profiler)
60
+	public function create( Database $database, Configuration $config, Profiler $profiler)
63 61
 	{
64 62
 		if (empty($config->get('system', 'debugging', false))) {
65 63
 			$logger = new VoidLogger();
@@ -76,7 +74,7 @@ class LoggerFactory
76 74
 				$loggerTimeZone = new \DateTimeZone('UTC');
77 75
 				Monolog\Logger::setTimezone($loggerTimeZone);
78 76
 
79
-				$logger = new Monolog\Logger($this->findChannel());
77
+				$logger = new Monolog\Logger($this->channel);
80 78
 				$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
81 79
 				$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
82 80
 				$logger->pushProcessor(new Monolog\Processor\UidProcessor());
@@ -91,7 +89,7 @@ class LoggerFactory
91 89
 				break;
92 90
 
93 91
 			case 'syslog':
94
-				$logger = new SyslogLogger($this->findChannel(), $introspection, $loglevel);
92
+				$logger = new SyslogLogger($this->channel, $introspection, $loglevel);
95 93
 				break;
96 94
 
97 95
 			case 'stream':
@@ -99,7 +97,7 @@ class LoggerFactory
99 97
 				$stream = $config->get('system', 'logfile');
100 98
 				// just add a stream in case it's either writable or not file
101 99
 				if (!is_file($stream) || is_writable($stream)) {
102
-					$logger = new StreamLogger($this->findChannel(), $stream, $introspection, $loglevel);
100
+					$logger = new StreamLogger($this->channel, $stream, $introspection, $loglevel);
103 101
 				} else {
104 102
 					$logger = new VoidLogger();
105 103
 				}

+ 8
- 2
static/dependencies.config.php View File

@@ -62,7 +62,7 @@ return [
62 62
 	],
63 63
 	App\Mode::class                 => [
64 64
 		'call' => [
65
-			['determineRunMode', [$_SERVER], Dice::CHAIN_CALL],
65
+			['determineRunMode', [true, $_SERVER], Dice::CHAIN_CALL],
66 66
 			['determine', [], Dice::CHAIN_CALL],
67 67
 		],
68 68
 	],
@@ -114,12 +114,18 @@ return [
114 114
 	 */
115 115
 	LoggerInterface::class          => [
116 116
 		'instanceOf' => Factory\LoggerFactory::class,
117
+		'constructParams' => [
118
+			'index',
119
+		],
117 120
 		'call'       => [
118
-			['create', [], Dice::CHAIN_CALL],
121
+			['create', ['index'], Dice::CHAIN_CALL],
119 122
 		],
120 123
 	],
121 124
 	'$devLogger'                    => [
122 125
 		'instanceOf' => Factory\LoggerFactory::class,
126
+		'constructParams' => [
127
+			'dev',
128
+		],
123 129
 		'call'       => [
124 130
 			['createDev', [], Dice::CHAIN_CALL],
125 131
 		]

+ 11
- 11
tests/src/App/ModeTest.php View File

@@ -183,13 +183,13 @@ class ModeTest extends MockedTest
183 183
 	/**
184 184
 	 * Test if not called by index is backend
185 185
 	 */
186
-	public function testIsBackendNotIndex()
186
+	public function testIsBackendNotIsBackend()
187 187
 	{
188
-		$server = ['PHP_SELF' => '/daemon.php'];
188
+		$server = [];
189 189
 		$module = new Module();
190 190
 		$mobileDetect = new MobileDetect();
191 191
 
192
-		$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
192
+		$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
193 193
 
194 194
 		$this->assertTrue($mode->isBackend());
195 195
 	}
@@ -199,11 +199,11 @@ class ModeTest extends MockedTest
199 199
 	 */
200 200
 	public function testIsBackendButIndex()
201 201
 	{
202
-		$server = ['PHP_SELF' => '/index.php'];
202
+		$server = [];
203 203
 		$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true);
204 204
 		$mobileDetect = new MobileDetect();
205 205
 
206
-		$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
206
+		$mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
207 207
 
208 208
 		$this->assertTrue($mode->isBackend());
209 209
 	}
@@ -213,11 +213,11 @@ class ModeTest extends MockedTest
213 213
 	 */
214 214
 	public function testIsNotBackend()
215 215
 	{
216
-		$server = ['PHP_SELF' => '/index.php'];
216
+		$server = [];
217 217
 		$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
218 218
 		$mobileDetect = new MobileDetect();
219 219
 
220
-		$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
220
+		$mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
221 221
 
222 222
 		$this->assertFalse($mode->isBackend());
223 223
 	}
@@ -235,7 +235,7 @@ class ModeTest extends MockedTest
235 235
 		$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
236 236
 		$mobileDetect = new MobileDetect();
237 237
 
238
-		$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
238
+		$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
239 239
 
240 240
 		$this->assertTrue($mode->isAjax());
241 241
 	}
@@ -249,7 +249,7 @@ class ModeTest extends MockedTest
249 249
 		$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
250 250
 		$mobileDetect = new MobileDetect();
251 251
 
252
-		$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
252
+		$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
253 253
 
254 254
 		$this->assertFalse($mode->isAjax());
255 255
 	}
@@ -265,7 +265,7 @@ class ModeTest extends MockedTest
265 265
 		$mobileDetect->shouldReceive('isMobile')->andReturn(true);
266 266
 		$mobileDetect->shouldReceive('isTablet')->andReturn(true);
267 267
 
268
-		$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
268
+		$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
269 269
 
270 270
 		$this->assertTrue($mode->isMobile());
271 271
 		$this->assertTrue($mode->isTablet());
@@ -283,7 +283,7 @@ class ModeTest extends MockedTest
283 283
 		$mobileDetect->shouldReceive('isMobile')->andReturn(false);
284 284
 		$mobileDetect->shouldReceive('isTablet')->andReturn(false);
285 285
 
286
-		$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
286
+		$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
287 287
 
288 288
 		$this->assertFalse($mode->isMobile());
289 289
 		$this->assertFalse($mode->isTablet());

Loading…
Cancel
Save