From 92babb43a2a0041a71b54db35cbc9d2fba908a63 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 27 Nov 2014 02:41:21 +0100 Subject: Rewrote the linkshorter. --- linkshorter/class/db.php | 73 +++++++++++++++++ linkshorter/config.php | 7 ++ linkshorter/css/style.css | 68 ++++++++++++++++ linkshorter/db.php | 16 ---- linkshorter/error.php | 13 --- linkshorter/functions.php | 71 +++++------------ linkshorter/goto.php | 29 +++++-- linkshorter/header.php | 18 ----- linkshorter/index.php | 155 ++++++++++++++---------------------- linkshorter/insert.php | 36 --------- linkshorter/nginx.conf | 5 ++ linkshorter/style.css | 21 ----- linkshorter/view/templ-head.php | 15 ++++ linkshorter/view/templ-index.php | 55 +++++++++++++ linkshorter/view/templ-output.php | 12 +++ linkshorter/view/templ-password.php | 35 ++++++++ 16 files changed, 372 insertions(+), 257 deletions(-) create mode 100644 linkshorter/class/db.php create mode 100644 linkshorter/config.php create mode 100644 linkshorter/css/style.css delete mode 100644 linkshorter/db.php delete mode 100644 linkshorter/error.php delete mode 100644 linkshorter/header.php delete mode 100644 linkshorter/insert.php create mode 100644 linkshorter/nginx.conf delete mode 100644 linkshorter/style.css create mode 100644 linkshorter/view/templ-head.php create mode 100644 linkshorter/view/templ-index.php create mode 100644 linkshorter/view/templ-output.php create mode 100644 linkshorter/view/templ-password.php diff --git a/linkshorter/class/db.php b/linkshorter/class/db.php new file mode 100644 index 0000000..7713487 --- /dev/null +++ b/linkshorter/class/db.php @@ -0,0 +1,73 @@ +db = new Redis(); + + if ( ! $connect || ! $db ) + return; + + $this->connect($connect, $db); + } + + public function connect($connect, $db){ + try { + $this->db->connect($connect); + } catch (Exception $e){ + $this->error = $e; + return false; + } + + try { + $this->db->select($db); + } catch (Exception $e){ + $this->error = $e; + return false; + } + + try { + $this->db->ping(); + } catch (Exception $e){ + $this->error = $e; + return false; + } + + return true; + } + + public function expire($key, $ttl){ + try { + $this->db->setTimeout($key, $ttl); + } catch (Exception $e){ + $this->error = $e; + return false; + } + } + + public function set($key, $value, $ttl = null){ + if ( is_null($ttl) ) + return $this->db->set($key, $value); + else + return $this->db->set($key, $value, (int)$ttl); + } + + public function get($key){ + return $this->db->get($key); + } + + public function exists($key){ + return $this->db->exists($key); + } + + public function __destruct(){ + try { + $this->db->close(); + } catch (Exception $e){ + return false; + } + } +} diff --git a/linkshorter/config.php b/linkshorter/config.php new file mode 100644 index 0000000..4b38311 --- /dev/null +++ b/linkshorter/config.php @@ -0,0 +1,7 @@ + li > a { + color: white; +} + +.noscript { + background-color: #dd5148; + color: white; +} + +/* footer */ + +.footer { + background-color: #3083D6 ; + border-color: #3083D6 ; + background: #3083D6 ; + color: white ; + position: absolute; + bottom: 0; + width: 100%; +} + +.footer-a { + color: white; +} + +.footer-a:hover { + color: white; + text-decoration: underline; +} + +.underline { + text-decoration: underline; +} + +.actives { + color: white !important; + text-decoration: underline; + font-weight: bold; +} diff --git a/linkshorter/db.php b/linkshorter/db.php deleted file mode 100644 index 4dcf58a..0000000 --- a/linkshorter/db.php +++ /dev/null @@ -1,16 +0,0 @@ -connect('/var/run/redis/redis.sock'); - -try { - $db->ping(); -} catch (Exception $e){ - do_output("

No connection to the database established.

", "500 Server Failure", false, "

Redis went away

"); -} - -try { - $db->select(1); -} catch (Exception $e){ - do_output("

No connection to the database established.

", "500 Server Failure", false, "

Redis went away

"); -} diff --git a/linkshorter/error.php b/linkshorter/error.php deleted file mode 100644 index 92ba713..0000000 --- a/linkshorter/error.php +++ /dev/null @@ -1,13 +0,0 @@ -The requested url ( ".htmlentities($_SERVER['REQUEST_URI'])." ) wasn't found on this server.

", "404 Not Found", false, "

400 - Not Found

"); - break; - - default: - do_output("

There was a failure and your request can't be proceeded.

", "500 Error", false, "

Error!

"); -} diff --git a/linkshorter/functions.php b/linkshorter/functions.php index 7fa8cff..720fb4d 100644 --- a/linkshorter/functions.php +++ b/linkshorter/functions.php @@ -1,59 +1,32 @@ - - - - - Link Shorter - - - - - - - - -
-
- -
-
-
- -
-
- - -[^\S ]+/s', // strip whitespaces after tags, except space - '/[^\S ]+\', - '<', - '\\1' - ); + do { + $arr = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); - $buffer = preg_replace($search, $replace, $buffer); + $short=""; + for ($i=0;$i<5;$i++){ + $r = mt_rand(0, count($arr)-1); + $short.=$arr[$r]; + } + } while( $db->exists($short) ); - return $buffer; + return $short; +} + +function redirect($goto){ + header($_SERVER["SERVER_PROTOCOL"] . " 301 Moved Permanently"); + header("Location: " . $goto); + exit; } diff --git a/linkshorter/goto.php b/linkshorter/goto.php index d77e1e2..f42705f 100644 --- a/linkshorter/goto.php +++ b/linkshorter/goto.php @@ -1,13 +1,26 @@ get($_GET["goto"]); -if( ! $url || $url == "" ){ - do_output("

This url wasn't found on this server.

", "404 Not Found", false, "

404 - Not found

"); +if ( ! isset($_REQUEST["short"]) || $_REQUEST["short"] == "" ){ + header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found"); + _do_output("Failure!", "Requested ID not found."); } -header($_SERVER['SERVER_PROTOCOL'] . " 301 Moved Permanently"); -header("Location: ".$url); -exit; +$db = new Database(REDIS_CONNECT, REDIS_SELECT); + +if ( ! $db->exists($_REQUEST["short"])){ + + require 'view/templ-notfound.php'; + +} else { + $options = json_decode( $db->get($_REQUEST["short"]), true ); + + if ( $options["password"] == "" ){ + redirect($options["url"]); + } else { + require 'view/templ-password.php'; + } +} diff --git a/linkshorter/header.php b/linkshorter/header.php deleted file mode 100644 index c207321..0000000 --- a/linkshorter/header.php +++ /dev/null @@ -1,18 +0,0 @@ - diff --git a/linkshorter/index.php b/linkshorter/index.php index 1d56be5..c60bc82 100644 --- a/linkshorter/index.php +++ b/linkshorter/index.php @@ -1,114 +1,77 @@ exists($key) ) { - header("X-Cache: Hit"); - echo $db->get($key); - ob_end_flush(); - exit; + else { + + if ( ! isset($_REQUEST["short"]) || $_REQUEST["short"] == "" ){ + header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found"); + _do_output("Failure!", "Requested ID not found."); + } + + $db = new Database(REDIS_CONNECT, REDIS_SELECT); + + $options = json_decode( $db->get($_REQUEST["short"]), true ); + + if ( password_verify( $_REQUEST["password"] . PEPPER, $options["password"] ) ) + redirect($options["url"]); + else + _do_output("Failure!", "Wrong password supplied"); } -*/ - -?> - - - - - Link Shorter - - - - - - - - -
-
-
-
-
- -

Amazing Linkshorter

-

Short your link and use a easy to remembery query string

-
- -
- -
- - -
-
- -
- -
- - -
-
- -
- -
- -
-
- -
-
-
-
-
- - -set($key, $html, 3600); - ob_end_flush(); } else { - if ( empty($_POST["url"]) || $_POST["url"] == "" ){ - do_output("

We need a link to be shortened.

", "400 Client Failed", false, "

Missing URL

"); - } + $url = trim($_REQUEST['url']); - if ( ! preg_match("/^[a-z]+:\/\/[a-z0-9_]+/i", $_POST["url"]) ){ - do_output("

Only schemas like http:// or ftp:// are supported.

", "400 Client Failed", false, "

This does not look like an url

"); + if( ! preg_match("/^https?:\/\//", $url) ){ + $heading = "Failure!"; + $reason = "This doesn't look like a valid URL."; + _do_output($heading, $reason); } + $options = array("url" => $url); + if ( ! isset($_REQUEST["short"]) || $_REQUEST["short"] == "" ) + $options["short"] = ""; + else + $options["short"] = $_REQUEST["short"]; + + if ( ! isset($_REQUEST["ttl"]) || $_REQUEST["ttl"] == "" ) + $options["ttl"] = ""; + else { + if ( ! preg_match( "/^[0-9]+$/", trim($_REQUEST["ttl"]) ) ){ + _do_output("Failure", "Your Lifetime doesn't look like a valid number."); + } + $options["ttl"] = $_REQUEST["ttl"]; + } - $hash = md5($_POST["url"]); - if( ! empty($_POST["short"]) && $_POST["short"] != "" ) { - $short = $_POST["short"]; - if ( $db->exists($short) == 1 && $_POST["url"] != $db->get($short) ) - do_output("

Someone else has already a registered entry under '".htmlentities($short)."'.

", "422 Unprocessable Entity", false, "

Query string already exists.

"); + if ( ! isset($_REQUEST["password"]) || $_REQUEST["password"] == "" ) + $options["password"] = ""; + else + $options["password"] = password_hash($_REQUEST["password"] . PEPPER, PASSWORD_DEFAULT); + + $db = new Database(REDIS_CONNECT, REDIS_SELECT); + + if ( $options["short"] != "" && $db->exists($options["short"]) ) + _do_output("Failure", "Query string '".htmlentities($options["short"])."' already taken. Please choose a different one."); + + if ( $options["short"] == "" ) + $options["short"] = getToken(); + + if ( $options["ttl"] != "" ){ + if ( ! $db->set($options["short"], json_encode($options), $options["ttl"]) ){ + _do_output("Failure", "Database went away. :("); + } } else { - if( ! $short = $db->get($hash) ){ - $arr = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); - - do { - $short=""; - for ($i=0;$i<5;$i++){ - $r = mt_rand(0, count($arr)-1); - $short.=$arr[$r]; - } - } while ( $db->exists($short) ); - $db->set($hash, $short); + if ( ! $db->set($options["short"], json_encode($options)) ){ + _do_output("Failure", "Database went away. :("); } } - $db->set($short, $_POST["url"]); - do_output("

Your short link for ".htmlentities($_POST["url"])." is
http://s.moehm.org/".$short."

", "200 OK", false, "

Success

"); + _do_output("Success!", "Your shortlink is " . SHORTDOMAIN . htmlentities($options["short"]) . "."); } diff --git a/linkshorter/insert.php b/linkshorter/insert.php deleted file mode 100644 index 94d01a3..0000000 --- a/linkshorter/insert.php +++ /dev/null @@ -1,36 +0,0 @@ -We need a link to be shortened.

", false, "400 Client Failed", "

Missing URL

"); -} - -if ( ! preg_match("/^[a-z]+:\/\/[a-z0-9_]+/i", $_POST["url"]) ){ - failure("

Only schemas like http:// or ftp:// are supported.

", false, "400 Client Failed", "

This does not look like an url

"); -} - -require 'db.php'; - -$hash = md5($_POST["url"]); -if( ! $short = $db->get($hash)){ - - $arr = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); - - $short=""; - for ($i=0;$i<5;$i++){ - $r = mt_rand(0, count($arr)-1); - $short.=$arr[$r]; - } - - $db->set($short, $_POST["url"]); - $db->set($hash, $short);; -} - -failure("

Your short link for ".htmlentities($_POST["url"])." is
http://".$_SERVER["HTTP_HOST"]."/-".$short."

", false, "200 OK", "

Success

"); diff --git a/linkshorter/nginx.conf b/linkshorter/nginx.conf new file mode 100644 index 0000000..a4af9cb --- /dev/null +++ b/linkshorter/nginx.conf @@ -0,0 +1,5 @@ +location = / {} + +location / { + rewrite ^/(.*) /goto.php?short=$1 last; +} diff --git a/linkshorter/style.css b/linkshorter/style.css deleted file mode 100644 index c762039..0000000 --- a/linkshorter/style.css +++ /dev/null @@ -1,21 +0,0 @@ -html { - position: relative; - min-height: 100%; -} - -body { - margin-bottom: 60px; -} - -/* footer */ - -.footer { - position: absolute; - bottom: 0; - width: 100%; -} - -#copyright-text { - text-decoration: underline; - color: #333; -} diff --git a/linkshorter/view/templ-head.php b/linkshorter/view/templ-head.php new file mode 100644 index 0000000..abc29bf --- /dev/null +++ b/linkshorter/view/templ-head.php @@ -0,0 +1,15 @@ + + + + + <?php echo $title; ?> + + + + + + + diff --git a/linkshorter/view/templ-index.php b/linkshorter/view/templ-index.php new file mode 100644 index 0000000..b78b5c7 --- /dev/null +++ b/linkshorter/view/templ-index.php @@ -0,0 +1,55 @@ + +
+
+
+
+
+ +

Amazing Linkshorter

Short your link and use a easy to remembery query string.

+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + +
+ +
+ +
+
+ +
+ +
+ + Choose how long the shortlink should be valid. (In seconds) +
+
+ +
+ +
+ +
+
+ +
+
+
+
+
+ diff --git a/linkshorter/view/templ-output.php b/linkshorter/view/templ-output.php new file mode 100644 index 0000000..0ca6d29 --- /dev/null +++ b/linkshorter/view/templ-output.php @@ -0,0 +1,12 @@ + +
+
+

+
+

+
+
+ diff --git a/linkshorter/view/templ-password.php b/linkshorter/view/templ-password.php new file mode 100644 index 0000000..2be90f3 --- /dev/null +++ b/linkshorter/view/templ-password.php @@ -0,0 +1,35 @@ + +
+
+
+
+ + +

Password Required

Enter the correct password and you will be redirected.

+ + +
+ +
+ +
+
+ + "> + +
+ +
+ +
+
+ +
+
+ +
+
+ -- cgit v1.2.3