This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
index.php
executable file
·376 lines (335 loc) · 13.3 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/*
* Copyright (C) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Author: Jenny Murphy - http://google.com/+JennyMurphy
require_once 'config.php';
require_once 'mirror-client.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_MirrorService.php';
require_once 'util.php';
$client = get_google_api_client();
// Authenticate if we're not already
if (!isset($_SESSION['userid']) || get_credentials($_SESSION['userid']) == null) {
header('Location: ' . $base_url . '/oauth2callback.php');
exit;
} else {
verify_credentials(get_credentials($_SESSION['userid']));
$client->setAccessToken(get_credentials($_SESSION['userid']));
}
// A glass service for interacting with the Mirror API
$mirror_service = new Google_MirrorService($client);
// But first, handle POST data from the form (if there is any)
switch ($_POST['operation']) {
case 'insertItem':
$new_timeline_item = new Google_TimelineItem();
$new_timeline_item->setText($_POST['message']);
$notification = new Google_NotificationConfig();
$notification->setLevel("DEFAULT");
$new_timeline_item->setNotification($notification);
if (isset($_POST['imageUrl']) && isset($_POST['contentType'])) {
insert_timeline_item($mirror_service, $new_timeline_item,
$_POST['contentType'], file_get_contents($_POST['imageUrl']));
} else {
insert_timeline_item($mirror_service, $new_timeline_item, null, null);
}
$message = "Timeline Item inserted!";
break;
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);
$menu_items = array();
// A couple of built in menu items
$menu_item = new Google_MenuItem();
$menu_item->setAction("REPLY");
array_push($menu_items, $menu_item);
$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?");
$menu_item = new Google_MenuItem();
$menu_item->setAction("SHARE");
array_push($menu_items, $menu_item);
// A custom menu item
$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");
$custom_menu_item->setValues(array($custom_menu_value));
$custom_menu_item->setAction("CUSTOM");
// This is how you identify it on the notification ping
$custom_menu_item->setId("safe-for-later");
array_push($menu_items, $custom_menu_item);
$new_timeline_item->setMenuItems($menu_items);
insert_timeline_item($mirror_service, $new_timeline_item, null, null);
$message = "Inserted a timeline item you can reply to";
break;
case 'insertTimelineAllUsers':
$credentials = list_credentials();
if (count($credentials) > 10) {
$message = "Found " . count($credentials) . " users. Aborting to save your quota.";
} else {
foreach ($credentials as $credential) {
$user_specific_client = get_google_api_client();
$user_specific_client->setAccessToken($credential['credentials']);
$new_timeline_item = new Google_TimelineItem();
$new_timeline_item->setText("Did you know cats have 167 bones in their tails? Mee-wow!");
$user_specific_mirror_service = new Google_MirrorService($user_specific_client);
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 = subscribe_to_notifications($mirror_service, $_POST['subscriptionId'],
$_SESSION['userid'], $base_url . "/notify.php");
break;
case 'deleteSubscription':
$message = $mirror_service->subscriptions->delete($_POST['subscriptionId']);
break;
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':
delete_contact($mirror_service, $_POST['id']);
$message = "Contact deleted.";
break;
case 'deleteTimelineItem':
delete_timeline_item($mirror_service, $_POST['itemId']);
$message = "A timeline item has been deleted.";
break;
}
//Load cool stuff to show them.
$timeline = $mirror_service->timeline->listTimeline(array('maxResults'=>'3'));
try {
$contact = $mirror_service->contacts->get("php-quick-start");
} catch (Exception $e) {
// no contact found. Meh
$contact = null;
}
$subscriptions = $mirror_service->subscriptions->listSubscriptions();
$timeline_subscription_exists = false;
$location_subscription_exists = false;
foreach ($subscriptions->getItems() as $subscription) {
if ($subscription->getId() == 'timeline') {
$timeline_subscription_exists = true;
} elseif ($subscription->getId() == 'locations') {
$location_subscription_exists = true;
}
}
?>
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Glassware Starter Project</title>
<link href="./static/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="./static/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<link href="./static/main.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Glassware Starter Project: PHP Edition</a>
</div>
</div>
</div>
<div class="container">
<?php if ($message != "") { ?>
<div class="alert alert-info"><?php echo $message; ?> </div>
<?php } ?>
<h1>Your Recent Timeline</h1>
<div class="row">
<div style="margin-top: 5px;">
<?php if ($timeline->getItems()) { ?>
<?php foreach ($timeline->getItems() as $timeline_item) { ?>
<div class="span4">
<table class="table table-bordered">
<tbody>
<tr>
<th>ID</th>
<td><?php echo $timeline_item->getId(); ?></td>
</tr>
<tr>
<th>Text</th>
<td><?php echo htmlspecialchars($timeline_item->getText()); ?></td>
</tr>
<tr>
<th>HTML</th>
<td><?php echo htmlspecialchars($timeline_item->getHtml()); ?></td>
</tr>
<tr>
<th>Attachments</th>
<td>
<?php
if ($timeline_item->getAttachments() != null) {
$attachments = $timeline_item->getAttachments();
foreach ($attachments as $attachment) { ?>
<img src="<?php echo $base_url .
'/attachment-proxy.php?timeline_item_id=' .
$timeline_item->getId() . '&attachment_id=' .
$attachment->getId() ?>" />
<?php
}
}
?>
</td>
</tr>
<tr>
<td colspan="2">
<form class="form-inline" method="post">
<input type="hidden" name="itemId" value="<?php echo $timeline_item->getId(); ?>">
<input type="hidden" name="operation" value="deleteTimelineItem">
<button class="btn btn-danger btn-block" type="submit">Delete Item</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
<?php
}
} else { ?>
<div class="span12">
<div class="alert alert-info">
You haven't added any items to your timeline yet. Use the controls
below to add something!
</div>
</div>
<?php
} ?>
</div>
</div>
<div class="row">
<div class="span4">
<h2>Timeline</h2>
<p>When you first sign in, this Glassware inserts a welcome message. Use
these controls to insert more items into your timeline. Learn more about
the timeline APIs
<a href="https://developers.google.com/glass/timeline">here</a>.</p>
<form method="post">
<input type="hidden" name="operation" value="insertItem">
<textarea name="message" class="span4">Hello World!</textarea><br/>
<button class="btn btn-block" type="submit">
Insert the above message
</button>
</form>
<form method="post">
<input type="hidden" name="operation" value="insertItem">
<input type="hidden" name="message" value="Chipotle says hi!">
<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 btn-block" type="submit">Insert a picture
<img class="button-icon" src="<?php echo $base_url .
"/static/images/chipotle-tube-640x360.jpg" ?>">
</button>
</form>
<form method="post">
<input type="hidden" name="operation" value="insertItemWithAction">
<button class="btn btn-block" type="submit">
Insert a card you can reply to
</button>
</form>
<hr>
<form method="post">
<input type="hidden" name="operation" value="insertTimelineAllUsers">
<button class="btn btn-block" type="submit">
Insert a card to all users
</button>
</form>
</div>
<div class="span4">
<h2>Contacts</h2>
<p>By default, this project inserts a single contact that accepts
all content types. Learn more about contacts
<a href="https://developers.google.com/glass/contacts">here</a>.</p>
<?php if ($contact == null) { ?>
<form method="post">
<input type="hidden" name="operation" value="insertContact">
<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">
<button class="btn btn-block btn-success" type="submit">
Insert PHP Quick Start Contact
</button>
</form>
<?php } else { ?>
<form method="post">
<input type="hidden" name="operation" value="deleteContact">
<input type="hidden" name="id" value="php-quick-start">
<button class="btn btn-block btn-danger" type="submit">
Delete PHP Quick Start Contact
</button>
</form>
<?php } ?>
</div>
<div class="span4">
<h2>Subscriptions</h2>
<p>By default a subscription is inserted for changes to the
<code>timeline</code> collection. Learn more about subscriptions
<a href="https://developers.google.com/glass/subscriptions">here</a>.</p>
<div class="alert alert-info">
Note: Subscriptions require SSL. They will not work on localhost.
</div>
<?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 btn-block btn-danger" type="submit">
Unsubscribe from timeline updates
</button>
</form>
<?php } else { ?>
<form method="post">
<input type="hidden" name="operation" value="insertSubscription">
<input type="hidden" name="subscriptionId" value="timeline">
<button class="btn btn-block btn-success" type="submit">
Subscribe to timeline updates
</button>
</form>
<?php } ?>
<?php if ($location_subscription_exists) { ?>
<form method="post">
<input type="hidden" name="subscriptionId" value="locations">
<input type="hidden" name="operation" value="deleteSubscription">
<button class="btn btn-block btn-danger" type="submit">
Unsubscribe from location updates
</button>
</form>
<?php } else { ?>
<form method="post">
<input type="hidden" name="operation" value="insertSubscription">
<input type="hidden" name="subscriptionId" value="locations">
<button class="btn btn-block btn-success" type="submit">
Subscribe to location updates
</button>
</form>
<?php } ?>
</div>
</div>
</div>
<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>