]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/scripts/public_html/common.php
added raw query form
[helm.git] / helm / software / matita / scripts / public_html / common.php
1 <?php
2
3 $i = 0;
4   
5 function array_to_combo($a) {
6   foreach($a as $k => $v){
7     echo "<option value=\"{$v}\">{$v}</option>";
8   }
9 }
10
11 function prettify($s,$name) {
12   if (preg_match("/^[0-9]{12}$/",$s)) {
13     $year = substr($s,0,4);
14     $month = substr($s,4,2);
15     $day = substr($s,6,2);
16     $hour = substr($s,8,2);
17     $minute = substr($s,10,2);
18     return $day . "/" . $month . "/" . $year . " " . $hour . ":" . $minute;
19   } else if (preg_match("/time/",$name)){
20     $min = floor($s / 6000);
21     $sec = floor(($s - $min * 6000) / 100);
22     $cents = $s % 100;
23     return $min . "m" . $sec . "." . $cents . "s";
24   } else
25     return rtrim($s);
26 }
27   
28
29 function printer($q){
30   global $i;
31   echo "<tr>";
32   if ( $i == 0) {
33       foreach( $q as $name => $txt) {
34           echo "<th>$name</th>";
35       }
36   }
37   echo "</tr>\n";
38   if ( $i%2 == 0)
39      echo "<tr class=\"even\">";      
40   else
41      echo "<tr class=\"odd\">";
42   foreach( $q as $name => $txt) {
43      echo "<td>" . prettify($txt,$name) . "</td>";
44   }
45   echo "</tr>\n";      
46   $i++;
47 }
48
49
50 function query($q,$f) {
51   $db = mysql_pconnect("localhost","helm");
52   mysql_select_db("matita");
53   if (!preg_match("/^(select|describe)[^\n;]*;?$/i",$q)) {
54     echo "Query not allowed!";
55     return;
56   }
57   $rc = mysql_query($q,$db);
58   if(!$rc) {
59     die("Query failed: " . mysql_error());
60   }
61   while( $row = mysql_fetch_array($rc, MYSQL_ASSOC)){
62     $f($row);
63   }
64   mysql_free_result($rc);
65   mysql_close($db);
66 }
67
68 function time_2_cents($t) {
69   $matches = array();
70   $rex = "/^(\d+)m(\d\d?)\.(\d{2})s$/";
71   $m = preg_match($rex,$t,$matches);
72   if ( $m == 0 ) exit(1);
73   $t_minutes = $matches[1];
74   $t_secs = $matches[2];
75   $t_cents = $matches[3];
76   return ((int) $t_cents) + ((int) $t_secs) * 100 + ((int)$t_minutes) * 6000 ;
77 }
78
79 ?>