This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipa.php
485 lines (402 loc) · 11.5 KB
/
ipa.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/**
* IPA-Distributor
*
* IPA Distribution Class (ad-hoc).
* Creates manifest from ipa files for distribution or read manifests.
*
* @package IPA-Distributor
* @author Ali Alharthi <root@ali.ninja>
* @copyright 2017-2019 www.ali.ninja
* @license MIT
* @version 1.0
* @link https://github.com/ali-alharthi/IPA-Distributor
*
*/
namespace aAlharthi\Ninja;
class IPA {
/**
* Temp folder for the dirty work
*
* @var string
*/
protected $temp = 'tmp';
/**
* destination folder for output
*
* @var string
*/
protected $destination = 'ipaz'; // Output directory
/**
* Full URL for the current path
*
* @var string
*/
protected $url = 'https://www.ali.ninja/ipa/'; // Full url including http:// or https:// and the full path, don't be lazy!
/**
* Icon size [2x, 3x]
*
* @var string
*/
protected $appIconSize = "@2x";
/**
* IPA path
*
* @var string
*/
public $ipa;
/**
* Extracted IPA path
*
* @var string
*/
public $app;
/**
* IPA URL
*
* @var string
*/
public $appIPA;
/**
* App bundle name
*
* @var string
*/
public $appName;
/**
* App bundle identifier
*
* @var string
*/
public $appBundleID;
/**
* App bundle version
*
* @var string
*/
public $appVersion;
/**
* Manifest
*
* @var string
*/
public $manifest;
/**
* Manifest path
*
* @var string
*/
public $manifestPath;
/**
* Initialize and load IPA with an automatic processing option.
*
* @param string $ipa the IPA file path.
* @param boolean $auto for automatic IPA process.
* @access public
*/
public function __construct($ipa=NULL, $auto = FALSE){
/**
* Check if IPA file was set, if so load it
*/
!empty($ipa)?$this->ipa=$ipa:$this->ipa=null;
/**
* Check if auto was set, if so run as follow:
* 1. Extract the IPA (with both options set to true).
* 3. Generate a manifest.
* 4. Delete all the dirty work.
*/
if ($auto){
$this->eIPA(TRUE, TRUE);
$this->icon($this->read());
$this->generate();
$this->clean();
}
}
/**
* Extract IPA files.
*
* @param boolean $movetodestination to move IPA to destination.
* @param boolean $includeappicon to make a copy of app icon to destination.
* @return string extarcted .app path.
* @access public
*/
public function eIPA($movetodestination = TRUE, $includeappicon = TRUE){
/**
* Check if the IPA file exist
*/
if (!is_file($this->ipa) or empty($this->ipa))
die("ipa file not found.");
/**
* Check if the IPA file is readable
*/
if (!is_readable($this->ipa))
die("can not read ipa file.");
/**
* Check if the temp folder exist, if not create it
*/
if (!is_dir($this->temp))
mkdir($this->temp) or die("unable to create a folder.");
$zip = new \ZipArchive;
/**
* Check if the IPA file was unzipped correctly
*/
if ($zip->open($this->ipa) === TRUE){
$zip->extractTo('tmp');
$zip->close();
if (glob($this->temp."/Payload/*.app")){
$this->app = glob($this->temp."/Payload/*.app");
$this->app = $this->app[0];
if (!is_dir($this->app)){
// $this->clean(); // clear temp destination
die("ipa not recognized.");
}
if (!file_exists($this->app."/Info.plist")){
// $this->clean(); // clear temp destination
die("Info.plist not found.");
}
} else {
$this->clean(); // clear temp
die("ipa not recognized."); // maybe not supported yet
}
} else {
die("ipa not recognized.");
}
/**
* Read app info and apply user settings
*/
$this->read();
if ($movetodestination)
$this->move();
if ($includeappicon)
$this->icon($this->read());
return $this->app;
}
/**
* Read app information (info.plist).
*
* @param string $manifest to read using readManifest() function.
* @return array with app info.
* @access public
*/
public function read($manifest = NULL){
/**
* Check if manifest is set, if so send it to readManifest function
*/
if (!empty($manifest))
return $this->readManifest($manifest);
/**
* Check if manifest exist
*/
if (!file_exists($this->app."/Info.plist"))
die("Info.plist not found.");
/**
* Read plist file using CFPropertyList and check if its empty
*/
$list[] = null;
require_once('CFPropertyList/CFPropertyList.php');
$plist = new \CFPropertyList\CFPropertyList( $this->app . '/Info.plist' );
$plistArray = $plist->toArray();
if (empty($plistArray))
die("Info.plist is empty.");
/**
* Load app info
*/
$this->appIPA = $this->url.$this->ipa;
$this->appName = $plistArray['CFBundleDisplayName'];
$this->appBundleID = $plistArray['CFBundleIdentifier'];
$this->appVersion = $plistArray['CFBundleVersion'];
$list['appIPA'] = $this->appIPA;
$list['appName'] = $this->appName;
$list['appBundleID'] = $this->appBundleID;
$list['appVersion'] = $this->appVersion;
return $list;
}
/**
* Extract information from a manifest.
*
* @param string $manifest to manifest path.
* @return array with app info.
* @access protected
*/
protected function readManifest($manifest){
/**
* Check if manifest exist
*/
if (!file_exists($manifest))
die("manifest not found.");
/**
* Check if manifest is readable
*/
if (!is_readable($manifest))
die("can not read manifest.");
/**
* Load the manifest
*/
$this->manifest = file_get_contents($manifest);
$this->manifestPath = $manifest;
/**
* Read plist file using CFPropertyList and check if its empty
*/
$list[] = null;
require_once('CFPropertyList/CFPropertyList.php');
$plist = new \CFPropertyList\CFPropertyList( $this->manifestPath );
$plistArray = $plist->toArray();
if (empty($plistArray))
die("manifest is empty.");
/**
* Load app info
*/
$this->appIPA = $plistArray['items']['0']['assets']['0']['url'];
$this->appName = $plistArray['items']['0']['metadata']['title'];
$this->appBundleID = $plistArray['items']['0']['metadata']['bundle-identifier'];
$this->appVersion = $plistArray['items']['0']['metadata']['bundle-version'];
$list['appIPA'] = $this->appIPA;
$list['appName'] = $this->appName;
$list['appBundleID'] = $this->appBundleID;
$list['appVersion'] = $this->appVersion;
return $list;
}
/**
* Copy app icon to destination.
*
* @param array $info to app information.
* @return copies icon path.
* @access protected
*/
protected function icon($info){
$icon = str_replace(' ', '', strtolower(basename($this->ipa, ".ipa"))).'.png';
/**
* Check if destination exist, if not create it
*/
if (!is_dir($this->destination))
mkdir($this->destination) or die("unable to create a folder.");
/**
* Get the app icon with the size 60x60 if we could find it
*/
foreach (glob($this->app.'/*'.$this->appIconSize.'.png') as $file) {
if (strpos($file, '60') === false)
'';
else
$appIcon = $file;
}
empty($appIcon)?die("no icon found."):copy($appIcon, $this->destination.'/'.$icon);
return $this->destination.'/'.$icon;
}
/**
* Generate a manifest.
*
* @return manifest path.
* @access public
*/
public function generate(){
/**
* Check if information is missing
*/
if (empty($this->ipa) or empty($this->bundleID) or empty($this->appVersion) or empty($this->appName))
empty($this->read())?die("missing information."):'';
$this->manifest = '<!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->url.'/'.$this->destination.'/'.$this->ipa.'</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>'.$this->appBundleID.'</string>
<key>bundle-version</key>
<string>'.$this->appVersion.'</string>
<key>kind</key>
<string>software</string>
<key>title</key>
<string>'.$this->appName.'</string>
</dict>
</dict>
</array>
</dict>
</plist>';
/**
* Use the app name to create a manifest name
*/
$this->manifestPath = $this->destination.'/'.str_replace(' ', '', strtolower(basename($this->ipa, ".ipa"))).".plist";
/**
* Write the manifest to the created name
*/
$file = fopen($this->manifestPath, "w") or die("unable to create a new file.");
fwrite($file, $this->manifest);
fclose($file);
return $this->manifestPath;
}
/**
* Move the ipa file to destination.
*
* @return string new IPA path.
* @access protected
*/
protected function move(){
/**
* Check if destination exist, if not create it
*/
if (!is_dir($this->destination))
mkdir($this->destination) or die("unable to create a folder.");
/**
* Move it
*/
rename($this->ipa, $this->destination.'/'.$this->ipa);
return $this->destination.'/'.$this->ipa;
}
/**
* Create a download link for ios devices.
*
* @return string download link.
* @access public
*/
public function link($manifest = NULL){
empty($manifest)? $link = "itms-services://?action=download-manifest&url=".$this->url."/".$this->destination.'/'.$this->appName.".plist" : $link = "itms-services://?action=download-manifest&url=".$this->url."/".$manifest;
return $link;
}
/**
* Clean the temp folder (extracted ipa).
*
* @access public
*/
public function clean(){
$list = glob($this->temp.'/*'); // get all files (notice hidden files are not included here!)
foreach ($list as $name) {
if (is_dir($name)){
if($name!=$this->temp."." or $name!=$this->temp."..")
$this->rm($name);
} else {
unlink($name);
}
}
}
/**
* Remove a folder.
*
* @access protected
*/
protected function rm($dir){
// Stackoverflow
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($dir);
}
}
?>