Commit JeLuF's register_globals fixes, first phase
[lhc/web/wiklou.git] / includes / SpecialAsksql.php
1 <?
2
3 function wfSpecialAsksql()
4 {
5 global $wgUser, $wgOut, $action;
6
7 if ( ! $wgUser->isSysop() ) {
8 $wgOut->sysopRequired();
9 return;
10 }
11 $fields = array( "wpSqlQuery" );
12 wfCleanFormFields( $fields );
13 $f = new SqlQueryForm();
14
15 if ( "submit" == $action ) { $f->doSubmit(); }
16 else { $f->showForm( "" ); }
17 }
18
19 class SqlQueryForm {
20
21 function showForm( $err )
22 {
23 global $wgOut, $wgUser, $wgLang;
24 global $wpSqlQuery;
25 global $wgLogQueries;
26 $wpSqlQuery = $_REQUEST["wpSqlQuery"];
27
28 $wgOut->setPagetitle( wfMsg( "asksql" ) );
29 $note = wfMsg( "asksqltext" );
30 if($wgLogQueries)
31 $note .= " " . wfMsg( "sqlislogged" );
32 $wgOut->addWikiText( $note );
33
34 if ( "" != $err ) {
35 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
36 }
37 if ( ! $wpSqlQuery ) { $wpSqlQuery = "SELECT ... FROM ... WHERE ..."; }
38 $q = wfMsg( "sqlquery" );
39 $qb = wfMsg( "querybtn" );
40 $action = wfLocalUrlE( $wgLang->specialPage( "Asksql" ),
41 "action=submit" );
42
43 $wgOut->addHTML( "<p>
44 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
45 <table border=0><tr>
46 <td align=right>{$q}:</td>
47 <td align=left>
48 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
49 . htmlspecialchars($wpSqlQuery) ."
50 </textarea>
51 </td>
52 </tr><tr>
53 <td>&nbsp;</td><td align=\"left\">
54 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
55 </td></tr></table>
56 </form>\n" );
57
58 }
59
60 function doSubmit()
61 {
62 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
63 global $wpSqlQuery;
64 global $wgDBsqluser, $wgDBsqlpassword;
65 $wpSqlQuery = $_REQUEST["wpSqlQuery"];
66
67 # Use a limit, folks!
68 $wpSqlQuery = trim( $wpSqlQuery );
69 if( preg_match( "/^SELECT/i", $wpSqlQuery )
70 and !preg_match( "/LIMIT/i", $wpSqlQuery ) ) {
71 $wpSqlQuery .= " LIMIT 100";
72 }
73 if ( ! $wgUser->isDeveloper() ) {
74 $connection = wfGetDB( $wgDBsqluser, $wgDBsqlpassword );
75 }
76 $this->logQuery( $wpSqlQuery );
77 $res = wfQuery( $wpSqlQuery, DB_WRITE, "SpecialAsksql::doSubmit" );
78 $this->logFinishedQuery();
79
80 $n = 0;
81 @$n = wfNumFields( $res );
82 $titleList = false;
83
84 if ( $n ) {
85 $k = array();
86 for ( $x = 0; $x < $n; ++$x ) {
87 array_push( $k, wfFieldName( $res, $x ) );
88 }
89
90 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
91 $titleList = true;
92 }
93
94 $a = array();
95 while ( $s = wfFetchObject( $res ) ) {
96 array_push( $a, $s );
97 }
98 wfFreeResult( $res );
99
100 if ( $titleList ) {
101 $r = "";
102 foreach ( $a as $y ) {
103 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
104 htmlspecialchars( $y->$x ) . "</a>" ;
105 $sTitle = htmlspecialchars( $y->cur_title );
106 if ( $y->cur_namespace ) {
107 $sNamespace = $wgLang->getNsText( $y->cur_namespace );
108 $link = "$sNamespace:$sTitle";
109 } else {
110 $link = "$sTitle";
111 }
112 $skin = $wgUser->getSkin();
113 $link = $skin->makeLink( $link );
114 $r .= "* [[$link]]<br>\n";
115 }
116 } else {
117
118 $r = "<table border=1 bordercolor=black cellspacing=0 " .
119 "cellpadding=2><tr>\n";
120 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
121 $r .= "</tr>\n";
122
123 foreach ( $a as $y ) {
124 $r .= "<tr>";
125 foreach ( $k as $x ) {
126 $o = $y->$x ;
127 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
128 $namespace = 0;
129 if( $x == "cur_title" ) $namespace = $y->cur_namespace;
130 if( $x == "old_title" ) $namespace = $y->old_namespace;
131 if( $x == "rc_title" ) $namespace = $y->rc_namespace;
132 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
133 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
134 htmlspecialchars( $y->$x ) . "</a>" ;
135 } else {
136 $o = htmlspecialchars( $o );
137 }
138 $r .= "<td>" . $o . "</td>\n";
139 }
140 $r .= "</tr>\n";
141 }
142 $r .= "</table>\n";
143 }
144 }
145 $this->showForm( wfMsg( "querysuccessful" ) );
146 $wgOut->addHTML( "<hr>{$r}\n" );
147 }
148
149 function logQuery( $q ) {
150 global $wgSqlLogFile, $wgLogQueries, $wgUser;
151 if(!$wgLogQueries) return;
152
153 $f = fopen( $wgSqlLogFile, "a" );
154 fputs( $f, "\n\n" . wfTimestampNow() .
155 " query by " . $wgUser->getName() .
156 ":\n$q\n" );
157 fclose( $f );
158 $this->starttime = microtime();
159 }
160
161 function logFinishedQuery() {
162 global $wgSqlLogFile, $wgLogQueries;
163 if(!$wgLogQueries) return;
164
165 list($sec, $usec) = explode( " ", microtime() );
166 list($sec1, $usec1) = explode( " ", $this->starttime );
167 $interval = ($sec + $usec) - ($sec1 + $usec1);
168
169 $f = fopen( $wgSqlLogFile, "a" );
170 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );
171 fclose( $f );
172 }
173
174 }
175
176 ?>