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

Commit

Permalink
lots of clean up in response to Alain's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mimming committed May 29, 2013
1 parent 958e63a commit b1ab44b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 52 deletions.
6 changes: 3 additions & 3 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

// TODO: You must configure these fields for the starter project to function.
// Visit https://developers.google.com/glass/getting-started to learn more
$api_client_id = "API_CLIENT_ID";
$api_client_secret = "API_CLIENT_SECRET";
$api_simple_key = "API_KEY";
$api_client_id = "465303650846.apps.googleusercontent.com";

This comment has been minimized.

Copy link
@mimming

mimming May 29, 2013

Author Contributor

Well that client ID was nice until I leaked it. I've disabled this project and reverted the file in the next commit.

$api_client_secret = "wZgGkyRosOTHQkPnCB2zvb5L";
$api_simple_key = "AIzaSyCCbHcqDanbwH5eb0oycQ9niV8P3n0F0qM";

$base_url = "http://localhost/mirror-quickstart-php";

Expand Down
14 changes: 7 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

// But first, handle POST data from the form (if there is any)
switch ($_POST['operation']) {
case "insertItem":
case 'insertItem':
$new_timeline_item = new Google_TimelineItem();
$new_timeline_item->setText($_POST['message']);

Expand All @@ -54,7 +54,7 @@

$message = "Timeline Item inserted!";
break;
case "insertItemWithAction":
case 'insertItemWithAction':
$new_timeline_item = new Google_TimelineItem();
$new_timeline_item->setText("What did you have for lunch?");

Expand Down Expand Up @@ -92,7 +92,7 @@

$message = "Inserted a timeline item you can reply to";
break;
case "insertTimelineAllUsers":
case 'insertTimelineAllUsers':
$credentials = list_credentials();
if (count($credentials) > 10) {
$message = "Found " . count($credentials) . " users. Aborting to save your quota.";
Expand All @@ -111,19 +111,19 @@
$message = "Sent a cat fact to " . count($credentials) . " users.";
}
break;
case "insertSubscription":
case 'insertSubscription':
$message = subscribeToNotifications($mirror_service, $_POST['subscriptionId'],
$_SESSION['userid'], $base_url . "/notify.php");
break;
case "deleteSubscription":
case 'deleteSubscription':
$message = $mirror_service->subscriptions->delete($_POST['subscriptionId']);
break;
case "insertContact":
case 'insertContact':
insertContact($mirror_service, $_POST['id'], $_POST['name'],
$base_url . "/static/images/chipotle-tube-640x360.jpg");
$message = "Contact inserted. Enable it on MyGlass.";
break;
case "deleteContact":
case 'deleteContact':
deleteContact($mirror_service, $_POST['id']);
$message = "Contact deleted.";
break;
Expand Down
44 changes: 26 additions & 18 deletions notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
set_time_limit(0);

// And one more thing to try: forking the heavy lifting into a new process. Yeah, crazy eh?
if(function_exists('pcntl_fork')) {
if (function_exists('pcntl_fork')) {
$pid = pcntl_fork();
if ($pid == -1) {
error_log("could not fork!");
Expand All @@ -49,7 +49,7 @@
require_once 'google-api-php-client/src/contrib/Google_MirrorService.php';
require_once 'util.php';

if($_SERVER['REQUEST_METHOD'] != "POST") {
if ($_SERVER['REQUEST_METHOD'] != "POST") {
echo "method not supported";
exit();
}
Expand All @@ -70,26 +70,34 @@
// A glass service for interacting with the Mirror API
$mirror_service = new Google_MirrorService($client);

switch($request['collection']) {
case "timeline":
$timeline_item_id = $request['itemId'];
switch ($request['collection']) {
case 'timeline':
// Verify that it's a share
foreach ($request['userActions'] as $i => $user_action) {
if ($user_action['type'] == 'SHARE') {

$timeline_item = new Google_TimelineItem($mirror_service->timeline->get($timeline_item_id));
$timeline_item_id = $request['itemId'];

$attachments = $timeline_item->getAttachments();
$attachment = $attachments[0];
$timeline_item = $mirror_service->timeline->get($timeline_item_id);

$bytes = downloadAttachment($timeline_item_id, $attachment);
foreach($timeline_item->getAttachments() as $j => $attachment) {
$attachment = $mirror_service->timeline->attachments->get($timeline_item_id, $attachment.getId());
$bytes = downloadAttachment($timeline_item_id, $attachment);

// Insert a new timeline card, with a copy of that photo attached
$echo_timeline_item = new Google_TimelineItem();
$echo_timeline_item->setText("Echoing your shared photo");
$echo_timeline_item->setNotification(
new google_NotificationConfig(array("level"=>"DEFAULT")));
insertTimelineItem($mirror_service, $echo_timeline_item, "image/jpeg", $bytes);
}
break;
}
}

// Insert a new timeline card, with a copy of that photo attached
$echo_timeline_item = new Google_TimelineItem();
$echo_timeline_item->setText("Echoing your shared photo");
$echo_timeline_item->setNotification(
new google_NotificationConfig(array("level"=>"DEFAULT")));
insertTimelineItem($mirror_service, $echo_timeline_item, "image/jpeg", $bytes);
break;
case "locations":
$location = new Google_Location($mirror_service->locations->get("latest"));
case 'locations':
$location = $mirror_service->locations->get("latest");
// Insert a new timeline card, with a copy of that photo attached
$loc_timeline_item = new Google_TimelineItem();
$loc_timeline_item->setText("You are at " . $location->getLatitude() . " by " .
Expand All @@ -98,6 +106,6 @@
insertTimelineItem($mirror_service, $loc_timeline_item, null, null);
break;
default:
error_log("I don't know how to process this notification:" . $request);
error_log("I don't know how to process this notification: $request");
}

24 changes: 0 additions & 24 deletions util.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,6 @@
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_MirrorService.php';

// Returns an unauthenticated service
function get_google_api_client() {
global $api_client_id, $api_client_secret, $api_simple_key, $base_url;
// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();

$client = new Google_Client();

$client->setApplicationName('Google Mirror API PHP Quick Start');

// These are set in config.php
$client->setClientId($api_client_id);
$client->setClientSecret($api_client_secret);
$client->setDeveloperKey($api_simple_key);
$client->setRedirectUri($base_url."/oauth2callback.php");

$client->setScopes(array(
'https://www.googleapis.com/auth/glass.timeline',
'https://www.googleapis.com/auth/glass.location',
'https://www.googleapis.com/auth/userinfo.profile'));

return $client;
}

function store_credentials($user_id, $credentials) {
$db = init_db();
Expand Down

0 comments on commit b1ab44b

Please sign in to comment.