Server Side
The server side requires a web service in php or any other development langauge that provide the functionaluty the mobile application needs.
Below is a skeleton php file that can be use to integrate the mobile communication with existing back office system.
//—————————————————————————————————————————————————————-
/*
Name: Ajax API's
Author: JW. Theiss
Created: 06/2010
Description
This file is designed to handle the ajax API's and commands.
*/
//—————————————————————————————————————————————————————-
/*
The getfile command.
This function uses httpget command
The source comes from the rsetup.xml file on the Handheld terminal
example: applications/eparking/wp-content/plugins/idev/data/
The destination is in the application folder on the Handheld terminal.
example: tws
*/
//—————————————————————————————————————————————————————-
// Declare functions
function getParam($sParam)
{
$text = "";
if (isset($_POST[$sParam]))
$text = $_POST[$sParam];
else
{
if (isset($_GET[$sParam]))
$text = $_GET[$sParam];
}
if(get_magic_quotes_gpc())
{
$text = stripslashes($text);
}
return $text;
}
//—————————————————————————————————————————————————————-
function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "")
{
$fiveMBs = 5 * 1024 * 1024;
$fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+');
fputs($fp, $input);
rewind($fp);
$data = fgetcsv($fp, 1000, $delimiter, $enclosure); // $escape only got added in 5.3.0
fclose($fp);
return $data;
}
//—————————————————————————————————————————————————————-
function readxmlfile()
{
if (!isset($_POST['filename']))
{
echo "999,Need Filename";
return;
}
$filename = $this->prepare($_POST['filename']);
if ($this->validPath($filename))
{
$fd = fopen($filename,'r');
if ($fd)
{
$contents = fread($fd, filesize($filename));
echo "000," . $contents;
fclose($fd);
}
else
echo "999,Failed To Open File";
}
else
echo "999,Invalid Path $filename";
}
//—————————————————————————————————————————————————————-
function removefile()
{
if (!isset($_POST['filename']))
{
echo "999,Need Filename";
return;
}
$filename = $this->prepare($_POST['filename']);
if ($this->validPath($filename))
{
if (file_exists($filename))
{
unlink($filename);
}
echo "000,OK";
}
else
echo "999,Invalid Path $filename";
}
//—————————————————————————————————————————————————————-
function putfile()
{
if (!isset($_POST['destination']))
{
echo "999,Need Destination Filename";
return;
}
$filename = $this->prepare($_POST['destination']);
if ($this->validPath($filename))
{
if (isset($_FILES['file']))
{
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, $fileSize);
fclose($fp);
$fd = fopen($filename,'w');
if ($fd)
{
if (fwrite($fd, $content,$fileSize))
echo "000,OK : " . $filename . " = " . $fileSize;
else
echo "999,Failed To Write To $filename";
fclose($fd);
}
else
echo "999,Failed To Open File";
}
else
echo "999,No File Passed";
}
else
echo "999,Invalid Path $filename";
}
//—————————————————————————————————————————————————————-
function databaseExecute()
{
if (!isset($_POST['sql']))
{
echo "999,Need SQL Statement";
return;
}
$db = new dbClass();
if (!$db->connect($this->dbtype, $this->dbhost, $this->dbname, $this->dbuser, $this->dbpass))
{
return;
}
$sql = $this->prepare($_POST['sql']);
if (substr_compare($sql,"select",0,6,true) != 0)
{
if ($db->execute($sql,false))
{
echo "000," . $db->affected();
}
else
echo "999,Failed To Execute Statememt $sql " . $db->lastError();
}
else
{
if ($db->query($sql))
{
$data = "000,
while ($db->fetchrow())
{
$data .= "
for ($i = 0;$i < $db->fields();$i++)
{
$data .= "
$data .= $db->field($i);
$data .= "
}
$data .= "";
}
$data .= "";
echo $data;
}
else
echo "999,Failed To Execute Query $sql" . $db->lastError();
}
}
//—————————————————————————————————————————————————————-
function databaseExport()
{
if (!isset($_POST['filename']))
{
echo "999,Need Destination Filename";
return;
}
if (!isset($_POST['sql']))
{
echo "999,Need Export SQL Statement";
return;
}
$filename = $this->prepare($_POST['filename']);
$sql = $this->prepare($_POST['sql']);
$db = new dbClass();
if (!$db->connect($this->dbtype, $this->dbhost, $this->dbname, $this->dbuser, $this->dbpass))
{
echo "999,Failed To Connect";
return;
}
if ($db->query($sql))
{
$text = "";
while ($db->fetchrow())
{
for ($i = 0;$i < $db->fields();$i++)
{
if ($i == 0)
$text .= $db->field($i);
else
$text .= "," . $db->field($i);
}
$text .= $this->newline;
}
$fd = fopen($filename,'w');
if ($fd)
{
if (fwrite($fd, $text,strlen($text)))
echo "000,Data Exported To $filename";
else
echo "999,Failed To Write To $filename";
fclose($fd);
}
else
echo "999,Failed To Open File";
}
else
echo "999,Failed To Execute Query $sql " . $db->lastError();
}
//—————————————————————————————————————————————————————-
//—————————————- Code To Do This —————————————-
//—————————————————————————————————————————————————————-
$command = getParam('command');
if ($sCommand == "system.readxmlfile")
{
$this->readxmlfile();
return true;
}
//—————————————————————————————————————————————————————-
if ($sCommand == "system.readfile")
{
if (!isset($_POST['filename']))
{
echo "999,Need Filename";
return;
}
$filename = $this->prepare($_POST['filename']);
$this->readfile( $filename );
return true;
}
//—————————————————————————————————————————————————————-
if ($sCommand == "system.writexmlfile")
{
$this->writexmlfile();
return true;
}
//—————————————————————————————————————————————————————-
if ($sCommand == "system.removefile")
{
$this->removefile();
return true;
}
//—————————————————————————————————————————————————————-
if ($sCommand == "system.putfile")
{
$this->putfile();
return true;
}
//—————————————————————————————————————————————————————-
if ($sCommand == "database.execute")
{
$this->databaseExecute();
return true;
}
//—————————————————————————————————————————————————————-
if ($sCommand == "database.export")
{
$this->databaseExport();
return true;
}
//—————————————————————————————————————————————————————-
if ($command == "transactions")
{
$data = getParam('data');
if ($data == "")
{
$db->close();
echo "999,Need Data";
exit();
}
$data = stripslashes($data);
$xml = new SimpleXMLElement($data);
foreach ($xml->row as $row)
{
$i = 0;
foreach ($row as $column)
{
if ($i == 0)
$guid = (string)$column;
if ($i == 1)
$dt = (string)$column;
if ($i == 2)
$transid = (string)$column;
if ($i == 3)
$data = (string)$column;
if ($i == 4)
$userid = (string)$column;
if ($i == 5)
$hht = (string)$column;
$i++;
}
$fields = str_getcsv($data);
$dguid = $fields[0];
switch ($transid)
{
case 'TR01': // Prime
break;
}
}
echo "000,OK";
exit();
}
?>