$RANGE_BEGIN AND date < $RANGE_END"; } elseif( !empty( $RANGE_BEGIN ) ) { $range = "WHERE date > $RANGE_BEGIN"; } elseif( !empty( $RANGE_END ) ) { $range = "WHERE date < $RANGE_END"; } else { $range = ""; } // query for SMS info $result = $db->query("SELECT address,date,text,flags FROM message $range ORDER BY date ASC"); if( !$result ) { $error_array = $db->errorInfo(); error_return( "Cannot read from database ($DATABASE): ".$error_array[2] ); } $sms_pool = $result->fetchAll(); // deal with single SMS foreach( $sms_pool as $single ) { // grep relevant information from array $text = $single['text']; $timestamp = $single['date']; $address = $single['address']; $flags = $single['flags']; // format timestamp human readable $time = date( $TIME_FORMAT, $timestamp); /* flags aren't really commented by Apple, but this is * what I found out myself. * (2: received, 3: sent, 33: failed to send, * 35: failed to send with retry) */ if( $FILTER == "ALL" || $FILTER == "IN" && $flags == 2 || $FILTER == "OUT" && ( $flags == 3 || $flags == 33 || $flags == 35 ) ) { if( $flags == 33 ) { $senderror = " (never sent)"; } elseif( $flags == 35 ) { $senderror = " (never sent, with retry)"; } else { $senderror = ""; } if( $OUTFILE_TYPE == "TXT" ) { $OUTSTRING .= "$time$senderror$NEWLINE$address$NEWLINE$text$NEWLINE$NEWLINE$NEWLINE"; } elseif( $OUTFILE_TYPE == "CSV" ) { // surround text with " and replace " with "" (according to RFC 4180) $OUTSTRING .= "$address,$time,\"" . str_replace( "\"", "\"\"", $text) . "\"$NEWLINE"; } $sms_counter++; } } $OUTSTRING .= "$NEWLINE-------$NEWLINE"."exported a total of $sms_counter SMS.$NEWLINE"; /* -----------------------------| */ /* |-------------- */ /* OUTPUT SEQUENCE */ // open file $f_handle = fopen( $OUTFILE, "a" ); fwrite( $f_handle, $OUTSTRING ); // close file fclose( $f_handle ); /* --------------| */ ?>