]> git.mxchange.org Git - friendica.git/blob - src/Module/HTTPException/PageNotFound.php
Don't display Diaspora reshares
[friendica.git] / src / Module / HTTPException / PageNotFound.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\HTTPException;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\System;
28 use Friendica\DI;
29 use Friendica\Module\Response;
30 use Friendica\Network\HTTPException;
31 use Friendica\Util\Profiler;
32 use Psr\Http\Message\ResponseInterface;
33 use Psr\Log\LoggerInterface;
34
35 class PageNotFound extends BaseModule
36 {
37         /** @var string */
38         private $remoteAddress;
39
40         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, App\Request $request, array $server, array $parameters = [])
41         {
42                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
43
44                 $this->remoteAddress = $request->getRemoteAddress();
45         }
46
47         protected function content(array $request = []): string
48         {
49                 throw new HTTPException\NotFoundException(DI::l10n()->t('Page not found.'));
50         }
51
52         public function run(array $request = []): ResponseInterface
53         {
54                 /* The URL provided does not resolve to a valid module.
55                  *
56                  * On Dreamhost sites, quite often things go wrong for no apparent reason and they send us to '/internal_error.html'.
57                  * We don't like doing this, but as it occasionally accounts for 10-20% or more of all site traffic -
58                  * 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
59                  * this will often succeed and eventually do the right thing.
60                  *
61                  * Otherwise we are going to emit a 404 not found.
62                  */
63                 $queryString = $this->server['QUERY_STRING'];
64                 // Stupid browser tried to pre-fetch our Javascript img template. Don't log the event or return anything - just quietly exit.
65                 if (!empty($queryString) && preg_match('/{[0-9]}/', $queryString) !== 0) {
66                         System::exit();
67                 }
68
69                 if (!empty($queryString) && ($queryString === 'q=internal_error.html') && isset($dreamhost_error_hack)) {
70                         $this->logger->info('index.php: dreamhost_error_hack invoked.', ['Original URI' => $this->server['REQUEST_URI']]);
71                         $this->baseUrl->redirect($this->server['REQUEST_URI']);
72                 }
73
74                 $this->logger->debug('index.php: page not found.', [
75                         'request_uri' => $this->server['REQUEST_URI'],
76                         'address'     => $this->remoteAddress,
77                         'query'       => $this->server['QUERY_STRING']
78                 ]);
79
80                 return parent::run($request); // TODO: Change the autogenerated stub
81         }
82 }