Skip to content

Instantly share code, notes, and snippets.

@rsky
Created June 25, 2010 15:49
Show Gist options
  • Select an option

  • Save rsky/453033 to your computer and use it in GitHub Desktop.

Select an option

Save rsky/453033 to your computer and use it in GitHub Desktop.
Safari等でダウンロードしたファイルのメタデータからダウンロード元URLを取得する関数
<?php
function get_item_where_froms($filename)
{
$command = '/usr/bin/xattr -p "com.apple.metadata:kMDItemWhereFroms" '
. escapeshellarg($filename) . ' 2>&1 | sed "s/ //g"';
$line = exec($command, $output, $retval);
if ($retval === 0) {
$len = hexdec(substr($line, -2));
$str = '';
foreach ($output as $line) {
$str .= pack('H*', $line);
}
$str = substr($str, 0, $len);
if (preg_match('!(https?://[\\x21-\\x7E]+$)!', $str, $matches)) {
return $matches[1];
} else {
// todo: raise error
return false;
}
} else {
// todo: raise error
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment