[7] | 1 | <?php
|
---|
| 2 |
|
---|
[18] | 3 | $us3bin = exec( "ls -d ~us3/lims/bin" );
|
---|
| 4 | $us3etc = exec( "ls -d ~us3/lims/etc" );
|
---|
[7] | 5 | include_once "$us3bin/listen-config.php";
|
---|
| 6 |
|
---|
[30] | 7 | // Get the US3 system release of latest file on download site
|
---|
[37] | 8 | //$s_cmd1 = "ssh us3@ultrascan.uthscsa.edu 'ls -t /srv/www/htdocs/ultrascan3/software/us3*";
|
---|
| 9 | //$s_cmd1 = "$s_cmd1 | sed -n 1p | cut -d\- -f3" . "'";
|
---|
| 10 | $s_cmd1 = 'ls -t /srv/www/htdocs/uscn/ultrascan3/software/us3*';
|
---|
| 11 | $s_cmd1 = "$s_cmd1 | sed -n 1p | cut -d\- -f3";
|
---|
[30] | 12 | $s_cmd2 = exec( $s_cmd1 );
|
---|
| 13 | $sysrev = $s_cmd2;
|
---|
[37] | 14 | echo "s_cmd1=$s_cmd1\n";
|
---|
| 15 | echo "sysrev=$sysrev\n";
|
---|
[7] | 16 |
|
---|
| 17 | // Global variables
|
---|
| 18 | $notice_db = "us3_notice";
|
---|
| 19 | $dbhost = "localhost";
|
---|
[34] | 20 | $dbuser = "us3_notice";
|
---|
| 21 | $dbpassw = "us3_notice";
|
---|
[7] | 22 |
|
---|
| 23 | // Produce some output temporarily, so cron will send me message
|
---|
| 24 | $now = time();
|
---|
| 25 | echo "Time started: " . date( 'Y-m-d H:i:s', $now ) . "\n";
|
---|
| 26 |
|
---|
| 27 | // Read and parse the local notice file
|
---|
| 28 |
|
---|
| 29 | $n_fname = "$us3etc/us3-notice.xml";
|
---|
| 30 | $fh = fopen( $n_fname, "r" );
|
---|
| 31 | $xml = fread( $fh, filesize( $n_fname ) );
|
---|
| 32 |
|
---|
| 33 | $parser = new XMLReader();
|
---|
| 34 | $parser->xml( $xml );
|
---|
| 35 |
|
---|
| 36 | $notices = array();
|
---|
| 37 | $keys = array();
|
---|
| 38 |
|
---|
| 39 | echo "=====START of PARSE LOOP===== \n";
|
---|
| 40 | while( $parser->read() )
|
---|
| 41 | {
|
---|
| 42 | $n_type = $parser->nodeType;
|
---|
| 43 | //echo "n_type=$n_type \n";
|
---|
| 44 |
|
---|
| 45 | if ( $n_type == XMLReader::ELEMENT )
|
---|
| 46 | {
|
---|
| 47 | $name = $parser->name;
|
---|
| 48 | //echo "name=$name \n";
|
---|
| 49 |
|
---|
| 50 | if ( $name == "notice" )
|
---|
| 51 | {
|
---|
| 52 | $type = $parser->getAttribute( "type" );
|
---|
| 53 | //echo " type=$type \n";
|
---|
| 54 | $parser->moveToAttribute( "revision" );
|
---|
| 55 | $rev = $parser->value;
|
---|
| 56 | //echo " rev=$rev \n";
|
---|
| 57 | if ( $rev == "latest" )
|
---|
| 58 | $rev = $sysrev;
|
---|
| 59 | //echo " rev=$rev \n";
|
---|
| 60 |
|
---|
| 61 | $key = $type . $rev;
|
---|
| 62 | $notices[ $key ] = array();
|
---|
| 63 | //echo " key=$key \n";
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | else if ( $n_type == XMLReader::TEXT )
|
---|
| 68 | {
|
---|
| 69 | $msg = $parser->readString();
|
---|
| 70 | //echo " msg=+++$msg+++ \n";
|
---|
| 71 | $len = strlen( $msg );
|
---|
| 72 |
|
---|
| 73 | if ( $len > 0 )
|
---|
| 74 | { // Only add if not an empty message
|
---|
| 75 | $msg = preg_replace( "/\@revision/", $rev, $msg );
|
---|
| 76 | $msg = preg_replace( "/'/", "\\'", $msg );
|
---|
| 77 | $notices[ $key ][ 'type' ] = $type;
|
---|
| 78 | $notices[ $key ][ 'rev' ] = $rev;
|
---|
| 79 | $notices[ $key ][ 'msg' ] = $msg;
|
---|
| 80 | $notices[ $key ][ 'act' ] = 'add';
|
---|
| 81 | $notices[ $key ][ 'id' ] = '0';
|
---|
| 82 |
|
---|
| 83 | $keys[] = $key;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | $parser->close();
|
---|
| 89 | echo "=====END of PARSE LOOP===== \n";
|
---|
| 90 |
|
---|
| 91 | // Get data from notice DB. Update the action field to
|
---|
| 92 | // reflect which action is required on each entry:
|
---|
| 93 | // "add" - is only present in file (add to DB);
|
---|
| 94 | // "del" - is only present in DB (delete from DB);
|
---|
| 95 | // "upd" - present in both, but messages differ (update in DB);
|
---|
| 96 | // "none" - present in both and messages identical (no DB update).
|
---|
| 97 |
|
---|
[35] | 98 | $noteLink = mysqli_connect( $dbhost, $dbuser, $dbpassw, $notice_db );
|
---|
[7] | 99 |
|
---|
[35] | 100 | if ( ! $noteLink )
|
---|
[7] | 101 | {
|
---|
[35] | 102 | echo "Could not connect to DB $notice_db - " . mysqli_error($noteLink) . "\n";
|
---|
[7] | 103 | exit();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | $query = "SELECT id, type, revision, message FROM notice";
|
---|
| 107 |
|
---|
[35] | 108 | $result = mysqli_query( $noteLink, $query )
|
---|
| 109 | or die( "Query failed : $query<br />" . mysqli_error($noteLink) );
|
---|
[7] | 110 |
|
---|
| 111 | echo "=====START of DB QUERY LOOP===== \n";
|
---|
[35] | 112 | $num_rows = mysqli_num_rows( $result );
|
---|
[7] | 113 |
|
---|
| 114 | echo " numrows = $num_rows \n";
|
---|
[35] | 115 | while ( list( $id, $type, $rev, $msg ) = mysqli_fetch_array( $result ) )
|
---|
[7] | 116 | {
|
---|
| 117 | $key = $type . $rev;
|
---|
| 118 |
|
---|
| 119 | if ( in_array( $key, $keys ) )
|
---|
| 120 | { // Entry is in both file and DB
|
---|
[18] | 121 | $msgf = $notices[ $key ][ 'msg' ];
|
---|
[7] | 122 | $notices[ $key ][ 'id' ] = $id;
|
---|
| 123 | $notices[ $key ][ 'type' ] = $type;
|
---|
| 124 | $notices[ $key ][ 'rev' ] = $rev;
|
---|
| 125 |
|
---|
| 126 | if ( strcmp( $msg, $msgf ) == 0 )
|
---|
| 127 | { // Messages match, so no update is needed
|
---|
| 128 | $notices[ $key ][ 'act' ] = 'none';
|
---|
| 129 | }
|
---|
| 130 | else
|
---|
| 131 | { // Messages differ, so DB entry must be updated
|
---|
| 132 | $notices[ $key ][ 'act' ] = 'upd';
|
---|
| 133 | $notices[ $key ][ 'msg' ] = $msgf;
|
---|
| 134 | echo " msg = ++$msg++\n";
|
---|
| 135 | echo " msgf = ++$msgf++\n";
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | else
|
---|
| 140 | { // Entry is only in DB, so a delete is needed
|
---|
| 141 | $notices[ $key ] = array();
|
---|
| 142 | $notices[ $key ][ 'id' ] = $id;
|
---|
| 143 | $notices[ $key ][ 'type' ] = $type;
|
---|
| 144 | $notices[ $key ][ 'rev' ] = $rev;
|
---|
| 145 | $notices[ $key ][ 'act' ] = 'del';
|
---|
| 146 | $notices[ $key ][ 'msg' ] = $msg;
|
---|
| 147 | $keys[] = $key;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | echo "=====END of DB QUERY LOOP===== \n";
|
---|
| 151 |
|
---|
| 152 | echo "=====START of DB Update LOOP===== \n";
|
---|
| 153 | foreach ( $keys as $key )
|
---|
| 154 | {
|
---|
| 155 | $type = $notices[ $key ][ 'type' ];
|
---|
| 156 | $rev = $notices[ $key ][ 'rev' ];
|
---|
| 157 | $act = $notices[ $key ][ 'act' ];
|
---|
| 158 | $id = $notices[ $key ][ 'id' ];
|
---|
| 159 | $msg = $notices[ $key ][ 'msg' ];
|
---|
| 160 | $len = strlen( $msg );
|
---|
| 161 | echo "-- key=$key --\n";
|
---|
| 162 | echo " type: $type \n";
|
---|
| 163 | echo " rev: $rev \n";
|
---|
| 164 | echo " act: $act \n";
|
---|
| 165 | echo " id: $id \n";
|
---|
| 166 | // echo " msg: $msg \n";
|
---|
| 167 | echo " msg: ( $len characters ) \n";
|
---|
| 168 |
|
---|
| 169 | if ( $act == "add" )
|
---|
| 170 | { // Must add the entry to the database
|
---|
| 171 | $query = "INSERT INTO notice " .
|
---|
| 172 | "SET type='$type', revision='$rev', message='$msg'";
|
---|
| 173 | }
|
---|
| 174 | else if ( $act == "del" )
|
---|
| 175 | { // Must delete the entry from the database
|
---|
| 176 | $query = "DELETE FROM notice WHERE id='$id'";
|
---|
| 177 | }
|
---|
| 178 | else if ( $act == "upd" )
|
---|
| 179 | { // Must update an existing entry in the database
|
---|
| 180 | $query = "UPDATE notice " .
|
---|
| 181 | "SET message='$msg' " .
|
---|
| 182 | "WHERE id='$id'";
|
---|
| 183 | }
|
---|
| 184 | echo " query: [ $query ] \n";
|
---|
| 185 |
|
---|
[35] | 186 | $result = mysqli_query( $noteLink, $query )
|
---|
| 187 | or die( "Query failed : $query<br />" . mysqli_error($noteLink) );
|
---|
[7] | 188 |
|
---|
| 189 | }
|
---|
| 190 | echo "=====END of DB Update LOOP===== \n";
|
---|
| 191 |
|
---|
| 192 | ?>
|
---|