How to make article attachment with PHP and SOAP

Dont create your support topics here! No new topics with questions allowed!

Moderator: crythias

Forum rules
Dont create your support topics here! No new topics with questions allowed!
Post Reply
benoitm
Znuny newbie
Posts: 34
Joined: 21 Mar 2011, 10:22
Znuny Version: 3.0.6

How to make article attachment with PHP and SOAP

Post by benoitm »

If you want to create article with attachment with PHP, you have read here or elsewhere you need to encode the attachment trough php to be able to send it via SOAP. But at the other side, in OTRS, you can "see" the attachment but you are not able to open it : the file need to be decoded before to be read/inserted in OTRS DB.
So you can use

Code: Select all

$Attachment->{Content} = decode_base64($Attachment->{Content});
in "article.pm" but new problem : if you add an attachment thought OTRS (by a agent, for exemple), you can't read it after joint => OTRS is tryin to 64 bits decoding a non 64 bit encoded string , that is the reason !

Here is my workaround, with comments

Please tell me if you have suggestions, this workaround work perfect for me :)

[edit Article.pm file with this lines, make backup before :p ]

Code: Select all

	# add attachments
	# Change :
		# Detect if the attachment is 64 bits encoded, (attachment with PHP) 
		# if true, decoding else, do nothing
		# WARNING : need to change $eol to "" in PHP side, like this :
			# Eg : chunk_split(base64_encode(file_get_contents($tmp_file)), 76, "")

	if ( $Param{Attachment} ) {
		use MIME::Base64;
		for my $Attachment ( @{ $Param{Attachment} } ) {

				if($Attachment->{Content} =~ 	
					m{
					^
					(?: [A-Za-z0-9+/]{4} )*
					(?:
						[A-Za-z0-9+/]{2} [AEIMQUYcgkosw048] =
					|
						[A-Za-z0-9+/] [AQgw] ==
					)?
					\z
					}x
					){
					$Self->{LogObject}->Log( Priority => 'notice', Message  => "64 bits encoded attachment, decoding base 64 (file $Attachment->{Filename})");
					$Attachment->{Content} = decode_base64($Attachment->{Content});
				}
				else{
					$Self->{LogObject}->Log( Priority => 'notice', Message  => "No 64 bits encoded attachment, nothing to do (file $Attachment->{Filename})");
				}
		}

		for my $Attachment ( @{ $Param{Attachment} } ) {
			$Self->ArticleWriteAttachment(
				%{$Attachment},
				ArticleID => $ArticleID,
				UserID    => $Param{UserID},
			);
		}
    }

Sory for my poor English
Post Reply