Horde_Crypt_Pgp_Parse::parse PHP Method

parse() public method

Parses a message into text and PGP components.
public parse ( mixed $text ) : array
$text mixed Either the text to parse or a Horde_Stream object.
return array An array with the parsed text, returned in blocks of text corresponding to their actual order. Keys:
  - data: (array) The data for each section. Each line has been
          stripped of EOL characters.
  - type: (integer) The type of data contained in block. Valid types
          are the class ARMOR_* constants.
    public function parse($text)
    {
        $data = array();
        $temp = array('type' => self::ARMOR_TEXT);
        if ($text instanceof Horde_Stream) {
            $stream = $text;
            $stream->rewind();
        } else {
            $stream = new Horde_Stream_Temp();
            $stream->add($text, true);
        }
        while (!$stream->eof()) {
            $val = rtrim($stream->getToChar("\n", false), "\r");
            if (strpos($val, '-----') === 0 && preg_match('/^-----(BEGIN|END) PGP ([^-]+)-----\\s*$/', $val, $matches)) {
                if (isset($temp['data'])) {
                    $data[] = $temp;
                }
                $temp = array();
                if ($matches[1] == 'BEGIN') {
                    $temp['type'] = $this->_armor[$matches[2]];
                    $temp['data'][] = $val;
                } elseif ($matches[1] == 'END') {
                    $temp['type'] = self::ARMOR_TEXT;
                    $data[count($data) - 1]['data'][] = $val;
                }
            } else {
                $temp['data'][] = $val;
            }
        }
        if (isset($temp['data']) && (count($temp['data']) > 1 || !empty($temp['data'][0]))) {
            $data[] = $temp;
        }
        return $data;
    }

Usage Example

Example #1
0
 /**
  * Return the full rendered version of the Horde_Mime_Part object.
  *
  * @return array  See parent::render().
  */
 protected function _renderRaw()
 {
     $ret = array('data' => '', 'type' => 'text/plain; charset=' . $this->getConfigParam('charset'));
     switch ($this->_mimepart->getType()) {
         case 'application/pgp-signature':
             $parse = new Horde_Crypt_Pgp_Parse();
             $parts = $parse->parse($this->_mimepart->getContents());
             foreach (array_keys($parts) as $key) {
                 if ($parts[$key]['type'] == Horde_Crypt_Pgp::ARMOR_SIGNATURE) {
                     $ret['data'] = implode("\r\n", $parts[$key]['data']);
                     break;
                 }
             }
             break;
     }
     return array($this->_mimepart->getMimeId() => $ret);
 }
All Usage Examples Of Horde_Crypt_Pgp_Parse::parse
Horde_Crypt_Pgp_Parse