May 112011
 

UPDATE (10/06/2011): I started an online version of the script, see new blog post.

For a while I’ve been checking out various programs for extracting and converting text messages from the iPhone SMS storage SQLite database. For some reason they seem to be either for purchase or not doing their job well.
When I found out that all they are doing is pretty much just some simple SQL query and putting the result into various output formats, I decided to quickly write this myself, while keeping it simple and open.

Download

The choice of a programming language wasn’t easy taking into account that most iPhone users will be on Windows and the extractor should not require major alterations to the system (and doing that without the solution itself being bloated).
Even though the task can be easily done in various languages such as Perl, Python, PHP, C/C++ or Java, I decided to use PHP, because the program can stay very simple and can run with a minimal version of PHP. In addition many users with a Unix/Linux machine available will have PHP installed anyway and can also easily use the program.
That being said we can move on to the program details. The PHP script needs the iPhone SMS storage SQLite database file as input and will then convert all text messages and append them to a file in a certain format.
So far the script is configurable to a certain extent. Naturally the name of the input and output file can be modified. Furthermore you can decide whether you want to extract just incoming or outgoing texts or all of them. Last but not least a date range can be given, from which to extract the messages. See further down (or code) for details about customization.
At this point in time, it is not (yet) possible to limit extraction to a single contact. Even though it might be a common use case for many users, however the format of recipient/sender may vary (at least in my storage file), so it might be hard to determine which number formats are from the same contact.

The usage of the extractor is as follows:

  1. First you will need the SMS storage container file (it is usually named 3d0d7e5fb2ce288813306e4d4636395e047a3d28 and can be found in the iTunes backup folder, which can be found e.g. on Windows7 in C:\Users\[username]\AppData\Roaming\Apple Computer\MobileSync\Backup\[hexstring] or get it directly from your iPhone: /var/mobile/Library/SMS/sms.db )
  2. If you’re on Unix/Linux, make sure you have PHP5 (along with libs for sqlite) installed. If you’re on Windows, you can either use the official PHP installer [alternative: http://windows.php.net/download/] (just run the installer) or use my minimized packaged version of PHP (just unpack to a location of your desire)
  3. Download the SMS Extractor for iPhone (extract_sms.php_.txt), rename it to extract_sms.php and use a text editor of your desire to make configuration changes (see below and/or follow the instructions and explanations in the code)
  4. If you used the PHP installer, navigate to the folder containing extract_sms.php and double-click on it (make sure you placed the SMS storage container file in the same directory and modified the input configuration)
  5. If you used my minimized version, place extract_sms.php in the same folder you unpacked the PHP archive; use Start, Run…, “cmd.exe” and navigate to the correct folder / if you’re on Unix/Linux, navigate to the folder you downloaded extract_sms.php)
  6. (make sure you placed the SMS storage container file in the same directory and modified the input configuration) then run the script typing “php extract_sms.php”
  7. Unless you changed the output file, you will find a file “sms.txt” in the same folder

Possible customization can be done in the following ways (look for “user specific CONFIGURATION” in extract_sms.php ):

  • $DATABASE = “sms.db”
    change this to the appropriate filename if your SMS storage container file is named differently
  • $TIME_FORMAT = “d.m.Y H:i:s”
    the internal text message timestamp is the number of seconds since Unix Epoch, so in order to make this human readable, provide the time format of your desire (see http://www.php.net/manual/en/function.date.php for details)
  • $FILTER = “ALL”
    set here whether you would like to limit extraction to only incoming text messages (“IN“), or only outgoing messages (“OUT“) or no limit (“ALL“)
  • $OUTFILE = “./sms.txt”
    name for the output file, you can change this to any name with or without file extension
  • date_default_timezone_set(‘Europe/Berlin’)
    if you want to use a time range (see next item), you need to specify your timezone (which supposedly equals the timezone your text messages got timestamped). See http://www.php.net/manual/en/timezones.php for available timezones
  • $RANGE_BEGIN = mktime( 22, 45, 00, 11, 9, 2009 ) and/or $RANGE_END = mktime( 22, 55, 00, 11, 9, 2009 )
    if you want to use a time range, you can configure either a start or an end of range or even both. The order of comma separated digits in parentheses is: hour, min, sec, mon, day, year. Also, you need to uncomment (i.e. remove the leading //) the line of the variable(s) you want to use
  • Replace file instead of appending ( $f_handle = fopen( $OUTFILE, “a” ); )
    the default behavior is to open the output file in append mode in order to avoid accidental overwrite. If you would like replace the output file instead of appending the extracted text messages, put “w” instead of “a
  • Change output format ( fwrite( $f_handle, “$time$senderror\n$address\n$text\n\n\n” ); )
    if you would like to change the format in which the script is putting your extracted text message information, you will need to change this line appropriately. However, I cannot provide a manual how to change it to your liking at this moment; you will either know what you’re doing or have to google and trial ‘n error 😉

Please feel free to comment about the usage or drop me an email at blog {at} haraldkraft(.)de. It’s fine with me to continue development and add new (simple) features, and definitely fix bugs I might have there in PHP; just let me know :).

Also, please do use my script for your adaptions; I am releasing this under CC.
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

 Leave a Reply

(required)

(required)


(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">