123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616 |
- <?php
- namespace Sabre\VObject\Parser;
- use
- Sabre\VObject\ParseException,
- Sabre\VObject\EofException,
- Sabre\VObject\Component,
- Sabre\VObject\Property,
- Sabre\VObject\Component\VCalendar,
- Sabre\VObject\Component\VCard;
- class MimeDir extends Parser {
-
- protected $input;
-
- protected $root;
-
- public function parse($input = null, $options = null) {
- $this->root = null;
- if (!is_null($input)) {
- $this->setInput($input);
- }
- if (!is_null($options)) $this->options = $options;
- $this->parseDocument();
- return $this->root;
- }
-
- public function setInput($input) {
-
- $this->lineIndex = 0;
- $this->startLine = 0;
- if (is_string($input)) {
-
- $stream = fopen('php://temp', 'r+');
- fwrite($stream, $input);
- rewind($stream);
- $this->input = $stream;
- } elseif (is_resource($input)) {
- $this->input = $input;
- } else {
- throw new \InvalidArgumentException('This parser can only read from strings or streams.');
- }
- }
-
- protected function parseDocument() {
- $line = $this->readLine();
- switch(strtoupper($line)) {
- case 'BEGIN:VCALENDAR' :
- $class = isset(VCalendar::$componentMap['VCALENDAR'])
- ? VCalendar::$componentMap[$name]
- : 'Sabre\\VObject\\Component\\VCalendar';
- break;
- case 'BEGIN:VCARD' :
- $class = isset(VCard::$componentMap['VCARD'])
- ? VCard::$componentMap['VCARD']
- : 'Sabre\\VObject\\Component\\VCard';
- break;
- default :
- throw new ParseException('This parser only supports VCARD and VCALENDAR files');
- }
- $this->root = new $class(array(), false);
- while(true) {
-
- $line = $this->readLine();
- if (strtoupper(substr($line,0,4)) === 'END:') {
- break;
- }
- $result = $this->parseLine($line);
- if ($result) {
- $this->root->add($result);
- }
- }
- $name = strtoupper(substr($line, 4));
- if ($name!==$this->root->name) {
- throw new ParseException('Invalid MimeDir file. expected: "END:' . $this->root->name . '" got: "END:' . $name . '"');
- }
- }
-
- protected function parseLine($line) {
-
- if (strtoupper(substr($line, 0, 6)) === 'BEGIN:') {
- $component = $this->root->createComponent(substr($line,6), array(), false);
- while(true) {
-
- $line = $this->readLine();
- if (strtoupper(substr($line,0,4)) === 'END:') {
- break;
- }
- $result = $this->parseLine($line);
- if ($result) {
- $component->add($result);
- }
- }
- $name = strtoupper(substr($line, 4));
- if ($name!==$component->name) {
- throw new ParseException('Invalid MimeDir file. expected: "END:' . $component->name . '" got: "END:' . $name . '"');
- }
- return $component;
- } else {
-
- $property = $this->readProperty($line);
- if (!$property) {
-
- return false;
- }
- return $property;
- }
- }
-
- protected $lineBuffer;
-
- protected $lineIndex = 0;
-
- protected $startLine = 0;
-
- protected $rawLine;
-
- protected function readLine() {
- if (!is_null($this->lineBuffer)) {
- $rawLine = $this->lineBuffer;
- $this->lineBuffer = null;
- } else {
- do {
- $eof = feof($this->input);
- $rawLine = fgets($this->input);
- if ($eof || (feof($this->input) && $rawLine===false)) {
- throw new EofException('End of document reached prematurely');
- }
- if ($rawLine === false) {
- throw new ParseException('Error reading from input stream');
- }
- $rawLine = rtrim($rawLine, "\r\n");
- } while ($rawLine === '');
- $this->lineIndex++;
- }
- $line = $rawLine;
- $this->startLine = $this->lineIndex;
-
- while (true) {
- $nextLine = rtrim(fgets($this->input), "\r\n");
- $this->lineIndex++;
- if (!$nextLine) {
- break;
- }
- if ($nextLine[0] === "\t" || $nextLine[0] === " ") {
- $line .= substr($nextLine, 1);
- $rawLine .= "\n " . substr($nextLine, 1);
- } else {
- $this->lineBuffer = $nextLine;
- break;
- }
- }
- $this->rawLine = $rawLine;
- return $line;
- }
-
- protected function readProperty($line) {
- if ($this->options & self::OPTION_FORGIVING) {
- $propNameToken = 'A-Z0-9\-\._\\/';
- } else {
- $propNameToken = 'A-Z0-9\-\.';
- }
- $paramNameToken = 'A-Z0-9\-';
- $safeChar = '^";:,';
- $qSafeChar = '^"';
- $regex = "/
- ^(?P<name> [$propNameToken]+ ) (?=[;:]) # property name
- |
- (?<=:)(?P<propValue> .+)$ # property value
- |
- ;(?P<paramName> [$paramNameToken]+) (?=[=;:]) # parameter name
- |
- (=|,)(?P<paramValue> # parameter value
- (?: [$safeChar]*) |
- \"(?: [$qSafeChar]+)\"
- ) (?=[;:,])
- /xi";
-
- preg_match_all($regex, $line, $matches, PREG_SET_ORDER);
- $property = array(
- 'name' => null,
- 'parameters' => array(),
- 'value' => null
- );
- $lastParam = null;
-
- foreach($matches as $match) {
- if (isset($match['paramValue'])) {
- if ($match['paramValue'] && $match['paramValue'][0] === '"') {
- $value = substr($match['paramValue'], 1, -1);
- } else {
- $value = $match['paramValue'];
- }
- $value = $this->unescapeParam($value);
- if (is_null($property['parameters'][$lastParam])) {
- $property['parameters'][$lastParam] = $value;
- } elseif (is_array($property['parameters'][$lastParam])) {
- $property['parameters'][$lastParam][] = $value;
- } else {
- $property['parameters'][$lastParam] = array(
- $property['parameters'][$lastParam],
- $value
- );
- }
- continue;
- }
- if (isset($match['paramName'])) {
- $lastParam = strtoupper($match['paramName']);
- if (!isset($property['parameters'][$lastParam])) {
- $property['parameters'][$lastParam] = null;
- }
- continue;
- }
- if (isset($match['propValue'])) {
- $property['value'] = $match['propValue'];
- continue;
- }
- if (isset($match['name']) && $match['name']) {
- $property['name'] = strtoupper($match['name']);
- continue;
- }
-
- throw new \LogicException('This code should not be reachable');
-
- }
- if (is_null($property['value'])) {
- $property['value'] = '';
- }
- if (!$property['name']) {
- if ($this->options & self::OPTION_IGNORE_INVALID_LINES) {
- return false;
- }
- throw new ParseException('Invalid Mimedir file. Line starting at ' . $this->startLine . ' did not follow iCalendar/vCard conventions');
- }
-
-
-
-
-
- $namedParameters = array();
- $namelessParameters = array();
- foreach($property['parameters'] as $name=>$value) {
- if (!is_null($value)) {
- $namedParameters[$name] = $value;
- } else {
- $namelessParameters[] = $name;
- }
- }
- $propObj = $this->root->createProperty($property['name'], null, $namedParameters);
- foreach($namelessParameters as $namelessParameter) {
- $propObj->add(null, $namelessParameter);
- }
- if (strtoupper($propObj['ENCODING']) === 'QUOTED-PRINTABLE') {
- $propObj->setQuotedPrintableValue($this->extractQuotedPrintableValue());
- } else {
- $propObj->setRawMimeDirValue($property['value']);
- }
- return $propObj;
- }
-
- static public function unescapeValue($input, $delimiter = ';') {
- $regex = '# (?: (\\\\ (?: \\\\ | N | n | ; | , ) )';
- if ($delimiter) {
- $regex .= ' | (' . $delimiter . ')';
- }
- $regex .= ') #x';
- $matches = preg_split($regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
- $resultArray = array();
- $result = '';
- foreach($matches as $match) {
- switch ($match) {
- case '\\\\' :
- $result .='\\';
- break;
- case '\N' :
- case '\n' :
- $result .="\n";
- break;
- case '\;' :
- $result .=';';
- break;
- case '\,' :
- $result .=',';
- break;
- case $delimiter :
- $resultArray[] = $result;
- $result = '';
- break;
- default :
- $result .= $match;
- break;
- }
- }
- $resultArray[] = $result;
- return $delimiter ? $resultArray : $result;
- }
-
- private function unescapeParam($input) {
- return
- preg_replace_callback(
- '#(\^(\^|n|\'))#',
- function($matches) {
- switch($matches[2]) {
- case 'n' :
- return "\n";
- case '^' :
- return '^';
- case '\'' :
- return '"';
-
- }
-
- },
- $input
- );
- }
-
- private function extractQuotedPrintableValue() {
-
-
-
-
- $regex = '/^
- (?: [^:])+ # Anything but a colon
- (?: "[^"]")* # A parameter in double quotes
- : # start of the value we really care about
- (.*)$
- /xs';
- preg_match($regex, $this->rawLine, $matches);
- $value = $matches[1];
-
-
- $value = str_replace("\n ", "\n", $value);
-
-
-
- if ($this->options & self::OPTION_FORGIVING) {
- while(substr($value,-1) === '=') {
-
- $this->readLine();
-
- $value.="\n" . $this->rawLine;
- }
- }
- return $value;
- }
- }
|