Dubz
01-06-2012, 09:46 PM
I have a post function that was made by someone else (an advanced coder) which works fine. I'm using this function to interact with php files and an online database. Theirs a file that does all the database interaction on the site so the database information isn't leaked out. The file i created for this purpose works fine the way I made it to but for some reason after adding a new case to it, it won't work for that case. Everything else works the way it should. When I open the debug file (wrote the content from the post function to it) their is 4 random characters on the first line, the actual data that comes up on the second, a random character on the third and fourth, and a couple blank lines under it.
Post function:
function post($url, $data, $ref = "") {
$url = parse_url($url);
$http = fsockopen($url['host'], 80, $en, $es, 45);
if($http) {
fputs($http, "POST ".$url['path']." HTTP/1.1\r\n");
fputs($http, "Host: ".$url['host']."\r\n");
if($ref != "") { fputs($http, "Referer: ".$ref."\r\n"); }
fputs($http, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($http, "Content-length: ".strlen($data)."\r\n");
fputs($http, "Connection: close\r\n\r\n");
fputs($http, $data);
while(!feof($http)&&!is_bool($http)) {
@$result .= fgets($http, 128);
}
} else {
return array(
"status" => "error",
"error" => "(".$en.") ".$es
);
}
fclose($http);
$result = explode("\r\n\r\n", $result, 2);
$header = (isset($result[0])) ? $result[0] : false;
$content = (isset($result[1])) ? $result[1] : false;
return array(
"status" => "ok",
"header" => $header,
"content" => $content
);
}
remoteDB.php (the bridge on the site)
<?php
if(isset($_POST['process'])){
//Database information
$sHost = "localhost";
$sUser = "";
$sPass = "";
$sDatabase = "";
mysql_connect($sHost,$sUser,$sPass) or die(mysql_error());
mysql_select_db($sDatabase) or die (mysql_error());
//Get the variables and set them again
foreach($_POST as $k => $v){
if(isset($v)) ${$k} = $v;
}
unset($process);//Value only needed to submit the request
if(!empty($password)) $password = md5(md5($password));
/*
$username
$password
$botID
$action
$table
$column
$value
*/
/*
//Debug
print "Username: $username<br />";
print "Password: $password<br />";
print "botID: $botID<br />";
print "action: $action<br />";
print "Table: $table<br />";
print "Column: $column<br />";
print "Value: $value<br />";
*/
function getAccount($user,$pass){
$query = getDB("users","id","username='{$user}' AND password='{$pass}'");
$num = mysql_num_rows($query);
if($num!=1) return false;
$account = mysql_fetch_assoc($query);
return $account;
}
function getBot($account,$botID){
$query = getDB("bots","*","id='{$botID}' AND owner='{$account['id']}'");
$num = mysql_num_rows($query);
if($num!=1) return false;
$bot = mysql_fetch_assoc($query);
return $bot;
}
function updateDB($column,$value,$where = null){
if($where!=null) $where = "WHERE ".$where;
mysql_query("UPDATE bots SET $column='{$value}' {$where}") or die(mysql_error());
}
function getDB($table,$column,$where = null){
if($where!=null) $where = "WHERE ".$where;
$query = mysql_query("SELECT {$column} FROM {$table} {$where}") or die(mysql_error());
return $query;
}
$account = getAccount($username,$password);
if($account === false) die("Login failed!");
if(!$action) die();
switch($action){
case 'checkBot':
$bot = getBot($account,$botID);
if($bot === false)
die("Validation failed!");
elseif($account['id']==$bot['owner'])
die("Validation passed!");
else
die("An unknown error occured.");
break;
case 'getDB':
switch($table){
case 'access':
$result = array();
$query = getDB($table,'*');
while($row = mysql_fetch_assoc($query)){
$result[$row['userid']] = $row['rank'];
}
break;
case 'bots':
$query = getDB($table,$column,"id={$botID}");
$results = mysql_fetch_assoc($query);
if($column=='*') $result = $results;
else $result = $results[$column];
break;
case 'madgabList':
$result = array();
$query = getDB($table,'*');
while($row = mysql_fetch_assoc($query)){
$result[$row['phrase']] = $row['solution'];
}
break;
case 'powers':
$result = array();
$query = getDB($table,'*');
while($row = mysql_fetch_assoc($query)){
$result[$row['id']] = $row['name'];
}
break;
case 'wordList'://Happens with this one
$result = array();
$query = getDB($table,'*');
while($row = mysql_fetch_assoc($query)){
$result[$row['id']] = $row['word'];
}
break;
}
break;
case 'updateDB':
switch($table){
case 'bots':
updateDB($column,$value,"id={$botID}");
die("Updated!");
break;
}
break;
default:
die;
}
die(print_r($result)); //Debug
print serialize($result);
die;
}
?>
<html>
<body>
<form method="post">
<table>
<?
$postArray = array('username','password','botID','action','table','column','value');
foreach($postArray as $name){
print "<tr>";
print "<td>{$name}</td>";
switch($name){
case 'password':
print "<td><input type='password' name='{$name}' value='{$_GET[$name]}'/></td>";
break;
case 'action':
print "<td><select name='{$name}'>";
print "<option value='checkBot'>checkBot</option>";
print "<option value='getDB'>getDB</option>";
print "<option value='updateDB'>updateDB</option>";
print "</select></td>";
break;
default:
print "<td><input type='text' name='{$name}' value='{$_GET[$name]}'/></td>";
}
print "</tr>\r\n";
}
?>
<tr>
<td><input type="submit" name="process"/></td>
</tr>
</table>
</form>
</body>
</html>
When the 'die(print_r($result));' happens, the array is printed as desired. Their is no extra characters before or after it either. When I print the serialized version of it (used to pass the data from one file to the other), their is still no data before or after it. The post function hasn't changed at all and all the other actions work properly but this one. Any ideas?
The array should be something like this:
$wordList = array(
1 => 'word1',
2 => 'word2',
3 => 'word3'
);
When serialized, it should be like this:
a:3:{i:1;s:5:"word1";i:2;s:5:"word2";i:3;s:5:"word3";}
When it's saved to the debug.txt it looks something like this:
f67a
a:3:{i:1;s:5:"word1";i:2;s:5:"word2";i:3;s:5:"word3";}
0
*extra line*
*extra line*
Post function:
function post($url, $data, $ref = "") {
$url = parse_url($url);
$http = fsockopen($url['host'], 80, $en, $es, 45);
if($http) {
fputs($http, "POST ".$url['path']." HTTP/1.1\r\n");
fputs($http, "Host: ".$url['host']."\r\n");
if($ref != "") { fputs($http, "Referer: ".$ref."\r\n"); }
fputs($http, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($http, "Content-length: ".strlen($data)."\r\n");
fputs($http, "Connection: close\r\n\r\n");
fputs($http, $data);
while(!feof($http)&&!is_bool($http)) {
@$result .= fgets($http, 128);
}
} else {
return array(
"status" => "error",
"error" => "(".$en.") ".$es
);
}
fclose($http);
$result = explode("\r\n\r\n", $result, 2);
$header = (isset($result[0])) ? $result[0] : false;
$content = (isset($result[1])) ? $result[1] : false;
return array(
"status" => "ok",
"header" => $header,
"content" => $content
);
}
remoteDB.php (the bridge on the site)
<?php
if(isset($_POST['process'])){
//Database information
$sHost = "localhost";
$sUser = "";
$sPass = "";
$sDatabase = "";
mysql_connect($sHost,$sUser,$sPass) or die(mysql_error());
mysql_select_db($sDatabase) or die (mysql_error());
//Get the variables and set them again
foreach($_POST as $k => $v){
if(isset($v)) ${$k} = $v;
}
unset($process);//Value only needed to submit the request
if(!empty($password)) $password = md5(md5($password));
/*
$username
$password
$botID
$action
$table
$column
$value
*/
/*
//Debug
print "Username: $username<br />";
print "Password: $password<br />";
print "botID: $botID<br />";
print "action: $action<br />";
print "Table: $table<br />";
print "Column: $column<br />";
print "Value: $value<br />";
*/
function getAccount($user,$pass){
$query = getDB("users","id","username='{$user}' AND password='{$pass}'");
$num = mysql_num_rows($query);
if($num!=1) return false;
$account = mysql_fetch_assoc($query);
return $account;
}
function getBot($account,$botID){
$query = getDB("bots","*","id='{$botID}' AND owner='{$account['id']}'");
$num = mysql_num_rows($query);
if($num!=1) return false;
$bot = mysql_fetch_assoc($query);
return $bot;
}
function updateDB($column,$value,$where = null){
if($where!=null) $where = "WHERE ".$where;
mysql_query("UPDATE bots SET $column='{$value}' {$where}") or die(mysql_error());
}
function getDB($table,$column,$where = null){
if($where!=null) $where = "WHERE ".$where;
$query = mysql_query("SELECT {$column} FROM {$table} {$where}") or die(mysql_error());
return $query;
}
$account = getAccount($username,$password);
if($account === false) die("Login failed!");
if(!$action) die();
switch($action){
case 'checkBot':
$bot = getBot($account,$botID);
if($bot === false)
die("Validation failed!");
elseif($account['id']==$bot['owner'])
die("Validation passed!");
else
die("An unknown error occured.");
break;
case 'getDB':
switch($table){
case 'access':
$result = array();
$query = getDB($table,'*');
while($row = mysql_fetch_assoc($query)){
$result[$row['userid']] = $row['rank'];
}
break;
case 'bots':
$query = getDB($table,$column,"id={$botID}");
$results = mysql_fetch_assoc($query);
if($column=='*') $result = $results;
else $result = $results[$column];
break;
case 'madgabList':
$result = array();
$query = getDB($table,'*');
while($row = mysql_fetch_assoc($query)){
$result[$row['phrase']] = $row['solution'];
}
break;
case 'powers':
$result = array();
$query = getDB($table,'*');
while($row = mysql_fetch_assoc($query)){
$result[$row['id']] = $row['name'];
}
break;
case 'wordList'://Happens with this one
$result = array();
$query = getDB($table,'*');
while($row = mysql_fetch_assoc($query)){
$result[$row['id']] = $row['word'];
}
break;
}
break;
case 'updateDB':
switch($table){
case 'bots':
updateDB($column,$value,"id={$botID}");
die("Updated!");
break;
}
break;
default:
die;
}
die(print_r($result)); //Debug
print serialize($result);
die;
}
?>
<html>
<body>
<form method="post">
<table>
<?
$postArray = array('username','password','botID','action','table','column','value');
foreach($postArray as $name){
print "<tr>";
print "<td>{$name}</td>";
switch($name){
case 'password':
print "<td><input type='password' name='{$name}' value='{$_GET[$name]}'/></td>";
break;
case 'action':
print "<td><select name='{$name}'>";
print "<option value='checkBot'>checkBot</option>";
print "<option value='getDB'>getDB</option>";
print "<option value='updateDB'>updateDB</option>";
print "</select></td>";
break;
default:
print "<td><input type='text' name='{$name}' value='{$_GET[$name]}'/></td>";
}
print "</tr>\r\n";
}
?>
<tr>
<td><input type="submit" name="process"/></td>
</tr>
</table>
</form>
</body>
</html>
When the 'die(print_r($result));' happens, the array is printed as desired. Their is no extra characters before or after it either. When I print the serialized version of it (used to pass the data from one file to the other), their is still no data before or after it. The post function hasn't changed at all and all the other actions work properly but this one. Any ideas?
The array should be something like this:
$wordList = array(
1 => 'word1',
2 => 'word2',
3 => 'word3'
);
When serialized, it should be like this:
a:3:{i:1;s:5:"word1";i:2;s:5:"word2";i:3;s:5:"word3";}
When it's saved to the debug.txt it looks something like this:
f67a
a:3:{i:1;s:5:"word1";i:2;s:5:"word2";i:3;s:5:"word3";}
0
*extra line*
*extra line*