ApiLoggerPlugin.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. const PLUGIN_VERSION = '2.0.0';
  29. // Lower this to do random sampling of API requests rather than all.
  30. // 0.1 will check about 10% of hits, etc.
  31. public $frequency = 1.0;
  32. function onArgsInitialize($args)
  33. {
  34. if (isset($args['action'])) {
  35. $action = strtolower($args['action']);
  36. if (substr($action, 0, 3) == 'api') {
  37. if ($this->frequency < 1.0 && $this->frequency > 0.0) {
  38. $max = mt_getrandmax();
  39. $n = mt_rand() / $max;
  40. if ($n > $this->frequency) {
  41. return true;
  42. }
  43. }
  44. $uri = $_SERVER['REQUEST_URI'];
  45. $method = $_SERVER['REQUEST_METHOD'];
  46. $ssl = empty($_SERVER['HTTPS']) ? 'no' : 'yes';
  47. $cookie = empty($_SERVER['HTTP_COOKIE']) ? 'no' : 'yes';
  48. $etag = empty($_SERVER['HTTP_IF_MATCH']) ? 'no' : 'yes';
  49. $last = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? 'no' : 'yes';
  50. $auth = empty($_SERVER['HTTP_AUTHORIZATION']) ? 'no' : 'yes';
  51. if ($auth == 'no' && function_exists('apache_request_headers')) {
  52. // Sometimes Authorization doesn't make it into $_SERVER.
  53. // Probably someone thought it was scary.
  54. $headers = apache_request_headers();
  55. if (isset($headers['Authorization'])) {
  56. $auth = 'yes';
  57. }
  58. }
  59. $agent = empty($_SERVER['HTTP_USER_AGENT']) ? 'no' : $_SERVER['HTTP_USER_AGENT'];
  60. $query = (strpos($uri, '?') === false) ? 'no' : 'yes';
  61. if ($query == 'yes') {
  62. if (preg_match('/\?since_id=\d+$/', $uri)) {
  63. $query = 'since_id';
  64. }
  65. }
  66. common_log(LOG_INFO, "STATLOG action:$action method:$method ssl:$ssl query:$query cookie:$cookie auth:$auth ifmatch:$etag ifmod:$last agent:$agent");
  67. }
  68. }
  69. return true;
  70. }
  71. public function onPluginVersion(array &$versions): bool
  72. {
  73. $versions[] = array('name' => 'ApiLogger',
  74. 'version' => self::PLUGIN_VERSION,
  75. 'author' => 'Brion Vibber',
  76. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/ApiLogger',
  77. 'rawdescription' =>
  78. // TRANS: Plugin description.
  79. _m('Allows random sampling of API requests.'));
  80. return true;
  81. }
  82. }