Beispiel #1
0
 /**
  * Imports a public or private key into the keyring
  *
  * @param string  $key    the key to be imported.
  * @param boolean $isFile whether or not the input is a filename.
  *
  * @return array an associative array containing the following elements:
  *               - <kbd>fingerprint</kbd>       - the fingerprint of the
  *                                                imported key,
  *               - <kbd>public_imported</kbd>   - the number of public
  *                                                keys imported,
  *               - <kbd>public_unchanged</kbd>  - the number of unchanged
  *                                                public keys,
  *               - <kbd>private_imported</kbd>  - the number of private
  *                                                keys imported,
  *               - <kbd>private_unchanged</kbd> - the number of unchanged
  *                                                private keys.
  *
  * @throws Crypt_GPG_NoDataException if the key data is missing or if the
  *         data is is not valid key data.
  *
  * @throws Crypt_GPG_FileException if the key file is not readable.
  *
  * @throws Crypt_GPG_Exception if an unknown or unexpected error occurs.
  *         Use the <kbd>debug</kbd> option and file a bug report if these
  *         exceptions occur.
  */
 protected function _importKey($key, $isFile)
 {
     $result = array();
     if ($isFile) {
         $input = @fopen($key, 'rb');
         if ($input === false) {
             throw new Crypt_GPG_FileException('Could not open key file "' . $key . '" for importing.', 0, $key);
         }
     } else {
         $input = strval($key);
         if ($input == '') {
             throw new Crypt_GPG_NoDataException('No valid GPG key data found.', self::ERROR_NO_DATA);
         }
     }
     $arguments = array();
     $version = $this->engine->getVersion();
     if (version_compare($version, '1.0.5', 'ge') && version_compare($version, '1.0.7', 'lt')) {
         $arguments[] = '--allow-secret-key-import';
     }
     $this->engine->reset();
     $this->engine->addStatusHandler(array($this, 'handleImportKeyStatus'), array(&$result));
     $this->engine->setOperation('--import', $arguments);
     $this->engine->setInput($input);
     $this->engine->run();
     if ($isFile) {
         fclose($input);
     }
     $code = $this->engine->getErrorCode();
     switch ($code) {
         case self::ERROR_DUPLICATE_KEY:
         case self::ERROR_NONE:
             // ignore duplicate key import errors
             break;
         case self::ERROR_NO_DATA:
             throw new Crypt_GPG_NoDataException('No valid GPG key data found.', $code);
         default:
             throw new Crypt_GPG_Exception('Unknown error importing GPG key. Please use the \'debug\' ' . 'option when creating the Crypt_GPG object, and file a bug ' . 'report at ' . self::BUG_URI, $code);
     }
     return $result;
 }
Beispiel #2
0
 /**
  * Returns version of the engine (GnuPG) used for operation.
  *
  * @return string GnuPG version.
  *
  * @throws Crypt_GPG_Exception if an unknown or unexpected error occurs.
  *         Use the <kbd>debug</kbd> option and file a bug report if these
  *         exceptions occur.
  */
 public function getVersion()
 {
     return $this->engine->getVersion();
 }