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