aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/vobject/bin/vobjectvalidate.php
blob: e0b2a479ff8ee9b4b4207d3ae1ca29de1926d1fe (plain) (blame)
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
#!/usr/bin/env php
<?php

namespace Sabre\VObject;

// This sucks.. we have to try to find the composer autoloader. But chances
// are, we can't find it this way. So we'll do our bestest
$paths = array(
    __DIR__ . '/../vendor/autoload.php',  // In case vobject is cloned directly
    __DIR__ . '/../../../autoload.php',   // In case vobject is a composer dependency.
);

foreach($paths as $path) {
    if (file_exists($path)) {
        include $path;
        break;
    }
}

if (!class_exists('Sabre\\VObject\\Version')) {
    fwrite(STDERR, "Composer autoloader could not be properly loaded.\n");
    die(1);
}

fwrite(STDERR, "SabreTooth VObject validator " . Version::VERSION . "\n");


$repair = false;
$posArgs = array();

// Argument parsing:
foreach($argv as $k=>$v) {

    if ($k===0) {
        continue;
    }
    if (substr($v,0,2)==='--') {
        switch($v) {
            case '--repair' :
                $repair = true;
                break;
            default :
                throw new InvalidArgumentException('Unknown option: ' . $v);
                break;
        }
        continue;
    }
    $posArgs[] = $v;

}

function help() {

    global $argv;

    fwrite(STDERR, <<<HELP
Usage instructions:

  {$argv[0]} [--repair] inputfile [outputfile]

  inputfile   Input .vcf or .ics file.
  outputfile  Output .vcf or .ics file. This is only used with --repair.
  --repair    Attempt to automatically repair broken files.

For both the output- and inputfile "-" can be specified, to use STDIN and STDOUT
respectively.

All other output from this script is sent to STDERR.

https://github.com/fruux/sabre-vobject

HELP
);

}

if (count($posArgs) < 1) {
    help();
    die();
}

if ($posArgs[0]!=='-') {
    $input = fopen($posArgs[0],'r');
} else {
    $input = STDIN;
}

if (isset($posArgs[1]) && $posArgs[1]!=='-') {
    $output = fopen($posArgs[1],'w');
} else {
    $output = STDOUT;
}

// This is a bit of a hack to easily support multiple objects in a single file.
$inputStr = "BEGIN:X-SABRE-WRAPPER\r\n" . stream_get_contents($input);

$inputStr = rtrim($inputStr, "\r\n") . "\r\nEND:X-SABRE-WRAPPER\r\n";

// Now the actual work.
$vObj = Reader::read($inputStr);

$objects = $vObj->children();

foreach($objects as $child) {

    switch($child->name) {
        case 'VCALENDAR' :
            fwrite(STDERR, "iCalendar: " . (string)$child->VERSION . "\n");
            break;
        case 'VCARD' :
            fwrite(STDERR, "vCard: " . (string)$child->VERSION . "\n");
            break;
        default :
            fwrite(STDERR, "This was an unknown object, but it did parse. It's likely that validation will give you unexpected results.\n");
            break;
    }

    $options = 0;
    if ($repair) $options |= Node::REPAIR;

    $warnings = $child->validate($options);

    if (!count($warnings)) {
        fwrite(STDERR, "[GOOD NEWS] No warnings!\n");
    } else {
        foreach($warnings as $warn) {

            fwrite(STDERR, $warn['message'] . "\n");

        }

    }

    if ($repair) {
        fwrite($output, $child->serialize());
    }

}