Subversion Repositories f9daq

Rev

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