"account",
"downloads" => "downloads",
"history" => "history",
"status" => "status",
"logout" => "logout",
);
function Customer ($id=false,$key=false) {
$this->init(self::$table);
if ($this->load($id,$key)) return true;
else return false;
}
function management () {
global $Shopp;
if (isset($_GET['acct'])) {
switch ($_GET['acct']) {
case "receipt": break;
case "history": $this->load_orders(); break;
case "downloads": $this->load_downloads(); break;
// case "logout": $Shopp->Cart->logout(); break;
}
}
if (!empty($_POST['vieworder']) && !empty($_POST['purchaseid'])) {
$Purchase = new Purchase($_POST['purchaseid']);
if ($Purchase->email == $_POST['email']) {
$Shopp->Cart->data->Purchase = $Purchase;
$Purchase->load_purchased();
ob_start();
include(SHOPP_TEMPLATES."/receipt.php");
$content = ob_get_contents();
ob_end_clean();
return '
'.$content.'
';
}
}
if (!empty($_GET['acct']) && !empty($_GET['id'])) {
$Purchase = new Purchase($_GET['id']);
if ($Purchase->customer != $this->id) {
new ShoppError(sprintf(__('Order number %s could not be found in your order history.','Shopp'),$Purchase->id),'customer_order_history',SHOPP_AUTH_ERR);
unset($_GET['acct']);
return false;
} else {
$Shopp->Cart->data->Purchase = $Purchase;
$Purchase->load_purchased();
ob_start();
include(SHOPP_TEMPLATES."/receipt.php");
$content = ob_get_contents();
ob_end_clean();
}
$management = apply_filters('shopp_account_management_url',
'
';
return false;
}
if (!empty($_POST['customer'])) {
$this->updates($_POST);
if ($_POST['password'] == $_POST['confirm-password'])
$this->password = wp_hash_password($_POST['password']);
$this->save();
}
}
function recovery () {
global $Shopp;
$authentication = $Shopp->Settings->get('account_system');
$errors = array();
// Check email or login supplied
if (empty($_POST['account-login'])) {
if ($authentication == "wordpress") $errors[] = new ShoppError(__('Enter an email address or login name','Shopp'));
else $errors[] = new ShoppError(__('Enter an email address','Shopp'));
} else {
// Check that the account exists
if (strpos($_POST['account-login'],'@') !== false) {
$RecoveryCustomer = new Customer($_POST['account-login'],'email');
if (!$RecoveryCustomer->id)
$errors[] = new ShoppError(__('There is no user registered with that email address.','Shopp'),'password_recover_noaccount',SHOPP_AUTH_ERR);
} else {
$user_data = get_userdatabylogin($_POST['account-login']);
$RecoveryCustomer = new Customer($user_data->ID,'wpuser');
if (empty($RecoveryCustomer->id))
$errors[] = new ShoppError(__('There is no user registered with that login name.','Shopp'),'password_recover_noaccount',SHOPP_AUTH_ERR);
}
}
// return errors
if (!empty($errors)) return;
// Generate new key
$RecoveryCustomer->activation = wp_generate_password(20, false);
do_action_ref_array('shopp_generate_password_key', array(&$RecoveryCustomer));
$RecoveryCustomer->save();
$subject = apply_filters('shopp_recover_password_subject', sprintf(__('[%s] Password Recovery Request','Shopp'),get_option('blogname')));
$_ = array();
$_[] = 'From: "'.get_option('blogname').'" <'.$Shopp->Settings->get('merchant_email').'>';
$_[] = 'To: '.$RecoveryCustomer->email;
$_[] = 'Subject: '.$subject;
$_[] = '';
$_[] = __('A request has beem made to reset the password for the following site and account:','Shopp');
$_[] = get_option('siteurl');
$_[] = '';
if (isset($_POST['email-login']))
$_[] = sprintf(__('Email: %s','Shopp'), $RecoveryCustomer->email);
if (isset($_POST['loginname-login']))
$_[] = sprintf(__('Login name: %s','Shopp'), $user_data->user_login);
$_[] = '';
$_[] = __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.');
$_[] = '';
$_[] = add_query_arg(array('acct'=>'rp','key'=>$RecoveryCustomer->activation),$Shopp->link('account'));
$message = apply_filters('shopp_recover_password_message',join("\r\n",$_));
if (!shopp_email($message)) {
new ShoppError(__('The e-mail could not be sent.'),'password_recovery_email',SHOPP_ERR);
shopp_redirect(add_query_arg('acct','recover',$Shopp->link('account')));
} else {
new ShoppError(__('Check your email address for instructions on resetting the password for your account.','Shopp'),'password_recovery_email',SHOPP_ERR);
}
}
function reset_password ($activation) {
global $Shopp;
$authentication = $Shopp->Settings->get('account_system');
$user_data = false;
$activation = preg_replace('/[^a-z0-9]/i', '', $activation);
$errors = array();
if (empty($activation) || !is_string($activation))
$errors[] = new ShoppError(__('Invalid key'));
$RecoveryCustomer = new Customer($activation,'activation');
if (empty($RecoveryCustomer->id))
$errors[] = new ShoppError(__('Invalid key'));
if (!empty($errors)) return false;
// Generate a new random password
$password = wp_generate_password();
do_action_ref_array('password_reset', array(&$RecoveryCustomer,$password));
$RecoveryCustomer->password = wp_hash_password($password);
if ($authentication == "wordpress") {
$user_data = get_userdata($RecoveryCustomer->wpuser);
wp_set_password($password, $user_data->ID);
}
$RecoveryCustomer->activation = '';
$RecoveryCustomer->save();
$subject = apply_filters('shopp_reset_password_subject', sprintf(__('[%s] New Password','Shopp'),get_option('blogname')));
$_ = array();
$_[] = 'From: "'.get_option('blogname').'" <'.$Shopp->Settings->get('merchant_email').'>';
$_[] = 'To: '.$RecoveryCustomer->email;
$_[] = 'Subject: '.$subject;
$_[] = '';
$_[] = sprintf(__('Your new password for %s:','Shopp'),get_option('siteurl'));
$_[] = '';
if ($user_data)
$_[] = sprintf(__('Login name: %s','Shopp'), $user_data->user_login);
$_[] = sprintf(__('Password: %s'), $password) . "\r\n";
$_[] = '';
$_[] = __('Click here to login:').' '.$Shopp->link('account');
$message = apply_filters('shopp_reset_password_message',join("\r\n",$_));
if (!shopp_email($message)) {
new ShoppError(__('The e-mail could not be sent.'),'password_reset_email',SHOPP_ERR);
shopp_redirect(add_query_arg('acct','recover',$Shopp->link('account')));
} else {
new ShoppError(__('Check your email address for your new password.','Shopp'),'password_reset_email',SHOPP_ERR);
}
unset($_GET['acct']);
}
function load_downloads () {
if (empty($this->id)) return false;
$db =& DB::get();
$orders = DatabaseObject::tablename(Purchase::$table);
$purchases = DatabaseObject::tablename(Purchased::$table);
$pricing = DatabaseObject::tablename(Price::$table);
$asset = DatabaseObject::tablename(Asset::$table);
$query = "SELECT p.*,f.name as filename,f.size,f.properties FROM $purchases AS p LEFT JOIN $orders AS o ON o.id=p.purchase LEFT JOIN $asset AS f ON f.parent=p.price WHERE o.customer=$this->id AND f.size > 0";
$this->downloads = $db->query($query,AS_ARRAY);
}
function load_orders ($filters=array()) {
if (empty($this->id)) return false;
global $Shopp;
$db =& DB::get();
$where = '';
if (isset($filters['where'])) $where = " AND {$filters['where']}";
$orders = DatabaseObject::tablename(Purchase::$table);
$purchases = DatabaseObject::tablename(Purchased::$table);
$query = "SELECT o.* FROM $orders AS o LEFT JOIN $purchases AS p ON p.purchase=o.id WHERE o.customer=$this->id $where ORDER BY created DESC";
$Shopp->purchases = $db->query($query,AS_ARRAY);
foreach($Shopp->purchases as &$p) {
$Purchase = new Purchase();
$Purchase->updates($p);
$p = $Purchase;
}
}
function new_wpuser () {
global $Shopp;
require_once(ABSPATH."/wp-includes/registration.php");
if (empty($this->login)) return false;
if (username_exists($this->login)){
new ShoppError(__('The login name you provided is already in use. Please choose another login name.','Shopp'),'login_exists',SHOPP_ERR);
return false;
}
if (empty($this->password)) $this->password = wp_generate_password(12,true);
// Create the WordPress account
$wpuser = wp_insert_user(array(
'user_login' => $this->login,
'user_pass' => $this->password,
'user_email' => $this->email,
'display_name' => $this->firstname.' '.$this->Customer->lastname,
'nickname' => $handle,
'first_name' => $this->firstname,
'last_name' => $this->lastname
));
if (!$wpuser) return false;
// Link the WP user ID to this customer record
$this->wpuser = $wpuser;
// Send email notification of the new account
wp_new_user_notification( $wpuser, $this->password );
$this->password = "";
if (SHOPP_DEBUG) new ShoppError('Successfully created the WordPress user for the Shopp account.',false,SHOPP_DEBUG_ERR);
return true;
}
function exportcolumns () {
$prefix = "c.";
return array(
$prefix.'firstname' => __('Customer\'s First Name','Shopp'),
$prefix.'lastname' => __('Customer\'s Last Name','Shopp'),
$prefix.'email' => __('Customer\'s Email Address','Shopp'),
$prefix.'phone' => __('Customer\'s Phone Number','Shopp'),
$prefix.'company' => __('Customer\'s Company','Shopp'),
$prefix.'info' => __('Customer\'s Custom Information','Shopp'),
$prefix.'created' => __('Customer Created Date','Shopp'),
$prefix.'modified' => __('Customer Last Updated Date','Shopp'),
);
}
function tag ($property,$options=array()) {
global $Shopp;
$menus = array(
"account" => __("My Account","Shopp"),
"downloads" => __("Downloads","Shopp"),
"history" => __("Order History","Shopp"),
"status" => __("Order Status","Shopp"),
"logout" => __("Logout","Shopp")
);
// Return strings with no options
switch ($property) {
case "url": return $Shopp->link('account');
case "recover-url": return add_query_arg('acct','recover',$Shopp->link('account'));
case "process":
if (isset($_GET['acct'])) return $_GET['acct'];
return false;
case "loggedin": return $Shopp->Cart->data->login; break;
case "notloggedin": return (!$Shopp->Cart->data->login && $Shopp->Settings->get('account_system') != "none"); break;
case "login-label":
$accounts = $Shopp->Settings->get('account_system');
$label = __('Email Address','Shopp');
if ($accounts == "wordpress") $label = __('Login Name','Shopp');
if (isset($options['label'])) $label = $options['label'];
return $label;
break;
case "email-login":
case "loginname-login":
case "account-login":
if (!empty($_POST['account-login']))
$options['value'] = $_POST['account-login'];
return '';
break;
case "password-login":
if (!empty($_POST['password-login']))
$options['value'] = $_POST['password-login'];
return '';
break;
case "recover-button":
if (!isset($options['value'])) $options['value'] = __('Get New Password','Shopp');
return '';
break;
case "submit-login": // Deprecating
case "login-button":
if (!isset($options['value'])) $options['value'] = __('Login','Shopp');
if (is_shopp_page('account'))
$string = '';
else $string = '';
$string .= '';
return $string;
break;
case "errors-exist":
$Errors =& ShoppErrors();
return ($Errors->exist(SHOPP_AUTH_ERR));
break;
case "login-errors":
$Errors =& ShoppErrors();
$result = "";
if (!$Errors->exist(SHOPP_AUTH_ERR)) return false;
$errors = $Errors->get(SHOPP_AUTH_ERR);
foreach ((array)$errors as $error)
if (!empty($error)) $result .= '
'.$error->message().'
';
$Errors->reset();
return $result;
break;
case "menu":
if (!$this->looping) {
reset($this->management);
$this->looping = true;
} else next($this->management);
if (current($this->management)) return true;
else {
$this->looping = false;
reset($this->management);
return false;
}
break;
case "management":
if (array_key_exists('url',$options)) return add_query_arg('acct',key($this->management),$Shopp->link('account'));
if (array_key_exists('action',$options)) return key($this->management);
return $menus[key($this->management)];
case "accounts": return $Shopp->Settings->get('account_system'); break;
case "order-lookup":
$auth = $Shopp->Settings->get('account_system');
if ($auth != "none") return true;
if (!empty($_POST['vieworder']) && !empty($_POST['purchaseid'])) {
require_once("Purchase.php");
$Purchase = new Purchase($_POST['purchaseid']);
if ($Purchase->email == $_POST['email']) {
$Shopp->Cart->data->Purchase = $Purchase;
$Purchase->load_purchased();
ob_start();
include(SHOPP_TEMPLATES."/receipt.php");
$content = ob_get_contents();
ob_end_clean();
return '