<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id: ps_shopper.php 3600 2012-04-12 00:00:00Z Joseph Kwan $
* @package VirtueMart
* @subpackage classes
* @copyright Copyright (C) 2004-2009 soeren - All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/
/**
*
* The class is meant to manage shopper entries
*/
class vm_ps_shopper {
/**
* Validates the input parameters onBeforeShopperAdd
*
* @param array $d
* @return boolean
*/
function validate_add(&$d) {
global $my, $mosConfig_absolute_path;
$provided_required = true;
$missing = "";
require_once( CLASSPATH . 'ps_userfield.php' );
$registrationFields = ps_userfield::getUserFields( 'registration', false, '', true );
$skipFields = array();
if( VM_REGISTRATION_TYPE == 'SILENT_REGISTRATION' || VM_REGISTRATION_TYPE == 'NO_REGISTRATION'
|| (VM_REGISTRATION_TYPE == 'OPTIONAL_REGISTRATION' && empty($d['register_account']))) {
$skipFields = array( 'username', 'password', 'password2');
}
if ( $my->id > 0 || (VM_REGISTRATION_TYPE != 'NORMAL_REGISTRATION' && VM_REGISTRATION_TYPE != 'OPTIONAL_REGISTRATION')) {
$skipFields = array( 'username', 'password', 'password2');
}
if( $my->id ) {
$skipFields[] = 'email';
}
$d['isValidVATID'] = false;
foreach( $registrationFields as $field ) {
/* Special checking for EU VAT ID */
if ($field->type == 'euvatid') {
if( $field->required == 0 && empty( $d[$field->name])) {
continue; // Do nothing when the EU VAT ID field was left empty
}
if( $field->required == 1 && empty( $d[$field->name])) {
$provided_required = false;
$missing .= $field->name . ",";
}
// Check the VAT ID against the validation server of the European Union
$d['isValidVATID'] = vmValidateEUVat( $d[$field->name] );
if( !$d['isValidVATID'] ) {
//TODO: Roland - insert your error message here
}
if( !$d['isValidVATID'] && $field->required == 1) {
$provided_required = false;
$missing .= $field->name . ",";
}
$d['__euvatid_field'] = $field;
}
else {
if( $field->required == 0 ) continue;
if( in_array( $field->name, $skipFields )) {
continue;
}
switch( $field->type ) {
case 'age_verification':
// The Age Verification here is just a simple check if the selected date
// is a birthday older than the minimum age (default: 18)
$d[$field->name] = vmRequest::getInt('birthday_selector_year')
.'-'.vmRequest::getInt('birthday_selector_month')
.'-'.vmRequest::getInt('birthday_selector_day');
$params = new vmParameters( $field->params );
$min_age = $params->get('minimum_age', 18 );
$min_date = (date('Y') - $min_age).'-'.date('n').'-'.date('j');
if( $d[$field->name] > $min_date ) {
// User too young!
$provided_required = false;
$missing .= $field->name . ",";
}
break;
case 'captcha':
$this->checkCaptcha($provided_required, $missing);
break;
default:
if ( empty( $d[$field->name])) {
$provided_required = false;
$missing .= $field->name . ",";
}
break;
}
}
}
global $default;
$default = $_POST;
if (!$provided_required) {
$_REQUEST['missing'] = $missing;
return false;
}
$d['user_email'] = vmGet( $d, 'email', $my->email );
$d['perms'] = 'shopper';
return true;
}
function checkCaptcha(&$provided_required, &$missing) {
global $mosConfig_absolute_path;
$securityimages4 = $mosConfig_absolute_path.'/administrator/components/com_securityimages/server.php';
$securityimages5 = $mosConfig_absolute_path.'/plugins/system/securityimages.php';
$checkSecurity = true;
$packageName = 'securityVMRegistrationCheck';
if( file_exists($securityimages4)) {
include_once($securityimages4);
$security_refid = vmGet($_POST, $packageName.'_refid');
$security_try = vmGet($_POST, $packageName.'_try');
$security_reload = vmGet($_POST, $packageName.'_reload');
$checkSecurity = checkSecurityImage($security_refid, $security_try );
} else if( file_exists($securityimages5)) {
global $mainframe;
$security_try = vmGet($_POST, $packageName.'_try');
$mainframe->triggerEvent('onSecurityImagesCheck', array($security_try ,&$checkSecurity));
}
if( !$checkSecurity ) {
$provided_required = false;
$missing .= $field->name . ",";
}
}
/**************************************************************************
** name: validate_update()
** created by:
** description:
** parameters:
** returns:
***************************************************************************/
function validate_update(&$d) {
global $my, $perm, $vmLogger, $mosConfig_absolute_path, $auth;
if ( $my->id == 0 && $auth['user_id'] == 0 ){
$vmLogger->err( "Please Login first." );
return false;
}
$db = new ps_DB;
$provided_required = true;
$missing = "";
require_once( CLASSPATH . 'ps_userfield.php' );
$accountFields = ps_userfield::getUserFields( 'account', false, '', true );
if( VM_REGISTRATION_TYPE == 'SILENT_REGISTRATION' || VM_REGISTRATION_TYPE == 'NO_REGISTRATION' || (VM_REGISTRATION_TYPE == 'OPTIONAL_REGISTRATION' && empty($d['register_account'] ))) {
$skipFields = array( 'username', 'password', 'password2');
}
if ( $my->id > 0 || (VM_REGISTRATION_TYPE != 'NORMAL_REGISTRATION' && VM_REGISTRATION_TYPE != 'OPTIONAL_REGISTRATION')) {
$skipFields = array( 'username', 'password', 'password2');
}
if( $my->id ) {
$skipFields[] = 'email';
}
foreach( $accountFields as $field ) {
if( $field->required == 0 ) {
if( $field->type == 'euvatid' && !empty($d[$field->name])) {}
else continue;
}
if( in_array( $field->name, $skipFields )) {
continue;
}
switch( $field->type ) {
case 'age_verification':
// The Age Verification here is just a simple check if the selected date
// is a birthday older than the minimum age (default: 18)
$d[$field->name] = vmRequest::getInt('birthday_selector_year')
.'-'.vmRequest::getInt('birthday_selector_month')
.'-'.vmRequest::getInt('birthday_selector_day');
$params = new vmParameters( $field->params );
$min_age = $params->get('minimum_age', 18 );
$min_date = (date('Y') - $min_age).'-'.date('n').'-'.date('j');
if( $d[$field->name] > $min_date ) {
// User too young!
$provided_required = false;
$missing .= $field->name . ",";
}
break;
case 'captcha':
$this->checkCaptcha($provided_required, $missing);
break;
case 'euvatid':
if( empty( $d[$field->name])) break; // Do nothing when the EU VAT ID field was left empty
// Check the VAT ID against the validation server of the European Union
$d['isValidVATID'] = vmValidateEUVat( $d[$field->name] );
$d['__euvatid_field'] = $field;
break; // We don't need to go further in the loop
default:
if ( empty( $d[$field->name])) {
$provided_required = false;
$missing .= $field->name . ",";
}
break;
}
}
if (!$provided_required) {
$_REQUEST['missing'] = $missing;
return false;
}
$d['user_email'] = vmGet( $d, 'email', $my->email );
$d['perms'] = 'shopper';
return true;
}
/**************************************************************************
** name: validate_delete()
** created by:
** description:
** parameters:
** returns:
***************************************************************************/
function validate_delete(&$d) {
global $my;
if ($my->id == 0){
$vmLogger->err( "Please Login first." );
return false;
}
if (!$d["user_id"]) {
$vmLogger->err( "Please select a user to delete." );
return False;
}
else {
return True;
}
}
/**
* Function to add a new Shopper into the Shop and Joomla
*
* @param array $d
* @return boolean
*/
function add( &$d ) {
global $my, $auth, $mainframe, $mosConfig_absolute_path, $sess,
$VM_LANG, $vmLogger, $database, $mosConfig_useractivation;
$ps_vendor_id = $_SESSION["ps_vendor_id"];
$hash_secret = "VirtueMartIsCool";
$db = new ps_DB;
$timestamp = time();
if (!$this->validate_add($d)) {
return False;
}
if( empty( $my->id ) ) {
$_POST['name'] = vmGet($d,'first_name','First Name' )." ".vmGet($d,'last_name','Last Name' );
if( VM_REGISTRATION_TYPE == 'SILENT_REGISTRATION' || VM_REGISTRATION_TYPE == 'NO_REGISTRATION' || (VM_REGISTRATION_TYPE == 'OPTIONAL_REGISTRATION' && empty($d['register_account'] ))) {
// Silent Registration, Optional Registration with no account wanted and No Registration
// means we need to create a hidden user
if( vmIsJoomla('1.5') ) {
$username_length = 100;
} else {
$username_length = 25;
}
$silent_username = substr( vmGet($d,'email'), 0, $username_length );
$db->query( 'SELECT username FROM `