Subversion Repositories computer_asset_manager_v1

Rev

Rev 5 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php

/*
 * getSysinfoMail.php
 * 
 * Author: R. W. Rodolico
 * Copyright: 20151002, Daily Data, Inc.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 * 
 * Read messages from mail server. If they are YAML or XML sysinfo reports
 * save them to local drive, then remove them from mail server.
 * Files will be named with the following values, separated by underscores
 * report date (YYYY-MM-DD)
 * report time (HH:MM:SS) (CONTAINS COLONS)
 * client name (MAY CONTAIN SPACES)
 * device name
 * serial number (default to 00000 if it is not in the report)
 * 
 * Requires php5-imap
 * apt-get install php5-imap
 * 
 * Good information taken from
 * http://docstore.mik.ua/orelly/webprog/pcook/ch17_04.htm
 * 
 */

$VERSION = '1.0';
$MAXDOWNLOAD = 5000; // maximum number of e-mails to download at one time
$CLI = isset( $_SERVER["TERM"]); // only set if we are not in a cron job



require "sysinfoRead.php.conf";

function OpenMailbox ( $username, $password, $server, $ssl = true, $port=null, $mailbox = 'INBOX' ) {
   if ( $port == null )
      if ( $ssl ) $port = 993;  else $port = 143;
   $server = gethostbyname( $server );
   $serverString = "$server:$port/imap";
   if ( $ssl ) $serverString .= '/ssl';
   $serverString .= '/novalidate-cert';
   $serverString = '{' . $serverString . '}' . $mailbox;
   $server = imap_open( $serverString, $username, $password );
   
   return array($server, $serverString);
}

function getContents ($mail, $n ) {
   $body = '';
   $st = imap_fetchstructure($mail, $n);
   if (!empty($st->parts)) {
       for ($i = 0, $j = count($st->parts); $i < $j; $i++) {
           $part = $st->parts[$i];
           if ($part->subtype == 'PLAIN') {
                $body = imap_fetchbody($mail, $n, $i+1);
           }
        }
   } else {
       $body = imap_body($mail, $n);
   }
   return getBody( $body );
}

function getBody ( $body ) {
   global $bodyContents;
   foreach ( $bodyContents as $key => $regexes ) {
      $matches;
      $pattern = $regexes['startTag'] . '.*' . $regexes['endTag'];
      if ( preg_match( "/$pattern/ms", $body, $matches ) ) 
         return array( $key, $matches[0] );
   }
   return array( false, '' );
}


function makeFileName ( $data, $type ) {
   global $outpath;
   // we will create filename as yyyy-mm-dd_HH:mm:ss_client_hostname_serial
   $date =  empty( $data['report']['date'] ) ? '' : strtotime( $data['report']['date'] ); // store in Unix timestamp
   $date = strftime( '%F_%T', $date ); // save a copy of the date in SQL format

   // add client name
   $client = empty( $data['report']['client'] ) ? '' : $data['report']['client'];

   // add hostname
   $hostname = empty ( $data['system']['hostname'] ) ? '' : $data['system']['hostname'];

   // add serial number if it exists, otherwise use '00000' (5 0's)
   $serial = empty ( $data['system']['serial'] ) ? '00000' : $data['system']['serial'];
   return ( $date && $client && $hostname && $serial ) ? "$outpath/$date" . '_' . $client . '_' . $hostname . '_' . "$serial.$type" : false;
   
} 

function saveFile( $filename, $contents, $timestamp ) {
   $bytesWritten = file_put_contents( $filename, $contents );
   if ( $bytesWritten ) {
      if ( ! touch( $filename, $timestamp ) ) logError( "could not set timestamp of $filename to $timestamp" );
   }
   return $bytesWritten;
}

function logError ( $message ) {
   global $logFile;
   error_log( "$message\n", 3, $logFile );
}

/**************************************************************************************************************
 * Begin Main Program
 **************************************************************************************************************/


// ensure the path we want to write to exists
if ( ! is_dir( $outpath ) ) {
   if ( ! mkdir( $outpath, 0777, true ) ) {
      die( "Could not create path $outpath\n" );
   }
}

$listMailboxes =  isset( $argv[1] ) ; // anything passed on cli results in a list of mailboxes instead of the job

foreach ( $servers as $thisServer ) {
   if ($MAXDOWNLOAD <= 0 ) break;
   if ( ! $thisServer['enabled'] ) continue; // ignore anything that is not enabled
   print "Working on " . $thisServer['servername'] . "\n";
   $messagesToDelete = array(); // trap the UID's of messages to delete, more accurate than using message number
   print "Opening " . $thisServer['servername'] . "\n";
   
   // open the mail server connection. NOTE: the $connectString is used in other functions, so we need to preserve it
   list( $server, $connectString ) = OpenMailbox(  $thisServer['username'], 
                                                   $thisServer['password'], 
                                                   $thisServer['servername'] , 
                                                   $thisServer['ssl'],
                                                   $thisServer['port'], 
                                                   $listMailboxes ? '' : $thisServer['mailbox'] 
                                                );
   print "Success with $connectString\n";
   if ( $listMailboxes ) {
      $list = imap_list($server, $connectString, "*");
      print_r( $list ); print "\n";
      continue;
   }
   $count = 0;
   if ( $server ) {
      $folderCount = imap_num_msg( $server );
      for ( $num = 1; ($num <= $folderCount) && $MAXDOWNLOAD; $num++ ) {
         list( $type,$body ) = getContents( $server, $num );
         if ( ! $type ) {
            logError( "Unknown File Type" );
            continue;
         }
         if ( ! ($data = $bodyContents[$type]['eval']($body) ) ) {
            logError( "Unable to parse file" );
            continue;
         }
         if ( !(  $filename  = makeFileName( $data, $type ) ) ) {
            logError( "unable to create a file name" );
            continue;
         }
         if ( !(  $bytesWritten = saveFile( $filename, $body, strtotime( $data['report']['date'] ) ) ) ) {
            logError( "unable to save file $filename" );
            continue;
         }
         $messagesToDelete[] = imap_uid( $server, $num );
         $count++;
         if ( $CLI ) print '.'; // only do this if interactive session
         $MAXDOWNLOAD--;
      } // for
      if ( $thisServer['deleteProcessed'] ) {
         foreach ( $messagesToDelete as $uid ) {
            imap_delete( $server, $uid, FT_UID ) or logError( "Can't delete [$uid]: imap_last_error()" );
         }
         imap_expunge( $server );
      }  // if delete
      imap_close( $server );
   } else {
      print "Could not open server " . $thisServer['servername'] . "\n";
   } // if..else
   print "\nProcessed $count messages\n\n";
} // outer for
?>