@@ -2109,6 +2109,10 @@ public function getPolicyName() {
2109
2109
// -----------------------------------------------------------------------
2110
2110
class APICore {
2111
2111
2112
+ // request debug
2113
+ public $ debug = false ;
2114
+ public $ debugMessages = [];
2115
+
2112
2116
public $ curl_handle ;
2113
2117
public $ server_url ;
2114
2118
public $ start_time ;
@@ -2118,8 +2122,9 @@ class APICore {
2118
2122
public $ xsrf_not_set ;
2119
2123
//public $cookie_data;
2120
2124
2121
- public function __construct ($ SERVER_URL ) {
2125
+ public function __construct ($ SERVER_URL , $ debug = false ) {
2122
2126
$ this ->server_url = $ SERVER_URL ;
2127
+ $ this ->debug = $ debug ;
2123
2128
$ this ->init ($ SERVER_URL );
2124
2129
}
2125
2130
@@ -2184,7 +2189,10 @@ protected function doGET($url) {
2184
2189
);
2185
2190
curl_setopt ($ this ->curl_handle , CURLOPT_HTTPHEADER , $ headers );
2186
2191
}
2187
- return curl_exec ($ this ->curl_handle );
2192
+ $ this ->preRequestDebug ();
2193
+ $ result = curl_exec ($ this ->curl_handle );
2194
+ $ result = $ this ->afterRequestDebug ('GET ' , $ url , '' , $ result , true );
2195
+ return $ result ;
2188
2196
}
2189
2197
2190
2198
protected function doPOST ($ url , $ postdata ) {
@@ -2206,7 +2214,10 @@ protected function doPOST($url, $postdata) {
2206
2214
// var_dump( $this->cookie_data);
2207
2215
curl_setopt ($ this ->curl_handle , CURLOPT_HTTPHEADER , $ headers );
2208
2216
}
2209
- return curl_exec ($ this ->curl_handle );
2217
+ $ this ->preRequestDebug ();
2218
+ $ result = curl_exec ($ this ->curl_handle );
2219
+ $ result = $ this ->afterRequestDebug ('POST ' , $ url , $ postdata , $ result , true );
2220
+ return $ result ;
2210
2221
}
2211
2222
2212
2223
protected function parseHeader ($ result )
@@ -2254,6 +2265,70 @@ protected function parseHeader($result)
2254
2265
return $ buffer ;
2255
2266
}
2256
2267
2268
+ protected function preRequestDebug ()
2269
+ {
2270
+ // clean up the debug buffer
2271
+ $ this ->debugMessages = [];
2272
+
2273
+ if ($ this ->debug ) {
2274
+ curl_setopt ($ this ->curl_handle , CURLOPT_HEADER , true );
2275
+ curl_setopt ($ this ->curl_handle , CURLINFO_HEADER_OUT , true );
2276
+ }
2277
+ }
2278
+
2279
+ protected function afterRequestDebug (string $ method , string $ url , string $ postData , string $ result , $ removeHeaders = false ): string
2280
+ {
2281
+ if ($ this ->debug ) {
2282
+
2283
+ // request
2284
+ $ this ->debugMessages ['Request ' ] = "$ method $ url " ;
2285
+ $ body = [];
2286
+ parse_str ($ postData , $ body );
2287
+ $ this ->debugMessages ['Request Body ' ] = $ body ;
2288
+
2289
+ // request headers
2290
+ $ rawRequest = curl_getinfo ($ this ->curl_handle , CURLINFO_HEADER_OUT );
2291
+ $ lines = explode (PHP_EOL , trim ($ rawRequest ));
2292
+ array_shift ($ lines ); // remove the first line and keep the headers
2293
+ $ headers = [];
2294
+ foreach ($ lines as $ line ) {
2295
+ [$ header , $ value ] = explode (': ' , $ line );
2296
+ $ headers [trim ($ header )] = trim ($ value );
2297
+ }
2298
+ $ this ->debugMessages ['Request Headers ' ] = $ headers ;
2299
+
2300
+ // request cookies
2301
+ $ rawCookies = curl_getinfo ($ this ->curl_handle , CURLINFO_COOKIELIST );
2302
+ $ cookies = [];
2303
+ foreach ($ rawCookies as $ item ) {
2304
+ $ pieces = preg_split ('/\s+/ ' , $ item );
2305
+ $ cookies [$ pieces [5 ]] = $ pieces [6 ];
2306
+ }
2307
+ $ this ->debugMessages ['Request Cookies ' ] = $ cookies ;
2308
+
2309
+ // Response code
2310
+ $ this ->debugMessages ['Response Code ' ] = curl_getinfo ($ this ->curl_handle , CURLINFO_HTTP_CODE );
2311
+
2312
+ // Response Headers and body
2313
+ [$ rawHeaders , $ body ] = explode (PHP_EOL . PHP_EOL , $ result );
2314
+ $ lines = explode (PHP_EOL , trim ($ rawHeaders ));
2315
+ array_shift ($ lines );
2316
+ $ headers = [];
2317
+ foreach ($ lines as $ line ) {
2318
+ [$ header , $ value ] = explode (': ' , $ line );
2319
+ $ headers [trim ($ header )] = trim ($ value );
2320
+ }
2321
+ $ this ->debugMessages ['Response Headers ' ] = $ headers ;
2322
+ $ body = trim ($ body );
2323
+ $ this ->debugMessages ['Response Body ' ] = $ body ;
2324
+
2325
+ if ($ removeHeaders ) {
2326
+ return $ body ;
2327
+ }
2328
+ }
2329
+ return $ result ;
2330
+ }
2331
+
2257
2332
protected function doPOSTWithHeader ($ url , $ postdata ) {
2258
2333
//clear token first
2259
2334
$ this ->xsrf_token = "" ;
@@ -2264,7 +2339,10 @@ protected function doPOSTWithHeader($url, $postdata) {
2264
2339
curl_setopt ($ this ->curl_handle , CURLOPT_POSTFIELDS , $ postdata );
2265
2340
curl_setopt ($ this ->curl_handle , CURLOPT_RETURNTRANSFER , 1 );
2266
2341
curl_setopt ($ this ->curl_handle , CURLOPT_HEADER , 1 );
2267
- $ result = curl_exec ($ this ->curl_handle );
2342
+ $ this ->preRequestDebug ();
2343
+ $ result = curl_exec ($ this ->curl_handle );
2344
+ $ this ->afterRequestDebug ('POST ' , $ url , $ postdata , $ result );
2345
+
2268
2346
return $ this ->parseHeader ($ result );
2269
2347
}
2270
2348
@@ -2279,7 +2357,10 @@ protected function doPOSTWithAgent($url, $postdata, $agent )
2279
2357
curl_setopt ($ this ->curl_handle , CURLOPT_POSTFIELDS , $ postdata );
2280
2358
curl_setopt ($ this ->curl_handle , CURLOPT_RETURNTRANSFER , TRUE );
2281
2359
curl_setopt ($ this ->curl_handle , CURLOPT_HEADER , 1 );
2282
- $ result = curl_exec ($ this ->curl_handle );
2360
+ $ this ->preRequestDebug ();
2361
+ $ result = curl_exec ($ this ->curl_handle );
2362
+ $ this ->afterRequestDebug ('POST ' , $ url , $ postdata , $ result );
2363
+
2283
2364
return $ this ->parseHeader ($ result );
2284
2365
}
2285
2366
@@ -2371,8 +2452,8 @@ protected function doChunkedUpload($url, $filechunkpath, $filename) {
2371
2452
2372
2453
class CloudAPI extends APICore {
2373
2454
2374
- public function __construct ($ SERVER_URL ) {
2375
- parent ::__construct ($ SERVER_URL );
2455
+ public function __construct ($ SERVER_URL , $ debug = false ) {
2456
+ parent ::__construct ($ SERVER_URL , $ debug );
2376
2457
}
2377
2458
2378
2459
public function __destruct () {
@@ -4562,8 +4643,8 @@ public function getfilelistforshare($path, $start = "", $limit = "", $sortby , $
4562
4643
class CloudAdminAPI extends APICore
4563
4644
{
4564
4645
4565
- public function __construct ($ SERVER_URL ) {
4566
- parent ::__construct ($ SERVER_URL );
4646
+ public function __construct ($ SERVER_URL , $ debug = false ) {
4647
+ parent ::__construct ($ SERVER_URL , $ debug );
4567
4648
}
4568
4649
4569
4650
public function __destruct () {
0 commit comments