Skip to content

Commit

Permalink
[mms] Move Horde_Crypt_Pgp#parsePGPData() to separate class (Horde_Cr…
Browse files Browse the repository at this point in the history
…ypt_Pgp_Parse), since it can be used even if GnuPG binary is not available.
  • Loading branch information
slusarz authored and mrubinsk committed Nov 14, 2013
1 parent 088df62 commit 37412bd
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 73 deletions.
73 changes: 6 additions & 67 deletions framework/Crypt/lib/Horde/Crypt/Pgp.php
Expand Up @@ -30,43 +30,17 @@
class Horde_Crypt_Pgp extends Horde_Crypt
{
/**
* Armor Header Lines - From RFC 2440:
*
* An Armor Header Line consists of the appropriate header line text
* surrounded by five (5) dashes ('-', 0x2D) on either side of the header
* line text. The header line text is chosen based upon the type of data
* that is being encoded in Armor, and how it is being encoded.
*
* All Armor Header Lines are prefixed with 'PGP'.
*
* The Armor Tail Line is composed in the same manner as the Armor Header
* Line, except the string "BEGIN" is replaced by the string "END."
* @deprecated Use Horde_Crypt_Pgp_Parse instead.
*/

/* Used for signed, encrypted, or compressed files. */
const ARMOR_MESSAGE = 1;

/* Used for signed files. */
const ARMOR_SIGNED_MESSAGE = 2;

/* Used for armoring public keys. */
const ARMOR_PUBLIC_KEY = 3;

/* Used for armoring private keys. */
const ARMOR_PRIVATE_KEY = 4;

/* Used for detached signatures, PGP/MIME signatures, and natures
* following clearsigned messages. */
const ARMOR_SIGNATURE = 5;

/* Regular text contained in an PGP message. */
const ARMOR_TEXT = 6;

/**
* Strings in armor header lines used to distinguish between the different
* types of PGP decryption/encryption.
*
* @var array
* @deprecated Use Horde_Crypt_Pgp_Parse instead.
*/
protected $_armor = array(
'MESSAGE' => self::ARMOR_MESSAGE,
Expand Down Expand Up @@ -693,6 +667,8 @@ public function verifyPassphrase($public_key, $private_key, $passphrase)
/**
* Parses a message into text and PGP components.
*
* @deprecated Use Horde_Crypt_Pgp_Parse instead.
*
* @param mixed $text Either the text to parse or a Horde_Stream object
* (@since 2.3.0).
*
Expand All @@ -707,45 +683,8 @@ public function verifyPassphrase($public_key, $private_key, $passphrase)
*/
public function parsePGPData($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 (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;
$parse = new Horde_Crypt_Pgp_Parse();
return $parse->parse();
}

/**
Expand Down
130 changes: 130 additions & 0 deletions framework/Crypt/lib/Horde/Crypt/Pgp/Parse.php
@@ -0,0 +1,130 @@
<?php
/**
* Copyright 2002-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2002-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Crypt
*/

/**
* Provides method to parse PGP armored text data.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2002-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Crypt
* @since 2.4.0
*/
class Horde_Crypt_Pgp_Parse
{
/**
* Armor Header Lines - From RFC 2440:
*
* An Armor Header Line consists of the appropriate header line text
* surrounded by five (5) dashes ('-', 0x2D) on either side of the header
* line text. The header line text is chosen based upon the type of data
* that is being encoded in Armor, and how it is being encoded.
*
* All Armor Header Lines are prefixed with 'PGP'.
*
* The Armor Tail Line is composed in the same manner as the Armor Header
* Line, except the string "BEGIN" is replaced by the string "END."
*/

/* Used for signed, encrypted, or compressed files. */
const ARMOR_MESSAGE = 1;

/* Used for signed files. */
const ARMOR_SIGNED_MESSAGE = 2;

/* Used for armoring public keys. */
const ARMOR_PUBLIC_KEY = 3;

/* Used for armoring private keys. */
const ARMOR_PRIVATE_KEY = 4;

/* Used for detached signatures, PGP/MIME signatures, and natures
* following clearsigned messages. */
const ARMOR_SIGNATURE = 5;

/* Regular text contained in an PGP message. */
const ARMOR_TEXT = 6;

/**
* Strings in armor header lines used to distinguish between the different
* types of PGP decryption/encryption.
*
* @var array
*/
protected $_armor = array(
'MESSAGE' => self::ARMOR_MESSAGE,
'SIGNED MESSAGE' => self::ARMOR_SIGNED_MESSAGE,
'PUBLIC KEY BLOCK' => self::ARMOR_PUBLIC_KEY,
'PRIVATE KEY BLOCK' => self::ARMOR_PRIVATE_KEY,
'SIGNATURE' => self::ARMOR_SIGNATURE
);

/**
* Parses a message into text and PGP components.
*
* @param mixed $text 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:
* <pre>
* - 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.
* </pre>
*/
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 (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;
}

}
16 changes: 10 additions & 6 deletions framework/Crypt/package.xml
Expand Up @@ -18,16 +18,16 @@
</lead>
<date>2013-11-12</date>
<version>
<release>2.3.1</release>
<api>2.3.0</api>
<release>2.4.0</release>
<api>2.4.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Move Horde_Crypt_Pgp#parsePGPData() to separate class (Horde_Crypt_Pgp_Parse), since it can be used even if GnuPG binary is not available.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand All @@ -41,6 +41,9 @@
<dir name="lib">
<dir name="Horde">
<dir name="Crypt">
<dir name="Pgp">
<file name="Parse.php" role="php" />
</dir> <!-- /lib/Horde/Crypt/Pgp -->
<file name="Exception.php" role="php" />
<file name="Pgp.php" role="php" />
<file name="Smime.php" role="php" />
Expand Down Expand Up @@ -414,6 +417,7 @@
<install as="Horde/Crypt/Pgp.php" name="lib/Horde/Crypt/Pgp.php" />
<install as="Horde/Crypt/Smime.php" name="lib/Horde/Crypt/Smime.php" />
<install as="Horde/Crypt/Translation.php" name="lib/Horde/Crypt/Translation.php" />
<install as="Horde/Crypt/Pgp/Parse.php" name="lib/Horde/Crypt/Pgp/Parse.php" />
<install as="locale/Horde_Crypt.pot" name="locale/Horde_Crypt.pot" />
<install as="locale/ar/LC_MESSAGES/Horde_Crypt.mo" name="locale/ar/LC_MESSAGES/Horde_Crypt.mo" />
<install as="locale/ar/LC_MESSAGES/Horde_Crypt.po" name="locale/ar/LC_MESSAGES/Horde_Crypt.po" />
Expand Down Expand Up @@ -944,15 +948,15 @@ Initial release as a PEAR package
</release>
<release>
<version>
<release>2.3.1</release>
<api>2.3.0</api></version>
<release>2.4.0</release>
<api>2.4.0</api></version>
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2013-11-12</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Move Horde_Crypt_Pgp#parsePGPData() to separate class (Horde_Crypt_Pgp_Parse), since it can be used even if GnuPG binary is not available.
</notes>
</release>
</changelog>
Expand Down

0 comments on commit 37412bd

Please sign in to comment.