<?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.

if (isset($_GET['xml'])) {
    
header("Content-type: application/xml");
}
else {
require_once(
"header.php"); // NOT REQUIRED FOR FUNCTIONALITY
}
require_once(
"itms_twofish.php");

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

if (isset(
$_GET['url']))
{
    
$url $_GET['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 '';
}

if (!isset(
$_GET['xml']))
{
?>
    <form id="form" name="form" action="<?=$_SERVER['PHP_SELF']?>" method="get">
<!-- I took this out because it'd be way too abusable
        iTMS Server:
        <input type="text" name="server" id="server" size=50 maxlength=200 value="<?=$server?>"><br>
-->
        iTMS URL:
        <input type="text" name="url" id="url" size=100 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));
}

if (isset(
$_GET['xml']))
{
    echo 
$payload;
    exit;
}

echo 
'<pre>'.$headers.'</pre>';
echo 
'<iframe src="./itunes-debug.php?server='.$server.'&url='.urlencode($oldurl).'&xml=1" width="100%" height="500"></iframe><br>';
echo 
'<a href="./itunes-debug.php?server='.$server.'&url='.urlencode($oldurl).'&xml=1">Link to XML</a><p>';
echo 
'<a href="./itunes-debug.phps">Source</a><br>';
echo 
'<a href="./itms_twofish.phps">itms_twofish.php</a>';
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>';

function 
error($error)
{
    if (!isset(
$_GET['xml']))
    {
        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>';
        exit;
    }
}

?>