Created
June 25, 2010 15:49
-
-
Save rsky/453033 to your computer and use it in GitHub Desktop.
Safari等でダウンロードしたファイルのメタデータからダウンロード元URLを取得する関数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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