]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/PostDebug/PostDebugPlugin.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / PostDebug / PostDebugPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Debugging helper plugin -- records detailed data on POSTs to log
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Sample
24  * @package   StatusNet
25  * @author    Brion Vibber <brionv@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 class PostDebugPlugin extends Plugin
36 {
37     /**
38      * Set to a directory to dump individual items instead of
39      * sending to the debug log
40      */
41     public $dir=false;
42
43     public function onArgsInitialize(&$args)
44     {
45         if (isset($_SERVER['REQUEST_METHOD']) &&
46             $_SERVER['REQUEST_METHOD'] == 'POST') {
47             $this->doDebug();
48         }
49     }
50
51     public function onPluginVersion(array &$versions)
52     {
53         $versions[] = array('name' => 'PostDebug',
54                             'version' => GNUSOCIAL_VERSION,
55                             'author' => 'Brion Vibber',
56                             'homepage' => 'http://status.net/wiki/Plugin:PostDebug',
57                             'rawdescription' =>
58                             // TRANS: Plugin description.
59                             _m('Debugging tool to record request details on POST.'));
60         return true;
61     }
62
63     protected function doDebug()
64     {
65         $data = array('timestamp' => gmdate('r'),
66                       'remote_addr' => @$_SERVER['REMOTE_ADDR'],
67                       'url' => @$_SERVER['REQUEST_URI'],
68                       'have_session' => common_have_session(),
69                       'logged_in' => common_logged_in(),
70                       'is_real_login' => common_is_real_login(),
71                       'user' => common_logged_in() ? common_current_user()->nickname : null,
72                       'headers' => $this->getHttpHeaders(),
73                       'post_data' => $this->sanitizePostData($_POST));
74         $this->saveDebug($data);
75     }
76
77     protected function saveDebug($data)
78     {
79         $output = var_export($data, true);
80         if ($this->dir) {
81             $file = $this->dir . DIRECTORY_SEPARATOR . $this->logFileName();
82             file_put_contents($file, $output);
83         } else {
84             common_log(LOG_DEBUG, "PostDebug: $output");
85         }
86     }
87
88     protected function logFileName()
89     {
90         $base = common_request_id();
91         $base = preg_replace('/^(.+?) .*$/', '$1', $base);
92         $base = str_replace(':', '-', $base);
93         $base = rawurlencode($base);
94         return $base;
95     }
96
97     protected function getHttpHeaders()
98     {
99         if (function_exists('getallheaders')) {
100             $headers = getallheaders();
101         } else {
102             $headers = array();
103             $prefix = 'HTTP_';
104             $prefixLen = strlen($prefix);
105             foreach ($_SERVER as $key => $val) {
106                 if (substr($key, 0, $prefixLen) == $prefix) {
107                     $header = $this->normalizeHeader(substr($key, $prefixLen));
108                     $headers[$header] = $val;
109                 }
110             }
111         }
112         foreach ($headers as $header => $val) {
113             if (strtolower($header) == 'cookie') {
114                 $headers[$header] = $this->sanitizeCookies($val);
115             }
116         }
117         return $headers;
118     }
119
120     protected function normalizeHeader($key)
121     {
122         return implode('-',
123                        array_map('ucfirst',
124                                  explode("_",
125                                          strtolower($key))));
126     }
127
128     function sanitizeCookies($val)
129     {
130         $blacklist = array(session_name(), 'rememberme');
131         foreach ($blacklist as $name) {
132             $val = preg_replace("/(^|;\s*)({$name}=)(.*?)(;|$)/",
133                                 "$1$2########$4",
134                                 $val);
135         }
136         return $val;
137     }
138
139     function sanitizePostData($data)
140     {
141         $blacklist = array('password', 'confirm', 'token');
142         foreach ($data as $key => $val) {
143             if (in_array($key, $blacklist)) {
144                 $data[$key] = '########';
145             }
146         }
147         return $data;
148     }
149 }