ApiLoggerPlugin.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * @package ApiLoggerPlugin
  21. * @maintainer Brion Vibber <brion@status.net>
  22. */
  23. if (!defined('STATUSNET')) {
  24. exit(1);
  25. }
  26. class ApiLoggerPlugin extends Plugin
  27. {
  28. // Lower this to do random sampling of API requests rather than all.
  29. // 0.1 will check about 10% of hits, etc.
  30. public $frequency = 1.0;
  31. function onArgsInitialize($args)
  32. {
  33. if (isset($args['action'])) {
  34. $action = strtolower($args['action']);
  35. if (substr($action, 0, 3) == 'api') {
  36. if ($this->frequency < 1.0 && $this->frequency > 0.0) {
  37. $max = mt_getrandmax();
  38. $n = mt_rand() / $max;
  39. if ($n > $this->frequency) {
  40. return true;
  41. }
  42. }
  43. $uri = $_SERVER['REQUEST_URI'];
  44. $method = $_SERVER['REQUEST_METHOD'];
  45. $ssl = empty($_SERVER['HTTPS']) ? 'no' : 'yes';
  46. $cookie = empty($_SERVER['HTTP_COOKIE']) ? 'no' : 'yes';
  47. $etag = empty($_SERVER['HTTP_IF_MATCH']) ? 'no' : 'yes';
  48. $last = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? 'no' : 'yes';
  49. $auth = empty($_SERVER['HTTP_AUTHORIZATION']) ? 'no' : 'yes';
  50. if ($auth == 'no' && function_exists('apache_request_headers')) {
  51. // Sometimes Authorization doesn't make it into $_SERVER.
  52. // Probably someone thought it was scary.
  53. $headers = apache_request_headers();
  54. if (isset($headers['Authorization'])) {
  55. $auth = 'yes';
  56. }
  57. }
  58. $agent = empty($_SERVER['HTTP_USER_AGENT']) ? 'no' : $_SERVER['HTTP_USER_AGENT'];
  59. $query = (strpos($uri, '?') === false) ? 'no' : 'yes';
  60. if ($query == 'yes') {
  61. if (preg_match('/\?since_id=\d+$/', $uri)) {
  62. $query = 'since_id';
  63. }
  64. }
  65. common_log(LOG_INFO, "STATLOG action:$action method:$method ssl:$ssl query:$query cookie:$cookie auth:$auth ifmatch:$etag ifmod:$last agent:$agent");
  66. }
  67. }
  68. return true;
  69. }
  70. function onPluginVersion(&$versions)
  71. {
  72. $versions[] = array('name' => 'ApiLogger',
  73. 'version' => GNUSOCIAL_VERSION,
  74. 'author' => 'Brion Vibber',
  75. 'homepage' => 'http://status.net/wiki/Plugin:ApiLogger',
  76. 'rawdescription' =>
  77. // TRANS: Plugin description.
  78. _m('Allows random sampling of API requests.'));
  79. return true;
  80. }
  81. }