forked from tschoffelen/php-pkpass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PKPass.php
369 lines (297 loc) · 8.37 KB
/
PKPass.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
<?php
/**
* PKPass - Creates iOS 6 passes
*
* Author: Tom Schoffelen
* Revision: Tom Janssen
*
* www.tomttb.com
*/
class PKPass {
#################################
#######PROTECTED VARIABLES#######
#################################
/*
* Holds the path to the certificate
* Variable: string
*/
protected $certPath;
/*
* Holds the files to include in the .pkpass
* Variable: array
*/
protected $files = array();
/*
* Holds the json
* Variable: class
*/
protected $JSON;
/*
* Holds the SHAs of the $files array
* Variable: array
*/
protected $SHAs;
/*
* Holds the password to the certificate
* Variable: string
*/
protected $certPass = '';
/*
* Holds the path to the WWDR Intermediate certificate
* Variable: string
*/
protected $WWDRcertPath = '';
#################################
########PRIVATE VARIABLES########
#################################
/*
* Holds the path to a temporary folder
*/
private $tempPath = '/tmp/'; // Must end with slash!
/*
* Holds error info if an error occured
*/
private $sError = '';
/*
* Holds a autogenerated uniqid to prevent overwriting other processes pass files
*/
private $uniqid = null;
#################################
#########PUBLIC FUNCTIONS########
#################################
public function __construct($certPath = false, $certPass = false, $JSON = false) {
if($certPath != false) {
$this->setCertificate($certPath);
}
if($certPass != false) {
$this->setCertificatePassword($certPass);
}
if($JSON != false) {
$this->setJSON($JSON);
}
}
/*
* Sets the path to a certificate
* Parameter: string, path to certificate
* Return: boolean, true on succes, false if file doesn't exist
*/
public function setCertificate($path) {
if(file_exists($path)) {
$this->certPath = $path;
return true;
}
$this->sError = 'Certificate file did not exist.';
return false;
}
/*
* Sets the certificate's password
* Parameter: string, password to the certificate
* Return: boolean, always true
*/
public function setCertificatePassword($p) {
$this->certPass = $p;
return true;
}
/*
* Decodes JSON and saves it to a variable
* Parameter: json-string
* Return: boolean, true on succes, false if json wasn't decodable
*/
public function setJSON($JSON) {
if(json_decode($JSON) !== false) {
$this->JSON = $JSON;
return true;
}
$this->sError = 'This is not a JSON string.';
return false;
}
/*
* Adds file to the file array
* Parameter: string, path to file
* Return: boolean, true on succes, false if file doesn't exist
*/
public function addFile($path){
if(file_exists($path)){
$this->files[] = $path;
return true;
}
$this->sError = 'File did not exist.';
return false;
}
/*
* Creates the actual .pkpass file
* Parameter: boolean, if output is true, send the zip file to the browser.
* Return: zipped .pkpass file on succes, false on failure
*/
public function create($output = false) {
$paths = $this->paths();
//Creates and saves the json manifest
$manifest = $this->createManifest();
//Create signature
if($this->createSignature($manifest) == false) {
$this->clean();
return false;
}
if($this->createZip($manifest) == false) {
$this->clean();
return false;
}
// Check if pass is created and valid
if(!file_exists($paths['pkpass']) || filesize($paths['pkpass']) < 1) {
$this->sError = 'Error while creating pass.pkpass. Check your Zip extension.';
$this->clean();
return false;
}
// Output pass
if($output == true) {
header('Pragma: no-cache');
header('Content-type: application/vnd.apple.pkpass');
header('Content-length: '.filesize($paths['pkpass']));
header('Content-Disposition: attachment; filename="'.basename($paths['pkpass']).'"');
echo file_get_contents($paths['pkpass']);
$this->clean();
} else {
$file = file_get_contents($paths['pkpass']);
$this->clean();
return $file;
}
}
public function checkError(&$error) {
if(trim($this->sError) == '') {
return false;
}
$error = $this->sError;
return true;
}
public function getError() {
return $this->sError;
}
#################################
#######PROTECTED FUNCTIONS#######
#################################
/*
* Subfunction of create()
* This function creates the hashes for the files and adds them into a json string.
*/
protected function createManifest() {
// Creates SHA hashes for all files in package
$this->SHAs['pass.json'] = sha1($this->JSON);
foreach($this->files as $file) {
$this->SHAs[basename($file)] = sha1(file_get_contents($file));
}
$manifest = json_encode((object)$this->SHAs);
return $manifest;
}
/*
* Converts PKCS7 PEM to PKCS7 DER
* Parameter: string, holding PKCS7 PEM, binary, detached
* Return: string, PKCS7 DER
*/
protected function convertPEMtoDER($signature) {
//DO NOT MOVE THESE WITH TABS, OTHERWISE THE FUNCTION WON'T WORK ANYMORE!!
$begin = 'filename="smime.p7s"
';
$end = '
------';
$signature = substr($signature, strpos($signature, $begin)+strlen($begin));
$signature = substr($signature, 0, strpos($signature, $end));
$signature = base64_decode($signature);
return $signature;
}
/*
* Creates a signature and saves it
* Parameter: json-string, manifest file
* Return: boolean, true on succes, failse on failure
*/
protected function createSignature($manifest) {
$paths = $this->paths();
file_put_contents($paths['manifest'], $manifest);
$pkcs12 = file_get_contents($this->certPath);
$certs = array();
if(openssl_pkcs12_read($pkcs12, $certs, $this->certPass) == true) {
$certdata = openssl_x509_read($certs['cert']);
$privkey = openssl_pkey_get_private($certs['pkey'], $this->certPass );
if(!empty($this->WWDRcertPath)){
if(!file_exists($this->WWDRcertPath)){
$this->sError = 'WWDC Intermediate Certificate does not exist';
return false;
}
openssl_pkcs7_sign($paths['manifest'], $paths['signature'], $certdata, $privkey, array(), PKCS7_BINARY | PKCS7_DETACHED, $this->WWDRcertPath);
}else{
openssl_pkcs7_sign($paths['manifest'], $paths['signature'], $certdata, $privkey, array(), PKCS7_BINARY | PKCS7_DETACHED);
}
$signature = file_get_contents($paths['signature']);
$signature = $this->convertPEMtoDER($signature);
file_put_contents($paths['signature'], $signature);
return true;
} else {
$this->sError = 'Could not read the certificate';
return false;
}
}
/*
* Creates .pkpass (zip archive)
* Parameter: json-string, manifest file
* Return: boolean, true on succes, false on failure
*/
protected function createZip($manifest) {
$paths = $this->paths();
// Package file in Zip (as .pkpass)
$zip = new ZipArchive();
if(!$zip->open($paths['pkpass'], ZIPARCHIVE::CREATE)) {
$this->sError = 'Could not open '.basename($paths['pkpass']).' with ZipArchive extension.';
return false;
}
$zip->addFile($paths['signature'],'signature');
$zip->addFromString('manifest.json',$manifest);
$zip->addFromString('pass.json',$this->JSON);
foreach($this->files as $file){
$zip->addFile($file, basename($file));
}
$zip->close();
return true;
}
/*
* Declares all paths used for temporary files.
*/
protected function paths() {
//Declare base paths
$paths = array(
'pkpass' => 'pass.pkpass',
'signature' => 'signature',
'manifest' => 'manifest.json'
);
//If trailing slash is missing, add it
if(substr($this->tempPath, -1) != '/') {
$this->tempPath = $this->tempPath.'/';
}
//Generate a unique subfolder in the tempPath to support generating more passes at the same time without erasing/overwriting each others files
if (empty($this->uniqid)) {
$this->uniqid = uniqid('PKPass', true);
if (!is_dir($this->tempPath.$this->uniqid)) {
mkdir($this->tempPath.$this->uniqid);
}
}
//Add temp folder path
foreach($paths AS $pathName => $path) {
$paths[$pathName] = $this->tempPath.$this->uniqid.'/'.$path;
}
return $paths;
}
/*
* Removes all temporary files
*/
protected function clean() {
$paths = $this->paths();
foreach($paths AS $path) {
if(file_exists($path)) {
unlink($path);
}
}
//Remove our unique temporary folder
if (is_dir($this->tempPath.$this->uniqid)) {
rmdir($this->tempPath.$this->uniqid);
}
return true;
}
}