· 7 years ago · Feb 19, 2019, 11:56 AM
1<?php
2
3//$pass='go';
4
5/*******************************************************************************
6* COMPONENT NAME:
7* msgproc.php
8*
9* DESCRIPTION:
10* This script accepts the various XML documents from G*.
11* The document is passed to the correct routine
12* in msgproc.php based on what it is.
13*
14*******************************************************************************/
15require('sensdb.php');
16require('dbutil.php');
17require('msgproc_util.php');
18
19// first this is to pipe all output to the buffer for loggin later in the file
20// echo and print statements will get logged instead of outputted
21ob_start();
22
23//$raw_post_data = $HTTP_RAW_POST_DATA;
24
25$raw_post_data = file_get_contents("php://input");
26
27$time_log = date("d/m/Y G:i:s") . ' - aeroastro sensor sys time';
28
29/* CONNECT TO THE DATABASE */
30sensdb_connect();
31
32$script_time = date("M-d-Y H:i:s");
33
34// logging variables
35$type='';
36$byte_count=0;
37$message_id='';
38$error='';
39$error_type='';
40$source = $_SERVER['REMOTE_ADDR'];
41
42/*******************************************************************************
43* COMPONENT NAME:
44* main process
45*
46* DESCRIPTION:
47*
48*******************************************************************************/
49
50/****** formerly function test_read_xml_post() ***********************/
51//$fp = fopen("/usr/local/www/data/sensor/sens/message_test.xml", "r");
52//$fp = fopen("php://input", "r");
53
54echo "string length:" . strlen($raw_post_data) . ' ';
55
56// reject all data not from Globalstar of development
57//if ( $source == "207.88.248.142" ) {
58 $data = trim($raw_post_data);
59//} else {
60// $data = NULL;
61//}
62
63$byte_count += strlen($data);
64//fclose($fp);
65
66// test of php5 parsing xml .... much easier
67if ( $xml = simplexml_load_string($data) ) {
68
69 // innocent until proven guilty
70 $status = true;
71
72 // this will help us find the root element to determine message type
73 $dom = dom_import_simplexml($xml);
74 $xml_root = $dom->tagName;
75
76 // process stumessages here
77 if ( $xml_root == 'stuMessages' ) {
78
79 $type = 'STxMessage';
80
81 // what is the message id
82 $xml_messageID = $xml->xpath('/stuMessages/@messageID');
83 foreach ( $xml_messageID as $id ) {
84 $message_id = $id;
85 }
86
87 // what is the packet time
88 $packetTimes = $xml->xpath('/stuMessages/@timeStamp');
89 foreach ( $packetTimes as $_time ) {
90 $packetTime = $_time;
91 }
92
93 $time_log .= "\n". $packetTime . ' - globalstar packet time';
94
95 // how many messages
96 $message_count = count($xml->xpath('/stuMessages/stuMessage'));
97
98 if ( $message_count >= 1 ) {
99
100 // fomat the sql string
101 $sql = get_msglog_sql($type, $source, $byte_count, $message_id, $error );
102
103 if ( mysql_query($sql) ) {
104
105 // get the insert id
106 // be careful not to change the sql above which might break our ability here to get the last insert id
107 $incoming_id = mysql_insert_id();
108
109 echo $script_time . " - stu_message packet $message_id logged: insert_id:$incoming_id";
110 } else {
111 echo $script_time . " - stu_message failed logging";
112 }
113
114// mysql_query("LOCK TABLES STxMessage WRITE");
115// mysql_query("SET AUTOCOMMIT = 0");
116
117 mysql_query("BEGIN");
118
119 foreach( $xml->stuMessage as $stuMessage ) {
120
121 $unixtime = $stuMessage->unixTime;
122 $gps = $stuMessage->gps;
123 $payload = $stuMessage->payload;
124 // convert to an ESN int
125 $esn = $stuMessage->esn;
126 $esn = esn_to_num($esn);
127
128 // test the message time against system time
129 // JL 2007
130 $time_log .= "\n" . gmdate("d/m/Y H:i:s", "$unixtime") . " - packet stu message time";
131
132 // JL 2006 Apr
133 // log message data larger than 255 chars for TSM account a la Tony B
134 // these messages don't fit in our DB
135 if ( strlen($payload) > 255 ) {
136
137 // log any output
138 $large_message_log = '/usr/local/www/logs/large_message_log';
139
140 $sql = 'INSERT INTO STxLargeMessage ( ESN,GPS,Payload,UnixTime,IncomingID) values ("'.$esn.'","'.$gps.'","'.$payload.'","'.$unixtime.'","'.$incoming_id.'")';
141 // Perform Insert of large message
142 $result = @mysql_query($sql);
143
144 // Let's make sure the file exists and is writable first.
145 if ( is_writable($large_message_log) ) {
146
147 // The file pointer is at the bottom of the file hence
148 $handle = @fopen($large_message_log, 'a');
149
150 // Write $response to our opened file.
151 @fwrite($handle, "\n".$data);
152 @fclose($handle);
153 }
154 }
155
156 // do we have an ESN for this message
157// $select_esn = 'select ESN from STx where ESN = "'.$esn.'"';
158// $result = mysql_query($select_esn);
159// $result_count = mysql_num_rows($result);
160
161 $sql = 'INSERT INTO STxMessage( ESN,GPS,Payload,UnixTime,IncomingID) values ("'.$esn.'","'.$gps.'","'.$payload.'","'.$unixtime.'","'.$incoming_id.'")';
162 // Perform Insert
163 $result = mysql_query($sql);
164
165 $GMT_unixtime = gmdate("Y-m-d H:i:s", "$unixtime");
166
167 $update = 'update STx set MessageCount = MessageCount + 1, LastMessageTime = "' . $GMT_unixtime . '" where ESN = "' . $esn . '"';
168
169 // increment the message count by 1
170 mysql_query($update);
171 unset($update);
172
173 // Check db result
174 if ( !$result ) {
175 $status = false;
176 // log the error
177 echo 'Invalid query: ' . mysql_error() . "\n";
178 echo $message .= 'Whole query: ' . $query;
179 }
180 unset($result);
181
182 unset($unixtime);
183 unset($gps);
184 unset($payload);
185 unset($esn);
186 }
187
188 if ( $status ) {
189 mysql_query("COMMIT");
190
191 } else {
192 mysql_query("ROLLBACK");
193 }
194// mysql_query("UNLOCK TABLES");
195// mysql_query("SET AUTOCOMMIT = 1");
196
197 } else { // no data but valid xml, return pass, probably G* "pining" aeroastro for life
198 $error = "no data in xml packet id:" . $message_id . "\n";
199 echo $error;
200 }
201
202 // make our response xml for globalstar
203 $response = make_globalstar_response_xml('stuResponseMsg', $message_id, $status);
204
205 } elseif ( $xml_root == 'prvmsgs' ) { // process prvmsgs messages
206
207 /*******************************************************************************
208 * COMPONENT NAME:
209 * PRVMSGS
210 *
211 * DESCRIPTION:
212 * These routines accept the PRVMSGS sent by Globalstar in response to the
213 * INIMSGS that were sent. This routine stores the PRVMSGS into the database,
214 * and also sends a respose message back to Globalstar.
215 *
216 *******************************************************************************/
217
218 $type = 'PrvMessage';
219
220 // what is the message id
221 $xml_messageID = $xml->xpath('/prvmsgs/@prvMessageID');
222 foreach ( $xml_messageID as $id ) {
223 $message_id = $id;
224 }
225
226 // how many messages
227 $message_count = count($xml->xpath('/prvmsgs/prvmsg'));
228
229 if ( $message_count >= 1 ) {
230
231 // fomat the sql string for logging
232 $sql = get_msglog_sql($type, $source, $byte_count, $message_id, $error );
233
234 if ( mysql_query($sql) ) {
235 // get the insert id
236 $incoming_id = mysql_insert_id();
237 echo $script_time . " - PrvMessage packet $message_id logged";
238 } else {
239 echo $script_time . " - PrvMessage failed logging";
240 }
241
242 mysql_query("BEGIN");
243
244 // loop through the messages
245 foreach( $xml->prvmsg as $prvmsg ) {
246
247 $esn = esn_to_num($prvmsg->esn);
248 $provid = $prvmsg->provID;
249 $tstart = $prvmsg->tStart;
250 $tend = $prvmsg->tEnd;
251 $txretryminsec = $prvmsg->txRetryMinSec;
252 $txretrymaxsec = $prvmsg->txRetryMaxSec;
253 $txretries = $prvmsg->txRetries;
254 $rfchannel = $prvmsg->rfChannel;
255
256 $sql = 'REPLACE INTO PrvMessage ( ESN, ProvID, TStart, TEnd, TxRetryMinSec, TxRetryMaxSec, TxRetries, RFChannel, IncomingID) values ("'.$esn.'","'.$provid.'","'.$tstart.'","'.$tend.'","'.$txretryminsec.'","'.$txretrymaxsec.'","'.$txretries.'","'.$rfchannel.'","'.$incoming_id.'")';
257
258 // Perform Insert
259 $result = mysql_query($sql);
260
261 // insert status "change" record
262 $insert_sql = 'insert INTO STxStatusChangeHistory (StatusChangeID, ESN, Status, StatusChangeTimestamp) values ("","'.$esn.'","Provisioned",NOW())';
263
264 // Perform Insert
265 $insert = mysql_query($insert_sql);
266
267 // Check result
268 if (! $result) {
269
270 $status = false;
271 // log the error
272 echo 'Invalid provisioning: ' . mysql_error() . "\n";
273 echo 'Whole query: ' . $sql;
274
275 } else {
276
277 $sql = 'UPDATE STx SET Status="Provisioned" WHERE ESN ="'.$esn.'"';
278 $result = mysql_query($sql);
279
280 // Check result
281 if (! $result) {
282 $status = false;
283 // log the error
284 echo 'Invalid STx update during provisioning: ' . mysql_error() . "\n";
285 echo 'Whole query: ' . $sql;
286 }
287 }
288
289 unset($result);
290
291 unset($esn);
292 unset($provid);
293 unset($tstart);
294 unset($tend);
295 unset($txretryminsec);
296 unset($txretrymaxsec);
297 unset($txretries);
298 unset($rfchannel);
299
300 }
301
302 if ( $status ) {
303 mysql_query("COMMIT");
304 } else {
305 mysql_query("ROLLBACK");
306 msglog_update($message_id, "PrvMessage INSERT ERROR");
307 }
308 }
309
310 // make our response xml for globalstar
311 $response = make_globalstar_response_xml('prvResponseMsg', $message_id, $status);
312
313 } elseif ( $xml_root == 'instmsgs' ) { // process instmsgs messages
314
315 /*******************************************************************************
316 * COMPONENT NAME:
317 * INSTMSGS
318 *
319 * DESCRIPTION:
320 * After the VAR gets the PRVMSGS and installs the RTU, it will send the
321 * INSTMSG back here to indicate where the unit was installed.
322 *
323 *******************************************************************************/
324
325 $type = 'InstMessage';
326
327 // what is the message id
328 $xml_messageID = $xml->xpath('/instmsgs/@instMessageID');
329 foreach ( $xml_messageID as $id ) {
330 $message_id = $id;
331 }
332
333 // how many messages
334 $message_count = count($xml->xpath('/instmsgs/instmsg'));
335
336 if ( $message_count >= 1 ) {
337
338 // fomat the sql string for logging
339 $sql = get_msglog_sql($type, $source, $byte_count, $message_id, $error );
340
341 if ( mysql_query($sql) ) {
342 // get the insert id
343 $incoming_id = mysql_insert_id();
344 echo $script_time . " - InstMessage packet $message_id logged";
345 } else {
346 echo $script_time . " - InstMessage failed logging";
347 }
348
349 mysql_query("BEGIN");
350
351 // loop through the messages
352 foreach( $xml->instmsg as $instmsg ) {
353
354 $esn = esn_to_num($instmsg->esn);
355 $provid = $instmsg->provID;
356 $rfchannel = $instmsg->rfChannel;
357 $tactual = $instmsg->tActual;
358 $deltat = $instmsg->deltaT;
359 $latitude = $instmsg->latitude;
360 $longitude = $instmsg->longitude;
361 $txretries = $instmsg->txRetries;
362 $txoffset = $instmsg->txOffset;
363 $txretryminsec = $instmsg->txRetryMinSec;
364 $txretrymaxsec = $instmsg->txRetryMaxSec;
365 $powerlevel = $instmsg->powerLevel;
366
367 $sql = 'REPLACE INTO InstMessage ( ESN
368 ,ProvID
369 ,IncomingID
370 ,RFChannel
371 ,TActual
372 ,DeltaT
373 ,Latitude
374 ,Longitude
375 ,TxRetries
376 ,TxOffset
377 ,TxRetryMinSec
378 ,TxRetryMaxSec
379 ,PowerLevel ) values ("'.$esn.'"
380 ,"'.$provid.'"
381 ,"'.$incoming_id.'"
382 ,"'.$rfchannel.'"
383 ,"'.$tactual.'"
384 ,"'.$deltat.'"
385 ,"'.$latitude.'"
386 ,"'.$longitude.'"
387 ,"'.$txretries.'"
388 ,"'.$txoffset.'"
389 ,"'.$txretryminsec.'"
390 ,"'.$txretrymaxsec.'"
391 ,"'.$powerlevel.'")';
392
393 // Perform Insert
394 $result = mysql_query($sql);
395
396 // Check result
397 if (! $result) {
398
399 $status = false;
400 // log the error
401 echo 'Invalid InstMessage sql: ' . mysql_error() . "\n";
402 echo 'Whole query: ' . $sql;
403
404 } else {
405
406 $sql = 'UPDATE STx SET Status="Installed" WHERE ESN ="'.$esn.'"';
407 $result = mysql_query($sql);
408
409 // Check result
410 if (! $result) {
411 $status = false;
412 // log the error
413 echo 'Invalid STx update during Install: ' . mysql_error() . "\n";
414 echo 'Whole query: ' . $sql;
415 }
416 }
417 unset($result);
418 unset($esn);
419 unset($provid);
420 unset($tstart);
421 unset($tend);
422 unset($txretryminsec);
423 unset($txretrymaxsec);
424 unset($txretries);
425 unset($rfchannel);
426 unset($txoffset);
427 unset($txretryminsec);
428 unset($txretrymaxsec);
429 unset($powerlevel);
430 }
431
432 if ( $status ) {
433 mysql_query("COMMIT");
434 } else {
435 mysql_query("ROLLBACK");
436 msglog_update($message_id, "InstMessage INSERT ERROR");
437 }
438 }
439
440 // make our response xml for globalstar
441 $response = make_globalstar_response_xml('instResponseMsg', $message_id, $status);
442
443
444 } elseif ( $xml_root == 'inimsgs' ) { // process inimsgs messages
445
446 /*******************************************************************************
447 * COMPONENT NAME:
448 * INIMSGS
449 *
450 * DESCRIPTION:
451 * The 'INIMSGS' are the messages sent to begin the provisioning process. The
452 * message contains a range of ESN numbers to be provisiond. The 'INIMSGS'
453 * are forwarded to Globalstar. Then, the 'get_prvmsgs' function is called
454 * to poll the database for the response 'prvmsgs' that Globalstar will send.
455 *
456 *******************************************************************************/
457
458 $type = 'inimsgs';
459
460 // what is the message id
461 $xml_messageID = $xml->xpath('/inimsgs/@iniMessageID');
462 foreach ( $xml_messageID as $id ) {
463 $message_id = $id;
464 }
465
466 // how many messages
467 $message_count = count($xml->xpath('/inimsgs/inimsg'));
468
469 if ( $message_count >= 1 ) {
470
471 /* DONT FORGET - have a trigger in the database to set a default route for NASCORP */
472
473 $sql = "INSERT INTO STx (ESN, RouteID, GroupID, SubGroupID, STxModel, STxModelGenType, STxName, Note, Status, FilterProfile) VALUES ";
474 $sep = "";
475
476 /* FOR EACH OF THE MESSAGES IN THE PACKET.. .*/
477 foreach ( $xml->inimsg as $inimsg ) {
478
479 $groupid = $inimsg->groupID;
480
481 $subgroupid = strlen($inimsg->subGroupID) ? $inimsg->subGroupID : 'NULL';
482
483 /* FIRST CHECK THAT THE REQUESTED RANGE IS AVAILABLE */
484 $esnStart = $inimsg->esnStart;
485 $esnEnd = $inimsg->esnEnd;
486 $number = check_esn_range($esnStart, $esnEnd, $inuse);
487 $isreal = ($inimsg->isReal == 'Yes') ? true : false;
488 $route_id = get_route_id($groupid, $inimsg->routeAddress);
489
490 if ( $number > 0 ) {
491
492 /* SPLIT THE ESN NUMBER INTO MANUFACTURER AND SERIAL NUMBER */
493 $esnS = explode('-', $esnStart);
494 $esnE = explode('-', $esnEnd);
495
496 /* FOR EACH ESN IN THE SPECIFIED RANGE... */
497 for ( $mfg=(int) $esnS[0]; $mfg <= (int) $esnE[0]; $mfg++ ) {
498
499 for ( $ser=(int) $esnS[1]; $ser <= (int) $esnE[1]; $ser++ ) {
500
501 $sxtmodel = $inimsg->stxModel;
502 $sxtmodelgentype='1';
503
504 switch ($sxtmodel) {
505 case "101-1":
506 $sxtmodel = '101';
507 $sxtmodelgentype = "1";
508 break;
509 case "101-2":
510 $sxtmodel = '101';
511 $sxtmodelgentype = "2";
512 break;
513 case "101-3":
514 $sxtmodel = '101';
515 $sxtmodelgentype = "3";
516 break;
517 }
518
519 $esn = (($mfg << 23) | $ser);
520 $sql .= $sep;
521 $sql .= sprintf("(%d ,%d ,%d ,%s ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' )",
522 $esn
523 ,$route_id
524 ,$groupid
525 ,$subgroupid
526 ,mysql_escape_string($sxtmodel)
527 ,$sxtmodelgentype
528 ,mysql_escape_string($inimsg->stxName)
529 ,mysql_escape_string($inimsg->note)
530 ,$isreal ? 'Initiated' : 'Fake'
531 ,$inimsg->filterProfile);
532 $sep = ",";
533 }
534 }
535
536 $groupid = $inimsg->groupID;
537 $subgroupid = $inimsg->subGroupID;
538 $esnstart = esn_to_num($inimsg->esnStart);
539 $esnend = esn_to_num($inimsg->esnEnd);
540 $stxmodel = $inimsg->stxModel;
541 $stxname = $inimsg->stxName;
542 // new filter setting
543 $filterprofile = $inimsg->filterProfile;
544 $routeaddress = $inimsg->routeAddress;
545// $mobility = $inimsg->mobility;
546// $latdefault = $inimsg->latDefault;
547// $londefault = $inimsg->lonDefault;
548 $stxspacing = $inimsg->txSpacing;
549// $txfrequency = $inimsg->txFrequency;
550// $trequestedstart = $inimsg->tRequestedStart;
551// $trequestedend = $inimsg->tRequestedEnd;
552 $note = $inimsg->note;
553 $isreal = $inimsg->isReal;
554
555 $insert_inimessage = 'INSERT INTO IniMessage (GroupID
556 ,SubGroupID
557 ,ESNStart
558 ,ESNEnd
559 ,STxModel
560 ,STxName
561 ,FilterProfile
562 ,TxSpacing
563 ,RouteAddress
564 ,Note
565 ,IsReal) VALUES ("'.$groupid.'"
566 ,"'.$subgroupid.'"
567 ,"'.$esnstart.'"
568 ,"'.$esnend.'"
569 ,"'.$stxmodel.'"
570 ,"'.$stxname.'"
571 ,"'.$filterprofile.'"
572 ,"'.$stxspacing.'"
573 ,"'.$routeaddress.'"
574 ,"'.$note.'"
575 ,"'.$isreal.'")';
576
577 mysql_query("BEGIN");
578
579 if ( mysql_query($insert_inimessage) ) {
580 // not used for anything
581 $inserted = true;
582 } else {
583 $status=false;
584 }
585
586 unset($groupid);
587 unset($subgroupid);
588 unset($esnstart);
589 unset($esnend);
590 unset($stxmodel);
591 unset($stxname);
592 unset($filterprofile);
593 unset($routeaddress);
594 unset($mobility);
595 unset($stxspacing);
596 unset($note);
597 unset($isreal);
598
599 } else {
600
601 $status = false;
602 echo $script_time . " invalid ESN range\n".$raw_post_data;
603 }
604 }
605
606 /* IF THE CONTENTS OF THE MESSAGE LOOKED OK, THEN COMMIT
607 IT TO THE DATABASE... */
608
609 /* CREATE THE STx TABLE ENTRIES */
610 if ( $status ) {
611
612 if ( $status = mysql_query($sql) ) {
613 mysql_query("COMMIT");
614 } else {
615 $doc->errors = mysql_error();
616 mysql_query("ROLLBACK");
617 }
618 } else {
619 $doc->errors = mysql_error();
620 mysql_query("ROLLBACK");
621 }
622
623 } else {
624 $status = false;
625 echo $script_time . " no messages in xml \n".$raw_post_data;
626 }
627
628 // make our response xml for globalstar
629 $response = make_globalstar_response_xml('iniResponseMsg', $message_id, $status);
630 }
631
632} else {
633
634 // failed to load xml so lets fail
635 $status=false;
636
637 $response = make_globalstar_response_xml('xml_error', 0, $status);
638 echo $script_time . " - xml error\n" . $raw_post_data;
639}
640// log any output
641$msgproc_log = '/usr/local/www/logs/msgproc_log';
642
643// collect any trash here for logging
644$buffer_string = ob_get_contents()."\n";
645
646// Let's make sure the file exists and is writable first.
647if ( is_writable($msgproc_log) ) {
648
649 // The file pointer is at the bottom of the file hence
650 $handle = @fopen($msgproc_log, 'a');
651
652 // Write $response to our opened file.
653 @fwrite($handle, $buffer_string);
654 @fclose($handle);
655}
656
657//empty buffer to logs so that globalstar gets response only
658ob_end_clean();
659
660mysql_close();
661
662// send the output for Globalstar to see
663echo $response;
664
665?>