diff --git a/.htaccess b/.htaccess index e2bbc23..5a9840e 100644 --- a/.htaccess +++ b/.htaccess @@ -16,3 +16,8 @@ RewriteRule ^(.+?)/?$ $1.php [L] # 2) Optional: strip trailing slash for non-directories (keeps .php links working) RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)/$ $1 [R=301,L] + +# 3) Redirect short links to redirect.php +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^([a-zA-Z0-9]+)$ redirect.php?code=$1 [L] \ No newline at end of file diff --git a/assets/css/custom.css b/assets/css/custom.css new file mode 100644 index 0000000..09e65e4 --- /dev/null +++ b/assets/css/custom.css @@ -0,0 +1,38 @@ +/* Light Mode */ +:root { + --bg-color: #FFFFFF; + --surface-color: #F8F9FA; + --text-color: #212529; + --primary-color: #0D6EFD; +} + +/* Dark Mode */ +[data-bs-theme="dark"] { + --bg-color: #121212; + --surface-color: #1E1E1E; + --text-color: #E0E0E0; + --primary-color: #589BFF; +} + +body { + background-color: var(--bg-color); + color: var(--text-color); + font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; +} + +.hero { + background-color: var(--surface-color); +} + +.btn-primary { + background-color: var(--primary-color); + border-color: var(--primary-color); +} + +#result { + background-color: var(--surface-color); + border-radius: .25rem; + padding: 1rem; + margin-top: 1rem; + display: none; +} diff --git a/assets/js/main.js b/assets/js/main.js new file mode 100644 index 0000000..af8c2c2 --- /dev/null +++ b/assets/js/main.js @@ -0,0 +1,32 @@ +document.addEventListener('DOMContentLoaded', function () { + const shortenForm = document.getElementById('shortenForm'); + if (shortenForm) { + shortenForm.addEventListener('submit', function (e) { + e.preventDefault(); + const url = document.getElementById('url').value; + fetch('shorten.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: 'url=' + encodeURIComponent(url), + }) + .then(response => response.json()) + .then(data => { + const resultDiv = document.getElementById('result'); + const shortUrlLink = document.getElementById('shortUrlLink'); + if (data.short_url) { + shortUrlLink.href = data.short_url; + shortUrlLink.textContent = data.short_url; + resultDiv.style.display = 'block'; + } else { + alert(data.error || 'An error occurred.'); + } + }) + .catch(error => { + console.error('Error:', error); + alert('An error occurred.'); + }); + }); + } +}); diff --git a/db/config.php b/db/config.php index 60fff7f..17db9b9 100644 --- a/db/config.php +++ b/db/config.php @@ -15,3 +15,15 @@ function db() { } return $pdo; } + +function run_migrations() { + $pdo = db(); + $migrations_dir = __DIR__ . '/migrations'; + $files = glob($migrations_dir . '/*.sql'); + foreach ($files as $file) { + $sql = file_get_contents($file); + $pdo->exec($sql); + } +} + +run_migrations(); \ No newline at end of file diff --git a/db/migrations/001_create_links_table.sql b/db/migrations/001_create_links_table.sql new file mode 100644 index 0000000..3cf3048 --- /dev/null +++ b/db/migrations/001_create_links_table.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS links ( + id INT AUTO_INCREMENT PRIMARY KEY, + short_code VARCHAR(255) NOT NULL UNIQUE, + original_url TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); diff --git a/index.php b/index.php index 7205f3d..bfeb4f2 100644 --- a/index.php +++ b/index.php @@ -4,6 +4,8 @@ declare(strict_types=1); @error_reporting(E_ALL); @date_default_timezone_set('UTC'); +require_once 'db/config.php'; + $phpVersion = PHP_VERSION; $now = date('Y-m-d H:i:s'); ?> @@ -12,139 +14,53 @@ $now = date('Y-m-d H:i:s'); - New Style - - - - - - - - - - - - - - - + helloqwe + + + + + + + + + - + -
-
-

Analyzing your requirements and generating your website…

-
- Loading… + + + +
+
+

Create Short & Powerful Links

+

A simple and powerful tool to shorten your long URLs.

+
+
+
-

AI is collecting your requirements and applying the first changes.

-

This page will update automatically as the plan is implemented.

-

Runtime: PHP — UTC

+
+ +
+
+ -
- +
+
+ + + + + - + \ No newline at end of file diff --git a/redirect.php b/redirect.php new file mode 100644 index 0000000..4bf813b --- /dev/null +++ b/redirect.php @@ -0,0 +1,22 @@ +prepare("SELECT original_url FROM links WHERE short_code = ?"); +$stmt->execute([$short_code]); +$link = $stmt->fetch(); + +if ($link) { + header("Location: " . $link['original_url']); + exit; +} else { + header("Location: /"); + exit; +} diff --git a/shorten.php b/shorten.php new file mode 100644 index 0000000..3688810 --- /dev/null +++ b/shorten.php @@ -0,0 +1,38 @@ + 'Please provide a valid URL.']); + exit; +} + +function generateShortCode($length = 6) { + return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, $length); +} + +$short_code = generateShortCode(); + +// Ensure the short code is unique +$pdoconnection = db(); +$stmt = $pdoconnection->prepare("SELECT id FROM links WHERE short_code = ?"); +$stmt->execute([$short_code]); + +while ($stmt->fetch()) { + $short_code = generateShortCode(); + $stmt->execute([$short_code]); +} + +$stmt = $pdoconnection->prepare("INSERT INTO links (original_url, short_code) VALUES (?, ?)"); + +if ($stmt->execute([$url, $short_code])) { + $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http"; + $host = $_SERVER['HTTP_HOST']; + $short_url = "$protocol://$host/$short_code"; + echo json_encode(['short_url' => $short_url]); +} else { + echo json_encode(['error' => 'Could not create short link.']); +}