Hi,
Here is the old code. What does it do, it takes a MySql Command from an external 3rd Party App, and gets the results from the database, and Pulls in the results. So the POST Query is effectively the MySql Command to pull records from the database. This is a MySql Wrapper effectively.
<?php
header("Content-type: text/html; charset=utf-8");
define('DB_HOST','myhost');
define('DB_USER','myusername');
define('DB_PASS','mypassword');
define('DB_NAME','mydb');
define('PASSWORD','connectionpassword');
$db = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die('<?xml version="1.0"?><error><![CDATA[' . mysql_error(). ']]></error>');
mysql_select_db(DB_NAME, $db) or die('<?xml version="1.0"?><error><![CDATA[' . mysql_error(). ']]></error>');
mysql_query("SET NAMES 'utf8'") or die('<?xml version="1.0"?><error><![CDATA[' . mysql_error(). ']]></error>'); ;
mysql_query("SET SESSION SQL_BIG_SELECTS=1;") or die('<?xml version="1.0"?><error><![CDATA[' . mysql_error(). ']]></error>');
if ($_GET["password"]!=PASSWORD){exit('<?xml version="1.0"?><error><![CDATA[Password is incorrect!]]></error>');}
$xmlP = new Queries(str_replace('\\"','"',str_replace("\\'","'",urldecode($_POST['query']))));
$xmlP->parseQueries();
foreach($xmlP->queries as $k=>$v)
{
$queries = split("--GO;--",$v);
foreach($queries as $q){
$result = mysql_query($q);
}
if($xmlP->request == 'RETURN'){
$xmlP->select($result, $k);
} else {
$xmlP->exec($result, $k);
}
}
$xmlP->prepareReturn();
echo('<?xml version="1.0"?>'.$xmlP->xmlReturn);
class Queries
{
private $xml;
private $xmlQueries;
public $request;
public $queries = array();
public $ids = array();
public $return = array();
public $xmlReturn = array();
public function __construct($xml)
{
$this->xml = $xml;
}
public function parseQueries()
{
preg_match('/<queries type="([^"]+)">(.+)<\/queries>/s', $this->xml, $m);
$this->request = $m[1];
$this->xmlQueries = $m[2];
preg_match_all('/<query id="([^"]+)">(.+?)<\/query>/s', $this->xmlQueries, $m);
$this->ids = $m[1];
$this->queries = $m[2];
}
public function select($res, $k)
{
if(!mysql_num_rows($res) && mysql_error()!=""){
$this->return[$this->ids[$k]]['status'] = 'ERROR';
$this->return[$this->ids[$k]]['error'] = mysql_error();
} else {
$this->return[$this->ids[$k]]['status'] = 'OK';
while($row = mysql_fetch_array($res))
{
foreach($row as $key=>$val)
{
if(preg_match('/^[0-9]+$/',$key)) unset($row[$key]);
}
$this->return[$this->ids[$k]][] = $row;
}
}
}
public function exec($res, $k)
{
if($res || mysql_error()==""){
$this->return[$this->ids[$k]]['status'] = 'OK';
} else {
$this->return[$this->ids[$k]]['status'] = 'ERROR';
$this->return[$this->ids[$k]]['error'] = mysql_error();
}
}
public function prepareReturn()
{
$this->xmlReturn = '<resultset>';
foreach($this->return as $k=>$v)
{
$this->xmlReturn.= '<return id="'.$k.'" status="'.$v['status'].'">';
if($v['status'] == 'ERROR'){
$this->xmlReturn.= '<errormessage>'.$v['error'].'</errormessage>';
} else {
foreach($v as $key=>$val)
{
if(preg_match('/^[0-9]+$/',$key)){
$this->xmlReturn.= '<row>';
foreach($val as $recId=>$recValue)
{
$this->xmlReturn.= "<$recId><![CDATA[$recValue]]></$recId>";
}
$this->xmlReturn.= '</row>';
}
}
}
$this->xmlReturn.= '</return>';
}
$this->xmlReturn.= '</resultset>';
}
}
?>
And here is my attempt to start converting it, apologies, but its been 15 years since I've been actively coding:
<?php
header("Content-type: text/html; charset=utf-8");
define('DB_HOST','myhost');
define('DB_USER','myusername');
define('DB_PASS','mypassword');
define('DB_NAME','mydb');
define('PASSWORD','connectionpassword');
$db = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die('<?xml version="1.0"?><error><![CDATA[' . mysqli_error(). ']]></error>');
mysqli_set_charset($db, 'utf8');
mysqli_select_db($db, DB_NAME) or die('<?xml version="1.0"?><error><![CDATA[' . mysqli_error(). ']]></error>');
if ($_GET["password"]!=PASSWORD){exit('<?xml version="1.0"?><error><![CDATA[Password is incorrect!]]></error>');}
$xmlP = new Queries(str_replace('\\"','"',str_replace("\\'","'",urldecode($_POST['query']))));
$xmlP->parseQueries();
foreach($xmlP->queries as $k=>$v)
{
$queries = explode("--GO;--",$v);
foreach($queries as $q){
$result = mysqli_query($q);
}
if($xmlP->request == 'RETURN'){
$xmlP->select($result, $k);
} else {
$xmlP->exec($result, $k);
}
}
$xmlP->prepareReturn();
echo('<?xml version="1.0"?>'.$xmlP->xmlReturn);
class Queries
{
private $xml;
private $xmlQueries;
public $request;
public $queries = array();
public $ids = array();
public $return = array();
public $xmlReturn = array();
public function __construct($xml)
{
$this->xml = $xml;
}
public function parseQueries()
{
preg_match('/<queries type="([^"]+)">(.+)<\/queries>/s', $this->xml, $m);
$this->request = $m[1];
$this->xmlQueries = $m[2];
preg_match_all('/<query id="([^"]+)">(.+?)<\/query>/s', $this->xmlQueries, $m);
$this->ids = $m[1];
$this->queries = $m[2];
}
public function select($res, $k)
{
if(!mysqli_num_rows($res) && mysqli_error()!=""){
$this->return[$this->ids[$k]]['status'] = 'ERROR';
$this->return[$this->ids[$k]]['error'] = mysqli_error();
} else {
$this->return[$this->ids[$k]]['status'] = 'OK';
while($row = mysqli_fetch_array($res))
{
foreach($row as $key=>$val)
{
if(preg_match('/^[0-9]+$/',$key)) unset($row[$key]);
}
$this->return[$this->ids[$k]][] = $row;
}
}
}
public function exec($res, $k)
{
if($res || mysqli_error()==""){
$this->return[$this->ids[$k]]['status'] = 'OK';
} else {
$this->return[$this->ids[$k]]['status'] = 'ERROR';
$this->return[$this->ids[$k]]['error'] = mysqli_error();
}
}
public function prepareReturn()
{
$this->xmlReturn = '<resultset>';
foreach($this->return as $k=>$v)
{
$this->xmlReturn.= '<return id="'.$k.'" status="'.$v['status'].'">';
if($v['status'] == 'ERROR'){
$this->xmlReturn.= '<errormessage>'.$v['error'].'</errormessage>';
} else {
foreach($v as $key=>$val)
{
if(preg_match('/^[0-9]+$/',$key)){
$this->xmlReturn.= '<row>';
foreach($val as $recId=>$recValue)
{
$this->xmlReturn.= "<$recId><![CDATA[$recValue]]></$recId>";
}
$this->xmlReturn.= '</row>';
}
}
}
$this->xmlReturn.= '</return>';
}
$this->xmlReturn.= '</resultset>';
}
}
?>
Any Help Appreciated.
Thanks
Mabs