Final(?) version
This commit is contained in:
committed by
Polonkai Gergely
parent
30f450df66
commit
31c6abc039
@@ -1,62 +1,95 @@
|
||||
<?php
|
||||
require_once 'smsToken.php';
|
||||
|
||||
class smsSender
|
||||
{
|
||||
protected $sessionId = null;
|
||||
protected $backend;
|
||||
protected $dbBackend;
|
||||
protected $smsBackend;
|
||||
|
||||
public function __construct($backend, $sessionId)
|
||||
public function __construct($dbBackend, $smsBackend, $sessionId)
|
||||
{
|
||||
$this->sessionId = $sessionId;
|
||||
$this->backend = $backend;
|
||||
$this->dbBackend = $dbBackend;
|
||||
$this->smsBackend = $smsBackend;
|
||||
}
|
||||
|
||||
public function login($username, $password)
|
||||
{
|
||||
$token = '';
|
||||
|
||||
try
|
||||
{
|
||||
$token = $this->backend->getToken($username, $password, $_SERVER['REMOTE_ADDR'], $this->sessionId);
|
||||
$token = $this->dbBackend->getToken($username, $password, $_SERVER['REMOTE_ADDR'], $this->sessionId);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
throw new Exception('Authentication failed. Reason: ' . $e->getMessage());
|
||||
}
|
||||
$this->backend->auditLog($_SERVER['REMOTE_ADDR'], 'login', 'Successful login by ' . $username);
|
||||
$this->dbBackend->auditLog($_SERVER['REMOTE_ADDR'], 'login', 'Successful login by ' . $username);
|
||||
return $token;
|
||||
}
|
||||
|
||||
public function send($token, $recipient, $message, $passwordLocations)
|
||||
{
|
||||
/*
|
||||
if (valid_token($token)
|
||||
try
|
||||
{
|
||||
if (send_sms($recipient, $message))
|
||||
{
|
||||
audit_log('Successful message sending by $token->username at $ip');
|
||||
message_log('$message successfully sent to $recipient');
|
||||
}
|
||||
else
|
||||
{
|
||||
audit_log('Message sending failed for $token->username at $ip');
|
||||
}
|
||||
$tokenObj = $this->dbBackend->checkToken($token, $this->sessionId, $_SERVER['REMOTE_ADDR']);
|
||||
}
|
||||
else
|
||||
catch (Exception $e)
|
||||
{
|
||||
audit_log('Message sending attempt from $ip with invalid token');
|
||||
throw new Exception('Authentication failed. Reason: Invalid Token');
|
||||
$this->dbBackend->auditLog($_SERVER['REMOTE_ADDR'], 'send', 'Message sending attempt by invalid token ' . $token);
|
||||
throw new Exception('Authentication failed. Reason: Bad Token', 0, $e);
|
||||
}
|
||||
*/
|
||||
/* TODO: implement */
|
||||
throw new Exception('This feature is not yet implemented');
|
||||
|
||||
try
|
||||
{
|
||||
$this->smsBackend->sendSMS($recipient, $message);
|
||||
$this->dbBackend->auditLog($_SERVER['REMOTE_ADDR'], 'send', 'Successful SMS sending by ' . $tokenObj->getUsername());
|
||||
$this->dbBackend->messageLog($tokenObj->getUserId(), $recipient, $this->maskPasswords($message, $passwordLocations), $_SERVER['REMOTE_ADDR']);
|
||||
return 'success';
|
||||
}
|
||||
catch (PDOException $e)
|
||||
{
|
||||
error_log('SMS sending cannot be logged due to a database error!');
|
||||
$this->dbBackend->auditLog($_SERVER['REMOTE_ADDR'], 'send', 'SMS sending by ' . $tokenObj->getUserName() . ' cannot be logged due to a database error');
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$this->dbBackend->auditLog($_SERVER['REMOTE_ADDR'], 'send', 'Error during SMS sending by user ' . $token->getUserName() . ': ' . $e->getMessage());
|
||||
error_log('Error during SMS sending: ' . $e->getMessage());
|
||||
}
|
||||
throw new Exception('Send failed: Unknown Error');
|
||||
}
|
||||
|
||||
protected function maskPasswords($message, $passwordLocations)
|
||||
{
|
||||
$msg = $message;
|
||||
|
||||
foreach ($passwordLocations as $loc)
|
||||
{
|
||||
$msg = substr_replace($msg, '<masked password>', $loc[0], $loc[1]);
|
||||
}
|
||||
|
||||
return $msg;
|
||||
}
|
||||
|
||||
public function logout($token)
|
||||
{
|
||||
/*
|
||||
delete_token($token);
|
||||
audit_log('$token->username logged out at $ip');
|
||||
return 'success';
|
||||
*/
|
||||
/* TODO: implement */
|
||||
throw new Exception('This feature is not yet implemented');
|
||||
try
|
||||
{
|
||||
$username = $this->dbBackend->removeToken($_SERVER['REMOTE_ADDR'], $this->sessionId, $token);
|
||||
$this->dbBackend->auditLog($_SERVER['REMOTE_ADDR'], 'logout', $username . ' logged out successfully');
|
||||
session_destroy();
|
||||
session_id('');
|
||||
unset($_COOKIE['PHPSESSID']);
|
||||
return 'success';
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
error_log('Logout failed: ' . $e->getMessage());
|
||||
$this->dbBackend->auditLog('Logout failed: ' . $e->getMessage());
|
||||
throw new Exception('Logout failed: Internal Server Error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user