diff --git a/.gitignore b/.gitignore index 496ee2c..2020c40 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.DS_Store \ No newline at end of file +.DS_Store +.idea +config.php \ No newline at end of file diff --git a/README.md b/README.md index bc0d95b..cd49760 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ Requires PHP ≥ 5.4.0 or higher. 1. Download the source code as located within this repository, and upload it to your web server. 2. Use `database.sql` to create the `redirect` table in a database of choice. (Do *not* delete the `INSERT` statement on [line 10](https://github.com/mathiasbynens/php-url-shortener/blob/f64ee342246fa5bf0340641372680a2d398afc79/database.sql#L10) as it is needed to initialize the database.) -3. Edit `config.php` and enter your database credentials. -4. For additional *security through obscurity™*, consider renaming `shorten.php` to a secret file name of your choosing and tweaking the `.htaccess` file ([line 3](https://github.com/mathiasbynens/php-url-shortener/blob/f64ee342246fa5bf0340641372680a2d398afc79/.htaccess#L3)) accordingly. +3. Rename `config-example.php` to `config.php`. +4. Edit `config.php` and enter your database credentials. +5. For additional *security through obscurity™*, consider renaming `shorten.php` to a secret file name of your choosing and tweaking the `.htaccess` file ([line 3](https://github.com/mathiasbynens/php-url-shortener/blob/f64ee342246fa5bf0340641372680a2d398afc79/.htaccess#L3)) accordingly. ## Features @@ -49,4 +50,5 @@ This script is available under the MIT license. * [Peter Beverloo](http://peter.sh/) * [Tomislav Biscan](https://github.com/B-Scan) +* [Medard Mandane](https://github.com/medardm/) diff --git a/config.php b/config-example.php similarity index 62% rename from config.php rename to config-example.php index 997257e..9e6168c 100644 --- a/config.php +++ b/config-example.php @@ -1,12 +1,11 @@ diff --git a/index.php b/index.php index f26979a..a678857 100644 --- a/index.php +++ b/index.php @@ -1,46 +1,53 @@ 8) { - $url = 'https://twitter.com/' . TWITTER_USERNAME . '/status/' . $slug; - } else { - - $db = new MySQLi(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE); - $db->set_charset('utf8mb4'); - - $escapedSlug = $db->real_escape_string($slug); - $redirectResult = $db->query('SELECT url FROM redirect WHERE slug = "' . $escapedSlug . '"'); - - if ($redirectResult && $redirectResult->num_rows > 0) { - $db->query('UPDATE redirect SET hits = hits + 1 WHERE slug = "' . $escapedSlug . '"'); - $url = $redirectResult->fetch_object()->url; - } else { - $url = DEFAULT_URL . $_SERVER['REQUEST_URI']; - } - - $db->close(); - - } - } + $slug = $_GET['slug']; + + if ('@' == $slug) { + $url = 'https://twitter.com/' . TWITTER_USERNAME; + } else { + if (' ' == $slug) { + // + + $url = 'https://plus.google.com/u/0/' . GOOGLE_PLUS_ID . '/posts'; + } else { + $slug = preg_replace('/[^a-z0-9]/si', '', $slug); + + if (is_numeric($slug) && strlen($slug) > 8) { + $url = 'https://twitter.com/' . TWITTER_USERNAME . '/status/' . $slug; + } else { + $db = new MySQLi(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT); + $db->set_charset('utf8mb4'); + + $escapedSlug = $db->real_escape_string($slug); + $redirectResult = $db->query('SELECT url FROM redirect WHERE slug = "' . $escapedSlug . '"'); + + if ($redirectResult && $redirectResult->num_rows > 0) { + $db->query('UPDATE redirect SET hits = hits + 1 WHERE slug = "' . $escapedSlug . '"'); + $url = $redirectResult->fetch_object()->url; + } else { + $url = DEFAULT_URL . $_SERVER['REQUEST_URI']; + } + + $db->close(); + } + } + } } header('Location: ' . $url, null, 301); $attributeValue = htmlspecialchars($url); ?> -Continue +Continue + diff --git a/shorten.php b/shorten.php index 1944845..634b7a7 100644 --- a/shorten.php +++ b/shorten.php @@ -1,55 +1,70 @@ set_charset('utf8mb4'); $url = $db->real_escape_string($url); $result = $db->query('SELECT slug FROM redirect WHERE url = "' . $url . '" LIMIT 1'); +if (!empty($customSlug)) { + $resultSlug = $db->query('SELECT slug FROM redirect WHERE slug = "' . $customSlug . '" LIMIT 1'); + + if ($resultSlug && $resultSlug->num_rows > 0) { // If there’s already a short URL for this URL + die(SHORT_URL . $result->fetch_object()->slug); + } +} if ($result && $result->num_rows > 0) { // If there’s already a short URL for this URL - die(SHORT_URL . $result->fetch_object()->slug); + die(SHORT_URL . $result->fetch_object()->slug); } else { - $result = $db->query('SELECT slug, url FROM redirect ORDER BY date DESC, slug DESC LIMIT 1'); - if ($result && $result->num_rows > 0) { - $slug = getNextShortURL($result->fetch_object()->slug); - if ($db->query('INSERT INTO redirect (slug, url, date, hits) VALUES ("' . $slug . '", "' . $url . '", NOW(), 0)')) { - header('HTTP/1.1 201 Created'); - echo SHORT_URL . $slug; - $db->query('OPTIMIZE TABLE `redirect`'); - } - } + $result = $db->query('SELECT slug, url FROM redirect ORDER BY date DESC, slug DESC LIMIT 1'); + if ($result && $result->num_rows > 0) { + $slug = !empty($customSlug) ? $customSlug : getNextShortURL($result->fetch_object()->slug); + + if ($db->query('INSERT INTO redirect (slug, url, date, hits) VALUES ("' . $slug . '", "' . $url . '", NOW(), 0)')) { + header('HTTP/1.1 201 Created'); + echo SHORT_URL . $slug; + $db->query('OPTIMIZE TABLE `redirect`'); + } + } } ?> \ No newline at end of file