$NEWLINE$NEWLINE$NEWLINE"; } // create range string if( !empty( $RANGE_BEGIN ) && !empty( $RANGE_END ) ) { $range = "WHERE date > $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 = ""; } // determine if container is < iOS5, iOS5 or iOS6 $v5 = FALSE; $v6 = FALSE; $result = $db->query( "PRAGMA table_info('message')" ); if( !$result ) { $error_array = $db->errorInfo(); error_return( "Cannot read from database ($DATABASE): ".$error_array[2] ); } $table_columns = $result->fetchAll(); foreach( $table_columns as $column ) { $column_name = $column['name']; // any column that is present in v6 ONLY is ok here if( $column_name == "is_from_me" ) { $v6 = TRUE; break; } // any column that is present in v5 ONLY is ok here if( $column_name == "is_madrid" ) { $v5 = TRUE; break; } } // query for SMS info if( $v6 ) { $result = $db->query("SELECT date,text,handle_id,is_from_me,service FROM message $range ORDER BY ROWID ASC"); } else if( $v5 ) { $result = $db->query("SELECT address,date,text,flags,madrid_flags,madrid_handle FROM message $range ORDER BY ROWID ASC"); } else { $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 ) { $iMessage = FALSE; $type = "SMS"; // grep relevant information from array $text = $single['text']; $timestamp = $single['date']; if( $v6 ) { $is_from_me = $single['is_from_me']; $v6_type = $single['service']; $handle_id = $single['handle_id']; // get comm partner $result_2 = $db->query("SELECT id FROM handle WHERE ROWID = $handle_id"); if( !$result_2 ) { $error_array = $db->errorInfo(); error_return( "Cannot read from database ($DATABASE): ".$error_array[2] ); } $address_array = $result_2->fetchAll(); $address = $address_array[0]['id']; } else if( $v5 ) { $madrid_flags = $single['madrid_flags']; $address = $single['address']; } else { $flags = $single['flags']; $address = $single['address']; } // determine if iMessage under iOS6 if( $v6 && ( $v6_type == "iMessage" ) ) { $iMessage = TRUE; } // format timestamp human readable if( $v6 ) { $time = date( $TIME_FORMAT, $timestamp + 978307200 ); } else if( $v5 && ($flags === '0') ) { // iMessage // thx to https://github.com/toffer/iphone-sms-backup/blob/master/sms-backup.py $time = date( $TIME_FORMAT, $timestamp + 978307200 ); $iMessage = TRUE; } else { $time = date( $TIME_FORMAT, $timestamp); } // set iMessage specific data if( $iMessage === TRUE ) { if( $v5 ) { $address = $single['madrid_handle']; } $type = "iMessage"; } /* 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) * madrid_flags: * ( 12289, 45061: received, 77825: received with email/url/etc, * 36869: sent, 102405: sent with email/url/etc ) */ if( $FILTER == "ALL" || $FILTER == "IN" && $flags == 2 || $FILTER == "OUT" && ( $flags == 3 || $flags == 33 || $flags == 35 ) || $iMessage && $FILTER == "IN" && ( $madrid_flags == 12289 || $madrid_flags == 45061 || $madrid_flags == 77825 ) || $iMessage && $FILTER == "OUT" && ( $madrid_flags == 36869 || $madrid_flags == 102405 ) || $v6 && $FILTER == "IN" && ( $is_from_me == 0 ) || $v6 && $FILTER == "OUT" && ( $is_from_me == 1 ) ) { 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"; } elseif( $OUTFILE_TYPE == "XML" ) { $OUTSTRING .= "\t$NEWLINE\t\t
$address
$NEWLINE\t\t$NEWLINE\t\t$text$NEWLINE\t
$NEWLINE"; } $sms_counter++; } } if( $OUTFILE_TYPE == "XML" ) { $OUTSTRING .= "
$NEWLINE"; } else { $OUTSTRING .= "$NEWLINE-------$NEWLINE"."exported a total of $sms_counter SMS.$NEWLINE"; } /* -----------------------------| */ /* |-------------- */ /* OUTPUT SEQUENCE */ // open file $f_handle = fopen( $OUTFILE, "w" ); fwrite( $f_handle, $OUTSTRING ); // close file fclose( $f_handle ); /* --------------| */ ?>