Removed most Google Account management and mailing stuff.

This commit is contained in:
Markus Birth 2017-02-04 02:40:37 +01:00
parent ba7520caad
commit 608854b57d
11 changed files with 111 additions and 419 deletions

View File

@ -1,17 +1,15 @@
{
"name": "Fake Reservation System",
"description": "Send mails with markup to yourself to trigger Google Now features",
"name": "mbirth/FRS",
"description": "Fake Reservation System - Send mails with markup to yourself to trigger Google Now features",
"require": {
"php": ">=5.3.0",
"mustache/mustache": "~2.10",
"php": ">=5.6.0",
"mustache/mustache": "~2.11",
"bower-asset/material-design-lite": "*",
"npm-asset/mdl-ext": "*",
"pear/mail_mime": "^1.10",
"google/apiclient": "^2.0.0@RC"
"npm-asset/mdl-ext": "*"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"npm-asset/coffee-script": "*"
"npm-asset/coffee-script": "~1.12"
},
"autoload": {
"classmap": ["lib/"]

View File

@ -11,7 +11,6 @@ use \Frs\Output\Transport\GScriptTransport;
$stdout = new StdoutTransport();
$ho = new HtmlOutput($stdout, dirname(__FILE__) . '/templates');
$ho->setTemplateVar('session_time_left', 0);
$sm = new SessionManager();
@ -21,55 +20,16 @@ if (isset($_GET['action'])) {
}
$ho->setTemplateVar('action', $action);
$ho->setTemplateVar('action_uc', ucwords($action));
if (isset($_GET['code']) && $_GET['code']) {
$sm->authAndRedirect($_GET['code']); // exits
}
$tpl_done = false;
// route pages that work with and without login
switch ($action) {
case 'logout':
$sm->logoutAndRedirect(); // exits
case 'faq':
$ho->setTemplate('faq_html');
$tpl_done = true;
break;
case 'send':
// Store input in session, in case the token timed out
$sm->storeFormData($_POST['form_type']);
break;
}
if (!$tpl_done && $sm->hasSessionToken()) {
// Authenticated
$created = $_SESSION['access_token']['created'];
$expires = $_SESSION['access_token']['expires_in'];
$expire_stamp = intval($created) + intval($expires);
$ho->setTemplateVar('session_created', $created);
$ho->setTemplateVar('session_expires', $expires);
$ho->setTemplateVar('session_time_left', ($expire_stamp) - time());
try {
$sm->verifySession();
} catch (Exception $e) {
echo $e->getMessage();
#session_destroy();
die();
}
$userInfo = $sm->getUserinfo();
$ho->setTemplateVar('user', $userInfo);
$ho->setTemplateVar('date_today', date('Y-m-d'));
// Check $userdata->verifiedEmail and deny if not verified.
if (!$userInfo['verifiedEmail']) {
$ho->setTemplate('notverified_html');
$tpl_done = true;
} else {
switch ($action) {
case 'faq':
$ho->setTemplate('faq_html');
break;
case 'send':
// Store input in session, in case something happens
$sm->storeFormData($_POST['form_type']);
echo 'This would send the mail...';
//$mt = new MailTransport($sm);
$mt = new GScriptTransport();
@ -97,15 +57,20 @@ if (!$tpl_done && $sm->hasSessionToken()) {
$ho->setTemplate('mail_failed_html');
}
break;
default:
if (in_array($action, array('event', 'flight', 'hotel', 'restaurant', 'rentalcar', 'train', 'bus'))) {
case 'event':
case 'flight':
case 'hotel':
case 'restaurant':
case 'rentalcar':
case 'train':
case 'bus':
$ho->setTemplate($action . '_html');
$tpl_done = true;
$skey = 'form_' . $action;
$placeholders = array(
'USER_NAME' => $userInfo['name_first'] . ' ' . $userInfo['name_last'],
'USER_EMAIL' => $userInfo['email'],
'USER_NAME' => '',
'USER_EMAIL' => '',
);
$fd = new FieldDefinition($action);
$fd->setPlaceholders($placeholders);
@ -118,18 +83,12 @@ if (!$tpl_done && $sm->hasSessionToken()) {
// Convert hash to list for Mustache compatibility
$by_group = array_values($by_group);
$ho->setTemplateVar('form_data', $by_group);
}
if (!$tpl_done) {
$ho->setTemplate('loggedin_html');
$tpl_done = true;
}
break;
}
}
} elseif (!$tpl_done) {
// Not authenticated
$ho->setTemplateVar('auth_url', $sm->getAuthUrl());
$ho->setTemplate('index_html');
default:
// Show welcome page
$ho->setTemplate('welcome_html');
break;
}
$ho->send();

View File

@ -1,16 +0,0 @@
window.page_load_time = Math.floor(Date.now() / 1000);
window.expire_time = window.page_load_time + window.seconds_left;
window.updateTimeLeft = ->
now = Math.floor Date.now() / 1000
time_left = window.expire_time - now
obj = document.getElementById 'session_expires'
minutes = Math.floor time_left / 60
seconds = time_left % 60
if minutes+seconds > 0
obj.innerHTML = ' (' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2) + ')'
else
obj?.innerHTML = ''
window.clearInterval window.updateTimer
window.updateTimer = window.setInterval 'updateTimeLeft();', 1000

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -8,11 +8,49 @@
<h3>Frequently Asked Questions</h3>
<h4>What is this?</h4>
<p>
Some time ago, Google introduced features to highlight special items from your
mails, like hotel reservations, event tickets, car rentals and more.
</p>
<p>
Later, they added features to Google Now on Android smartphones so that you
get notified when to leave to catch your flight or event … or how to get to
your booked hotel and when the checkin opens.
</p>
<p>
And recently, they even added a feature to Google Search, that shows your
booked items when you search for similar things. All that from a mail in your
inbox.
</p>
<p>
A nice overview with sample images can be found on Google's Developer pages:<br/>
<a href="https://developers.google.com/gmail/markup/">https://developers.google.com/gmail/markup/</a>.
</p>
<p>
But while the needed data is easy to be added to confirmation mails, many
companies didn't do this yet. And sometimes, you don't even get any confirmation
by email. To make those things show up in Google Now, I made this tool.
</p>
<h4>What does it do?</h4>
<p>
You can select a type of reservation, fill in all your details and have it sent to the mail address
of your account. We will add all the neccessary markup to the mail so that Google Now can
recognise it. This will make the events/reservations show up when you need them.
</p>
<h4>What happens to my data?</h4>
<p>
Your data won't be stored on our servers. It is solely used to compose the mail.
After the mail is sent, your data is deleted from our servers.
Your data won't be stored on our servers. It is solely used to compose the mail. After the mail is
sent, your data is deleted from our servers.
</p>

View File

@ -1,58 +0,0 @@
{{> html_head}}
{{> mdl_head}}
{{> mdl_content_head}}
<div class="frs-crumbs mdl-color-text--grey-500">
<a href="./">Fake Reservation System</a> &gt; Welcome
</div>
<h3>What is this?</h3>
<p>
Some time ago, Google introduced features to highlight special items from your
mails, like hotel reservations, event tickets, car rentals and more.
</p>
<p>
Later, they added features to Google Now on Android smartphones so that you
get notified when to leave to catch your flight or event … or how to get to
your booked hotel and when the checkin opens.
</p>
<p>
And recently, they even added a feature to Google Search, that shows your
booked items when you search for similar things. All that from a mail in your
inbox.
</p>
<p>
A nice overview with sample images can be found on Google's Developer pages:<br/>
<a href="https://developers.google.com/gmail/markup/">https://developers.google.com/gmail/markup/</a>.
</p>
<p>
But while the needed data is easy to be added to confirmation mails, many
companies didn't do this yet. And sometimes, you don't even get any confirmation
by email. To make those things show up in Google Now, I made this tool.
</p>
<h3>What does it do?</h3>
<p>
After login with your Google account, you can select a type of reservation,
fill in all your details and send it to the mail address of your account.
We will add all the neccessary markup to the mail so that Google Now can
recognise it. This will make the events/reservations show up when you need
them.
</p>
<p>
To begin, please sign in:<br/>
<a href="{{auth_url}}"><img id="signin-button" src="img/btn_google_signin_dark_normal_web.png"/></a>
</p>
{{> mdl_content_foot}}
{{> mdl_foot}}
<a href="https://github.com/mbirth/php-frs/" target="_blank" id="view-source" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-color--accent mdl-color-text--accent-contrast">View Source</a>
{{> html_foot}}

View File

@ -1,21 +0,0 @@
{{> html_head}}
{{> mdl_head}}
{{> mdl_content_head}}
<div class="frs-crumbs mdl-color-text--grey-500">
<a href="./">Fake Reservation System</a> &gt; Logged in
</div>
<h3>Your email address is not verified!</h3>
<p>
To use this system, you must verify your Google email address first!
</p>
<p>
You can find more information at this address:<br/>
<a href="https://support.google.com/accounts/answer/63950">https://support.google.com/accounts/answer/63950</a>.
</p>
{{> mdl_content_foot}}
{{> mdl_foot}}
{{> html_foot}}

View File

@ -5,22 +5,15 @@
<div class="mdl-layout-spacer"></div>
<!-- Navigation -->
<nav class="mdl-navigation">
{{# user.name_first}}
<span class="mdl-navigation__link mdl-color-text--grey-800">Hello, {{user.name_first}}!<span id="session_expires"></span></span>
{{/ user.name_first}}
<button id="frs-menu-top" class="mdl-button mdl-js-button mdl-button--icon">
<span class="mdl-navigation__link mdl-color-text--grey-800">Hello, User!</span>
<!--button id="frs-menu-top" class="mdl-button mdl-js-button mdl-button--icon">
<span class="material-icons">more_vert</span>
</button>
<ul class="mdl-menu mdl-menu--bottom-right mdl-js-menu mdl-js-ripple-effect" for="frs-menu-top">
<li id="frs-menuitem-logout" class="mdl-menu__item" onclick="location.href='?action=logout';">Logout</li>
</ul>
</ul-->
</nav>
</div>
</header>
<div class="frs-ribbon"></div>
<main class="frs-main mdl-layout__content">
<script>
var seconds_left = {{session_time_left}};
</script>
<script src="js/session.js"></script>

View File

@ -3,7 +3,7 @@
{{> mdl_content_head}}
<div class="frs-crumbs mdl-color-text--grey-500">
<a href="./">Fake Reservation System</a> &gt; Logged in
<a href="./">Fake Reservation System</a> &gt; Welcome
</div>
<h3>Select the type of reservation you want to create</h3>
@ -81,4 +81,5 @@
{{> mdl_content_foot}}
{{> mdl_foot}}
<a href="https://github.com/mbirth/php-frs/" target="_blank" id="view-source" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-color--accent mdl-color-text--accent-contrast">View Source</a>
{{> html_foot}}