$entityManager = initDoctrine();
$invitation = $entityManager->getRepository('Entities\\Invitation')->findOneBy(array('token' => $_GET['token']));
if ($invitation == NULL) {
    die('This invitation token is not valid!');
}
if (!isset($_GET['udid'])) {
    $action = 'ENROLL';
    $isNotRegistered = $invitation->getStatus() == Invitation::STATUS_SENT;
} else {
    $action = 'DOWNLOAD';
}
$version = $invitation->getVersion();
if ($action == 'ENROLL' && $isNotRegistered) {
    $mail = $invitation->getTester()->getEmail();
    header('Content-Type: application/x-apple-aspen-config');
    $payload = profile_service_payload('signed-auth-token', $_GET['token']);
    echo $payload;
    die;
}
//step3 check that this app is already signed for this udid
//if not sign it.
$isAppSigned = isVersionAppSignedForUdid($_GET['udid'], $version);
$isAppSigned = false;
//step4 provid link to dld app
generateDownloadPlistFile($version);
$appLink = Tools::rel2abs(UPLOAD_PATH . $version->getToken() . '.plist', Tools::current_url());
$profileLink = Tools::rel2abs(UPLOAD_PATH . $version->getToken() . '.mobileprovision', Tools::current_url());
?>
<html>
<head>
    $payload['PayloadUUID'] = uniqid();
    // must be unique
    //will be shown to the user.
    $payload['PayloadOrganization'] = "RunThisApp";
    return $payload;
}
function profile_service_payload($challenge)
{
    $payload = general_payload();
    $payload['PayloadType'] = "Profile Service";
    // do not modify
    $payload['PayloadIdentifier'] = "com.runthisapp.mobileconfig.profile-service";
    // strings that show up in UI, customisable
    $payload['PayloadDisplayName'] = "RunThisApp Profile Service";
    $payload['PayloadDescription'] = "Install this profile to allow applications deployement from RunThisApp";
    $payload_content = array();
    $payload_content['URL'] = Tools::rel2abs('/profile.php?key=' . $_GET['key'], Tools::current_url());
    $payload_content['DeviceAttributes'] = array('UDID', 'VERSION', 'PRODUCT', 'MAC_ADDRESS_EN0', 'DEVICE_NAME', 'IMEI', 'ICCID');
    if (!empty($challenge)) {
        $payload_content['Challenge'] = $challenge;
    }
    $payload['PayloadContent'] = $payload_content;
    $plist = new CFPropertyList();
    $td = new CFTypeDetector();
    $cfPayload = $td->toCFType($payload);
    $plist->add($cfPayload);
    return $plist->toXML(true);
}
header('Content-Type: application/x-apple-aspen-config');
$payload = profile_service_payload('signed-auth-token');
echo $payload;