-
Notifications
You must be signed in to change notification settings - Fork 7
/
Basecamp.class.php
713 lines (546 loc) · 23.6 KB
/
Basecamp.class.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
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
<?php
/**
* Project: Basecamp PHP API
* File: Basecamp.class.php
*
*
*
*
*
*/
require_once(dirname(__FILE__).'/Basecamp/Attachment.php');
require_once(dirname(__FILE__).'/Basecamp/Project.php');
require_once(dirname(__FILE__).'/Basecamp/Person.php');
class Basecamp {
private $_client;
private $_oAuth;
var $client_id;
private $client_secret;
private $password;
private $username;
private $authMethod;
var $authenticated = false;
var $oauth_url;
var $app_name;
var $account;
private $session_key;
private $access_token;
var $debug = false;
function __construct($app_name) {
$this->app_name = $app_name;
}
function setOAuthAuthentication($client_id,$client_secret,$oauth_url,$session_key='oAuth') {
session_start();
$this->authMethod = 'oAuth';
$this->client_id = $client_id;
$this->client_secret = $client_secret;
$this->oauth_url = $oauth_url;
$this->_oAuth = new OAuth($client_id,$client_secret);
$this->session_key = $session_key;
if ( $_SESSION[$session_key]->access_token ) {
$this->access_token = $_SESSION[$session_key]->access_token;
$this->authenticated = true;
}
}
function setOAuthAuthenticationToken($token) {
$this->authMethod = 'oAuth';
$this->access_token = $token;
$this->authenticated = true;
}
function setServerAuthentication($username,$password) {
$this->authMethod = 'server';
$this->username = $username;
$this->password = $password;
$this->authenticated = true;
}
function setDebug($debug=true) {
$this->debug = $debug;
}
function getIdentity() {
$authorization = $this->_makeAuthenticatedRequest('https://launchpad.37signals.com/authorization.json');
return $authorization->identity;
}
function getAccounts() {
$authorization = $this->_makeAuthenticatedRequest('https://launchpad.37signals.com/authorization.json');
return $authorization->accounts;
}
function setAccount($account) {
$this->account = $account;
}
function getAccountURL($target) {
if ( !$this->account ) {
throw new Exception('You must set an account first, using Basecamp::setAccount');
}
return "$this->account/$target";
}
//- mark Projects
//- jilagan: Modified to support limit and offset parameters, since default only allows for
// 25 entries at a time and with offset = 0
function getProjects($limit=25, $offset=0) {
return $this->_makeAuthenticatedRequest($this->getAccountURL('projects.json?limit=' . $limit . '&offset=' . $offset));
}
function getProject($id,$native=false) {
$project = $this->_makeAuthenticatedRequest($this->getAccountURL('projects/'.$id.'.json'));
if ( $native ) return Basecamp_Project::objectFromProject($project,$this);
return $project;
}
function getProjectAccess($id) {
return $this->getAccesses('projects',$id);
}
function grantProjectAccess($id,$users=array(),$emails=array()) {
return $this->grantAccesss('projects',$id,$users,$emails);
}
function revokeProjectAccess($id,$user) {
return $this->revokeAccess('projects',$id,$user);
}
function getProjectEvents($id,$since='',$page='') {
return $this->getEvents($since,$page,'projects',$id);
}
function getProjectTopics($id,$page='') {
return $this->getTopics($page,'projects',$id);
}
function getProjectAttachments($id,$page='') {
return $this->getAttachments($page,$id);
}
function getProjectTodoLists($id,$completed=false) {
return $this->getTodoLists($completed,$id);
}
function getProjectDocuments($id) {
return $this->getDocuments($id);
}
function getProjectMilestones($id,$past=false) {
return $this->getMilestones('projects',$id,$past);
}
function getProjectMilestone($project,$id) {
return $this->getMilestone('projects',$project,$id);
}
function createProjectMilestone($id,$summary,$description,$starts_at,$ends_at='',$all_day=false) {
return $this->createMilestone('projects',$id,$summary,$description,$starts_at,$ends_at,$all_day);
}
function updateProjectMilestone($project,$id,$summary='',$description='',$starts_at='',$ends_at='',$all_day='') {
return $this->updateMilestone('projects',$project,$id,$summary,$description,$starts_at,$ends_at,$all_day);
}
function deleteProjectMilestone($project,$id) {
return $this->deleteMilestone('projects',$project,$id);
}
function createProject($name,$description) {
return $this->_makeAuthenticatedRequest($this->getAccountURL('projects.json'),json_encode(array('name'=>$name,'description'=>$description)),'POST');
}
function updateProject($id,$name,$description) {
return $this->_makeAuthenticatedRequest($this->getAccountURL('projects/'.$id.'.json'),json_encode(array('name'=>$name,'description'=>$description)),'PUT');
}
function archiveProject($id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL('projects/'.$id.'.json'),json_encode(array('archived'=>true)),'PUT');
}
function unArchiveProject($id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL('projects/'.$id.'.json'),json_encode(array('archived'=>false)),'PUT');
}
function deleteProject($id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL('projects/'.$id.'.json'),null,'DELETE');
}
//- mark People
function me($asPerson=false) {
$me = $this->_makeAuthenticatedRequest($this->getAccountURL('people/me.json'));
if ( $asPerson ) {
return Basecamp_Person::objectFromResponse($me,&$this);
}
return $me;
}
function getPeople() {
return $this->_makeAuthenticatedRequest($this->getAccountURL('people.json'));
}
function getPerson($id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL('people/'.$id.'.json'));
}
function getPersonEvents($id,$since='',$page='') {
return $this->getEvents($since,$page,'people',$id);
}
function deletePerson($id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL('people/'.$id.'.json'),null,'DELETE');
}
//- mark Access
function getAccesses($type,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("$type/$id/accesses.json"));
}
function grantAccesss($type,$id,$users=array(),$emails=array()) {
$users = $this->_makeArray($users);
$emails = $this->_makeArray($emails);
if ( $users ) $args["ids"] = $users;
if ( $emails ) $args["email_addresses"] = $emails;
if ( !$users && !$emails ) return;
return $this->_makeAuthenticatedRequest($this->getAccountURL("$type/$id/accesses.json"),json_encode($args),'POST');
}
function revokeAccess($type,$id,$user) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("$type/$id/accesses/$user.json"),null,'DELETE');
}
//- mark Events
function getEvents($since='',$page='',$type='',$id='') {
if ( $since ) $args['since'] = $since;
if ( $page ) $args['page'] = $page;
if ( $type && $id ) $url = "$type/$id/";
return $this->_makeAuthenticatedRequest($this->getAccountURL($url."events.json"),$args);
}
//- mark Topics
function getTopics($page='',$type='',$id='') {
if ( $page ) $args['page'] = $page;
if ( $type && $id ) $url = "$type/$id/";
return $this->_makeAuthenticatedRequest($this->getAccountURL($url."topics.json"),$args);
}
//- mark Messages
function getMessages($page='',$type='',$id='') {
return $this->getTopics($page,$type,$id);
}
function getMessage($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/messages/$id.json"));
}
function createMessage($project,$subject,$content,$subscribers=array()) {
$subscribers = $this->_makeArray($subscribers);
$args['subject'] = $subject;
$args['content'] = $content;
if ( $subscribers ) $args['subscribers'] = $subscribers;
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/messages.json"),json_encode($args),'POST');
}
function createMessageComment($project,$id,$comment,$subscribers='',$attachmentName='',$tokenOrFilename='',$isFilename=false) {
return $this->createComment($project,'messages',$id,$comment,$subscribers,$attachmentName,$tokenOrFilename,$isFilename);
}
function updateMessage($project,$id,$subject,$content) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/messages/$id.json"),json_encode(array('subject'=>$subject,'content'=>$content)),'PUT');
}
function deleteMessage($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/messages/$id.json"),null,'DELETE');
}
//- mark Attachments
function createAttachment($filename) {
$fileData = file_get_contents($filename);
if ( $fileData ) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("attachments.json"),$fileData,'POST',array('Content-Type'=>'image/png'));
}
}
function getAttachments($page='',$project='') {
if ( $project ) $url = "projects/$project/attachments.json";
else $url = "attachments.json";
if ( $page ) $args['page'] = $page;
return $this->_makeAuthenticatedRequest($this->getAccountURL($url),$args);
}
//- mark Uploads
function createUpload($project,$tokenOrFilename,$name,$content,$subscribers=array(),$isFilename=false) {
if ( $isFilename ) {
//createAttachment and get token
} else $token = $tokenOrFilename;
$args['content'] = $content;
$args['attachments'] = array(new Basecamp_Attachment($token,$name));
$subscribers = $this->_makeArray($subscribers);
if ( $subscribers ) $args['subscribers'] = $subscribers;
return $this->_makeAuthenticatedRequest($this->getAccountURL("/projects/$project/uploads.json"),json_encode($args),'POST');
}
function getUpload($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("/projects/$project/uploads/$id.json"));
}
//-mark TodoLists
function getTodoLists($completed=false,$project='') {
if ( $project ) {
$url = "projects/$project/todolists";
if ( $completed ) $url .= "/completed.json";
else $url .= ".json";
} else {
$url = "todolists";
if ( $completed ) $url .= "/completed.json";
else $url .= ".json";
}
return $this->_makeAuthenticatedRequest($this->getAccountURL($url));
}
function getAssignedTodoLists($user) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("people/$user/assigned_todos.json"));
}
function getTodoList($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/todolists/$id.json"));
}
function createTodoList($project,$name,$description) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/todolists.json"),json_encode(array('name'=>$name,'description'=>$description)),'POST');
}
function updateTodoList($project,$id,$name,$description,$position='') {
if ( $name ) $args['name'] = $name;
if ( $description ) $args['description'] = $description;
if ( $position ) $args['position'] = $position;
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/todolists/$id.json"),json_encode($args),'PUT');
}
function deleteTodoList($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/todolists/$id.json"),null,'DELETE');
}
//- mark Todos
function getTodo($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/todos/$id.json"));
}
function createTodo($project,$list,$todo,$dueAt='',$assignee='') {
$args['content'] = $todo;
if ( $dueAt ) $args['due_at'] = $dueAt;
if ( $assignee ) $args['assignee'] = array( "id"=>$assignee, "type"=>"Person" );
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/todolists/$list/todos.json"),json_encode($args),'POST');
}
function createTodoComment($project,$id,$comment,$subscribers='',$attachmentName='',$tokenOrFilename='',$isFilename=false) {
return $this->createComment($project,'todos',$id,$comment,$subscribers,$attachmentName,$tokenOrFilename,$isFilename);
}
function updateTodo($project,$id,$todo,$dueAt='',$assignee='',$completed='',$position='') {
if ( $todo ) $args['content'] = $todo;
if ( $dueAt ) $args['due_at'] = $dueAt;
if ( $assignee ) $args['assignee'] = array( "id"=>$assignee, "type"=>"Person" );
if ( $completed || $completed === false ) $args['completed'] = $completed;
if ( $position ) $args['position'] = $position;
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/todos/$id.json"),json_encode($args),'PUT');
}
function completeTodo($project,$id) {
return $this->updateTodo($project,$id,'','','',true);
}
function unCompleteTodo($project,$id) {
return $this->updateTodo($project,$id,'','','',false);
}
function reorderTodo($project,$id,$position) {
return $this->updateTodo($project,$id,'','','','',$position);
}
function deleteTodo($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/todos/$id.json"),'','DELETE');
}
//-mark Comments
function createComment($project,$on,$id,$comment,$subscribers='',$attachmentName='',$tokenOrFilename='',$isFilename=false) {
$args['content'] = $comment;
$subscribers = $this->_makeArray($subscribers);
if ( $subscribers ) $args['subscribers'] = $subscribers;
if ( $attachmentName ) {
if ( $isFilename ) {
$attachment = $this->createAttachment($tokenOrFilename);
$token = $attachment->token;
} else $token = $tokenOrFilename;
$args['attachments'] = array(new Basecamp_Attachment($token,$attachmentName));
}
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/$on/$id/comments.json"),json_encode($args),'POST');
}
function deleteComment($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/comments/$id.json"),'','DELETE');
}
//- mark Documents
function getDocuments($project) {
$url = 'documents.json';
if ( $project ) $url = "projects/$project/documents.json";
return $this->_makeAuthenticatedRequest($this->getAccountURL($url));
}
function getDocument($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/documents/$id.json"));
}
function createDocument($project,$title,$content) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/documents.json"),json_encode(array('title'=>$title,'content'=>$content)),'POST');
}
function updateDocument($project,$id,$title,$content) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/documents/$id.json"),json_encode(array('title'=>$title,'content'=>$content)),'PUT');
}
function deleteDocument($project,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$project/documents/$id.json"),null,'DELETE');
}
//- mark Calendars
function getCalendars() {
return $this->_makeAuthenticatedRequest($this->getAccountURL("calendars.json"));
}
function getCalendar($id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("calendars/$id.json"));
}
function getCalendarEvents($id,$past=false) {
return $this->getMilestones('calendars',$id,$past);
}
function getCalendarMilestones($id,$past=false) {
return $this->getMilestones('calendars',$id,$past);
}
function getCalendarEvent($calendar,$id) {
return $this->getMilestone('calendars',$calendar,$id);
}
function getCalendarMilestone($calendar,$id) {
return $this->getMilestone('calendars',$calendar,$id);
}
function createCalendarEvent($id,$summary,$description,$starts_at,$ends_at='',$all_day=false) {
return $this->createMilestone('calendars',$id,$summary,$description,$starts_at,$ends_at,$all_day);
}
function createCalendarMilestone($id,$summary,$description,$starts_at,$ends_at='',$all_day=false) {
return $this->createMilestone('calendars',$id,$summary,$description,$starts_at,$ends_at,$all_day);
}
function updateCalendarEvent($calendar,$id,$summary='',$description='',$starts_at='',$ends_at='',$all_day='') {
return $this->updateMilestone('calendars',$calendar,$id,$summary,$description,$starts_at,$ends_at,$all_day);
}
function updateCalendarMilestone($calendar,$id,$summary='',$description='',$starts_at='',$ends_at='',$all_day='') {
return $this->updateMilestone('calendars',$calendar,$id,$summary,$description,$starts_at,$ends_at,$all_day);
}
function deleteCalendarEvent($calendar,$id) {
return $this->deleteMilestone('calendars',$calendar,$id);
}
function deleteCalendarMilestone($calendar,$id) {
return $this->deleteMilestone('calendars',$calendar,$id);
}
function createCalendar($name) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("calendars.json"),json_encode(array('name'=>$name)),'POST');
}
function updateCalendar($id,$name) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("calendars/$id.json"),json_encode(array('name'=>$name)),'PUT');
}
function deleteCalendar($id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("calendars/$id.json"),null,'DELETE');
}
//-mark Calendar Events (We call them Milestones to avoid confuson)
function getMilestones($source,$sourceid,$past=false) {
$url = "$source/$sourceid/calendar_events";
if ( $past ) $url .= '/past.json';
else $url .= '.json';
return $this->_makeAuthenticatedRequest($this->getAccountURL($url));
}
function getMilestone($source,$sourceid,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("$source/$sourceid/calendar_events/$id.json"));
}
function createMilestone($source,$sourceid,$summary,$description,$starts_at,$ends_at='',$all_day=false) {
$args['summary'] = $summary;
$args['description'] = $description;
$args['starts_at'] = $starts_at;
$args['all_day'] = $all_day;
if ( $ends_at ) $args['ends_at'] = $ends_at;
return $this->_makeAuthenticatedRequest($this->getAccountURL("$source/$sourceid/calendar_events.json"),json_encode($args),'POST');
}
function updateMilestone($source,$sourceid,$id,$summary='',$description='',$starts_at='',$ends_at='',$all_day='') {
if ( $summary ) $args['summary'] = $summary;
if ( $description ) $args['description'] = $description;
if ( $starts_at) $args['starts_at'] = $starts_at;
if ( $all_day || $all_day === false ) $args['all_day'] = $all_day;
if ( $ends_at ) $args['ends_at'] = $ends_at;
return $this->_makeAuthenticatedRequest($this->getAccountURL("$source/$sourceid/calendar_events/$id.json"),json_encode($args),'PUT');
}
function deleteMilestone($source,$sourceid,$id) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("$source/$sourceid/calendar_events/$id.json"),null,'DELETE');
}
//- mark Stars
function getStars() {
return $this->_makeAuthenticatedRequest($this->getAccountURL('stars.json'));
}
function starProject($projectid) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$projectid/star.json"),json_encode(array('project_id'=>$projectid)),'POST');
}
function unStarProject($projectid) {
return $this->_makeAuthenticatedRequest($this->getAccountURL("projects/$projectid/star.json"),null,'DELETE');
}
//- makeArray
function _makeArray($arg) {
if ( $arg && !is_array($arg) ) return array($arg);
return $arg;
}
//- mark Format Date
function formatDate($date,$dateOnly=false) {
$time = strtotime($date);
$date = date('Y-m-d',$time);
if ( !$dateOnly ) $date .= 'T'.date('H:i:sP',$time);
return $date;
}
function getRawURL($url) {
return $this->_makeAuthenticatedRequest($url);
}
//- mark Main request function
function _makeAuthenticatedRequest($resource,$params=array(),$method='GET',$headers=array(),$returnRawResponse=false) {
//$debug = true;
if ( !$this->authMethod ) {
throw new Exception('You must set an authentication method using Basecamp::setOAuthAuthentication or Basecamp::setServerAuthentication');
}
$default_headers = array(
'Content-Type'=>'application/json',
'User-Agent'=>$this->app_name
);
if ( $this->authMethod == 'oAuth' )
$default_headers['Authorization'] = 'Bearer '.$this->access_token;
$all_headers = array_merge($default_headers,$headers);
$headers = array();
foreach ( $all_headers as $header_name => $header_val ) {
$headers[] = "$header_name: $header_val";
}
$ch = curl_init();
if ( $this->authMethod == 'server' )
curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
if ( $method == 'GET' ) {
if ( $params ) $resource = $resource.'?'.http_build_query($params);
//print "Resource: $resource\n";
} elseif ( $method == 'POST' ) {
curl_setopt($ch, CURLOPT_POST, true );
if ( $params ) curl_setopt($ch, CURLOPT_POSTFIELDS, $params );
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method );
if ( $params ) curl_setopt($ch, CURLOPT_POSTFIELDS, $params );
}
curl_setopt($ch, CURLOPT_URL, $resource);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ( $this->debug ) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true );
}
$response = curl_exec($ch);
if ( $this->debug ) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$response = $body;
}
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $http_code == 401 ) {
$response = json_encode(array('error_code'=>401,'error'=>'Unauthorized Access'));
} elseif ( $http_code == 403 ) {
$response = json_encode(array('error_code'=>403,'error'=>'Forbidden'));
}
elseif ( $http_code == 429 ) {
$response = json_encode(array('error_code'=>429,'error'=>'Too Many Requests'));
}
elseif ( $http_code == 422 ) {
$response = json_encode(array('error_code'=>422,'error'=>'Identical Record Created Within Last 5 Mins'));
}
if ( $this->debug ) {
print_r(curl_getinfo($ch));
print "Header: $header\n";
print "Params: ".print_r($params,1);
print "Response: $response\n";
}
if ( $returnRawResponse ) return $response;
else return json_decode($response);
}
//- mark oAuth Functions
function getDialogURL() {
$dialog_url = 'https://launchpad.37signals.com/authorization/new?type=web_server&client_id='.$this->client_id.'&redirect_uri='.urlencode('http://'.$_SERVER['HTTP_HOST'].dirname(strtok($_SERVER['REQUEST_URI'],'?')).$this->oauth_url);
return $dialog_url;
}
function redirectToDialog() {
$dialog_url = $this->getDialogURL();
header("Location: $dialog_url");
}
function fetchToken($code) {
$response = $this->_makeRequest('https://launchpad.37signals.com/authorization/token',array(
'code'=>$code,
'client_id'=>$this->client_id,
'client_secret'=>$this->client_secret,
'type'=>'web_server',
'redirect_uri'=>'http://'.$_SERVER['HTTP_HOST'].dirname(strtok($_SERVER['REQUEST_URI'],'?')).$this->oauth_url
),'POST');
$decoded = json_decode($response);
$_SESSION[$this->session_key] = $decoded;
return $decoded;
}
function refreshToken($refreshToken) {
$response = $this->_makeRequest('https://launchpad.37signals.com/authorization/token',array(
'type' => 'refresh',
'client_id'=>$this->client_id,
'client_secret'=>$this->client_secret,
'redirect_uri'=>'http://'.$_SERVER['HTTP_HOST'].dirname(strtok($_SERVER['REQUEST_URI'],'?')).$this->oauth_url,
'refresh_token'=>$refreshToken
),'POST');
$decoded = json_decode($response);
$_SESSION[$this->session_key] = $decoded;
return $decoded;
}
function _makeRequest($resource,$params,$method='GET',$headers=array()) {
$all_headers = $default_headers = array('Content-Type: application/json',"User-Agent: $this->app_name");
try {
$this->_oAuth->fetch($resource,$params,$method,$all_headers);
return $this->_oAuth->getLastResponse();
} catch ( OAuthException $e ) {
return $e;
}
}
}
?>