[# tag: ewok file op page #]
[!
use Ewok::Config;
use Ewok::File qw(remove cutcopy pastefiles paste);
use Ewok::Publish qw(getSiteHash getPublishDirs publish publishAll);
use Ewok::Authz qw(:common authzObjCheck);
use Ewok::Util qw(:dir :error);
use URI::Escape;
!]
[- $javascript = !exists $req->{pref}->{javascript} || $req->{pref}->{javascript}; -]
ewok file [+ "\l$fdat{op}" +]
[# Error conditions #]
[-
Execute({ inputfile => 'util.epl', import => 1 });
$req = shift;
$debug = 0;
@ops = ('Delete', 'Rename', 'Link', 'Cut', 'Copy', 'Paste', 'Paste As', 'Publish', 'Cancel', 'Watch');
@treeOps = ('Publish', 'Watch');
@errors = ();
($web_dir, $fs_dir, $wd) = webfsdir($req, $fdat{p}, { errors => \@errors });
if (! @errors) {
$authz = authzGetUserHash($req, $fs_dir);
$editPriv = defined $authz->{$EDIT_PRIV};
$publPriv = defined $authz->{$PUBL_PRIV};
$tmplPriv = defined $authz->{$TMPL_PRIV};
$authzPriv = defined $authz->{$AUTHZ_PRIV};
$adminPriv = $authz->{$ADMIN_PRIV};
@dirlist = split /\t/, $fdat{dirs};
@filelist = split /\t/, $fdat{files};
@namesFromList = split /\t/, $fdat{namesFrom};
@namesToList = split /\t/, $fdat{namesTo};
$fdat{op} ||= $fdat{area};
%rename = ();
for (my $i = 0; $i < @namesFromList; $i++) {
$rename{$namesFromList[$i]} = $namesToList[$i];
}
info("Error: cannot find directory '$wd'.", \@errors)
if ! -d $fs_dir;
info("Error: cannot write to directory '$wd'.", \@errors)
if ! @errors && ! -w $fs_dir;
info("Error: unknown operation '$fdat{op}'.", \@errors)
if ! $fdat{op} || ! grep /^$fdat{op}$/, @ops;
info("Error: \l$fdat{op} functionality not available without ewok authorisations on. Please contact your ewok administrator or webmaster.", \@errors)
if (substr($fdat{op},0,5) eq 'Paste' && ! $req->{user});
if ($fdat{op} eq 'Publish') {
info("Error: you do not have publish privilege on directory $wd.", \@errors)
if ! $publPriv;
info("Error: $fdat{scope} scope is incompatible with 'Publish as filename' option.", \@errors)
if $fdat{scope} ne 'selected' && $fdat{pubfilename};
info("Error: 'Publish as filename' option is valid only when publishing a single file.", \@errors)
if $fdat{scope} eq 'selected' && $fdat{pubfilename} && @filelist > 1;
info("Error: $fdat{scope} scope is incompatible with 'Staging' target.", \@errors)
if $fdat{scope} ne 'selected' && $fdat{target} eq 'stage';
info("Error: 'Publish as filename' is a required field for 'Staging' target.", \@errors)
if $fdat{target} eq 'stage' && ! $fdat{pubfilename};
info("Error: cannot select files and directories to publish at the same time (perhaps you mean 'Publish Directory'?)", \@errors)
if $fdat{scope} eq 'selected' && @filelist && @dirlist;
if (! @errors) {
$site = getSiteHash($fs_dir);
if (ref $site eq 'HASH' && ref $site->{publish} eq 'HASH' &&
keys %{$site->{publish}}) {
# Ensure $fdat{target} is set to something sane
undef $fdat{target} unless exists $site->{publish}->{$fdat{target}};
$fdat{target} ||= $site->{default_target} || (sort keys %{$site->{publish}})[0];
# Abort unless the a sensible target can be found
if (! exists $site->{publish}->{$fdat{target}}) {
undef $site;
warning("Error: loading publishing settings for '$wd' failed.", \@errors);
}
}
else {
warning("Error: loading publishing settings for '$wd' failed.", \@errors);
}
}
}
else {
info("Error: you do not have edit privilege on directory $wd.", \@errors)
if ! $editPriv;
}
}
warning("Error: cannot find referring page.", \@errors)
if $fdat{op} eq 'Cancel' && ! $fdat{referer};
-]
[$ if ($fdat{op} eq 'Cancel' && $fdat{referer}) $]
[- $http_headers_out{Location} = $fdat{referer}; -]
[$ elsif (! @errors) $]
[-
# Setup %publishArgs if Publish page 2
if ($fdat{op} eq 'Publish' && $fdat{next} eq 'page2') {
$publishArgs = {};
$publishArgs->{web_dir} = $web_dir;
$publishArgs->{site} = $site;
$publishArgs->{recursive} = ($fdat{scope} eq 'tree');
$publishArgs->{refresh} = $fdat{refresh};
$publishArgs->{test} = $fdat{test};
$publishArgs->{force} = $fdat{force} || $fdat{stage_replace};
$publishArgs->{copylinks} = $fdat{copylinks};
$publishArgs->{sync_deletes} = $fdat{syncdeletes};
$publishArgs->{template} = $fdat{template};
$publishArgs->{target} = $fdat{target};
$publishArgs->{stage_replace} = $fdat{stage_replace};
$publishArgs->{pubfilename} = $fdat{pubfilename};
$publishArgs->{fdat} = \%fdat;
$publishArgs->{debug} = $debug;
$results = {};
$results->{pubList} = \@pubList;
$results->{deleteList} = \@deleteList;
$results->{errors} = \@errors;
$results->{warnings} = \@warnings;
}
# Check scope is valid
if ($fdat{scope} && $fdat{scope} ne 'selected' &&
! grep /$fdat{op}/, @treeOps) {
info("Error: $fdat{scope} scope not supported for \l$fdat{op}.", \@errors)
}
# Rename
elsif ($fdat{op} eq 'Rename' && $fdat{namesFrom} && $fdat{namesTo}) {
if (Ewok::Authz::authzObjCheck(\@namesToList, $authz, 'rename to', \@errors) &&
Ewok::File::rename($req, \@namesFromList, \@namesToList, $fs_dir, \@errors, $web_dir)) {
# If successful, redisplay dir in file manager
$http_headers_out{Location} = "/ewok/fm.html?p=$web_dir";
}
# else @errors populated
}
# Publish directory or tree, page 2
elsif ($fdat{op} eq 'Publish') {
# If page 2 and directory publish (either explicit dir or tree publish or
# implicit via selected directory) do a (set of) publishAll
if ($fdat{next} eq 'page2' && ($fdat{scope} ne 'selected' || @dirlist)) {
if ($fdat{scope} ne 'selected') {
publishAll($req, $fs_dir, $results, %$publishArgs);
}
else {
# Publish selected directories
for my $dir (@dirlist) {
$results->{pubList} = [];
publishAll($req, "$fs_dir$dir", $results, %$publishArgs);
# Doctor the published list
push @pubList, map { "$dir/$_" } @{$results->{pubList}};
}
}
# Omit warnings for 'Publish Tree';
@warnings = () if $fdat{scope} eq 'tree';
}
}
# Paste / Paste As
elsif (substr($fdat{op},0,5) eq 'Paste') {
@filelist = @{pastefiles($req->{user}, 1)};
@exists = ();
if (!$fdat{existsCheck}) {
# Check if any of the paste targets already exist
for my $file (@filelist) {
if (exists $rename{$file}) {
push @exists, $file if (-e "$fs_dir/$rename{$file}");
}
else {
push @exists, $file if (-e "$fs_dir/$file");
}
}
}
if ($fdat{op} eq 'Paste' && ! @exists) {
my %clobber = ();
# If some paste targets have been found to exist, setup a clobber hash
if ($fdat{existsCheck}) {
for my $file (@filelist) {
# Default to no clobber (0)
$clobber{$file} = ($fdat{"overwrite-$file"} eq 'Y') ? 1 : 0;
}
}
# Paste - if successful, redisplay dir in file manager
if (paste($req, $fs_dir, {
tag => $req->{user},
authz => $authz,
errors => \@errors,
web_dir => $web_dir,
fromlist => \@namesFromList,
tolist => \@namesToList,
clobber => \%clobber })) {
$http_headers_out{Location} = "/ewok/fm.html?p=$web_dir";
}
}
}
elsif ($fdat{op} eq 'Watch') {
if ($fdat{next} eq 'page2') {
info("Watch not implemented yet.", \@errors)
}
}
# If not Rename or Paste or Publish or Watch, check something has been selected
elsif (!$fdat{dirs} && !$fdat{files}) {
info("Error: nothing selected for \L$fdat{op}.", \@errors)
}
# Delete
elsif ($fdat{op} eq 'Delete') {
if (remove($req, $fs_dir, [ @dirlist, @filelist ],
{ errors => \@errors, web_dir => $web_dir, })) {
# If successful, redisplay dir in file manager
$http_headers_out{Location} = "/ewok/fm.html?p=$web_dir";
}
}
# Cut/Copy
elsif ($fdat{op} eq 'Cut' || $fdat{op} eq 'Copy') {
if (! $req->{user}) {
info("Error: \l$fdat{op} functionality not available without ewok authorisations on.", \@errors)
}
elsif (cutcopy($req, $fs_dir, [ @dirlist, @filelist ], {
tag => $req->{user}, op => lc($fdat{op}), errors => \@errors,
web_dir => $web_dir,
})) {
# If successful, redisplay dir in file manager
$http_headers_out{Location} = "/ewok/fm.html?p=$web_dir";
}
}
# Publish Page 2, selected objects
if ($fdat{op} eq 'Publish' && $fdat{next} eq 'page2' &&
$fdat{scope} eq 'selected' && (@filelist || $fdat{syncdeletes})) {
# Do the publish
publish($req, \@filelist, $fs_dir, $results, %$publishArgs);
}
-]
[$ endif $]
[$ if (@errors) $]
[- errors([ @errors, @warnings ]) -]
[$ elsif ($fdat{op} eq 'Watch') $][-
$fdat{scope} = 'directory'
if $fdat{scope} eq 'selected' && !@dirlist && !@filelist;
-]
Watch in [+ $wd +]:
[- errors(\@errors, { h1 => undef }) -]
[# Publish Page 1 - additional publish parameters #]
[$ elsif ($fdat{op} eq 'Publish' && $fdat{next} ne 'page2') $][-
$fdat{scope} = 'directory' if $fdat{scope} eq 'selected' && !@dirlist && !@filelist;
# Ewok::File::restrictCheck($req, lc($fdat{op}), $fs_dir, \@filelist, { errors => \@errors, authz => $authz });
-]
Publish from [+ $wd +]:
[$ if (@errors) $]
[- errors(\@errors, { h1 => undef }) -]
[$ else $]
[$ if ($debug) $]
scope: [+ $fdat{scope} +]
filelist: [+ join ',',@filelist +]
dir: [+ $fdat{d} +]
[$ endif $]
[$ endif $][# no errors, Publish Page 1 #]
[# Publish Page 2 - present results #]
[$ elsif ($fdat{op} eq 'Publish' && $fdat{next} eq 'page2') $][-
if ($fdat{scope} ne 'selected') {
$scope = 'entire ' . $fdat{scope};
}
elsif (@dirlist) {
$scope = 'selected directories';
}
elsif (@filelist) {
$scope = 'selected files';
}
-][+
($fdat{test}) ? "Test mode publish of $scope from $wd:" : "Publishing $scope from $wd:";
+]
[$ if (ref $site eq 'HASH') $][-
if ($fdat{target} eq 'stage') {
$pub_url = $wd;
}
else {
($pub_url, $pub_dir) = getPublishDirs($web_dir,
$site->{alias},
$site->{publish}->{$fdat{target}}->{dest_web_dir},
$site->{publish}->{$fdat{target}}->{dest_url},
);
}
-][$ if (@pubList || @deleteList) $]
[$ if (@pubList) $]
The following files [+ ($fdat{test}) ? 'would be' : 'were' +] published:
[$ foreach $file (@pubList) $]
- [$ if ($fdat{test}) $][+ $file +]
[$ else $][+ $file +]
[$ endif $]
[$ endforeach $]
[$ endif $][$ if (@deleteList) $]
The following files [+ ($fdat{test}) ? 'would be' : 'were' +] deleted:
[$ foreach $file (@deleteList) $]
- [+ $file +]
[$ endforeach $]
[$ endif $]
[$ elsif ! @errors && ! @warnings $]
All files up to date - nothing to be published.
[$ else $]
[- errors(\@errors, { align => 'left', h1 => undef }) -]
[$ if (@warnings) $][+
local $escmode = 0; join "
\n", @warnings;
+]
[$ endif $]
[$ endif $]
Staging directory
[$ if ($pub_dir) $]
Publish directory
[$ endif $]
[$ elsif @filelist $][# 'Publish', $site not defined #]
- Error: publishing settings for [+ $wd +] not found.
[$ endif $]
[$ elsif (($fdat{op} eq 'Rename' || $fdat{op} eq 'Paste As') &&
(@filelist || @dirlist)) $]
[# Rename or Paste As page 1 - gather rename destination names #]
[$ if (! Ewok::File::restrictCheck($req, "\L$fdat{op}", $fs_dir, \@filelist, { errors => \@errors, authz => $authz })) $]
[- errors(\@errors, { h1 => undef }) -]
[$ else $]
[$ if ($fdat{op} eq 'Rename') $]
Rename these items in [+ $wd +] to:
[- $button = 'Rename' -]
[$ else $]
Paste these items as:
[- $button = 'Paste' -]
[$ endif $][- errors(\@errors, { h1 => undef }); @objList = (@dirlist, @filelist); -]
[$ endif $]
[# End of Rename / Paste As #]
[# Paste where files exist #]
[$ elsif ($fdat{op} eq 'Paste') $]
Pasting files ...
[$ endif $]