3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
21 * @package ApiLoggerPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 if (!defined('STATUSNET')) {
29 class ApiLoggerPlugin extends Plugin
31 // Lower this to do random sampling of API requests rather than all.
32 // 0.1 will check about 10% of hits, etc.
33 public $frequency = 1.0;
35 function onArgsInitialize($args)
37 if (isset($args['action'])) {
38 $action = strtolower($args['action']);
39 if (substr($action, 0, 3) == 'api') {
40 if ($this->frequency < 1.0 && $this->frequency > 0.0) {
41 $max = mt_getrandmax();
42 $n = mt_rand() / $max;
43 if ($n > $this->frequency) {
47 $uri = $_SERVER['REQUEST_URI'];
49 $method = $_SERVER['REQUEST_METHOD'];
50 $ssl = empty($_SERVER['HTTPS']) ? 'no' : 'yes';
51 $cookie = empty($_SERVER['HTTP_COOKIE']) ? 'no' : 'yes';
52 $etag = empty($_SERVER['HTTP_IF_MATCH']) ? 'no' : 'yes';
53 $last = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? 'no' : 'yes';
54 $auth = empty($_SERVER['HTTP_AUTHORIZATION']) ? 'no' : 'yes';
55 if ($auth == 'no' && function_exists('apache_request_headers')) {
56 // Sometimes Authorization doesn't make it into $_SERVER.
57 // Probably someone thought it was scary.
58 $headers = apache_request_headers();
59 if (isset($headers['Authorization'])) {
63 $agent = empty($_SERVER['HTTP_USER_AGENT']) ? 'no' : $_SERVER['HTTP_USER_AGENT'];
65 $query = (strpos($uri, '?') === false) ? 'no' : 'yes';
66 if ($query == 'yes') {
67 if (preg_match('/\?since_id=\d+$/', $uri)) {
72 common_log(LOG_INFO, "STATLOG action:$action method:$method ssl:$ssl query:$query cookie:$cookie auth:$auth ifmatch:$etag ifmod:$last agent:$agent");
78 function onPluginVersion(array &$versions)
80 $versions[] = array('name' => 'ApiLogger',
81 'version' => GNUSOCIAL_VERSION,
82 'author' => 'Brion Vibber',
83 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/ApiLogger',
85 // TRANS: Plugin description.
86 _m('Allows random sampling of API requests.'));