Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
Cleaned up the tabbing to make it more readable compared to previous …
Browse files Browse the repository at this point in the history
…versions... However, im not cliaming to be perfect..
  • Loading branch information
MartyniP committed Oct 19, 2012
1 parent 8ad6fd3 commit 42d622e
Show file tree
Hide file tree
Showing 13 changed files with 390 additions and 423 deletions.
1 change: 0 additions & 1 deletion admin/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$WORKING_DIR = '../';
if (file_exists("../".get_phurl_option('theme_path') . "header.php")) {
include ("../".get_phurl_option('theme_path') . "header.php");

} else {
die ("<h2>Could not load theme</h2>");
}
Expand Down
1 change: 0 additions & 1 deletion admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$WORKING_DIR = '../';
if (file_exists("../".get_phurl_option('theme_path') . "header.php")) {
include ("../".get_phurl_option('theme_path') . "header.php");

} else {
die ("<h2>Could not load theme</h2>");
}
Expand Down
222 changes: 100 additions & 122 deletions api/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,57 @@
$response = mysql_real_escape_string(trim($_GET['response']));
}
if (isset($_GET['apiKey'])) {
$apiLimit = get_phurl_option("api_limit");
if ((int)$apiLimit != 0) {
$apiKey = mysql_real_escape_string(trim($_GET['apiKey']));
$db_result = mysql_query("SELECT remain,time FROM ".DB_PREFIX."api WHERE apiKey='$apiKey'") or db_die(__FILE__, __LINE__, mysql_error());
if (mysql_num_rows($db_result) == 0) {
mysql_query("INSERT INTO ".DB_PREFIX."api (apiKey, time, remain) VALUES('".$apiKey."', '".time()."', ".(int)$apiLimit.")") or db_die(__FILE__, __LINE__, mysql_error());
$apiLimit = get_phurl_option("api_limit");
if ((int)$apiLimit != 0) {
$apiKey = mysql_real_escape_string(trim($_GET['apiKey']));
$db_result = mysql_query("SELECT remain,time FROM ".DB_PREFIX."api WHERE apiKey='$apiKey'") or db_die(__FILE__, __LINE__, mysql_error());
}
$db_row = mysql_fetch_assoc($db_result);
if ((int)$db_row['time'] <= time()-60*60) {
mysql_query("UPDATE ".DB_PREFIX."api SET remain=".((int)$apiLimit-1).", time='".time()."'") or db_die(__FILE__, __LINE__, mysql_error());
} elseif ($db_row['remain'] != 0) {
mysql_query("UPDATE ".DB_PREFIX."api SET remain = (remain - 1)") or db_die(__FILE__, __LINE__, mysql_error());
} else {
$errorCode = 11;
if ($response == "json") {
echo json_encode(array('code'=>'403', 'error'=>array('0',$errorCode)), JSON_FORCE_OBJECT);
exit();
}
if ($response == "text") {
echo "error: ".$errorCode."\n";
exit();
if (mysql_num_rows($db_result) == 0) {
mysql_query("INSERT INTO ".DB_PREFIX."api (apiKey, time, remain) VALUES('".$apiKey."', '".time()."', ".(int)$apiLimit.")") or db_die(__FILE__, __LINE__, mysql_error());
$db_result = mysql_query("SELECT remain,time FROM ".DB_PREFIX."api WHERE apiKey='$apiKey'") or db_die(__FILE__, __LINE__, mysql_error());
}
$db_row = mysql_fetch_assoc($db_result);
if ((int)$db_row['time'] <= time()-60*60) {
mysql_query("UPDATE ".DB_PREFIX."api SET remain=".((int)$apiLimit-1).", time='".time()."'") or db_die(__FILE__, __LINE__, mysql_error());
} elseif ($db_row['remain'] != 0) {
mysql_query("UPDATE ".DB_PREFIX."api SET remain = (remain - 1)") or db_die(__FILE__, __LINE__, mysql_error());
} else {
$errorCode = 11;
if ($response == "json") {
echo json_encode(array('code'=>'403', 'error'=>array('0',$errorCode)), JSON_FORCE_OBJECT);
exit();
}
if ($response == "text") {
echo "error: ".$errorCode."\n";
exit();
}

}
}
}
}
if (isset($_GET['apiKey']) && isset($_GET['url'])) {
$alias = "";
if (isset($_GET['a'])) {
$alias = mysql_real_escape_string(trim($_GET['a']));
}
$apiKey = mysql_real_escape_string(trim($_GET['apiKey']));
$url = mysql_real_escape_string(trim($_GET['url']));

if (!preg_match("/^(".URL_PROTOCOLS.")\:\/\//i", $url)) {
if (!preg_match("/^(".URL_PROTOCOLS.")\:\/\//i", $url)) {
$prefix = explode(":", $url);
if ($prefix[0] == 'mailto') {
$url = $url;
} else {
$url = "http://".$url;
}
}

$last = $url[strlen($url) - 1];

if ($last == "/") {
$url = substr($url, 0, -1);
}

$data = @parse_url($url);
if ($prefix[0] == 'mailto') {
$data['scheme'] = 'mailto';
$data['host'] = 'none';
$url = "http://".$url;
}
}
$last = $url[strlen($url) - 1];
if ($last == "/") {
$url = substr($url, 0, -1);
}
$data = @parse_url($url);
if ($prefix[0] == 'mailto') {
$data['scheme'] = 'mailto';
$data['host'] = 'none';
}

$db_result = mysql_query("SELECT id,apiKey FROM ".DB_PREFIX."users WHERE suspended='0'");
while ($db_row = mysql_fetch_assoc($db_result)) {
Expand All @@ -72,92 +68,74 @@
}
}

if (strlen($url) == 0) {
$_ERROR[] = "01";
}
else if (empty($data['scheme']) || empty($data['host'])) {
$_ERROR[] = "02";
}
else if (!isset($validApiKey[$apiKey]) || $validApiKey[$apiKey] != 1) {
$_ERROR[] = "08";
}
else {
$blcheck = file_get_contents("http://gsb.phurlproject.org/lookup.php?url=$url");
if (trim($blcheck) == "1") {
$_ERROR[] = "03";
if (strlen($url) == 0) {
$_ERROR[] = "01";
} elseif (empty($data['scheme']) || empty($data['host'])) {
$_ERROR[] = "02";
} elseif (!isset($validApiKey[$apiKey]) || $validApiKey[$apiKey] != 1) {
$_ERROR[] = "08";
} else {
$blcheck = file_get_contents("http://gsb.phurlproject.org/lookup.php?url=$url");
if (trim($blcheck) == "1") {
$_ERROR[] = "03";
}
$hostname = get_hostname();
if (preg_match("/($hostname)/i", $data['host'])) {
//echo $hostname."|".$domain."|".$data['host'];
$_ERROR[] = "04";
}
}
$hostname = get_hostname();
if (preg_match("/($hostname)/i", $data['host'])) {
echo $hostname."|".$domain."|".$data['host'];
$_ERROR[] = "04";
}
}

if (strlen($alias) > 0) {
if (!preg_match("/^[a-zA-Z0-9_-]+$/", $alias)) {
$_ERROR[] = "05";
}
else if (code_exists($alias) || alias_exists($alias)) {
$_ERROR[] = "06";
}
}
// print_errors();
if (count($_ERROR) == 0) {
$create = true;

if (($url_data = url_exists($url))) {
$create = false;
$id = $url_data[0];
$code = $url_data[1];
$old_alias = $url_data[2];

if (strlen($alias) > 0) {
if ($old_alias != $alias) {
$create = true;
}
}
}

if ($create) {
do {
if (strlen($alias) > 0) {
if (!preg_match("/^[a-zA-Z0-9_-]+$/", $alias)) {
$_ERROR[] = "05";
} else if (code_exists($alias) || alias_exists($alias)) {
$_ERROR[] = "06";
}
}
if (count($_ERROR) == 0) {
$create = true;
if (($url_data = url_exists($url))) {
$create = false;
$id = $url_data[0];
$code = $url_data[1];
$old_alias = $url_data[2];
if (strlen($alias) > 0) {
if ($old_alias != $alias) {
$create = true;
}
}
}
if ($create) {
do {
$sctype = get_phurl_option('shortcode_type');
if ($sctype = "r") {
$code = generate_code_rand();
} else {
$code = generate_code(get_last_number());

if (!increase_last_number()) {
die("System error!");
}
if ($sctype = "r") {
$code = generate_code_rand();
} else {
$code = generate_code(get_last_number());
if (!increase_last_number()) {
die("System error!");
}
if (code_exists($code) || alias_exists($code)) {
continue;
}

break;
} while (1);

$id = insert_url($url, $code, $alias, $apiKey);
}

if (strlen($alias) > 0) {
$code = $alias;
}

$short_url = get_phurl_option('site_url')."/".$code;

// $_GET['url'] = "";
// $_GET['alias'] = "";
if ($response == "json") {
$json = array('code'=>'200', 'request'=>$_GET, 'url'=>$short_url);
echo json_encode($json, JSON_FORCE_OBJECT);
exit();
}
if ($response == "text") {
echo "$short_url\n";
exit();
}
if (code_exists($code) || alias_exists($code)) {
continue;
}
break;
} while (1);
$id = insert_url($url, $code, $alias, $apiKey);
}
if (strlen($alias) > 0) {
$code = $alias;
}
$short_url = get_phurl_option('site_url')."/".$code;
if ($response == "json") {
$json = array('code'=>'200', 'request'=>$_GET, 'url'=>$short_url);
echo json_encode($json, JSON_FORCE_OBJECT);
exit();
} elseif ($response == "text") {
echo "$short_url\n";
exit();
}
}
}
}
if (!isset($_GET['apiKey'])) {
$_ERROR[] = "07";
Expand All @@ -167,9 +145,9 @@
}
if ($response == "json") {
echo json_encode(array('code'=>'400', 'error'=>$_ERROR), JSON_FORCE_OBJECT);
}
if ($response == "text") {
}elseif ($response == "text") {
foreach ($_ERROR as $errorCode) {
echo "error: ".$errorCode."\n";
}
}
?>
Loading

0 comments on commit 42d622e

Please sign in to comment.