Rev 193 | Rev 267 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 193 | Rev 266 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | ini_set('display_errors', 1); |
3 | ini_set('display_errors', 1); |
4 | ini_set('display_startup_errors', 1); |
4 | ini_set('display_startup_errors', 1); |
5 | error_reporting(E_ALL); |
5 | error_reporting(E_ALL); |
6 | 6 | ||
7 | require 'config.php'; |
7 | require 'config.php'; |
8 | require 'ip2coordinates.php'; |
8 | require 'ip2coordinates.php'; |
9 | 9 | ||
10 | header('Content-Type: text/event-stream'); |
10 | header('Content-Type: text/event-stream'); |
11 | // recommended to prevent caching of event data. |
11 | // recommended to prevent caching of event data. |
12 | header('Cache-Control: no-cache'); |
12 | header('Cache-Control: no-cache'); |
13 | 13 | ||
14 | 14 | ||
15 | function xmlWalker($xml_array, $parent) { |
15 | function xmlWalker($xml_array, $parent) { |
16 | $cmd=""; |
16 | $cmd=""; |
17 | foreach($xml_array as $tag => $value) { |
17 | foreach($xml_array as $tag => $value) { |
18 | if ((int)$tag === $tag) { |
18 | if ((int)$tag === $tag) { |
19 | $tag = mb_substr($parent, 0, -1); |
19 | $tag = mb_substr($parent, 0, -1); |
20 | } |
20 | } |
21 | $cmd = $cmd . "<" .$tag. ">"; |
21 | $cmd = $cmd . "<" .$tag. ">"; |
22 | if (is_array($value)) { |
22 | if (is_array($value)) { |
23 | $cmd = $cmd . xmlWalker($value, $tag); |
23 | $cmd = $cmd . xmlWalker($value, $tag); |
24 | } else { |
24 | } else { |
25 | $cmd = $cmd . $value; |
25 | $cmd = $cmd . $value; |
26 | } |
26 | } |
27 | $cmd = $cmd . "</" .$tag. ">"; |
27 | $cmd = $cmd . "</" .$tag. ">"; |
28 | } |
28 | } |
29 | return $cmd; |
29 | return $cmd; |
30 | } |
30 | } |
31 | 31 | ||
32 | 32 | ||
33 | 33 | ||
34 | 34 | ||
35 | function parseDOMNode(DOMNode $domNode) { |
35 | function parseDOMNode(DOMNode $domNode) { |
36 | $str=""; |
36 | $str=""; |
37 | $cnt=0; |
37 | $cnt=0; |
38 | foreach ($domNode->childNodes as $node) |
38 | foreach ($domNode->childNodes as $node) |
39 | { |
39 | { |
40 | if ($cnt){ |
40 | if ($cnt){ |
41 | $str .= ","; |
41 | $str .= ","; |
42 | } |
42 | } |
43 | if($node->hasChildNodes()) { |
43 | if($node->hasChildNodes()) { |
44 | if (!$node->childNodes->item(0)->hasChildNodes() || |
44 | if (!$node->childNodes->item(0)->hasChildNodes() || |
45 | strpos($node->nodeName,"list")!==false || |
45 | strpos($node->nodeName,"list")!==false || |
46 | $node->nodeName === "histogram" ) { |
46 | $node->nodeName === "histogram" ) { |
47 | $str .= parseDOMNode($node); |
47 | $str .= parseDOMNode($node); |
48 | } else { |
48 | } else { |
49 | $str .= $node->nodeName.'(' ; |
49 | $str .= $node->nodeName.'(' ; |
50 | $str .= parseDOMNode($node); |
50 | $str .= parseDOMNode($node); |
51 | $str .= ') ' ; |
51 | $str .= ') ' ; |
52 | } |
52 | } |
53 | 53 | ||
54 | } else { |
54 | } else { |
55 | 55 | ||
56 | if (is_numeric($node->nodeValue) || $node->parentNode->nodeName === "pid") { |
56 | if (is_numeric($node->nodeValue) || $node->parentNode->nodeName === "pid") { |
57 | $str .= $node->nodeValue ; |
57 | $str .= $node->nodeValue ; |
58 | } else { |
58 | } else { |
59 | if (strlen($node->nodeValue)) { |
59 | if (strlen($node->nodeValue)) { |
60 | $str .= '"' . $node->nodeValue . '"' ; |
60 | $str .= '"' . $node->nodeValue . '"' ; |
61 | } else { |
61 | } else { |
62 | $str .= "-1"; |
62 | $str .= "-1"; |
63 | } |
63 | } |
64 | } |
64 | } |
65 | } |
65 | } |
66 | $cnt++; |
66 | $cnt++; |
67 | } |
67 | } |
68 | return $str; |
68 | return $str; |
69 | } |
69 | } |
70 | 70 | ||
71 | 71 | ||
72 | 72 | ||
73 | 73 | ||
74 | 74 | ||
75 | 75 | ||
76 | function stat2db($info){ |
76 | function stat2db($info){ |
77 | global $code; |
77 | global $code; |
78 | // Establish a MySQL connection and select our database using values contained in config.php. |
78 | // Establish a MySQL connection and select our database using values contained in config.php. |
79 | 79 | ||
80 | if (PHP_SAPI === 'cli') { |
80 | if (PHP_SAPI === 'cli') { |
81 | $ip = "171.173.43.71"; |
81 | $ip = "171.173.43.71"; |
82 | } else { |
82 | } else { |
83 | $ip = $_SERVER['REMOTE_ADDR']; |
83 | $ip = $_SERVER['REMOTE_ADDR']; |
84 | } |
84 | } |
85 | 85 | ||
86 | $userinfo = IPtoCoordinates($ip); |
86 | $userinfo = IPtoCoordinates($ip); |
87 | 87 | ||
88 | 88 | ||
89 | mysql_connect(DB_HOST, DB_USER, DB_PASS); |
89 | mysql_connect(DB_HOST, DB_USER, DB_PASS); |
90 | mysql_select_db(DB_NAME); |
90 | mysql_select_db(DB_NAME); |
91 | 91 | ||
92 | 92 | ||
93 | $msql = 'INSERT INTO `visitor_map` (`ip`, `location`, `longitude`, `latitude`,`time`, `neve`, `realtime`, `cputime`,`code` ) VALUES (' |
93 | $msql = 'INSERT INTO `visitor_map` (`ip`, `location`, `longitude`, `latitude`,`time`, `neve`, `realtime`, `cputime`,`code` ) VALUES (' |
94 | . '\'' . mysql_real_escape_string($ip) . '\',' |
94 | . '\'' . mysql_real_escape_string($ip) . '\',' |
95 | . '\'' . $userinfo['location'] . '\', ' |
95 | . '\'' . $userinfo['location'] . '\', ' |
96 | . $userinfo['longitude'] . ', ' |
96 | . $userinfo['longitude'] . ', ' |
97 | . $userinfo['latitude'] . ' , ' |
97 | . $userinfo['latitude'] . ' , ' |
98 | . $info |
98 | . $info |
99 | . ', \'' . mysql_real_escape_string($code) . '\'' . |
99 | . ', \'' . mysql_real_escape_string($code) . '\'' . |
100 | 100 | ||
101 | ')'; |
101 | ')'; |
102 | 102 | ||
103 | send_message(0,$msql,100); |
103 | send_message(0,$msql,100); |
104 | 104 | ||
105 | 105 | ||
106 | // Assign the user's IP to a variable and plot it into our function, whose return value is assigned to $userinfo |
106 | // Assign the user's IP to a variable and plot it into our function, whose return value is assigned to $userinfo |
107 | // Remember, the user's IP is not to be trusted 100% |
107 | // Remember, the user's IP is not to be trusted 100% |
108 | 108 | ||
109 | // We select the location column from the database |
109 | // We select the location column from the database |
110 | $user = mysql_query('SELECT `location` FROM `visitor_map` WHERE `location` = \'' . $userinfo['location'] . '\''); |
110 | $user = mysql_query('SELECT `location` FROM `visitor_map` WHERE `location` = \'' . $userinfo['location'] . '\''); |
111 | // If it does NOT return a value, and the user's IP could indeed be matched... |
111 | // If it does NOT return a value, and the user's IP could indeed be matched... |
112 | // (This makes sure that we have no duplicate rows (which would be a waste) and can determine the visitor's location, respectively) |
112 | // (This makes sure that we have no duplicate rows (which would be a waste) and can determine the visitor's location, respectively) |
113 | // ...We insert the values returned by our function into the database, escaping any possible dangerous input |
113 | // ...We insert the values returned by our function into the database, escaping any possible dangerous input |
114 | if(!mysql_fetch_row($user) && $userinfo) |
114 | if(!mysql_fetch_row($user) && $userinfo) |
- | 115 | mysql_query($msql); |
|
115 |
|
116 | // or die(mysql_error()); |
116 | 117 | ||
117 | } |
118 | } |
118 | 119 | ||
119 | 120 | ||
120 | 121 | ||
121 | function executeCmd($commandLine) { |
122 | function executeCmd($commandLine) { |
122 | 123 | ||
123 | $pipe = popen("$commandLine" , 'r'); |
124 | $pipe = popen("$commandLine" , 'r'); |
124 | 125 | ||
125 | if (!$pipe) { |
126 | if (!$pipe) { |
126 | print "pipe failed."; |
127 | print "pipe failed."; |
127 | return ""; |
128 | return ""; |
128 | } |
129 | } |
129 | $output = ''; |
130 | $output = ''; |
130 | while(!feof($pipe)) { |
131 | while(!feof($pipe)) { |
131 | $contents = fread($pipe, 12); |
132 | $contents = fread($pipe, 12); |
132 | $unpacked = unpack("l*",$contents); |
133 | $unpacked = unpack("l*",$contents); |
133 | $id = $unpacked[1]; |
134 | $id = $unpacked[1]; |
134 | $len = $unpacked[2]; |
135 | $len = $unpacked[2]; |
135 | $progress = $unpacked[3]; |
136 | $progress = $unpacked[3]; |
136 | if ($len>0){ |
137 | if ($len>0){ |
137 | $out= fread($pipe, $len); |
138 | $out= fread($pipe, $len); |
138 | if ($id == 3){ |
139 | if ($id == 3){ |
139 | stat2db($out); |
140 | stat2db($out); |
140 | // $out = $_SERVER['REMOTE_ADDR'] . ";" .$out ; |
141 | // $out = $_SERVER['REMOTE_ADDR'] . ";" .$out ; |
141 | // $retval = system("echo '$out' >> public/blab2stat.txt "); |
142 | // $retval = system("echo '$out' >> public/blab2stat.txt "); |
142 | // $out .= ";" . $retval; |
143 | // $out .= ";" . $retval; |
143 | $id = 0 ; |
144 | $id = 0 ; |
144 | } |
145 | } |
145 | send_message($id,$out,$progress); |
146 | send_message($id,$out,$progress); |
146 | } |
147 | } |
147 | $output .=$out; |
148 | $output .=$out; |
148 | } |
149 | } |
149 | pclose($pipe); |
150 | pclose($pipe); |
150 | return $output; |
151 | return $output; |
151 | } |
152 | } |
152 | 153 | ||
153 | function send_message($id, $message, $progress) { |
154 | function send_message($id, $message, $progress) { |
154 | $d = array('message' => $message , 'progress' => $progress); //prepare json |
155 | $d = array('message' => $message , 'progress' => $progress); //prepare json |
155 | 156 | ||
156 | echo "id: $id" . PHP_EOL; |
157 | echo "id: $id" . PHP_EOL; |
157 | echo "data: " . json_encode($d) . PHP_EOL; |
158 | echo "data: " . json_encode($d) . PHP_EOL; |
158 | 159 | ||
159 | if (PHP_SAPI !== 'cli') { |
160 | if (PHP_SAPI !== 'cli') { |
160 | echo PHP_EOL; |
161 | echo PHP_EOL; |
161 | ob_flush(); |
162 | ob_flush(); |
162 | } |
163 | } |
163 | flush(); |
164 | flush(); |
164 | } |
165 | } |
165 | 166 | ||
166 | 167 | ||
167 | 168 | ||
168 | $code=""; |
169 | $code=""; |
169 | if (PHP_SAPI === 'cli') { |
170 | if (PHP_SAPI === 'cli') { |
170 | $code = $argv[1]; |
171 | $code = $argv[1]; |
171 | } else { |
172 | } else { |
172 | if (isset($_GET["code"])){ |
173 | if (isset($_GET["code"])){ |
173 | $code = $_GET["code"]; |
174 | $code = $_GET["code"]; |
174 | } |
175 | } |
175 | } |
176 | } |
176 | 177 | ||
177 | $data = json_decode($code, true); |
178 | $data = json_decode($code, true); |
178 | 179 | ||
179 | $neve = $data['analysis']['neve']; |
180 | $neve = $data['analysis']['neve']; |
180 | $source= $data['analysis']['datasource']; |
181 | $source= $data['analysis']['datasource']; |
181 | 182 | ||
182 | $xml = '<?xml version="1.0" encoding="utf-8"?>' . xmlWalker($data,'start'); |
183 | $xml = '<?xml version="1.0" encoding="utf-8"?>' . xmlWalker($data,'start'); |
183 | $dom = new DOMDocument; |
184 | $dom = new DOMDocument; |
184 | $dom->loadXML($xml); |
185 | $dom->loadXML($xml); |
185 | 186 | ||
186 | 187 | ||
187 | echo "<pre>"; |
188 | echo "<pre>"; |
188 | echo $dom->saveXML(); |
189 | echo $dom->saveXML(); |
189 | echo "</pre>"; |
190 | echo "</pre>"; |
190 | echo PHP_EOL; |
191 | echo PHP_EOL; |
191 | 192 | ||
192 | $cnt=0; |
193 | $cnt=0; |
193 | 194 | ||
194 | $fstart = $dom->createElement("init"); |
195 | $fstart = $dom->createElement("init"); |
195 | $dom->appendChild($fstart); |
196 | $dom->appendChild($fstart); |
196 | 197 | ||
197 | $el = $dom->getElementsByTagName('neve')->item(0); |
198 | $el = $dom->getElementsByTagName('neve')->item(0); |
198 | $fstart->appendChild($el); |
199 | $fstart->appendChild($el); |
199 | $el = $dom->getElementsByTagName('datasource')->item(0); |
200 | $el = $dom->getElementsByTagName('datasource')->item(0); |
200 | $fstart->appendChild($el); |
201 | $fstart->appendChild($el); |
201 | 202 | ||
202 | $histogramCount = $dom->getElementsByTagName('h1d')->length; |
203 | $histogramCount = $dom->getElementsByTagName('h1d')->length; |
203 | for($i= $histogramCount-1;$i>=0;--$i) |
204 | for($i= $histogramCount-1;$i>=0;--$i) |
204 | { |
205 | { |
205 | $idnode=$dom->createElement("id","$i"); |
206 | $idnode=$dom->createElement("id","$i"); |
206 | 207 | ||
207 | $histo=$dom->getElementsByTagName('h1d')->item($i); |
208 | $histo=$dom->getElementsByTagName('h1d')->item($i); |
208 | $histo->appendChild($idnode); |
209 | $histo->appendChild($idnode); |
209 | 210 | ||
210 | $newnode=$dom->createTextNode("$i"); |
211 | $newnode=$dom->createTextNode("$i"); |
211 | $parent = $histo->parentNode; |
212 | $parent = $histo->parentNode; |
212 | //$histo->setAttribute('id',"$i"); |
213 | //$histo->setAttribute('id',"$i"); |
213 | $fstart->appendChild($histo); |
214 | $fstart->appendChild($histo); |
214 | $parent->appendChild($newnode); |
215 | $parent->appendChild($newnode); |
215 | 216 | ||
216 | } |
217 | } |
217 | 218 | ||
218 | 219 | ||
219 | $xpath = new DOMXpath($dom); |
220 | $xpath = new DOMXpath($dom); |
220 | $nodelist = $xpath->query('//selector|//combiner'); |
221 | $nodelist = $xpath->query('//selector|//combiner'); |
221 | $cnt=1; |
222 | $cnt=1; |
222 | foreach ($nodelist as $plist) { |
223 | foreach ($nodelist as $plist) { |
223 | $newnode=$dom->createElement("plist","$cnt"); |
224 | $newnode=$dom->createElement("plist","$cnt"); |
224 | $fstart->appendChild($newnode); |
225 | $fstart->appendChild($newnode); |
225 | $newnode=$dom->createElement("id","$cnt"); |
226 | $newnode=$dom->createElement("id","$cnt"); |
226 | $plist->appendChild($newnode); |
227 | $plist->appendChild($newnode); |
227 | $cnt++; |
228 | $cnt++; |
228 | } |
229 | } |
229 | 230 | ||
230 | $nodelist = $xpath->query('//list|//list1|//list2'); |
231 | $nodelist = $xpath->query('//list|//list1|//list2'); |
231 | $cnt=1; |
232 | $cnt=1; |
232 | foreach ($nodelist as $plist) { |
233 | foreach ($nodelist as $plist) { |
233 | if( $plist->childNodes->length === 0){ |
234 | if( $plist->childNodes->length === 0){ |
234 | $newnode=$dom->createTextNode("-1"); |
235 | $newnode=$dom->createTextNode("-1"); |
235 | $plist->appendChild($newnode); |
236 | $plist->appendChild($newnode); |
236 | } |
237 | } |
237 | } |
238 | } |
238 | //$isEmpty = $elem->childNodes->length === 0; |
239 | //$isEmpty = $elem->childNodes->length === 0; |
239 | 240 | ||
240 | echo "<pre>"; |
241 | echo "<pre>"; |
241 | echo $dom->saveXML(); |
242 | echo $dom->saveXML(); |
242 | echo "</pre>"; |
243 | echo "</pre>"; |
243 | 244 | ||
244 | echo PHP_EOL; |
245 | echo PHP_EOL; |
245 | 246 | ||
246 | $str=""; |
247 | $str=""; |
247 | foreach ($dom->getElementsByTagName('analysis') as $node){ |
248 | foreach ($dom->getElementsByTagName('analysis') as $node){ |
248 | $str = PHP_EOL . "void Blab2::event(){" . PHP_EOL ; |
249 | $str = PHP_EOL . "void Blab2::event(){" . PHP_EOL ; |
249 | $str .= parseDOMNode($node) . ";"; |
250 | $str .= parseDOMNode($node) . ";"; |
250 | $str .= PHP_EOL . "}" . PHP_EOL ; |
251 | $str .= PHP_EOL . "}" . PHP_EOL ; |
251 | } |
252 | } |
252 | 253 | ||
253 | 254 | ||
254 | $nodelist = $fstart->getElementsByTagName('h1d'); |
255 | $nodelist = $fstart->getElementsByTagName('h1d'); |
255 | $init = PHP_EOL . "void Blab2::Init(){" . PHP_EOL ; |
256 | $init = PHP_EOL . "void Blab2::Init(){" . PHP_EOL ; |
256 | $init .="fNeve=$neve;" . PHP_EOL ; |
257 | $init .="fNeve=$neve;" . PHP_EOL ; |
257 | $init .="fData=$source;" . PHP_EOL ; |
258 | $init .="fData=$source;" . PHP_EOL ; |
258 | foreach ($nodelist as $node) { |
259 | foreach ($nodelist as $node) { |
259 | $init .= $node->nodeName . "("; |
260 | $init .= $node->nodeName . "("; |
260 | $cnt=0; |
261 | $cnt=0; |
261 | foreach ($node->childNodes as $el) { |
262 | foreach ($node->childNodes as $el) { |
262 | if ($cnt) { |
263 | if ($cnt) { |
263 | $init .= ","; |
264 | $init .= ","; |
264 | } |
265 | } |
265 | if (is_numeric($el->nodeValue)) { |
266 | if (is_numeric($el->nodeValue)) { |
266 | $init .= $el->nodeValue ; |
267 | $init .= $el->nodeValue ; |
267 | } else { |
268 | } else { |
268 | $init .= '"' . $el->nodeValue . '"' ; |
269 | $init .= '"' . $el->nodeValue . '"' ; |
269 | } |
270 | } |
270 | $cnt++; |
271 | $cnt++; |
271 | } |
272 | } |
272 | $init .= ");" . PHP_EOL; |
273 | $init .= ");" . PHP_EOL; |
273 | } |
274 | } |
274 | 275 | ||
275 | foreach ($fstart->getElementsByTagName('plist') as $node) { |
276 | foreach ($fstart->getElementsByTagName('plist') as $node) { |
276 | $init .= $node->nodeName . "(" .$node->nodeValue . ");" . PHP_EOL; |
277 | $init .= $node->nodeName . "(" .$node->nodeValue . ");" . PHP_EOL; |
277 | } |
278 | } |
278 | $init .= PHP_EOL . "}" . PHP_EOL ; |
279 | $init .= PHP_EOL . "}" . PHP_EOL ; |
279 | 280 | ||
280 | 281 | ||
281 | $cmd0 = ".L BParticle.cc+". PHP_EOL; |
282 | $cmd0 = ".L BParticle.cc+". PHP_EOL; |
282 | $cmd0 .= ".L BEvent.cc+". PHP_EOL; |
283 | $cmd0 .= ".L BEvent.cc+". PHP_EOL; |
283 | $cmd0 .= ".L Blab2.cc". PHP_EOL; |
284 | $cmd0 .= ".L Blab2.cc". PHP_EOL; |
284 | $cmd0 .= $str. PHP_EOL; |
285 | $cmd0 .= $str. PHP_EOL; |
285 | $cmd0 .= $init. PHP_EOL; |
286 | $cmd0 .= $init. PHP_EOL; |
286 | $cmd0 .= "Blab2 *blab2 = new Blab2();". PHP_EOL; |
287 | $cmd0 .= "Blab2 *blab2 = new Blab2();". PHP_EOL; |
287 | 288 | ||
288 | //LONG RUNNING TASK |
289 | //LONG RUNNING TASK |
289 | //executeCmd(5, 'ls'); |
290 | //executeCmd(5, 'ls'); |
290 | //executeCmd(5, "/home/rok/public_html/blab/blockly/app/runscript.sh"); |
291 | //executeCmd(5, "/home/rok/public_html/blab/blockly/app/runscript.sh"); |
291 | //executeCmd(5, 'for ((i=0;i<5;i++)) ; do echo $i; sleep 1; done;'); |
292 | //executeCmd(5, 'for ((i=0;i<5;i++)) ; do echo $i; sleep 1; done;'); |
292 | if (file_exists('/opt/root/bin/thisroot.sh')){ |
293 | if (file_exists('/opt/root/bin/thisroot.sh')){ |
293 | $profile ='/opt/root/bin/thisroot.sh'; |
294 | $profile ='/opt/root/bin/thisroot.sh'; |
294 | } else { |
295 | } else { |
295 | $profile ='/home/rok/root/bin/thisroot.sh'; |
296 | $profile ='/home/rok/root/bin/thisroot.sh'; |
296 | } |
297 | } |
297 | $cmd ="bash -c 'source $profile ;cd src; root -b -l <<EOL" . PHP_EOL; |
298 | $cmd ="bash -c 'source $profile ;cd src; root -b -l <<EOL" . PHP_EOL; |
298 | $cmd .= $cmd0. PHP_EOL; |
299 | $cmd .= $cmd0. PHP_EOL; |
299 | $cmd .= "EOL'". PHP_EOL; |
300 | $cmd .= "EOL'". PHP_EOL; |
300 | 301 | ||
301 | send_message(0,$cmd0,0); |
302 | send_message(0,$cmd0,0); |
302 | executeCmd($cmd); |
303 | executeCmd($cmd); |
303 | send_message('CLOSE', $data,100); |
304 | send_message('CLOSE', $data,100); |
304 | ?> |
305 | ?> |
305 | 306 |