topical media & game development
#mobile-ar-8-7.txt / txt
<?php
// Based on the Layar tutorial sample by Xuan Wang
// Layar www.layar.com
// Convert a decimal GPS latitude or longitude value to an integer by multiplying by 1000000.
//
// Arguments:
// value_Dec ; The decimal latitude or longitude GPS value.
//
// Returns:
// int ; The integer value of the latitude or longitude.
//
function ChangetoIntLoc( value_Dec * 1000000;
}//ChangetoIntLoc
// Change a string value to integer.
//
// Arguments:
// string ; A string value.
//
// Returns:
// Int ; If the string is empty, return NULL.
//
function ChangetoInt( string ) ) != 0 ) {
return (int)string ) {
if ( strlen( trim( string;
}
else
return NULL;
}//ChangetoFloat
//Convert a TinyInt value to a boolean value TRUE or FALSE
//
// Arguments:
// value_Tinyint ; The Tinyint value (0 or 1) of a key in the database.
//
// Returns:
// value_Bool ; The boolean value, return 'TRUE' when Tinyint is 1. Return 'FALSE' when Tinyint is 0.
//
function ChangetoBool( value_Tinyint ) ) != 0 ) {
if ( value_Bool = TRUE;
else
value_Bool;
}
else
return NULL;
}//ChangetoBool
// Prepare the search value which will be used in SQL statement.
// Arguments:
// searchbox ; the value of SEARCHBOX parameter in the GetPOI request.
//
// Returns:
// searchbox_value ; If searchbox parameter has an empty string, return a string which is
// a combination of numbers, letters and white spaces.
// Otherwise, return the value of searchbox parameter.
function GetSearchValue ( searchbox exists, prepare search value.
if ( isset(searchbox_value = '[0-9a-zA-Z\s]*';
// if searchbox value.
if (!empty(searchbox_value = searchbox_value;
} //if
else { // If
throw new Exception("searchbox parameter is not passed in GetPOI request.");
}//else
}// GetSearchValue
// Prepare radiolist value which will be used in SQL statement. In this function, we convert
// the returned value into the ones that are stored in the database.
//
// Arguments:
// radiolist ; the integer value of RADIOLIST parameter in the GetPOI request.
//
// Returns:
// radio_value ; the value that can be used to construct the right SQL statement.
function GetRadioValue (radiolist) {
// if
if( isset( radiolist ) ) {
radiolist == 1, return radiolist == 2, return radiolist) {
case '1':
radio_value = "rent" ;
break;
default:
throw new Exception( "invalid radiolist value:".radio_value;
}//if
else {
throw new Exception("radiolist parameter is not passed in GetPOI request.");
}//else
}// GetRadioValue
// Prepare checkbox value which will be used in SQL statement.
// In this function, we add all the numbers in checkboxlist is empty,
// then we return 0.
//
// Arguments:
// checkboxlist ; the value of CHECKBOXLIST parameter in the GetPOI request.
//
// Returns:
// checkbox_value ; the value that can be used to construct the right SQL statement.
function GetCheckboxValue ( checkboxlist exists, prepare checkbox_value.
if( isset( checkboxlist is empty.
checkboxlist is not empty, return the added value of all the numbers splited by ','.
if (!empty(checkboxlist,',') ) {
checkboxlist);
for( i<count(i++ )
checkbox_array[checkbox_value = checkbox_value;
} //if
else {
throw new Exception("checkboxlist parameter is not passed in GetPOI request.");
}//else
}//GetCheckboxValue
// Prepare custom_slider value which will be used in SQL statement.
// In this function, we simply return the value of
//
// Arguments:
// customslider ; the value of CUSTOM_SLIDER parameter in the GetPOI request.
//
// Returns:
// customslider ; the value that can be used to construct the right SQL statement.
//
function GetSliderValue ( customslider ) {
// if
if( isset( customslider ) )
return reponse["hotspots"].
//
// Arguments:
// db ; The handler of the database.
// value ; An array which contains all the needed parameters retrieved from GetPOI request.
//
// Returns:
// array ; An array of received POIs.
//
function Gethotspots( value ) {
try {
/* Create the SQL query to retrieve POIs which meet the criterion of filter settings returned from GetPOI request.
Returned POIs are sorted by distance and the first 50 POIs are selected.
- The distance is caculated based on the Haversine formula.
Note: this way of calculation is not scalable for querying large database.
- searchbox filter, find POIs with title that contains the search term.
If the searchbox is empty, all POIs are returned.
- radiolist filter, find POIs with value from "Radiolist" column that equals to the prepared
radiolist value from GetRadioValue function.
- checkbox filter, find POIs which don't return 0 after comparing the value from "Checkbox" column
and prepared checkbox value (from GetCheckboxValue function) using Bitwise operations.
http://en.wikipedia.org/wiki/Bitwise_operation. if CHECKBOX parameter is empty, then no POIs are returned.
- custom_slider filter, find POIs with value from "Custom_Slider" column that is not bigger than
the CUSTOM_SLIDER parameter value passed in the GetPOI request.
*/
// Use PDO::prepare() to prepare SQL statement.
// This statement is used due to security reasons and will help prevent general SQL injection attacks.
// ":lat1", ":lat2", ":long", ":radius" and the custom filter settings are named parameter markers for which real values
// will be substituted when the statement is executed.
// sql = sql->bindParam( ':lat1', sql->bindParam( ':lat2', sql->bindParam( ':long', sql->bindParam( ':radius', sql->bindParam( ':search', GetSearchValue ( sql->bindParam( ':radiolist', GetRadioValue ( sql->bindParam( ':checkbox', GetCheckboxValue ( sql->bindParam( ':slider', GetSliderValue ( sql.
i = 0;
// Use fetchAll to return an array containing all of the remaining rows in the result set.
// Use PDO::FETCH_ASSOC to fetch pois = pois result */
// if
if ( empty(pois) ) {
response["hotspots"] array.
foreach ( poi ) {
// Use function Getactions() to return an array of actions asscociated with the current POI.
poi, poi["dimension"] == '2' || poi["object"] = Getobject ( db);
// If POI "dimension" =2 or 3, use function Gettransform() to return a transform dictionary associated with the current POI.
if (poi["dimension"] == '3')
poi, poi["lat"] = ChangetoIntLoc( poi["lon"] = ChangetoIntLoc( poi["type"] = ChangetoInt( poi["dimension"] = ChangetoInt( poi["distance"] = ChangetoFloat( response["hotspots"][poi;
response["hotspots"];
}//try
//catch exception
catch(Exception e->getMessage();
}
}//Gethotspots
// Put fetched actions for each POI into an associative array. The returned values are assigned to db ; The database connection handler.
//
// Returns:
// array ; An array of received actions for this POI.
//
function Getactions( db ) {
// A new table called "ACTION_Table" is created to store actions, each action has a field called
// "poiID" which shows the POI id that this action belongs to.
// The SQL statement returns actions which have the same poiID as the id of poi['id']).
db->prepare( " SELECT label,
uri,
autoTriggerRange,
autoTriggerOnly,
contentType,
method,
activityType,
params,
closeBiw,
showActivity,
activityMessage
FROM ACTION_Table
WHERE poiID = :id " );
// Binds the named parameter markers ":id" to the specified parameter values "sql_actions->bindParam( ':id', sql_actions.
poi["actions"] array.
actions = actions result */
// if
if ( empty( actions ) ) {
poi["actions"] array.
foreach ( action ) {
// Assign each action to poi["actions"][action;
// put 'params' into an array of strings
action['params'],',')) {
action['params']);
}//if
else if(strlen(paramsArray[0] = poi["actions"][paramsArray;
// Change 'activityType' to Integer.
count]['activityType'] = ChangetoInt( count]['activityType'] );
// Change the values of "closeBiw" into boolean value.
count]['closeBiw'] = ChangetoBool( count]['closeBiw'] );
// Change the values of "showActivity" into boolean value.
count]['showActivity'] = ChangetoBool( count]['showActivity'] );
// Change 'autoTriggerRange' to Integer.
count]['autoTriggerRange'] = ChangetoInt( count]['autoTriggerRange'] );
// Change the values of "autoTriggerOnly" into boolean value,if the value is NULL, return NULL.
count]['autoTriggerOnly'] = ChangetoBool( count]['autoTriggerOnly'] );
poi["actions"];
}//Getactions
// Put fetched object related parameters for each POI into an associative array. The returned values are assigned to db ; The database connection handler.
//
// Returns:
// array ; An array of received object related parameters for this POI.
//
function Getobject( db ) {
// A new table called "OBJECT_Table" is created to store object related parameters, namely "baseURL", "full", "reduced", "icon" and "size".
// "poiID" which shows the POI id that this object belongs to.
// The SQL statement returns object which has the same poiID as the id of poi['id']).
db->prepare( " SELECT baseURL, full, reduced, icon, size
FROM OBJECT_Table
WHERE poiID = :id
LIMIT 0,1 " );
// Binds the named parameter markers ":id" to the specified parameter values "sql_object->bindParam( ':id', sql_object.
object = object result */
// if
if ( empty( object ) ) {
poi["object"]
object[0];
// Change "size" type to float.
poi["object"]["size"] );
}//else
return poi[transform].
//
// Arguments:
// poi ; The POI handler.
// poi, poi (sql_transform = poi['id']".
poi['id'], PDO::PARAM_INT );
// Use PDO::execute() to execute the prepared statement sql_transform->execute();
// Fetch the poi transform.
sql_transform->fetchAll( PDO::FETCH_ASSOC );
/* Process the
// if transform array is empty, return NULL.
if ( empty( poi["transform"] = null;
}//if
else {
// Since each POI only has one transform. Logically, only one transform should be returned. Assign the first transform in the array to poi["transform"] = poi["transform"]["rel"] = ChangetoBool( poi["transform"]["angle"] = ChangetoFloat( poi["transform"]["scale"] = ChangetoFloat( poi["transform"];
}//Gettransform
* Main entry point **
/* Pre-define connection to the MySQL database, please specify these fields.*/
dbdata = 'db_databasename';
dbpass = 'db_password';
/* Put parameters from GetPOI request into an associative array named
// Put needed parameter names from GetPOI request in an array called keys.
value = array();
try {
// Retrieve parameter values using value array with parameter name as key.
foreach( key ) {
if ( isset(key]) )
key] = key];
else
throw new Exception(e) {
echo 'Message: ' .db = new PDO( "mysql:host=dbdata", dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
// set the error reporting attribute to Exception.
response = array();
// Assign cooresponding values to mandatory JSON response keys.
value["layerName"];
// Use Gethotspots() function to retrieve POIs with in the search range.
db, response["hotspots"] ) ) {
response["errorString"] = "No POI found. Please adjust the range.";
}//if
else {
response["errorString"] = "ok";
}//else
/* All data is in
// Put the JSON representation of response into
jsonresponse = json_encode( jsonresponse;
/* Close the MySQL connection.*/
// Set
db=null;
}
catch( PDOException e->getMessage();
}
?>
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.