PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/oauth/OAuthRequestLogger.php

http://github.com/jasonhinkle/phreeze
PHP | 318 lines | 175 code | 36 blank | 107 comment | 19 complexity | 8c59c1d2dd8b07d5f3a9367bef10d133 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Log OAuth requests
  4. *
  5. * @version $Id: OAuthRequestLogger.php 98 2010-03-08 12:48:59Z brunobg@corollarium.com $
  6. * @author Marc Worrell <marcw@pobox.com>
  7. * @date Dec 7, 2007 12:22:43 PM
  8. *
  9. *
  10. * The MIT License
  11. *
  12. * Copyright (c) 2007-2008 Mediamatic Lab
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. * THE SOFTWARE.
  31. */
  32. class OAuthRequestLogger
  33. {
  34. static private $logging = 0;
  35. static private $enable_logging = null;
  36. static private $store_log = null;
  37. static private $note = '';
  38. static private $user_id = null;
  39. static private $request_object = null;
  40. static private $sent = null;
  41. static private $received = null;
  42. static private $log = array();
  43. /**
  44. * Start any logging, checks the system configuration if logging is needed.
  45. *
  46. * @param OAuthRequest $request_object
  47. */
  48. static function start ( $request_object = null )
  49. {
  50. if (defined('OAUTH_LOG_REQUEST'))
  51. {
  52. if (is_null(OAuthRequestLogger::$enable_logging))
  53. {
  54. OAuthRequestLogger::$enable_logging = true;
  55. }
  56. if (is_null(OAuthRequestLogger::$store_log))
  57. {
  58. OAuthRequestLogger::$store_log = true;
  59. }
  60. }
  61. if (OAuthRequestLogger::$enable_logging && !OAuthRequestLogger::$logging)
  62. {
  63. OAuthRequestLogger::$logging = true;
  64. OAuthRequestLogger::$request_object = $request_object;
  65. ob_start();
  66. // Make sure we flush our log entry when we stop the request (eg on an exception)
  67. register_shutdown_function(array('OAuthRequestLogger','flush'));
  68. }
  69. }
  70. /**
  71. * Force logging, needed for performing test connects independent from the debugging setting.
  72. *
  73. * @param boolean store_log (optional) true to store the log in the db
  74. */
  75. static function enableLogging ( $store_log = null )
  76. {
  77. OAuthRequestLogger::$enable_logging = true;
  78. if (!is_null($store_log))
  79. {
  80. OAuthRequestLogger::$store_log = $store_log;
  81. }
  82. }
  83. /**
  84. * Logs the request to the database, sends any cached output.
  85. * Also called on shutdown, to make sure we always log the request being handled.
  86. */
  87. static function flush ()
  88. {
  89. if (OAuthRequestLogger::$logging)
  90. {
  91. OAuthRequestLogger::$logging = false;
  92. if (is_null(OAuthRequestLogger::$sent))
  93. {
  94. // What has been sent to the user-agent?
  95. $data = ob_get_contents();
  96. if (strlen($data) > 0)
  97. {
  98. ob_end_flush();
  99. }
  100. elseif (ob_get_level())
  101. {
  102. ob_end_clean();
  103. }
  104. $hs = headers_list();
  105. $sent = implode("\n", $hs) . "\n\n" . $data;
  106. }
  107. else
  108. {
  109. // The request we sent
  110. $sent = OAuthRequestLogger::$sent;
  111. }
  112. if (is_null(OAuthRequestLogger::$received))
  113. {
  114. // Build the request we received
  115. $hs0 = self::getAllHeaders();
  116. $hs = array();
  117. foreach ($hs0 as $h => $v)
  118. {
  119. $hs[] = "$h: $v";
  120. }
  121. // updated by j.hinkle to avoid re-reading php://input, which messes up PUT requests
  122. include_once('verysimple/HTTP/RequestUtil.php');
  123. $body = RequestUtil::GetBody();
  124. // $fh = @fopen('php://input', 'r');
  125. // if ($fh)
  126. // {
  127. // while (!feof($fh))
  128. // {
  129. // $s = fread($fh, 1024);
  130. // if (is_string($s))
  131. // {
  132. // $data .= $s;
  133. // }
  134. // }
  135. // fclose($fh);
  136. // }
  137. $received = implode("\n", $hs) . "\n\n" . $data;
  138. }
  139. else
  140. {
  141. // The answer we received
  142. $received = OAuthRequestLogger::$received;
  143. }
  144. // The request base string
  145. if (OAuthRequestLogger::$request_object)
  146. {
  147. $base_string = OAuthRequestLogger::$request_object->signatureBaseString();
  148. }
  149. else
  150. {
  151. $base_string = '';
  152. }
  153. // Figure out to what keys we want to log this request
  154. $keys = array();
  155. if (OAuthRequestLogger::$request_object)
  156. {
  157. $consumer_key = OAuthRequestLogger::$request_object->getParam('oauth_consumer_key', true);
  158. $token = OAuthRequestLogger::$request_object->getParam('oauth_token', true);
  159. switch (get_class(OAuthRequestLogger::$request_object))
  160. {
  161. // tokens are access/request tokens by a consumer
  162. case 'OAuthServer':
  163. case 'OAuthRequestVerifier':
  164. $keys['ocr_consumer_key'] = $consumer_key;
  165. $keys['oct_token'] = $token;
  166. break;
  167. // tokens are access/request tokens to a server
  168. case 'OAuthRequester':
  169. case 'OAuthRequestSigner':
  170. $keys['osr_consumer_key'] = $consumer_key;
  171. $keys['ost_token'] = $token;
  172. break;
  173. }
  174. }
  175. // Log the request
  176. if (OAuthRequestLogger::$store_log)
  177. {
  178. $store = OAuthStore::instance();
  179. $store->addLog($keys, $received, $sent, $base_string, OAuthRequestLogger::$note, OAuthRequestLogger::$user_id);
  180. }
  181. OAuthRequestLogger::$log[] = array(
  182. 'keys' => $keys,
  183. 'received' => $received,
  184. 'sent' => $sent,
  185. 'base_string' => $base_string,
  186. 'note' => OAuthRequestLogger::$note
  187. );
  188. }
  189. }
  190. /**
  191. * Add a note, used by the OAuthException2 to log all exceptions.
  192. *
  193. * @param string note
  194. */
  195. static function addNote ( $note )
  196. {
  197. OAuthRequestLogger::$note .= $note . "\n\n";
  198. }
  199. /**
  200. * Set the OAuth request object being used
  201. *
  202. * @param OAuthRequest request_object
  203. */
  204. static function setRequestObject ( $request_object )
  205. {
  206. OAuthRequestLogger::$request_object = $request_object;
  207. }
  208. /**
  209. * Set the relevant user (defaults to the current user)
  210. *
  211. * @param int user_id
  212. */
  213. static function setUser ( $user_id )
  214. {
  215. OAuthRequestLogger::$user_id = $user_id;
  216. }
  217. /**
  218. * Set the request we sent
  219. *
  220. * @param string request
  221. */
  222. static function setSent ( $request )
  223. {
  224. OAuthRequestLogger::$sent = $request;
  225. }
  226. /**
  227. * Set the reply we received
  228. *
  229. * @param string request
  230. */
  231. static function setReceived ( $reply )
  232. {
  233. OAuthRequestLogger::$received = $reply;
  234. }
  235. /**
  236. * Get the the log till now
  237. *
  238. * @return array
  239. */
  240. static function getLog ()
  241. {
  242. return OAuthRequestLogger::$log;
  243. }
  244. /**
  245. * helper to try to sort out headers for people who aren't running apache,
  246. * or people who are running PHP as FastCGI.
  247. *
  248. * @return array of request headers as associative array.
  249. */
  250. public static function getAllHeaders() {
  251. $retarr = array();
  252. $headers = array();
  253. if (function_exists('apache_request_headers')) {
  254. $headers = apache_request_headers();
  255. ksort($headers);
  256. return $headers;
  257. } else {
  258. $headers = array_merge($_ENV, $_SERVER);
  259. foreach ($headers as $key => $val) {
  260. //we need this header
  261. if (strpos(strtolower($key), 'content-type') !== FALSE)
  262. continue;
  263. if (strtoupper(substr($key, 0, 5)) != "HTTP_")
  264. unset($headers[$key]);
  265. }
  266. }
  267. //Normalize this array to Cased-Like-This structure.
  268. foreach ($headers AS $key => $value) {
  269. $key = preg_replace('/^HTTP_/i', '', $key);
  270. $key = str_replace(
  271. " ",
  272. "-",
  273. ucwords(strtolower(str_replace(array("-", "_"), " ", $key)))
  274. );
  275. $retarr[$key] = $value;
  276. }
  277. ksort($retarr);
  278. return $retarr;
  279. }
  280. }
  281. /* vi:set ts=4 sts=4 sw=4 binary noeol: */
  282. ?>