Skip to content

Instantly share code, notes, and snippets.

@tylerhall
Created August 19, 2010 20:58
Show Gist options
  • Save tylerhall/538898 to your computer and use it in GitHub Desktop.
Save tylerhall/538898 to your computer and use it in GitHub Desktop.
Converts your Safari cookies into the standard Netscape (Firefox) format.
#!/usr/bin/php
<?PHP
$plist = file_get_contents($_ENV['HOME'] . '/Library/Cookies/Cookies.plist');
preg_match_all('/<dict>(.*?)<\/dict>/ms', $plist, $matches);
$dicts = $matches[1];
$fp = fopen('cookies.txt', 'w');
foreach($dicts as $dict)
{
$domain = match('/Domain<\/key>.*?<string>(.*?)<\/string>/ms', $dict, 1);
$expires = match('/Expires<\/key>.*?<date>(.*?)<\/date>/ms', $dict, 1);
$name = match('/Name<\/key>.*?<string>(.*?)<\/string>/ms', $dict, 1);
$path = match('/Path<\/key>.*?<string>(.*?)<\/string>/ms', $dict, 1);
$value = match('/Value<\/key>.*?<string>(.*?)<\/string>/ms', $dict, 1);
$domain = trim($domain, '.');
$expires = strtotime($expires);
fwrite($fp, "$domain\tFALSE\t$path\tFALSE\t$expires\t$name\t$value\n");
}
fclose($fp);
function match($regex, $str, $i = 0)
{
if(preg_match($regex, $str, $match) == 1)
return $match[$i];
else
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment