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