Code:
<?php
/**
* Zen iphone.php -- version 1.0beta
*
* Modified by MrChristoh
*
* By Dirk Malorny <iphone-apps (at) malorny (dot) net>
* You may modify this script to fulfill your special needs, but all modifications you make are at your own risk!
*/
require('includes/application_top.php');
define("VERSION","1.0beta conversion");
define("DELIMITER","|");
// =========================== MAIN ===========================
$action = (isset($_REQUEST['a']) ? $_REQUEST['a'] : '');
if (zen_not_null($action)) {
header('Content-Type: text/plain; Charset=UTF-8');
switch ($action) {
case 'login':
echo "OK";
break;
case 'status':
// 1: plugin version
echo VERSION."\n";
// 2: order_status
$status_query = create_status_query();
$arr = array();
while ($status = zen_db_fetch_array($status_query)) {
$arr[] = $status['orders_status_id'].':'.escape($status['orders_status_name']);
}
outputStatusLine($arr);
// 3: order count
$order_count = zen_db_fetch_array(create_order_count_query());
echo $order_count['count']."\n";
break;
case 'customers':
$customers_query = create_customer_query($_REQUEST['lu']);
while ($customers = zen_db_fetch_array($customers_query)) {
$arr = array();
$arr[] = $customers['customers_id'];
$arr[] = $customers['customers_lastname'];
$arr[] = $customers['customers_firstname'];
$arr[] = $customers['customers_email_address'];
$arr[] = $customers['customers_dob'];
$arr[] = $customers['customers_telephone'];
$arr[] = $customers['customers_fax'];
$arr[] = $customers['entry_company'];
$arr[] = $customers['entry_street_address'];
$arr[] = $customers['entry_postcode'];
$arr[] = $customers['entry_city'];
$arr[] = $customers['entry_state'];
$arr[] = $customers['countries_name'];
outputLine($arr);
}
break;
case 'products':
$products_query = create_products_query($_REQUEST['lu'],$_REQUEST['of'],$_REQUEST['li']);
while ($products = zen_db_fetch_array($products_query)) {
$arr = array();
$arr[] = $products['products_id'];
$arr[] = $products['products_model'];
$arr[] = $products['products_price'];
$arr[] = $products['products_name'];
$arr[] = ereg_replace("[\n\r]",'\n',$products['products_description']);
outputLine($arr);
}
break;
case 'orders':
$orders_query = create_orders_query($_REQUEST['lu'],$_REQUEST['of'],$_REQUEST['li']);
$orderIds = array();
while ($orders = zen_db_fetch_array($orders_query)) {
$orderIds[] = $orders['orders_id'];
$arr = array();
$arr[] = $orders['orders_id'];
$arr[] = $orders['customers_id'];
$arr[] = $orders['orders_status_id'];
$arr[] = $orders['customers_name'];
$arr[] = $orders['payment_method'];
$arr[] = $orders['date_purchased'];
$arr[] = $orders['currency'];
$arr[] = $orders['orders_status_name'];
$arr[] = $orders['order_total'];
outputLine($arr);
}
if (!$orderIds) continue;
// order items
echo "\n--\n\n";
$orderIds = implode(',',$orderIds);
$items_query = create_items_query($orderIds);
while ($items = zen_db_fetch_array($items_query)) {
$arr = array();
$arr[] = $items['orders_products_id'];
$arr[] = $items['orders_id'];
$arr[] = $items['products_id'];
$arr[] = $items['products_quantity'];
$arr[] = $items['products_model'];
$arr[] = $items['products_name'];
$arr[] = $items['products_price'];
$arr[] = $items['final_price'];
$arr[] = $items['products_tax'];
$arr[] = html_entity_decode(ereg_replace("[\n\r]",'\n',$items['attributes']));
outputLine($arr);
}
break;
default:
echo "unknown action";
break;
} // switch
} else {
// TESTMODE
$TEST_RESULT = true;
testSuite();
} // MAIN
// =========================== QUERY functions ===========================
function create_customer_query($lu)
{
$filter = '';
if (isset($lu) && zen_not_null($lu)) {
$last_update = zen_db_input(zen_db_prepare_input($lu));
$filter = "where i.customers_info_date_account_created > '$last_update'
or i.customers_info_date_account_last_modified > '$last_update'";
}
$customers_query = zen_db_query("
select
c.customers_id,
c.customers_lastname,
c.customers_firstname,
c.customers_email_address,
NULLIF(DATE_FORMAT(c.customers_dob,'%Y-%m-%d'),'0000-00-00') customers_dob,
c.customers_telephone,
c.customers_fax,
a.entry_company,
a.entry_street_address,
a.entry_postcode,
a.entry_city,
a.entry_state,
l.countries_name
from " . zen_customers . " c
left join " . zen_address_book . " a on c.customers_id = a.customers_id and c.customers_default_address_id = a.address_book_id
left join " . zen_countries . " l on a.entry_country_id = l.countries_id
left join " . zen_customers_info . " i on c.customers_id = i.customers_info_id
$filter
order by c.customers_id");
return $customers_query;
}
function create_products_query($lu,$of,$li)
{
$filter = '';
$limit = '';
if (isset($lu) && zen_not_null($lu)) {
$last_update = zen_db_input(zen_db_prepare_input($lu));
$filter = " and p.products_date_added > '$last_update'
or p.products_last_modified > '$last_update'";
}
if (isset($of) && zen_not_null($of)) {
$offset = zen_db_input(zen_db_prepare_input($of));
$filter .= " and p.products_id < $offset";
}
if (isset($li) && zen_not_null($li)) {
$limit = "limit " . zen_db_input(zen_db_prepare_input($li));
}
$products_query = zen_db_query("
select
p.products_id,
p.products_model,
p.products_price,
d.products_name,
d.products_description
from " . ZEN_PRODUCTS . " p
left join " . ZEN_PRODUCTS_DESCRIPTION . " d on d.products_id = p.products_id
left join " . ZEN_LANGUAGES . " l on l.languages_id = d.language_id
where 1
$filter
order by p.products_id DESC
$limit
");
return $products_query;
}
function create_orders_query($lu,$of,$li)
{
global $languages_id;
$filter = '';
$limit = '';
if (isset($lu) && zen_not_null($lu)) {
$last_update = zen_db_input(zen_db_prepare_input($lu));
$filter .= " and (o.date_purchased > '$last_update'
or o.last_modified > '$last_update')";
}
if (isset($of) && zen_not_null($of)) {
$offset = zen_db_input(zen_db_prepare_input($of));
$filter .= " and o.orders_id < $offset";
}
if (isset($li) && zen_not_null($li)) {
$limit = "limit " . zen_db_input(zen_db_prepare_input($li));
}
$orders_query = zen_db_query("
select
o.orders_id,
o.customers_id,
o.customers_name,
o.payment_method,
DATE_FORMAT(o.date_purchased,'%Y-%m-%d %H:%i:%s') date_purchased,
o.currency,
s.orders_status_id,
s.orders_status_name,
ot.value as order_total
from " . ZEN_ORDERS . " o
left join " . ZEN_ORDERS_TOTAL . " ot on (o.orders_id = ot.orders_id),
" . ZEN_ORDERS_STATUS . " s
where o.orders_status = s.orders_status_id
and s.language_id = '" . (int)$languages_id . "'
and ot.class = 'ot_total'
$filter
order by o.orders_id DESC
$limit
");
return $orders_query;
}
function create_items_query($orderIds)
{
$items_query = zen_db_query("
select
op.orders_products_id,
op.orders_id,
op.products_id,
op.products_quantity,
op.products_model,
op.products_name,
op.products_price,
op.final_price,
op.products_tax,
group_concat(replace(concat_ws(':',replace(opa.products_options,':','{colon}'),replace(opa.products_options_values,':','{colon}')),'*','{star}') separator '*') attributes
from " . ZEN_ORDERS_PRODUCTS . " op
left outer join
" . ZEN_ORDERS_PRODUCTS_ATTRIBUTES . " opa
on op.orders_id=opa.orders_id
and op.orders_products_id=opa.orders_products_id
where op.orders_id in ($orderIds)
group by op.orders_products_id
order by op.orders_id DESC
");
return $items_query;
}
function create_status_query()
{
global $languages_id;
$status_query = zen_db_query("
select
s.orders_status_id,
s.orders_status_name
from " . ZEN_ORDERS_STATUS . " s
where s.language_id = '" . (int)$languages_id . "'
order by s.orders_status_id
");
return $status_query;
}
function create_order_count_query()
{
$query = zen_db_query("select count(*) as count from ".ZEN_ORDERS);
return $query;
}
function create_customer_count_query()
{
$query = zen_db_query("select count(*) as count from ".ZEN_CUSTOMERS);
return $query;
}
// =========================== MISC functions ===========================
function outputLine($arr) {
$arr = array_map("clean", $arr);
$line = implode(DELIMITER,$arr);
$line = unhtmlentities($line);
$line = mb_convert_encoding($line,"UTF-8");
echo $line."\n";
}
function outputStatusLine($arr) {
$line = implode("*",$arr);
$line = unhtmlentities($line);
$line = mb_convert_encoding($line,"UTF-8");
echo $line."\n";
}
function clean($str)
{
$str = str_replace(DELIMITER,'{pipe}',$str);
return $str;
}
function escape($str)
{
$str = str_replace(':','{colon}',$str);
$str = str_replace('*','{star}',$str);
return $str;
}
function unhtmlentities($string)
{
// replace numeric entities
$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
$string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
// replace literal entities
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}
// =========================== TEST functions ===========================
function testSuite()
{
global $TEST_RESULT;
ini_set('display_errors', 1);
header('Content-Type: text/html; Charset=UTF-8');
echo "<html><body><b>MyShop iPhone App plugin version ".VERSION."</b><br /><br /><table>";
assertVersion ("phpversion",phpversion(),"4.3.11");
assertVersion ("mysqlversion",mysql_get_server_info(),"4.1.0");
assertTrue ("mb_convert_encoding",function_exists("mb_convert_encoding"));
assertTrue ("zen_db_prepare_input",function_exists("zen_db_prepare_input"));
assertTrue ("zen_db_input",function_exists("zen_db_input"));
assertTrue ("zen_db_query",function_exists("zen_db_query"));
assertTrue ("zen_db_fetch_array",function_exists("zen_db_fetch_array"));
// test customer query
$customers_query = create_customer_query("2000-01-01");
$customers = zen_db_fetch_array($customers_query);
assertEqual ("customer query",count($customers),13);
// test products query
$products_query = create_products_query("2000-01-01");
$products = zen_db_fetch_array($products_query);
assertEqual ("products query",count($products),5);
// test products query
$orders_query = create_orders_query("2000-01-01",null,1);
$orders = zen_db_fetch_array($orders_query);
assertEqual ("orders query",count($orders),9);
// test items query
$items_query = create_items_query($orders['orders_id']);
$items = zen_db_fetch_array($items_query);
assertEqual ("items query",count($items),10);
echo "</table></body></html>";
if ($TEST_RESULT) {
echo "<p><b>All tests passed!</b> The app MyShop should run on your iPhone / iPod touch!</p>";
} else {
echo "<p><b style=\"color:red\">There were test failures!</b> Please visit <a href='http://appstore.malorny.net'>http://appstore.malorny.net</a> and contact the author if you can run the app MyShop!</p>";
}
}
function assertTrue($name,$success)
{
global $TEST_RESULT;
$style = $success ? "color:green" : "color:red";
$text = $success ? "OK" : "FAIL";
echo "<tr><td>$name ... </td><td></td><td style=\"$style\">$text</td></tr>\n";
if (!$success) $TEST_RESULT = false;
}
function assertEqual($name,$test,$expected)
{
global $TEST_RESULT;
$success = ($test == $expected);
$style = $success ? "color:green" : "color:red";
$text = $success ? "OK" : "FAIL";
echo "<tr><td>$name ... </td><td>$test</td><td style=\"$style\">$text</td></tr>\n";
if (!$success) $TEST_RESULT = false;
}
function assertVersion($name,$test,$expected)
{
global $TEST_RESULT;
$success = version_compare($test,$expected,">=");
$style = $success ? "color:green" : "color:red";
$text = $success ? "OK" : "FAIL";
echo "<tr><td>$name ... </td><td>$test</td><td style=\"$style\">$text</td></tr>\n";
if (!$success) $TEST_RESULT = false;
}
?>
If this is great to share I am happy to do so (cant help it seeing its here)
Bookmarks