@@ -33,7 +33,8 @@ class QuickBase {
33
33
'includeRids ' => true ,
34
34
'returnPercentage ' => false ,
35
35
'fmt ' => 'structured ' ,
36
- 'encoding ' => 'UTF-8 '
36
+ 'encoding ' => 'UTF-8 ' ,
37
+ 'dbidAsParam ' => false
37
38
),
38
39
39
40
'status ' => array (
@@ -58,8 +59,6 @@ final public function api($action, $options = array()){
58
59
->actionRequest ()
59
60
->constructPayload ()
60
61
->transmit ()
61
- ->processResponse ()
62
- ->checkForAndHandleError ()
63
62
->actionResponse ();
64
63
65
64
return $ query ->response ;
@@ -104,10 +103,10 @@ class QuickBaseQuery {
104
103
public $ response = array ();
105
104
106
105
protected $ payload = '' ;
107
- protected $ xmlResponse = array ();
108
106
109
107
public function __construct (&$ parent , $ action = '' , $ options = array ()){
110
108
$ this ->parent = $ parent ;
109
+ $ this ->settings = array_replace_recursive (array (), $ this ->parent ->settings );
111
110
$ this ->action = $ action ;
112
111
$ this ->options = $ options ;
113
112
@@ -131,20 +130,20 @@ final public function actionResponse(){
131
130
}
132
131
133
132
final public function addFlags (){
134
- if (!isset ($ this ->options ['msInUTC ' ]) && $ this ->parent -> settings ['flags ' ]['msInUTC ' ]){
133
+ if (!isset ($ this ->options ['msInUTC ' ]) && $ this ->settings ['flags ' ]['msInUTC ' ]){
135
134
$ this ->options ['msInUTC ' ] = 1 ;
136
135
}
137
136
138
- if (!isset ($ this ->options ['appToken ' ]) && $ this ->parent -> settings ['appToken ' ]){
139
- $ this ->options ['appToken ' ] = $ this ->parent -> settings ['appToken ' ];
137
+ if (!isset ($ this ->options ['appToken ' ]) && $ this ->settings ['appToken ' ]){
138
+ $ this ->options ['appToken ' ] = $ this ->settings ['appToken ' ];
140
139
}
141
140
142
- if (!isset ($ this ->options ['ticket ' ]) && $ this ->parent -> settings ['ticket ' ]){
143
- $ this ->options ['ticket ' ] = $ this ->parent -> settings ['ticket ' ];
141
+ if (!isset ($ this ->options ['ticket ' ]) && $ this ->settings ['ticket ' ]){
142
+ $ this ->options ['ticket ' ] = $ this ->settings ['ticket ' ];
144
143
}
145
144
146
- if (!isset ($ this ->options ['encoding ' ]) && $ this ->parent -> settings ['flags ' ]['encoding ' ]){
147
- $ this ->options ['encoding ' ] = $ this ->parent -> settings ['flags ' ]['encoding ' ];
145
+ if (!isset ($ this ->options ['encoding ' ]) && $ this ->settings ['flags ' ]['encoding ' ]){
146
+ $ this ->options ['encoding ' ] = $ this ->settings ['flags ' ]['encoding ' ];
148
147
}
149
148
150
149
return $ this ;
@@ -153,7 +152,7 @@ final public function addFlags(){
153
152
final public function constructPayload (){
154
153
$ this ->payload = '' ;
155
154
156
- if ($ this ->parent -> settings ['flags ' ]['useXML ' ]){
155
+ if ($ this ->settings ['flags ' ]['useXML ' ]){
157
156
$ xmlDoc = new SimpleXMLElement (implode ('' , array (
158
157
'<?xml version="1.0" encoding=" ' ,
159
158
$ this ->options ['encoding ' ],
@@ -174,7 +173,7 @@ final public function constructPayload(){
174
173
}
175
174
176
175
final public function checkForAndHandleError (){
177
- if ($ this ->response ['errcode ' ] != $ this ->parent -> settings ['status ' ]['errcode ' ]){
176
+ if ($ this ->response ['errcode ' ] != $ this ->settings ['status ' ]['errcode ' ]){
178
177
if ($ this ->response ['errcode ' ] == 4 && isset ($ this ->parent ->settings ['username ' ]) && isset ($ this ->parent ->settings ['password ' ])){
179
178
try {
180
179
$ newTicket = $ this ->parent ->api ('API_Authenticate ' , array (
@@ -183,13 +182,12 @@ final public function checkForAndHandleError(){
183
182
));
184
183
185
184
$ this ->parent ->settings ['ticket ' ] = $ newTicket ['ticket ' ];
185
+ $ this ->settings ['ticket ' ] = $ newTicket ['ticket ' ];
186
186
$ this ->options ['ticket ' ] = $ newTicket ['ticket ' ];
187
187
188
188
return $ this
189
189
->constructPayload ()
190
- ->transmit ()
191
- ->processResponse ()
192
- ->checkForAndHandleError ();
190
+ ->transmit ();
193
191
}catch (Exception $ newTicketErr ){
194
192
throw $ newTicketErr ;
195
193
}
@@ -217,35 +215,26 @@ final public function processOptions(){
217
215
return $ this ;
218
216
}
219
217
220
- final public function processResponse (){
221
- $ this ->response = array ();
222
-
223
- $ this ->xml2Arr ($ this ->xmlResponse , $ this ->response );
224
-
225
- $ this ->cleanXml2Arr ($ this ->response );
226
-
227
- return $ this ;
228
- }
229
-
230
218
final public function transmit (){
231
219
$ ch = curl_init (implode ('' , array (
232
- $ this ->parent -> settings ['useSSL ' ] ? 'https:// ' : 'http:// ' ,
233
- $ this ->parent -> settings ['realm ' ],
220
+ $ this ->settings ['useSSL ' ] ? 'https:// ' : 'http:// ' ,
221
+ $ this ->settings ['realm ' ],
234
222
'. ' ,
235
- $ this ->parent -> settings ['domain ' ],
223
+ $ this ->settings ['domain ' ],
236
224
'/db/ ' ,
237
- isset ($ this ->options ['dbid ' ]) ? $ this ->options ['dbid ' ] : 'main ' ,
225
+ isset ($ this ->options ['dbid ' ]) && ! $ this -> settings [ ' flags ' ][ ' dbidAsParam ' ] ? $ this ->options ['dbid ' ] : 'main ' ,
238
226
'?act= ' ,
239
227
$ this ->action ,
240
- $ this ->parent -> settings ['flags ' ]['useXML ' ] ? '' : $ this ->payload
228
+ $ this ->settings ['flags ' ]['useXML ' ] ? '' : $ this ->payload
241
229
)));
242
230
243
- curl_setopt ($ ch , CURLOPT_PORT , $ this ->parent -> settings ['useSSL ' ] ? 443 : 80 );
231
+ curl_setopt ($ ch , CURLOPT_PORT , $ this ->settings ['useSSL ' ] ? 443 : 80 );
244
232
curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
245
233
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
234
+ curl_setopt ($ ch , CURLOPT_HEADER , true );
246
235
curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
247
236
248
- if ($ this ->parent -> settings ['flags ' ]['useXML ' ]){
237
+ if ($ this ->settings ['flags ' ]['useXML ' ]){
249
238
curl_setopt ($ ch , CURLOPT_POST , true );
250
239
curl_setopt ($ ch , CURLOPT_HTTPHEADER , array (
251
240
'POST /db/ ' .(isset ($ this ->options ['dbid ' ]) ? $ this ->options ['dbid ' ] : 'main ' ).' HTTP/1.0 ' ,
@@ -264,13 +253,32 @@ final public function transmit(){
264
253
$ errno = curl_errno ($ ch );
265
254
$ error = curl_error ($ ch );
266
255
256
+ $ headerSize = curl_getinfo ($ ch , CURLINFO_HEADER_SIZE );
257
+
267
258
curl_close ($ ch );
268
259
269
260
if ($ response === false ){
270
261
throw new QuickBaseError ($ errno , $ error );
271
262
}
272
263
273
- $ this ->xmlResponse = new SimpleXmlIterator ($ response );
264
+ $ headers = substr ($ response , 0 , $ headerSize );
265
+ $ body = substr ($ response , $ headerSize );
266
+
267
+ self ::parseCURLHeaders ($ headers );
268
+
269
+ if ($ headers ['Content-Type ' ] === 'application/xml ' ){
270
+ $ this ->response = array ();
271
+
272
+ $ xml = new SimpleXmlIterator ($ body );
273
+
274
+ $ this ->xml2Arr ($ xml , $ this ->response );
275
+
276
+ $ this ->cleanXml2Arr ($ this ->response );
277
+
278
+ $ this ->checkForAndHandleError ();
279
+ }else {
280
+ $ this ->response = $ body ;
281
+ }
274
282
275
283
return $ this ;
276
284
}
@@ -355,6 +363,19 @@ final protected static function cleanXml2Arr(&$arr){
355
363
}
356
364
}
357
365
366
+ final protected static function parseCURLHeaders (&$ headers ){
367
+ $ newHeaders = array ();
368
+ $ headers = explode ("\r\n" , $ headers );
369
+
370
+ foreach ($ headers as $ header ){
371
+ $ i = strpos ($ header , ': ' );
372
+
373
+ $ newHeaders [substr ($ header , 0 , $ i )] = substr ($ header , $ i + 2 );
374
+ }
375
+
376
+ $ headers = $ newHeaders ;
377
+ }
378
+
358
379
final protected static function xml2Arr ($ xml , &$ arr ){
359
380
for ($ xml ->rewind (); $ xml ->valid (); $ xml ->next ()){
360
381
$ key = $ xml ->key ();
@@ -408,7 +429,7 @@ class QuickBaseRequest {
408
429
409
430
final public static function API_Authenticate (&$ query ){
410
431
// API_Authenticate can only happen over SSL
411
- $ query ->parent -> settings ['useSSL ' ] = true ;
432
+ $ query ->settings ['useSSL ' ] = true ;
412
433
}
413
434
414
435
// final public static function API_ChangeGroupInfo(&$query){ }
@@ -427,16 +448,16 @@ final public static function API_Authenticate(&$query){
427
448
// final public static function API_DeleteRecord(&$query){ }
428
449
429
450
final public static function API_DoQuery (&$ query ){
430
- if (!isset ($ query ->options ['returnPercentage ' ]) && isset ($ query ->parent -> settings ['flags ' ]['returnPercentage ' ])){
431
- $ query ->options ['returnPercentage ' ] = $ query ->parent -> settings ['flags ' ]['returnPercentage ' ];
451
+ if (!isset ($ query ->options ['returnPercentage ' ]) && isset ($ query ->settings ['flags ' ]['returnPercentage ' ])){
452
+ $ query ->options ['returnPercentage ' ] = $ query ->settings ['flags ' ]['returnPercentage ' ];
432
453
}
433
454
434
- if (!isset ($ query ->options ['fmt ' ]) && isset ($ query ->parent -> settings ['flags ' ]['fmt ' ])){
435
- $ query ->options ['fmt ' ] = $ query ->parent -> settings ['flags ' ]['fmt ' ];
455
+ if (!isset ($ query ->options ['fmt ' ]) && isset ($ query ->settings ['flags ' ]['fmt ' ])){
456
+ $ query ->options ['fmt ' ] = $ query ->settings ['flags ' ]['fmt ' ];
436
457
}
437
458
438
- if (!isset ($ query ->options ['includeRids ' ]) && isset ($ query ->parent -> settings ['flags ' ]['includeRids ' ])){
439
- $ query ->options ['includeRids ' ] = $ query ->parent -> settings ['flags ' ]['includeRids ' ];
459
+ if (!isset ($ query ->options ['includeRids ' ]) && isset ($ query ->settings ['flags ' ]['includeRids ' ])){
460
+ $ query ->options ['includeRids ' ] = $ query ->settings ['flags ' ]['includeRids ' ];
440
461
}
441
462
}
442
463
@@ -448,7 +469,11 @@ final public static function API_DoQuery(&$query){
448
469
// final public static function API_GenAddRecordForm(&$query){ }
449
470
// final public static function API_GenResultsTable(&$query){ }
450
471
// final public static function API_GetAncestorInfo(&$query){ }
451
- // final public static function API_GetAppDTMInfo(&$query){ }
472
+
473
+ final public static function API_GetAppDTMInfo (&$ query ){
474
+ $ query ->settings ['flags ' ]['dbidAsParam ' ] = true ;
475
+ }
476
+
452
477
// final public static function API_GetDBPage(&$query){ }
453
478
// final public static function API_GetDBInfo(&$query){ }
454
479
// final public static function API_GetDBVar(&$query){ }
@@ -500,8 +525,13 @@ class QuickBaseResponse {
500
525
501
526
final public static function API_Authenticate (&$ query , &$ results ){
502
527
$ query ->parent ->settings ['ticket ' ] = $ results ['ticket ' ];
528
+ $ query ->settings ['ticket ' ] = $ results ['ticket ' ];
529
+
503
530
$ query ->parent ->settings ['username ' ] = $ query ->options ['username ' ];
531
+ $ query ->settings ['username ' ] = $ query ->options ['username ' ];
532
+
504
533
$ query ->parent ->settings ['password ' ] = $ query ->options ['password ' ];
534
+ $ query ->settings ['password ' ] = $ query ->options ['password ' ];
505
535
}
506
536
507
537
// final public static function API_ChangeGroupInfo(&$query, &$results){ }
@@ -654,11 +684,30 @@ final public static function API_GetSchema(&$query, &$results){
654
684
655
685
// final public static function API_GetRecordAsHTML(&$query, &$results){ }
656
686
// final public static function API_GetRecordInfo(&$query, &$results){ }
657
- // final public static function API_GetRoleInfo(&$query, &$results){ }
687
+
688
+ final public static function API_GetRoleInfo (&$ query , &$ results ){
689
+ if (isset ($ results ['roles ' ]['id ' ])){
690
+ $ results ['roles ' ] = array ( $ results ['roles ' ] );
691
+ }
692
+
693
+ for ($ i = 0 , $ l = count ($ results ['roles ' ]); $ i < $ l ; ++$ i ){
694
+ $ results ['roles ' ][$ i ]['access ' ] = array (
695
+ 'name ' => $ results ['roles ' ][$ i ]['access ' ]['_ ' ],
696
+ 'id ' => $ results ['roles ' ][$ i ]['access ' ]['id ' ]
697
+ );
698
+ }
699
+ }
700
+
658
701
// final public static function API_GetUserInfo(&$query, &$results){ }
659
702
// final public static function API_GetUserRole(&$query, &$results){ }
660
703
// final public static function API_GetUsersInGroup(&$query, &$results){ }
661
- // final public static function API_GrantedDBs(&$query, &$results){ }
704
+
705
+ final public static function API_GrantedDBs (&$ query , &$ results ){
706
+ if (isset ($ results ['databases ' ]['dbinfo ' ])){
707
+ $ results ['databases ' ] = $ results ['databases ' ]['dbinfo ' ];
708
+ }
709
+ }
710
+
662
711
// final public static function API_GrantedDBsForGroup(&$query, &$results){ }
663
712
// final public static function API_GrantedGroups(&$query, &$results){ }
664
713
// final public static function API_ImportFromCSV(&$query, &$results){ }
@@ -676,7 +725,17 @@ final public static function API_GetSchema(&$query, &$results){
676
725
// final public static function API_SetKeyField(&$query, &$results){ }
677
726
// final public static function API_SignOut(&$query, &$results){ }
678
727
// final public static function API_UploadFile(&$query, &$results){ }
679
- // final public static function API_UserRoles(&$query, &$results){ }
728
+
729
+ final public static function API_UserRoles (&$ query , &$ results ){
730
+ for ($ i = 0 , $ l = count ($ results ['users ' ]); $ i < $ l ; ++$ i ){
731
+ for ($ o = 0 , $ k = count ($ results ['users ' ][$ i ]['roles ' ]); $ o < $ k ; ++$ o ){
732
+ $ results ['users ' ][$ i ]['roles ' ][$ o ]['access ' ] = array (
733
+ 'name ' => $ results ['users ' ][$ i ]['roles ' ][$ o ]['access ' ]['_ ' ],
734
+ 'id ' => $ results ['users ' ][$ i ]['roles ' ][$ o ]['access ' ]['id ' ]
735
+ );
736
+ }
737
+ }
738
+ }
680
739
681
740
}
682
741
0 commit comments