87ebc5a8a726872dac9027f9da1181c561de14df
[atutor.git] / mods / wiki / plugins / mpi / off / mpi_sqlquery.php
1 <?php
2
3 /*
4    Use like: <?plugin SqlQuery  SELECT * FROM table2;  ?>
5    What will yield a table dump of the selected result sets.
6
7    You must first enable this plugin - create a symlink into the
8    parent dir (it isn't found here in the off/ subdir).
9 */
10
11 $ewiki_plugins["mpi"]["sqlquery"] = "ewiki_mpi_sqlquery";
12
13
14 function ewiki_mpi_sqlquery($action, &$args, &$iii, &$s) {
15
16    #-- select PHP db funcs
17    if (function_exists("anydb_query")) {
18       $SQL_QUERY = "anydb_query";
19       $SQL_FETCH = "anydb_fetch_array";
20    }
21    else {
22       $SQL_QUERY = "mysql_query";
23       $SQL_FETCH = "mysql_fetch_array";
24    }
25
26    #-- query
27    if ($query = $args["_"]) {
28
29       #-- security check
30       if (!preg_match('/^\s*(SELECT|SHOW)\s+/i', $query, $uu)) {
31          return("SQL query rejected");
32       }
33
34       $result = $SQL_QUERY($query);
35       if (!$result) {
36          return("failed SQL query");
37       }
38
39       #-- fetch data
40       $r = array();
41       while($row = $SQL_FETCH($result)) {
42          foreach ($row as $i=>$d) {
43             if (is_int($i)) {
44                unset($row[$i]);
45             }
46          }
47          $r[] = $row;
48       }
49
50       #-- output
51       $o .= '<table border="1" cellpadding="2" cellspacing="0" class="sql-query">';
52       $o .= '<tr><th>' . implode('</th><th>',array_keys($r[0])) . '</th></tr>'."\n";
53       $alt = 0;
54       foreach ($r as $row) {
55          $alt = ($alt++) % 2;
56          $add = $alt ? ' class="alternate"' : '';
57          $o .= "<tr><td$add>" . implode("</td><td$add>",$row) . '</td></tr>'."\n";
58       }
59       $o .= "</table>\n";
60
61       return($o);
62    }
63
64    return(mysql_error());
65 }
66
67 ?>