]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ApiLogger/ApiLoggerPlugin.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / ApiLogger / ApiLoggerPlugin.php
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 /**
21  * @package ApiLoggerPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET')) {
26     exit(1);
27 }
28
29 class ApiLoggerPlugin extends Plugin
30 {
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;
34
35     function onArgsInitialize($args)
36     {
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) {
44                         return true;
45                     }
46                 }
47                 $uri = $_SERVER['REQUEST_URI'];
48
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'])) {
60                         $auth = 'yes';
61                     }
62                 }
63                 $agent = empty($_SERVER['HTTP_USER_AGENT']) ? 'no' : $_SERVER['HTTP_USER_AGENT'];
64
65                 $query = (strpos($uri, '?') === false) ? 'no' : 'yes';
66                 if ($query == 'yes') {
67                     if (preg_match('/\?since_id=\d+$/', $uri)) {
68                         $query = 'since_id';
69                     }
70                 }
71
72                 common_log(LOG_INFO, "STATLOG action:$action method:$method ssl:$ssl query:$query cookie:$cookie auth:$auth ifmatch:$etag ifmod:$last agent:$agent");
73             }
74         }
75         return true;
76     }
77
78     function onPluginVersion(array &$versions)
79     {
80         $versions[] = array('name' => 'ApiLogger',
81                             'version' => GNUSOCIAL_VERSION,
82                             'author' => 'Brion Vibber',
83                             'homepage' => 'http://status.net/wiki/Plugin:ApiLogger',
84                             'rawdescription' =>
85                             // TRANS: Plugin description.
86                             _m('Allows random sampling of API requests.'));
87         return true;
88     }
89 }