Compare commits
5 Commits
master
...
8b68547644
Author | SHA1 | Date | |
---|---|---|---|
8b68547644 | |||
0f85fcf021 | |||
1d4606db27 | |||
116bbe5218 | |||
1b52864952 |
@@ -14,7 +14,7 @@
|
|||||||
<input type="text" name="url" id="URL" value="https://example.com" required><br><br>
|
<input type="text" name="url" id="URL" value="https://example.com" required><br><br>
|
||||||
|
|
||||||
<label for="labels">GLink:</label><span class="mandatory">*</span>
|
<label for="labels">GLink:</label><span class="mandatory">*</span>
|
||||||
<label for="GLink" id="labels" class="glink">glink.zip/</label><input type="text" name="glink" id="GLink" class="glink" value="exampleWebsite" required>
|
<label for="GLink" id="labels" class="glink">glink.zip/</label><input type="text" name="glink" id="GLink" class="glink" value="exampleWebsite">
|
||||||
|
|
||||||
<span role="alert" id="error" aria-hidden="true">Invalid URL</span>
|
<span role="alert" id="error" aria-hidden="true">Invalid URL</span>
|
||||||
|
|
||||||
|
36
redirect.php
Normal file
36
redirect.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
use Cassandra;
|
||||||
|
|
||||||
|
$uri = $_SERVER['REQUEST_URI'];
|
||||||
|
$uri = substr($uri,1);
|
||||||
|
|
||||||
|
$matches_uri = preg_match('/^[a-zA-Z]+$/',$uri);
|
||||||
|
|
||||||
|
if (($matches_uri == 0) || ($matches_uri == false)) {
|
||||||
|
header("Location: http://glink.zip/");
|
||||||
|
exit;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
$cluster = Cassandra::cluster()->withPersistentSessions(true)->build();
|
||||||
|
$keyspace = 'glink';
|
||||||
|
$session = $cluster->connect($keyspace);
|
||||||
|
|
||||||
|
$statement = $session->prepare('SELECT url FROM data WHERE shortlink=? ALLOW FILTERING;');
|
||||||
|
$result = $session->execute($statement,array('arguments' => array($uri)));
|
||||||
|
|
||||||
|
if ($result->count() == 0) {
|
||||||
|
printf('The given GLink was invalid, and doesn\'t point to a specific web page.');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($result as $row) {
|
||||||
|
if (is_null($row)) {
|
||||||
|
printf('The given GLink was invalid, and doesn\'t point to a specific web page.');
|
||||||
|
exit;
|
||||||
|
} else {
|
||||||
|
header("Location: " . $row['url']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
14
result.js
14
result.js
@@ -1,14 +0,0 @@
|
|||||||
console.log('Beginning database execution');
|
|
||||||
|
|
||||||
const cassandra = require('cassandra-driver');
|
|
||||||
|
|
||||||
const client = new cassandra.Client({
|
|
||||||
contactPoints: ['127.0.0.1:9042'],
|
|
||||||
keyspace: 'glink',
|
|
||||||
});
|
|
||||||
|
|
||||||
const query = 'SELECT name FROM data WHERE id = ?';
|
|
||||||
|
|
||||||
console.log(query);
|
|
||||||
|
|
||||||
client.execute(query, [5]).then(result => console.log('User name is %s',result.rows[0].name));
|
|
96
result.php
96
result.php
@@ -1,42 +1,98 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
function gen_base62_rand_shortlink($len) {
|
||||||
|
$rand_bytes = random_bytes(intval(($len * 2) / 3));
|
||||||
|
$rand_string = base64_encode($rand_bytes);
|
||||||
|
$rand_string = str_replace("+","",$rand_string);
|
||||||
|
$rand_string = str_replace("/","",$rand_string);
|
||||||
|
$rand_string = str_replace("=","",$rand_string);
|
||||||
|
|
||||||
|
if (mb_strlen($rand_string) < $len) {
|
||||||
|
$curlen = mb_strlen($rand_string);
|
||||||
|
$rand_string = $rand_string . gen_rand_shortlink($len - $curlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rand_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function gen_rand_shortlink($len) {
|
||||||
|
$to_return = '';
|
||||||
|
$possible_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
|
while (mb_strlen($to_return) < $len) {
|
||||||
|
$to_return = $to_return . $possible_chars[rand(0, mb_strlen($possible_chars)-1)];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $to_return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
ini_set('display_errors', 1);
|
ini_set('display_errors', 1);
|
||||||
use Casssandra;
|
use Casssandra;
|
||||||
|
|
||||||
$cluster = Cassandra::cluster()->build();
|
$cluster = Cassandra::cluster()->withPersistentSessions(true)->build();
|
||||||
$keyspace = 'glink';
|
$keyspace = 'glink';
|
||||||
|
|
||||||
$url = $_GET["url"];
|
$url = $_GET["url"];
|
||||||
|
$matches = preg_match('/^http(s)*:\\/\\/[a-zA-Z0-9\\-]+(\\.[a-zA-Z0-9\\-]+)+$/',$url);
|
||||||
|
if (($matches == 0) || ($matches == false)) {
|
||||||
|
printf("The URL entered was invalid. Please try again.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$shortlink = $_GET["glink"];
|
$shortlink = $_GET["glink"];
|
||||||
|
|
||||||
|
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);
|
$session = $cluster->connect($keyspace);
|
||||||
|
|
||||||
//$statement = new Cassandra\SimpleStatement('SELECT name FROM data WHERE id=5');
|
//$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);
|
$rand_num = rand(0,99999999);
|
||||||
|
|
||||||
$values = array(
|
$statement = $session->prepare('INSERT INTO data (id, url, shortlink, when_created) VALUES (?,?,?,toTimestamp(now())) USING TTL 20');
|
||||||
'id' => $rand_num,
|
$result = $session->execute($statement,array('arguments' => array($rand_num,$url,$shortlink)));
|
||||||
'url' => $url,
|
|
||||||
'shortlink' => $shortlink,
|
|
||||||
);
|
|
||||||
$statement = new Cassandra\SimpleStatement('INSERT INTO data (id, url, shortlink, when_created) VALUES (?,?,?,toTimestamp(now()))');
|
|
||||||
$options = array('arguments' => $values);
|
|
||||||
$result = $session->execute($statement,$options);
|
|
||||||
|
|
||||||
$statement = new Cassandra\SimpleStatement('SELECT url,shortlink FROM data WHERE id=?');
|
|
||||||
$options = array('arguments' => array('id' => $rand_num));
|
|
||||||
$result = $session->execute($statement,$options);
|
|
||||||
|
|
||||||
//$stringRepresentation= json_encode($result[0]);
|
//$stringRepresentation= json_encode($result[0]);
|
||||||
|
|
||||||
//printf("%s\n\n\n",$stringRepresentation);
|
//printf("%s\n\n\n",$stringRepresentation);
|
||||||
|
|
||||||
foreach($result as $row) {
|
//foreach($result as $row) {
|
||||||
if (is_null($row)) {
|
// if (is_null($row)) {
|
||||||
printf('Unsuccessful');
|
// printf('Unsuccessful');
|
||||||
} else {
|
// } else {
|
||||||
printf('Successful: The URL you entered was: %s and your GLink is: https://glink.zip/%s', $row['url'],$row['shortlink']);
|
printf('Successful: The URL you entered was: %s and your GLink is: https://glink.zip/%s', $url,$shortlink);
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
//printf('Done');
|
//printf('Done');
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
Reference in New Issue
Block a user