# Copyright (c) 2007-2009 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: RealmFilePage.pm,v 1.14 2010/12/16 22:05:46 moeller Exp $ package Bivio::UI::HTML::Widget::RealmFilePage; use strict; use Bivio::Base 'HTMLWidget.Page'; use Bivio::UI::ViewLanguageAUTOLOAD; use URI (); our($VERSION) = sprintf('%d.%02d', q$Revision: 1.14 $ =~ /\d+/g); my($_ATTRS) = [qw(view_attr_prefix realm_id path default_path)]; my($_FP) = Bivio::Type->get_instance('FilePath'); my($_HTML) = b_use('Bivio.HTML'); sub execute { my($self, $req) = @_; $self->execute_with_content_type($req, 'text/html'); $req->get('reply')->set_output_type($req->get("$self")); return; } sub initialize { my($self) = shift; $self->map_invoke(initialize_attr => $_ATTRS); $self->internal_initialize_head_attrs(@_); return; } sub internal_new_args { shift; return b_use('HTMLWidget.ControlBase') ->internal_compute_new_args($_ATTRS, \@_); } sub render { my($self, $source, $buffer) = @_; my($req) = $source->get_request; $self->internal_setup_xhtml($req); my($rid) = $self->render_simple_attr('realm_id', $source); my($p) = $self->render_simple_attr('path', $source); my($rf) = Bivio::Biz::Model->new($req, 'RealmFile'); $p && $rf->unauth_load({ realm_id => $rid, path => $p, }) or $rf->unauth_load_or_die({ realm_id => $rid, path => $self->render_simple_attr('default_path', $source), }); $req->put("$self" => $rf->get_content_type); my($b) = $rf->get_content; $p = $rf->get('path'); #TODO: Encapsulate $p = $_FP->from_public($p) if $rf->get('is_public'); $p = URI->new($rf->get_request->format_http({uri => $p})); $$b =~ s{(\b(?:href|src)=")([^"]+)}{$1 . $_HTML->escape_attr_value(_render_uri($2, $p))}sige; my($vap) = $self->render_simple_attr('view_attr_prefix', $source); $$b =~ s{ \<\!--\s*bivio-([\w-]+)\s*--\> | \<\!--\s*start-bivio-([\w-]+)\s*--\> .*?\<\!--\s*end-bivio-([\w-]+)\s*--\> }{_render_view_attr($self, $source, $vap, [$1, $2, $3])}sigex; # It's ok to be missing a so we can use for XML if (my $h = $self->internal_render_head_attrs($source)) { $$b =~ s{(?<=\)}{\n$h}is or $self->die($b, $source, 'missing '); } $$buffer .= $$b; return; } sub _render_uri { my($rel, $file_uri) = @_; # URI doesn't support cid: return $rel if $rel =~ /^(cid|javascript):/; my($abs) = URI->new($rel); return $rel if $abs->scheme || $abs->path =~ m{^(/|$)}; $abs = $abs->abs($file_uri); my($q) = $abs->query; my($f) = $abs->fragment; return $abs->path . (defined($q) ? "?$q" : '') . (defined($f) ? "#$f" : ''); } sub _render_view_attr { my($self, $source, $vap, $matches) = @_; my($a) = $matches->[0]; unless ($a) { $a = $matches->[1]; Bivio::IO::Alert->warn( $a, ' != ', $matches->[2], ': start/end-bivio do not agree', ) unless $a eq $matches->[2]; } $a =~ s/\W/_/g; $a = "$vap$a"; return ${$self->render_value($a, view_get($a), $source)}; } 1;