Example #1
0
 /**
  * Logs the request to the database, sends any cached output.
  * Also called on shutdown, to make sure we always log the request being handled.
  */
 static function flush()
 {
     if (OAuthRequestLogger::$logging) {
         OAuthRequestLogger::$logging = false;
         if (is_null(OAuthRequestLogger::$sent)) {
             // What has been sent to the user-agent?
             $data = ob_get_contents();
             if (strlen($data) > 0) {
                 ob_end_flush();
             } elseif (ob_get_level()) {
                 ob_end_clean();
             }
             $hs = headers_list();
             $sent = implode("\n", $hs) . "\n\n" . $data;
         } else {
             // The request we sent
             $sent = OAuthRequestLogger::$sent;
         }
         if (is_null(OAuthRequestLogger::$received)) {
             // Build the request we received
             $hs0 = self::getAllHeaders();
             $hs = array();
             foreach ($hs0 as $h => $v) {
                 $hs[] = "{$h}: {$v}";
             }
             $data = '';
             $fh = @fopen('php://input', 'r');
             if ($fh) {
                 while (!feof($fh)) {
                     $s = fread($fh, 1024);
                     if (is_string($s)) {
                         $data .= $s;
                     }
                 }
                 fclose($fh);
             }
             $received = implode("\n", $hs) . "\n\n" . $data;
         } else {
             // The answer we received
             $received = OAuthRequestLogger::$received;
         }
         // The request base string
         if (OAuthRequestLogger::$request_object) {
             $base_string = OAuthRequestLogger::$request_object->signatureBaseString();
         } else {
             $base_string = '';
         }
         // Figure out to what keys we want to log this request
         $keys = array();
         if (OAuthRequestLogger::$request_object) {
             $consumer_key = OAuthRequestLogger::$request_object->getParam('oauth_consumer_key', true);
             $token = OAuthRequestLogger::$request_object->getParam('oauth_token', true);
             switch (get_class(OAuthRequestLogger::$request_object)) {
                 // tokens are access/request tokens by a consumer
                 case 'OAuthServer':
                 case 'OAuthRequestVerifier':
                     $keys['ocr_consumer_key'] = $consumer_key;
                     $keys['oct_token'] = $token;
                     break;
                     // tokens are access/request tokens to a server
                 // tokens are access/request tokens to a server
                 case 'OAuthRequester':
                 case 'OAuthRequestSigner':
                     $keys['osr_consumer_key'] = $consumer_key;
                     $keys['ost_token'] = $token;
                     break;
             }
         }
         // Log the request
         if (OAuthRequestLogger::$store_log) {
             $store = OAuthStore::instance();
             $store->addLog($keys, $received, $sent, $base_string, OAuthRequestLogger::$note, OAuthRequestLogger::$user_id);
         }
         OAuthRequestLogger::$log[] = array('keys' => $keys, 'received' => $received, 'sent' => $sent, 'base_string' => $base_string, 'note' => OAuthRequestLogger::$note);
     }
 }