| 1 | package Lavoco::Web::Editor; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Digest::SHA1 qw(sha1_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::Slurp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use FindBin qw($Bin); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Log::AutoDump; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Plack::Handler::FCGI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Term::ANSIColor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use Time::HiRes qw(gettimeofday); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Lavoco::Web::Editor - FastCGI app to edit flat-files. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 VERSION | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Version 0.06 | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | This application was originally designed to aid in the editing of basic templates for a L<Lavoco::Web::App> project. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | use Lavoco::Web::Editor; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | my $editor = Lavoco::Web::Editor->new; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | my $action = lc( $ARGV[0] ); # (start|stop|restart) | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | $editor->$action; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 METHODS | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head2 Class Methods | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head3 new | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Creates a new instance of the editor object. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head2 Attributes | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | has processes => ( is => 'rw', isa => 'Int', default => 5 ); | |||||||||||||||||||||||||||
| 64 | 3 | has _base => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build__base' ); | |||||||||||||||||||||||||||
| 65 | 3 | has _pid => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build__pid' ); | |||||||||||||||||||||||||||
| 66 | 3 | has _socket => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build__socket' ); | |||||||||||||||||||||||||||
| 67 | 3 | has filename => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_filename' ); | |||||||||||||||||||||||||||
| 68 | 3 | has config => ( is => 'rw', isa => 'HashRef' ); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub _build__base | ||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return $Bin; | ||||||||||||||||||||||||||
|
Subroutine
_build__base: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub _build__pid | ||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build__pid: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | return $self->_base . '/editor.pid'; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub _build__socket | ||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build__socket: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | return $self->_base . '/editor.sock'; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub _build_filename | ||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_filename: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | return $self->_base . '/editor.json'; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head3 processes | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Number of FastCGI process to spawn, 5 by default. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head3 filename | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Filename for the config file, default is C<editor.json> and only JSON is currently supported. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head3 config | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | The loaded config as a hash-reference. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 Instance Methods | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head3 start | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Starts the FastCGI daemon. Performs basic checks of your environment and config, dies if there's a problem. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub start | ||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
start: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( -e $self->_pid ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||
| 122 | 0 | print "PID file " . $self->_pid . " already exists, I think you should kill that first, or specify a new pid file with the -p option\n"; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | return $self; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | $self->_init; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | print "Building FastCGI engine...\n"; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | my $server = Plack::Handler::FCGI->new( | |||||||||||||||||||||||||||
| 132 | nproc => $self->processes, | ||||||||||||||||||||||||||||
| 133 | listen => [ $self->_socket ], | ||||||||||||||||||||||||||||
| 134 | pid => $self->_pid, | ||||||||||||||||||||||||||||
| 135 | detach => 1, | ||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | $server->run( $self->_handler ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub _init | ||||||||||||||||||||||||||||
| 142 | { | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_init: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | ############################### | ||||||||||||||||||||||||||||
| 146 | # make sure there's a log dir # | ||||||||||||||||||||||||||||
| 147 | ############################### | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | printf( "%-50s", "Checking logs directory"); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | my $log_dir = $self->_base . '/logs'; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if ( ! -e $log_dir || ! -d $log_dir ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $log_dir and -d $log_dir
MC/DC0%0/2
-e $log_dir and -d $log_dir
-e $log_dir
-d $log_dir
Truth table0%0/3
A and B
A= -e $log_dirB= -d $log_dir
| |||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 0 | _print_red( "[ FAIL ]\n" ); | |||||||||||||||||||||||||||
| 156 | 0 | print $log_dir . " does not exist, or it's not a folder.\nExiting...\n"; | |||||||||||||||||||||||||||
| 157 | 0 | exit; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | _print_green( "[ OK ]\n" ); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | ######################## | ||||||||||||||||||||||||||||
| 163 | # load the config file # | ||||||||||||||||||||||||||||
| 164 | ######################## | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | printf( "%-50s", "Checking config"); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ( ! -e $self->filename ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||
| 170 | 0 | _print_red( "[ FAIL ]\n" ); | |||||||||||||||||||||||||||
| 171 | 0 | print $self->filename . " does not exist.\nExiting...\n"; | |||||||||||||||||||||||||||
| 172 | 0 | exit; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | my $string = read_file( $self->filename, { binmode => ':utf8' } ); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | my $config = undef; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | eval { | |||||||||||||||||||||||||||
| 180 | 0 | $config = decode_json $string; | |||||||||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if ( $@ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||
| 185 | 0 | _print_red( "[ FAIL ]\n" ); | |||||||||||||||||||||||||||
| 186 | 0 | print "Config file error...\n" . $@ . "Exiting...\n"; | |||||||||||||||||||||||||||
| 187 | 0 | exit; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | ################################### | ||||||||||||||||||||||||||||
| 191 | # basic checks on the config file # | ||||||||||||||||||||||||||||
| 192 | ################################### | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if ( $config->{ password } && ! exists $config->{ salt } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$config->{"password"} and not exists $config->{"salt"}
MC/DC0%0/2
$config->{"password"} and not exists $config->{"salt"}
$config->{"password"}
not exists $config->{"salt"}
Truth table0%0/3
A and not(B)
A= $config->{"password"}B= not exists $config->{"salt"}
| |||||||||||||||||||||||||||||
| 195 | { | ||||||||||||||||||||||||||||
| 196 | 0 | _print_red( "[ FAIL ]\n" ); | |||||||||||||||||||||||||||
| 197 | 0 | print "'password' attribute but no 'salt'.\nExiting...\n"; | |||||||||||||||||||||||||||
| 198 | 0 | exit; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ( ! $config->{ files } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | { | ||||||||||||||||||||||||||||
| 204 | 0 | _print_red( "[ FAIL ]\n" ); | |||||||||||||||||||||||||||
| 205 | 0 | print "'files' attribute missing at top level.\nExiting...\n"; | |||||||||||||||||||||||||||
| 206 | 0 | exit; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if ( ref $config->{ files } ne 'ARRAY' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | { | ||||||||||||||||||||||||||||
| 211 | 0 | _print_red( "[ FAIL ]\n" ); | |||||||||||||||||||||||||||
| 212 | 0 | print "'files' attribute is not a list.\nExiting...\n"; | |||||||||||||||||||||||||||
| 213 | 0 | exit; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | ▶ | if ( scalar @{ $config->{ files } } == 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | { | ||||||||||||||||||||||||||||
| 218 | 0 | _print_organge( "[ISSUE]\n" ); | |||||||||||||||||||||||||||
| 219 | 0 | print "No 'files' defined in config, so no files to edit.\n"; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | _print_green( "[ OK ]\n" ); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | return $self; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | sub _print_green | ||||||||||||||||||||||||||||
| 228 | { | ||||||||||||||||||||||||||||
| 229 | 0 | ▶ | my $string = shift; | ||||||||||||||||||||||||||
|
Subroutine
_print_green: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 230 | 0 | print color 'bold green'; | |||||||||||||||||||||||||||
| 231 | 0 | print $string; | |||||||||||||||||||||||||||
| 232 | 0 | print color 'reset'; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | sub _print_orange | ||||||||||||||||||||||||||||
| 236 | { | ||||||||||||||||||||||||||||
| 237 | 0 | ▶ | my $string = shift; | ||||||||||||||||||||||||||
|
Subroutine
_print_orange: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 238 | 0 | print color 'bold orange'; | |||||||||||||||||||||||||||
| 239 | 0 | print $string; | |||||||||||||||||||||||||||
| 240 | 0 | print color 'reset'; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | sub _print_red | ||||||||||||||||||||||||||||
| 244 | { | ||||||||||||||||||||||||||||
| 245 | 0 | ▶ | my $string = shift; | ||||||||||||||||||||||||||
|
Subroutine
_print_red: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 246 | 0 | print color 'bold red'; | |||||||||||||||||||||||||||
| 247 | 0 | print $string; | |||||||||||||||||||||||||||
| 248 | 0 | print color 'reset'; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head3 stop | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Stops the FastCGI daemon. | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | sub stop | ||||||||||||||||||||||||||||
| 258 | { | ||||||||||||||||||||||||||||
| 259 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 0 | ▶ | if ( ! -e $self->_pid ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | { | ||||||||||||||||||||||||||||
| 263 | 0 | return $self; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | open( my $fh, "<", $self->_pid ) or die "Cannot open pidfile: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | my @pids = <$fh>; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | close $fh; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 0 | chomp( $pids[0] ); | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 0 | print "Killing pid $pids[0] ...\n"; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | kill 15, $pids[0]; | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 0 | return $self; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head3 restart | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | Restarts the FastCGI daemon, with a 1 second delay between stopping and starting. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | =cut | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | sub restart | ||||||||||||||||||||||||||||
| 288 | { | ||||||||||||||||||||||||||||
| 289 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
restart: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 0 | $self->stop; | |||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 0 | $self->start; | |||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | return $self; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | The editor app should be a simple Perl script in a folder with the following structure: | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | editor.pl # see the synopsis | ||||||||||||||||||||||||||||
| 305 | editor.json # config, see below | ||||||||||||||||||||||||||||
| 306 | editor.pid # generated, to control the process | ||||||||||||||||||||||||||||
| 307 | editor.sock # generated, to accept incoming FastCGI connections | ||||||||||||||||||||||||||||
| 308 | logs/ | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | The config file is read for each and every request, so you can reasonably enable editing of the editors own config file. | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | See the C<examples> directory for a sample JSON config file, similar to the following... | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | { | ||||||||||||||||||||||||||||
| 315 | "folders" : [ | ||||||||||||||||||||||||||||
| 316 | "templates/content/organic", | ||||||||||||||||||||||||||||
| 317 | "templates/content/store" | ||||||||||||||||||||||||||||
| 318 | ], | ||||||||||||||||||||||||||||
| 319 | "files" : [ | ||||||||||||||||||||||||||||
| 320 | "app.json", | ||||||||||||||||||||||||||||
| 321 | "site/style.css" | ||||||||||||||||||||||||||||
| 322 | ], | ||||||||||||||||||||||||||||
| 323 | "password" : "foo", | ||||||||||||||||||||||||||||
| 324 | "salt" : "abc123" | ||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | Two fields which drive the editor are C<folders> and C<files>, each of which is an array of paths, all relative to the base directory of the editor script. | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | All visible files in the C<folders> are editable, but not sub-directories, you need to add those separately. The editor can also create new files in each folder. | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | Files in the C<files> list are editable, but the editor can not create new files in their respective containing directories. | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | If there is a defined C<password> in the config, then this will be requested before a user can access the index page (listing all files that can be edited). | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | When using a C<password>, a C<salt> is also required, just create a random string, it's simply concatenated to the password before SHA-hashing and setting as a cookie. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =cut | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | # returns a code-ref for the FCGI handler/server. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | sub _handler | ||||||||||||||||||||||||||||
| 342 | { | ||||||||||||||||||||||||||||
| 343 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_handler: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | return sub { | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | ############## | ||||||||||||||||||||||||||||
| 348 | # initialise # | ||||||||||||||||||||||||||||
| 349 | ############## | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 0 | ▶ | my $req = Plack::Request->new( shift ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=38 SCAR=73.0
| |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 0 | my $res = $req->new_response; | |||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 0 | my %stash = ( | |||||||||||||||||||||||||||
| 356 | app => $self, | ||||||||||||||||||||||||||||
| 357 | req => $req, | ||||||||||||||||||||||||||||
| 358 | now => DateTime->now, | ||||||||||||||||||||||||||||
| 359 | started => join( '.', gettimeofday ), | ||||||||||||||||||||||||||||
| 360 | ); | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 0 | my $log = Log::AutoDump->new( base_dir => $stash{ app }->_base . '/logs', filename => 'editor.log' ); | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | $log->debug("Started"); | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 0 | my $path = $req->uri->path; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 0 | $log->debug( "Requested path: " . $path ); | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 0 | $stash{ app }->_reload_config( log => $log ); | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | ############################### | ||||||||||||||||||||||||||||
| 373 | # check for password required # | ||||||||||||||||||||||||||||
| 374 | ############################### | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 0 | my $template = 'login.tt'; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 0 | ▶ | if ( ! exists $stash{ app }->config->{ password } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | { | ||||||||||||||||||||||||||||
| 380 | 0 | $log->debug( "No password set, so going straight to index.tt" ); | |||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 0 | $template = 'index.tt'; | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | else | ||||||||||||||||||||||||||||
| 385 | { | ||||||||||||||||||||||||||||
| 386 | 0 | ▶ | if ( exists $req->parameters->{ password } ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 387 | { | ||||||||||||||||||||||||||||
| 388 | 0 | ▶ | if ( $req->parameters->{ password } eq $stash{ app }->config->{ password } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | { | ||||||||||||||||||||||||||||
| 390 | 0 | $res->cookies->{ password } = sha1_hex( $stash{ app }->config->{ salt } . $stash{ app }->config->{ password } ); | |||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 0 | $template = 'index.tt'; | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | else | ||||||||||||||||||||||||||||
| 395 | { | ||||||||||||||||||||||||||||
| 396 | 0 | $res->cookies->{ password } = ''; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | elsif ( $req->cookies->{ password } ) | ||||||||||||||||||||||||||||
| 400 | { | ||||||||||||||||||||||||||||
| 401 | 0 | $log->debug( "We have a cookie for a password" ); | |||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 0 | ▶ | if ( $req->cookies->{ password } eq sha1_hex( $stash{ app }->config->{ salt } . $stash{ app }->config->{ password } ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | { | ||||||||||||||||||||||||||||
| 405 | 0 | $log->debug( "Cookie matches sha1 hash" ); | |||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 0 | $template = 'index.tt'; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 0 | ▶ | if ( $template ne 'login.tt' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | { | ||||||||||||||||||||||||||||
| 414 | 0 | my @folders = (); | |||||||||||||||||||||||||||
| 415 | 0 | my @files = (); | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | ########### | ||||||||||||||||||||||||||||
| 418 | # folders # | ||||||||||||||||||||||||||||
| 419 | ########### | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 0 | ▶ | if ( exists $stash{ app }->config->{ folders } && @{ $stash{ app }->config->{ folders } } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $stash{"app"}->config->{"folders"} and @{$stash{"app"}->config->{"folders"};}
MC/DC0%0/2
exists $stash{"app"}->config->{"folders"} and @{$stash{"app"}->config->{"folders"};}
exists $stash{"app"}->config->{"folders"}
@{$stash{"app"}->config->{"folders"};}
Truth table0%0/3
A and B
A= exists $stash{"app"}->config->{"folders"}B= @{$stash{"app"}->config->{"folders"};}
| |||||||||||||||||||||||||||||
| 422 | { | ||||||||||||||||||||||||||||
| 423 | 0 | foreach my $folder ( @{ $stash{ app }->config->{ folders } } ) | |||||||||||||||||||||||||||
| 424 | { | ||||||||||||||||||||||||||||
| 425 | 0 | $folder =~ s/^\///g; # remove leading slashes | |||||||||||||||||||||||||||
| 426 | 0 | $folder =~ s/\/$//g; # remove trailing slashes | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 0 | $log->debug( "Processing folder: " . $folder ); | |||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 0 | my $path = $stash{ app }->_base . '/' . $folder; | |||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 0 | ▶ | if ( -d $path ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 433 | { | ||||||||||||||||||||||||||||
| 434 | 0 | my %folder = ( path => $folder, files => [ ] ); | |||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 0 | ▶ | opendir( my $dh, $path ) || $log->debug("Can't opendir $path: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 0 | push @{ $folder{ files } }, sort { $a cmp $b } grep { ! -d ( $stash{ app }->_base . '/' . $folder . '/' . $_ ) } grep { $_ !~ /^\./ } readdir( $dh ); | |||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 0 | closedir( $dh ); | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | push @folders, \%folder; | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | ######### | ||||||||||||||||||||||||||||
| 448 | # files # | ||||||||||||||||||||||||||||
| 449 | ######### | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 0 | ▶ | if ( exists $stash{ app }->config->{ files } && @{ $stash{ app }->config->{ files } } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $stash{"app"}->config->{"files"} and @{$stash{"app"}->config->{"files"};}
MC/DC0%0/2
exists $stash{"app"}->config->{"files"} and @{$stash{"app"}->config->{"files"};}
exists $stash{"app"}->config->{"files"}
@{$stash{"app"}->config->{"files"};}
Truth table0%0/3
A and B
A= exists $stash{"app"}->config->{"files"}B= @{$stash{"app"}->config->{"files"};}
| |||||||||||||||||||||||||||||
| 452 | { | ||||||||||||||||||||||||||||
| 453 | 0 | foreach my $file ( @{ $stash{ app }->config->{ files } } ) | |||||||||||||||||||||||||||
| 454 | { | ||||||||||||||||||||||||||||
| 455 | 0 | $file =~ s/^\///g; # remove leading slashes | |||||||||||||||||||||||||||
| 456 | 0 | $file =~ s/\/$//g; # remove trailing slashes | |||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 0 | $log->debug( "Processing file: " . $file ); | |||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 0 | my $path = $stash{ app }->_base . '/' . $file; | |||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | 0 | ▶ | if ( -f $path ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 463 | { | ||||||||||||||||||||||||||||
| 464 | 0 | push @files, $file; | |||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | ###################################### | ||||||||||||||||||||||||||||
| 470 | # if we've requested a file, edit it # | ||||||||||||||||||||||||||||
| 471 | ###################################### | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | 0 | ▶ | if ( exists $req->parameters->{ folder } ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 474 | { | ||||||||||||||||||||||||||||
| 475 | 0 | foreach my $folder ( @folders ) | |||||||||||||||||||||||||||
| 476 | { | ||||||||||||||||||||||||||||
| 477 | 0 | ▶ | next if $folder->{ path } ne $req->parameters->{ folder }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | 0 | $stash{ folder } = $req->parameters->{ folder }; | |||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | 0 | $template = 'edit.tt'; | |||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 0 | ▶ | if ( $req->parameters->{ file } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 484 | { | ||||||||||||||||||||||||||||
| 485 | 0 | foreach my $file ( @{ $folder->{ files } } ) | |||||||||||||||||||||||||||
| 486 | { | ||||||||||||||||||||||||||||
| 487 | 0 | ▶ | next if $file ne $req->parameters->{ file }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 0 | $stash{ file } = $req->parameters->{ file }; | |||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | 0 | ▶ | if ( ! exists $req->parameters->{ content } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | { | ||||||||||||||||||||||||||||
| 493 | 0 | $log->debug( "Reading content of " . $stash{ app }->_base . '/' . $folder->{ path } . '/' . $file ); | |||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 0 | $stash{ content } = read_file( $stash{ app }->_base . '/' . $folder->{ path } . '/' . $file, { binmode => ':utf8' } ); | |||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 0 | ▶ | if ( exists $req->parameters->{ content } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | { | ||||||||||||||||||||||||||||
| 502 | 0 | $log->debug( "We've got some content" ); | |||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | 0 | ▶ | if ( $req->parameters->{ file } =~ /\.json/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 505 | { | ||||||||||||||||||||||||||||
| 506 | 0 | $log->debug( "It's a JSON file" ); | |||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | ######################### | ||||||||||||||||||||||||||||
| 509 | # basic json validation # | ||||||||||||||||||||||||||||
| 510 | ######################### | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 0 | eval { | |||||||||||||||||||||||||||
| 513 | 0 | my $json = JSON->new; | |||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | 0 | $json->relaxed( 1 ); | |||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | 0 | $json->decode( $req->parameters->{ content } ); | |||||||||||||||||||||||||||
| 518 | }; | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | 0 | ▶ | $log->debug( $@ ) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | 0 | ▶ | $stash{ error } = $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | 0 | ▶ | if ( ! exists $stash{ error } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 526 | { | ||||||||||||||||||||||||||||
| 527 | 0 | write_file( $stash{ app }->_base . '/' . $folder->{ path } . '/' . $req->parameters->{ file }, { binmode => ':utf8' }, $req->parameters->{ content } ); | |||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | 0 | $stash{ success } = "Saved OK"; | |||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | 0 | $stash{ file } = $req->parameters->{ file }; | |||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 0 | $stash{ content } = $req->parameters->{ content }; | |||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||
| 538 | elsif ( exists $req->parameters->{ file } ) | ||||||||||||||||||||||||||||
| 539 | { | ||||||||||||||||||||||||||||
| 540 | 0 | foreach my $file ( @files ) | |||||||||||||||||||||||||||
| 541 | { | ||||||||||||||||||||||||||||
| 542 | 0 | ▶ | next if $file ne $req->parameters->{ file }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | 0 | $stash{ file } = $req->parameters->{ file }; | |||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | 0 | $template = 'edit.tt'; | |||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | 0 | ▶ | if ( ! exists $req->parameters->{ content } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 549 | { | ||||||||||||||||||||||||||||
| 550 | 0 | $log->debug( "Reading content of " . $stash{ app }->_base . '/' . $file ); | |||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | 0 | $stash{ content } = read_file( $stash{ app }->_base . '/' . $file, { binmode => ':utf8' } ); | |||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||
| 554 | else | ||||||||||||||||||||||||||||
| 555 | { | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | 0 | ▶ | if ( $req->parameters->{ file } =~ /\.json/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 558 | { | ||||||||||||||||||||||||||||
| 559 | 0 | $log->debug( "It's a JSON file" ); | |||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | ######################### | ||||||||||||||||||||||||||||
| 562 | # basic json validation # | ||||||||||||||||||||||||||||
| 563 | ######################### | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | 0 | eval { | |||||||||||||||||||||||||||
| 566 | 0 | my $json = JSON->new; | |||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | 0 | $json->relaxed( 1 ); | |||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | 0 | $json->decode( $req->parameters->{ content } ); | |||||||||||||||||||||||||||
| 571 | }; | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | 0 | ▶ | $log->debug( $@ ) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | 0 | ▶ | $stash{ error } = $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | 0 | ▶ | if ( ! exists $stash{ error } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 579 | { | ||||||||||||||||||||||||||||
| 580 | 0 | write_file( $stash{ app }->_base . '/' . $file, { binmode => ':utf8' }, $req->parameters->{ content } ); | |||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | 0 | $stash{ success } = "Saved OK"; | |||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | 0 | $stash{ file } = $req->parameters->{ file }; | |||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | 0 | $stash{ content } = $req->parameters->{ content }; | |||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | 0 | $stash{ folders } = \@folders; | |||||||||||||||||||||||||||
| 593 | 0 | $stash{ files } = \@files; | |||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | ############################## | ||||||||||||||||||||||||||||
| 597 | # responding with a template # | ||||||||||||||||||||||||||||
| 598 | ############################## | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | 0 | ▶ | $stash{ error } =~ s/ at \/.*$// if exists $stash{ error }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | 0 | $res->status( 200 ); | |||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | 0 | my $tt = Template->new( ENCODING => 'UTF-8' ); | |||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | 0 | $log->debug("Processing template: " . $template ); | |||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | 0 | my $body = ''; | |||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | 0 | ▶ | $tt->process( $stash{ app }->_template_tt( $template ), \%stash, \$body ) or $log->debug( $tt->error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | 0 | $res->content_type('text/html; charset=utf-8'); | |||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | 0 | $res->body( encode( "UTF-8", $body ) ); | |||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | ######### | ||||||||||||||||||||||||||||
| 617 | # stats # | ||||||||||||||||||||||||||||
| 618 | ######### | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | 0 | $stash{ took } = join( '.', gettimeofday ) - $stash{ started }; | |||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | 0 | $log->debug( "Took " . sprintf("%.5f", $stash{ took } ) . " seconds"); | |||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | 0 | return $res->finalize; | |||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||
| 626 | 0 | } | |||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | sub _reload_config | ||||||||||||||||||||||||||||
| 629 | { | ||||||||||||||||||||||||||||
| 630 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_reload_config: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | 0 | my $log = $args{ log }; | |||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | 0 | $log->debug( "Opening config file: " . $self->filename ); | |||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | 0 | my $string = read_file( $self->filename, { binmode => ':utf8' } ); | |||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | 0 | my $config = undef; | |||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | 0 | eval { | |||||||||||||||||||||||||||
| 641 | 0 | $self->config( decode_json $string ); | |||||||||||||||||||||||||||
| 642 | }; | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | 0 | ▶ | $log->debug( $@ ) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | 0 | return $self; | |||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | # returns a scalar-ref to feed into TT | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | sub _template_tt | ||||||||||||||||||||||||||||
| 652 | { | ||||||||||||||||||||||||||||
| 653 | 0 | ▶ | my ( $self, $template ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_template_tt: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | 0 | my $string = ''; | |||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | 0 | ▶ | if ( $template eq 'login.tt' ) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 658 | { | ||||||||||||||||||||||||||||
| 659 | 0 | $string = <<EOF; | |||||||||||||||||||||||||||
| 660 | <html> | ||||||||||||||||||||||||||||
| 661 | <head> | ||||||||||||||||||||||||||||
| 662 | <style> | ||||||||||||||||||||||||||||
| 663 | body { font-family: Tahoma,Arial,Helvetica,sans-serif; } | ||||||||||||||||||||||||||||
| 664 | </style> | ||||||||||||||||||||||||||||
| 665 | </head> | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | <body> | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | <h1>Website Content Editor</h2> | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | <form action="/" method="POST"> | ||||||||||||||||||||||||||||
| 672 | <input type="text" name="password" value="" style="float: left; clear: both;"> | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | <input type="submit" value="Login" style="float: left; clear: both;"> | ||||||||||||||||||||||||||||
| 675 | </form> | ||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | </body> | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | </html> | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | EOF | ||||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||
| 683 | elsif ( $template eq 'index.tt' ) | ||||||||||||||||||||||||||||
| 684 | { | ||||||||||||||||||||||||||||
| 685 | 0 | $string = <<EOF; | |||||||||||||||||||||||||||
| 686 | <html> | ||||||||||||||||||||||||||||
| 687 | <head> | ||||||||||||||||||||||||||||
| 688 | <style> | ||||||||||||||||||||||||||||
| 689 | body { font-family: Tahoma,Arial,Helvetica,sans-serif; } | ||||||||||||||||||||||||||||
| 690 | ul li { margin-top: 5px; } | ||||||||||||||||||||||||||||
| 691 | a,a:visited { color: #0000EE; } | ||||||||||||||||||||||||||||
| 692 | </style> | ||||||||||||||||||||||||||||
| 693 | </head> | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | <body> | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | <h1>Website Content Editor</h2> | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | <h3><a href="/?password=">Logout</a></h3> | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | <ul> | ||||||||||||||||||||||||||||
| 702 | [% FOREACH folder IN folders %] | ||||||||||||||||||||||||||||
| 703 | <li> | ||||||||||||||||||||||||||||
| 704 | <strong>[% folder.path %]/</strong> [ <a href="/?folder=[% folder.path | uri %]">create new file</a> ] | ||||||||||||||||||||||||||||
| 705 | <ul> | ||||||||||||||||||||||||||||
| 706 | [% FOREACH file IN folder.files %] | ||||||||||||||||||||||||||||
| 707 | <li><a href="/?folder=[% folder.path | uri %]&file=[% file | uri%]">[% file %]</a></li> | ||||||||||||||||||||||||||||
| 708 | [% END %] | ||||||||||||||||||||||||||||
| 709 | </ul> | ||||||||||||||||||||||||||||
| 710 | </li> | ||||||||||||||||||||||||||||
| 711 | [% END %] | ||||||||||||||||||||||||||||
| 712 | [% IF files.size %] | ||||||||||||||||||||||||||||
| 713 | <li><strong>/</strong> | ||||||||||||||||||||||||||||
| 714 | <ul> | ||||||||||||||||||||||||||||
| 715 | [% FOREACH file IN files %] | ||||||||||||||||||||||||||||
| 716 | <li><a href="/?file=[% file | uri%]">[% file %]</a></li> | ||||||||||||||||||||||||||||
| 717 | [% END %] | ||||||||||||||||||||||||||||
| 718 | </ul> | ||||||||||||||||||||||||||||
| 719 | </li> | ||||||||||||||||||||||||||||
| 720 | [% END %] | ||||||||||||||||||||||||||||
| 721 | </ul> | ||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | </body> | ||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||
| 725 | </html> | ||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||
| 727 | EOF | ||||||||||||||||||||||||||||
| 728 | } | ||||||||||||||||||||||||||||
| 729 | elsif ( $template eq 'edit.tt' ) | ||||||||||||||||||||||||||||
| 730 | { | ||||||||||||||||||||||||||||
| 731 | 0 | $string = <<EOF; | |||||||||||||||||||||||||||
| 732 | <html> | ||||||||||||||||||||||||||||
| 733 | <head> | ||||||||||||||||||||||||||||
| 734 | <style> | ||||||||||||||||||||||||||||
| 735 | body { font-family: Tahoma,Arial,Helvetica,sans-serif; } | ||||||||||||||||||||||||||||
| 736 | a,a:visited { color: #0000EE; } | ||||||||||||||||||||||||||||
| 737 | </style> | ||||||||||||||||||||||||||||
| 738 | </head> | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | <body> | ||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | <h1><a href="/">Website Content Editor</a></h2> | ||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||
| 744 | <h2>[% folder %]/[% file %][% IF success %] - <span style="color: #0c0;">[% success %]</span>[% END %][% IF error %] - <span style="color: #f00;">[% error %]</span>[% END %]</h2> | ||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | <form action="/" method="POST"> | ||||||||||||||||||||||||||||
| 747 | [% IF folder %] | ||||||||||||||||||||||||||||
| 748 | <input type="hidden" name="folder" value="[% folder | html %]"> | ||||||||||||||||||||||||||||
| 749 | [% END %] | ||||||||||||||||||||||||||||
| 750 | [% IF file %] | ||||||||||||||||||||||||||||
| 751 | <input type="hidden" name="file" value="[% file | html %]"> | ||||||||||||||||||||||||||||
| 752 | [% ELSE %] | ||||||||||||||||||||||||||||
| 753 | <label for="file">New filename</label> | ||||||||||||||||||||||||||||
| 754 | <input type="text" id="file" name="file" value="" style="margin-bottom: 10px;"> | ||||||||||||||||||||||||||||
| 755 | [% END %] | ||||||||||||||||||||||||||||
| 756 | <textarea name="content" style="float: left; width: 100%; height: 600px;">[% content | html %]</textarea> | ||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | <input type="submit" value="Save Changes" style="float: left; clear: both;"> | ||||||||||||||||||||||||||||
| 759 | </form> | ||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||
| 761 | </body> | ||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||
| 763 | </html> | ||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||
| 765 | EOF | ||||||||||||||||||||||||||||
| 766 | } | ||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||
| 768 | 0 | return \$string; | |||||||||||||||||||||||||||
| 769 | } | ||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||
| 771 | =head1 TODO | ||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | Allow absolute paths to any part of the filesystem? | ||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||
| 775 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||
| 777 | Rob Brown, C<< <rob at intelcompute.com> >> | ||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||
| 779 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||
| 781 | Copyright 2015 Rob Brown. | ||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||
| 783 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 784 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 785 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||
| 787 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||
| 789 | =cut | ||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||
| 791 | 3 | 1; | |||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||