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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
|
<?php
namespace Sabre\VObject;
use InvalidArgumentException;
/**
* This is the CLI interface for sabre-vobject.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class Cli
{
/**
* No output.
*
* @var bool
*/
protected $quiet = false;
/**
* Help display.
*
* @var bool
*/
protected $showHelp = false;
/**
* Whether to spit out 'mimedir' or 'json' format.
*
* @var string
*/
protected $format;
/**
* JSON pretty print.
*
* @var bool
*/
protected $pretty;
/**
* Source file.
*
* @var string
*/
protected $inputPath;
/**
* Destination file.
*
* @var string
*/
protected $outputPath;
/**
* output stream.
*
* @var resource
*/
protected $stdout;
/**
* stdin.
*
* @var resource
*/
protected $stdin;
/**
* stderr.
*
* @var resource
*/
protected $stderr;
/**
* Input format (one of json or mimedir).
*
* @var string
*/
protected $inputFormat;
/**
* Makes the parser less strict.
*
* @var bool
*/
protected $forgiving = false;
/**
* Main function.
*
* @return int
*/
public function main(array $argv)
{
// @codeCoverageIgnoreStart
// We cannot easily test this, so we'll skip it. Pretty basic anyway.
if (!$this->stderr) {
$this->stderr = fopen('php://stderr', 'w');
}
if (!$this->stdout) {
$this->stdout = fopen('php://stdout', 'w');
}
if (!$this->stdin) {
$this->stdin = fopen('php://stdin', 'r');
}
// @codeCoverageIgnoreEnd
try {
list($options, $positional) = $this->parseArguments($argv);
if (isset($options['q'])) {
$this->quiet = true;
}
$this->log($this->colorize('green', 'sabre/vobject ').$this->colorize('yellow', Version::VERSION));
foreach ($options as $name => $value) {
switch ($name) {
case 'q':
// Already handled earlier.
break;
case 'h':
case 'help':
$this->showHelp();
return 0;
break;
case 'format':
switch ($value) {
// jcard/jcal documents
case 'jcard':
case 'jcal':
// specific document versions
case 'vcard21':
case 'vcard30':
case 'vcard40':
case 'icalendar20':
// specific formats
case 'json':
case 'mimedir':
// icalendar/vcad
case 'icalendar':
case 'vcard':
$this->format = $value;
break;
default:
throw new InvalidArgumentException('Unknown format: '.$value);
}
break;
case 'pretty':
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$this->pretty = true;
}
break;
case 'forgiving':
$this->forgiving = true;
break;
case 'inputformat':
switch ($value) {
// json formats
case 'jcard':
case 'jcal':
case 'json':
$this->inputFormat = 'json';
break;
// mimedir formats
case 'mimedir':
case 'icalendar':
case 'vcard':
case 'vcard21':
case 'vcard30':
case 'vcard40':
case 'icalendar20':
$this->inputFormat = 'mimedir';
break;
default:
throw new InvalidArgumentException('Unknown format: '.$value);
}
break;
default:
throw new InvalidArgumentException('Unknown option: '.$name);
}
}
if (0 === count($positional)) {
$this->showHelp();
return 1;
}
if (1 === count($positional)) {
throw new InvalidArgumentException('Inputfile is a required argument');
}
if (count($positional) > 3) {
throw new InvalidArgumentException('Too many arguments');
}
if (!in_array($positional[0], ['validate', 'repair', 'convert', 'color'])) {
throw new InvalidArgumentException('Unknown command: '.$positional[0]);
}
} catch (InvalidArgumentException $e) {
$this->showHelp();
$this->log('Error: '.$e->getMessage(), 'red');
return 1;
}
$command = $positional[0];
$this->inputPath = $positional[1];
$this->outputPath = isset($positional[2]) ? $positional[2] : '-';
if ('-' !== $this->outputPath) {
$this->stdout = fopen($this->outputPath, 'w');
}
if (!$this->inputFormat) {
if ('.json' === substr($this->inputPath, -5)) {
$this->inputFormat = 'json';
} else {
$this->inputFormat = 'mimedir';
}
}
if (!$this->format) {
if ('.json' === substr($this->outputPath, -5)) {
$this->format = 'json';
} else {
$this->format = 'mimedir';
}
}
$realCode = 0;
try {
while ($input = $this->readInput()) {
$returnCode = $this->$command($input);
if (0 !== $returnCode) {
$realCode = $returnCode;
}
}
} catch (EofException $e) {
// end of file
} catch (\Exception $e) {
$this->log('Error: '.$e->getMessage(), 'red');
return 2;
}
return $realCode;
}
/**
* Shows the help message.
*/
protected function showHelp()
{
$this->log('Usage:', 'yellow');
$this->log(' vobject [options] command [arguments]');
$this->log('');
$this->log('Options:', 'yellow');
$this->log($this->colorize('green', ' -q ')."Don't output anything.");
$this->log($this->colorize('green', ' -help -h ').'Display this help message.');
$this->log($this->colorize('green', ' --format ').'Convert to a specific format. Must be one of: vcard, vcard21,');
$this->log($this->colorize('green', ' --forgiving ').'Makes the parser less strict.');
$this->log(' vcard30, vcard40, icalendar20, jcal, jcard, json, mimedir.');
$this->log($this->colorize('green', ' --inputformat ').'If the input format cannot be guessed from the extension, it');
$this->log(' must be specified here.');
// Only PHP 5.4 and up
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$this->log($this->colorize('green', ' --pretty ').'json pretty-print.');
}
$this->log('');
$this->log('Commands:', 'yellow');
$this->log($this->colorize('green', ' validate').' source_file Validates a file for correctness.');
$this->log($this->colorize('green', ' repair').' source_file [output_file] Repairs a file.');
$this->log($this->colorize('green', ' convert').' source_file [output_file] Converts a file.');
$this->log($this->colorize('green', ' color').' source_file Colorize a file, useful for debugging.');
$this->log(
<<<HELP
If source_file is set as '-', STDIN will be used.
If output_file is omitted, STDOUT will be used.
All other output is sent to STDERR.
HELP
);
$this->log('Examples:', 'yellow');
$this->log(' vobject convert contact.vcf contact.json');
$this->log(' vobject convert --format=vcard40 old.vcf new.vcf');
$this->log(' vobject convert --inputformat=json --format=mimedir - -');
$this->log(' vobject color calendar.ics');
$this->log('');
$this->log('https://github.com/fruux/sabre-vobject', 'purple');
}
/**
* Validates a VObject file.
*
* @return int
*/
protected function validate(Component $vObj)
{
$returnCode = 0;
switch ($vObj->name) {
case 'VCALENDAR':
$this->log('iCalendar: '.(string) $vObj->VERSION);
break;
case 'VCARD':
$this->log('vCard: '.(string) $vObj->VERSION);
break;
}
$warnings = $vObj->validate();
if (!count($warnings)) {
$this->log(' No warnings!');
} else {
$levels = [
1 => 'REPAIRED',
2 => 'WARNING',
3 => 'ERROR',
];
$returnCode = 2;
foreach ($warnings as $warn) {
$extra = '';
if ($warn['node'] instanceof Property) {
$extra = ' (property: "'.$warn['node']->name.'")';
}
$this->log(' ['.$levels[$warn['level']].'] '.$warn['message'].$extra);
}
}
return $returnCode;
}
/**
* Repairs a VObject file.
*
* @return int
*/
protected function repair(Component $vObj)
{
$returnCode = 0;
switch ($vObj->name) {
case 'VCALENDAR':
$this->log('iCalendar: '.(string) $vObj->VERSION);
break;
case 'VCARD':
$this->log('vCard: '.(string) $vObj->VERSION);
break;
}
$warnings = $vObj->validate(Node::REPAIR);
if (!count($warnings)) {
$this->log(' No warnings!');
} else {
$levels = [
1 => 'REPAIRED',
2 => 'WARNING',
3 => 'ERROR',
];
$returnCode = 2;
foreach ($warnings as $warn) {
$extra = '';
if ($warn['node'] instanceof Property) {
$extra = ' (property: "'.$warn['node']->name.'")';
}
$this->log(' ['.$levels[$warn['level']].'] '.$warn['message'].$extra);
}
}
fwrite($this->stdout, $vObj->serialize());
return $returnCode;
}
/**
* Converts a vObject file to a new format.
*
* @param Component $vObj
*
* @return int
*/
protected function convert($vObj)
{
$json = false;
$convertVersion = null;
$forceInput = null;
switch ($this->format) {
case 'json':
$json = true;
if ('VCARD' === $vObj->name) {
$convertVersion = Document::VCARD40;
}
break;
case 'jcard':
$json = true;
$forceInput = 'VCARD';
$convertVersion = Document::VCARD40;
break;
case 'jcal':
$json = true;
$forceInput = 'VCALENDAR';
break;
case 'mimedir':
case 'icalendar':
case 'icalendar20':
case 'vcard':
break;
case 'vcard21':
$convertVersion = Document::VCARD21;
break;
case 'vcard30':
$convertVersion = Document::VCARD30;
break;
case 'vcard40':
$convertVersion = Document::VCARD40;
break;
}
if ($forceInput && $vObj->name !== $forceInput) {
throw new \Exception('You cannot convert a '.strtolower($vObj->name).' to '.$this->format);
}
if ($convertVersion) {
$vObj = $vObj->convert($convertVersion);
}
if ($json) {
$jsonOptions = 0;
if ($this->pretty) {
$jsonOptions = JSON_PRETTY_PRINT;
}
fwrite($this->stdout, json_encode($vObj->jsonSerialize(), $jsonOptions));
} else {
fwrite($this->stdout, $vObj->serialize());
}
return 0;
}
/**
* Colorizes a file.
*
* @param Component $vObj
*/
protected function color($vObj)
{
$this->serializeComponent($vObj);
}
/**
* Returns an ansi color string for a color name.
*
* @param string $color
*
* @return string
*/
protected function colorize($color, $str, $resetTo = 'default')
{
$colors = [
'cyan' => '1;36',
'red' => '1;31',
'yellow' => '1;33',
'blue' => '0;34',
'green' => '0;32',
'default' => '0',
'purple' => '0;35',
];
return "\033[".$colors[$color].'m'.$str."\033[".$colors[$resetTo].'m';
}
/**
* Writes out a string in specific color.
*
* @param string $color
* @param string $str
*/
protected function cWrite($color, $str)
{
fwrite($this->stdout, $this->colorize($color, $str));
}
protected function serializeComponent(Component $vObj)
{
$this->cWrite('cyan', 'BEGIN');
$this->cWrite('red', ':');
$this->cWrite('yellow', $vObj->name."\n");
/**
* Gives a component a 'score' for sorting purposes.
*
* This is solely used by the childrenSort method.
*
* A higher score means the item will be lower in the list.
* To avoid score collisions, each "score category" has a reasonable
* space to accommodate elements. The $key is added to the $score to
* preserve the original relative order of elements.
*
* @param int $key
* @param array $array
*
* @return int
*/
$sortScore = function ($key, $array) {
if ($array[$key] instanceof Component) {
// We want to encode VTIMEZONE first, this is a personal
// preference.
if ('VTIMEZONE' === $array[$key]->name) {
$score = 300000000;
return $score + $key;
} else {
$score = 400000000;
return $score + $key;
}
} else {
// Properties get encoded first
// VCARD version 4.0 wants the VERSION property to appear first
if ($array[$key] instanceof Property) {
if ('VERSION' === $array[$key]->name) {
$score = 100000000;
return $score + $key;
} else {
// All other properties
$score = 200000000;
return $score + $key;
}
}
}
};
$children = $vObj->children();
$tmp = $children;
uksort(
$children,
function ($a, $b) use ($sortScore, $tmp) {
$sA = $sortScore($a, $tmp);
$sB = $sortScore($b, $tmp);
return $sA - $sB;
}
);
foreach ($children as $child) {
if ($child instanceof Component) {
$this->serializeComponent($child);
} else {
$this->serializeProperty($child);
}
}
$this->cWrite('cyan', 'END');
$this->cWrite('red', ':');
$this->cWrite('yellow', $vObj->name."\n");
}
/**
* Colorizes a property.
*/
protected function serializeProperty(Property $property)
{
if ($property->group) {
$this->cWrite('default', $property->group);
$this->cWrite('red', '.');
}
$this->cWrite('yellow', $property->name);
foreach ($property->parameters as $param) {
$this->cWrite('red', ';');
$this->cWrite('blue', $param->serialize());
}
$this->cWrite('red', ':');
if ($property instanceof Property\Binary) {
$this->cWrite('default', 'embedded binary stripped. ('.strlen($property->getValue()).' bytes)');
} else {
$parts = $property->getParts();
$first1 = true;
// Looping through property values
foreach ($parts as $part) {
if ($first1) {
$first1 = false;
} else {
$this->cWrite('red', $property->delimiter);
}
$first2 = true;
// Looping through property sub-values
foreach ((array) $part as $subPart) {
if ($first2) {
$first2 = false;
} else {
// The sub-value delimiter is always comma
$this->cWrite('red', ',');
}
$subPart = strtr(
$subPart,
[
'\\' => $this->colorize('purple', '\\\\', 'green'),
';' => $this->colorize('purple', '\;', 'green'),
',' => $this->colorize('purple', '\,', 'green'),
"\n" => $this->colorize('purple', "\\n\n\t", 'green'),
"\r" => '',
]
);
$this->cWrite('green', $subPart);
}
}
}
$this->cWrite('default', "\n");
}
/**
* Parses the list of arguments.
*/
protected function parseArguments(array $argv)
{
$positional = [];
$options = [];
for ($ii = 0; $ii < count($argv); ++$ii) {
// Skipping the first argument.
if (0 === $ii) {
continue;
}
$v = $argv[$ii];
if ('--' === substr($v, 0, 2)) {
// This is a long-form option.
$optionName = substr($v, 2);
$optionValue = true;
if (strpos($optionName, '=')) {
list($optionName, $optionValue) = explode('=', $optionName);
}
$options[$optionName] = $optionValue;
} elseif ('-' === substr($v, 0, 1) && strlen($v) > 1) {
// This is a short-form option.
foreach (str_split(substr($v, 1)) as $option) {
$options[$option] = true;
}
} else {
$positional[] = $v;
}
}
return [$options, $positional];
}
protected $parser;
/**
* Reads the input file.
*
* @return Component
*/
protected function readInput()
{
if (!$this->parser) {
if ('-' !== $this->inputPath) {
$this->stdin = fopen($this->inputPath, 'r');
}
if ('mimedir' === $this->inputFormat) {
$this->parser = new Parser\MimeDir($this->stdin, ($this->forgiving ? Reader::OPTION_FORGIVING : 0));
} else {
$this->parser = new Parser\Json($this->stdin, ($this->forgiving ? Reader::OPTION_FORGIVING : 0));
}
}
return $this->parser->parse();
}
/**
* Sends a message to STDERR.
*
* @param string $msg
*/
protected function log($msg, $color = 'default')
{
if (!$this->quiet) {
if ('default' !== $color) {
$msg = $this->colorize($color, $msg);
}
fwrite($this->stderr, $msg."\n");
}
}
}
|