UnPS-API/api.backend.php
Arctic Code 7b01ce691f Initial commit
UnPS-GAMA API first release - Not in usable production state
Version 0.0.1
2013-07-18 17:26:53 -05:00

79 lines
2.9 KiB
PHP

<?php
function checkRemoteFile($link){
if (@file_get_contents($link)): return true;
else: return false;
endif;
}
class api{
function shorten($apidb, $apikey, $sdb, $link, $dpass=null){
$apisql = "SELECT * FROM `users` WHERE `key` = '$apikey' LIMIT 1";
if(!$result = $apidb->query($apisql)) return 'ERROR: ['.$apidb->error.']';
if($row = $result->fetch_assoc()){
$canshort = $row['short'];
$name = $row['name'];
$name = addslashes($name);
$ip = '127.0.0.1';
$apisql = "INSERT INTO `apiuse` (time, name, apikey, ip, type, allowed, misc) VALUES (NOW(), '$name', '$apikey', '$ip', 'Link Shorten', '$canshort', '$link')";
if(!$result = $apidb->query($apisql)) return 'ERROR: ['.$apidb->error.']';
}
if($canshort != 1) return 'You are not authorized to shorten links';
$sql = "SELECT * FROM `links` WHERE `link` = '$link' LIMIT 1;";
if($result = $sdb->query($sql)){
if($row = $result->fetch_assoc()){
$short = $row['shortlink'];
return "Existing link: http://unps.us/?l=$short";
}
}
if(checkRemoteFile($link) !== true) return "Dead Link: $link";
$short = substr(number_format(time() * mt_rand(),0,'',''),0,10);
$short = base_convert($short, 10, 36);
$dpass = addslashes($dpass);
if($dpass != null): $sql = "INSERT INTO `links` (link, shortlink, dpass) VALUES ('$link', '$short', '$dpass')";
else: $sql = "INSERT INTO `links` (link, shortlink, dpass) VALUES ('$link', '$short', '$apikey')";
endif;
if($result = $sdb->query($sql)): return "Shortened: http://unps.us/?l=$short";
else: return 'ERROR: ['.$sdb->error.']';
endif;
}
function delShort ($apidb, $apikey, $sdb, $link, $dpass=null){
$apisql = "SELECT * FROM `users` WHERE `key` = '$apikey' LIMIT 1";
if(!$result = $apidb->query($apisql)) return 'ERROR: ['.$apidb->error.']';
if($row = $result->fetch_assoc()){
$canshort = $row['short'];
$name = $row['name'];
$name = addslashes($name);
$ip = '127.0.0.1';
$apisql = "INSERT INTO `apiuse` (time, name, apikey, ip, type, allowed, misc) VALUES (NOW(), '$name', '$apikey', '$ip', 'Short Link Delete', '$canshort', '$link')";
if(!$result = $apidb->query($apisql)) return 'ERROR: ['.$apidb->error.']';
}
if($canshort != 1) return 'You are not authorized to delete short links';
$sql = "SELECT * FROM `links` WHERE `link` = '$link' LIMIT 1;";
if($result = $sdb->query($sql)){
if($row = $result->fetch_assoc()){
$short = $row['shortlink'];
$password = $row['dpass'];
if($dpass != null) $apikey = addslashes($dpass);
if($apikey == $password){
$sql = "DELETE FROM `links` WHERE `shortlink` = '$link' AND `dpass` = '$apikey' LIMIT 1;";
if(!$result = $sdb->query($sql)) return 'ERROR: ['.$sdb->error.']';
return "Deleted: $link";
}else return "You are not authorized to delete that link.";
}
}else{ return 'ERROR: ['.$sdb->error.']'; }
}
}
?>