From 13389b52f1a1db9bdebf564eda91912a241757cd Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 16 Jan 2011 21:30:39 +0000 Subject: [PATCH] more work on web iface for customers git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@707 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- wob/classes/cart.wolf | 18 ++++++- wob/db/cart.wolf | 6 +- www/inc/classes/autoload.php | 1 - www/inc/classes/websession.php | 111 ------------------------------------ www/inc/rendering/cart_listing.php | 17 ++++++ www/inc/wext/autoload.php | 1 + www/inc/wext/websession.php | 64 +++++++++++++++++++++ www/index.php | 4 +- www/template/en/layout.html | 2 + 9 files changed, 108 insertions(+), 116 deletions(-) delete mode 100644 www/inc/classes/websession.php create mode 100644 www/inc/wext/websession.php diff --git a/wob/classes/cart.wolf b/wob/classes/cart.wolf index 5b66c37..6553e48 100644 --- a/wob/classes/cart.wolf +++ b/wob/classes/cart.wolf @@ -151,4 +151,20 @@ - \ No newline at end of file + + + + Cookie for this session + ID of the customer using this session or null + customer object or null + time at which this cookie is deleted + + + + + + + + + + diff --git a/wob/db/cart.wolf b/wob/db/cart.wolf index f77fda3..24354bb 100644 --- a/wob/db/cart.wolf +++ b/wob/db/cart.wolf @@ -49,9 +49,11 @@ - + + + - +
diff --git a/www/inc/classes/autoload.php b/www/inc/classes/autoload.php index 4c520c1..84f0a55 100644 --- a/www/inc/classes/autoload.php +++ b/www/inc/classes/autoload.php @@ -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 index be48f98..0000000 --- a/www/inc/classes/websession.php +++ /dev/null @@ -1,111 +0,0 @@ -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 diff --git a/www/inc/rendering/cart_listing.php b/www/inc/rendering/cart_listing.php index 8d8d146..f319c71 100644 --- a/www/inc/rendering/cart_listing.php +++ b/www/inc/rendering/cart_listing.php @@ -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!=""){ diff --git a/www/inc/wext/autoload.php b/www/inc/wext/autoload.php index 1e010f2..acb2bbb 100644 --- a/www/inc/wext/autoload.php +++ b/www/inc/wext/autoload.php @@ -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 index 0000000..a2d34a9 --- /dev/null +++ b/www/inc/wext/websession.php @@ -0,0 +1,64 @@ +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 diff --git a/www/index.php b/www/index.php index bd7f2ef..61b84c3 100644 --- a/www/index.php +++ b/www/index.php @@ -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(); diff --git a/www/template/en/layout.html b/www/template/en/layout.html index 88693c9..28e3839 100644 --- a/www/template/en/layout.html +++ b/www/template/en/layout.html @@ -18,6 +18,8 @@ {{lng}} {% endfor %} {% endif %} +
+
Session: {{sessionid}}

-- 1.7.2.5