OJD Commit: Fix node id reset
[eidogo-ojd.git] / php / search.php
blobbe4618eb75ca3501ce67eccfbf5c2172ad52614a
1 <?php
3 require("json.php");
5 $kombilo_dir = "../kombilo";
7 // safety checks
8 $q = preg_replace("/[^nsew]/", "", $_GET['q']);
9 $w = (int)$_GET['w'];
10 $h = (int)$_GET['h'];
11 $p = preg_replace("/[^\.OX]/", "", strtoupper($_GET['p']));
12 $a = preg_replace("/[^a-z]/", "", $_GET['a']);
14 $output = null;
16 $cache_fn = "$kombilo_dir/cache/$q-$w-$h-$p-$a";
17 if (file_exists($cache_fn)) {
18 $output = array();
19 $fp = @fopen($cache_fn, "r");
20 while (!feof($fp)) {
21 $output[] = fgets($fp);
23 fclose($fp);
24 if ($output[0] == "") {
25 $output = null;
27 } else {
28 /*exec("python $kombilo_dir/search.py " .
29 escapeshellcmd($q) . " " .
30 escapeshellcmd($w) . " " .
31 escapeshellcmd($h) . " " .
32 escapeshellcmd($p) . " " .
33 escapeshellcmd($a),
34 $output, $retval);
35 if ($retval) {
36 echo "ERROR $retval";
37 exit;
40 $fp = fsockopen("127.0.0.1", 6060, $errno, $errstr, 10);
41 if (!$fp) {
42 echo "$errstr ($errno)<br />\n";
43 exit;
45 fwrite($fp, "$q $w $h $p $a\n");
46 $output = "";
47 while (!feof($fp)) {
48 $output .= fgets($fp, 2048);
50 fclose($fp);
52 $output = split("\n", $output);
53 // print_r($output);
55 file_put_contents($cache_fn, join("\n", $output));
58 if (!count($output)) {
59 echo "NONE";
60 exit;
63 if (count($output) > 50) {
64 $output = array_slice($output, 0, 50);
67 $odd = true;
69 $results = array();
71 foreach ($output as $line) {
72 list($fn, $pw, $wr, $pb, $br, $re, $dt, $mv) = split("\t", $line);
73 if (!$fn) continue;
74 $id = str_replace(".sgf", "", $fn);
75 $mv = split(",", $mv);
76 $mv = (int)$mv[count($mv)-2];
77 array_push($results, array(
78 "id" => $id,
79 "pw" => $pw,
80 "wr" => $wr,
81 "pb" => $pb,
82 "br" => $br,
83 "re" => $re,
84 "dt" => $dt,
85 "mv" => $mv,
86 ));
87 $odd = $odd ? false : true;
90 $json = new Services_JSON();
91 echo $json->encode($results);