added template functions
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 9 Mar 2008 10:22:31 +0000 (10:22 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 9 Mar 2008 10:22:31 +0000 (10:22 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@105 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

doc/prog_odttemplate.html [new file with mode: 0644]
doc/prog_protocol.html
doc/prog_tickettemplate.html [new file with mode: 0644]
www/inc/db/db.php
www/inc/db/db_mysql.php
www/inc/db/db_scheme.php
www/inc/machine/template.php [new file with mode: 0644]
www/machine.php

diff --git a/doc/prog_odttemplate.html b/doc/prog_odttemplate.html
new file mode 100644 (file)
index 0000000..cf2ca19
--- /dev/null
@@ -0,0 +1,7 @@
+<html>
+<title>Magic Smoke ODT Templates</title>
+<body>
+<h1>Magic Smoke ODT Templates</h1>
+
+
+</html>
\ No newline at end of file
index b6f2e7b..7202895 100644 (file)
@@ -359,3 +359,38 @@ The <tt>deletehost</tt> transaction deletes one single host. The request contain
 
 FIXME: special hosts (beginning with "_") return errors. Non-existing hosts are silently ignored.
 
+<h2>Customers</h2>
+
+<h2>Orders and Sales</h2>
+
+<h2>Templates</h2>
+
+Templates are used for printouts and documents. There are several types of templates:<p>
+
+<ul>
+<li><a href="prog_odttemplate.html">Open Document Text Template</a></li>
+<li><a href="prog_tickettemplate.html">XML Ticket printer template</a></li>
+</ul><p>
+
+Filenames of templates are restricted to the regular expression <tt>^[a-z0-9_.]+$</tt> and are case-insensitive.
+
+<h3>List of Templates</h3>
+
+The <tt>gettemplatelist</tt> transaction returns an informational list of templates. The request is empty, the response looks like:<p>
+
+<pre>
+&lt;TList>
+  &lt;Template name="filename" hash="md5-hash"/>
+  ...
+&lt;/TList>
+</pre>
+
+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).
+
+<h3>Getting a Template</h3>
+
+The <tt>gettemplate</tt> 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.
+
+</h3>Setting a Template</h3>
+
+The <tt>settemplate</tt> 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 (file)
index 0000000..74182e5
--- /dev/null
@@ -0,0 +1,7 @@
+<html>
+<title>Magic Smoke Ticket Templates</title>
+<body>
+<h1>Magic Smoke Ticket Templates</h1>
+
+
+</html>
\ No newline at end of file
index 07d9fc2..66b7bc7 100644 (file)
@@ -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)
        {
index 6365ca9..8b23ce8 100644 (file)
@@ -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
index aaa81fb..ccbf9bb 100644 (file)
@@ -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 (file)
index 0000000..0239af3
--- /dev/null
@@ -0,0 +1,68 @@
+<?
+//
+// PHP Implementation: template
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2008
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+function getTemplateList()
+{
+       global $db;
+       header("X-MagicSmoke-Status: Ok");
+       print("<TList>\n");
+       $res=$db->select("template","filename,hash");
+       for($i=0;$i<count($res);$i++){
+               //it is safe to do this since setTemplate checks syntax:
+               print("<Template name=\"".$res[$i]["filename"]."\" hash=\"".$res[$i]["hash"]."\"/>\n");
+       }
+       print("</TList>\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
index fecdf9e..0c21d37 100644 (file)
@@ -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."));