From 0072fbefbe6021f5d2611ecb79b13d6955991b4f Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 9 Mar 2008 10:22:31 +0000 Subject: [PATCH] added template functions git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@105 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- doc/prog_odttemplate.html | 7 ++++ doc/prog_protocol.html | 35 +++++++++++++++++++++ doc/prog_tickettemplate.html | 7 ++++ www/inc/db/db.php | 13 ++++++++ www/inc/db/db_mysql.php | 8 +++++ www/inc/db/db_scheme.php | 26 ++++++++++++++-- www/inc/machine/template.php | 68 ++++++++++++++++++++++++++++++++++++++++++ www/machine.php | 22 +++++++++++++- 8 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 doc/prog_odttemplate.html create mode 100644 doc/prog_tickettemplate.html create mode 100644 www/inc/machine/template.php diff --git a/doc/prog_odttemplate.html b/doc/prog_odttemplate.html new file mode 100644 index 0000000..cf2ca19 --- /dev/null +++ b/doc/prog_odttemplate.html @@ -0,0 +1,7 @@ + +Magic Smoke ODT Templates + +

Magic Smoke ODT Templates

+ + + \ No newline at end of file diff --git a/doc/prog_protocol.html b/doc/prog_protocol.html index b6f2e7b..7202895 100644 --- a/doc/prog_protocol.html +++ b/doc/prog_protocol.html @@ -359,3 +359,38 @@ The deletehost transaction deletes one single host. The request contain FIXME: special hosts (beginning with "_") return errors. Non-existing hosts are silently ignored. +

Customers

+ +

Orders and Sales

+ +

Templates

+ +Templates are used for printouts and documents. There are several types of templates:

+ +

+ +Filenames of templates are restricted to the regular expression ^[a-z0-9_.]+$ and are case-insensitive. + +

List of Templates

+ +The gettemplatelist transaction returns an informational list of templates. The request is empty, the response looks like:

+ +

+<TList>
+  <Template name="filename" hash="md5-hash"/>
+  ...
+</TList>
+
+ +The Hash is caculated at the server side and can be used to find out whether the template has been changed. It does not necessarily need to be MD5 or any other specific algorithm, it can be any kind of ASCII string that shows changes (hence the client must remember the string if it wants to use it). + +

Getting a Template

+ +The gettemplate transaction returns a specific template. The request contains only the file name, the response contains the template file as content. In case of an error the content is left empty and the status code is set appropriately. + +Setting a Template + +The settemplate transaction creates a new template file. The request contains the file name terminated with a newline ("\n") and then the binary content of the template - it is important that there are no additional characters between the newline and the template content. The response contains the new hash of the template or error details. \ No newline at end of file diff --git a/doc/prog_tickettemplate.html b/doc/prog_tickettemplate.html new file mode 100644 index 0000000..74182e5 --- /dev/null +++ b/doc/prog_tickettemplate.html @@ -0,0 +1,7 @@ + +Magic Smoke Ticket Templates + +

Magic Smoke Ticket Templates

+ + + \ No newline at end of file diff --git a/www/inc/db/db.php b/www/inc/db/db.php index 07d9fc2..66b7bc7 100644 --- a/www/inc/db/db.php +++ b/www/inc/db/db.php @@ -167,6 +167,9 @@ abstract class DbEngine if($dbScheme->isStringColumn($table,$k)) $val.=$this->escapeString($v); else + if($dbScheme->isBlobColumn($table,$k)) + $val.=$this->escapeBlob($v); + else //don't know how to escape it... $val.="NULL"; } @@ -199,6 +202,9 @@ abstract class DbEngine if($dbScheme->isStringColumn($table,$k)) $ret.=$this->escapeString($v); else + if($dbScheme->isBlobColumn($table,$k)) + $ret.=$this->escapeBlob($v); + else //don't know how to escape it... $ret.="NULL"; } @@ -221,6 +227,13 @@ abstract class DbEngine return "'".addslashes($s)."'"; } + /**escapes blobs; the default uses addslashes and encloses the value in ''*/ + public function escapeBlob($s) + { + if($s === false) return "NULL"; + return "'".addslashes($s)."'"; + } + /**returns a configuration setting*/ public function getConfig($key) { diff --git a/www/inc/db/db_mysql.php b/www/inc/db/db_mysql.php index 6365ca9..8b23ce8 100644 --- a/www/inc/db/db_mysql.php +++ b/www/inc/db/db_mysql.php @@ -104,6 +104,7 @@ class MysqlEngine extends DbEngine if($type=="seq32")return "INT AUTO_INCREMENT"; if($type=="seq64")return "BIGINT AUTO_INCREMENT"; if($type=="text")return "TEXT"; + if($type=="blob")return "MEDIUMBLOB"; //max.16MB of blob $tpa=explode(":",$type); if($tpa[0]=="string"){ if(isset($tpa[1])) @@ -168,4 +169,11 @@ class MysqlEngine extends DbEngine if($s === false) return "NULL"; return "'".mysqli_real_escape_string($this->dbhdl,$s)."'"; } + + /**escapes blobs; it uses mysqli_escape_string and encloses the value in '' - blobs are binary strings in MySQL*/ + public function escapeBlob($s) + { + if($s === false) return "NULL"; + return "'".mysqli_real_escape_string($this->dbhdl,$s)."'"; + } }; \ No newline at end of file diff --git a/www/inc/db/db_scheme.php b/www/inc/db/db_scheme.php index aaa81fb..ccbf9bb 100644 --- a/www/inc/db/db_scheme.php +++ b/www/inc/db/db_scheme.php @@ -46,6 +46,12 @@ class DbScheme { // this needs to change to 64-bit int in 2038 "timeout"=>array("int32","notnull") ); + //templates + $this->scheme["template"]=array( + "filename" => array("string","primarykey"), + "content" => array("blob"), + "hash" => array("string:32","notnull") //md5 + ); // ////////////////////// @@ -80,7 +86,7 @@ class DbScheme { "customerid" => array("seq32","primarykey"), //contact data "name" => array("string","notnull"), - "address" => array("string"), + "address" => array("text"), "contact" => array("string"),//phone or something "comments" => array("text"), //online login data @@ -95,7 +101,7 @@ class DbScheme { //seller (_online for web forms) "soldby" => array("string:64","foreignkey:users:uname"), //if not null/empty: this address for delivery, customer address for invoice - "deliveryaddress" => array("string"), + "deliveryaddress" => array("text"), //if not null/empty: lodge/deposit the tickets at a seller with _deposit flag "depositat" => array("string:64","foreignkey:users:uname"), //status, see ORDER_* constants @@ -146,7 +152,7 @@ class DbScheme { //when the cart expires "timeout" => array("int32","notnull"), //shipping address during order process - "shippingaddress" => array("string") + "shippingaddress" => array("text") ); //buying tickets $this->scheme["cart_ticket"]=array( @@ -255,6 +261,20 @@ class DbScheme { return false; } } + + /**returns true if the given column is of a string type*/ + public function isBlobColumn($tab,$col) + { + if(!isset($this->scheme[$tab][$col])) + return false; + $tpa=explode(":",$this->scheme[$tab][$col][0]); + switch($tpa[0]){ + case "blob": + return true; + default: + return false; + } + } }; $dbScheme=new DbScheme; ?> \ No newline at end of file diff --git a/www/inc/machine/template.php b/www/inc/machine/template.php new file mode 100644 index 0000000..0239af3 --- /dev/null +++ b/www/inc/machine/template.php @@ -0,0 +1,68 @@ +, (C) 2008 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +function getTemplateList() +{ + global $db; + header("X-MagicSmoke-Status: Ok"); + print("\n"); + $res=$db->select("template","filename,hash"); + for($i=0;$i\n"); + } + print("\n"); +} + +function getTemplate($fname) +{ + global $db; + $res=$db->select("template","content","filename=".$db->escapeString($fname)); + if(count($res)==0){ + header("X-MagicSmoke-Status: Error"); + die(tr("Template File not found in database")); + } + header("X-MagicSmoke-Status: Ok"); + print($res[0]["content"]); +} + +function setTemplate($data) +{ + $pos=strpos($data,'\n'); + if($pos===false){ + header("X-MagicSmoke-Status: Error"); + die(tr("Unable to find file name")); + } + //split + $fname=strtolower(trim(substr($data,0,$pos))); + $data=substr($data,$pos+1); + //syntax check + if(ereg("^[a-z0-9_\\.]+$",$fname)===false){ + header("X-MagicSmoke-Status: Error"); + die(tr("Illegal File Name")); + } + //store + $hash=md5($data); + global $db; + $db->beginTransaction(); + $res=$db->select("template","filename","filename=".$db->escapeString($fname)); + if(count($res)==1){ + $db->update("template",array("content"=>$data,"hash"=>$hash),"filename=".$db->escapeString($fname)); + }else{ + $db->insert("template",array("content"=>$data,"hash"=>$hash,"filename"=>$fname)); + } + $db->commitTransaction(); + header("X-MagicSmoke-Status: Ok"); +} + +?> \ No newline at end of file diff --git a/www/machine.php b/www/machine.php index fecdf9e..0c21d37 100644 --- a/www/machine.php +++ b/www/machine.php @@ -22,7 +22,9 @@ $ALLOWEDREQUESTS=array( tr("gethosts"),tr("sethost"),tr("addhost"),tr("deletehost"), //host management tr("geteventlist"),tr("geteventdata"),tr("seteventdata"), //event infos tr("getroomdata"),tr("setroomdata"),//room infos - tr("getcustomerlist") //customer info + tr("getcustomerlist"), //customer info + tr("createorder"),tr("createsale"), //sell/order stuff + tr("gettemplatelist"),tr("gettemplate"),tr("settemplate") //templates ); /**contains the low-level request name from the client*/ $SMOKEREQUEST=strtolower($_SERVER["HTTP_X_MAGICSMOKE_REQUEST"]); @@ -52,6 +54,7 @@ include("inc/loader_nonadmin.php"); //load machine interface include("inc/machine/session.php"); include("inc/machine/host.php"); +include("inc/machine/template.php"); // request to start a session if($SMOKEREQUEST=="startsession"){ @@ -216,6 +219,23 @@ if($SMOKEREQUEST=="deletehost"){ } +//return list of templates +if($SMOKEREQUEST=="gettemplatelist"){ + getTemplateList(); + exit(); +} +//get specific template +if($SMOKEREQUEST=="gettemplate"){ + getTemplate(trim($REQUESTDATA)); + exit(); +} +//set a specific template +if($SMOKEREQUEST=="settemplate"){ + setTemplate($REQUESTDATA); + exit(); +} + + //EOF header("X-MagicSmoke-Status: Error"); die(tr("Internal Error: unknown command, hiccup in code structure.")); -- 1.7.2.5