OSDN Git Service

tag: sqlite0904
[nucleus-jp/nucleus-plugins.git] / sqlite / tags / sqlite0904 / nucleus / plugins / sqlite / index.php
1 <?php
2     /*****************************
3     * SQLite database tool       *
4     *                 ver 0.8.0  *
5     * Written by Katsumi         *
6     *****************************/
7     
8 // This library is GPL.
9
10         include('../../../config.php');
11         
12         include($DIR_LIBS . 'PLUGINADMIN.php');
13
14         // create the admin area page
15         $pa = new PluginAdmin('SQLite');
16         $pa->start();
17         $p=&$pa->plugin;
18         $pluginUrl=$p->getAdminURL();
19
20
21         // check if superadmin is logged in
22         if (!($member->isLoggedIn() && $member->isAdmin()))
23         {
24                 echo '<p>' . _ERROR_DISALLOWED . '</p>';
25                 $pa->end();
26                 exit;
27         }
28
29         // Check ticket
30         if (requestVar('SQLiteCommand') && (!$manager->checkTicket())){
31                 echo '<p>' . _ERROR_BADTICKET . '</p>';
32                 $pa->end();
33                 exit;
34         }
35         $ticket=$manager->addTicketToUrl('');
36         $ticket=substr($ticket,strpos($ticket,'ticket=')+7);
37
38 ?><script type="text/javascript">
39 //<![CDATA[
40 function $(id) {
41   return document.getElementById(id);
42 }
43 //]]>
44 </script><?php
45
46         $idnum=0;
47         echo '<h2>'.$p->translated('SQLite management')."</h2>\n";
48         
49         $infostr='';
50         switch(requestVar('SQLiteCommand')){
51         case 'VACUUM':
52                 nucleus_mysql_query('VACUUM');
53                 $infostr='VACUUM was done.';
54                 break;
55         case 'integrity_check':
56                 $res=nucleus_mysql_query('PRAGMA integrity_check');
57                 $infostr='Integrity check result:'."<br />\n";
58                 while ($a=nucleus_mysql_fetch_array($res)) $infostr.=$a[0]."<br />\n";
59                 break;
60         case 'plugin_check':
61                 $pluginfile=requestVar('plugin');
62                 $query='SELECT COUNT(*) as result FROM `'.sql_table('plugin').'` WHERE pfile="'.addslashes($pluginfile).'"';
63                 if (!quickQuery($query)) {
64                         $infostr="No such plugin!";
65                         break;
66                 }
67                 if ($p->modify_plugin($pluginfile)) {
68                         $infostr=$p->translated('The plugin, ').$pluginfile.$p->translated(' was checked and modified (if modification required)');
69                         break;
70                 }
71
72                 // Modification failed.  Show the lines that must be modified
73                 $phpfiles=array();
74                 array_push($phpfiles,$DIR_PLUGINS.$pluginfile.'.php');
75                 $admindir=$DIR_PLUGINS.strtolower(substr($pluginfile,3));
76                 $p->seekPhpFiles($admindir,$phpfiles);
77                 $infostr=$p->translated('Please modify the PHP files').": <br />\n";
78                 foreach ($phpfiles as $file) $infostr.=$p->show_Lines($file);
79                 break;
80         case "QUERY":
81                 $infostr='<table><tr><th>'.'Query'.
82 '&nbsp;&nbsp;&nbsp;&nbsp;(-&gt;<a href="javascript:Copy this query" onclick="
83 var t=$(\'QueryShown\').innerHTML+\'\';
84 $(\'ExecQuery\').value=t.replace(/^\s+|\s+$/g,\'\');
85 return false;
86 ">Copy</a>)'.
87                 "</th></td><tr><td id=\"QueryShown\">\n".htmlspecialchars($query=requestVar('query'))."</td></tr></table><br />\n";
88                 if (requestVar('confirm')!='confirmed'){
89                         $infostr=$p->translated('Please check the "I am sure." checkbox to execute query').$infostr;
90                         break;
91                 }
92                 ob_start();
93                 $res=nucleus_mysql_query($query);
94                 $errorstr=ob_get_contents();
95                 ob_end_clean();
96                 if (!$res) {
97                         $infostr.=nucleus_mysql_error()."<br />\n";
98                         if (preg_match('/sqlite_query\(\):([^<]*)in <b>/i',$errorstr,$matches)) $infostr.=$matches[1];
99                         break;
100                 }
101                 if (preg_match('/ OFFSET ([0-9]+)$/i',$query,$matches)) $offset=$matches[1];
102                 else $offset=0;
103                 
104                 // Get resut into an array
105                 $resulttable=array();
106                 $columnname=array();
107                 $columnnum=0;
108                 while ($a=nucleus_mysql_fetch_array($res,SQLITE_ASSOC)) {
109                         if ($columnnum<count($a)) {
110                                 $i=0;
111                                 foreach ($a as $key=>$value) $columnname[$i++]=$key;
112                                 $columnnum=count($a);
113                         }
114                         $templine=array();
115                         foreach ($a as $key=>$value) $templine[$key]=$value;
116                         array_push($resulttable,$templine);
117                 }
118                 
119                 // Create table HTML from the array
120                 $infostr.="<table><tr>";
121                 for ($i=0;$i<$columnnum;$i++) $infostr.="<th>".$columnname[$i]."</th>";
122                 $infostr.="</tr>\n";
123                 foreach ($resulttable as $templine) {
124                         $infostr.="<tr>";
125                         for ($i=0;$i<$columnnum;$i++) {
126                                 $value=(string)$templine[$columnname[$i]];
127                                 if (50<strlen($value)) {
128                                         $value='<span id="sqliteobj'.$idnum.'"><a href="" title="'.'Click this to show all'.'" onclick="'.
129                                                 '$(\'sqliteobj'.$idnum.'\').innerHTML='.
130                                                 '$(\'sqliteobj'.$idnum.'-2\').innerHTML;'.
131                                                 'return false;">'.htmlspecialchars(substr($value,0,50)).".....</a></span>\n".
132                                                 '<span style="DISPLAY:none;" id="sqliteobj'.$idnum.'-2">'.htmlspecialchars($value)."</span>\n";
133                                         $idnum++;
134                                 } else $value=htmlspecialchars($value);
135                                 switch(requestVar('option')){
136                                 case 'showalltables':
137                                         if ($columnname[$i]!='name') break;
138                                         $query="SELECT * FROM '$value' LIMIT 10";
139                                         $value='<a href="'.htmlspecialchars($pluginUrl).
140                                                 '?SQLiteCommand=QUERY&confirm=confirmed&option=showtable&ticket='.$ticket.
141                                                 '&query='.htmlspecialchars($query).'">'.
142                                                 $value.'</a>';
143                                         break;
144                                 default:
145                                 }
146                                 $value=preg_replace('/\\n$/','',$value);
147                                 $value=str_replace("\n","<br />\n",$value);
148                                 $infostr.="<td>".$value."</td>";
149                         }
150                         $infostr.="</tr>\n";
151                         $offset++;
152                 }
153                 $infostr.="</table>\n";
154                 switch(requestVar('option')){
155                 case 'showtable':
156                         $query=requestVar('query');
157                         $offset=(int)$offset;
158                         $query=preg_replace('/ OFFSET ([0-9]+)$/i','',$query)." OFFSET $offset";
159                         $res=nucleus_mysql_query($query);
160                         if (!nucleus_mysql_fetch_array($res)) break;
161                         $infostr.='
162 <form method="POST" action="'.htmlspecialchars($pluginUrl).'">
163 <input type="hidden" name="SQLiteCommand" value="QUERY">
164 <input type="hidden" name="confirm" value="confirmed">
165 <input type="hidden" name="ticket" value="'.$ticket.'">
166 <input type="hidden" name="query" value="'.htmlspecialchars($query).'">
167 <input type="hidden" name="option" value="showtable">
168 <input type="submit" value="More">
169 </form>';
170                         break;
171                 default:
172                 }
173                 break;
174         default:
175         }
176         
177         echo $p->translated('PHP version: ').phpversion()."<br />\n";
178         if ($res = nucleus_mysql_query('SELECT sqlite_version();')) $ret = nucleus_mysql_fetch_array($res);
179         if (!$ret) $SQLiteVersion='?.?.?';
180         else if (!($SQLiteVersion=$ret[0])) $SQLiteVersion='?.?.?';
181         echo $p->translated('SQLite DB version: ').$SQLiteVersion."<br />\n";;
182         echo $p->translated('SQLite wrapper version: ').$SQLITECONF['VERSION']."<br />\n";
183         echo $p->translated('SQLite DB file size: ').filesize($SQLITECONF['DBFILENAME'])." bytes<br />\n";
184         echo "<hr />\n";
185
186 ?><table><tr><th><?php echo $p->translated('Tools'); ?></th><th><?php echo $p->translated('Execute SQL Query'); ?></th></tr>
187 <tr><td>
188 <form method="POST" action="<?php echo htmlspecialchars($pluginUrl);?>">
189 <input type="hidden" name="SQLiteCommand" value="VACUUM">
190 <input type="hidden" name="ticket" value="<?php echo $ticket; ?>">
191 <input type="submit" value="<?php echo $p->translated('VACUUM'); ?>">
192 </form>
193 <form method="POST" action="<?php echo htmlspecialchars($pluginUrl);?>">
194 <input type="hidden" name="SQLiteCommand" value="integrity_check">
195 <input type="hidden" name="ticket" value="<?php echo $ticket; ?>">
196 <input type="submit" value="<?php echo $p->translated('Integrity Check'); ?>">
197 </form>
198 <form method="POST" action="<?php echo htmlspecialchars($pluginUrl);?>">
199 <select name="plugin">
200 <?php
201         $res=nucleus_mysql_query('SELECT pfile FROM `'.sql_table('plugin').'`');
202         while($result=nucleus_mysql_fetch_row($res)) {
203                 if (requestVar('plugin')==$result[0]) echo '<option selected value="'.$result[0].'">'.$result[0]."</option>\n";
204                 else echo '<option value="'.$result[0].'">'.$result[0]."</option>\n";
205         }
206 ?>
207 </select>
208 <input type="hidden" name="SQLiteCommand" value="plugin_check">
209 <input type="hidden" name="ticket" value="<?php echo $ticket; ?>">
210 <input type="submit" value="<?php echo $p->translated('Check plugin'); ?>">
211 </form>
212 <?php
213         if ('yes'==$p->getOption('allowsql')) {
214 ?><form method="POST" action="<?php echo htmlspecialchars($pluginUrl);?>">
215 <input type="hidden" name="SQLiteCommand" value="QUERY">
216 <input type="hidden" name="ticket" value="<?php echo $ticket; ?>">
217 <input type="hidden" name="confirm" value="confirmed">
218 <input type="hidden" name="query" value="SELECT name, sqlite_table_structure(name) as table_structure FROM sqlite_master WHERE type='table'">
219 <input type="hidden" name="option" value="showalltables">
220 <input type="submit" value="<?php echo $p->translated('Show all tables'); ?>">
221 </form><?php
222         }
223 ?>
224 </td><td><?php
225         if ('yes'!=$p->getOption('allowsql')) echo $p->translated('Query not allowed. To use it, change the plugin option.');
226         else {
227 ?><form method="POST" action="<?php echo htmlspecialchars($pluginUrl);?>">
228 <input type="hidden" name="SQLiteCommand" value="QUERY">
229 <input type="hidden" name="ticket" value="<?php echo $ticket; ?>">
230 <textarea name="query" id="ExecQuery" cols="50" rows="10"></textarea><br />
231 <input type="submit" value="<?php echo $p->translated('Execute'); ?>">
232 <input type="checkbox" name="confirm" value="confirmed"><?php echo $p->translated('I am sure.'); ?>
233 </form>
234 <?php   }
235 ?></td>
236 </table>
237 <br /><?php
238
239         if ($infostr) echo "<hr />\n".$infostr;
240         $pa->end();
241 ?>