/**
  * Connect the developer with its user name/password and store the connection cookie
  * @param string $user
  * @param string $pwd 
  * 
  * if $result['resultCode'] equal 0 you are connected
  * user info are acessible via $result['firstName'] and $result['lastName']
  * 
  * else you can print the error $result['resultString']
  * 
  * @return array $result
  */
 public static function connect($user, $pwd)
 {
     self::$user = $user;
     self::$passwod = $pwd;
     $url = 'https://daw2.apple.com/cgi-bin/WebObjects/DSAuthWeb.woa/wa/clientDAW?format=plist&appIdKey=' . self::$app_id_key . '&appleId=' . urlencode($user) . '&password='******'&userLocale=' . self::$user_locale . '&protocolVersion=' . self::$protocol_version_daw2;
     // Create a new cURL resource
     $ch = curl_init();
     // Set URL and other appropriate options
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Xcode', 'Accept: text/x-xml-plist', 'Accept-Language: en-us', 'Connection: keep-alive'));
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
     curl_setopt($ch, CURLOPT_HEADER, true);
     // Execute and close cURL
     $data = curl_exec($ch);
     curl_close($ch);
     self::$cookie = AppleServices::get_cookie($data);
     $content = AppleServices::get_content($data);
     $plist = new CFPropertyList();
     $plist->parse($content, CFPropertyList::FORMAT_AUTO);
     $plistData = $plist->toArray();
     return $plistData;
 }
    if ($result['resultCode'] != 0) {
        Tools::dieError($result['userString']);
    }
    foreach ($result['provisioningProfiles'] as $profile) {
        if ($version->getApplication()->getBundleId() == $profile['appId']['identifier']) {
            $new_profile_id = $profile['provisioningProfileId'];
            break;
        }
    }
    //do something to avoid stackoverflow in case of long generation
    if (++$iterCount > 50) {
        Tools::dieError('Profile not yet updated by Apple after ' . $iterCount . ' iterations. abandon.');
    }
}
echo '<p>New profile id: ' . $new_profile_id . '</p>' . PHP_EOL;
$result = AppleServices::downloadProvisioningProfile($new_profile_id, $team_id);
if ($result['resultCode'] != 0) {
    if ($result['userString'] != NULL) {
        Tools::dieError($result['userString']);
    } else {
        echo '<p>Error downloading the new profile.</p>' . PHP_EOL;
        var_dump($result);
    }
}
$new_profile = $result['provisioningProfile']['encodedProfile'];
//TODO: save this with the app binary (tmp: save in app folder with bundle id)
$newfile = __DIR__ . '/' . UPLOAD_PATH . $version->getToken() . '.mobileprovision';
$file = fopen($newfile, "w");
fwrite($file, $new_profile);
fclose($file);
echo '<p>New profile saved.</p>' . PHP_EOL;