]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/PostDebug/PostDebugPlugin.php
[TRANSLATION] Update license and copyright notice in translation files
[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     const PLUGIN_VERSION = '2.0.0';
38
39     /**
40      * Set to a directory to dump individual items instead of
41      * sending to the debug log
42      */
43     public $dir=false;
44
45     public function onArgsInitialize(&$args)
46     {
47         if (isset($_SERVER['REQUEST_METHOD']) &&
48             $_SERVER['REQUEST_METHOD'] == 'POST') {
49             $this->doDebug();
50         }
51     }
52
53     public function onPluginVersion(array &$versions)
54     {
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',
59                             'rawdescription' =>
60                             // TRANS: Plugin description.
61                             _m('Debugging tool to record request details on POST.'));
62         return true;
63     }
64
65     protected function doDebug()
66     {
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);
77     }
78
79     protected function saveDebug($data)
80     {
81         $output = var_export($data, true);
82         if ($this->dir) {
83             $file = $this->dir . DIRECTORY_SEPARATOR . $this->logFileName();
84             file_put_contents($file, $output);
85         } else {
86             common_log(LOG_DEBUG, "PostDebug: $output");
87         }
88     }
89
90     protected function logFileName()
91     {
92         $base = common_request_id();
93         $base = preg_replace('/^(.+?) .*$/', '$1', $base);
94         $base = str_replace(':', '-', $base);
95         $base = rawurlencode($base);
96         return $base;
97     }
98
99     protected function getHttpHeaders()
100     {
101         if (function_exists('getallheaders')) {
102             $headers = getallheaders();
103         } else {
104             $headers = array();
105             $prefix = 'HTTP_';
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;
111                 }
112             }
113         }
114         foreach ($headers as $header => $val) {
115             if (strtolower($header) == 'cookie') {
116                 $headers[$header] = $this->sanitizeCookies($val);
117             }
118         }
119         return $headers;
120     }
121
122     protected function normalizeHeader($key)
123     {
124         return implode('-',
125                        array_map('ucfirst',
126                                  explode("_",
127                                          strtolower($key))));
128     }
129
130     function sanitizeCookies($val)
131     {
132         $blacklist = array(session_name(), 'rememberme');
133         foreach ($blacklist as $name) {
134             $val = preg_replace("/(^|;\s*)({$name}=)(.*?)(;|$)/",
135                                 "$1$2########$4",
136                                 $val);
137         }
138         return $val;
139     }
140
141     function sanitizePostData($data)
142     {
143         $blacklist = array('password', 'confirm', 'token');
144         foreach ($data as $key => $val) {
145             if (in_array($key, $blacklist)) {
146                 $data[$key] = '########';
147             }
148         }
149         return $data;
150     }
151 }