]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/PostDebug/PostDebugPlugin.php
Merge branch 'master' into testing
[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(&$versions)
52     {
53         $versions[] = array('name' => 'PostDebug',
54                             'version' => STATUSNET_VERSION,
55                             'author' => 'Brion Vibber',
56                             'homepage' => 'http://status.net/wiki/Plugin:PostDebug',
57                             'rawdescription' =>
58                             _m('Debugging tool to record request details on POST.'));
59         return true;
60     }
61
62     protected function doDebug()
63     {
64         $data = array('timestamp' => gmdate('r'),
65                       'remote_addr' => @$_SERVER['REMOTE_ADDR'],
66                       'url' => @$_SERVER['REQUEST_URI'],
67                       'have_session' => common_have_session(),
68                       'logged_in' => common_logged_in(),
69                       'is_real_login' => common_is_real_login(),
70                       'user' => common_logged_in() ? common_current_user()->nickname : null,
71                       'headers' => $this->getHttpHeaders(),
72                       'post_data' => $this->sanitizePostData($_POST));
73         $this->saveDebug($data);
74     }
75
76     protected function saveDebug($data)
77     {
78         $output = var_export($data, true);
79         if ($this->dir) {
80             $file = $this->dir . DIRECTORY_SEPARATOR . $this->logFileName();
81             file_put_contents($file, $output);
82         } else {
83             common_log(LOG_DEBUG, "PostDebug: $output");
84         }
85     }
86
87     protected function logFileName()
88     {
89         $base = common_request_id();
90         $base = preg_replace('/^(.+?) .*$/', '$1', $base);
91         $base = str_replace(':', '-', $base);
92         $base = rawurlencode($base);
93         return $base;
94     }
95
96     protected function getHttpHeaders()
97     {
98         if (function_exists('getallheaders')) {
99             $headers = getallheaders();
100         } else {
101             $headers = array();
102             $prefix = 'HTTP_';
103             $prefixLen = strlen($prefix);
104             foreach ($_SERVER as $key => $val) {
105                 if (substr($key, 0, $prefixLen) == $prefix) {
106                     $header = $this->normalizeHeader(substr($key, $prefixLen));
107                     $headers[$header] = $val;
108                 }
109             }
110         }
111         foreach ($headers as $header => $val) {
112             if (strtolower($header) == 'cookie') {
113                 $headers[$header] = $this->sanitizeCookies($val);
114             }
115         }
116         return $headers;
117     }
118
119     protected function normalizeHeader($key)
120     {
121         return implode('-',
122                        array_map('ucfirst',
123                                  explode("_",
124                                          strtolower($key))));
125     }
126
127     function sanitizeCookies($val)
128     {
129         $blacklist = array(session_name(), 'rememberme');
130         foreach ($blacklist as $name) {
131             $val = preg_replace("/(^|;\s*)({$name}=)(.*?)(;|$)/",
132                                 "$1$2########$4",
133                                 $val);
134         }
135         return $val;
136     }
137
138     function sanitizePostData($data)
139     {
140         $blacklist = array('password', 'confirm', 'token');
141         foreach ($data as $key => $val) {
142             if (in_array($key, $blacklist)) {
143                 $data[$key] = '########';
144             }
145         }
146         return $data;
147     }
148
149 }
150