937631e705619a7b909410e667514ae040ab365d
[www.jankratochvil.net.git] / project / Nokia61 / Nokia61.php
1 <?php
2         error_reporting(E_ALL);
3
4         if (!isset($HTTP_GET_VARS["nohtml"])) {
5         ?>
6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
7 <HTML>
8 <HEAD>
9 <TITLE>Nokia61</TITLE>
10 <LINK REV=MADE HREF="mailto:short@ucw.cz">
11 </HEAD><BODY>
12         <?php
13                 }
14
15 $tables_per_row=4;
16 $bgcolor_head="#D0D0D0";
17 $bgcolor_body="#F0F0F0";
18
19 function table($array,$rw=0)
20 {
21 global $tables,$bgcolor_head,$bgcolor_body,$HTTP_GET_VARS;
22
23         print "<FORM METHOD=GET ACTION=\""
24                         .(!isset($HTTP_GET_VARS["base"]) ? "./" : $HTTP_GET_VARS["base"])
25                         ."Nokia61.php\"><TABLE BORDER=1 RULES=NONE>\n";
26         print "<TR><TD COLSPAN=3 BGCOLOR=\"".$bgcolor_head."\">";
27         if ($rw)
28                 print "Input:";
29         else
30                 printf("Step #%d",$tables+1);
31         print "</TD></TR>\n";
32         for ($row=0;$row<3;$row++) {
33                 print "<TR>";
34                 for ($col=0;$col<3;$col++) {
35                         $idx=$row*3+$col;
36                         printf("<TD BGCOLOR=\"".$bgcolor_body."\"><INPUT NAME=\"%d\"",$idx);
37                         if (isset($array[$idx]))
38                                 printf(" VALUE=\"%d\"",$array[$idx]);
39                         if (!$rw)
40                                 print " READONLY";
41                         print " TYPE=STRING SIZE=2 MAXLENGTH=2></TD>";
42                         }
43                 print "</TR>\n";
44                 }
45         if ($rw)
46                 print "<TR><TD COLSPAN=3 ALIGN=CENTER><INPUT TYPE=SUBMIT VALUE=\"Solve!\"></TD></TR>\n";
47         print "</TABLE></FORM>\n";
48         $tables++;
49 }
50
51         $input=array();
52         $used=array();
53         $filled=0;
54         $tables=0;
55         
56         for ($idx=0;$idx<9;$idx++) {
57                 $s=$HTTP_GET_VARS[sprintf("%d",$idx)];
58                 if (!strlen($s))
59                         continue;
60                 $x=ord($s)-ord("0");
61                 if (strlen($s)!=1 || $x<1 || $x>9 || isset($used[$x])) {
62                         printf("<H1>WARNING: Digit at row %d, column %d was %s, erased!</H1>\n",1+$idx/3,1+$idx%3,
63                                         ($used[$x] ? "repeated" : "invalid"));
64                         continue;
65                         }
66                 $input[$idx]=$x;
67                 $used[$x]=1;
68                 $filled++;
69                 }
70         // Both popen() and proc_open() require: /bin/sh
71         if ($filled==9 && !is_executable("/bin/sh")) {
72                 print "<h1>Current installation environment forbids the execution!</h1>\n";
73                 $filled=0;
74                 }
75         if ($filled==9) {
76                 print "<TABLE BORDER=0><TR>";
77                 $cmd="echo -n -e '";
78                 for ($row=0;$row<3;$row++) {
79                         for ($col=0;$col<3;$col++) {
80                                 if ($col)
81                                         $cmd.=" ";
82                                 $cmd.=sprintf("%d",$input[$row*3+$col]);
83                                 }
84                         $cmd.="\\n";
85                         }
86                 $cmd.="' | ./Nokia61";
87                 $cmd=popen($cmd,"r");
88                 $idx=0;
89                 $output=array();
90                 while (!feof($cmd)) {
91                         if (3!=fscanf($cmd,"\n  %d%d%d",$output[$idx+0],$output[$idx+1],$output[$idx+2]))
92                                 continue;
93                         $idx+=3;
94                         if ($idx<9)
95                                 continue;
96                         print "<TD>";
97                         table($output);
98                         print "</TD><TD>&nbsp;</TD>";
99                         if (!($tables%$tables_per_row))
100                                 print "</TR><TR>";
101                         $idx=0;
102                         }
103                 pclose($cmd);
104                 print "</TR></TABLE>\n";
105                 print "<HR>\n";
106                 $input=array();
107                 }
108         table($input,1);
109
110         if (!isset($HTTP_GET_VARS["nohtml"])) {
111         ?>
112 </BODY></HTML>
113 <?php
114                 }
115 ?>