-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend.php
144 lines (127 loc) · 4.26 KB
/
backend.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
<?
require_once '/home/gpeters/tasksapiapp/vendor/autoload.php';
define('APPLICATION_NAME', 'Geoff Tasks');
define('CREDENTIALS_PATH', '/home/gpeters/.credentials/tasks-php-gpeters.json');
define('CLIENT_SECRET_PATH', '/home/gpeters/tasksapiapp/client_secret.json');
define('TASK_LISTS_CACHE_PATH', '/home/gpeters/tasksCache/tasks-php-gpeters-task-list-cache.json');
// If modifying these scopes, delete your previously saved credentials
// at CREDENTIALS_PATH.
define('SCOPES', implode(' ', array(
Google_Service_Tasks::TASKS)
));
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/tasks/auth.php');
// Load previously authorized credentials from a file.
$credentialsPath = CREDENTIALS_PATH;
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
// Request authorization from the user.
$client->setApprovalPrompt('force');
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
exit;
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
return $client;
}
function storeTaskListsInCache($jsonTaskLists) {
file_put_contents(TASK_LISTS_CACHE_PATH, $jsonTaskLists);
}
function getCachedTaskLists() {
header('Content-type: application/json');
if (!is_readable(TASK_LISTS_CACHE_PATH)) {
echo "[]";
return;
}
$jsonTaskLists = file_get_contents(TASK_LISTS_CACHE_PATH);
echo $jsonTaskLists;
return;
}
function getTaskLists($service) {
$results = $service->tasklists->listTasklists();
$taskLists = array();
$currentItems = $results->getItems();
$times = 0;
while(count($currentItems) > 0 && $times < 10) {
$taskLists = array_merge($taskLists, $currentItems);
if (property_exists($results, 'nextPageToken') && !empty($results->nextPageToken)) {
$optParams = array(
'pageToken' => $results->nextPageToken
);
$results = $service->tasklists->listTasklists($optParams);
$currentItems = $results->getItems();
} else {
break;
}
$times++;
}
$resultArray = array();
foreach ($taskLists as $taskList) {
$arrItem = array(
'title' => $taskList->getTitle(),
'id' => $taskList->getId()
);
array_push($resultArray, $arrItem);
}
function sort_by_order($a, $b) {
if ($a['title'] < $b['title']) {
return -1;
}
if ($a['title'] > $b['title']) {
return 1;
}
// a must be equal to b
return 0;
}
usort($resultArray, 'sort_by_order');
$jsonTaskLists = json_encode($resultArray);
storeTaskListsInCache($jsonTaskLists);
header('Content-type: application/json');
echo $jsonTaskLists;
return;
}
// taskDate is a string in form: 2010-10-15
function addTask($service, $taskListId, $title, $note, $taskDate) {
$task = new Google_Service_Tasks_Task();
$task->setTitle($title);
$task->setNotes($note);
if (!empty($taskDate)) {
$task->setDue($taskDate . 'T12:00:00.000Z');
}
$tasks = $service->tasks;
$result = $tasks->insert($taskListId, $task);
header('Content-type: application/json');
echo json_encode($result);
return;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Tasks($client);
if (!isset($_GET['method'])) {
print("Logged in, but no method provided");
exit;
}
$method = $_GET['method'];
if ($method == 'getCachedTaskLists') {
getCachedTaskLists();
} else if ($method == 'getTaskLists') {
getTaskLists($service);
} else if ($method == 'addTask') {
addTask($service, $_GET['taskListId'], $_GET['title'], $_GET['note'], $_GET['taskDate']);
}
?>