<?php
// WARNING: This script is for educational purposes only. Use of it in any form comes with your express knowledge that you are personally responsible for your actions.

require_once("header.php"); // NOT REQUIRED FOR FUNCTIONALITY
require_once("itms_twofish.php");

$server 'ax.phobos.apple.com.edgesuite.net';

if (isset(
$_POST['url']))
{
    
$url $_POST['url'];
    
$oldurl $url;

    
$parseoldurl parse_url($oldurl);
    
preg_match('/(&)?s=([0-9]*)?/i'$parseoldurl['query'], $matches);
    
$store $matches[2];

    
$url ereg_replace('&?s=[0-9]*&?'''$url);
    
$parseurl parse_url($url);
}
else
{
    
$error 'Copy the iTunes Music Store URL then paste above.';
    
$url '';
}

?>
<html>
<body>
<div align="center">
    <h3>iTunes Music Album Art</h3>
    <font size="2">[ <a href="./">Home</a> ][ <a href="./itunes-art.phps">Source</a> ]</font><p>
    <form id="form" name="form" action="<?=$_SERVER['PHP_SELF']?>" method="post">
        iTunes Album URL:
        <input type="text" name="url" id="url" size=50 maxlength=200 value="<?=$oldurl?>">
        <input type="submit" value="submit">
    </form><hr>
<?php

if (isset($error)) error($error);

if (!
$fp fsockopen($server80)) error("Couldn't connect to server.");
$headers "GET ".$parseurl['path']."?".$parseurl['query']." HTTP/1.1\r\n";
$headers .= "Host: {$server}\r\n";
$headers .= "User-Agent: iTunes/5.0 (Macintosh; U; PPC Mac OS X 10.4.2)\r\n";
$headers .= "Accept-Language: en-us, en;q=0.50\r\n";
$headers .= "X-Apple-Store-Front: {$store}\r\n";
$headers .= "Cookie: countryVerified=1\r\n";
$headers .= "Accept-Encoding: gzip, x-aes-cbc\r\n";
$headers .= "Connection: close\r\n\r\n";

if (!
fwrite($fp$headers)) error("Error sending request.");

$payload "";
while (!
feof($fp)) $payload .= fread($fp128);
fclose($fp);

preg_match("/Content-Length: (\d+)/"$payload$length);
$length $length[1];

preg_match("/Content-Encoding: (.+)\r\n/"$payload$encoding);
$encoding $encoding[1];

if (
preg_match("/x-aes-cbc/"$encoding))
{
    
preg_match("/x-apple-crypto-iv: (\w+)/"$payload$iv);
    if (
count($iv) < 2error("No x-apple-crypto-iv was found in the result.");
    
$iv pack('H*'$iv[1] );

    
preg_match("/x-apple-twofish-key: (\w+)/"$payload$twofish);
    if (
count($twofish) < 2)
        
error("No x-apple-twofish-key was found in the result.");
    
$twofish pack('H*'$twofish[1] );
    
$key descramble_twofish_key($twofish);
}

$payload substr($payload,strpos($payload,"\r\n\r\n"), strlen($payload));
$payload ltrim($payload"\r\n");

if (
strlen($payload) != $lengtherror("Payload is incorrect length.");

if (
preg_match("/x-aes-cbc/"$encoding))
{
    
$td mcrypt_module_open(MCRYPT_RIJNDAEL_128''MCRYPT_MODE_CBC'');
    if (
mcrypt_generic_init($td$key$iv) >= 0)
    {
        
$payload mdecrypt_generic($td$payload);
        
mcrypt_generic_deinit($td);
        
mcrypt_module_close($td);
    }
}

if (
preg_match("/gzip/"$encoding))
{
    
$payload gzinflate(substr($payload,10));
}

preg_match('/<PictureView.*url="(http:.*[0-9]*x[0-9]*-[0-9]*\.jpg)"/'$payload$matches);

if (
count($matches) < 2)
{
    echo 
'Got something unexpected:<br></div>';
    echo 
'<pre>'.htmlentities($payload).'</pre><div>';
}
else
{
    
$hires str_replace("170x170-99""600x600-100"$matches[1]);
    echo 
'<a href="'.$hires.'"><img src="'.$matches[1].'" border=0></a><br>';
    echo 
'<font size="2">[ <a href="'.$matches[1].'">Small</a> ][ <a href="'.$hires.'">Large</a> ]</font><p>';
}

        echo 
'<p><font size="1">Modified from <a href="http://www.s-seven.net/blog/">s-seven.net</a> by <a href="http://tpemble.urfbownd.net/itunes/">Tristan</a></font></div></body></html>';


function 
error($error)
{
    echo 
$error.'<p><font size="1">Modified from <a href="http://www.s-seven.net/blog/">s-seven.net</a> by <a href="http://tpemble.urfbownd.net/itunes/">Tristan</a></font></div></body></html>';
    exit;
}

?>