-
Notifications
You must be signed in to change notification settings - Fork 7
/
runthisapp.php
254 lines (207 loc) · 8.41 KB
/
runthisapp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/*
* RunThisApp allows sharing test builds of iOS apps with testers.
* Copyright (C) 2011 Ludovic Landry & Pascal Cans
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use Entities\Application;
use Entities\Version;
use Entities\Tester;
use Entities\Invitation;
require_once __DIR__ . '/constants.php';
require_once __DIR__ . '/lib/cfpropertylist/CFPropertyList.php';
require_once __DIR__ . '/core/index.php';
require_once __DIR__ . '/tools.php';
require_once __DIR__ . '/asn.php';
function general_payload() {
$payload = array();
$payload['PayloadVersion'] = 1; // do not modify
$payload['PayloadUUID'] = uniqid(); // must be unique
//will be shown to the user.
$payload['PayloadOrganization'] = "RunThisApp";
return $payload;
}
function profile_service_payload($challenge, $key) {
$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=' . $key, Tools::current_url());
$payload_content['DeviceAttributes'] = array(
'UDID',
'VERSION',
'PRODUCT', // ie. iPhone1,1 or iPod2,1
'MAC_ADDRESS_EN0', // WiFi MAC address
'DEVICE_NAME', // given device name "iPhone"
// Items below are only available on iPhones
'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);
}
function isVersionAppSignedForUdid($udid, $version)
{
$plistValue = __DIR__ . '/' . UPLOAD_PATH . $version->getToken() . '/app_bundle/Payload/' . $version->getApplication()->getBundleName() . '.app/embedded.mobileprovision';
$plistValue = retreivePlistFromAsn($plistValue);
if (empty($plistValue)) {
die("unable to read plist from configuration profile challenge.");
}
$plist = new CFPropertyList();
$plist->parse($plistValue, CFPropertyList::FORMAT_AUTO);
$plistData = $plist->toArray();
$provisionedDevices = $plistData['ProvisionedDevices'];
$found = false;
foreach ($provisionedDevices as $device)
{
if (strtoupper($device) == strtoupper($udid)) {
return true;
}
}
return false;
}
function generateDownloadPlistFile($version) {
$payload_assets_content = array();
$payload_assets_content['kind'] = 'software-package';
$payload_assets_content['url'] = Tools::rel2abs(UPLOAD_PATH . $version->getToken() . '/app_bundle.ipa', Tools::current_url());
$payload_content = array();
$payload_content['assets'] = array( $payload_assets_content );
$payload_metadata_content = array();
$payload_metadata_content['bundle-identifier'] = $version->getApplication()->getBundleId();
$payload_metadata_content['kind'] = 'software';
$payload_metadata_content['title'] = $version->getName();
$payload_content['metadata'] = $payload_metadata_content;
$payload = array();
$payload['items'] = array( $payload_content );
$plist = new CFPropertyList();
$td = new CFTypeDetector();
$cfPayload = $td->toCFType( $payload );
$plist->add( $cfPayload );
$data = $plist->toXML(true);
$my_file = __DIR__ . '/'. UPLOAD_PATH . $version->getToken() . '.plist';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($handle, $data);
}
//Step 1 check needed params:
if (!isset($_GET['token']))
{
die('parameter token are needed (udid optional)');
}
//step 2 check that user is allowed to dl this app and this version:
//TODO using $_GET["udid"]
$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>
<script src="js/jquery-1.6.1.min.js"></script>
<script>
function showTheLink(bool) {
if (bool) {
$("#link").css("display", "inline");
$("#wait").css("display", "none");
} else {
$("#link").css("display", "none");
$("#wait").css("display", "inline");
}
}
</script>
<style>
#link {
display: none;
}
</style>
</head>
<body>
<span id="link">
Here is your link: <a href="itms-services://?action=download-manifest&url=<?php echo $appLink; ?>">Application id <?php echo $version->getApplication()->getBundleId(); ?></a>
(Profile link: <a href="<?php echo $profileLink; ?>">Application profile</a>)
</span>
<?php
if ($action == 'ENROLL') {
if (!$isNotRegistered) {
echo '<div>Your device is already registered!</div>';
}
}
else if ($action == 'DOWNLOAD') {
if ($isAppSigned) {
?>
<script>
showTheLink(true);
</script>
<?php
} else {
?>
<span id="wait">
Please Wait...
</span>
<script>
$.ajax({
type: "GET",
url: "sign.php",
data: "token=<?php echo $_GET['token']; ?>&udid=<?php echo $_GET['udid']; ?>",
success: function(msg){
showTheLink(true);
},
error: function(jqXHR, textStatus, errorThrown) {
var msg = $("span#wait").empty().append("Unable to sign!");
if (jqXHR && jqXHR.responseText) {
//append error msg
msg.append(' ' . jqXHR.responseText);
}
}
});
</script>
<?php
}
}
?>
</body>
</html>