busyTimeout(300000); // 5 min. is a lot but who knows how busy we are; apart from that people don't notice as things run in background :) } catch (Exception $ex) { // if not supported then can't set it but it's not a break of the leg ;) } $ShoppingListKey = null; $BrowserId = null; function readGlobalVars() { global $db, $ShoppingListKey, $BrowserId; $BrowserId=$_POST['BrowserId']; $ShoppingListName=$_POST['ShoppingListName']; $ShoppingListKey = $db->querySingle("SELECT key FROM shoppinglist where name='" . SQLite3::escapeString($ShoppingListName) . "'"); return($ShoppingListKey); } function BrandBrowser() { global $db, $ShoppingListKey, $BrowserId; $ShoppingListName=$_POST['ShoppingListName']; $res=true; $ShoppingListKey = readGlobalVars(); if (null == $ShoppingListKey ) { $res=$db->exec("insert into shoppinglist (name) values ('" . SQLite3::escapeString($ShoppingListName) . "')"); $ShoppingListKey = $db->lastInsertRowID(); } echo "{ \"Result\":\"" . ($res==true?"True":"False") . "\", \"TransferQKey\":\"" . $_POST['TransferQKey'] . "\" }"; } function DeleteShop(){ global $db, $ShoppingListKey, $BrowserId; $shopGlobalId = $_POST['GlobalId']; $sql="update entry set BrowserId = '" . SQLite3::escapeString($BrowserId) . "', status='d' where shoppingListKey=$ShoppingListKey and shopKey in (select key from Shop where globalId='" . SQLite3::escapeString($shopGlobalId) . "') "; $res=$db->exec($sql); if ($res == true) { $res=$db->exec("update shop set BrowserId = '" . SQLite3::escapeString($BrowserId) . "', status='d' where shoppingListKey=" . $ShoppingListKey . " and globalId='" . SQLite3::escapeString($shopGlobalId) . "' "); } echo "{ \"Result\":\"" . ($res==true?"True":"False") . "\", \"TransferQKey\":\"" . $_POST['TransferQKey'] . "\" }"; } function ReceiveShop() { global $db, $ShoppingListKey, $BrowserId; $description = $_POST['Description']; $shopGlobalId = $_POST['GlobalId']; $status = isset($_POST['value']) ? $_POST['value'] : "a"; if ($status != "d") { $status = "a"; } $res=true; $shopKey = $db->querySingle("Select key from shop where globalId='" . SQLite3::escapeString($shopGlobalId) . "'"); if (null == $shopKey) { $res=$db->exec("insert into Shop (status,shoppinglistKey,BrowserId,globalId,Description) values ('a'," . $ShoppingListKey . ", '" . $BrowserId . "','" . SQLite3::escapeString($shopGlobalId) . "','" . SQLite3::escapeString($description) . "')"); $shopKey = $db->lastInsertRowID(); } else { $res=$db->exec("update Shop set status='" . SQLite3::escapeString($status) . "', BrowserId='" . $BrowserId . "', Description = '" . SQLite3::escapeString($description) . "' where key=" . $shopKey); } echo "{ \"Result\":\"" . ($res==true?"True":"False") . "\", \"TransferQKey\":\"" . $_POST['TransferQKey'] . "\", \"ShopKey\":\"$shopKey\" }"; } function ReceiveEntry() { global $db, $ShoppingListKey, $BrowserId; $description = $_POST['Description']; $shopGlobalId = $_POST['shopGlobalId']; $res = true; $shopKey = $db->querySingle("Select key from shop where shoppinglistkey=" . $ShoppingListKey . " and globalId='" . SQLite3::escapeString($shopGlobalId) . "'"); if (null == $shopKey) { // normally the shop should arrive first, but it's not guaranteed.... if it doesn't the create a dummy shop... $res=$db->exec("insert into Shop (status,shoppinglistkey,BrowserId,globalId,Description) values ('a'," . $ShoppingListKey . ", '" . SQLite3::escapeString($BrowserId) . "','" . SQLite3::escapeString($shopGlobalId) . "','Default')"); $shopKey = $db->lastInsertRowID(); } if ($res==true) { $EntryKey = $db->querySingle("SELECT key FROM Entry where Description='" . SQLite3::escapeString($description) . "' and ShoppingListKey=" . $ShoppingListKey . " and shopKey=" . $shopKey); if (null == $EntryKey) { $res=$db->exec("insert into Entry (Status,ShoppingListKey,BrowserId,shopKey,Description) values ('a'," . $ShoppingListKey . ",'" . SQLite3::escapeString($BrowserId) . "'," . $shopKey . ",'" . SQLite3::escapeString($description) . "')"); $EntryKey = $db->lastInsertRowID(); } else { $res=$db->exec("update Entry set status='a', BrowserId='" . $BrowserId . "', Description = '" . SQLite3::escapeString($description) . "' where key=" . $EntryKey); } } echo "{ \"Result\":\"" . ($res==true?"True":"False") . "\", \"TransferQKey\":\"" . $_POST['TransferQKey'] . "\", \"EntryKey\":\"$EntryKey\" }"; } function SendChangedData() { global $db, $ShoppingListKey, $BrowserId; $maxModified = $_POST['maxModified']; $sleepTimer=5; // sleep 5 seconds per Iteration $numIterations=0; $Data=null; while ($Data == null && ($numIterations++ * $sleepTimer)<100){ // do a roundtrip all 100 seconds. $Data=getModifiedShopData($maxModified["Shop"]); if ($Data==null) { $Data=getModifiedEntryData($maxModified["Entry"] ); } if ($Data != null) { echo $Data; } else { sleep($sleepTimer); } } if (null == $Data) { echo "{ \"ChangeType\":\"None\", \"Result\":\"True\" }"; } } function getModifiedShopData($maxModified) { global $db, $ShoppingListKey, $BrowserId; $sAddCond=""; // For initial downloads do not send deleted records. if ($maxModified == 0) { $sAddCond=" and Shop.status != 'd' "; } $allShops = null; $retVal = null; $ojShops=null; $sql="SELECT shop.status, shop.modified, shop.key, shop.globalId, shop.Description FROM shop where shop.shoppinglistKey=" . $ShoppingListKey . " and shop.browserId != '" . SQLite3::escapeString($BrowserId) . "' and shop.modified>" . SQLite3::escapeString($maxModified) . "" . $sAddCond . " Order by shop.modified"; $allShops = $db->query($sql); while ($theShop = $allShops->fetchArray()) { if (null==$ojShops) { $ojShops=", \"Shops\":["; } $ojShops .= "{ \"ShopKey\":\"" . $theShop["key"] . "\", \"Modified\":\"" . $theShop["modified"] . "\", \"Status\":\"" . $theShop["status"] . "\", \"GlobalId\":" . json_encode($theShop["globalId"]) . ", \"Description\":" . json_encode($theShop["Description"]) . " },"; } if (null!=$ojShops) { $ojShops = rtrim($ojShops,",") . "]"; } else { $ojShops=""; } if ($ojShops!=null) { $retVal= "{ \"ChangeType\":\"Shop\", \"Result\":\"" . ($allShops==true?"True":"False") . "\" " . $ojShops . " }"; } return($retVal); } function getModifiedEntryData($maxModified) { global $db, $ShoppingListKey, $BrowserId; $sAddCond=""; // For initial downloads do not send deleted records. if ($maxModified == 0) { $sAddCond=" and Entry.status != 'd' "; } $allEntries = null; $retVal = null; $ojEntries=null; $allEntries = $db->query("SELECT entry.status, entry.modified, entry.key, entry.Description, shop.globalId FROM entry,shop where entry.shoppingListKey=" . $ShoppingListKey . " and shop.key=entry.shopKey and entry.BrowserId != '" . SQLite3::escapeString($BrowserId) . "' and entry.modified>" . SQLite3::escapeString($maxModified) . "" . $sAddCond . " Order by entry.modified"); while ($theEntry = $allEntries->fetchArray()) { if (null==$ojEntries) { $ojEntries=", \"Entries\":["; } $ojEntries .= "{ \"EntryKey\":\"" . $theEntry["key"] . "\", \"Modified\":\"" . $theEntry["modified"] . "\", \"Status\":\"" . $theEntry["status"] . "\", \"Description\":" . json_encode($theEntry["Description"]) . ", \"shopGlobalId\":" . json_encode($theEntry["globalId"]) . " },"; } if (null!=$ojEntries) { $ojEntries = rtrim($ojEntries,",") . "]"; } else { $ojEntries=""; } if ($ojEntries!=null) { $retVal= "{ \"ChangeType\":\"Entry\", \"Result\":\"" . ($allEntries==true?"True":"False") . "\" " . $ojEntries . " }"; } return($retVal); } function DeleteEntry(){ global $db, $ShoppingListKey, $BrowserId; $Description = $_POST['Description']; $shopGlobalId = null; if (!empty($_POST['shopGlobalId'])) { $shopGlobalId = $_POST['shopGlobalId']; } $res = true; if ($res==true) { $sql="update entry set BrowserId = '" . SQLite3::escapeString($BrowserId) . "', status='d' where shoppingListKey=$ShoppingListKey and Description='" . SQLite3::escapeString($Description) . "'"; if (null != $shopGlobalId) { $sql = $sql . " and shopKey = (select key from shop where globalId='" . SQLite3::escapeString($shopGlobalId) . "')"; } $res=$db->exec($sql); } echo "{ \"Result\":\"" . ($res==true?"True":"False") . "\", \"TransferQKey\":\"" . $_POST['TransferQKey'] . "\" }"; } readGlobalVars(); if(isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; switch($action) { case 'BrandBrowser' : BrandBrowser() ; break; case 'ReceiveEntry' : ReceiveEntry() ; break; case 'ReceiveShop' : ReceiveShop() ; break; case 'SendChangedData' : SendChangedData() ; break; case 'DeleteEntry' : DeleteEntry() ; break; case 'DeleteShop' : DeleteShop() ; break; } } $db->close(); ?>