more work on web iface for customers
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 16 Jan 2011 21:30:39 +0000 (21:30 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 16 Jan 2011 21:30:39 +0000 (21:30 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@707 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

wob/classes/cart.wolf
wob/db/cart.wolf
www/inc/classes/autoload.php
www/inc/classes/websession.php [deleted file]
www/inc/rendering/cart_listing.php
www/inc/wext/autoload.php
www/inc/wext/websession.php [new file with mode: 0644]
www/index.php
www/template/en/layout.html

index 5b66c37..6553e48 100644 (file)
                        </Map>
                </Mapping>
        </Class>
-</Wolf>
\ No newline at end of file
+       
+       <Class name="WebSession">
+               <Abstract lang="php"/>
+               <Property name="sessionid" type="string">Cookie for this session</Property>
+               <Property name="customerid" type="int">ID of the customer using this session or null</Property>
+               <Property name="customer" type="Customer">customer object or null</Property>
+               <Property name="timeout" type="int64">time at which this cookie is deleted</Property>
+               <Mapping table="websession">
+                       <Map property="sessionid"/>
+                       <Map property="customerid"/>
+                       <Map property="timeout"/>
+                       <Map property="customer">
+                               <Call lang="php" method="WOCustomer::fromTablecustomer(WTcustomer::getFromDB($table->customerid))"/>
+                       </Map>
+               </Mapping>
+       </Class>
+</Wolf>
index f77fda3..24354bb 100644 (file)
        </Table>
        
        <Table name="websession" backup="no">
-               <Column name="sessionid" type="string:64" primarykey="yes"/>
+               <Column name="sessionid" type="string:64" primarykey="yes">
+                       <Call lang="php" method="WOWebSession::getNewSessionId()"/>
+               </Column>
                <!--/customer-->
-               <Column name="customerid" type="int32" notnull="yes" foreignkey="customer:customerid"/>
+               <Column name="customerid" type="int32" null="yes" foreignkey="customer:customerid"/>
                <!--unix timestamp at which to delete this session-->
                <Column name="timeout" type="int64" notnull="yes"/>
        </Table>
index 4c520c1..84f0a55 100644 (file)
@@ -6,6 +6,5 @@
 
 wob_autoclass('LanguageManager','./inc/classes/language_manager.php');
 wob_autoclass('ConfigManager','./inc/classes/config_manager.php');
-wob_autoclass('Websession','./inc/classes/websession.php');
 wob_autoclass('BaseVars','./inc/classes/basevars.php');
 ?>
\ No newline at end of file
diff --git a/www/inc/classes/websession.php b/www/inc/classes/websession.php
deleted file mode 100644 (file)
index be48f98..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-<?
-// (c) Konrad Rosenbaum, 2007-2011
-// (c) Peter Keller, 2007/8
-// protected under the GNU AGPL version 3 or at your option any newer
-// see COPYING.AGPL
-
-define("COOKIE_WEBSESSION", "msmoke_session");
-
-/** creates a web session to authorize a customer */
-class Websession {
-       
-       private $loggedin;
-       private $customerid;
-       private $sessionid;
-       
-       public function __construct()
-       {
-               global $_COOKIE;
-               global $db;
-               
-               //prune session table
-               $db->deleteRows("websession", "timeout < ".time());
-               
-               //default:
-               $this->loggedin=false;
-               $this->customerid=null;
-               
-               //check cookie
-               if(isset($_COOKIE[COOKIE_WEBSESSION])){
-                       $res = $db->select("websession", "*", "sessionid=".$db->escapeString($_COOKIE[COOKIE_WEBSESSION]));
-                       if (count($res) > 0){
-                               $uid = $res[0]["customerid"];
-                               $this->sessionid = $res[0]["sessionid"];
-                               
-                               $customer = new Customer($uid);
-                               if ($customer->exists()) {
-                                       $this->customerid = $customer->getID();
-                                       $this->loggedin = true;
-                               } else {
-                                       $this->destroySession();
-                               }
-                       }
-               }
-       }
-       
-       /** checks if the customer is authorized */
-       public function isAuthorized()
-       {
-               return $this->loggedin;
-       }
-       
-       /** returns the current customer */
-       public function getCustomer()
-       {
-               if ($this->loggedin)
-                       return new Customer($this->customerid);
-               else
-                       return false;
-       }
-       
-       /** creates a new session for the customer */
-       public function createSession($customerId)
-       {
-               global $db;
-               global $WebSessionTimeout;
-       
-               $this->destroySession();
-               
-               $customer = new Customer($customerId);
-               
-               // only if customer exists, create session
-               if ($customer->exists()) {
-               
-                       //create session and set cookie
-                       do{
-                               $sessionid = getRandom(128);
-                               $res = $db->select("websession", "*", "sessionid=".$db->escapeString($sessionid));
-                               $again = count($res) > 0;
-                       } while ($again);
-
-                       $timeout=time()+$WebSessionTimeout;
-                       $db->insert("websession", array("sessionid"=>$sessionid, "customerid"=>$customer->getID(), "timeout"=>$timeout));
-                       
-                       setcookie(COOKIE_WEBSESSION, $sessionid, $timeout);
-                       
-                       $this->loggedin = true;
-                       $this->customerid = $customer->getID();
-               }
-       }
-       
-       /** destroys the current session */
-       public function destroySession()
-       {
-               global $db;
-               
-               if($this->loggedin){
-                       $db->deleteRows("websession", "sessionid=".$db->escapeString($this->sessionid));
-               }
-
-               setcookie(COOKIE_WEBSESSION, "", 1);
-       }
-       
-       /** logs the customer out */
-       public function logout()
-       {
-               $this->destroySession();
-               header("Location: index.php");
-               exit();
-       }
-};
-?>
\ No newline at end of file
index 8d8d146..f319c71 100644 (file)
@@ -88,8 +88,25 @@ static public function removeItem()
        redirectHome(array("mode"=>"cart","cartid"=>$cartid));
 }
 
+///clean up old carts
+static protected function cleanupDb()
+{
+       global $db;
+       $res=$db->select("cart","cartid","timeout < ".time());
+       foreach($res as $row){
+               $where="cartid = ".$db->escapeString($row['cartid']);
+               $db->deleteRows("cartticket",$where);
+               $db->deleteRows("cartvoucher",$where);
+               $db->deleteRows("cartitem",$where);
+               $db->deleteRows("cart",$where);
+       }
+}
+
 /**returns the current cart ID, or an empty string if there is no cart, automatically updates its timeout*/
 static public function getCart(){
+       //DB cleanup
+       self::cleanupDb();
+       //actually look for cart
        global $CartTimeout;
        $c=self::findCart();
        if($c!=""){
index 1e010f2..acb2bbb 100644 (file)
@@ -21,6 +21,7 @@ wob_autoclass("WOTemplate","inc/wext/template.php");
 wob_autoclass("WOTicket","inc/wext/ticket.php");
 wob_autoclass("WOVoucher","inc/wext/voucher.php");
 wob_autoclass("WOWebCart","inc/wext/webcart.php");
+wob_autoclass("WOWebSession","inc/wext/websession.php");
 wob_autoclass("WOCartVoucher","inc/wext/webcart.php");
 
 wob_autoclass("MSmokeTransaction","inc/wext/transaction.php");
diff --git a/www/inc/wext/websession.php b/www/inc/wext/websession.php
new file mode 100644 (file)
index 0000000..a2d34a9
--- /dev/null
@@ -0,0 +1,64 @@
+<?
+// (c) Konrad Rosenbaum, 2007-2011
+// (c) Peter Keller, 2007/8
+// protected under the GNU AGPL version 3 or at your option any newer
+// see COPYING.AGPL
+
+define("COOKIE_WEBSESSION", "msmoke_session");
+
+/** creates a web session to authorize a customer */
+class WOWebSession extends WOWebSessionAbstract
+{
+       ///if the cookie exists: gets the current web session, if not: creates it
+       static public function getOrCreateWebSession()
+       {
+               global $_COOKIE;
+               global $db;
+               
+               //prune session table
+               $db->deleteRows("websession", "timeout < ".time());
+               
+               //check cookie
+               if(isset($_COOKIE[COOKIE_WEBSESSION])){
+                       $wsid=$_COOKIE[COOKIE_WEBSESSION];
+                       $res = WTwebsession::getFromDB($wsid);
+                       if (is_a($res,"WTwebsession")){
+                               return WOWebSession::fromTablewebsession($res);
+                       }
+               }
+               //fall back
+               //create entry
+               global $WebSessionTimeout;
+               $ws=WTwebsession::newRow();
+               $ws->timeout=time()+$WebSessionTimeout;
+               $ws->insert();
+               //set cookie
+               setCookie(COOKIE_WEBSESSION,$ws->sessionid,0);
+               //return
+               return WOWebSession::fromTablewebsession($ws);
+       }
+       
+       /** \internal called to generate a new session ID, used by WTwebsession to generate the primary key*/
+       static public function getNewSessionId(){
+               do{
+                       //generate ID
+                       $ci=getCode39ID(32);
+                       //look for duplicate
+                       $res=WTwebsession::getFromDB($ci);
+                       if(is_a($res,"WTcart"))continue;
+                       //return ID
+                       return $ci;
+               }while(true);
+       }
+
+       /** logs the customer out */
+       public function logout()
+       {
+               global $db;
+               $db->deleteRows("websession", "sessionid=".$db->escapeString($this->sessionid));
+               setcookie(COOKIE_WEBSESSION, "", 1);
+               redirectHome();
+               exit();
+       }
+};
+?>
\ No newline at end of file
index bd7f2ef..61b84c3 100644 (file)
@@ -18,8 +18,9 @@ if(isset($_GET["mode"])){
        $mode=$_GET["mode"];
 }
 
-//set internal session to virtual "_web" user for use by transactions
+//set internal $session to virtual "_web" user for use by transactions
 Session::setWebSession();
+$websession=WOWebSession::getOrCreateWebSession();
 
 //initialize TWIG
 BaseVars::initTwig();
@@ -28,6 +29,7 @@ BaseVars::initTwig();
 BaseVars::init();
 $basevars['inputnames']['cartid']=WebCart::cartIdName;
 $basevars['cartcookie']=WebCart::cartIdName;
+$basevars['sessionid']=$websession->getsessionid();
 // other info
 $basevars['lang']=LanguageManager::singleton();
 
index 88693c9..28e3839 100644 (file)
@@ -18,6 +18,8 @@
  <a href="{{script.setlanguage|raw}}{{lng|raw}}"><img src="images/{{lng|raw}}.png" alt="{{lng}}"/></a>
 {% endfor %}
 {% endif %}
+<br/>
+<div style="font-size:50%">Session: {{sessionid}}</div>
 </p>
 <!-- End Menu -->