|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (c), Includable. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to |
| 8 | + * deal in the Software without restriction, including without limitation the |
| 9 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + */ |
| 16 | + |
| 17 | +namespace PKPass; |
| 18 | + |
| 19 | +use ZipArchive; |
| 20 | + |
| 21 | +class FinanceOrder extends PKPass |
| 22 | +{ |
| 23 | + const FILE_TYPE = 'order'; |
| 24 | + const FILE_EXT = 'order'; |
| 25 | + const MIME_TYPE = 'application/vnd.apple.finance.order'; |
| 26 | + const PAYLOAD_FILE = 'order.json'; |
| 27 | + const HASH_ALGO = 'sha256'; |
| 28 | + |
| 29 | + /** |
| 30 | + * Sub-function of create() |
| 31 | + * This function creates the hashes for the files and adds them into a json string. |
| 32 | + * |
| 33 | + * @throws PKPassException |
| 34 | + */ |
| 35 | + protected function createManifest() |
| 36 | + { |
| 37 | + // Creates SHA hashes for all files in package |
| 38 | + $sha = []; |
| 39 | + $sha[self::PAYLOAD_FILE] = hash(self::HASH_ALGO, $this->json); |
| 40 | + |
| 41 | + // Creates SHA hashes for string files in each project. |
| 42 | + foreach ($this->locales as $language => $strings) { |
| 43 | + $sha[$language . '.lproj/' . self::FILE_TYPE . '.strings'] = hash(self::HASH_ALGO, $strings); |
| 44 | + } |
| 45 | + |
| 46 | + foreach ($this->files as $name => $path) { |
| 47 | + $sha[$name] = hash(self::HASH_ALGO, file_get_contents($path)); |
| 48 | + } |
| 49 | + |
| 50 | + foreach ($this->remote_file_urls as $name => $url) { |
| 51 | + $sha[$name] = hash(self::HASH_ALGO, file_get_contents($url)); |
| 52 | + } |
| 53 | + |
| 54 | + foreach ($this->files_content as $name => $content) { |
| 55 | + $sha[$name] = hash(self::HASH_ALGO, $content); |
| 56 | + } |
| 57 | + |
| 58 | + return json_encode((object)$sha); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates .pkpass zip archive. |
| 63 | + * |
| 64 | + * @param string $manifest |
| 65 | + * @param string $signature |
| 66 | + * @return string |
| 67 | + * @throws PKPassException |
| 68 | + */ |
| 69 | + protected function createZip($manifest, $signature) |
| 70 | + { |
| 71 | + // Package file in Zip (as .order) |
| 72 | + $zip = new ZipArchive(); |
| 73 | + $filename = tempnam($this->tempPath, self::FILE_TYPE); |
| 74 | + if (!$zip->open($filename, ZipArchive::OVERWRITE)) { |
| 75 | + throw new PKPassException('Could not open ' . basename($filename) . ' with ZipArchive extension.'); |
| 76 | + } |
| 77 | + $zip->addFromString('signature', $signature); |
| 78 | + $zip->addFromString('manifest.json', $manifest); |
| 79 | + $zip->addFromString(self::PAYLOAD_FILE, $this->json); |
| 80 | + |
| 81 | + // Add translation dictionary |
| 82 | + foreach ($this->locales as $language => $strings) { |
| 83 | + if (!$zip->addEmptyDir($language . '.lproj')) { |
| 84 | + throw new PKPassException('Could not create ' . $language . '.lproj folder in zip archive.'); |
| 85 | + } |
| 86 | + $zip->addFromString($language . '.lproj/' . self::FILE_TYPE . '.strings', $strings); |
| 87 | + } |
| 88 | + |
| 89 | + foreach ($this->files as $name => $path) { |
| 90 | + $zip->addFile($path, $name); |
| 91 | + } |
| 92 | + |
| 93 | + foreach ($this->remote_file_urls as $name => $url) { |
| 94 | + $download_file = file_get_contents($url); |
| 95 | + $zip->addFromString($name, $download_file); |
| 96 | + } |
| 97 | + |
| 98 | + foreach ($this->files_content as $name => $content) { |
| 99 | + $zip->addFromString($name, $content); |
| 100 | + } |
| 101 | + |
| 102 | + $zip->close(); |
| 103 | + |
| 104 | + if (!file_exists($filename) || filesize($filename) < 1) { |
| 105 | + @unlink($filename); |
| 106 | + throw new PKPassException('Error while creating order.order. Check your ZIP extension.'); |
| 107 | + } |
| 108 | + |
| 109 | + $content = file_get_contents($filename); |
| 110 | + unlink($filename); |
| 111 | + |
| 112 | + return $content; |
| 113 | + } |
| 114 | +} |
0 commit comments