<?php
function strip_data($key, $string) { 
  $beginning = '<key>'.$key.'</key>'; 
  $end = '</data>'; 
  $beginningPos = strpos($string, $beginning); 
  $tmpstring = substr($string, $beginningPos); 
  $endPos = strpos($tmpstring, $end); 
  if ($beginningPos === false || $endPos === false) { 
   return $string; 
  } 
  $textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) ); 
  file_put_contents('delete', $textToDelete); 
  return strip_data($key, str_replace($textToDelete, '', $string)); // recursion to ensure all occurrences are replaced 
 } 
  
 function strToHex($string){ 
  $hex = ''; 
  for ($i=0; $i<strlen($string); $i++){ 
   $ord = ord($string[$i]); 
   $hexCode = dechex($ord); 
   $hex .= substr('0'.$hexCode, -2); 
  } 
  return strToUpper($hex); 
 } 
 
 function hexToStr($hex){ 
  $string=''; 
  for ($i=0; $i < strlen($hex)-1; $i+=2){ 
   $string .= chr(hexdec($hex[$i].$hex[$i+1])); 
  } 
  return $string; 
 } 
 
 
  
 $activation = $_POST['activation-info']; 
 $ticketID = $_POST['activation-info']; 
 $encodedrequest = new DOMDocument; 
 $encodedrequest->loadXML($activation); 
 
 $activationDecoded= base64_decode($encodedrequest->getElementsByTagName('data')->item(0)->nodeValue); 
 
 $decodedrequest = new DOMDocument; 
 $decodedrequest->loadXML($activationDecoded); 
 $nodes = $decodedrequest->getElementsByTagName('dict')->item(0)->getElementsByTagName('*'); 
      
    file_put_contents('info', $activation); 
 for ($i = 0; $i < $nodes->length - 1; $i=$i+2) 
 { 
   ${$nodes->item($i)->nodeValue} = preg_match('/(true|false)/', $nodes->item($i + 1)->nodeName) ? $nodes->item($i + 1)->nodeName : $nodes->item($i + 1)->nodeValue; 
     switch ($nodes->item($i)->nodeValue) 
     { 
        case "ActivationState": $activationState = $nodes->item($i + 1)->nodeValue; break; 
        case "ActivationRandomness": $activationRandomness = $nodes->item($i + 1)->nodeValue; break; 
        case "DeviceCertRequest": $deviceCertRequest = $nodes->item($i + 1)->nodeValue; break; 
        case "DeviceClass": $deviceClass = $nodes->item($i + 1)->nodeValue; break; 
        case "BasebandChipID": $BasebandChipID = $nodes->item($i + 1)->nodeValue; break; 
        case "InternationalMobileEquipmentIdentity": $imei = $nodes->item($i + 1)->nodeValue; break; 
        case "MobileEquipmentIdentifier": $meid = $nodes->item($i + 1)->nodeValue; break; 
        case "ProductType": $productType = $nodes->item($i + 1)->nodeValue; break; 
        case "ProductVersion": $productVersion = $nodes->item($i + 1)->nodeValue; break; 
        case "OSType": $OSType = $nodes->item($i + 1)->nodeValue; break; 
        case "WifiAddress": $wifi = $nodes->item($i + 1)->nodeValue; break; 
        case "UniqueChipID": $ecid = $nodes->item($i + 1)->nodeValue; break; 
        case "ChipID": $chipID = $nodes->item($i + 1)->nodeValue; break; 
        case "BluetoothAddress": $BluetoothAddress = $nodes->item($i + 1)->nodeValue; break; 
        case "UniqueDeviceID": $uniqueDeviceID = $nodes->item($i + 1)->nodeValue; break; 
        case "SerialNumber": $serialNumber = $nodes->item($i + 1)->nodeValue; break; 
        case "BasebandMasterKeyHash": $BasebandMasterKeyHash = $nodes->item($i + 1)->nodeValue; break; 
        case "BasebandSerialNumber": $BasebandSerialNumber = $nodes->item($i + 1)->nodeValue; break; 
        case "BasebandChipID": $BasebandChipID = $nodes->item($i + 1)->nodeValue; break; 
        case "RegulatoryModelNumber": $RegulatoryModelNumber = $nodes->item($i + 1)->nodeValue; break; 
        case "ModelNumber": $ModelNumber = $nodes->item($i + 1)->nodeValue; break; 
        case "BuildVersion": $BuildVersion = $nodes->item($i + 1)->nodeValue; break; 
        case "mac_fg": $mac_fg = $nodes->item($i + 1)->nodeValue; break; 
        case "SIMStatus": $SIMStatus = $nodes->item($i + 1)->nodeValue; break; 
        case "UIKCertification": $UIKCertification = $nodes->item($i + 1)->nodeValue; break; 
        case "InternationalMobileSubscriberIdentity": $imsi = $nodes->item($i + 1)->nodeValue; break;
        case "IntegratedCircuitCardIdentity": $iccid = $nodes->item($i + 1)->nodeValue; break; 
  } 
 } 
error_reporting(0); 
$ucid = $ecid; 
$udid = $uniqueDeviceID; 
$xsn = $serialNumber;
$Decoded = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
<plist version=\"1.0\"> 
<dict> 
    <key>ActivationRandomness</key> 
    <string>F597381A-4F7A-4639-8C50-CB134326DC1D</string> 
    <key>ActivationState</key> 
    <string>Unactivated</string> 
    <key>BasebandActivationTicketVersion</key> 
    <string>V2</string> 
    <key>BasebandChipID</key> 
    <integer>7278817</integer> 
    <key>BasebandMasterKeyHash</key> 
    <string>8CB15EE4C8002199070D9500BB8FB183B02713A5CA2A6B92DB5E75CE15536182</string> 
    <key>BasebandSerialNumber</key> 
    <data>0xEMaVABEqT5emWk</data> 
    <key>BuildVersion</key> 
    <string>13G36</string> 
    <key>DeviceCertRequest</key> 
 <data> 
 LS0tLS1CRUdJTiBDRVJUSUZJQ0FURSBSRVFVRVNULS0tLS0KTUlJQnhEQ0NBUzBDQVFB 
 d2dZTXhMVEFyQmdOVkJBTVRKRVl3TUVSRVEwSTJMVE0yTkRZdE5FVTJReTA1TkRsRg0K 
 TFVSRFFrWTVRVEU1T1VZeVFqRUxNQWtHQTFVRUJoTUNWVk14Q3pBSkJnTlZCQWdUQWtO 
 Qk1SSXdFQVlEVlFRSA0KRXdsRGRYQmxjblJwYm04eEV6QVJCZ05WQkFvVENrRndjR3hs 
 SUVsdVl5NHhEekFOQmdOVkJBc1RCbWxRYUc5dQ0KWlRDQm56QU5CZ2txaGtpRzl3MEJB 
 UUVGQUFPQmpRQXdnWWtDZ1lFQXVYb1FFS2tXTGIrQUk1clBUb1N5dC9HYw0Ka1B0Y0lO 
 a3NCUGtETEZCOWthOHhyQnZWOENmUTZaOGE4aW9MLzdUM0Y5Nnkyc3Z3dnM5Z2RFRXRF 
 TXAyU1BkMw0KM0lwMUlKcG9INjNGbE9ySFZpTWg0N0grV0ttMk5BZlU3dW1rNWFveHY5 
 aG9lYVRMMmphTDZGbkFudGIxY2pxQw0KS3RRQkE0eXFMTG1SV1hnM2JkTUNBd0VBQWFB 
 QU1BMEdDU3FHU0liM0RRRUJCUVVBQTRHQkFBRC9senFkMG1sRw0KU3VYdnYxNGg4M3N2 
 MFYyc0Q0a2RoU0JmOFVsOXlkU29YcVVrY0l4eXpya2taay9ZUGtmamFHOFhGVmtCR1ky 
 eg0KTDgrRnhwMUpaTnFBQ1k5ZWpKQWp4ZmZkYTBHcSsxTGVQdVJLU2lucVVXblFlTWtl 
 bVVkQ2duOVBpcSs5UGh1eA0KT2ZuemRVbGhETVA1VFVSZWJrMVZMRjdkd0d2UjZtTFoK 
 LS0tLS1FTkQgQ0VSVElGSUNBVEUgUkVRVUVTVC0tLS0tCg== 
 </data> 
    <key>DeviceClass</key> 
    <string>iPad</string> 
    <key>DeviceVariant</key> 
    <string>A</string> 
    <key>FMiPAccountExists</key> 
    <false/> 
    <key>ModelNumber</key> 
    <string>MC979</string> 
    <key>OSType</key> 
    <string>iPhone OS</string> 
    <key>ProductType</key> 
    <string>iPad2,1</string> 
    <key>ProductVersion</key> 
    <string>9.3.5</string> 
 <key>InternationalMobileSubscriberIdentity</key> 
 <string></string> 
 <key>IntegratedCircuitCardIdentity</key> 
 <string></string> 
 <key>RegionCode</key> 
 <string>E</string> 
 <key>RegionInfo</key> 
 <string>E/A</string> 
    <key>SerialNumber</key> 
    <string>DMPGNT84DKPH</string> 
    <key>UniqueChipID</key> 
    <integer>".$ucid."</integer> 
    <key>UniqueDeviceID</key> 
    <string>".$udid."</string> 
</dict> 
</plist>"; 
$Key = '-----BEGIN RSA PRIVATE KEY----- 
MIICWwIBAAKBgQC3BKrLPIBabhpr+4SvuQHnbF0ssqRIQ67/1bTfArVuUF6p9sdc 
v70N+r8yFxesDmpTmKitLP06szKNAO1k5JVk9/P1ejz08BMe9eAb4juAhVWdfAIy 
aJ7sGFjeSL015mAvrxTFcOM10F/qSlARBiccxHjPXtuWVr0fLGrhM+/AMQIDAQAB 
AoGACGW3bHHPNdb9cVzt/p4Pf03SjJ15ujMY0XY9wUm/h1s6rLO8+/10MDMEGMlE 
dcmHiWRkwOVijRHxzNRxEAMI87AruofhjddbNVLt6ppW2nLCK7cEDQJFahTW9GQF 
zpVRQXXfxr4cs1X3kutlB6uY2VGltxQFYsj5djv7D+A72A0CQQDZj1RGdxbeOo4X 
zxfA6n42GpZavTlM3QzGFoBJgCqqVu1JQOzooAMRT+NPfgoE8+usIVVB4Io0bCUT 
WLpkEytTAkEA11rzIpGIhFkPtNc/33fvBFgwUbsjTs1V5G6z5ly/XnG9ENfLblgE 
obLmSmz3irvBRWADiwUx5zY6FN/Dmti56wJAdiScakufcnyvzwQZ7Rwp/61+erYJ 
GNFtb2Cmt8NO6AOehcopHMZQBCWy1ecm/7uJ/oZ3avfJdWBI3fGv/kpemwJAGMXy 
oDBjpu3j26bDRz6xtSs767r+VctTLSL6+O4EaaXl3PEmCrx/U+aTjU45r7Dni8Z+ 
wdhIJFPdnJcdFkwGHwJAPQ+wVqRjc4h3Hwu8I6llk9whpK9O70FLo1FMVdaytElM 
yqzQ2/05fMb7F6yaWhu+Q2GGXvdlURiA3tY0CsfM0w== 
-----END RSA PRIVATE KEY-----'; 
$pkeyid = openssl_pkey_get_private($Key); 
openssl_sign($Decoded, $signature, $pkeyid, 'sha1WithRSAEncryption'); 
openssl_free_key($pkeyid); 
$ActivationInfoXMLSignature = base64_encode($signature); 
$ActivationInfoXML64 = base64_encode($Decoded); 
$FINAL = '<dict> 
 <key>ActivationInfoComplete</key> 
 <true/> 
 <key>ActivationInfoXML</key> 
 <data>'.$ActivationInfoXML64.'</data> 
 <key>FairPlayCertChain</key> 
 <data>
   
 </data> 
 <key>FairPlaySignature</key> 
 <data> 
 '.$ActivationInfoXMLSignature.' 
 </data> 
</dict>'; 
 
$url = "https://albert.apple.com/deviceservices/deviceActivation"; 
 
$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
 
$headers = array( 
   "Content-Type: application/x-www-form-urlencoded", 
); 
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
 
$data = "activation-info=".urlencode($FINAL); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
 
//for debug only! 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
 
$resp = curl_exec($curl); 
curl_close($curl); 
$ac = str_replace('<key>device-activation</key><dict><key>ack-received</key><true/><key>activation-record</key><dict>', '', $resp); 
$rec = str_replace('</dict><key>show-settings</key><true/></dict>', '', $ac); 
$Folder = "devices/".$xsn."/"; 
if(!file_exists($Folder)) 
 mkdir($Folder, 00777, true); 
file_put_contents($Folder."/activation_record.plist", $rec); 
$Fair = file_get_contents($Folder."/activation_record.plist"); 
$_pre = explode('<key>FairPlayKeyData</key>', $Fair)[1]; 
$_icin = explode('<data>', $_pre)[1]; 
$_icinf = explode('</data>', $_icin)[0]; 
$_decode = base64_decode($_icinf); 
$_deletekeys = str_replace('-----BEGIN CONTAINER-----', '', $_decode); 
$_deletekey = str_replace('-----END CONTAINER-----', '', $_deletekeys); 
$_IC_INFO = base64_decode($_deletekey); 
file_put_contents($Folder."/IC-Info.sisv", $_IC_INFO); 
header('content-type: application/xml'); 
echo $rec; 
?>