$RANGE_BEGIN AND date < $RANGE_END"; } elseif( isset( $RANGE_BEGIN ) ) { $range = "WHERE date > $RANGE_BEGIN"; } elseif( isset( $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"); $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 = ""; } /* OUTPUT format * if your target system will be Windows, use \r\n * if your target system will be Unix/Linux, use \n * */ fwrite( $f_handle, "$time$senderror\r\n$address\r\n$text\r\n\r\n\r\n" ); //fwrite( $f_handle, "$address,$time,$text\r\n" ); // CSV $sms_counter++; } } fwrite( $f_handle, "\r\n-------\r\nexported a total of $sms_counter SMS.\r\n" ); // close file fclose( $f_handle ); ?>