3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\HTTPException;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\System;
28 use Friendica\Module\Response;
29 use Friendica\Module\Special\HTTPException as ModuleHTTPException;
30 use Friendica\Network\HTTPException;
31 use Friendica\Util\Profiler;
32 use Psr\Http\Message\ResponseInterface;
33 use Psr\Log\LoggerInterface;
35 class PageNotFound extends BaseModule
38 private $remoteAddress;
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 = [])
42 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
44 $this->remoteAddress = $request->getRemoteAddress();
47 protected function content(array $request = []): string
49 throw new HTTPException\NotFoundException($this->t('Page not found.'));
52 public function run(ModuleHTTPException $httpException, array $request = []): ResponseInterface
54 /* The URL provided does not resolve to a valid module.
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.
61 * Otherwise we are going to emit a 404 not found.
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) {
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']);
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']
80 return parent::run($httpException, $request);