예제 #1
0
파일: Smime.php 프로젝트: DSNS-LAB/Dmail
 /**
  * Parse signed data.
  *
  * @param boolean $sig_only  Only do signature checking?
  *
  * @return mixed  See self::_getEmbeddedMimeParts().
  */
 protected function _parseSignedData($sig_only = false)
 {
     $partlist = array_keys($this->_mimepart->contentTypeMap());
     $base_id = reset($partlist);
     $data_id = next($partlist);
     $sig_id = Horde_Mime::mimeIdArithmetic($data_id, 'next');
     /* Initialize inline data. */
     $status = new IMP_Mime_Status(_("The data in this part has been digitally signed via S/MIME."));
     $status->icon('mime/encryption.png', 'S/MIME');
     $cache = $this->getConfigParam('imp_contents')->getViewCache();
     $cache->smime[$base_id] = array('sig' => $sig_id, 'status' => $status, 'wrap' => 'mimePartWrap');
     if (!$GLOBALS['prefs']->getValue('use_smime')) {
         $status->addText(_("S/MIME support is not enabled so the digital signature is unable to be verified."));
         return null;
     }
     /* Sanity checking to make sure MIME structure is correct. */
     if (!in_array($sig_id, $partlist)) {
         $status->action(IMP_Mime_Status::ERROR);
         $cache->smime[$base_id]['wrap'] = 'mimePartWrapInvalid';
         $status->addText(_("Invalid S/MIME data."));
         /* This will suppress displaying the invalid part. */
         $cache->smime[$base_id]['sig'] = $data_id;
         return null;
     }
     $imp_contents = $this->getConfigParam('imp_contents');
     $stream = $imp_contents->isEmbedded($base_id) ? $this->_mimepart->getMetadata('imp-smime-decrypt')->stream : $this->_getPartStream($base_id);
     $raw_text = $this->_mimepart->replaceEOL($stream, Horde_Mime_Part::RFC_EOL);
     $this->_initSmime();
     $sig_result = null;
     if ($GLOBALS['prefs']->getValue('smime_verify') || $GLOBALS['injector']->getInstance('Horde_Variables')->smime_verify_msg) {
         try {
             $sig_result = $this->_impsmime->verifySignature($raw_text);
             if ($sig_result->verify) {
                 $status->action(IMP_Mime_Status::SUCCESS);
             } else {
                 $status->action(IMP_Mime_Status::WARNING);
             }
             $cache->smime[$base_id]['wrap'] = 'mimePartWrapValid';
             $email = is_array($sig_result->email) ? implode(', ', $sig_result->email) : $sig_result->email;
             $status->addText($sig_result->msg);
             if (!empty($sig_result->cert)) {
                 $cert = $this->_impsmime->parseCert($sig_result->cert);
                 if (isset($cert['certificate']['subject']['CommonName']) && strcasecmp($email, $cert['certificate']['subject']['CommonName']) !== 0) {
                     $email = $cert['certificate']['subject']['CommonName'] . ' (' . trim($email) . ')';
                 }
             }
             if (!empty($sig_result->cert) && isset($sig_result->email) && $GLOBALS['registry']->hasMethod('contacts/addField') && $GLOBALS['prefs']->getValue('add_source')) {
                 $status->addText(sprintf(_("Sender: %s"), $imp_contents->linkViewJS($this->_mimepart, 'view_attach', htmlspecialchars(strlen($email) ? $email : $sig_result->email), array('jstext' => _("View certificate details"), 'params' => array('mode' => IMP_Contents::RENDER_INLINE, 'view_smime_key' => 1)))));
                 try {
                     $this->_impsmime->getPublicKey($sig_result->email);
                 } catch (Horde_Exception $e) {
                     $imple = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Imple')->create('IMP_Ajax_Imple_ImportEncryptKey', array('mime_id' => $base_id, 'muid' => strval($imp_contents->getIndicesOb()), 'type' => 'smime'));
                     $status->addText(Horde::link('#', '', '', '', '', '', '', array('id' => $imple->getDomId())) . _("Save the certificate to your Address Book.") . '</a>');
                 }
             } elseif (strlen($email)) {
                 $status->addText(sprintf(_("Sender: %s"), htmlspecialchars($email)));
             }
         } catch (Horde_Exception $e) {
             $status->action(IMP_Mime_Status::ERROR);
             $cache->smime[$base_id]['wrap'] = 'mimePartWrapInvalid';
             $status->addText($e->getMessage());
         }
     } else {
         switch ($GLOBALS['registry']->getView()) {
             case Horde_Registry::VIEW_BASIC:
                 $status->addText(Horde::link(Horde::selfUrlParams()->add('smime_verify_msg', 1)) . _("Click HERE to verify the data.") . '</a>');
                 break;
             case Horde_Registry::VIEW_DYNAMIC:
                 $status->addText(Horde::link('#', '', 'smimeVerifyMsg') . _("Click HERE to verify the data.") . '</a>');
                 break;
         }
     }
     if ($sig_only) {
         return;
     }
     $subpart = $imp_contents->getMIMEPart($sig_id);
     if (empty($subpart)) {
         try {
             $msg_data = $this->_impsmime->extractSignedContents($raw_text);
             $subpart = Horde_Mime_Part::parseMessage($msg_data, array('forcemime' => true));
         } catch (Horde_Exception $e) {
             $status->addText($e->getMessage());
             return null;
         }
     }
     return $subpart;
 }