Example #1
0
	function emptyCartFromStorageSession ($session_id, $order_number) {

		$conf = JFactory::getConfig ();
		$handler = $conf->get ('session_handler', 'none');

		$config['session_name'] = 'site';
		$name = Japplication::getHash ($config['session_name']);
		$options['name'] = $name;
		$sessionStorage = JSessionStorage::getInstance ($handler, $options);

		// The session store MUST be registered.
		$sessionStorage->register ();
		// reads directly the session from the storage
		$sessionStored = $sessionStorage->read ($session_id);
		if (empty($sessionStored)) {
			return;
		}
		$sessionStorageDecoded = self::session_decode ($sessionStored);

		$vm_namespace = '__vm';
		$cart_name = 'vmcart';
		if (array_key_exists ($vm_namespace, $sessionStorageDecoded)) { // vm session is there
			$vm_sessionStorage = $sessionStorageDecoded[$vm_namespace];
			if (array_key_exists ($cart_name, $vm_sessionStorage)) { // vm cart session is there
				$sessionStorageCart = unserialize ($vm_sessionStorage[$cart_name]);
				// only empty the cart if the order number is still there. If not there, it means that the cart has already been emptied.
				if ($sessionStorageCart->order_number == $order_number) {
					if (!class_exists ('VirtueMartCart')) {
						require(VMPATH_SITE . DS . 'helpers' . DS . 'cart.php');
					}
					VirtueMartCart::emptyCartValues ($sessionStorageCart);
					$sessionStorageDecoded[$vm_namespace][$cart_name] = serialize ($sessionStorageCart);
					$sessionStorageEncoded = self::session_encode ($sessionStorageDecoded);
					$sessionStorage->write ($session_id, $sessionStorageEncoded);
				}
			}
		}
	}
 /**
  * Bind the post data to the JUser object and the VM tables, then saves it
  * It is used to register new users
  * This function can also change already registered users, this is important when a registered user changes his email within the checkout.
  *
  * @author Max Milbers
  * @author Oscar van Eijk
  * @return boolean True is the save was successful, false otherwise.
  */
 public function store(&$data)
 {
     $message = '';
     $user = '';
     $newId = 0;
     JSession::checkToken() or JSession::checkToken('get') or jexit('Invalid Token, while trying to save user');
     $mainframe = JFactory::getApplication();
     if (empty($data)) {
         vmError('Developer notice, no data to store for user');
         return false;
     }
     //To find out, if we have to register a new user, we take a look on the id of the usermodel object.
     //The constructor sets automatically the right id.
     $new = $this->_id < 1;
     if (empty($this->_id)) {
         $user = JFactory::getUser();
     } else {
         $user = JFactory::getUser($this->_id);
     }
     $gid = $user->get('gid');
     // Save original gid
     // Preformat and control user datas by plugin
     JPluginHelper::importPlugin('vmuserfield');
     $dispatcher = JDispatcher::getInstance();
     $valid = true;
     $dispatcher->trigger('plgVmOnBeforeUserfieldDataSave', array(&$valid, $this->_id, &$data, $user));
     // $valid must be false if plugin detect an error
     if ($valid == false) {
         return false;
     }
     // Before I used this "if($cart && !$new)"
     // This construction is necessary, because this function is used to register a new JUser, so we need all the JUser data in $data.
     // On the other hand this function is also used just for updating JUser data, like the email for the BT address. In this case the
     // name, username, password and so on is already stored in the JUser and dont need to be entered again.
     if (empty($data['email'])) {
         $email = $user->get('email');
         if (!empty($email)) {
             $data['email'] = $email;
         }
     } else {
         $data['email'] = JRequest::getString('email', '', 'post', 'email');
     }
     $data['email'] = str_replace(array('\'', '"', ',', '%', '*', '/', '\\', '?', '^', '`', '{', '}', '|', '~'), array(''), $data['email']);
     //This is important, when a user changes his email address from the cart,
     //that means using view user layout edit_address (which is called from the cart)
     $user->set('email', $data['email']);
     if (empty($data['name'])) {
         $name = $user->get('name');
         if (!empty($name)) {
             $data['name'] = $name;
         }
     } else {
         $data['name'] = JRequest::getString('name', '', 'post', 'name');
     }
     $data['name'] = str_replace(array('\'', '"', ',', '%', '*', '/', '\\', '?', '^', '`', '{', '}', '|', '~'), array(''), $data['name']);
     if (empty($data['username'])) {
         $username = $user->get('username');
         if (!empty($username)) {
             $data['username'] = $username;
         } else {
             $data['username'] = JRequest::getVar('username', '', 'post', 'username');
         }
     }
     if (empty($data['password'])) {
         $data['password'] = JRequest::getVar('password', '', 'post', 'string', JREQUEST_ALLOWRAW);
     }
     if (empty($data['password2'])) {
         $data['password2'] = JRequest::getVar('password2', '', 'post', 'string', JREQUEST_ALLOWRAW);
     }
     if (!$new && !empty($data['password']) && empty($data['password2'])) {
         unset($data['password']);
         unset($data['password2']);
     }
     // Bind Joomla userdata
     if (!$user->bind($data)) {
         foreach ($user->getErrors() as $error) {
             // 				vmError('user bind '.$error);
             vmError('user bind ' . $error, JText::sprintf('COM_VIRTUEMART_USER_STORE_ERROR', $error));
         }
         $message = 'Couldnt bind data to joomla user';
         array('user' => $user, 'password' => $data['password'], 'message' => $message, 'newId' => $newId, 'success' => false);
     }
     if ($new) {
         // If user registration is not allowed, show 403 not authorized.
         // But it is possible for admins and storeadmins to save
         $usersConfig = JComponentHelper::getParams('com_users');
         if (!class_exists('Permissions')) {
             require JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'permissions.php';
         }
         if (!Permissions::getInstance()->check("admin,storeadmin") && $usersConfig->get('allowUserRegistration') == '0') {
             JError::raiseError(403, JText::_('COM_VIRTUEMART_ACCESS_FORBIDDEN'));
             return;
         }
         // $authorize	= JFactory::getACL();
         // Initialize new usertype setting
         $newUsertype = $usersConfig->get('new_usertype');
         if (!$newUsertype) {
             $newUsertype = 2;
         }
         // Set some initial user values
         // $user->set('usertype', $newUsertype); NOTE : j1.5
         $user->groups[] = $newUsertype;
         $date = JFactory::getDate();
         $user->set('registerDate', $date->toSql());
         // If user activation is turned on, we need to set the activation information
         $useractivation = $usersConfig->get('useractivation');
         $doUserActivation = false;
         if ($useractivation == '1' or $useractivation == '2') {
             $doUserActivation = true;
         }
         vmdebug('user', $useractivation, $doUserActivation);
         if ($doUserActivation) {
             jimport('joomla.user.helper');
             $user->set('activation', Japplication::getHash(JUserHelper::genRandomPassword()));
             $user->set('block', '1');
             //$user->set('lastvisitDate', '0000-00-00 00:00:00');
         }
     }
     $option = JRequest::getCmd('option');
     // If an exising superadmin gets a new group, make sure enough admins are left... j1.5+
     if (!$new && $user->authorise('core.admin') && !in_array('8', $user->groups)) {
         if ($this->getSuperAdminCount() <= 1) {
             vmError(JText::_('COM_VIRTUEMART_USER_ERR_ONLYSUPERADMIN'));
             return false;
         }
     }
     // Save the JUser object
     if (!$user->save()) {
         vmError(JText::_($user->getError()), JText::_($user->getError()));
         return false;
     }
     //vmdebug('my user, why logged in? ',$user);
     $newId = $user->get('id');
     $data['virtuemart_user_id'] = $newId;
     //We need this in that case, because data is bound to table later
     $this->setUserId($newId);
     //Save the VM user stuff
     if (!$this->saveUserData($data) || !self::storeAddress($data)) {
         vmError('COM_VIRTUEMART_NOT_ABLE_TO_SAVE_USER_DATA');
         // 			vmError(Jtext::_('COM_VIRTUEMART_NOT_ABLE_TO_SAVE_USERINFO_DATA'));
     } else {
         if ($new) {
             $this->sendRegistrationEmail($user, $user->password_clear, $doUserActivation);
             if ($doUserActivation) {
                 vmInfo('COM_VIRTUEMART_REG_COMPLETE_ACTIVATE');
             } else {
                 vmInfo('COM_VIRTUEMART_REG_COMPLETE');
             }
         } else {
             vmInfo('COM_VIRTUEMART_USER_DATA_STORED');
         }
     }
     if ((int) $data['user_is_vendor'] == 1) {
         // 			vmdebug('vendor recognised');
         if ($this->storeVendorData($data)) {
             if ($new) {
                 if ($doUserActivation) {
                     vmInfo('COM_VIRTUEMART_REG_VENDOR_COMPLETE_ACTIVATE');
                 } else {
                     vmInfo('COM_VIRTUEMART_REG_VENDOR_COMPLETE');
                 }
             } else {
                 vmInfo('COM_VIRTUEMART_VENDOR_DATA_STORED');
             }
         }
     }
     return array('user' => $user, 'password' => $data['password'], 'message' => $message, 'newId' => $newId, 'success' => true);
 }