-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgalleryadd.pl
More file actions
executable file
·646 lines (609 loc) · 19.6 KB
/
galleryadd.pl
File metadata and controls
executable file
·646 lines (609 loc) · 19.6 KB
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#!/usr/bin/perl
use strict;
my $Version = ".07alpha";
########################################################
# README
# Previously I went to the trouble of building a whole
# distribution with README files and installation help.
# Since modifications to the script by 3rd parties were
# distributed without the accompanying files, I merged
# them all into this file.
########################################################
# MISC
# Script: galleryadd.pl
# http://jpmullan.com/galleryupdates/
# Purpose: Adds an image or recursive directory structure
# to a gallery
# Author: Jesse Mullan <jmullan@visi.com>
# Comment: I'd still rather be programming
########################################################
# LICENSE
# Copyright (C) 2002 Jesse Mullan
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
########################################################
# AUTHORS
# Jesse Mullan <jmullan@visi.com>
# Here is a list of folks in alphabetical
# order who have contributed to the
# project in the form of new code,
# patches, bug fixes, or support.
# Corrections are welcome.
# Send them to jmullan@visi.com
# Bharat Mediratta <bharat@menalto.com>
# Greg Ewing <greg.ewing@pobox.com>
########################################################
# REQUIREMENTS
# Assuming that you have all of the requirements, simply
# make galleryadd.pl executable by you and run it.
# chmod u+x galleryadd.pl
# or use perl to run it
# perl galleryadd.pl
# galleryadd.pl requires gallery_remote2.php from
# gallery 1.3.3 or better
# galleryadd.pl requires a few perl modules.
# These modules are all available via CPAN.
# CPAN has instructions for retrieving modules.
# http://www.cpan.org/modules/
# If you don't know if you are missing any modules, try running galleryadd.pl.
# perl will complain about any missing modules like this:
# "Can't locate LWP/UserAgent1.pm in @INC (@INC contains"
# and so on.
# Here are the modules that galleryadd.pl tries to use:
# LWP::UserAgent
# http://www.linpro.no/lwp/
# http://www.perldoc.com/perl5.6.1/lib/LWP.html
require LWP::UserAgent;
# HTTP::Request::Common
# http://www.perldoc.com/perl5.6.1/lib/HTTP/Request/Common.html
use HTTP::Request::Common;
# HTTP::Cookies
# http://www.perldoc.com/perl5.6.1/lib/HTTP/Cookies.html
use HTTP::Cookies;
# HTTP::Response
# http://www.perldoc.com/perl5.6.1/lib/HTTP/Response.html
use HTTP::Response;
# Getopt::Simple
# http://search.cpan.org/author/RSAVAGE/Getopt-Simple-1.45/Simple.pm
use Getopt::Simple;
# Data::Dumper;
# when debugging we might use Data::Dumper
# http://www.perldoc.com/perl5.6.1/lib/Data/Dumper.html
#use Data::Dumper;
# constants
my $gallery_remote_protocol_version = '2.1';
my $gallery_file = '/gallery_remote2.php';
my @formats = ('JPG','jpeg','jpg', 'gif', 'png', 'crw','avi', 'mpg', 'mpeg', 'wmv', 'mov', 'swf');
# variables
# derived from arguments
my $gallery_location;
my $gallery_album;
my $gallery_username;
my $gallery_password;
my $gallery_setcaption;
my $gallery_runquiet;
my $gallery_versiononly;
my $gallery_noverify;
my $gallery_log = 0;
my $gallery_log_filename = '';
my $gallery_log_open = 0;
# calculated
my $gallery_album_exists;
my $gallery_filename;
my $correct_arguments = 0;
my $gallery_url;
my $ua;
my $response;
my $gallery_response_text;
my $gallery_response_throwaway;
my $gallery_response_code;
my @gallery_files =();
my $gallery_file_count;
my @gallery_response_array;
my @items;
my @albums;
my @albumnames;
my $dir;
my $i;
my $return;
# program logic and control
if ($gallery_log && $gallery_log_filename) {
open(LOG, '>>~/galleryadd.log') || die 'Could not open logfile';
$gallery_log_open =1;
}
my($options) = {
'help' => {
'type' => '',
'env' => '-',
'default' => '',
# 'verbose' => '', # Not needed on every key.
'order' => 1,
},
'quiet' => {
'type' => '!',
'env' => '-',
'default' => '0',
'verbose' => 'Do not print anything unless errors are encountered',
'order' => 2,
},
'captions' => {
'type' => '!',
'env' => '-',
'default' => '0',
'verbose' => 'Use the filename to provide a caption for the image',
'order' => 3,
},
'noverify' => {
'type' => '!',
'env' => '-',
'default' => '0',
'verbose' => 'Don\'t check if the album exists before attempting to upload',
'order' => 4,
},
'location' => {
'type' => '=s',
'env' => '-',
'default' => '',
'verbose' => 'Specify the url of your gallery',
'order' => 5,
},
'album' => {
'type' => '=s',
'env' => '-',
'default' => '',
'verbose' => 'Specify the album in which to put the image',
'order' => 6,
},
'username' => {
'type' => '=s', # As per Getopt::Long.
'env' => '-', # Help text.
'default' => '', # In case $USER is undef.
'verbose' => 'Specify the username to log into your gallery',
'order' => 7, # Help text sort order.
},
'password' => {
'type' => '=s',
'env' => '-',
'default' => '',
'verbose' => 'Specify the password to log into your gallery',
'order' => 8,
},
'version' => {
'type' => '!',
'env' => '-',
'default' => '0',
'verbose' => 'Display version information and exit',
'order' => 10,
},
};
my($option) = new Getopt::Simple;
if (! $option->getOptions($options, "Version: $Version\nUsage: galleryadd.pl -l url -a album -u username -p password filenames [-c] [-q]") ) {
exit(-1); # Failure.
}
$gallery_setcaption = $option->{'switch'}{'captions'};
$gallery_runquiet = $option->{'switch'}{'quiet'};
$gallery_versiononly = $option->{'switch'}{'version'};
$gallery_noverify = $option->{'switch'}{'noverify'};
$gallery_location = $option->{'switch'}{'location'};
$gallery_album = $option->{'switch'}{'album'};
$gallery_username = $option->{'switch'}{'username'};
$gallery_password = $option->{'switch'}{'password'};
if ($gallery_versiononly) {
print "galleryadd.pl version $Version\n";
exit;
};
if (!$gallery_location) {
print "Please specify the url of your gallery\n";
$correct_arguments = -1;
};
if (!$gallery_album) {
print "Please specify the album in which to put the image\n";
$correct_arguments = -1;
};
if (!$gallery_username) {
print "Please specify the username to log into your gallery\n";
$correct_arguments = -1;
};
if (!$gallery_password) {
print "Please specify the password to log into your gallery\n";
$correct_arguments = -1;
};
for my $filename (@ARGV) {
# ignore . and .. :
next if ($filename eq '.' || $filename eq '..' || $filename eq '');
$filename =~ s|\\|/|;
if(substr($filename,-1) eq '/'){
$filename=substr($filename,0,-1);
}
push (@gallery_files,$filename);
}
$gallery_file_count = @gallery_files;
if ($gallery_file_count==0) {
print "Please specify more than $gallery_file_count files to add to your gallery\n";
$correct_arguments = -1;
}
if ($correct_arguments) {
print "\nUsage: galleryadd.pl -l url -a album -u username -p password [-c] [-q] [-n]\nFor help: galleryadd.pl -h\n";
exit(-1);
}
# check the url
if (substr($gallery_location,0,7) ne 'http://') {
$gallery_url = 'http://' . $gallery_location . $gallery_file;
} else {
$gallery_url = $gallery_location . $gallery_file;
}
# set up the user agent
$ua = LWP::UserAgent->new;
$ua->cookie_jar(HTTP::Cookies->new(file => 'cookie_jar', autosave => 1));
# log in
if (!$gallery_runquiet) {
print "Logging In\n";
}
$response = $ua->request(POST $gallery_url,
Content_Type => 'form-data',
Content => [ protocol_version => $gallery_remote_protocol_version,
cmd => "login",
uname => $gallery_username,
password => $gallery_password
] );
if ($gallery_log_open) {
print LOG "POST $gallery_url\n";
print LOG "Content_Type => 'form-data'\n";
print LOG "Content => \n";
print LOG "\tprotocol_version => $gallery_remote_protocol_version\n";
print LOG "\tcmd => 'login'\n";
print LOG "\tuname => $gallery_username\n";
print LOG "\tpassword => password not logged\n";
print LOG "\n";
}
if ($response->is_error) {
$gallery_response_text = $response->error_as_HTML;
} else {
$gallery_response_text = $response->content;
}
$gallery_response_code = $response->code;
if ($gallery_response_code != 200) {
if ($gallery_response_code == 404) {
print "Could not find gallery_remote2.php on server\n";
print "Is gallery v1.3.3 or higher installed at this url?\n";
die "Could not log in:\tHTTP error\n$gallery_url\n"
. $gallery_response_code . "\n"
. $gallery_response_text . "\n";
} else {
die "Could not log in:\tHTTP error\n$gallery_url\n"
. $gallery_response_code . "\n"
. $gallery_response_text . "\n";
}
} else {
SWITCH: {
if ($gallery_response_text =~ /Login successful/) {
if (!$gallery_runquiet) {
print "Logged In successfully\n";
if ($gallery_log_open) {
print LOG $gallery_response_text . "\n";
}
}
last SWITCH;
}
if ($gallery_response_text =~ /Login Incorrect/) {
die "Could not log in:\t'$gallery_username' $gallery_response_text\n";
last SWITCH;
}
die "Could not log in:\tUnknown error " . $gallery_response_code . "\t" . $gallery_response_text . "\n";
}
}
if ($gallery_log_open) {
print LOG $gallery_response_text . "\n";
}
if (!$gallery_noverify) {
if (!$gallery_runquiet) {
print "Fetching list of albums (may be slow)\n";
}
#Fetch list of albums
$response = $ua->request(POST $gallery_url,
Content_Type => 'form-data',
Content => [ protocol_version => $gallery_remote_protocol_version,
cmd => "fetch-albums"
] );
if ($response->is_error) {
$gallery_response_text = $response->error_as_HTML;
} else {
$gallery_response_text = $response->content;
}
if ($gallery_log_open) {
print LOG $gallery_response_text . "\n";
}
if ($gallery_response_code != 200) {
die "Could not log in:\tHTTP error\n$gallery_url\n" . $gallery_response_code . "\n" . $gallery_response_text . "\n";
} else {
SWITCH: {
if ($gallery_response_text =~ /Fetch albums successful./) {
if (!$gallery_runquiet) {
print "Fetched list of albums.\n";
}
last SWITCH;
}
die "Could not fetch list of albums:\tUnknown error " . $gallery_response_code . "\t" . $gallery_response_text . "\n";
}
}
@gallery_response_array = split(/\n/, $gallery_response_text);
$gallery_response_text = pop(@gallery_response_array);
$gallery_response_throwaway = pop(@gallery_response_array);
$gallery_response_code = $response->code;
$gallery_album_exists = 0;
foreach my $item (sort (@gallery_response_array)) {
chomp($item);
# print $item . "\n";
my ($field,@value) = split(/=/, $item);
my @foo = split(/\./, $field);
my $bar = shift(@foo);
if ($bar eq 'album_count') {
next;
}
my $fieldname = shift(@foo);
if ($fieldname eq 'perms') {
my $fieldname .= '.' . shift(@foo);
}
my $number = shift(@foo);
if ($fieldname eq 'name') {
$albums[$number]['name'] = join('=',@value);
push (@albumnames,join('=',@value));
if ($gallery_album eq $albums[$number]['name']) {
$gallery_album_exists = 1;
if (!$gallery_runquiet) {
print "Found album '$gallery_album'\n";
}
}
}
if ($fieldname eq 'title') {
$albums[$number]['title'] = join('=',@value);
}
if ($fieldname eq 'perms.add') {
$albums[$number]['add'] = join('=',@value);
}
if ($fieldname eq 'perms.create_sub') {
$albums[$number]['create_sub'] = join('=',@value);
}
if ($fieldname eq 'perms.del_alb') {
$albums[$number]['del_alb'] = join('=',@value);
}
if ($fieldname eq 'perms.del_item') {
$albums[$number]['del_item'] = join('=',@value);
}
if ($fieldname eq 'perms.write') {
$albums[$number]['write'] = join('=',@value);
}
}
if (!$gallery_album_exists) {
die "Album does not exist: $gallery_album\n"; # crazy
}
}
for my $filename (@ARGV) {
# ignore . and .. :
next if ($filename eq '.' || $filename eq '..');
if (-d $filename) {
if (!-e "$filename") {
die "Could not find file: $filename\n"; # nuts!
}
if (!-r "$filename") {
die "This file exists, but I can't read it: $filename\n"; # looney toons!
}
if (!-x $filename) {
die "This directory exists, but I can't read it: $filename\nPlease verify the ownership and permissions"; # looney toons!
} else {
add_dir($gallery_album,$filename);
}
} else {
if (isAcceptable($filename)) {
if (!-e "$filename") {
die "Could not find file: $filename\n"; # nuts!
}
if (!-r "$filename") {
die "This file exists, but I can't read it: $filename\n"; # looney toons!
}
add_image($gallery_album,"$filename");
} else {
if (!$gallery_runquiet) {
print "Ignoring $filename\n";
}
}
}
}
if ($gallery_log_open) {
close(LOG);
}
########################################################
# SUBROUTINES
sub albumexists($) {
my $name = shift(@_);
for my $album (@albumnames) {
if ($album eq $name) {
return -1;
}
}
return 0;
}
sub add_dir ($$) {
my $album = shift(@_);
my $dir = shift(@_);
my @dirname = split(/\//, $dir);
my $wantedAlbumName = $dirname[$#dirname];
my $newAlbumName = $wantedAlbumName;
$i = 0;
while (albumexists($newAlbumName)) {
$newAlbumName = $wantedAlbumName . '_' . $i;
$i++;
}
opendir(DIR, $dir) || die "I can't read this directory: $dir $!";
my @filename = readdir(DIR);
closedir DIR;
if (!$gallery_runquiet) {
print "Creating Album '$dir'\n";
}
$response = $ua->request(POST $gallery_url,
Content_Type => 'form-data',
Content => [ protocol_version => $gallery_remote_protocol_version,
cmd => "new-album",
set_albumName => $album,
newAlbumName => $newAlbumName,
newAlbumTitle => $dirname[$#dirname],
newAlbumDesc => ''
] );
if ($response->is_error) {
$gallery_response_text = $response->error_as_HTML;
} else {
$gallery_response_text = $response->content;
}
$gallery_response_code = $response->code;
if ($gallery_response_code != 200) {
die "Could not create album:\tHTTP error " .$gallery_response_code . "\t" . $gallery_response_text . "\n";
} else {
SWITCH: {
if ($gallery_response_text =~ /New album created successfully./) {
if (!$gallery_runquiet) {
print "Album '$newAlbumName' created successfully\n";
}
push (@albumnames,$newAlbumName);
last SWITCH;
}
if ($gallery_response_text =~ /A new album could not be created because the user does not have permission to do so./) {
if (!$gallery_runquiet) {
die "Could not create album '$newAlbumName':\tThe user does not have permission to do so. " . $gallery_response_code . "\t" . $gallery_response_text . "\n";
}
last SWITCH;
}
die "Could not create album '$newAlbumName':\tUnknown error " . $gallery_response_code . "\t" . $gallery_response_text . "\n";
}
}
foreach my $filename (sort (@filename)) {
# ignore . and .. :
if ($filename ne '.' && $filename ne '..') {
if (-d "$dir/$filename") {
if (!-e "$dir/$filename") {
die "Could not find file: $dir/$filename\n"; # nuts!
}
if (!-r "$dir/$filename") {
die "This file exists, but I can't read it: $dir/$filename\n"; # looney toons!
}
if (!-x "$dir/$filename") {
die "This directory exists, but I can't read it: $dir/$filename\nPlease verify the ownership and permissions"; # looney toons!
} else {
add_dir($newAlbumName,"$dir/$filename");
}
} else {
if (isAcceptable($filename)) {
if (!-e "$dir/$filename") {
die "Could not find file: $dir/$filename\n"; # nuts!
}
if (!-r "$dir/$filename") {
die "This file exists, but I can't read it: $dir/$filename\n"; # looney toons!
}
add_image($newAlbumName,"$dir/$filename");
} else {
if (!$gallery_runquiet) {
print "Ignoring $filename\n";
}
}
}
}
}
}
sub isAcceptable($) {
my @pieces = split(/\./, shift(@_));
my $extension = $pieces[$#pieces];
for my $format (@formats) {
if ($format eq $extension) {
return -1;
}
}
return 0;
}
sub stripPathAndExtension($) {
my $fullpath = shift(@_);
my @path = split(/\//, $fullpath);
my $filename = pop(@path);
my @pieces = split(/\./, $filename);
pop @pieces;
return join('.',@pieces);
}
sub add_image($$) {
my $album = shift(@_);
my $filename = shift(@_);
if (!$gallery_runquiet) {
print "Uploading image '$filename'\n";
}
if ($gallery_setcaption) {
$response = $ua->request(POST $gallery_url,
Content_Type => 'form-data',
Content => [ protocol_version => $gallery_remote_protocol_version,
cmd => "add-item",
set_albumName => $album,
caption => stripPathAndExtension($filename),
userfile => ["$filename"]
] );
if ($gallery_log_open) {
print LOG "POST $gallery_url\n";
print LOG "Content_Type => 'form-data'\n";
print LOG "Content => \n";
print LOG "\tprotocol_version => $gallery_remote_protocol_version\n";
print LOG "\tcmd => 'add-item'\n";
print LOG "\tset_albumName => $album\n";
print LOG "\tcaption => " . stripPathAndExtension($filename) . "\n";
print LOG "\tuserfile => \"$filename\"\n";
print LOG "\n";
}
} else {
$response = $ua->request(POST $gallery_url,
Content_Type => 'form-data',
Content => [ protocol_version => $gallery_remote_protocol_version,
cmd => "add-item",
set_albumName => $album,
setCaption => '',
userfile => ["$filename"]
] );
if ($gallery_log_open) {
print LOG "POST $gallery_url\n";
print LOG "Content_Type => 'form-data'\n";
print LOG "Content => \n";
print LOG "\tprotocol_version => $gallery_remote_protocol_version\n";
print LOG "\tcmd => 'add-item'\n";
print LOG "\tset_albumName => $album\n";
print LOG "\tsetcaption => ''\n";
print LOG "\tuserfile => \"$filename\"\n";
print LOG "\n";
}
}
if ($response->is_error) {
$gallery_response_text = $response->error_as_HTML;
} else {
$gallery_response_text = $response->content;
}
if ($gallery_log_open) {
print LOG $gallery_response_text . "\n";
}
$gallery_response_code = $response->code;
if ($gallery_response_code != 200) {
die "Could not upload image:\tHTTP error " . $gallery_response_code . "\t" . $gallery_response_text . "\n";
} else {
SWITCH: {
if ($gallery_response_text =~ /Add photo successful./) {
if (!$gallery_runquiet) {
print "Image '$filename' uploaded successfully\n";
}
last SWITCH;
}
die "Could not upload image '$filename':\tUnknown error " . $gallery_response_code . "\t" . $gallery_response_text . "\n";
}
}
}