]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SQLProfile/SQLProfilePlugin.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / SQLProfile / SQLProfilePlugin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 /**
25  * Check DB queries for filesorts and such and log em.
26  *
27  * @package SQLProfilePlugin
28  * @maintainer Brion Vibber <brion@status.net>
29  */
30 class SQLProfilePlugin extends Plugin
31 {
32     private $recursionGuard = false;
33
34     function onPluginVersion(array &$versions)
35     {
36         $versions[] = array('name' => 'SQLProfile',
37                             'version' => GNUSOCIAL_VERSION,
38                             'author' => 'Brion Vibber',
39                             'homepage' => 'http://status.net/wiki/Plugin:SQLProfile',
40                             'rawdescription' =>
41                             // TRANS: Plugin description.
42                             _m('Debug tool to watch for poorly indexed DB queries.'));
43
44         return true;
45     }
46
47     function onStartDBQuery($obj, $query, &$result)
48     {
49         if (!$this->recursionGuard && preg_match('/\bselect\b/i', $query)) {
50             $this->recursionGuard = true;
51             $xobj = clone($obj);
52             $explain = $xobj->query('EXPLAIN ' . $query);
53             $this->recursionGuard = false;
54
55             while ($xobj->fetch()) {
56                 $extra = $xobj->Extra;
57                 $evil = (strpos($extra, 'Using filesort') !== false) ||
58                         (strpos($extra, 'Using temporary') !== false);
59                 if ($evil) {
60                     $xquery = $xobj->sanitizeQuery($query);
61                     common_log(LOG_DEBUG, "$extra | $xquery");
62                 }
63             }
64         }
65         return true;
66     }
67 }