| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Apache::AxKit::Provider::OpenOffice; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 15343 | use strict; | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 113 |  | 
| 4 | 3 |  |  | 3 |  | 14 | use vars qw($VERSION @ISA); | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 156 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 3 |  |  | 3 |  | 4701 | use AxKit; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | use Apache::AxKit::Provider::File; | 
| 8 |  |  |  |  |  |  | use Archive::Zip qw(:ERROR_CODES); | 
| 9 |  |  |  |  |  |  | use Apache::Request; | 
| 10 |  |  |  |  |  |  | use IO::File; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | $VERSION = '1.02'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | @ISA = ('Apache::AxKit::Provider::File'); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | Archive::Zip::setErrorHandler(\&_error_handler); | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub _error_handler { | 
| 19 |  |  |  |  |  |  | my $error = shift; | 
| 20 |  |  |  |  |  |  | AxKit::Debug(3, $error); | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub get_fh { | 
| 24 |  |  |  |  |  |  | my $self = shift; | 
| 25 |  |  |  |  |  |  | my $zip = Archive::Zip->new(); | 
| 26 |  |  |  |  |  |  | if ($zip->read($self->{file}) != AZ_OK) { | 
| 27 |  |  |  |  |  |  | throw Apache::AxKit::Exception::IO (-text => "Couldn't read OpenOffice file '$self->{file}'"); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | my $r = $self->apache_request; | 
| 30 |  |  |  |  |  |  | my $member; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | my $path_info = $r->path_info; | 
| 33 |  |  |  |  |  |  | $path_info =~ s|^/||; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | if ($path_info) { | 
| 37 |  |  |  |  |  |  | AxKit::Debug(7, "[OpenOffice]: path info found in get_fh: $path_info" ); | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # probably need to get smarter here at some point... | 
| 40 |  |  |  |  |  |  | return \"" if $path_info =~ /office\.dtd/; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | $member = $zip->memberNamed($path_info); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | else { | 
| 45 |  |  |  |  |  |  | $member = $zip->memberNamed('content.xml') || $zip->memberNamed('Content.xml'); | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  | my $fh = IO::File->new_tmpfile; | 
| 48 |  |  |  |  |  |  | $member->extractToFileHandle($fh); | 
| 49 |  |  |  |  |  |  | seek($fh, 0, 0); | 
| 50 |  |  |  |  |  |  | return $fh; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub get_strref { | 
| 54 |  |  |  |  |  |  | my $self = shift; | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | my $zip = Archive::Zip->new(); | 
| 57 |  |  |  |  |  |  | if ($zip->read($self->{file}) != AZ_OK) { | 
| 58 |  |  |  |  |  |  | throw Apache::AxKit::Exception::IO (-text => "Couldn't read OpenOffice file '$self->{file}'"); | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  | my $r = $self->apache_request; | 
| 61 |  |  |  |  |  |  | my $member; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | my $path_info = $r->path_info; | 
| 64 |  |  |  |  |  |  | $path_info =~ s|^/||; | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | if ($path_info) { | 
| 67 |  |  |  |  |  |  | AxKit::Debug(7, "[OpenOffice]: path info found in get_strref: $path_info" ); | 
| 68 |  |  |  |  |  |  | # probably need to get smarter here at some point... | 
| 69 |  |  |  |  |  |  | return \"" if $path_info eq 'office.dtd'; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | $member = $zip->memberNamed($path_info); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | else { | 
| 74 |  |  |  |  |  |  | $member = $zip->memberNamed('content.xml') || $zip->memberNamed('Content.xml'); | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | my ($data, $status) = $member->contents(); | 
| 77 |  |  |  |  |  |  | if ($status != AZ_OK) { | 
| 78 |  |  |  |  |  |  | throw Apache::AxKit::Exception::Error( | 
| 79 |  |  |  |  |  |  | -text => "Contents.xml could not be retrieved from $self->{file}" | 
| 80 |  |  |  |  |  |  | ); | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | if ( $path_info =~ /\.(png|gif|jpg)$/ ) { | 
| 84 |  |  |  |  |  |  | my $image_type = $1; | 
| 85 |  |  |  |  |  |  | $r->content_type( 'image/' . $image_type ); | 
| 86 |  |  |  |  |  |  | $r->send_http_header(); | 
| 87 |  |  |  |  |  |  | $r->print( $data ); | 
| 88 |  |  |  |  |  |  | throw Apache::AxKit::Exception::Declined( | 
| 89 |  |  |  |  |  |  | -text => "[OpenOffice] Image detected, skipping further processing." | 
| 90 |  |  |  |  |  |  | ); | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | return \$data; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub __process { | 
| 97 |  |  |  |  |  |  | my $self = shift; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | my $xmlfile = $self->key; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | unless ($self->exists()) { | 
| 102 |  |  |  |  |  |  | AxKit::Debug(5, "file '$xmlfile' does not exist or is not readable"); | 
| 103 |  |  |  |  |  |  | return 0; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | if ( $self->_is_dir ) { | 
| 107 |  |  |  |  |  |  | # else | 
| 108 |  |  |  |  |  |  | AxKit::Debug(5, "'$xmlfile' is a directory"); | 
| 109 |  |  |  |  |  |  | return 0; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | return 1; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | 1; | 
| 117 |  |  |  |  |  |  | __END__ |