1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Persistent_Model_Observer_Session
36: {
37: 38: 39: 40: 41:
42: public function synchronizePersistentOnLogin(Varien_Event_Observer $observer)
43: {
44:
45: $customer = $observer->getEvent()->getCustomer();
46:
47: if (!$customer || !$customer->getId() || !Mage::helper('persistent/session')->isRememberMeChecked()) {
48: Mage::getModel('persistent/session')->removePersistentCookie();
49: return;
50: }
51:
52: $persistentLifeTime = Mage::helper('persistent')->getLifeTime();
53:
54: if (Mage::helper('persistent')->isEnabled() && ($persistentLifeTime <= 0)) {
55:
56: Mage::getModel('persistent/session')->deleteByCustomerId($customer->getId());
57: return;
58: }
59:
60:
61: $sessionModel = Mage::helper('persistent/session')->getSession();
62:
63:
64: if (!$sessionModel->getId() || ($sessionModel->getCustomerId() != $customer->getId())) {
65: $sessionModel = Mage::getModel('persistent/session')
66: ->setLoadExpired()
67: ->loadByCustomerId($customer->getId());
68: if (!$sessionModel->getId()) {
69: $sessionModel = Mage::getModel('persistent/session')
70: ->setCustomerId($customer->getId())
71: ->save();
72: }
73:
74: Mage::helper('persistent/session')->setSession($sessionModel);
75: }
76:
77:
78: if ($sessionModel->getId()) {
79: Mage::getSingleton('core/cookie')->set(
80: Mage_Persistent_Model_Session::COOKIE_NAME,
81: $sessionModel->getKey(),
82: $persistentLifeTime
83: );
84: }
85: }
86:
87: 88: 89: 90: 91:
92: public function synchronizePersistentOnLogout(Varien_Event_Observer $observer)
93: {
94: if (!Mage::helper('persistent')->isEnabled() || !Mage::helper('persistent')->getClearOnLogout()) {
95: return;
96: }
97:
98:
99: $customer = $observer->getEvent()->getCustomer();
100:
101: if (!$customer || !$customer->getId()) {
102: return;
103: }
104:
105: Mage::getModel('persistent/session')->removePersistentCookie();
106:
107:
108: Mage::helper('persistent/session')->setSession(null);
109: }
110:
111: 112: 113: 114: 115:
116: public function synchronizePersistentInfo(Varien_Event_Observer $observer)
117: {
118: if (!Mage::helper('persistent')->isEnabled() || !Mage::helper('persistent/session')->isPersistent()) {
119: return;
120: }
121:
122:
123: $sessionModel = Mage::helper('persistent/session')->getSession();
124:
125:
126: $request = $observer->getEvent()->getFront()->getRequest();
127:
128:
129: if (Mage::getSingleton('customer/session')->isLoggedIn()
130: || ($request && $request->getActionName() == 'logout' && $request->getControllerName() == 'account')
131: ) {
132: $sessionModel->save();
133: }
134: }
135:
136: 137: 138: 139: 140:
141: public function setRememberMeCheckedStatus(Varien_Event_Observer $observer)
142: {
143: if (!Mage::helper('persistent')->canProcess($observer)
144: || !Mage::helper('persistent')->isEnabled() || !Mage::helper('persistent')->isRememberMeEnabled()
145: ) {
146: return;
147: }
148:
149:
150: $controllerAction = $observer->getEvent()->getControllerAction();
151: if ($controllerAction) {
152: $rememberMeCheckbox = $controllerAction->getRequest()->getPost('persistent_remember_me');
153: Mage::helper('persistent/session')->setRememberMeChecked((bool)$rememberMeCheckbox);
154: if (
155: $controllerAction->getFullActionName() == 'checkout_onepage_saveBilling'
156: || $controllerAction->getFullActionName() == 'customer_account_createpost'
157: ) {
158: Mage::getSingleton('checkout/session')->setRememberMeChecked((bool)$rememberMeCheckbox);
159: }
160: }
161: }
162:
163: 164: 165: 166: 167:
168: public function renewCookie(Varien_Event_Observer $observer)
169: {
170: if (!Mage::helper('persistent')->canProcess($observer)
171: || !Mage::helper('persistent')->isEnabled() || !Mage::helper('persistent/session')->isPersistent()
172: ) {
173: return;
174: }
175:
176:
177: $controllerAction = $observer->getEvent()->getControllerAction();
178:
179: if (Mage::getSingleton('customer/session')->isLoggedIn()
180: || $controllerAction->getFullActionName() == 'customer_account_logout'
181: ) {
182: Mage::getSingleton('core/cookie')->renew(
183: Mage_Persistent_Model_Session::COOKIE_NAME,
184: Mage::helper('persistent')->getLifeTime()
185: );
186: }
187: }
188: }
189: