| 1 | package OpenFrame::Segment::ContentLoader; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings::register; | ||
| 5 | |||
| 6 | use File::Type; | ||
| 7 | use File::Spec; | ||
| 8 | use FileHandle; | ||
| 9 | use OpenFrame::Response; | ||
| 10 | |||
| 11 | use Pipeline::Segment; | ||
| 12 | use OpenFrame::Object; | ||
| 13 | use base qw ( Pipeline::Segment OpenFrame::Object ); | ||
| 14 | |||
| 15 | our $VERSION=3.05; | ||
| 16 | |||
| 17 | sub init { | ||
| 18 | my $self = shift; | ||
| 19 | $self->{directory} = undef; | ||
| 20 | $self->SUPER::init(@_); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub directory { | ||
| 24 | my $self = shift; | ||
| 25 | my $dir = shift; | ||
| 26 | if (defined( $dir )) { | ||
| 27 | $self->{directory} = $dir; | ||
| 28 | return $self; | ||
| 29 | } else { | ||
| 30 | return $self->{directory}; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | sub dispatch { | ||
| 35 | my $self = shift; | ||
| 36 | my $store = shift->store(); | ||
| 37 | |||
| 38 | my $request = $store->get('OpenFrame::Request'); | ||
| 39 | |||
| 40 | if (!$request) { | ||
| 41 | return undef; | ||
| 42 | } | ||
| 43 | |||
| 44 | my $uri = $request->uri(); | ||
| 45 | my $path = $uri->path(); | ||
| 46 | |||
| 47 | my ($volume, $dirs, $file) = File::Spec->splitpath( $path ); | ||
| 48 | if (!$file) { | ||
| 49 | $file = "index.html"; | ||
| 50 | } | ||
| 51 | |||
| 52 | my $response = OpenFrame::Response->new(); | ||
| 53 | my $reconstituted = File::Spec->catfile( $self->directory, $dirs, $file ); | ||
| 54 | if (!-e $reconstituted) { | ||
| 55 | $response->code(ofERROR); | ||
| 56 | my $date = localtime(time()); | ||
| 57 | $response->message( | ||
| 58 | qq{ | ||
| 59 | <html> | ||
| 60 | <head> | ||
| 61 | <title>File Not Found</title> | ||
| 62 | </head> | ||
| 63 | <body> | ||
| 64 | <h1>File Not Found</h1> | ||
| 65 | <p> | ||
| 66 | The file $path was not found. | ||
| 67 | </p> | ||
| 68 | <hr> | ||
| 69 | <i>$date, OpenFrame $OpenFrame::VERSION</i> | ||
| 70 | </body> | ||
| 71 | </html> | ||
| 72 | } | ||
| 73 | ); | ||
| 74 | $self->error("could not find file $path"); | ||
| 75 | return $response; | ||
| 76 | |||
| 77 | } else { | ||
| 78 | my $mm = File::Type->new(); | ||
| 79 | my $res = $mm->checktype_filename( $reconstituted ); | ||
| 80 | my $fh = FileHandle->new( "<$reconstituted" ); | ||
| 81 | if ( $fh ) { | ||
| 82 | local $/ = undef; | ||
| 83 | my $data = <$fh>; | ||
| 84 | $fh->close(); | ||
| 85 | $response->code( ofOK ); | ||
| 86 | $response->message( $data ); | ||
| 87 | $response->mimetype( $res ); | ||
| 88 | return $response; | ||
| 89 | } else { | ||
| 90 | my $date = localtime(time()); | ||
| 91 | $response->code( ofERROR ); | ||
| 92 | $response->message( | ||
| 93 | qq{ | ||
| 94 | <html> | ||
| 95 | <head> | ||
| 96 | <title>Access Forbidden</title> | ||
| 97 | </head> | ||
| 98 | <body> | ||
| 99 | <h1>Access Forbidden</h1> | ||
| 100 | <p> | ||
| 101 | You aren't allowed to access the file $path | ||
| 102 | </p> | ||
| 103 | <hr> | ||
| 104 | <i>$date, OpenFrame $OpenFrame::VERSION</i> | ||
| 105 | </body> | ||
| 106 | </html> | ||
| 107 | } #'} | ||
| 108 | ); | ||
| 109 | |||
| 110 | $self->error("could not access file $path: permission denied"); | ||
| 111 | return $response; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | } | ||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ |