This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wwwdesc.pl
executable file
·191 lines (167 loc) · 5.18 KB
/
wwwdesc.pl
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
#! /usr/bin/perl -w
###################################################################################
# Dan Cardamore <[email protected]>
# Copyright 2000 all rights reserved
# This software is released under the GNU GPL license
###################################################################################
# $rcs = ' $Id: wwwdesc.pl,v 1.2 2001/04/05 01:04:29 wombat Exp $ ' ;
###################################################################################
use strict;
use CGI qw(param);
use DBI;
use POSIX ":sys_wait_h";
use File::stat;
use Date::Manip;
use vars qw/$dbname $dbhost $dbuser $dbpass $dbh/;
###################################################################################
################## User Configuration Data #############################
###################################################################################
# these are for connecting to the Postgres database
$dbname = 'wwwindex';
$dbhost = 'localhost';
$dbuser = 'wwwindex';
##################################################################################
############### END OF CONFIGURATION DATA ################################
##################################################################################
sub get_files {
opendir (DIR, '.');
my @allfiles = readdir(DIR);
closedir (DIR);
@allfiles = sort @allfiles;
return @allfiles;
}
sub handleAll {
my @files = get_files();
foreach my $file (@files) {
handleEntry($file);
}
return;
}
sub updateEntry($$) {
my ($dbfile,$description) = @_;
my $sth;
my $results = &getEntry($dbfile);
if (defined $results->[0]) {
$sth=$dbh->prepare("UPDATE files set description=\'$description\'" .
"where name=\'$dbfile\'" );
}
else {
$sth=$dbh->prepare("INSERT into files values(\'$dbfile\', \'0\', \'$description\')");
}
$sth->execute;
}
sub getEntry {
my $dbfile = shift;
my $sth = $dbh->prepare( "SELECT * FROM files where name=\'$dbfile\'" );
$sth->execute;
my @results = $sth->fetchrow_array;
return \@results;
}
sub flagDirForThumbs {
my ($dir, $size, $random, $thumbed) = @_;
my $date = ParseDate("today");
if ($thumbed == 1) {
# just update the database to indicate that this directory has images as thumbs.
# then we want to create all the thumbnails using "convert"
opendir (DIR, $dir) or die $!;
my @files = readdir(DIR);
closedir (DIR);
foreach my $file (@files) {
unless ( -f "$file") { next; } # only look at files
if ( $file =~ /PAsmall/ ) { next; } # ignore thumbnails.
if ( $file =~ /(png|gif|jpg|jpeg)$/i ) {
`convert -geometry $size $file PAsmall$file`;
}
}
}
my $sth = $dbh->prepare( "SELECT * FROM pictures where name=\'$dir\'" );
$sth->execute;
my @results = $sth->fetchrow_array;
if (defined $results[0]) {
$sth=$dbh->prepare("UPDATE pictures set size=\'$size\', random=\'$random\'," .
"thumbed=\'$thumbed\' where name=\'$dir\'");
}
else {
$sth=$dbh->prepare("INSERT into pictures values(\'$dir\', \'$date\'," .
"\'$size\',\'$random\', \'$thumbed\')");
}
$sth->execute;
return;
}
sub handleEntry {
my ($file) = @_;
if ($file eq "..") { return; }
my $pwd = `pwd`;
chomp $pwd;
my $dbfile = relpath("$pwd" ."/$file");
my $results = getEntry($dbfile);
if (not defined $results->[2]) {
$results->[2] = "";
}
print "--\n";
print "$dbfile description was \"$results->[2]\"\n";
print "$dbfile new description-> ";
my $description = <STDIN>;
chop $description;
if ($description ne "") {
updateEntry($dbfile, $description);
}
if (-d $dbfile) {
# ask about the pictures
print "Would you like this to be a thumbnailed page (y/n): ";
my $thumbed = <STDIN>;
chop $thumbed;
if ($thumbed =~ 'y') {
# create the thumbs in this directory
print "What size would you like the thumbnails to be? (default: 80x80): ";
my $size = <STDIN>;
chop $size;
if ($size eq '') {
$size = '80x80';
}
print "Would you like a random thumbnail to be displayed for this directory (y/n): ";
my $random = <STDIN>;
chop $random;
if ($random =~ 'y') {
$random = 1;
# flag this directory to have a random image popup
}
else { $random = 0; }
&flagDirForThumbs($dbfile, $size, $random, 1);
}
else {
&flagDirForThumbs($dbfile, "80x80", 0, 0);
}
}
return;
}
sub relpath {
my $file = shift;
my $newfile = "";
$file =~ s/\n//g; # remove new lines
my @peices = split /\//, $file;
for (my $i = 0; $i <= $#peices; $i++) {
if ($peices[$i] eq '.') {
last;
}
if (defined $peices[$i+1] and $peices[$i + 1] eq "..") {
last;
}
if ($peices[$i] ne ".") {
$newfile .= "/$peices[$i]";
}
}
$newfile =~ s/\/\//\//g;
return $newfile;
}
##########################################################################
###################### MAIN ########################################
##########################################################################
print "Updating descriptions for this directory\n";
print "Please enter your password for database user $dbuser: ";
$dbpass = <STDIN>;
chop $dbpass;
$dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$dbhost", "$dbuser", "$dbpass") or die $!;
handleAll();
$dbh->disconnect;
print "Done updates\n";