From df4861cba6ee718cae5ab350d657d45f9178d492 Mon Sep 17 00:00:00 2001 From: Matt Wiseley Date: Sat, 28 Nov 2026 10:32:52 -0500 Subject: [PATCH] init --- README.md | 80 +++++++++++++++++++++- index.php | 196 +++++++++++++++++++++++++++++++++++++++++++----------- last.txt | 1 + urls.txt | 1 + 4 files changed, 238 insertions(+), 40 deletions(-) create mode 100644 last.txt create mode 100644 urls.txt diff --git a/README.md b/README.md index a1a9510..52f9058 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,79 @@ -# shortener +# Micro URL Shortener -A single file URL shortener in PHP. Designed for self hosted personal use. \ No newline at end of file +A tiny URL shortener in a single PHP file (`index.php`). It needs no database: links are stored in plain text files. + +## How it works + +- `urls.txt`: one link per line, written as ` `. +- `last.txt`: the most recently issued key. Each new key is made by incrementing it over a shuffled character set (`a`, `c`, `r`, … then `aa`, `ac`, …). +- Visiting `/` sends a 301 redirect to the stored URL, or returns 404 if the key is unknown. +- Visiting `/` shows a mobile-friendly form for shortening a URL. + +## Requirements + +- PHP 7+ with the `mbstring` extension. +- The web server must be able to write `urls.txt` and `last.txt` in the app directory. +- Every request path must be routed to `index.php` (see below). + +## Setup + +1. Copy `index.php` to your web root. +2. Edit the configuration at the top of the file. At minimum, **change `$password`**. +3. Route all requests to `index.php`. + +**Apache** (`.htaccess`): + +```apache +RewriteEngine On +RewriteRule ^(urls|last)\.txt$ - [F] +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^ index.php [L] +``` + +**nginx**: + +```nginx +location ~ ^/(urls|last)\.txt$ { deny all; } +location / { try_files $uri /index.php?$query_string; } +``` + +**Local development** (PHP's built-in server, using `index.php` as the router): + +```sh +php -S localhost:4000 index.php +``` + +> Block direct access to `urls.txt` and `last.txt` (as in the rules above). Otherwise anyone can download your whole link list. + +## Configuration + +| Variable | Default | Purpose | +|--------------------|----------------|----------------------------------------------------------------------| +| `$password` | `'change me!'` | Required to shorten a URL. Set to `''` to disable (not recommended). | +| `$allowed_domains` | `[]` | Domains allowed as link targets. An empty list allows any domain. | +| `$max_url_length` | `100` | Longer URLs are rejected. | +| `$charset` | (shuffled) | Characters used in keys. Don't change it once links exist. | +| `$enable_test` | `false` | Turns on the key-generation test form. | +| `$urls_file`, `$last_file` | `urls.txt`, `last.txt` | Where the data is stored. | + +## API + +Shorten a URL from the command line. The response is plain text: + +```sh +curl --data-urlencode 'url=https://example.com/page' \ + --data-urlencode 'password=your-password' \ + https://your-host/ +# https://your-host/c +``` + +| Status | Meaning | +|--------|----------------------------| +| 200 | Success; body is the short URL | +| 401 | Wrong or missing password | +| 403 | Domain not allowed | +| 422 | Invalid or overlong URL | + +GET parameters work too (`/?url=...&password=...`), but they put the password in server logs and browser history, so prefer POST. + +Short URLs always start with `https://` followed by the request's host. diff --git a/index.php b/index.php index 3698e82..65fa15f 100644 --- a/index.php +++ b/index.php @@ -146,41 +146,46 @@ $key = get_input('key'); $test_start = get_input('test_start'); $test_count = get_input('test_count'); +// Requests from the HTML form send format=html and get the result rendered +// in the page; everything else (curl, scripts) gets the plain-text response. +$from_form = get_input('format') === 'html'; +$shortened_url = null; +$error = null; + // If a URL is provided, attempt to shorten it if ($url) { - // if a password is set, check it - if ($password !== '' && $input_password !== $password) { - http_response_code(401); // Unauthorized - echo "Unauthorized: Incorrect password.\n"; - exit; - } - // return 422 if the URL is longer than max_url_length - if (strlen($url) > $max_url_length) { - http_response_code(422); // Unprocessable Entity - echo "Invalid URL.\n"; - exit; - } - // return 403 if the URL's domain is not in the allowed list + $status = 200; $parsed_url = parse_url($url); - if ($parsed_url === false || !isset($parsed_url['host']) || !test_host($parsed_url['host'])) { - http_response_code(403); - echo "Forbidden: This URL is not within the list of allowed domains.\n"; - exit; - } - // validate the URL and shorten it - if (filter_var($url, FILTER_VALIDATE_URL)) { + if ($password !== '' && $input_password !== $password) { + // if a password is set, check it + $status = 401; // Unauthorized + $error = "Unauthorized: Incorrect password."; + } elseif (strlen($url) > $max_url_length) { + // reject URLs longer than max_url_length + $status = 422; // Unprocessable Entity + $error = "Invalid URL."; + } elseif ($parsed_url === false || !isset($parsed_url['host']) || !test_host($parsed_url['host'])) { + // reject URLs whose domain is not in the allowed list + $status = 403; // Forbidden + $error = "Forbidden: This URL is not within the list of allowed domains."; + } elseif (!filter_var($url, FILTER_VALIDATE_URL)) { + $status = 422; // Unprocessable Entity + $error = "Invalid URL."; + } else { + // valid: shorten it $key = add_url($url); $shortened_url = "https://$_SERVER[HTTP_HOST]/$key"; - echo "$shortened_url\n"; - } else { - http_response_code(422); // Unprocessable Entity - echo "Invalid URL.\n"; } - exit; + http_response_code($status); + if (!$from_form) { + echo ($error ?? $shortened_url) . "\n"; + exit; + } + // form submission: fall through and render the result in the page } // If test parameters are provided, output a series of incremented keys -if ($enabed_test && $test_start !== null && $test_count !== null) { +if ($enable_test &&$test_start !== null && $test_count !== null) { // validate password if ($password !== '' && $input_password !== $password) { http_response_code(401); // Unauthorized @@ -196,7 +201,7 @@ $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $key = trim($path, '/'); // If the key is valid, look up the URL and redirect -if ($key !== '') { +if ($key !== '' && !$from_form) { $url = get_url($key); if ($url !== null) { header("Location: $url", true, 301); @@ -215,16 +220,130 @@ if ($key !== '') { Bagaag.com Micro URL Shortener + +

Bagaag.com Micro URL Shortener

+ +
+

Your short URL:

+
+ + +
+ +
+ + + + +
-
-

+ + + -
-

+ +
@@ -232,17 +351,18 @@ if ($key !== '') {

Test Key Generation

-
-

-
-

+ + + + -
-

+ +
- +
diff --git a/last.txt b/last.txt new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/last.txt @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/urls.txt b/urls.txt new file mode 100644 index 0000000..e86e29d --- /dev/null +++ b/urls.txt @@ -0,0 +1 @@ +a https://cnn.com