]> matita.cs.unibo.it Git - helm.git/blob - matita/scripts/public_html/common.php
tagged 0.5.0-rc1
[helm.git] / 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   $q = ltrim(rtrim(preg_replace("/\n/"," ",$q)));
54   if (!preg_match("/^(select|describe)[^\n;]*;?$/i",$q)) {
55     die("Query not allowed!<pre>" . $q . "</pre>");
56     return;
57   }
58   $rc = mysql_query($q,$db);
59   if(!$rc) {
60     die("Query failed: " . mysql_error());
61   }
62   while( $row = mysql_fetch_array($rc, MYSQL_ASSOC)){
63     $f($row);
64   }
65   mysql_free_result($rc);
66   mysql_close($db);
67 }
68
69 function time_2_cents($t) {
70   $matches = array();
71   $rex = "/^(\d+)m(\d\d?)\.(\d{2})s$/";
72   $m = preg_match($rex,$t,$matches);
73   if ( $m == 0 ) exit(1);
74   $t_minutes = $matches[1];
75   $t_secs = $matches[2];
76   $t_cents = $matches[3];
77   return ((int) $t_cents) + ((int) $t_secs) * 100 + ((int)$t_minutes) * 6000 ;
78 }
79
80 ?>