forked from radovanx/Google-Calendar-API-PHP-Class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcalendar.class.php
More file actions
850 lines (698 loc) · 27.9 KB
/
gcalendar.class.php
File metadata and controls
850 lines (698 loc) · 27.9 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
<?php
/*
Copyright 2011 Montania System AB
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.
*/
/**
* Google Calendar, version 2.1 API class
*
* This class implements the Google Calendar API version 2.1. To use this class you need to provide
* your username (usually an e-mail) and password to the Google account. We're of course not saving
* this information in any way and all requests are sent encrypted with HTTPS.
*
* https://github.com/montania/Google-Calendar-API-PHP-Class
*
* @author Rickard Andersson <rickard@montania.se>
* @copyright Montania System AB
* @version 1.0
* @license http://www.apache.org/licenses/LICENSE-2.0
* @package GCalendar
*/
/**
* This class implements the Google Calendar API version 2.1. To use this class you need to provide
* your username (usually an e-mail) and password to the Google account. We're of course not saving
* this information in any way and all requests are sent encrypted with HTTPS.
*
* @package GCalendar
*/
class GCalendar {
private $email;
private $password;
private $source = "Montania-GCalendar-PHP";
private $sid;
private $lsid;
private $auth;
private $authenticated = false;
/**
* Class constructor to create an instance, takes email and password as arguments
* @param string $email Your google account email
* @param string $password Your google account password
*/
function __construct($email, $password) {
$this->email = $email;
$this->password = $password;
date_default_timezone_set("Europe/Stockholm");
DEFINE("DEFAULT_MAX_EVENTS", 25);
}
/**
* Method to authenticate the user against Google, returns false if authentication failed.
* @return bool
*/
function authenticate() {
if ($this->authenticated == true) {
return true;
} else if (empty($this->email) || empty($this->password)) {
return false;
}
$ch = $this->curlPostHandle("https://www.google.com/accounts/ClientLogin", false);
curl_setopt($ch, CURLOPT_POSTFIELDS, sprintf("Email=%s&Passwd=%s&source=%s&service=cl", $this->email, $this->password, $this->source));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
switch ($http_code) {
case 200:
preg_match("/SID=([a-z0-9_-]+)/i", $response, $sid);
preg_match("/LSID=([a-z0-9_-]+)/i", $response, $lsid);
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $auth);
$this->sid = $sid[1];
$this->lsid = $lsid[1];
$this->auth = $auth[1];
$this->authenticated = true;
return true;
break;
case 403:
return false;
break;
default:
return false;
}
}
/**
* Helper function to check if the user is authenticated
* @return bool
*/
function isAuthenticated() {
return $this->authenticated;
}
/**
* Method to get an array with information about all calendars assocciated with this account.
* The array contains two keys, "handle" and "title". The handle can be used with getEvents() to retrieve events.
* @return bool|array Returns false on failure and array on success.
*/
function getAllCalendars() {
if ($this->authenticated === false) {
return false;
}
$data = $this->getJsonRequest("https://www.google.com/calendar/feeds/default/allcalendars/full?alt=jsonc", true);
if (!is_object($data)) {
return false;
}
foreach ($data->data->items as $item) {
$handle = str_replace(array("https://www.google.com/calendar/feeds/", "/private/full"), "", $item->eventFeedLink);
$calendars[] = array('title' => $item->title, 'handle' => $handle);
}
return $calendars;
}
/**
* Method to get an array with this users own calendars.
* The array contains two keys, "handle" and "title". The handle can be used with getEvents() to retrieve events.
* @return bool|array Returns false on failure and array on success.
*/
function getOwnCalendars() {
if ($this->authenticated === false) {
return false;
}
$data = $this->getJsonRequest("https://www.google.com/calendar/feeds/default/owncalendars/full?alt=jsonc", true);
if (!is_object($data)) {
return false;
}
foreach ($data->data->items as $item) {
$handle = str_replace(array("https://www.google.com/calendar/feeds/", "/private/full"), "", $item->eventFeedLink);
$calendars[] = array('title' => $item->title, 'handle' => $handle);
}
return $calendars;
}
/**
* Method to add a new calendar to the authenticated account
*
* Valid colors are:
* #A32929 #B1365F #7A367A #5229A3 #29527A #2952A3 #1B887A
* #28754E #0D7813 #528800 #88880E #AB8B00 #BE6D00 #B1440E
* #865A5A #705770 #4E5D6C #5A6986 #4A716C #6E6E41 #8D6F47
* #853104 #691426 #5C1158 #23164E #182C57 #060D5E #125A12
* #2F6213 #2F6309 #5F6B02 #8C500B #8C500B #754916 #6B3304
* #5B123B #42104A #113F47 #333333 #0F4B38 #856508
*
* @param string $title Title of the calendar
* @param string $details Calendar details
* @param string $timezone Which timezone the calendar is in
* @param bool $hidden If the calendar should be hidden or not
* @param string $color Which color should be used. See above
* @param string $location Location of this calendar, geographically
* @return bool|object Returns false on failure and object on success
*/
function createCalendar($title, $details, $timezone, $hidden, $color, $location) {
if ($this->authenticated === false) {
return false;
} else if (empty($title) || empty($timezone) || !is_bool($hidden) || empty($color) || empty($location)) {
return false;
}
$data = array(
"data" => array(
"title" => $title,
"details" => $details,
"timeZone" => $timezone,
"hidden" => $hidden,
"color" => $color,
"location" => $location
)
);
$headers = array('Content-type: application/json');
$ch = $this->curlPostHandle("https://www.google.com/calendar/feeds/default/owncalendars/full", true, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response_headers = $this->http_parse_headers($response);
curl_close($ch);
unset($ch);
if ($http_code == 302) {
$url = $response_headers['Location'];
$ch = $this->curlPostHandle($url, true, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 201) {
return json_decode($response);
} else {
return false;
}
} else if ($http_code == 201) {
return json_decode($response);
} else {
return false;
}
}
/**
* Function to delete a calendar
* @param string $handle E-mail or handle to identify the calendar
* @return bool Whether or not the calendar was deleted successfully
*/
public function deleteCalendar($handle) {
$url = "https://www.google.com/calendar/feeds/default/owncalendars/full/$handle";
$ch = $this->curlDeleteHandle($url, true, array());
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
if($http_code==200) {
return true;
} else {
return false;
}
}
/**
* Method to retrieve events from a specific calendar.
* @param string $handle E-mail or handle to identify the calendar
* @param integer $max Max amount of events to get. (optional, default = 25)
* @param string $from A date string where the selection should start (optional, default all events)
* @param string $to A date string where the selection should end (optional, default all events)
* @return bool|object Returns false on failure and object on success.
*/
function getEvents($handle, $max = DEFAULT_MAX_EVENTS, $from = null, $to = null) {
if ($this->authenticated === false) {
return false;
} else if (empty($handle)) {
return false;
}
if (empty($handle)) {
$handle = "default";
}
if (!is_numeric($max)) {
$max = DEFAULT_MAX_EVENTS;
}
$url = sprintf("https://www.google.com/calendar/feeds/%s/private/full?alt=jsonc&max-results=%s", $handle, $max);
if ($from != null) {
$from = urlencode(date("c", strtotime($from)));
$url .= "&start-min=" . $from;
}
if ($to != null) {
$to = urlencode(date("c", strtotime($to)));
$url .= "&start-max=" . $to;
}
$ch = $this->curlGetHandle($url, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == "200") {
return json_decode($response);
} else {
return false;
}
}
/**
* Method to check if an event has been updated.
* @param string $handle E-mail or handle to identify the calendar
* @param string $id The ID sent by Google Calendar
* @param string $etag The ETag property sent by Google Calendar
* @return bool|object Returns false on failure, true if event is up to date and object if event has been changed
*/
function getEvent($handle, $id, $etag) {
if ($this->authenticated === false) {
return false;
} else if (empty($id) || empty($etag)) {
return false;
}
if (empty($handle)) {
$handle = "default";
}
if (substr($etag, 0, 1) != '"') {
$etag = '"' . $etag;
}
if (substr($etag, -1, 1) != '"') {
$etag .= '"';
}
$url = sprintf("https://www.google.com/calendar/feeds/%s/private/full/%s?alt=jsonc", $handle, $id);
$ch = $this->curlGetHandle($url, true, array('If-None-Match: ' . $etag));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200) {
return json_decode($response);
} else if ($http_code == 304 || $http_code == 412) {
return true;
} else {
return false;
}
}
/**
* Get an event by its entryID
* @param string $handle E-mail or handle to identify the calendar
* @return bool|object Returns false on failure and object on success
*/
function getEventByID($handle, $event_id) {
if ($this->authenticated === false) {
return false;
} else if (empty($handle)) {
return false;
}
// GET https://www.google.com/calendar/feeds/default/private/full/entryID
$url = "https://www.google.com/calendar/feeds/$handle/private/full/$event_id?alt=jsonc";
$ch = $this->curlGetHandle($url, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == "200") {
$event = json_decode($response);
if (!empty($event)) {
return $event;
} else {
return array();
}
} else {
return false;
}
}
/**
* Method to search for an event.
* @param string $handle E-mail or handle to identify the calendar
* @param string $query The search query to perform
* @param integer $max Max amount of events to get. (optional, default = 25)
* @return bool|object Returns false on failure and object on success
*/
function findEvent($handle, $query, $max = DEFAULT_MAX_EVENTS) {
if ($this->authenticated === false) {
return false;
} else if (empty($query)) {
return false;
}
if (empty($handle)) {
$handle = "default";
}
if (!is_numeric($max)) {
$max = DEFAULT_MAX_EVENTS;
}
$url = sprintf("https://www.google.com/calendar/feeds/%s/private/full?q=%s&alt=jsonc&max-results=%s", $handle, urlencode($query), $max);
$ch = $this->curlGetHandle($url, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200) {
return json_decode($response);
} else {
return false;
}
}
/**
* Method to create an event in a specific calendar.
* @param string $handle E-mail or handle to identify the calendar
* @param bool $quick If quick is set to true, only the details argument is needed.
* @param string $details Details of the event
* @param string $title Title of the event (optional in quick mode)
* @param string $transparency Transparency (optional in quick mode)
* @param string $status Status of the event (optional in quick mode)
* @param string $location Location of the event (optional in quick mode)
* @param string $start Time when the event starts (optional in quick mode)
* @param string $end Time when the event ends (optional in quick mode)
* @return bool|object Returns false on failure and object on success
*/
function createEvent($handle, $quick = false, $details, $title = null, $transparency = null, $status = null, $location = null, $start = null, $end = null) {
if ($this->authenticated === false) {
return false;
} else if ($quick === false && (empty($title) || empty($transparency) || empty($status) || empty($location) || empty($start) || empty($end))) {
return false;
} else if ($quick === true && empty($details)) {
return false;
}
if (empty($handle)) {
$handle = "default";
}
if ($quick === true) {
$data = array("data" => array(
"details" => $details,
"quickAdd" => true
)
);
$data = json_encode($data);
} else {
$data = sprintf('{
"data": {
"title": "%s",
"details": "%s",
"transparency": "%s",
"status": "%s",
"location": "%s",
"when": [
{
"start": "%s",
"end": "%s"
}
]
}
}', $title, $details, $transparency, $status, $location, date("c", strtotime($start)), date("c", strtotime($end)));
}
$headers = array('Content-Type: application/json');
$url = sprintf("https://www.google.com/calendar/feeds/%s/private/full", $handle);
$ch = $this->curlPostHandle($url, true, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response_headers = $this->http_parse_headers($response);
curl_close($ch);
unset($ch);
if ($http_code == 302) {
$url = $response_headers['Location'];
$ch = $this->curlPostHandle($url, true, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 201) {
return json_decode($response);
} else {
return false;
}
} else if ($http_code == 201) {
return json_decode($response);
} else {
return false;
}
}
/**
* Method to remove an event from the calendar. If $etag is submitted it won't delete your event if it has been updated since you last retreived it
* @param string $handle E-mail or handle to identify the calendar
* @param string $id The id of the event
* @param string $etag The e-tag of the event (optional)
* @return bool Returns false on failure and true on success
*/
function deleteEvent($handle, $id, $etag = null) {
if ($this->authenticated === false) {
return false;
} else if (empty($handle) || empty($id)) {
return false;
}
if (empty($handle)) {
$handle = "default";
}
if (!empty($etag)) {
if (substr($etag, 0, 1) != '"') {
$etag = '"' . $etag;
}
if (substr($etag, -1, 1) != '"') {
$etag .= '"';
}
$headers = array('If-Match: ' . $etag);
} else {
$headers = array('If-Match: *');
}
$url = sprintf("https://www.google.com/calendar/feeds/%s/private/full/%s", $handle, $id);
$ch = $this->curlDeleteHandle($url, true, $headers);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return ($http_code == 200);
}
/**
* Method to update an event in the calendar. If $etag is submitted it won't update your event if it has been updated since you last retreived it
* @param string $handle E-mail or handle to identify the calendar
* @param string $id The id of the event
* @param string $etag The e-tag of the event (optional)
* @param string $json The complete json code from the event that you've retrieved with the changes that you want
* @return bool|object Returns false on failure and object on success
*/
function updateEvent($handle, $id, $etag, $json) {
if ($this->authenticated === false) {
return false;
} else if (empty($handle) || empty($id) || empty($json)) {
return false;
} else if (!is_object(json_decode($json))) {
return false;
} else {
$json = json_encode(json_decode($json));
}
if (empty($handle)) {
$handle = "default";
}
$headers = array('Content-type: application/json');
if (!empty($etag)) {
if (substr($etag, 0, 1) != '"') {
$etag = '"' . $etag;
}
if (substr($etag, -1, 1) != '"') {
$etag .= '"';
}
$headers[] = 'If-Match: ' . $etag;
} else {
$headers[] = 'If-Match: *';
}
$url = sprintf("https://www.google.com/calendar/feeds/%s/private/full/%s", $handle, $id);
$ch = $this->curlPutHandle($url, true, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response_headers = $this->http_parse_headers($response);
curl_close($ch);
unset($ch);
if ($http_code == 302) {
$url = $response_headers['Location'];
$ch = $this->curlPutHandle($url, true, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200) {
return json_decode($response);
} else {
return false;
}
} else if ($http_code == 200) {
return json_decode($response);
} else {
return false;
}
}
/**
* Private helper function to send a HTTP GET request and return the json decoded data
* @param string $url
* @param bool $authenticated If the request should contain authentication information
* @return bool|object Returns false on failure and object on success.
*/
private function getJsonRequest($url, $authenticated = false) {
if (empty($url)) {
return false;
}
$ch = $this->curlGetHandle($url, $authenticated);
if ($ch === false) {
return false;
}
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code == 200) {
return json_decode($response);
} else {
return false;
}
}
/**
* Private helper function to get a cURL handle with the correct options, authentication included. The user has to be successfully authenticated with authenticate() first
* @param string $url The URL where the http request should go
* @param bool $authenticated If the request should contain authentication information
* @param array $headers An array of headers to be sent with the request
* @return bool|curl handle Returns false on failure and a curl handle on success
*/
private function curlGetHandle($url, $authenticated = false, $headers = array()) {
if ($authenticated === true && $this->authenticated === false) {
return false;
} else if (empty($url)) {
return false;
}
$headers[] = 'GData-Version: 2.1';
if ($authenticated === true) {
$headers[] = 'Authorization: GoogleLogin auth='. $this->auth;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
return $ch;
}
/**
* Private helper function to get a cURL handle for POST actions with the correct options. The user has to be successfully authenticated with authenticate() first.
* @param string $url The URL where the http request should go
* @param bool $authenticated If the request should contain authentication information
* @param array $headers An array of headers to be sent with the request
* @return bool|curl handle Returns false on failure and a curl handle on success
*/
private function curlPostHandle($url, $authenticated = false, $headers = array()) {
if ($authenticated === true && $this->authenticated === false) {
return false;
} else if (empty($url)) {
return false;
}
$headers[] = 'GData-Version: 2.1';
if ($authenticated === true) {
$headers[] = 'Authorization: GoogleLogin auth='. $this->auth;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
return $ch;
}
/**
* Internal method to create custom method cURL handles
* @param string $url The URL where the HTTP request should go
* @param string $method Can be any of DELETE, PUT, etc.
* @param bool $authenticated If the request should contain authentication information (optional, default = false)
* @param array $headers An array of headers to be sent with the request (optional)
* @param bool $return If cURL should return data instead of printing it (optional, default = true)
* @param bool $follow If cURL should follow redirects (optional, default = true)
* @return bool|curl handle Returns false in failure and a curl handle on success
*/
private function curlCustomHandle($url, $method, $authenticated = false, $headers = array(), $return = true, $follow = true) {
if ($authenticated === true && $this->authenticated === false) {
return false;
} else if (empty($url) || empty($method)) {
return false;
}
$headers[] = 'GData-Version: 2.1';
if ($authenticated === true) {
$headers[] = 'Authorization: GoogleLogin auth='. $this->auth;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $return);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
return $ch;
}
/**
* Private helper function to get a cURL handle for DELETE actions with the correct options. The user has to be successfully authenticated with authenticate() first.
* @param string $url The URL where the http request should go
* @param bool $authenticated If the request should contain authentication information
* @param array $headers An array of headers to be sent with the request
* @return bool|curl handle Returns false on failure and a curl handle on success
*/
private function curlDeleteHandle($url, $authenticated = false, $headers = array()) {
return $this->curlCustomHandle($url, "DELETE", $authenticated, $headers);
}
/**
* Private helper function to get a cURL handle for PUT actions with the correct options. The user has to be successfully authenticated with authenticate() first.
* @param string $url The URL where the http request should go
* @param bool $authenticated If the request should contain authentication information
* @param array $headers An array of headers to be sent with the request
* @return bool|curl handle Returns false on failure and a curl handle on success
*/
private function curlPutHandle($url, $authenticated = false, $headers = array()) {
return $this->curlCustomHandle($url, "PUT", $authenticated, $headers, true, false);
}
/**
* Adds user(s) to the Access Control List
* @param string $handle E-mail or handle to identify the calendar
* @param string $scope A person or set of people ( e-mail address / domain name / null )
* @param string $scope_type The type of scope ( user / domain / default )
* @param string $role The access level ( root / owner / editor / freebusy / read / none )
*/
function addUserToACL($handle = "default", $role = "read", $scope = null, $scopeType = "default") {
// POST /calendar/feeds/liz@gmail.com/acl/full
$url = sprintf("https://www.google.com/calendar/feeds/%s/acl/full/", $handle);
$data = array(
'data' => array(
'scopeType' => $scopeType,
'role' => $role
)
);
if (!empty($scope)) {
$data['data']['scope'] = $scope;
}
$json = json_encode($data);
$headers = array('Content-type: application/json');
$ch = $this->curlPostHandle($url, true, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response_headers = $this->http_parse_headers($response);
curl_close($ch);
unset($ch);
if ($http_code == 302) {
$url = $response_headers['Location'];
$ch = $this->curlPostHandle($url, true, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($json));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 201) {
return json_decode($response);
} else {
return false;
}
} else if ($http_code == 201) {
return json_decode($response);
} else {
return false;
}
}
/**
* Creates the http_parse_headers function if pecl_http is not installed
*/
function http_parse_headers($header) {
if(!function_exists('http_parse_headers')) {
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach( $fields as $field ) {
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
return $retVal;
} else {
return http_parse_headers($header);
}
}
}