3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Debugging helper plugin -- records detailed data on POSTs to log
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
35 class PostDebugPlugin extends Plugin
38 * Set to a directory to dump individual items instead of
39 * sending to the debug log
43 public function onArgsInitialize(&$args)
45 if (isset($_SERVER['REQUEST_METHOD']) &&
46 $_SERVER['REQUEST_METHOD'] == 'POST') {
51 public function onPluginVersion(array &$versions)
53 $versions[] = array('name' => 'PostDebug',
54 'version' => GNUSOCIAL_VERSION,
55 'author' => 'Brion Vibber',
56 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/PostDebug',
58 // TRANS: Plugin description.
59 _m('Debugging tool to record request details on POST.'));
63 protected function doDebug()
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);
77 protected function saveDebug($data)
79 $output = var_export($data, true);
81 $file = $this->dir . DIRECTORY_SEPARATOR . $this->logFileName();
82 file_put_contents($file, $output);
84 common_log(LOG_DEBUG, "PostDebug: $output");
88 protected function logFileName()
90 $base = common_request_id();
91 $base = preg_replace('/^(.+?) .*$/', '$1', $base);
92 $base = str_replace(':', '-', $base);
93 $base = rawurlencode($base);
97 protected function getHttpHeaders()
99 if (function_exists('getallheaders')) {
100 $headers = getallheaders();
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;
112 foreach ($headers as $header => $val) {
113 if (strtolower($header) == 'cookie') {
114 $headers[$header] = $this->sanitizeCookies($val);
120 protected function normalizeHeader($key)
128 function sanitizeCookies($val)
130 $blacklist = array(session_name(), 'rememberme');
131 foreach ($blacklist as $name) {
132 $val = preg_replace("/(^|;\s*)({$name}=)(.*?)(;|$)/",
139 function sanitizePostData($data)
141 $blacklist = array('password', 'confirm', 'token');
142 foreach ($data as $key => $val) {
143 if (in_array($key, $blacklist)) {
144 $data[$key] = '########';