| Line 32... | Line 32... | 
          
            | 32 |  * apt-get install php5-imap
 | 32 |  * apt-get install php5-imap
 | 
          
            | 33 |  * 
 | 33 |  * 
 | 
          
            | 34 |  * Good information taken from
 | 34 |  * Good information taken from
 | 
          
            | 35 |  * http://docstore.mik.ua/orelly/webprog/pcook/ch17_04.htm
 | 35 |  * http://docstore.mik.ua/orelly/webprog/pcook/ch17_04.htm
 | 
          
            | 36 |  * 
 | 36 |  * 
 | 
          
            | - |   | 37 |  * V1.1 20160203 RWR
 | 
          
            | - |   | 38 |  * Changed to use yaml for the configuration file. Had to take the
 | 
          
            | - |   | 39 |  * anonymous functions which parsed the different file types and convert
 | 
          
            | - |   | 40 |  * them to simpl eval() blocks.
 | 
          
            | - |   | 41 |  * 
 | 
          
            | 37 |  */
 | 42 |  */
 | 
          
            | 38 |  
 | 43 |  
 | 
          
            | 39 | $VERSION = '1.0';
 | 44 | $VERSION = '1.1';
 | 
          
            | 40 | $MAXDOWNLOAD = 5000; // maximum number of e-mails to download at one time
 | 45 | $MAXDOWNLOAD = 5000; // maximum number of e-mails to download at one time
 | 
          
            | 41 | $CLI = isset( $_SERVER["TERM"]); // only set if we are not in a cron job
 | 46 | $CLI = isset( $_SERVER["TERM"]); // only set if we are not in a cron job
 | 
          
            | 42 |  
 | 47 |  
 | 
          
            | - |   | 48 | /*
 | 
          
            | - |   | 49 |  * we don't know where the configuration file will be, so we have a list
 | 
          
            | - |   | 50 |  * below (searchPaths) to look for it. It will load the first one it 
 | 
          
            | - |   | 51 |  * finds, then exit. THUS, you could have multiple configuration files
 | 
          
            | - |   | 52 |  * but only the first one in the list will be used
 | 
          
            | - |   | 53 |  */
 | 
          
            | 43 |  
 | 54 |  
 | 
          
            | - |   | 55 | $confFileName = "sysinfoRead.conf.yaml";
 | 
          
            | - |   | 56 | $searchPaths = array( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $_SERVER['SCRIPT_FILENAME'], getcwd() );
 | 
          
            | - |   | 57 | $configuration = array();
 | 
          
            | 44 |  
 | 58 |  
 | 
          
            | - |   | 59 |  
 | 
          
            | - |   | 60 | foreach ( $searchPaths as $path ) {
 | 
          
            | - |   | 61 |    if ( file_exists( "$path/$confFileName" ) ) {
 | 
          
            | - |   | 62 |       print "Found $path/$confFileName\n";
 | 
          
            | - |   | 63 |       $configuration = yaml_parse_file( "$path/$confFileName" );
 | 
          
            | 45 | require "sysinfoRead.php.conf";
 | 64 |       #require "$path/$confFileName";
 | 
          
            | - |   | 65 |       break; // exit out of the loop; we don't try to load it more than once
 | 
          
            | - |   | 66 |    } // if
 | 
          
            | - |   | 67 | } // foreach
 | 
          
            | - |   | 68 |  
 | 
          
            | 46 |  
 | 69 |  
 | 
          
            | - |   | 70 | /* 
 | 
          
            | - |   | 71 |  * function    OpenMailbox
 | 
          
            | - |   | 72 |  * Parameters: $username
 | 
          
            | - |   | 73 |  *             $password
 | 
          
            | - |   | 74 |  *             $server
 | 
          
            | - |   | 75 |  *             $ssl=true
 | 
          
            | - |   | 76 |  *             $port=null
 | 
          
            | - |   | 77 |  *             $mailbox='INBOX'
 | 
          
            | - |   | 78 |  * 
 | 
          
            | - |   | 79 |  * Returns:    The IMAP instance handle
 | 
          
            | - |   | 80 |  *             The server string (used by some functions)
 | 
          
            | - |   | 81 |  */
 | 
          
            | 47 | function OpenMailbox ( $username, $password, $server, $ssl = true, $port=null, $mailbox = 'INBOX' ) {
 | 82 | function OpenMailbox ( $username, $password, $server, $ssl = true, $port=null, $mailbox = 'INBOX' ) {
 | 
          
            | 48 |    if ( $port == null )
 | 83 |    if ( $port == null )
 | 
          
            | 49 |       if ( $ssl ) $port = 993;  else $port = 143;
 | 84 |       if ( $ssl ) $port = 993;  else $port = 143;
 | 
          
            | 50 |    $server = gethostbyname( $server );
 | 85 |    $server = gethostbyname( $server );
 | 
          
            | 51 |    $serverString = "$server:$port/imap";
 | 86 |    $serverString = "$server:$port/imap";
 | 
          
            | Line 55... | Line 90... | 
          
            | 55 |    $server = imap_open( $serverString, $username, $password );
 | 90 |    $server = imap_open( $serverString, $username, $password );
 | 
          
            | 56 |    
 | 91 |    
 | 
          
            | 57 |    return array($server, $serverString);
 | 92 |    return array($server, $serverString);
 | 
          
            | 58 | }
 | 93 | }
 | 
          
            | 59 |  
 | 94 |  
 | 
          
            | - |   | 95 | /*
 | 
          
            | - |   | 96 |  * function  getContents
 | 
          
            | - |   | 97 |  * Paramters $mail - handle to the connection
 | 
          
            | - |   | 98 |  *           $n    - the # of the message to get
 | 
          
            | - |   | 99 |  * Returns   The type of report (yaml,xml,ini)
 | 
          
            | - |   | 100 |  *           The contents of the above
 | 
          
            | - |   | 101 |  * 
 | 
          
            | - |   | 102 |  * See getBody() below
 | 
          
            | - |   | 103 |  * 
 | 
          
            | - |   | 104 |  * At the very end, calls getBody, which evaluates the type of file
 | 
          
            | - |   | 105 |  * for a valid yaml/xml/ini file
 | 
          
            | - |   | 106 |  */
 | 
          
            | 60 | function getContents ($mail, $n ) {
 | 107 | function getContents ($mail, $n ) {
 | 
          
            | 61 |    $body = '';
 | 108 |    $body = '';
 | 
          
            | 62 |    $st = imap_fetchstructure($mail, $n);
 | 109 |    $st = imap_fetchstructure($mail, $n);
 | 
          
            | 63 |    if (!empty($st->parts)) {
 | 110 |    if (!empty($st->parts)) {
 | 
          
            | 64 |        for ($i = 0, $j = count($st->parts); $i < $j; $i++) {
 | 111 |        for ($i = 0, $j = count($st->parts); $i < $j; $i++) {
 | 
          
            | Line 71... | Line 118... | 
          
            | 71 |        $body = imap_body($mail, $n);
 | 118 |        $body = imap_body($mail, $n);
 | 
          
            | 72 |    }
 | 119 |    }
 | 
          
            | 73 |    return getBody( $body );
 | 120 |    return getBody( $body );
 | 
          
            | 74 | }
 | 121 | }
 | 
          
            | 75 |  
 | 122 |  
 | 
          
            | - |   | 123 | /*
 | 
          
            | - |   | 124 |  * function  getBody
 | 
          
            | - |   | 125 |  * Parameter $body
 | 
          
            | - |   | 126 |  * Returns   type of report (yaml, ini, xml)
 | 
          
            | - |   | 127 |  *           contents of the report
 | 
          
            | - |   | 128 |  * Returns (false,'') if nothing found
 | 
          
            | - |   | 129 |  * 
 | 
          
            | - |   | 130 |  * Removes all cruft from the message, leaving only a yaml, xml
 | 
          
            | - |   | 131 |  * or ini. For example, anything before --- in a yaml file is remove
 | 
          
            | - |   | 132 |  * as is everything after ..., and in this case, the first return
 | 
          
            | - |   | 133 |  * would be the string 'yaml' and the second the actual yaml document
 | 
          
            | - |   | 134 |  */
 | 
          
            | 76 | function getBody ( $body ) {
 | 135 | function getBody ( $body ) {
 | 
          
            | 77 |    global $bodyContents;
 | 136 |    global $configuration;
 | 
          
            | 78 |    foreach ( $bodyContents as $key => $regexes ) {
 | 137 |    foreach ( $configuration['bodyContents'] as $key => $regexes ) {
 | 
          
            | 79 |       $matches;
 | 138 |       $matches;
 | 
          
            | 80 |       $pattern = $regexes['startTag'] . '.*' . $regexes['endTag'];
 | 139 |       $pattern = $regexes['startTag'] . '.*' . $regexes['endTag'];
 | 
          
            | 81 |       if ( preg_match( "/$pattern/ms", $body, $matches ) ) 
 | 140 |       if ( preg_match( "/$pattern/ms", $body, $matches ) ) 
 | 
          
            | 82 |          return array( $key, $matches[0] );
 | 141 |          return array( $key, $matches[0] );
 | 
          
            | 83 |    }
 | 142 |    }
 | 
          
            | 84 |    return array( false, '' );
 | 143 |    return array( false, '' );
 | 
          
            | 85 | }
 | 144 | }
 | 
          
            | 86 |  
 | 145 |  
 | 
          
            | 87 |  
 | 146 |  
 | 
          
            | - |   | 147 | /* function makeFileName
 | 
          
            | - |   | 148 |  * Parameter $data
 | 
          
            | - |   | 149 |  *           $type - the type of the file
 | 
          
            | - |   | 150 |  * Returns   properly formatted file name for saving
 | 
          
            | - |   | 151 |  * 
 | 
          
            | - |   | 152 |  * The file name is created as a combination of the report date
 | 
          
            | - |   | 153 |  * client name, hostname and serial number, separated by the underscore
 | 
          
            | - |   | 154 |  * character. The type of the file is appended, and the configuration
 | 
          
            | - |   | 155 |  * value for outpath is prepended
 | 
          
            | - |   | 156 |  */
 | 
          
            | 88 | function makeFileName ( $data, $type ) {
 | 157 | function makeFileName ( $data, $type ) {
 | 
          
            | 89 |    global $outpath;
 | 158 |    global $configuration;
 | 
          
            | - |   | 159 |    $outpath = $configuration['outpath'];
 | 
          
            | 90 |    // we will create filename as yyyy-mm-dd_HH:mm:ss_client_hostname_serial
 | 160 |    // we will create filename as yyyy-mm-dd_HH:mm:ss_client_hostname_serial
 | 
          
            | 91 |    $date =  empty( $data['report']['date'] ) ? '' : strtotime( $data['report']['date'] ); // store in Unix timestamp
 | 161 |    $date =  empty( $data['report']['date'] ) ? '' : strtotime( $data['report']['date'] ); // store in Unix timestamp
 | 
          
            | 92 |    $date = strftime( '%F_%T', $date ); // save a copy of the date in SQL format
 | 162 |    $date = strftime( '%F_%T', $date ); // save a copy of the date in SQL format
 | 
          
            | 93 |  
 | 163 |  
 | 
          
            | 94 |    // add client name
 | 164 |    // add client name
 | 
          
            | Line 101... | Line 171... | 
          
            | 101 |    $serial = empty ( $data['system']['serial'] ) ? '00000' : $data['system']['serial'];
 | 171 |    $serial = empty ( $data['system']['serial'] ) ? '00000' : $data['system']['serial'];
 | 
          
            | 102 |    return ( $date && $client && $hostname && $serial ) ? "$outpath/$date" . '_' . $client . '_' . $hostname . '_' . "$serial.$type" : false;
 | 172 |    return ( $date && $client && $hostname && $serial ) ? "$outpath/$date" . '_' . $client . '_' . $hostname . '_' . "$serial.$type" : false;
 | 
          
            | 103 |    
 | 173 |    
 | 
          
            | 104 | } 
 | 174 | } 
 | 
          
            | 105 |  
 | 175 |  
 | 
          
            | - |   | 176 | /*
 | 
          
            | - |   | 177 |  * function saveFile
 | 
          
            | - |   | 178 |  * parameter $filename
 | 
          
            | - |   | 179 |  *           $contents
 | 
          
            | - |   | 180 |  *           $timestamp
 | 
          
            | - |   | 181 |  * returns   Number of bytes written
 | 
          
            | - |   | 182 |  * 
 | 
          
            | - |   | 183 |  * Saves $contents to file $filename, then sets the timestamp to
 | 
          
            | - |   | 184 |  * $timestamp. It is assumed $filename was created by makeFileName
 | 
          
            | - |   | 185 |  * and $contents is the content of a report. $timestamp is the actual
 | 
          
            | - |   | 186 |  * report timestamp
 | 
          
            | - |   | 187 |  */
 | 
          
            | 106 | function saveFile( $filename, $contents, $timestamp ) {
 | 188 | function saveFile( $filename, $contents, $timestamp ) {
 | 
          
            | 107 |    $bytesWritten = file_put_contents( $filename, $contents );
 | 189 |    $bytesWritten = file_put_contents( $filename, $contents );
 | 
          
            | 108 |    if ( $bytesWritten ) {
 | 190 |    if ( $bytesWritten ) {
 | 
          
            | 109 |       if ( ! touch( $filename, $timestamp ) ) logError( "could not set timestamp of $filename to $timestamp" );
 | 191 |       if ( ! touch( $filename, $timestamp ) ) logError( "could not set timestamp of $filename to $timestamp" );
 | 
          
            | 110 |    }
 | 192 |    }
 | 
          
            | 111 |    return $bytesWritten;
 | 193 |    return $bytesWritten;
 | 
          
            | 112 | }
 | 194 | }
 | 
          
            | 113 |  
 | 195 |  
 | 
          
            | 114 | function logError ( $message ) {
 | 196 | function logError ( $message ) {
 | 
          
            | 115 |    global $logFile;
 | 197 |    global $configuration;
 | 
          
            | 116 |    error_log( "$message\n", 3, $logFile );
 | 198 |    error_log( "$message\n", 3, $configuration['logFile'] );
 | 
          
            | 117 | }
 | 199 | }
 | 
          
            | 118 |  
 | 200 |  
 | 
          
            | 119 | /**************************************************************************************************************
 | 201 | /**************************************************************************************************************
 | 
          
            | 120 |  * Begin Main Program
 | 202 |  * Begin Main Program
 | 
          
            | 121 |  **************************************************************************************************************/
 | 203 |  **************************************************************************************************************/
 | 
          
            | 122 |  
 | 204 |  
 | 
          
            | 123 |  
 | 205 |  
 | 
          
            | 124 | // ensure the path we want to write to exists
 | 206 | // ensure the path we want to write to exists
 | 
          
            | 125 | if ( ! is_dir( $outpath ) ) {
 | 207 | if ( ! is_dir( $configuration['outpath'] ) ) {
 | 
          
            | 126 |    if ( ! mkdir( $outpath, 0777, true ) ) {
 | 208 |    if ( ! mkdir( $configuration['outpath'], 0777, true ) ) {
 | 
          
            | 127 |       die( "Could not create path $outpath\n" );
 | 209 |       die( "Could not create path " . $configuration['outpath'] . "\n" );
 | 
          
            | 128 |    }
 | 210 |    }
 | 
          
            | 129 | }
 | 211 | }
 | 
          
            | 130 |  
 | 212 |  
 | 
          
            | 131 | $listMailboxes =  isset( $argv[1] ) ; // anything passed on cli results in a list of mailboxes instead of the job
 | 213 | $listMailboxes =  isset( $argv[1] ) ; // anything passed on cli results in a list of mailboxes instead of the job
 | 
          
            | 132 |  
 | 214 |  
 | 
          
            | 133 | foreach ( $servers as $thisServer ) {
 | 215 | foreach ( $configuration['servers'] as $thisServer ) {
 | 
          
            | 134 |    if ($MAXDOWNLOAD <= 0 ) break;
 | 216 |    if ($MAXDOWNLOAD <= 0 ) break;
 | 
          
            | 135 |    if ( ! $thisServer['enabled'] ) continue; // ignore anything that is not enabled
 | 217 |    if ( ! $thisServer['enabled'] ) continue; // ignore anything that is not enabled
 | 
          
            | 136 |    print "Working on " . $thisServer['servername'] . "\n";
 | 218 |    print "Working on " . $thisServer['servername'] . "\n";
 | 
          
            | 137 |    $messagesToDelete = array(); // trap the UID's of messages to delete, more accurate than using message number
 | 219 |    $messagesToDelete = array(); // trap the UID's of messages to delete, more accurate than using message number
 | 
          
            | 138 |    print "Opening " . $thisServer['servername'] . "\n";
 | 220 |    print "Opening " . $thisServer['servername'] . "\n";
 | 
          
            | Line 147... | Line 229... | 
          
            | 147 |                                                 );
 | 229 |                                                 );
 | 
          
            | 148 |    print "Success with $connectString\n";
 | 230 |    print "Success with $connectString\n";
 | 
          
            | 149 |    if ( $listMailboxes ) {
 | 231 |    if ( $listMailboxes ) {
 | 
          
            | 150 |       $list = imap_list($server, $connectString, "*");
 | 232 |       $list = imap_list($server, $connectString, "*");
 | 
          
            | 151 |       print_r( $list ); print "\n";
 | 233 |       print_r( $list ); print "\n";
 | 
          
            | 152 |       continue;
 | - |   | 
          
            | 153 |    }
 | 234 |    }
 | 
          
            | 154 |    $count = 0;
 | 235 |    $count = 0;
 | 
          
            | 155 |    if ( $server ) {
 | 236 |    if ( $server ) {
 | 
          
            | 156 |       $folderCount = imap_num_msg( $server );
 | 237 |       $folderCount = imap_num_msg( $server );
 | 
          
            | 157 |       for ( $num = 1; ($num <= $folderCount) && $MAXDOWNLOAD; $num++ ) {
 | 238 |       for ( $num = 1; ($num <= $folderCount) && $MAXDOWNLOAD; $num++ ) {
 | 
          
            | 158 |          list( $type,$body ) = getContents( $server, $num );
 | 239 |          list( $type,$body ) = getContents( $server, $num );
 | 
          
            | 159 |          if ( ! $type ) {
 | 240 |          if ( ! $type ) {
 | 
          
            | 160 |             logError( "Unknown File Type" );
 | 241 |             logError( "Unknown File Type" );
 | 
          
            | 161 |             continue;
 | 242 |             continue;
 | 
          
            | 162 |          }
 | 243 |          }
 | 
          
            | 163 |          if ( ! ($data = $bodyContents[$type]['eval']($body) ) ) {
 | 244 |          $data = eval( $configuration['bodyContents'][$type]['eval'] );
 | 
          
            | - |   | 245 |          if ( ! $data ) {
 | 
          
            | 164 |             logError( "Unable to parse file" );
 | 246 |             logError( "Unable to parse file" );
 | 
          
            | 165 |             continue;
 | 247 |             continue;
 | 
          
            | 166 |          }
 | 248 |          }
 | 
          
            | 167 |          if ( !(  $filename  = makeFileName( $data, $type ) ) ) {
 | 249 |          if ( !(  $filename  = makeFileName( $data, $type ) ) ) {
 | 
          
            | 168 |             logError( "unable to create a file name" );
 | 250 |             logError( "unable to create a file name" );
 |