2014-07-04 21:30:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require('Include/PHP/db.php');
|
|
|
|
|
|
|
|
// Returns will be in the structure: response code seperator extra data and will be formatted client side
|
|
|
|
/*
|
|
|
|
Response codes:
|
|
|
|
0 - Successful shorten
|
|
|
|
1 - Existing link found
|
|
|
|
2 - Dead link
|
|
|
|
3 - Database Error
|
|
|
|
4 - Sanitize failed
|
|
|
|
5 - Successful lob.li link resolve
|
|
|
|
6 - Successful lookup of non-lob.li link
|
|
|
|
7 - Unsuccessful lookup of non-lob.li link
|
2014-07-25 21:41:51 +00:00
|
|
|
8 - Lookup of link Stats (returns 8 $sep JSONarray)
|
2014-07-04 21:30:48 +00:00
|
|
|
*/
|
|
|
|
|
2014-07-25 21:41:51 +00:00
|
|
|
function shorten($redis, $link, $linkage, $seperator){
|
2014-08-11 00:37:34 +00:00
|
|
|
$short = $redis->get("llinks:$link");
|
2014-07-25 21:41:51 +00:00
|
|
|
if($short){
|
2014-08-11 00:37:34 +00:00
|
|
|
$sId = $short;
|
|
|
|
$short = $redis->lrange("links:$short", 0, 1);
|
|
|
|
$title = $short[1];
|
|
|
|
return "1$seperator$sId$seperator$title";
|
2014-07-25 21:41:51 +00:00
|
|
|
}else{
|
|
|
|
do {
|
|
|
|
if(checkRemoteFile($link) !== true) return "2$seperator$link";
|
2014-08-11 00:37:34 +00:00
|
|
|
//if(($title = getRemoteTitle($url)) !== true) return "2$seperator$link";
|
|
|
|
$title = "google";
|
2014-07-25 21:41:51 +00:00
|
|
|
|
|
|
|
$short = substr(number_format(time() * mt_rand(),0,'',''),0,5);
|
|
|
|
$short = base_convert($short, 10, 36);
|
|
|
|
|
2014-08-11 00:37:34 +00:00
|
|
|
if(!$redis->exists("links:$short")) {
|
2014-07-25 21:41:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
2014-08-09 23:35:09 +00:00
|
|
|
$now = time(NULL);
|
2014-07-25 21:41:51 +00:00
|
|
|
$xTime = 3136320000; // About 100 years, give or take
|
2014-07-04 21:30:48 +00:00
|
|
|
|
2014-07-25 21:41:51 +00:00
|
|
|
// 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;
|
|
|
|
|
2014-08-11 00:37:34 +00:00
|
|
|
$redis->set("llinks:$link", $short);
|
2014-07-25 21:48:46 +00:00
|
|
|
$redis->rpush("links:$short", $link);
|
|
|
|
$redis->rpush("links:$short", $title);
|
|
|
|
$redis->rpush("links:$short", date("d/m/Y", strtotime($str)));
|
2014-08-09 23:35:09 +00:00
|
|
|
$redis->expireAt("links:$short", $now+$xTime);
|
2014-08-11 00:37:34 +00:00
|
|
|
$redis->expireAt("llinks:$link", $now+$xTime);
|
2014-07-25 21:48:46 +00:00
|
|
|
|
2014-08-10 22:29:17 +00:00
|
|
|
$redis->zAdd("tracking:clicks", 1, $link);
|
2014-07-25 21:41:51 +00:00
|
|
|
|
|
|
|
return "0$seperator$short$seperator$title";
|
|
|
|
}
|
|
|
|
}
|
2014-07-04 21:30:48 +00:00
|
|
|
|
|
|
|
function getRemoteTitle($url){
|
|
|
|
$url = parse_url($url);
|
2014-07-25 21:41:51 +00:00
|
|
|
if($tags = get_meta_tags($url['scheme'].'://'.$url['host'])){
|
|
|
|
$ret = $tags['description'];
|
|
|
|
return $ret;
|
|
|
|
}else{ return false; }
|
2014-07-04 21:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkRemoteFile($ip=null){
|
|
|
|
if($ip==null) return false;
|
|
|
|
|
|
|
|
// Setup the connection and only get the headers
|
|
|
|
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
|
|
|
|
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
|
|
|
|
$header[] = "Cache-Control: max-age=0";
|
|
|
|
$header[] = "Connection: keep-alive";
|
|
|
|
$header[] = "Keep-Alive: 300";
|
|
|
|
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
|
|
|
|
$header[] = "Accept-Language: en-us,en;q=0.5";
|
|
|
|
$header[] = "Pragma: ";
|
|
|
|
|
|
|
|
$curlInit = curl_init($ip);
|
|
|
|
curl_setopt($curlInit, CURLOPT_CONNECTTIMEOUT, 10);
|
|
|
|
curl_setopt($curlInit, CURLOPT_HEADER, true);
|
|
|
|
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, true);
|
2014-07-22 19:54:40 +00:00
|
|
|
curl_setopt($curlInit, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; UnPS-GAMATechnologies (UnPS WebQuery/4-2.9; +http://lob.li))');
|
2014-07-04 21:30:48 +00:00
|
|
|
curl_setopt($curlInit, CURLOPT_HTTPHEADER, $header);
|
|
|
|
|
|
|
|
$response = curl_exec($curlInit);
|
|
|
|
curl_close($curlInit);
|
|
|
|
|
|
|
|
if($response) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:41:51 +00:00
|
|
|
function sanitize($input, $seperator){
|
|
|
|
if ($input == null) die("4$seperator");
|
2014-07-04 21:30:48 +00:00
|
|
|
$output = strip_tags($input);
|
|
|
|
$output = stripslashes($output);
|
2014-07-25 21:41:51 +00:00
|
|
|
//filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING)
|
2014-07-04 21:30:48 +00:00
|
|
|
$output = mysql_real_escape_string($output);
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2014-07-22 19:54:40 +00:00
|
|
|
?>
|