]> git.mxchange.org Git - friendica.git/blob - src/Module/HTTPException/PageNotFound.php
Merge pull request #11005 from nupplaphil/feat/module_di
[friendica.git] / src / Module / HTTPException / PageNotFound.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\BaseModule;
25 use Friendica\DI;
26 use Friendica\Network\HTTPException;
27 use Psr\Http\Message\ResponseInterface;
28
29 class PageNotFound extends BaseModule
30 {
31         protected function content(array $request = []): string
32         {
33                 throw new HTTPException\NotFoundException(DI::l10n()->t('Page not found.'));
34         }
35
36         public function run(array $post = [], array $request = []): ResponseInterface
37         {
38                 /* The URL provided does not resolve to a valid module.
39                  *
40                  * On Dreamhost sites, quite often things go wrong for no apparent reason and they send us to '/internal_error.html'.
41                  * We don't like doing this, but as it occasionally accounts for 10-20% or more of all site traffic -
42                  * 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
43                  * this will often succeed and eventually do the right thing.
44                  *
45                  * Otherwise we are going to emit a 404 not found.
46                  */
47                 $queryString = $this->server['QUERY_STRING'];
48                 // Stupid browser tried to pre-fetch our Javascript img template. Don't log the event or return anything - just quietly exit.
49                 if (!empty($queryString) && preg_match('/{[0-9]}/', $queryString) !== 0) {
50                         exit();
51                 }
52
53                 if (!empty($queryString) && ($queryString === 'q=internal_error.html') && isset($dreamhost_error_hack)) {
54                         $this->logger->info('index.php: dreamhost_error_hack invoked.', ['Original URI' => $this->server['REQUEST_URI']]);
55                         $this->baseUrl->redirect($this->server['REQUEST_URI']);
56                 }
57
58                 $this->logger->debug('index.php: page not found.', [
59                         'request_uri' => $this->server['REQUEST_URI'],
60                         'address'     => $this->server['REMOTE_ADDR'],
61                         'query'       => $this->server['QUERY_STRING']
62                 ]);
63
64                 return parent::run($post, $request); // TODO: Change the autogenerated stub
65         }
66 }