Removed most Google Account management and mailing stuff.
This commit is contained in:
@@ -1,73 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Frs\Output\Transport;
|
||||
|
||||
class GmailTransport implements TransportInterface
|
||||
{
|
||||
private $gms;
|
||||
private $recipients;
|
||||
private $subject;
|
||||
private $headers;
|
||||
private $content;
|
||||
|
||||
public function __construct(\Frs\SessionManager $sm)
|
||||
{
|
||||
$this->gms = new \Google_Service_Gmail($sm->getGoogleClient());
|
||||
}
|
||||
|
||||
public function setContent($content)
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function setParam($key, $value)
|
||||
{
|
||||
switch ($key) {
|
||||
case 'to':
|
||||
$this->recipients = $value;
|
||||
break;
|
||||
|
||||
case 'subject':
|
||||
$this->subject = $value;
|
||||
break;
|
||||
|
||||
case 'headers':
|
||||
$this->headers = $value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64 encoding with URL/filename safe characters.
|
||||
* Taken from http://php.net/manual/en/function.base64-encode.php#103849
|
||||
*
|
||||
* @param string $data Date to encode
|
||||
* @return string Encoded data
|
||||
*/
|
||||
private function base64UrlEncode($data)
|
||||
{
|
||||
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
||||
}
|
||||
|
||||
public function transmit()
|
||||
{
|
||||
$mime = new \Mail_mime();
|
||||
$mime->setParam('html_charset', 'utf-8');
|
||||
$mime->setParam('html_encoding', '8bit');
|
||||
$mime->addTo($this->recipients);
|
||||
$mime->setHTMLBody($this->content);
|
||||
$mime->setSubject($this->subject);
|
||||
|
||||
$message_body = $mime->getMessage(null, null, $this->headers);
|
||||
$encoded_message = $this->base64UrlEncode($message_body);
|
||||
|
||||
$postBody = new \Google_Service_Gmail_Message();
|
||||
$postBody->setRaw($encoded_message);
|
||||
|
||||
$msg = $this->gms->users_messages->send('me', $postBody);
|
||||
if ($msg->getId()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Frs\Output\Transport;
|
||||
|
||||
class MailTransport implements TransportInterface
|
||||
{
|
||||
private $recipients;
|
||||
private $subject;
|
||||
private $headers;
|
||||
private $content;
|
||||
|
||||
public function setContent($content)
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function setParam($key, $value)
|
||||
{
|
||||
switch ($key) {
|
||||
case 'to':
|
||||
$this->recipients = $value;
|
||||
break;
|
||||
|
||||
case 'subject':
|
||||
$this->subject = $value;
|
||||
break;
|
||||
|
||||
case 'headers':
|
||||
$this->setHeaders($value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private function setHeaders($headers)
|
||||
{
|
||||
$this->headers = '';
|
||||
foreach ($headers as $key=>$value) {
|
||||
$this->headers .= $key . ': ' . $value . "\r\n";
|
||||
}
|
||||
}
|
||||
|
||||
public function transmit()
|
||||
{
|
||||
return mail($this->recipients, $this->subject, $this->content, $this->headers);
|
||||
}
|
||||
}
|
||||
@@ -4,97 +4,14 @@ namespace Frs;
|
||||
|
||||
class SessionManager
|
||||
{
|
||||
private $client;
|
||||
private $googleAuthValid;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->googleAuthValid = false;
|
||||
$this->client = new \Google_Client();
|
||||
$this->client->setApplicationName('Fake Reservation System');
|
||||
$this->client->setAuthConfigFile('client_secret.json');
|
||||
$this->client->addScope(\Google_Service_Oauth2::USERINFO_EMAIL);
|
||||
$this->client->addScope(\Google_Service_Gmail::GMAIL_SEND);
|
||||
|
||||
session_start();
|
||||
}
|
||||
|
||||
public function getAuthUrl()
|
||||
{
|
||||
return $this->client->createAuthUrl();
|
||||
}
|
||||
|
||||
public function authAndRedirect($authCode)
|
||||
{
|
||||
// Validate OAuth2 result, set access token and redirect to self
|
||||
$this->client->authenticate($authCode);
|
||||
$_SESSION['access_token'] = $this->client->getAccessToken();
|
||||
$this->redirectAndExit();
|
||||
}
|
||||
|
||||
public function logoutAndRedirect()
|
||||
{
|
||||
// Delete session and redirect to self
|
||||
#$this->client->setAccessToken($_SESSION['access_token']);
|
||||
#$this->client->revokeToken(); // removed granted permissions from account
|
||||
$_SESSION = array();
|
||||
if (ini_get('session.use_cookies')) {
|
||||
$params = session_get_cookie_params();
|
||||
setcookie(session_name(), '', time()-42000, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
|
||||
}
|
||||
session_destroy();
|
||||
$this->redirectAndExit();
|
||||
}
|
||||
|
||||
private function redirectAndExit()
|
||||
{
|
||||
header('Location: ' . $this->client->getRedirectUri());
|
||||
exit(0);
|
||||
}
|
||||
|
||||
public function storeFormData($form_type)
|
||||
{
|
||||
$skey = 'form_' . $form_type;
|
||||
$_SESSION[$skey] = $_POST;
|
||||
}
|
||||
|
||||
public function hasSessionToken()
|
||||
{
|
||||
return (isset($_SESSION['access_token']) && $_SESSION['access_token']);
|
||||
}
|
||||
|
||||
public function verifySession()
|
||||
{
|
||||
$this->client->setAccessToken($_SESSION['access_token']);
|
||||
if ($this->client->isAccessTokenExpired()) {
|
||||
throw new \Exception('Token expired. <a href="' . $this->getAuthUrl() . '">Request new one</a>.');
|
||||
}
|
||||
|
||||
$this->googleAuthValid = true;
|
||||
}
|
||||
|
||||
public function getUserinfo()
|
||||
{
|
||||
if (!$this->googleAuthValid) {
|
||||
return array();
|
||||
}
|
||||
$oauth = new \Google_Service_Oauth2($this->client);
|
||||
$userdata = $oauth->userinfo->get();
|
||||
|
||||
$result = array(
|
||||
'name_first' => $userdata->givenName,
|
||||
'name_last' => $userdata->familyName,
|
||||
'name' => $userdata->name,
|
||||
'picture' => $userdata->picture,
|
||||
'email' => $userdata->email,
|
||||
'gender' => $userdata->gender,
|
||||
'verifiedEmail' => $userdata->verifiedEmail,
|
||||
);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getGoogleClient()
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user