-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStringEncrypt.php
More file actions
630 lines (500 loc) · 15.6 KB
/
Copy pathStringEncrypt.php
File metadata and controls
630 lines (500 loc) · 15.6 KB
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
<?php
declare(strict_types=1);
namespace StringEncrypt;
use InvalidArgumentException;
/**
* Stateful HTTP client: pass the activation key to the constructor, configure other options via
* setters, then {@see send()}.
*
* POST field `code` (activation key) is the value given at construction (empty string = demo).
*
* For {@see Command::Encrypt} with {@see setCompression()}`(true)`, the API returns
* `source` as base64-encoded gzip of the decryptor text. {@see send()} decodes that
* automatically so `source` is always plain text on success (unless
* {@see setDecompressEncryptSource()}`(false)`).
*/
final class StringEncrypt
{
public const DEFAULT_API_URL = 'https://www.stringencrypt.com/api.php';
private bool $preferCurl = false;
/** When true (default), decompress `source` after successful encrypt+compression responses. */
private bool $decompressEncryptSource = true;
private ?Command $command = null;
/** Activation code sent as `code` (empty = demo). */
private string $apiKey = '';
// —— encrypt ——
private string $label = 'Label';
private ?string $inputString = null;
private ?string $inputBytes = null;
private bool $compression = false;
private Language $language = Language::Php;
/** `false`, or highlight mode string such as `geshi` / `js` when supported server-side. */
private string|bool $highlight = false;
private int $cmdMin = 1;
private int $cmdMax = 3;
private bool $local = false;
private bool $unicode = true;
private string $langLocale = 'en_US.utf8';
private string $ansiEncoding = 'WINDOWS-1250';
private NewLine $newLines = NewLine::Lf;
private ?string $template = null;
private bool $returnTemplate = false;
private bool $includeTags = false;
private bool $includeExample = false;
/** When true, server emits a trace comment block above the encrypted array (hashes, VM JSON). */
private bool $includeDebugComments = false;
public function __construct(
string $apiKey = '',
bool $preferCurl = false,
) {
$this->apiKey = $apiKey;
$this->preferCurl = $preferCurl;
}
/**
* Activation status and limits for the activation key passed to the constructor.
*
* @return array<string, mixed>|false Parsed JSON on success, or false on transport/parse failure.
*/
public function isDemo(): array|false
{
$previousCommand = $this->command;
$this->setCommand(Command::IsDemo);
$result = $this->send();
$this->command = $previousCommand;
return $result;
}
/**
* Encrypt raw file contents (binary). Uses other encrypt options already set on this client.
*
* @return array<string, mixed>|false Parsed JSON on success, or false if the file is missing,
* unreadable, empty, or on transport/parse failure.
*/
public function encryptFileContents(string $filePath, string $label): array|false
{
$raw = @file_get_contents($filePath);
if ($raw === false || $raw === '') {
return false;
}
$saved = [
'command' => $this->command,
'inputString' => $this->inputString,
'inputBytes' => $this->inputBytes,
'label' => $this->label,
];
$this->setCommand(Command::Encrypt)->setBytes($raw)->setLabel($label);
$result = $this->send();
$this->command = $saved['command'];
$this->inputString = $saved['inputString'];
$this->inputBytes = $saved['inputBytes'];
$this->label = $saved['label'];
return $result;
}
/**
* Encrypt a UTF-8 string. Uses other encrypt options already set on this client (language,
* compression, cmd range, etc.).
*
* @return array<string, mixed>|false Parsed JSON on success, or false on transport/parse failure.
*/
public function encryptString(string $string, string $label): array|false
{
$saved = [
'command' => $this->command,
'inputString' => $this->inputString,
'inputBytes' => $this->inputBytes,
'label' => $this->label,
];
$this->setCommand(Command::Encrypt)->setString($string)->setLabel($label);
$result = $this->send();
$this->command = $saved['command'];
$this->inputString = $saved['inputString'];
$this->inputBytes = $saved['inputBytes'];
$this->label = $saved['label'];
return $result;
}
public function getPreferCurl(): bool
{
return $this->preferCurl;
}
public function setPreferCurl(bool $preferCurl): self
{
$this->preferCurl = $preferCurl;
return $this;
}
public function getDecompressEncryptSource(): bool
{
return $this->decompressEncryptSource;
}
/**
* Disable automatic gzip+base64 decoding of `source` for encrypt+compression responses
* if you need the raw payload from the API.
*/
public function setDecompressEncryptSource(bool $decompressEncryptSource): self
{
$this->decompressEncryptSource = $decompressEncryptSource;
return $this;
}
public function getCommand(): ?Command
{
return $this->command;
}
public function setCommand(Command $command): self
{
$this->command = $command;
return $this;
}
public function getLabel(): string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
/** UTF-8 text input; clears raw bytes input. */
public function setString(string $string): self
{
$this->inputString = $string;
$this->inputBytes = null;
return $this;
}
/** Raw binary input; clears string input. */
public function setBytes(string $bytes): self
{
$this->inputBytes = $bytes;
$this->inputString = null;
return $this;
}
public function getCompression(): bool
{
return $this->compression;
}
public function setCompression(bool $compression): self
{
$this->compression = $compression;
return $this;
}
public function getLanguage(): Language
{
return $this->language;
}
public function setLanguage(Language $language): self
{
$this->language = $language;
return $this;
}
public function getHighlight(): string|bool
{
return $this->highlight;
}
public function setHighlight(string|bool $highlight): self
{
$this->highlight = $highlight;
return $this;
}
public function getCmdMin(): int
{
return $this->cmdMin;
}
public function setCmdMin(int $cmdMin): self
{
$this->cmdMin = $cmdMin;
return $this;
}
public function getCmdMax(): int
{
return $this->cmdMax;
}
public function setCmdMax(int $cmdMax): self
{
$this->cmdMax = $cmdMax;
return $this;
}
public function getLocal(): bool
{
return $this->local;
}
public function setLocal(bool $local): self
{
$this->local = $local;
return $this;
}
public function getUnicode(): bool
{
return $this->unicode;
}
public function setUnicode(bool $unicode): self
{
$this->unicode = $unicode;
return $this;
}
public function getLangLocale(): string
{
return $this->langLocale;
}
public function setLangLocale(string $langLocale): self
{
$this->langLocale = $langLocale;
return $this;
}
public function getAnsiEncoding(): string
{
return $this->ansiEncoding;
}
public function setAnsiEncoding(string $ansiEncoding): self
{
$this->ansiEncoding = $ansiEncoding;
return $this;
}
public function getNewLines(): NewLine
{
return $this->newLines;
}
public function setNewLines(NewLine $newLines): self
{
$this->newLines = $newLines;
return $this;
}
public function getTemplate(): ?string
{
return $this->template;
}
public function setTemplate(?string $template): self
{
$this->template = $template;
return $this;
}
public function getReturnTemplate(): bool
{
return $this->returnTemplate;
}
public function setReturnTemplate(bool $returnTemplate): self
{
$this->returnTemplate = $returnTemplate;
return $this;
}
public function getIncludeTags(): bool
{
return $this->includeTags;
}
public function setIncludeTags(bool $includeTags): self
{
$this->includeTags = $includeTags;
return $this;
}
public function getIncludeExample(): bool
{
return $this->includeExample;
}
public function setIncludeExample(bool $includeExample): self
{
$this->includeExample = $includeExample;
return $this;
}
public function getIncludeDebugComments(): bool
{
return $this->includeDebugComments;
}
public function setIncludeDebugComments(bool $includeDebugComments): self
{
$this->includeDebugComments = $includeDebugComments;
return $this;
}
/**
* Reset request fields to defaults (reuse the same client for another call).
*/
public function reset(): self
{
$this->command = null;
$this->label = '$label';
$this->inputString = null;
$this->inputBytes = null;
$this->compression = false;
$this->language = Language::Php;
$this->highlight = false;
$this->cmdMin = 1;
$this->cmdMax = 3;
$this->local = false;
$this->unicode = true;
$this->langLocale = 'en_US.utf8';
$this->ansiEncoding = 'WINDOWS-1250';
$this->newLines = NewLine::Lf;
$this->template = null;
$this->returnTemplate = false;
$this->includeTags = false;
$this->includeExample = false;
$this->includeDebugComments = false;
return $this;
}
/**
* Build the POST body the client would send (for debugging and tests).
*
* @return array<string, mixed>
*/
public function toRequestArray(): array
{
if ($this->command === null) {
throw new InvalidArgumentException('Command must be set (use setCommand()).');
}
return match ($this->command) {
Command::Info => $this->buildInfoParams(),
Command::IsDemo => $this->buildIsDemoParams(),
Command::Encrypt => $this->buildEncryptParams(),
};
}
/**
* Send the request and return decoded JSON, or false on transport / JSON failure.
*
* @return array<string, mixed>|false
*/
public function send(?bool $useCurl = null): array|false
{
$useCurl = $useCurl ?? $this->preferCurl;
if ($useCurl && !function_exists('curl_init')) {
$useCurl = false;
}
$params = $this->toRequestArray();
$raw = $useCurl
? $this->postWithCurl(self::DEFAULT_API_URL, $params)
: $this->postWithStreams(self::DEFAULT_API_URL, $params);
if ($raw === false || $raw === '') {
return false;
}
$decoded = json_decode($raw, true);
if (!is_array($decoded)) {
return false;
}
return $this->applyDecryptorSourceDecompression($decoded);
}
/**
* Replace `source` with plain text when the request used compression (API returns
* base64(gzip(decryptor source))).
*
* @param array<string, mixed> $response
* @return array<string, mixed>
*/
private function applyDecryptorSourceDecompression(array $response): array
{
if ($this->command !== Command::Encrypt || !$this->compression || !$this->decompressEncryptSource) {
return $response;
}
if (($response['error'] ?? null) !== ErrorCode::SUCCESS) {
return $response;
}
if (!isset($response['source']) || !is_string($response['source'])) {
return $response;
}
$binary = base64_decode($response['source'], true);
if ($binary === false) {
return $response;
}
$plain = gzuncompress($binary);
if ($plain === false) {
return $response;
}
$response['source'] = $plain;
return $response;
}
/**
* @return array<string, string|int|bool>
*/
private function buildInfoParams(): array
{
return [
'command' => Command::Info->value,
'code' => $this->apiKey,
];
}
/**
* @return array<string, string|int|bool>
*/
private function buildIsDemoParams(): array
{
return [
'command' => Command::IsDemo->value,
'code' => $this->apiKey,
];
}
/**
* @return array<string, mixed>
*/
private function buildEncryptParams(): array
{
$p = [
'command' => Command::Encrypt->value,
'code' => $this->apiKey,
'label' => $this->label,
'compression' => $this->compression,
'lang' => $this->language->value,
'cmd_min' => $this->cmdMin,
'cmd_max' => $this->cmdMax,
'local' => $this->local,
'unicode' => $this->unicode,
'lang_locale' => $this->langLocale,
'ansi_encoding' => $this->ansiEncoding,
'new_lines' => $this->newLines->value,
'return_template' => $this->returnTemplate,
'include_tags' => $this->includeTags,
'include_example' => $this->includeExample,
'include_debug_comments' => $this->includeDebugComments,
];
if ($this->inputString !== null) {
$p['string'] = $this->inputString;
} elseif ($this->inputBytes !== null) {
$p['bytes'] = $this->inputBytes;
}
if ($this->highlight !== false) {
$p['highlight'] = $this->highlight;
}
if ($this->template !== null) {
$p['template'] = $this->template;
}
return $p;
}
/**
* @param array<string, mixed> $data
*/
private function postWithStreams(string $url, array $data): string|false
{
$body = http_build_query($data);
$params = [
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'content' => $body,
],
];
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if ($fp === false) {
return false;
}
try {
$response = stream_get_contents($fp);
} finally {
fclose($fp);
}
return $response !== false ? $response : false;
}
/**
* @param array<string, mixed> $data
*/
private function postWithCurl(string $url, array $data): string|false
{
$ch = curl_init($url);
if ($ch === false) {
return false;
}
$body = http_build_query($data);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'pelock/stringencrypt (+https://www.stringencrypt.com)',
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
]);
$result = curl_exec($ch);
curl_close($ch);
return $result !== false ? $result : false;
}
}