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
37 const PLUGIN_VERSION = '2.0.0';
40 * Set to a directory to dump individual items instead of
41 * sending to the debug log
45 public function onArgsInitialize(&$args)
47 if (isset($_SERVER['REQUEST_METHOD']) &&
48 $_SERVER['REQUEST_METHOD'] == 'POST') {
53 public function onPluginVersion(array &$versions)
55 $versions[] = array('name' => 'PostDebug',
56 'version' => self::PLUGIN_VERSION,
57 'author' => 'Brion Vibber',
58 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/PostDebug',
60 // TRANS: Plugin description.
61 _m('Debugging tool to record request details on POST.'));
65 protected function doDebug()
67 $data = array('timestamp' => gmdate('r'),
68 'remote_addr' => @$_SERVER['REMOTE_ADDR'],
69 'url' => @$_SERVER['REQUEST_URI'],
70 'have_session' => common_have_session(),
71 'logged_in' => common_logged_in(),
72 'is_real_login' => common_is_real_login(),
73 'user' => common_logged_in() ? common_current_user()->nickname : null,
74 'headers' => $this->getHttpHeaders(),
75 'post_data' => $this->sanitizePostData($_POST));
76 $this->saveDebug($data);
79 protected function saveDebug($data)
81 $output = var_export($data, true);
83 $file = $this->dir . DIRECTORY_SEPARATOR . $this->logFileName();
84 file_put_contents($file, $output);
86 common_log(LOG_DEBUG, "PostDebug: $output");
90 protected function logFileName()
92 $base = common_request_id();
93 $base = preg_replace('/^(.+?) .*$/', '$1', $base);
94 $base = str_replace(':', '-', $base);
95 $base = rawurlencode($base);
99 protected function getHttpHeaders()
101 if (function_exists('getallheaders')) {
102 $headers = getallheaders();
106 $prefixLen = strlen($prefix);
107 foreach ($_SERVER as $key => $val) {
108 if (substr($key, 0, $prefixLen) == $prefix) {
109 $header = $this->normalizeHeader(substr($key, $prefixLen));
110 $headers[$header] = $val;
114 foreach ($headers as $header => $val) {
115 if (strtolower($header) == 'cookie') {
116 $headers[$header] = $this->sanitizeCookies($val);
122 protected function normalizeHeader($key)
130 function sanitizeCookies($val)
132 $blacklist = array(session_name(), 'rememberme');
133 foreach ($blacklist as $name) {
134 $val = preg_replace("/(^|;\s*)({$name}=)(.*?)(;|$)/",
141 function sanitizePostData($data)
143 $blacklist = array('password', 'confirm', 'token');
144 foreach ($data as $key => $val) {
145 if (in_array($key, $blacklist)) {
146 $data[$key] = '########';