From 048e187821bdd7f0e710760a29ae718f440ac18e Mon Sep 17 00:00:00 2001 From: Medard Mandane Date: Mon, 22 Jun 2020 23:44:02 +0800 Subject: [PATCH 1/4] Add config-example.php and remove from repo and gitignore config.php --- .gitignore | 4 +++- config.php => config-example.php | 9 ++++----- 2 files changed, 7 insertions(+), 6 deletions(-) rename config.php => config-example.php (62%) 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/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 @@ From cc1983827481e8566b9e4ce202431a1964132b44 Mon Sep 17 00:00:00 2001 From: Medard Mandane Date: Mon, 22 Jun 2020 23:47:28 +0800 Subject: [PATCH 2/4] Include MYSQL_PORT when connecting to database and require config-example.php if config.php doesn't exist - update README --- README.md | 6 +++-- index.php | 75 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 45 insertions(+), 36 deletions(-) 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/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 + From 4a7761ee639d76e93a006acfd8d3dbee42a48c40 Mon Sep 17 00:00:00 2001 From: Medard Mandane Date: Tue, 23 Jun 2020 00:00:47 +0800 Subject: [PATCH 3/4] =?UTF-8?q?Update=20config=C2=A0=20require=20and=20add?= =?UTF-8?q?=20MYSQL=5FPORT=20when=20connecting=20to=20database=20in=20shor?= =?UTF-8?q?ten.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shorten.php | 62 +++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/shorten.php b/shorten.php index 1944845..eeb940e 100644 --- a/shorten.php +++ b/shorten.php @@ -1,55 +1,61 @@ set_charset('utf8mb4'); $url = $db->real_escape_string($url); $result = $db->query('SELECT slug FROM redirect WHERE url = "' . $url . '" LIMIT 1'); 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 = 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 From b63d956e1547f9e6ac72149aec7881f173921472 Mon Sep 17 00:00:00 2001 From: Medard Mandane Date: Tue, 23 Jun 2020 00:27:57 +0800 Subject: [PATCH 4/4] Add ability to assign custom slug to link - assign custom slug by appending &slug=customSlug in the url (e.g. http://shortener.be/shorten?url=https://linkedin.com&slug=linkedin --- shorten.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/shorten.php b/shorten.php index eeb940e..634b7a7 100644 --- a/shorten.php +++ b/shorten.php @@ -9,6 +9,7 @@ header('Content-Type: text/plain;charset=UTF-8'); $url = isset($_GET['url']) ? urldecode(trim($_GET['url'])) : ''; +$customSlug = isset($_GET['slug']) ? trim($_GET['slug']) : ''; if (in_array($url, ['', 'about:blank', 'undefined', 'http://localhost/'])) { die('Enter a URL.'); @@ -44,12 +45,20 @@ function getNextShortURL($s) $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); } 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); + $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;