-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
246 lines (211 loc) · 6.97 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
<?php header('Access-Control-Allow-Origin: http://www.ingress.com'); ?><?php
/*
* Ways to call this file
*
* ?refresh=true&json={json from the intel page}
* NOTE: Will return a 1x1 pixel unless &html is specified
*
* ?refresh=true (to refresh the token)
*
* ?logout (to logout)
*
* ?test=read (to read the database in)
*
* ?debug (to turn on debug mode)
*
*/
/*
* Limits
*
* Number of Cells: Total of 400,000 cells across all sheets
* Number of Columns: 256 columns per sheet
*
* More: https://support.google.com/drive/answer/37603?hl=en
*
*/
/*
* TODO
*
* Check spreadsheet to make sure all the columns we need exist
*
* Way to display error messages on the spreadsheet itself
*
* Log and keep track of portal ownership records
*
* Add support for mySQL for faster execution
*
* Use mySQL to queue portal locations and execute separately
*
*/
$html = '';
$threshold = 0; //seconds
$timeZoneAdjust = -18000; //EST is -5 hours from GMT
$northBoundary = 41.138538;
$eastBoundary = -73.866403;
$southBoundary = 40.711114;
$westBoundary = -74.310378;
// Enable (true) / Disable (false) Google Docs integration and (in the future) mySQL integration below
$googleDocs = true;
require_once 'debug.php';
debug( 'Requiring Google Client' ); // More details: https://github.com/google/google-api-php-client
require_once 'google_client_ingress_helper.php';
require_once 'google-api-php-client/src/Google_Client.php';
if ( $googleDocs ) {
}
else {
debug( "WARNING: Google Docs Disabled!", 'warning' );
}
session_start();
googleDocs_authenticate();
/* Look at the $_REQUEST to see what the user wants to do */
if (isset($_REQUEST['logout'])) {
debug('Logout is set.');
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
debug( 'Got code. About to authenticate...' );
googleDocs_authenticate_step_two();
}
if (isset($_SESSION['access_token'])) {
googleDocs_set_access_token();
}
if ( googleDocs_is_user_authenticated() ) {
// We are authenticated with Google. Continue like normal.
}
else {
// We are not authenticated. Disable GoogleDocs mode and proceed anyway.
$googleDocs = false;
}
if ( $googleDocs && $client->getAccessToken() ) {
// Connect to spreadsheet
googleDocs_display_access_token_status();
googleDocs_set_token_vars();
googleDocs_connect_to_spreadsheet( 'Ingress Portals', 'Portal Listing' ); // Spreadsheet name, Worksheet name
// Decode JSON and add data to spreadsheet
if (isset($_REQUEST['json'])) {
$portal = json_decode( $_REQUEST['json'] );
// TODO check if $portal->entityType == 'portal';
//Check that the portal is within the boundaries (this is done to prevent overloading Google)
if ( $portal->lat > $northBoundary ) doExit( 'Portal too far north. Exiting.' );
if ( $portal->lat < $southBoundary ) doExit( 'Portal too far south. Exiting.' );
if ( $portal->lng > $eastBoundary ) doExit( 'Portal too far east. Exiting.' );
if ( $portal->lng < $westBoundary ) doExit( 'Portal too far west. Exiting.' );
//Create the $row array, which will be added to the spreadsheet
$row = array(
'portalname' => htmlspecialchars( $portal->title ),
'team' => $portal->team->displayName,
'updatetime' => $portal->lastUpdated, // 1390070520377 = 1/18/2014
'lastupdated' => gmdate("Y-m-d H:i:s \E\S\T", $portal->lastUpdated/1000 + $timeZoneAdjust ),
'latitude' => $portal->lat,
'longitude' => $portal->lng,
'link' => "http://www.ingress.com/intel?ll={$portal->lat},{$portal->lng}&z=18",
'level' => $portal->level,
'resonatorcount' => $portal->resonatorCount,
'health' => $portal->health
//,'image' => 'http:'.$portal->image
);
if ( $portal->team->displayName == "Neutral" ) {
$row['lastupdated--'] = gmdate("Y-m-d H:i:s \E\S\T", $portal->lastUpdated/1000 + $timeZoneAdjust );
$row['ownsince'] = '';
$row['own150days'] = '';
$row['own90days'] = '';
$row['daysowned'] = '';
$row['owner'] = 'Neutral';
}
if ( isset( $portal->capturedTimeExact )) {
// $row['json'] = $_REQUEST['json']; // For debugging - make sure to add to spreadsheet
$row['lastupdated--'] = gmdate("Y-m-d H:i:s \E\S\T", $portal->lastUpdated/1000 + $timeZoneAdjust );
$row['ownsince'] = gmdate("Y-m-d H:i:s \E\S\T", $portal->capturedTimeExact/1000 + $timeZoneAdjust );
$row['own150days'] = gmdate("Y-m-d", ( $portal->capturedTimeExact/1000 ) + 12960000 + $timeZoneAdjust );
$row['own90days'] = gmdate("Y-m-d", ( $portal->capturedTimeExact/1000 ) + 7776000 + $timeZoneAdjust );
$row['daysowned'] = round((( time() - $portal->capturedTimeExact/1000 ) / 86400 ) , 1 );
}
if ( isset( $portal->capturingPlayer )) {
if ( $portal->capturedTime != 0 ) {
$row['owner'] = $portal->capturingPlayer;
}
}
//Calculate allies/enemies by looking at mods and resonators;
$portalAllies = array();
if ( isset( $portal->capturingPlayer )) {
$portalAllies[] = $portal->capturingPlayer;
}
if ( isset( $portal->linkedMods )) {
foreach ( $portal->linkedMods as $mod) {
$portalAllies[] = $mod->installer;
}
}
if ( isset( $portal->linkedResonators )) {
foreach ( $portal->linkedResonators as $resonator ) {
$portalAllies[] = $resonator->owner;
}
}
$row[ 'enemies' ] = '';
$row[ 'allies' ] = '';
if ( count( $portalAllies ) > 0 ) {
$portalAllies = array_unique( $portalAllies );
$portalAlliesCommas = implode( ', ', $portalAllies );
if ( $row['team'] == "Enlightened" ) {
$row['allies'] = $portalAlliesCommas;
}
elseif ( $row['team'] == "Resistance" ) {
$row['enemies'] = $portalAlliesCommas;
}
}
googleDocs_update_portal();
if (isset($_REQUEST['html'])) {
$html = $html . "<p>Data $result for {$portal->title}</p>";
}
else {
if ( isset( $_REQUEST[ 'debug' ] )) {
debug( 'SUCCESS!', 'success' );
}
else {
header('Content-Type: image/gif');
echo base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==');
exit();
}
}
}
// The access token may have been updated lazily.
googleDocs_set_session_access_token();
} else {
googleDocs_create_auth_url();
}
function doExit( $message = '' ) {
if ( isset( $_REQUEST[ 'debug' ] )) {
debug( "$message" );
exit();
}
else {
header('Content-Type: image/gif');
echo base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==');
exit();
}
}
?><!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel='stylesheet' href='style.css' />
<?php
if ( isset( $_REQUEST[ 'debug' ] )) { ?>
<link rel='stylesheet' href='/css/debug.css' />
<?php
}
?>
</head>
<body>
<header><h1>Ingress Portal Google Docs Spreadsheet</h1></header>
<div class="box">
<?php echo $html ?>
<?php
if(isset($authUrl)) {
print "<a class='login' href='$authUrl'>Connect Me!</a>";
} else {
print "<a class='logout' href='?logout'>Logout</a>";
}
?>
</div>
</body>
</html>