forked from maciekish/IPA-Distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipaDistrubution.php
307 lines (276 loc) · 9.58 KB
/
ipaDistrubution.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?
/**
* Creates an Manifest from any IPA iPhone application file for iOS Wireless App Distribution.
* and searches for the right provision profile in the same folder
*
* Script needs GD Library (for resizing the images) and CFPropertyList class (http://code.google.com/p/cfpropertylist/)
* @author Wouter van den Broek <[email protected]>
*/
class ipaDistrubution {
/**
* The base url of the script.
*/
protected $baseurl;
/**
* The base folder of the script.
*/
protected $basedir;
/**
* The folder of the app where the manifest will be written.
*/
protected $folder;
/**
* iTunesArtwork name which is an standard from Apple (http://developer.apple.com/iphone/library/qa/qa2010/qa1686.html).
*/
protected $itunesartwork = "iTunesArtwork";
/**
* App name which can be used for the HTML page.
*/
public $appname;
/**
* Bundle version which can be used for the HTML page.
*/
public $bundleversion;
/**
* Mobileprovision
*/
public $mobileprovision;
/**
* App ccon which can be used for the HTML page.
*/
public $appicon;
/**
* The link to the manifest for the iPhone .
*/
public $applink = "itms-services://?action=download-manifest&url=";
/**
* Bundle identifier which is used to find the proper provision profile.
*/
protected $identiefier;
/**
* Bundle icon name for extracting icon file
*/
protected $icon;
/**
* The name of the provision profile for the IPA iPhone application .
*/
public $provisionprofile;
/**
* Initialize the IPA and create the Manifest.
*
* @param String $ipa the IPA file for which an Manifest must be made
*/
public function __construct($ipa, $subfolder){
$this->baseurl = "http".((!empty($_SERVER['HTTPS'])) ? "s" : "")."://".$_SERVER['SERVER_NAME'];
$this->basedir = (strpos($_SERVER['REQUEST_URI'],".php")===false?$_SERVER['REQUEST_URI']:dirname($_SERVER['REQUEST_URI'])."/");
//Remove querystring from basedir
if( strpos( $this->basedir, "?" ) > 0 ) {
$questionmark_position = strpos( $this->basedir, "?" );
$this->basedir = substr( $this->basedir, 0, $questionmark_position);
}
$this->makeDir("files/" . $subfolder . basename($ipa, ".ipa"));
$this->getPlist($ipa);
$this->createManifest($ipa);
$this->seekMobileProvision($this->identiefier);
$this->getIcon($ipa);
$this->getMobileProvision($ipa);
if (file_exists($this->itunesartwork)) {
$this->makeImages();
}
$this->cleanUp();
}
/**
* Make a folder where the Manifest and icon files are held.
*
* @param String $dirname name of the folder
*/
function makeDir ($dirname) {
$this->folder = $dirname;
if (!is_dir($dirname)) {
if (!mkdir($dirname)) die('Failed to create folder '.$dirname.'... Is the current folder writeable?');
}
}
/**
* Get de Plist and iTunesArtwork from the IPA file
*
* @param String $ipa the location of the IPA file
*/
function getPlist ($ipa) {
if (is_dir($this->folder)) {
$zip = zip_open($ipa);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fileinfo = pathinfo(zip_entry_name($zip_entry));
if ($fileinfo['basename']=="Info.plist"||$fileinfo['basename']==$this->itunesartwork) {
$fp = fopen($fileinfo['basename'], "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
}
}
zip_close($zip);
}
}
}
/**
* Create the icon (if not in IPA) and itunes artwork from the original iTunesArtwork
*/
function makeImages () {
if (function_exists("ImageCreateFromJPEG")) {
$im = @ImageCreateFromJPEG ($this->itunesartwork);
$x = @getimagesize($this->itunesartwork);
$iTunesfile = @ImageCreateTrueColor (512, 512);
@ImageCopyResampled ($iTunesfile, $im, 0, 0, 0, 0, 512, 512, $x[0], $x[1]);
@ImagePNG($iTunesfile,$this->folder."/itunes.png",0);
@ImageDestroy($iTunesfile);
if ($this->icon==null) {
$iconfile = @ImageCreateTrueColor (57, 57);
@ImageCopyResampled ($iconfile, $im, 0, 0, 0, 0, 57, 57, $x[0], $x[1]);
@ImagePNG($iconfile,$this->folder."/icon.png",0);
@ImageDestroy($iconfile);
$this->appicon = $this->folder."/icon.png";
}
}
}
/**
* Get the icon file out of the IPA and place it in the right folder
*/
function getIcon ($ipa) {
if (is_dir($this->folder)) {
$zip = zip_open($ipa);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fileinfo = pathinfo(zip_entry_name($zip_entry));
if ($fileinfo['basename']==$this->icon) {
$fp = fopen($this->folder.'/'.$fileinfo['basename'], "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
$this->appicon = $this->folder."/".$this->icon;
}
}
zip_close($zip);
}
}
}
/**
* Get the .mobileprovision file out of the IPA and place it in the right folder
*/
function getMobileProvision ($ipa) {
if (is_dir($this->folder)) {
$zip = zip_open($ipa);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fileinfo = pathinfo(zip_entry_name($zip_entry));
if ($fileinfo['basename']=="embedded.mobileprovision") {
$fp = fopen($this->folder.'/'.$fileinfo['basename'], "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
$this->mobileprovision = $this->folder."/".$fileinfo['basename'];
}
}
zip_close($zip);
}
}
}
/**
* Parse the Plist and get the values for the creating an Manifest and write the Manifest
*
* @param String $ipa the location of the IPA file
*/
function createManifest ($ipa) {
if (file_exists(dirname(__FILE__).'/cfpropertylist/CFPropertyList.php')) {
require_once(dirname(__FILE__).'/cfpropertylist/CFPropertyList.php');
$plist = new CFPropertyList('Info.plist');
$plistArray = $plist->toArray();
//var_dump($plistArray);
$this->identiefier = $plistArray['CFBundleIdentifier'];
$this->appname = $plistArray['CFBundleDisplayName'];
$this->bundleversion = $plistArray['CFBundleVersion'];
$this->icon = ($plistArray['CFBundleIconFile']!=""?$plistArray['CFBundleIconFile']:(count($plistArray['CFBundleIconFile'])>0?$plistArray['CFBundleIconFile'][0]:null));
$manifest = '<?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>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>'.$this->baseurl.$this->basedir.$ipa.'</string>
</dict>
'.(file_exists($this->folder.'/itunes.png')?'<dict>
<key>kind</key>
<string>full-size-image</string>
<key>needs-shine</key>
<false/>
<key>url</key>
<string>'.$this->baseurl.$this->basedir.$this->folder.'/itunes.png</string>
</dict>':'').'
'.(file_exists($this->folder.'/icon.png')?'<dict>
<key>kind</key>
<string>display-image</string>
<key>needs-shine</key>
<false/>
<key>url</key>
<string>'.$this->baseurl.$this->basedir.$this->folder.'/'.($this->icon==null?'icon.png':$this->icon).'</string>
</dict>':'').'
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>'.$plistArray['CFBundleIdentifier'].'</string>
<key>bundle-version</key>
<string>'.$plistArray['CFBundleVersion'].'</string>
<key>kind</key>
<string>software</string>
<key>title</key>
<string>'.$plistArray['CFBundleDisplayName'].'</string>
</dict>
</dict>
</array>
</dict>
</plist>';
if (file_put_contents($this->folder."/".basename($ipa, ".ipa").".plist",$manifest)) $this->applink = $this->applink.$this->baseurl.$this->basedir.$this->folder."/".basename($ipa, ".ipa").".plist";
else die("Wireless manifest file could not be created !?! Is the folder ".$this->folder." writable?");
} else die("CFPropertyList class was not found! You need it to create the wireless manifest. Put it in de folder cfpropertylist!");
}
/**
* Removes temporary files
*/
function cleanUp () {
if (file_exists($this->itunesartwork)) @unlink($this->itunesartwork);
if (file_exists("Info.plist")) @unlink("Info.plist");
}
/**
* Search for the right provision profile in de current folder
*
* @param String $identiefier the bundle identifier for the app
*/
function seekMobileProvision ($identiefier) {
$wildcard = pathinfo($identiefier);
$bundels = array();
foreach (glob("*.mobileprovision") as $filename) {
$profile = file_get_contents($filename);
$seek = strpos(strstr($profile, $wildcard['filename']),"</string>");
if ($seek!== false) $bundels[substr(strstr($profile, $wildcard['filename']),0,$seek)] = $filename;
}
if (array_key_exists($this->identiefier,$bundels)) $this->provisionprofile = $bundels[$this->identiefier];
else if (array_key_exists($wildcard['filename'].".*",$bundels)) $this->provisionprofile = $bundels[$wildcard['filename'].".*"];
else $this->provisionprofile = null;
}
}