This script is the one that loads contents in the designer module in HP WallArt.
Requests can be done with the three standard call methods: GET | POST | PUT.
We will use mysql.class.php to communicate in a reliable manner with our mysql database.
//Load init.php file and connection class for MySql bbdd require_once '../core/init.php'; require_once '../core/mysql.class.php'; //create a function to capture the headers, because not all facilities have implemented the php function getallheaders if (!function_exists("getallheaders")) { function getallheaders_manual() { $result = array(); foreach($_SERVER as $key => $value) { if (substr($key, 0, 5) == "HTTP_") { $key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5))))); $result[$key] = $value; } else { $result[$key] = $value; } } return $result; } } //call the function to capture the headers $headers = getallheaders_manual(); //we seek project data $data = explode(':', $headers['Authorization']); $data2 = explode(',', $data[0]); foreach ($data2 as $val) { $valor = explode('=', $val); $header2[trim($valor[0])] = str_replace('"', '', trim($valor[1])); } //recover the value of oauth_token oauth_server_token $db = new db; $q = " SELECT * FROM oauth_server_token WHERE ost_token = '".$header2['oauth_token']."' "; $db->query($q); $db->next_record(); //get id of the project $project_id = $db->f('ost_usa_pro_ref'); //load the data back from the current project $db = new db; $q = " SELECT * FROM proyectos WHERE id = '".$project_id."' "; $db->query($q); $db->next_record(); //define a price per square meter $nuestro_precio = '9.90'; //if we set a price per square meter, we have to pass it to price in euros per square foot $precio_base = $nuestro_precio/10.7639; //create array of background images (scenes) with the values of the project: scene1, scene2 ... We assume that we have projects in the table for each project saved the URL of each background image for($i = 1; $i <= count('numero de escenas'); $i++) { if($db->f('scene'.$i) != '') $escenas[$i] = $db->f('scene'.$i); } $scenes = array(); //load the array then send the designer with all the background images or scenes related to the project foreach($escenas as $key => $value) { //dimensions of the image $imagesize = getimagesize($value); $escena["id"] = $key; $escena["image_width"] = $imagesize[0]; $escena["image_height"] = $imagesize[1]; $escena["flippable_x"] = true; $escena["image_path"] = $value; $escena["elements_ids"] = array(); $escena["thumbnail_path"] = $value; $escena["thumbnail_height"] = $imagesize[1]/5; $escena["thumbnail_width"] = $imagesize[0]/5; $escena["printable_asset_free_for_samples"] = true; $escena["printable_image_width"] = 0; $escena["printable_image_height"] = 0; //add element to array $scenes[] = $escena; } //create array of objects (elements) with the values of the project: element1, element2 ... We assume that we have projects in the table for each project saved the URL of each element for($i = 1; $i <= count('numero de elementos'); $i++) { if($db->f('element'.$i) != '') $objetos[$i] = $db->f('element'.$i); } $elements = array(); //load the array then send the designer with all objects or elements associated to the project foreach($objetos as $key => $value) { //dimensions of the image $imagesize = getimagesize($value); $objeto["id"] = $key; $objeto["type"] = "Element"; $objeto["default_height"] = 75; $objeto["width"] = $imagesize[0]; $objeto["height"] = $imagesize[1]; $objeto["flippable_x"] = true; $objeto["has_cutline"] = false; $objeto["image_path"] = $value; $objeto["thumbnail_path"] = $value; $objeto["rotatable"] = true; $objeto["printable_asset_free_for_samples"] = true; $objeto["thumbnail_height"] = $imagesize[1]/5; $objeto["thumbnail_width"] = $imagesize[0]/5; $objeto["image_height"] = $imagesize[1]; $objeto["image_width"] = $imagesize[0]; //add element to array $elements[] = $objeto; } //save in a variable the required path $request_path = $_SERVER['PATH_INFO']; //case 1: Themes if ($request_path == '/themes') { //we will load a single theme that includes the elements of the project. But we could send more items to be loaded while the current project. $themes = array( 0 => array( "id" => 1, "name" => "Theme name", "internal_name" => "my e-commerce name", "tooltip" => "SAMPLE_FILES_TOOLTIP", "thumbnail_path" => "http://www.mydomain.com/images/mylogo.jpg", "thumbnail_width" => 75, "thumbnail_height"=> 75, "cutout_element_ids" => array(), "photo_upload_support" => true, "rps_theme_id" => "", "price" => array( "msrp" => array( "base" => 0, "per_sqft" => $precio_base, "currency" => "EUR"), "your_price" => array( "base" => 0, "per_sqft" => $precio_base, "currency" => "EUR") ) ) ); echo json_encode($themes); exit; } //case 2: A particular theme. We sent information elseif(preg_match('/\/themes\/(\d+)$/', $request_path, $matches)) { //capture the id of the theme from the URL itself received $theme_id = $matches[1]; $themes = array( "id" => $theme_id, "name" => "Theme name", "internal_name" => "my e-commerce name", "tooltip" => "SAMPLE_FILES_TOOLTIP", "thumbnail_path" => "http://www.mydomain.com/images/mylogo.jpg", "thumbnail_width" => 75, "thumbnail_height" => 75, "cutout_element_ids" => array(), "photo_upload_support" => true, "rps_theme_id" => "", "price" => array( "msrp" => array( "base" => 0, "per_sqft" => $precio_base, "currency" => "EUR"), "your_price" => array( "base" => 0, "per_sqft" => $precio_base, "currency" => "EUR") ) ); echo json_encode($themes); exit; } //case 3: background images or scenes of our theme elseif(preg_match('/\/themes\/(\d+)\/scenes$/', $request_path, $matches)) { echo json_encode($scenes); exit; } //case 4: Back data from a particular background elseif(preg_match('/\/themes\/(\d+)\/scenes\/(\d+)$/', $request_path, $matches)) { foreach($scenes as $key => $value) { if($key == $matches[2]) { $scena = $value; } } echo json_encode($scena); exit; } //case 5: path of the background image specifically requested elseif(preg_match('/\/themes\/(\d+)\/scenes\/(\d+)\/printable_image_path$/', $request_path, $matches)) { foreach($scenes as $key => $value) { if($key == $matches[2]) { $printable = $value["image_path"]; } } echo json_encode($printable); exit; } //case 6: All objects in the theme specifically requested elseif(preg_match('/\/themes\/(\d+)\/elements$/', $request_path, $matches)) { echo json_encode($elements); exit; } //case 7: Back data from a particular object elseif(preg_match('/\/themes\/(\d+)\/elements\/(\d+)$/', $request_path, $matches)) { foreach($elements as $key => $value) { if($key == $matches[2]) { $element = $value; } } echo json_encode($element); exit; } //case 8: path of the object image specifically requested elseif(preg_match('/\/themes\/(\d+)\/elements\/(\d+)\/printable_image_path$/', $request_path, $matches)) { foreach($elements as $key => $value) { if($key == $matches[2]) { $printable = $value["image_path"]; } } echo json_encode($printable); exit; } //error control else { header('HTTP/1.1 500 Internal Server Error'); header('Content-Type: text/plain'); echo "Unknown request"; }