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

Added timeline echo for photos and timeline notifications #1

Merged
merged 6 commits into from
May 30, 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
4 changes: 2 additions & 2 deletions attachment-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
$_GET['timeline_item_id'], $_GET['attachment_id']);

// set the content type header
header('Content-type: '. $attachment['contentType']);
header('Content-type: '. $attachment->getContentType());

// echo the bytes
echo downloadAttachment($_GET['timeline_item_id'], $attachment);
echo download_attachment($_GET['timeline_item_id'], $attachment);
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

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

$sqlite_database = "/tmp/database.sqlite";
$sqlite_database = "/tmp/database.sqlite";
107 changes: 54 additions & 53 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 @@ -46,53 +46,53 @@
$new_timeline_item->setNotification($notification);

if (isset($_POST['imageUrl']) && isset($_POST['contentType'])) {
insertTimelineItem($mirror_service, $new_timeline_item,
insert_timeline_item($mirror_service, $new_timeline_item,
$_POST['contentType'], file_get_contents($_POST['imageUrl']));
} else {
insertTimelineItem($mirror_service, $new_timeline_item, null, null);
insert_timeline_item($mirror_service, $new_timeline_item, null, null);
}

$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?");

$notification = new Google_NotificationConfig();
$notification->setLevel("DEFAULT");
$new_timeline_item->setNotification($notification);

$menuItems = array();
$menu_items = array();

// A couple of built in menu items
$menuItem = new Google_MenuItem();
$menuItem->setAction("READ_ALOUD");
array_push($menuItems, $menuItem);
$menu_item = new Google_MenuItem();
$menu_item->setAction("READ_ALOUD");
array_push($menu_items, $menu_item);
$new_timeline_item->setSpeakableText("What did you eat? Bacon?");

$menuItem = new Google_MenuItem();
$menuItem->setAction("SHARE");
array_push($menuItems, $menuItem);
$menu_item = new Google_MenuItem();
$menu_item->setAction("SHARE");
array_push($menu_items, $menu_item);

// A custom menu item
$customMenuItem = new Google_MenuItem();
$customMenuValue = new Google_MenuValue();
$customMenuValue->setDisplayName("Drill Into");
$customMenuValue->setIconUrl($service_base_url . "/static/images/drill.png");
$custom_menu_item = new Google_MenuItem();
$custom_menu_value = new Google_MenuValue();
$custom_menu_value->setDisplayName("Drill Into");
$custom_menu_value->setIconUrl($service_base_url . "/static/images/drill.png");

$customMenuItem->setValues(array($customMenuValue));
$customMenuItem->setAction("CUSTOM");
$custom_menu_item->setValues(array($custom_menu_value));
$custom_menu_item->setAction("CUSTOM");
// This is how you identify it on the notification ping
$customMenuItem->setId("safe-for-later");
array_push($menuItems, $customMenuItem);
$custom_menu_item->setId("safe-for-later");
array_push($menu_items, $custom_menu_item);

$new_timeline_item->setMenuItems($menuItems);
$new_timeline_item->setMenuItems($menu_items);

insertTimelineItem($mirror_service, $new_timeline_item, null, null);
insert_timeline_item($mirror_service, $new_timeline_item, null, null);

$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 @@ -106,25 +106,25 @@

$user_specific_mirror_service = new Google_MirrorService($user_specific_client);

insertTimelineItem($user_specific_mirror_service, $new_timeline_item, null, null);
insert_timeline_item($user_specific_mirror_service, $new_timeline_item, null, null);
}
$message = "Sent a cat fact to " . count($credentials) . " users.";
}
break;
case "insertSubscription":
$message = subscribeToNotifications($mirror_service, $_POST['subscriptionId'],
case 'insertSubscription':
$message = subscribe_to_notifications($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":
insertContact($mirror_service, $_POST['id'], $_POST['name'],
case 'insertContact':
insert_contact($mirror_service, $_POST['id'], $_POST['name'],
$base_url . "/static/images/chipotle-tube-640x360.jpg");
$message = "Contact inserted. Enable it on MyGlass.";
break;
case "deleteContact":
deleteContact($mirror_service, $_POST['id']);
case 'deleteContact':
delete_contact($mirror_service, $_POST['id']);
$message = "Contact deleted.";
break;
}
Expand All @@ -138,13 +138,13 @@
$contact = null;
}
$subscriptions = $mirror_service->subscriptions->listSubscriptions();
$timelineSubscriptionExists = false;
$locationSubscriptionExists = false;
foreach ($subscriptions['items'] as $subscription) {
if ($subscription['id'] == 'timeline') {
$timelineSubscriptionExists = true;
} elseif ($subscription['id'] == 'location') {
$locationSubscriptionExists = true;
$timeline_subscription_exists = false;
$location_subscription_exists = false;
foreach ($subscriptions->getItems() as $subscription) {
if ($subscription->getId() == 'timeline') {
$timeline_subscription_exists = true;
} elseif ($subscription->getId() == 'location') {
$location_subscription_exists = true;
}
}

Expand Down Expand Up @@ -184,26 +184,27 @@
<div class="hero-unit">
<h1>Your Recent Timeline</h1>
<?php if ($message != "") { ?>
<span class="label label-warning">Message: <?= $message ?> </span>
<span class="label label-warning">Message: <?php echo $message; ?> </span>
<?php } ?>

<div style="margin-top: 5px;">
<?php foreach ($timeline['items'] as $timeline_item) { ?>
<?php foreach ($timeline->getItems() as $timeline_item) { ?>
<ul class="span3 tile">
<li><strong>ID: </strong> <?= $timeline_item['id'] ?>
<li><strong>ID: </strong> <?php echo $timeline_item->getId(); ?>
</li>
<li>
<strong>Text: </strong> <?= $timeline_item['text'] ?>
<strong>Text: </strong> <?php echo $timeline_item->getId(); ?>
</li>
<li>
<strong>Attachments: </strong>
<?php
if (isset($timeline_item['attachments'])) {
$attachments = $timeline_item['attachments'];
if ($timeline_item->getAttachments() != null) {
$attachments = $timeline_item->getAttachments();
foreach ($attachments as $attachment) { ?>
<img src="<?= $base_url .
'/attachment-proxy.php?timeline_item_id='.
$timeline_item['id'].'&attachment_id='.$attachment['id'] ?>" />
<img src="<?php echo $base_url .
'/attachment-proxy.php?timeline_item_id=' .
$timeline_item->getId() . '&attachment_id=' .
$attachment->getId() ?>" />
<?php
}
}
Expand Down Expand Up @@ -236,12 +237,12 @@
<input type="hidden" name="operation" value="insertItem">
<input type="hidden" name="message"
value="Chipotle says hi!">
<input type="hidden" name="imageUrl" value="<?= $base_url .
<input type="hidden" name="imageUrl" value="<?php echo $base_url .
"/static/images/chipotle-tube-640x360.jpg" ?>">
<input type="hidden" name="contentType" value="image/jpeg">

<button class="btn" type="submit">A picture
<img class="button-icon" src="<?= $base_url .
<img class="button-icon" src="<?php echo $base_url .
"/static/images/chipotle-tube-640x360.jpg" ?>">
</button>
</form>
Expand All @@ -265,7 +266,7 @@
<?php if ($contact == null) { ?>
<form class="span3"method="post">
<input type="hidden" name="operation" value="insertContact">
<input type="hidden" name="iconUrl" value="<?= $base_url .
<input type="hidden" name="iconUrl" value="<?php echo $base_url .
"/static/images/chipotle-tube-640x360.jpg" ?>">
<input type="hidden" name="name" value="PHP Quick Start">
<input type="hidden" name="id" value="php-quick-start">
Expand All @@ -277,7 +278,7 @@
<input type="hidden" name="id" value="php-quick-start">
<button class="btn" type="submit">Delete PHP Quick Start Contact</button>
</form>
<? } ?>
<?php } ?>
</div>

<div class="span4">
Expand All @@ -290,22 +291,22 @@
<p class="label label-info">Note: Subscriptions require SSL. <br>They will
not work on localhost.</p>

<?php if ($timelineSubscriptionExists) { ?>
<?php if ($timeline_subscription_exists) { ?>
<form method="post">
<input type="hidden" name="subscriptionId" value="timeline">
<input type="hidden" name="operation" value="deleteSubscription">
<button class="btn" type="submit">Unsubscribe from
timeline updates</button>
</form>
<? } else { ?>
<?php } else { ?>
<form method="post">
<input type="hidden" name="operation" value="insertSubscription">
<input type="hidden" name="subscriptionId" value="timeline">
<button class="btn" type="submit">Subscribe to timeline updates</button>
</form>
<?php } ?>

<?php if ($locationSubscriptionExists) { ?>
<?php if ($location_subscription_exists) { ?>
<form method="post">
<input type="hidden" name="subscriptionId" value="location">
<input type="hidden" name="operation" value="deleteSubscription">
Expand Down
68 changes: 47 additions & 21 deletions mirror-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,41 @@

require_once 'config.php';

function insertTimelineItem($service, $timelineItem, $contentType, $attachment)
// 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->setUseObjects(true);
$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 insert_timeline_item($service, $timeline_item, $content_type, $attachment)
{
try {
$optParams = array();
if ($contentType != null && $attachment != null) {
$optParams['data'] = $attachment;
$optParams['mimeType'] = $contentType;
$opt_params = array();
if ($content_type != null && $attachment != null) {
$opt_params['data'] = $attachment;
$opt_params['mimeType'] = $content_type;
}
return $service->timeline->insert($timelineItem, $optParams);
return $service->timeline->insert($timeline_item, $opt_params);
} catch (Exception $e) {
print 'An error ocurred: ' . $e->getMessage();
return null;
Expand All @@ -44,32 +70,32 @@ function insertTimelineItem($service, $timelineItem, $contentType, $attachment)
* @param Google_MirrorService $service Authorized Mirror service.
* @param string $collection Collection to subscribe to (supported
* values are "timeline" and "locations").
* @param string $userToken Opaque token used by the Service to
* @param string $user_token Opaque token used by the Service to
* identify the user the notification pings
* are sent for (recommended).
* @param string $callbackUrl URL receiving notification pings (must be HTTPS).
* @param string $callback_url URL receiving notification pings (must be HTTPS).
*/
function subscribeToNotifications($service, $collection, $userToken, $callbackUrl)
function subscribe_to_notifications($service, $collection, $user_token, $callback_url)
{
try {
$subscription = new Google_Subscription();
$subscription->setCollection($collection);
$subscription->setUserToken($userToken);
$subscription->setCallbackUrl($callbackUrl);
$subscription->setUserToken($user_token);
$subscription->setCallbackUrl($callback_url);
$service->subscriptions->insert($subscription);
return "Subscription inserted!";
} catch (Exception $e) {
return 'An error occurred: ' . $e->getMessage();
}
}

function insertContact($service, $contactId, $displayName, $iconUrl)
function insert_contact($service, $contact_id, $display_name, $icon_url)
{
try {
$contact = new Google_Contact();
$contact->setId($contactId);
$contact->setDisplayName($displayName);
$contact->setImageUrls(array($iconUrl));
$contact->setId($contact_id);
$contact->setDisplayName($display_name);
$contact->setImageUrls(array($icon_url));
return $service->contacts->insert($contact);
} catch (Exception $e) {
print 'An error ocurred: ' . $e->getMessage();
Expand All @@ -81,11 +107,11 @@ function insertContact($service, $contactId, $displayName, $iconUrl)
* Delete a contact for the current user.
*
* @param Google_MirrorService $service Authorized Mirror service.
* @param string $contactId ID of the Contact to delete.
* @param string $contact_id ID of the Contact to delete.
*/
function deleteContact($service, $contactId) {
function delete_contact($service, $contact_id) {
try {
$service->contacts->delete($contactId);
$service->contacts->delete($contact_id);
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
Expand All @@ -94,12 +120,12 @@ function deleteContact($service, $contactId) {
/**
* Download an attachment's content.
*
* @param string $timelineId ID of the timeline item the attachment belongs to.
* @param string item_id ID of the timeline item the attachment belongs to.
* @param Google_Attachment $attachment Attachment's metadata.
* @return string The attachment's content if successful, null otherwise.
*/
function downloadAttachment($itemId, $attachment) {
$request = new Google_HttpRequest($attachment['contentUrl'], 'GET', null, null);
function download_attachment($item_id, $attachment) {
$request = new Google_HttpRequest($attachment->getContentUrl(), 'GET', null, null);
$httpRequest = Google_Client::$io->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
Expand Down
Loading