File: D:/HostingSpaces/SBogers13/rie-jeanne.nl/wwwroot/kms/app/controllers/c_pass_activation.class.php
<?php
/**
* c_pass_activation.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 4/26/13
*/
class Pass_Activation extends Controller
{
/**
*
* @var object Database Handler object for executing basic queries
*/
private $_dbh;
/**
*
* @var array Activation data such as email, used and stored
*/
private $_data = array('email'=>'','hash'=>'','stored'=>'','used'=>'');
/**
*
* @var array Userdata used within activation class
*/
private $_userData = array();
/**
*
* @var string Activation hash which is stored in the database and is send to the user
*/
private $_actHash = '';
public function __construct()
{
parent::__construct();
$this->_dbh = new DatabaseHandler();
$this->_dbh->setTableName('kms_activation');
}
/*
* Display to the user that the pass hass been send
*/
public function isSend()
{
// Set Page Title
$this->View->setData('page_title','Forgot pass | ' . SITE_NAME);
$this->View->setData('body_id','body_login');
$this->View->setData('background','<div id="background"><img src="' . IMAGE_ROOT . 'static/water.jpg" alt="Komma Mediadesign" /></div>');
// Render the View
$this->View->render('login/v_pass_send');
}
/*
* Display to the user that the activation hash has been expired
*/
public function expired()
{
// Set Page Title
$this->View->setData('page_title','Activation expired | ' . SITE_NAME);
$this->View->setData('body_id','body_login');
$this->View->setData('background','<div id="background"><img src="' . IMAGE_ROOT . 'static/water.jpg" alt="Komma Mediadesign" /></div>');
// Render the View
$this->View->render('login/v_hash_expired');
}
/*
* Get a hash from the database
*/
public function get($hash = NULL)
{
if( ! empty($hash))
{
$this->_dbh->setData($this->_data);
$this->_data = $this->_dbh->addRule('hash', $hash);
if($data = $this->_dbh->select())
{
return $data;
}
}
return false;
}
/*
* Sends an activation mail to the user.
*/
public function send()
{
// Get user from session
$user = Session::get('activation_user');
Session::destroy('activation_user');
$this->_userData = $user;
if( isset($this->_userData['email']) && ! empty($this->_userData['email']))
{
// Get/Set info
$fromName = 'Komma Mediadesign';
$from = 'info@komma-mediadesign.nl';
$subject = 'Wachtwoord vergeten';
// Create Url
$this->_actHash = $this->createKey();
$url = $this->createUrl($this->_actHash);
// Message
$str = 'U heeft in het Komma Management Systeem aangegeven dat u uw wachtwoord bent vergeten.<br />';
$str .= 'Middels onderstaande link kunt opnieuw een wachtwoord kiezen.<br />';
$str .= 'Deze link is de komende 24 uur geldig, dus zorgt dat u binnen deze tijd uw wachtwoord hebt gewijzigd.<br /><br />';
$str .= '<a href="'.$url.'" target="_blank">link: '.$url.'</a><br /><br />';
$msg = $str;
// Send
$mail = new PHPMailer(TRUE);
try
{
$mail->AddAddress($this->_userData['email'], $this->_userData['user']);
$mail->SetFrom($from, $fromName);
$mail->Subject = $subject;
$mail->MsgHTML($msg);
$mail->Send();
}
catch (phpmailerException $e)
{
return FALSE;
}
// Update DB
if($this->store())
{
$this->Functions->redirect(LANG_ROOT . URL_PAGE . '/' . $this->View->urls['isSend'] . '/');
}
}
return FALSE;
}
/*
* Validates the activation hash
*/
public function validate()
{
if(defined('URL_SUB2'))
{
$actHash = URL_SUB2;
// Check if we can find this hash in the database
if($data = $this->get($actHash))
{
// This password has already been used
if( ! empty($data['used']))
{
$this->Functions->redirect(LANG_ROOT . URL_PAGE . '/' . $this->View->urls['expired'] . '/');
}
// Enter a new password
else{
Session::set('pass_update_act_hash',$actHash);
$this->Functions->redirect(LANG_ROOT . $this->View->urls['pass_updater'] . '/');
}
}
}
return false;
}
/**
* Creates a new ;
*
* @access private
* @param
* @return string
*/
private function createKey()
{
$actHash = sha1($this->_userData['email'].'flipflop'.time());
return $actHash;
}
/**
* Creates a new activationurl
*
* @access private
* @param
* @return string
*/
private function createUrl($actHash)
{
$url = BASE_ROOT . LANG_ROOT . $this->View->urls['pass_activation'] . '/validate/' . $actHash . '/';
return $url;
}
/**
* Stores the activationkey
*
* @access private
* @param
* @return null
*/
private function store()
{
$data = array();
$data['hash'] = $this->_actHash;
$data['email'] = $this->_userData['email'];
$data['stored'] = time();
$this->_dbh->setData($data);
if($this->_dbh->insert())
{
return TRUE;
}
return FALSE;
}
/**
* Updates activation key when used
*
* @access public
* @param string
* @return boolean
*/
public function update($actHash)
{
// value to update
$this->_dbh->setData(array('used'=>time()));
$this->_dbh->addRule('hash',$actHash);
// update
if($this->_dbh->update())
{
return TRUE;
}
return FALSE;
}
}