#!/usr/bin/perl
# wfindapi - Generate web server file list JSON API
#
# Copyright 2014 David Meyer <
[email protected]> +JMJ
# (License and documentation at bottom of file.)
# Modules ###########################################################
use strict;
use warnings;
use JSON; # Convert Perl data <-> JSON text
# Configuration #####################################################
$ROOTURL = '
http://totallynuclear.club';
# Web host top page URL
$ROOTPATH = '/var/www/html';
# File system path to web root directory
# - Set by DocumentRoot in Apache config.
$USERPATH = '/home/*/public_html';
# FS path to user web page directories
# - Set from UserDir in Apache configuration
# - Asterisk is replaced by user names,
# supports globbing find search path
$UPATHRE = '/home/(\w+)/public_html';
# Perl regular expression to match $USERPATH
$FIND = '/bin/find'; # Full path of FIND program: eliminates
# dependency on $PATH
# Command line arguments ############################################
if ($#ARGV == 0) { # If one argument ...
$fspec = "-type f -a -name '$ARGV[0]'";
# FIND will search for regular files with
# names matching arg
}
elsif (@ARGV) { # Else if more than one argument ...
@fspecs = ();
for (@ARGV) {
push @fspecs, "-name '$_'";
# Prepend arg w/ "-name" to make find opt.
}
$fspec = '-type f -a \( ' . (join ' -o ', @fspecs) . ' \)';
# FIND will search for regular files with
# names matching any arg
}
else { # Else (no arguments)
$fspec = '-type f'; # FIND will search for all regular files
}
# Main ##############################################################
@filelist = (); # Array to store list of file info
open WFILE, "$FIND $ROOTPATH $USERPATH $fspec 2>/dev/null |";
# FIND searches web server root directory
# file tree and user web file trees for
# regular files matching pattern argument(s).
while (<WFILE>) { # Repeat for each file found by FIND.
$urlpath = $user = $basename = $url = "";
# Initialize work variables.
chop $_; # Chop off end-of-line character.
if ($_ =~ m:^$ROOTPATH(.*)/([^/]+)$:) {
# If file name starts with site root path
$urlpath = $1; # Extract URL path
$basename = $2; # Extract file base name (w/out path)
$user = 'null'; # User is undefined for site root files
$url = $ROOTURL . $urlpath . '/' . $basefile;
# Construct file URL from components
}
else {
$_ =~ m:^$UPATHRE(.*)/([^/]+)$:;
# Match file name with user web path RE
$user = $1; # Extract user
$urlpath = $2; # Extract URL path
$basename = $3; # Extract file base name
$url = $ROOTURL . "/~$user" . $urlpath . '/' . $basefile;
# Construct file URL from components
}
%fileobj = ( # Hash stores data for current file.
file => $_,
user => $user,
urlpath => $urlpath,
basename => $basename,
url => $url
);
push @filelist, \%fileobj;
# Add ref. to file info hash to file list array.
}
%flobj = (
runtime => '',
rooturl => '',
userpath => '',
filelist => \@filelist
);
#print encode_json \%flobj;
$json = new JSON;
print $json->pretty([$enable])->encode(\%flobj) . "\n";;
exit 0
__END__
# Documentation #####################################################
=head1 NAME
wfindapi - Generate web server file list JSON API
=head1 USAGE
B<wfindapi> [I<pattern> ...]
=head1 DESCRIPTION
Uses B<find> to search web server root and user directories for
normal files with base names matching I<pattern> (defaults to listing
all files).
Outputs the following JSON schema to standard output.
{
}
=head1 CONFIGURATION
=head1 ENVIRONMENT
=over 6
=item VARIABLE
Description of usage of environment variable C<VARIABLE>.
=back
=head1 FILES
=head1 BUGS, LIMITATIONS, AND CAVEATS
=head1 NOTES
=head1 AUTHOR
David Meyer <
[email protected]>
=head1 HISTORY
2014-11-11 Originally programmed for totallynuclear.club
=head1 COPYRIGHT AND LICENSE
Copyright 2014, David Meyer
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
=cut
# Emacs control #####################################################
#Local variables:
#mode: perl
#End: