Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Bug fixes and improved MyGlass 'off' handling #6

Merged
merged 1 commit into from
Jul 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
header('Location: ' . $base_url . '/oauth2callback.php');
exit;
} else {
verify_credentials(get_credentials($_SESSION['userid']));
$client->setAccessToken(get_credentials($_SESSION['userid']));
}

Expand Down Expand Up @@ -170,11 +171,6 @@
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Glassware Starter Project: PHP Edition</a>
<div class="nav-collapse collapse">
<form class="navbar-form pull-right" action="signout.php" method="post">
<button type="submit" class="btn">Sign out</button>
</form>
</div>
</div>
</div>
</div>
Expand All @@ -193,7 +189,7 @@
<li><strong>ID: </strong> <?php echo $timeline_item->getId(); ?>
</li>
<li>
<strong>Text: </strong> <?php echo $timeline_item->getId(); ?>
<strong>Text: </strong> <?php echo $timeline_item->getText(); ?>
</li>
<li>
<strong>Attachments: </strong>
Expand Down
34 changes: 34 additions & 0 deletions mirror-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// found at https://developers.google.com/glass/v1/reference

require_once 'config.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';

// Returns an unauthenticated service
function get_google_api_client() {
Expand Down Expand Up @@ -49,6 +51,38 @@ function get_google_api_client() {
return $client;
}

/*
* Verify the credentials. If they're broken, attempt to re-auth
* This will only work if you haven't printed anything yet (since
* it uses an HTTP header for the redirect)
*/
function verify_credentials($credentials) {
//TODO: Use the oauth2.tokeninfo() method instead once it's
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add space before TODO and re-indent accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// exposed by the PHP client library
global $base_url;

$client = get_google_api_client();
$client->setAccessToken($credentials);

$token_checker = new Google_Oauth2Service($client);
try {
$token_checker->userinfo->get();
} catch (Google_ServiceException $e) {
if($e->getCode() == 401) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/if(/if (/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

// This user may have disabled the Glassware on MyGlass.
// Clean up the mess and attempt to re-auth.
unset($_SESSION['userid']);
header('Location: ' . $base_url . '/oauth2callback.php');
exit;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} else {
// Let it go...
throw $e;
}
}

}

function insert_timeline_item($service, $timeline_item, $content_type, $attachment)
{
try {
Expand Down
5 changes: 3 additions & 2 deletions util.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function store_credentials($user_id, $credentials) {
$user_id = sqlite_escape_string(strip_tags($user_id));
$credentials = sqlite_escape_string(strip_tags($credentials));

$insert = "insert into credentials values ('$user_id', '$credentials')";
$insert = "insert or replace into credentials values ('$user_id', '$credentials')";
sqlite_exec($db, $insert);

}
Expand Down Expand Up @@ -59,7 +59,8 @@ function init_db() {
$db = sqlite_open($sqlite_database);
$test_query = "select count(*) from sqlite_master where name = 'credentials'";
if (sqlite_fetch_single(sqlite_query($db, $test_query)) == 0) {
$create_table = "create table credentials (userid text, credentials text);";
$create_table = "create table credentials (userid text not null unique, " .
"credentials text not null);";
sqlite_exec($db, $create_table);
}
return $db;
Expand Down