コード例 #1
0
ファイル: AS2.php プロジェクト: techdata/as2secure-bundle
 /**
  * @param $toPartner
  * @param $fromPartner
  * @param $messageContent
  * @throws \Exception
  * @throws \TechData\AS2SecureBundle\Models\AS2Exception
  * @throws \TechData\AS2SecureBundle\Models\Exception
  */
 public function sendMessage($toPartner, $fromPartner, $messageContent)
 {
     // process request to build outbound AS2 message to VAR
     // initialize outbound AS2Message object
     $message = $this->messageFactory->build(false, array('partner_from' => $fromPartner, 'partner_to' => $toPartner));
     // initialize AS2Adapter for public key encryption between StreamOne and the receiving VAR
     $adapter = $this->adapterFactory->build($fromPartner, $toPartner);
     // write the EDI message that will be sent to a temp file, then use the AS2 adapter to encrypt it
     $tmp_file = $adapter->getTempFilename();
     file_put_contents($tmp_file, $messageContent);
     $message->addFile($tmp_file, 'application/edi-x12');
     $message->encode();
     // send AS2 message
     $result = $this->client->sendRequest($message);
     $messageSent = new MessageSent();
     $messageSent->setMessage(print_r($result, true));
     $this->eventDispatcher->dispatch(MessageSent::EVENT, $messageSent);
 }
コード例 #2
0
ファイル: Request.php プロジェクト: techdata/as2secure-bundle
 public function getObject()
 {
     // setup of full message
     $content = $this->getHeaders(true) . "\n\n";
     $content .= file_get_contents($this->getPath());
     $input = Adapter::getTempFilename();
     file_put_contents($input, $content);
     // setup of mailmime decoder
     $params = array('include_bodies' => false, 'decode_headers' => true, 'decode_bodies' => false, 'input' => false);
     $decoder = new Mail_mimeDecode(file_get_contents($input));
     $structure = $decoder->decode($params);
     $mimetype = $structure->ctype_primary . '/' . $structure->ctype_secondary;
     // handle crypted content
     $crypted = false;
     if (strtolower($mimetype) == 'application/pkcs7-mime') {
         try {
             // rewrite message into base64 encoding
             $content = file_get_contents($input);
             $mime_part = Horde_MIME_Structure::parseTextMIMEMessage($content);
             $input = Adapter::getTempFilename();
             file_put_contents($input, $mime_part->toString(true));
             $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'AS2 message is encrypted.'));
             $input = $this->adapter->decrypt($input);
             $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'The data has been decrypted using the key "' . $this->getPartnerTo() . '".'));
             $crypted = true;
             // reload extracted content to get mimetype
             $decoder = new Mail_mimeDecode(file_get_contents($input));
             $structure = $decoder->decode($params);
             $mimetype = $structure->ctype_primary . '/' . $structure->ctype_secondary;
         } catch (Exception $e) {
             throw new AS2Exception($e->getMessage(), 3);
         }
     }
     // handle signed content
     $signed = false;
     $mic = false;
     if (strtolower($mimetype) == 'multipart/signed') {
         try {
             $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'AS2 message is signed.'));
             // get MicChecksum from signature
             $mic = $this->adapter->getMicChecksum($input);
             $input = $this->adapter->verify($input);
             $signed = true;
             $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'The sender used the algorithm "' . $structure->ctype_parameters['micalg'] . '" to sign the message.'));
             // reload extracted content to get mimetype
             $decoder = new Mail_mimeDecode(file_get_contents($input));
             $structure = $decoder->decode($params);
             $mimetype = $structure->ctype_primary . '/' . $structure->ctype_secondary;
             $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'Using certificate "' . $this->getPartnerFrom() . '" to verify signature.'));
         } catch (Exception $e) {
             throw new AS2Exception($e->getMessage(), 5);
         }
     } else {
         // check requested algo
         $mic = Adapter::calculateMicChecksum($input, 'sha1');
     }
     // security check
     if (strtolower($mimetype) == 'multipart/report') {
         // check about sign
         /*if ($this->getPartnerFrom()->sec_signature_algorithm == Partner::SIGN_NONE && !$this->getPartnerFrom()->mdn_signed && $signed){
               throw new AS2Exception('AS2 message is signed and shouldn\'t be.', 4);
           }
           else*/
         if ($this->getPartnerFrom()->sec_signature_algorithm != Partner::SIGN_NONE && $this->getPartnerFrom()->mdn_signed && !$signed) {
             throw new AS2Exception('AS2 message is not signed and should be.', 4);
         }
     } else {
         // check about crypt
         /*if ($this->getPartnerFrom()->sec_encrypt_algorithm == Partner::CRYPT_NONE && $crypted){
               throw new AS2Exception('AS2 message is crypted and shouldn\'t be.', 4);
           }
           else*/
         if ($this->getPartnerFrom()->sec_encrypt_algorithm != Partner::CRYPT_NONE && !$crypted) {
             throw new AS2Exception('AS2 message is not crypted and should be.', 4);
         }
         // check about sign
         /*if ($this->getPartnerFrom()->sec_signature_algorithm == Partner::SIGN_NONE && $signed){
               throw new AS2Exception('AS2 message is signed and shouldn\'t be.', 4);
           }
           else*/
         if ($this->getPartnerFrom()->sec_signature_algorithm != Partner::SIGN_NONE && !$signed) {
             throw new AS2Exception('AS2 message is not signed and should be.', 4);
         }
     }
     try {
         // build object with extracted content
         $message = file_get_contents($input);
         $mime_part = Horde_MIME_Structure::parseTextMIMEMessage($message);
         switch (strtolower($mimetype)) {
             case 'multipart/report':
                 $params = array('partner_from' => $this->getPartnerTo(), 'partner_to' => $this->getPartnerFrom(), 'is_file' => false, 'mic' => $mic);
                 $object = $this->mdnFactory->build($mime_part, $params);
                 return $object;
             default:
                 $params = array('partner_from' => $this->getPartnerFrom(), 'partner_to' => $this->getPartnerTo(), 'is_file' => false, 'mic' => $mic);
                 $object = $this->messageFactory->build($mime_part, $params);
                 $object->setHeaders($this->getHeaders());
                 return $object;
         }
     } catch (Exception $e) {
         throw new AS2Exception($e->getMessage(), 6);
     }
     throw new AS2Exception('Unexpected error while handling message.', 6);
 }