From 8b6854764459d9025c140c346a58a428634da33d Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Fri, 9 Jun 2023 20:03:30 -0500 Subject: [PATCH] Added code to generate a random GLink if the user doesn't submit one --- result.php | 86 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 68 insertions(+), 18 deletions(-) diff --git a/result.php b/result.php index 3ed85d0..25f7fb0 100644 --- a/result.php +++ b/result.php @@ -1,8 +1,36 @@ build(); +$cluster = Cassandra::cluster()->withPersistentSessions(true)->build(); $keyspace = 'glink'; $url = $_GET["url"]; @@ -13,36 +41,58 @@ if (($matches == 0) || ($matches == false)) { } $shortlink = $_GET["glink"]; -$matches_shortlink = preg_match('/^[a-zA-Z]+$/',$shortlink); -if (($matches_shortlink == 0) || ($matches_shortlink == false)) { - printf("The GLink entered was invalid. The GLink can only contain letters. Please try again."); - return; -} +if ($shortlink != '') { + $matches_shortlink = preg_match('/^[a-zA-Z]+$/',$shortlink); + if (($matches_shortlink == 0) || ($matches_shortlink == false)) { + printf("The GLink entered was invalid. The GLink can only contain letters. Please try again."); + return; + } +} else { + /* generate a random shortlink */ + gen_shortlink: + $rand_string = gen_rand_shortlink(6); /* the function is defined at the start of this file */ + $shortlink = $rand_string; + + /* Check if shortlink is already taken by querying the database */ + $statement = $session->prepare('SELECT url FROM data WHERE id=?'); + $result = $session->execute($statement,array('arguments' => array($shortlink))); + + if ($result->count() != 0) { + goto gen_shortlink; + } + +} $session = $cluster->connect($keyspace); //$statement = new Cassandra\SimpleStatement('SELECT name FROM data WHERE id=5'); + +$statement = $session->prepare('SELECT url FROM data WHERE shortlink=? ALLOW FILTERING'); +$options = array('arguments' => array($shortlink)); +$result = $session->execute($statement,$options); + +if ($result->count() != 0) { + printf('That GLink is already taken. Please try another one.'); + exit; +} + $rand_num = rand(0,99999999); -$statement = $session->prepare('INSERT INTO data (id, url, shortlink, when_created) VALUES (?,?,?,toTimestamp(now()))'); +$statement = $session->prepare('INSERT INTO data (id, url, shortlink, when_created) VALUES (?,?,?,toTimestamp(now())) USING TTL 20'); $result = $session->execute($statement,array('arguments' => array($rand_num,$url,$shortlink))); -$statement = $session->prepare('SELECT url,shortlink FROM data WHERE id=?'); -$options = array('arguments' => array($rand_num)); -$result = $session->execute($statement,$options); - //$stringRepresentation= json_encode($result[0]); //printf("%s\n\n\n",$stringRepresentation); -foreach($result as $row) { - if (is_null($row)) { - printf('Unsuccessful'); - } else { - printf('Successful: The URL you entered was: %s and your GLink is: https://glink.zip/%s', $row['url'],$row['shortlink']); - } -} +//foreach($result as $row) { +// if (is_null($row)) { +// printf('Unsuccessful'); +// } else { + printf('Successful: The URL you entered was: %s and your GLink is: https://glink.zip/%s', $url,$shortlink); +// } +//} //printf('Done'); ?>