mirror of
https://github.com/gamaio/lobli.git
synced 2025-08-13 10:18:45 +00:00
Redis-only version of lob.li - Almost complete.
This version removes all the mysql stuff and uses a redis db for everything.
This commit is contained in:
@@ -13,41 +13,56 @@
|
||||
5 - Successful lob.li link resolve
|
||||
6 - Successful lookup of non-lob.li link
|
||||
7 - Unsuccessful lookup of non-lob.li link
|
||||
8 - Lookup of link Stats (returns 8 $sep JSONarray)
|
||||
*/
|
||||
|
||||
function shorten($sdb, $link, $seperator){
|
||||
$sql = "SELECT * FROM `links` WHERE `link` = '$link' LIMIT 1;";
|
||||
if($result = $sdb->query($sql)){
|
||||
if($row = $result->fetch_assoc()){
|
||||
$short = $row['shortlink'];
|
||||
return "1$seperator$short";
|
||||
}
|
||||
}
|
||||
if(checkRemoteFile($link) !== true) return "2$seperator$link";
|
||||
$title = getRemoteTitle($link);
|
||||
|
||||
$short = substr(number_format(time() * mt_rand(),0,'',''),0,5);
|
||||
$short = base_convert($short, 10, 36);
|
||||
|
||||
$dpass = substr(number_format(time() * mt_rand(),0,'',''),0,10);
|
||||
$dpass = base_convert($short.$dpass, 10, 36);
|
||||
function shorten($redis, $link, $linkage, $seperator){
|
||||
$short = $redis->get("links:id:$link");
|
||||
if($short){
|
||||
$title = $redis->get("links:title:$link");
|
||||
return "1$seperator$link$seperator$title";
|
||||
}else{
|
||||
do {
|
||||
if(checkRemoteFile($link) !== true) return "2$seperator$link";
|
||||
$title = getRemoteTitle($url);
|
||||
|
||||
$sql = "INSERT INTO `links` (link, shortlink, title, dpass, ddate) VALUES ('$link', '$short', '$title', '$dpass', NOW())";
|
||||
$short = substr(number_format(time() * mt_rand(),0,'',''),0,5);
|
||||
$short = base_convert($short, 10, 36);
|
||||
|
||||
if($result = $sdb->query($sql)): return "0$seperator$short$seperator$title";
|
||||
else: return '3'.$seperator.$sdb->error;
|
||||
endif;
|
||||
if(!$redis->exists("links:id:$short")) {
|
||||
break;
|
||||
}
|
||||
} while (1);
|
||||
|
||||
$xTime = 3136320000; // About 100 years, give or take
|
||||
|
||||
// Delete the links in 24 hours, 1 week, 1 month respectevly
|
||||
if($linkage == '0') $xTime = 86400;
|
||||
if($linkage == '1') $xTime = 604800;
|
||||
if($linkage == '2') $xTime = 2628000;
|
||||
|
||||
$redis->setex("links:id:$short", $xTime, $link);
|
||||
$redis->setex("links:title:$short", $xTime, $title);
|
||||
$redis->setex("links:date:$short", $xTime, date("d/m/Y", strtotime($str)));
|
||||
$redis->setex("tracking:clicks:$link", $xTime, 1);
|
||||
|
||||
return "0$seperator$short$seperator$title";
|
||||
}
|
||||
}
|
||||
|
||||
function stats($sdb, $seperator){
|
||||
|
||||
function stats($redis, $seperator){
|
||||
$tracking = $redis->keys("tracking:clicks:*");
|
||||
$tracking = rsort($tracking);
|
||||
$tracking = array_slice($tracking, 0, 5, true);
|
||||
return "8$seperator".json_encode($tracking);
|
||||
}
|
||||
|
||||
function getRemoteTitle($url){
|
||||
$url = parse_url($url);
|
||||
$tags = get_meta_tags($url['scheme'].'://'.$url['host']);
|
||||
$ret = sanitize($tags['description']);
|
||||
return $ret;
|
||||
if($tags = get_meta_tags($url['scheme'].'://'.$url['host'])){
|
||||
$ret = $tags['description'];
|
||||
return $ret;
|
||||
}else{ return false; }
|
||||
}
|
||||
|
||||
function checkRemoteFile($ip=null){
|
||||
@@ -77,10 +92,11 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
function sanitize($input){
|
||||
if ($input == null) die("4");
|
||||
function sanitize($input, $seperator){
|
||||
if ($input == null) die("4$seperator");
|
||||
$output = strip_tags($input);
|
||||
$output = stripslashes($output);
|
||||
//filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING)
|
||||
$output = mysql_real_escape_string($output);
|
||||
return $output;
|
||||
}
|
||||
|
Reference in New Issue
Block a user