blob: 87857da57a6521121085e3f539665ae8026021c0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?php declare(strict_types=1);
error_reporting(E_ALL);
require_once(__DIR__.'/../../includes/functions.php');
$config = require_once(__DIR__.'/../../config.php');
if ('GET' !== filter_input(INPUT_SERVER, 'REQUEST_METHOD')) {
respondAndExit(405, 'Method Not Allowed');
}
$requestedUrl = filter_input(INPUT_GET, 'url', FILTER_VALIDATE_URL);
if (!$requestedUrl || strlen($requestedUrl) > $config['max_url_length']) {
respondAndExit(400, 'Bad Request');
}
$urls = file($config['path_to_file'], FILE_IGNORE_NEW_LINES);
// Remove requested url from list if present.
$remaingUrls = array_diff($urls, [$requestedUrl]);
// Count will not have changed if requested url doesn't exist in the list.
if (count($remaingUrls) === count($urls)) {
respondAndExit(404, 'Not Found');
}
if (!file_put_contents($config['path_to_file'], implode("\n", $remaingUrls))) {
respondAndExit(500, 'Internal Server Error');
}
respondAndExit(308, 'Permanent Redirect', ["Location: $requestedUrl"]);
|