-
Notifications
You must be signed in to change notification settings - Fork 5
/
wgcmd
executable file
·630 lines (496 loc) · 15.5 KB
/
wgcmd
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#!/usr/bin/perl
# wgcmd - command-line replacement for confarc. Controls flow of webglimpse management interface.
#
# Run on command line only, wgarcmin.cgi is the cgi-script version
#
# Uses package CommandWeb (generic, useful for dual command-line/web interfaces)
# Includes functions
#
# ParseCommandLine Builds hash from command line options
# ValidatePassword Check password vs encrypted file
# PromptUser Command line prompts for inputting archive settings
#
# Uses packages wgConf, wgArch (specific to Webglimpse)
# wgConf handles the user interface
# wgArch represents an archive
#
my $REVISION = '$Id $';
BEGIN {
$WEBGLIMPSE_LIB = '|WEBGLIMPSE_LIB|';
unshift(@INC, "$WEBGLIMPSE_LIB"); # Find the rest of our libs
}
BEGIN {
use wgHeader qw( :all );
use wgTextMsgs qw( :wgarcmin );
use wgErrors;
}
my $debug = 0;
my $demomode = 0;
# Public libraries we need installed
use CGI qw(:cgi-lib);
# Our own libraries/modules
use wgConf;
use wgArch;
use wgSite;
use wgSiteConf;
use CommandWeb;
use CatTree;
# Initialize variables and load domain information
$WEBGLIMPSE = '/'.$CGIBIN.'/webglimpse.cgi';
&wgSiteConf::LoadSites;
my $srvname = $wgSiteConf::LocalServerName || 'localhost';
$WGARCMIN = "http://$srvname/$CGIBIN/wgarcmin.cgi";
# Customize messages for command-line version
$wgTextMsgs::CONFIGURE_DOMAIN = $wgTextMsgs::CONFIGURE_DOMAIN_CMD;
my ($next, $continue, $wgConf);
my $USEUPPER = 1;
my %in = ();
my $cmdtemplatefile;
my %templatehash = ();
$lastError = '';
# We may store some vars in templatehash from the next as well as
my %templatehash = ();
my @getfromuser = ();
my @catarr = (); # Array of category hashes (has to be structured this way for CommandWeb)
$continue = 1;
$auto = 0;
$menulevel = 0;
$ArchID = 0; # working archive - global
$ArchDir = '';
$Query = '';
$LastStatus = ''; # status of last next - global
my $mArch = 0; # Global archive object so we don't have to re-load
# We may be called from install to create new archive
if ($ARGV[0] eq 'N') {
$next = 'N';
$auto = 1;
} else {
print $MSG_INTRO;
}
#&CommandWeb::ParseCommandLine(\%in);
#
# We have Menus and Actions
#
# Menus:
#
# L = List archives
#
# Actions:
# N = New archive
# A = Add site/dir/tree to current archive
# B = Build current archive
# D = Delete current archive
# E = Edit archive settings (not implemented)
# ? = Help
# X = Exit program
while ($continue) {
# print relevent menu
if (! $auto) {
$next = &MenuOut($menulevel);
}
# may need data from user into global %in
&GetMoreInput;
if ($auto) {
$auto = 0;
}
# do something (and possibly set auto=1 to do something else next)
&DoIt($next);
# print status of last action unless automatically continuing
if (!$auto) {
&PrintStatus;
}
}
1;
sub SetArchiveTo {
$num = shift;
$mArch = &wgConf::GetArch($num);
if ($mArch) {
$ArchID = $num;
$ArchDir = $mArch->Get('Dir');
}
else {
$LastStatus = "$num is not a valid archive ID";
$ArchID = 0;
}
}
sub PrintStatus {
if ($LastStatus) {
print "\n$LastStatus\n";
$LastStatus = '';
}
if ($ArchID && $mArch) {
print "\n Current working archive: # $ArchID : ",$mArch->Get('Title')," : ",$mArch->Get('StatusMsg'),"\n\n";
}
return 1;
}
sub DoIt {
my $action = shift;
##############################################################
# Create Archive - check and save settings passed to script
# create files associated with archive
# On command line, this may happen as part of New or Edit
if ($action eq 'N') {
# Its ok if we can't load archives, maybe there aren't any yet.
&wgConf::LoadArchs();
$ArchID = &wgConf::GenArchID() || print("wgConf::GenArchID: $lastError") && return(0);
$mArch = wgArch->new($ArchID) || print("wgArch::new: $lastError. Arch id was $ArchID") && return(0);
&CommandWeb::AssignInputs(\%in, $mArch, \@wgArch::members) || print("CommandWeb::AssignInputs failed on archive $ArchID") && return(0);
# $mArch->Validate();
$mArch->Create() || print("wgArch::Create: $ret: $lastError") && return(0);
# if main archive dir is web-writable, make this one so too
$ArchDir = $mArch->Get('Dir');
MakeWebWritableIf($ArchDir);
$auto = 1;
$next = 'A';
}
# Adding a Root to an archive
elsif ($action eq 'A') {
if (!$ArchID) {
$LastStatus = "Cannot add root, no archive is defined";
return 0;
}
$mArch = &wgConf::GetArch($ArchID) || ($LastStatus = "wgConf::GetArch: $lastError, Arch ID was $ArchID") && return(0);
$type = $in{'TYPE'};
$mRoot = new wgRoot;
&CommandWeb::AssignInputs(\%in, $mRoot, \@wgRoot::members) || print("CommandWeb::AssignInputs failed on root") && return(0);
$mRoot->Validate() || print("wgRoot:Validate: $lastError") && return(0);
$mArch->LoadRoots || print("wgArch::LoadRoots: $lastError") && return(0);
$mArch->AddRoot($mRoot) || print ("wgArch::AddRoot: $lastError") && return(0);
$mArch->SaveRoots || print("wgArch::SaveRoots: $lastError") && return(0);
MakeWebWritableIf($mArch->{Dir},$mArch->{Dir});
$auto = 1;
$next = 'B';
}
# Save changes to an archive
elsif ($action eq 'E') {
if (!$ArchID) {
$LastStatus = "Cannot edit, no archive is defined";
return 0;
}
if (! $mArch) { # Should have been loaded in GetUserInputs
$LastStatus = "Archive $ArchID is not loaded\n";
$next = '';
next;
}
&CommandWeb::AssignInputs(\%in, $mArch, \@wgArch::members) || print("CommandWeb::AssignInputs failed on arch $ArchID") && return(0);
$mArch->MakeArchFiles; # Rebuild template files with new info
&wgConf::SaveArch($mArch) || print("wgConf::SaveArch: $lastError, archive id was $ArchID") && return(0);
}
elsif ($action =~ /^[0-9]+$/) {
SetArchiveTo($action);
}
##############################################################
# Search archive using Glimpse
elsif (($action eq 'S') || ($action eq 'G')) {
if (!$ArchID) {
$LastStatus = "Cannot search, no archive is defined";
return 0;
}
if (!$Query) {
$LastStatus = "Nothing to search for!";
return 0;
}
$mArch = &wgConf::GetArch($ArchID) || print("wgConf::GetArch: $lastError, id was $ArchID") && return(0);
my $cmd = "$GLIMPSE_LOC -U -X -j -y -i -w -H ".$mArch->{Dir}." '$Query'";
print "Search Results:\n\n";
print `$cmd`;
$LastStatus = "Ran search using command: $cmd \n";
}
##############################################################
# Build Archive - run wgreindex, if we have permissions to
elsif ($action eq 'B') {
if (!$ArchID) {
$LastStatus = "Cannot build, no archive is defined";
return 0;
}
$mArch = &wgConf::GetArch($ArchID) || print("wgConf::GetArch: $lastError, id was $ArchID") && return(0);
$mArch->Validate || print("wgArch::Validate: $lastError") && return(0);
# We may want to output the build results to an extra output window
my $extrawin = '';
if ($mArch->Build) {
$LastStatus = 'Build in progress. (Running in background)';
} else {
$LastStatus = 'Error building archive: $lastError';
}
MakeWebWritableIf($mArch->{Dir},$mArch->{Dir});
# Might put in an option to do this later; for now building in the background.
# if ($extrawin) {
# $templatehash{'EXTRAWIN'} = $extrawin;
# }
$auto = 1;
$next = '?S';
}
################################################################
# Delete archive
elsif ($action eq 'D') {
if (!$ArchID) {
$LastStatus = "Cannot delete, no archive is defined";
return 0;
}
$mArch = &wgConf::GetArch($ArchID) || print("wgConf::GetArch: $lastError, id was $ArchID") && return(0);
if ($mArch->Destroy) {
$LastStatus = "Archive # $ArchID deleted.\n";
$ArchID = 0;
} else {
$LastStatus = "wgArch::Destroy: $lastError";
}
}
################################################################
# Exit
elsif ($action eq 'X') {
$continue = 0;
}
################################################################
# Unknown action code
elsif ($action) {
}
################################################################
# No action
else {
}
return 1;
}
sub MenuOut {
$lvl = shift;
if ($ArchID) {
$mprompt = &subvars($PROMPT_NEXT_CMD);
} else {
$mprompt = &subvars($PROMPT_FIRST_CMD);
}
$next = &CommandWeb::prompt($mprompt);
if (($next =~ /^([A-Z#])\s+([0-9]+)$/i) && ($1 ne 'S')) {
$next = uc($1);
SetArchiveTo($2);
}
# Special patterns for search command
elsif (($next =~ /^S\s+\'([^\']+)\'\s*([0-9]*)$/) || ($next =~ /^S\s+(\S+)\s*([0-9]*)$/)) {
$next = 'S';
$Query = $1;
if ($2) { SetArchiveTo($2); }
} elsif ($next =~ /^S\s+([0-9]+)\s+(\S*\D+\S*)$/) { # other order ok, if not searching for digits only
$next = 'S';
$Query = $2;
SetArchiveTo($1);
} else {
$next = uc($next);
}
return($next);
}
##############################################################
##############################################################
sub LoadArchive {
my $march;
# Did user pass Archive ID or path? Check local dir.
my $id = shift || $ArchID || $in{'ID'} || $in{'ARCHID'};
if (defined($id)) { # User-specified archive
$march = &wgConf::GetArch($id);
} elsif (defined($in{'ARCHDIR'})) { # User-specified directory
$march = &wgConf::GetArchbyPath($in{'ARCHDIR'});
} elsif (!$CalledFromWeb) { # Try local dir
$march = &wgConf::GetArchbyPath('.');
}
return($march);
}
sub mygetlist {
foreach $var (@_) {
$in{$var} = myget($var);
}
}
sub myget {
my $var = shift;
my $def = shift || '';
my $prompt = shift || $Prompts{$var} || "Value for $var";
return($in{$var} = &CommandWeb::prompt($prompt,$def));
}
# Prompt user for inputs needed
#
# Actions:
# N = New archive
# A = Add site/dir/tree to current archive
# B = Build current archive
# D = Delete current archive
# E = Edit archive settings (not implemented)
# ? = Help
# X = Exit program
#
# Would be nice if we could do these things through text templates somehow, the way
# we do for web interface...
sub GetMoreInput {
if ($next eq 'A') {
if (!defined($ArchID) || ($ArchID < 1) ) {
print "First you must create an archive or select an existing archive from the list\n";
return '';
}
$type = &myget('TYPE','D');
if ($type =~ /^D/i ) {
$in{'TYPE'} = 'DIR';
myget('STARTDIR','','Directory to index:');
myget('STARTURL','','Equivalent URL:');
myget('MAXLOCAL',250);
} elsif ($type =~ /^S/i ) {
$in{'TYPE'} = 'SITE';
myget('STARTURL','','Site URL:');
myget('MAXLOCAL',250);
myget('HOPS',10);
} elsif ($type =~ /^T/i ) {
$in{'TYPE'} = 'TREE';
myget('STARTURL','','Starting URL for link traversal:');
mygetlist('HOPS', 'MAXLOCAL','MAXREMOTE', 'FOLLOWTOREMOTE', 'FOLLOWSAMESITE', 'FOLLOWALL');
}
} elsif ($next eq 'N') {
$ArchID = $in{'ID'} = &wgConf::GenArchID() || print("wgConf::GenArchID: $lastError") && return(0);
&myget('TITLE');
} elsif ($next eq 'B') {
# Nothing to do here, build is all done in DoIt
if ($auto) {
# confirm build if running on autopilot
my $yesno = &CommandWeb::prompt('Build archive now?','Y');
if ($yesno !~ /^[yY]/) {
$next = '';
$auto = 0;
}
}
} elsif ($next eq 'I') {
if ($ArchID && $mArch) {
my $fname = $mArch->{Dir}.'/'.$CONFIGFILE;
print "Archive configuration from $fname:\n";
open(F,$fname);
print <F>;
close F;
# TODO: just read first line of .glimpse_filenames instead!!
my $wc = trim(`wc $mArch->{Dir}/.glimpse_filenames`) - 1;
@_ = split(/\s+/,$wc);
print "\nNumber of files indexed: $_[0]\n\n";
} else {
$LastStatus .= "\nFor which archive? Use 'L' to get a list of available archives.\n";
}
} elsif ($next eq 'S') {
# done in DoIt
} elsif ($next eq 'L') {
&wgConf::LoadArchs;
print "\nAvailable archives:\n";
my @arr = sort { $a->{ID} <=> $b->{ID} } (values %wgConf::Archives);
foreach $march (@arr) {
print $march->Get('ID'),"\t: ",$march->Get('Title'),"\t: ",$march->Get('StatusMsg'),"\n";
}
print "\n";
} elsif ($next eq 'D') {
if ($ArchID) {
if (myget('verify','N',"Are you sure you want to delete archive # $ArchID? ") !~ /^y/i) {
$next = '';
return;
}
} else {
myget('ID',$ArchID,'ID of archive to delete: ');
SetArchiveTo($ArchID);
}
} elsif ($next eq 'X') {
print "$MSG_EXITING";
$continue = 0;
} elsif ($next eq '?') {
print "$MSG_HELP";
} elsif ($next eq '?E') {
print &subvars($MSG_EDIT_ARCHIVECFG);
} elsif ($next eq '?M') {
print &subvars($MSG_EDIT_ARCHIVECFG);
} elsif ($next eq '?H') {
print &subvars($MSG_EDIT_WGSITES);
} elsif ($next eq '?O') {
print &subvars($MSG_EDIT_ARCHIVES);
} elsif ($next eq '?S') {
print &subvars($MSG_WGINDEXHTML);
} elsif ($next eq '?G') {
print &subvars($MSG_GLIMPSE);
} elsif ($next eq '?W') {
print &subvars($MSG_WEB_INTERFACE);
}
return $next;
}
# Substitute our local vars into prompts & messages
sub subvars {
my $msg = shift;
# substitute our vars as needed
# We always have these
$msg =~ s/\|LASTERROR\|/$lastError/g;
$msg =~ s/\|DOCURL\|/$DOCURL/g;
$msg =~ s/\|VERSION\|/$VERSION/g;
$msg =~ s/\|WGHOME\|/$WGHOME/g;
$msg =~ s/\|WGARCHIVE_DIR\|/$WGARCHIVE_DIR/g;
$msg =~ s/\|CGIBIN_DIR\|/$CGIBIN_DIR/g;
$msg =~ s/\|CGIBIN\|/$CGIBIN/g;
$msg =~ s/\|LEGALOS\|/$LEGALOS/g;
$msg =~ s/\|WGSITECONF\|/$wgSiteConf::CfgFile/g;
$msg =~ s/\|WGARCMIN\|/$WGARCMIN/g;
# Other vars may be available
$srvname && ($msg =~ s/\|SERVERNAME\|/$srvname/g);
$docroot && ($msg =~ s/\|DOCROOT\|/$docroot/g);
$webuser && ($msg =~ s/\|WEBUSER\|/$webuser/g);
$ArchID && ($msg =~ s/\|ID\|/$ArchID/g);
$ArchDir && ($msg =~ s/\|DIR\|/$ArchDir/g);
$Title && ($msg =~ s/\|TITLE\|/$Title/g);
# remove any vars we don't use
$msg =~ s/\|[^\|\n]*\|//g;
return $msg;
}
sub CErrorExit {
my $msg = shift;
print "Content-type: text/html\n\n";
&ErrorExit($msg);
}
sub ErrorExit {
my $msg = shift;
$msg = &subvars($msg);
print STDERR scalar localtime, "$msg";
print "ERROR: $msg\n\n";
if ($debug) {
my $name;
print "Inputs were: <p>\n";
foreach $name (keys (%in)) {
print "$name = $in{$name} <p>\n";
}
}
exit 0;
}
sub nexthour {
my @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
"Sep","Oct","Nov","Dec");
my @days = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
my ($sec,$min,$hr,$mday,$mon,$yr,$wday,$yday,$isdst) = gmtime(time + 3600);
# format must be Wed, DD-Mon-YYYY HH:MM:SS GMT
my $timestr = sprintf("%3s, %02d-%3s-%4d %02d:%02d:%02d GMT",
$days[$wday],$mday,$months[$mon],$yr+1900,$hr,$min,$sec);
return $timestr;
}
sub MakeHostList {
my $aref = shift;
my @carray = ();
my @members = ();
if (&wgSiteConf::LoadSites) {
@carray = values (%wgSiteConf::Sites);
@members = @wgSite::members;
push(@members, 'IsLocal');
push(@members, 'BaseUrl');
push(@members, 'NotDefault');
&CommandWeb::BuildHashArray($aref, \@carray,\@members,$USEUPPER) || &ErrorExit("CommandWeb::BuildHashArray failed on Sites array: ".$CommandWeb::lastError);
} else {
print "Couldn't load sites, last error was $lastError\n<p>";
}
return 1;
}
# For compatibility with web interface
# if the main archive dir is web-writable, attempt to make this dir & contents web-writable too
sub MakeWebWritableIf {
my $mdir = shift;
my $cmpdir = shift || $WGARCHIVE_DIR;
my $own = (stat $cmpdir)[4];
if ($own == $WWWUID) {
system("chown -R $own $mdir");
}
print "notice: Keeping $mdir web-writable for compatibility with web interface\n";
}
sub trim {
my $s = shift;
$s =~ s/^\s+//g;
$s =~ s/\s+$//g;
return $s;
}