1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
package MojoMojo::Formatter::Dir;
use strict;
use warnings;
use base qw/MojoMojo::Formatter/;
use Path::Class ();
use MojoMojo::Formatter::File::Image;
my $debug=0;
=head1 NAME
MojoMojo::Formatter::Dir - format local directory as XHTML
=head1 DESCRIPTION
This formatter will format the directory argument as XHTML.
Usage:
{{dir directory exclude=exclude_regex}}
For security reasons the directory must be include in 'whitelisting'. You can use path_to(DIR) to describe directory in mojomojo.conf:
<Formatter::Dir>
prefix_url /myfiles
whitelisting __path_to(uploads)__
</Formatter::Dir>
=head1 METHODS
=over 4
=item format_content_order
Format order can be 1-99. The File formatter runs on 92.
=cut
sub format_content_order { 92 }
=item format_content
Calls the formatter. Takes a ref to the content as well as the
context object.
=cut
sub format_content {
my ( $self, $content, $c ) = @_;
# TODO : Add cache if directory is not modified
my @lines = split /\n/, $$content;
$$content = "";
foreach my $line (@lines) {
if ( $line =~ m|<p>{{dir\s*(\S*)\s*(\S*)}}</p>| ) {
my $dir = $1;
my $exclude = $2;
$exclude =~ s/exclude=//;
my $path_to = $c->path_to();
# use path_to(dir) ?
$dir =~ s/path_to\((\S*)\)/${path_to}\/$1/;
$dir =~ s/\/$//;
my $error;
if ( $error = $self->checkdir($dir, $c)){
$$content .= $error;
}
if ( ! $error ){
# format with plugin
$$content .= $self->format($dir, $exclude, $c);
}
}
else{
$$content .= $line . "\n";
}
}
return $content;
}
=item format
Return the content formatted
=cut
sub format {
my $self = shift;
my $dir = shift;
my $exclude = shift;
my $c = shift;
my $baseuri = $c->base_uri;
my $path = $c->stash->{path};
return $self->to_xhtml($dir, $exclude, $baseuri, $path);
}
=item to_xhtml
Return Directory and files lists in xhtml
=cut
sub to_xhtml{
my ($self, $dir, $exclude, $baseuri, $path) = @_;
my $pcdir = Path::Class::dir->new("$dir");
my @subdirs;
my @files;
while (my $file = $pcdir->next) {
next if ($file =~ m/^$dir\/?\.*$/ );
next if ( "$exclude" && grep(/$exclude/, $file ));
if ( -d $file ){
push @subdirs , $file;
}
else{
push @files, $file;
}
}
#-mxh Sort the array for predictable ordering in formatter_dir.t
@subdirs = sort @subdirs;
@files = sort @files;
$path =~ s/^\///;
$path =~ s/\/$//;
my $url = "${baseuri}/${path}";
my $ret;
if ( $subdirs[0] ){
$ret = '<div id="dirs"><ul>';
$ret .= "<li><a href=\"$url\">..</a></li>" if ( $url =! "/$path");
foreach my $d (@subdirs){
next if ( ! -r $d);
$d =~ s/$dir\///;
$ret .= "<li><a href=\"$url/$path/$d\">[$d]</a></li>";
}
$ret .= "</ul></div>\n";
}
if ( $files[0] ){
$ret .= '<div id="files"><ul>';
foreach my $f (@files){
next if ( ! -r $f);
$f =~ s/$dir\///;
$f =~ s/^\///;
# Use Image controller if it is a image
$f =~ /.*\.(.*)$/;
# replace dot with '_' if it's not a image
$f =~ s/\./_/
if ( ! MojoMojo::Formatter::File::Image->can_format($1) );
$ret .= "<li><a href=\"$url/$f\">$f</a></li>";
}
$ret .= "</ul></div>\n";
}
return $ret;
}
=item checkdir
Directory must be include in whitelisting
=cut
sub checkdir{
my ($self,$dir,$c) = @_;
return "Append a directory after 'dir'"
if ( ! $dir );
return "You can't use '..' in the name of directory"
if ( $dir =~ /\.\./ );
my $confwl = $c->config->{'Formatter::Dir'}{whitelisting};
my @whitelist = ref $confwl eq 'ARRAY' ?
@$confwl : ( $confwl );
# Add '/' if not exist at the end of whitelist directories
my @wl = map { $_ . '/' } # Add '/'
( map{ /(\S*[^\/])/ } # Delete '/' if exist
@whitelist );
# Add '/' if not exist at the end of dierctory
$dir =~ s|^(\S*[^/])$|$1\/|;
# if $dir is not include in whitelisting
if ( ! map ( $dir =~ m|^$_| , @wl) ){
return "Directory '$dir' must be include in whitelisting ! see Formatter::Dir:whitelisting in mojomojo.conf"
}
return "'$dir' is not a directory !\n"
if ( ! -d $dir );
return 0;
}
=back
=head1 SEE ALSO
L<MojoMojo>
=head1 AUTHORS
Daniel Brosseau <dab@catapulse.org>
=head1 LICENSE
This module is licensed under the same terms as Perl itself.
=cut
1;
|