Deobfuscation of font files
[epubpypt.git] / src / Cli.php
1 <?php
2 namespace Epubpypt;
3
4 /**
5  * Command line interface for the tool
6  */
7 class Cli
8 {
9     public function run(): void
10     {
11         $parser = $this->setupCliParser();
12         try {
13             $result = $parser->parse();
14         } catch (\Exception $exc) {
15             $parser->displayError($exc->getMessage());
16             exit(1);
17         }
18
19         switch ($result->command_name) {
20         case 'decrypt':
21             $cmd = new Command\Decrypt(
22                 $result->command->args['epub'], $result->command->args['path'],
23                 $result->command->options
24             );
25             break;
26
27         case 'deobfuscate':
28             $cmd = new Command\Deobfuscate(
29                 $result->command->args['epub'], $result->command->args['path'],
30                 $result->command->options
31             );
32             break;
33
34         case 'ls':
35             $cmd = new Command\ListContent(
36                 $result->command->args['epub'], $result->command->options
37             );
38             break;
39
40         default:
41             $parser->displayError('Command name missing');
42             exit(1);
43         }
44
45         $logger = new Logger();
46         $logger->showInfo = $result->options['verbose'];
47
48         try {
49             $cmd->logger = $logger;
50             $cmd->run();
51         } catch (\Exception $e) {
52             fwrite(STDERR, 'Error: ' . $e->getMessage() . "\n");
53             exit(2);
54         }
55     }
56
57     protected function setupCliParser(): \Console_Commandline
58     {
59         $parser = new \Console_CommandLine();
60         $parser->description = 'Encrypt and decrypt epub files';
61         $parser->version = 'fixme';
62
63         $parser->addOption(
64             'verbose',
65             [
66                 'short_name'  => '-v',
67                 'long_name'   => '--verbose',
68                 'description' => 'Show more messages',
69                 'action'      => 'StoreTrue',
70                 'default'     => false,
71             ]
72         );
73
74         $ls = $parser->addCommand(
75             'ls',
76             [
77                 'description' => 'List files inside the .epub with'
78                     . ' their encryption status' . "\n"
79                     . "\n"
80                     . "First column: E=encrypted, O=obfuscated\n"
81                     . "Second column:\n"
82                     . " RESOBF = EPUB Open Container Format (OCF) 3.2: Resource Obfuscation\n"
83                     . " AES256 = XML Encryption Syntax and Processing, AES-256",
84             ]
85         );
86         $ls->addArgument(
87             'epub',
88             ['description' => '.epub file to inspect']
89         );
90
91         $decrypt = $parser->addCommand(
92             'decrypt',
93             [
94                 'description' => 'Decrypt encrypted files inside the epub file',
95             ]
96         );
97         $decrypt->addOption(
98             'key',
99             [
100                 'short_name'  => '-k',
101                 'long_name'   => '--key',
102                 'description' => 'Hex-encoded encryption key',
103                 'action'      => 'StoreString',
104                 'default'     => null,
105             ]
106         );
107         $decrypt->addOption(
108             'keyfile',
109             [
110                 'short_name'  => '-k',
111                 'long_name'   => '--keyfile',
112                 'description' => 'File containing encryption key',
113                 'action'      => 'StoreString',
114                 'default'     => null,
115             ]
116         );
117         $decrypt->addOption(
118             'all',
119             [
120                 'short_name'  => '-a',
121                 'long_name'   => '--all',
122                 'description' => 'Decrypt all encrypted files',
123                 'action'      => 'StoreTrue',
124                 'default'     => false,
125             ]
126         );
127         $decrypt->addOption(
128             'stdout',
129             [
130                 'short_name'  => '-c',
131                 'long_name'   => '--stdout',
132                 'description' => 'Send decrypted contents to stdout. Do not modify epub file.',
133                 'action'      => 'StoreTrue',
134                 'default'     => false,
135             ]
136         );
137         $decrypt->addArgument(
138             'epub',
139             ['description' => '.epub file']
140         );
141         $decrypt->addArgument(
142             'path',
143             [
144                 'description' => 'File paths inside the epub file',
145                 'optional'    => true,
146                 'multiple'    => true,
147             ]
148         );
149
150         $deobfuscate = $parser->addCommand(
151             'deobfuscate',
152             [
153                 'description' => 'Deobfuscate files inside the epub file',
154             ]
155         );
156         $deobfuscate->addOption(
157             'key',
158             [
159                 'short_name'  => '-k',
160                 'long_name'   => '--key',
161                 'description' => 'Hex-encoded obfuscation key',
162                 'action'      => 'StoreString',
163                 'default'     => null,
164             ]
165         );
166         $deobfuscate->addOption(
167             'keyfile',
168             [
169                 'short_name'  => '-k',
170                 'long_name'   => '--keyfile',
171                 'description' => 'File containing obfuscatino key',
172                 'action'      => 'StoreString',
173                 'default'     => null,
174             ]
175         );
176         $deobfuscate->addOption(
177             'all',
178             [
179                 'short_name'  => '-a',
180                 'long_name'   => '--all',
181                 'description' => 'Deobfuscate all obfuscated files',
182                 'action'      => 'StoreTrue',
183                 'default'     => false,
184             ]
185         );
186         $deobfuscate->addOption(
187             'stdout',
188             [
189                 'short_name'  => '-c',
190                 'long_name'   => '--stdout',
191                 'description' => 'Send deobfuscated contents to stdout. Do not modify epub file.',
192                 'action'      => 'StoreTrue',
193                 'default'     => false,
194             ]
195         );
196         $deobfuscate->addArgument(
197             'epub',
198             ['description' => '.epub file']
199         );
200         $deobfuscate->addArgument(
201             'path',
202             [
203                 'description' => 'File paths inside the epub file',
204                 'optional'    => true,
205                 'multiple'    => true,
206             ]
207         );
208
209         return $parser;
210     }
211 }