Subversion Repositories f9daq

Rev

Rev 267 | Rev 320 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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