-
Notifications
You must be signed in to change notification settings - Fork 0
/
Images.pm
351 lines (255 loc) · 8.81 KB
/
Images.pm
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
package Labyrinth::Plugin::Images;
use warnings;
use strict;
my $VERSION = '5.08';
=head1 NAME
Labyrinth::Plugin::Images - Plugin Images handler for Labyrinth
=head1 DESCRIPTION
Contains all the image handling functionality for the Labyrinth
framework.
=cut
# -------------------------------------
# Library Modules
use base qw(Labyrinth::Plugin::Base);
use Image::Size;
use File::Copy;
use File::Basename;
use Labyrinth::Audit;
use Labyrinth::Globals qw(:default);
use Labyrinth::DBUtils;
use Labyrinth::DIUtils;
use Labyrinth::Media;
use Labyrinth::Metadata;
use Labyrinth::MLUtils;
use Labyrinth::Support;
use Labyrinth::Variables;
# -------------------------------------
# Constants
use constant MaxDefaultWidth => 120;
use constant MaxDefaultHeight => 120;
use constant MaxDefaultThumb => 120;
# -------------------------------------
# Variables
# type: 0 = optional, 1 = mandatory
# html: 0 = none, 1 = text, 2 = textarea
my %fields = (
imageid => { type => 1, html => 0 },
image => { type => 0, html => 0 },
tag => { type => 0, html => 1 },
link => { type => 0, html => 1 },
type => { type => 1, html => 0 },
href => { type => 0, html => 0 },
metadata => { type => 0, html => 1 },
);
my (@mandatory,@allfields);
for(keys %fields) {
push @mandatory, $_ if($fields{$_}->{type});
push @allfields, $_;
}
my $LEVEL = ADMIN;
# -------------------------------------
# The Subs
=head1 PUBLIC INTERFACE METHODS
=over 4
=item Random()
=item Random4()
=item Random6()
=item Random8()
=back
=cut
sub Random { RandomN(1); }
sub Random4 { RandomN(4); }
sub Random6 { RandomN(6); }
sub Random8 { RandomN(8); }
=head1 LOCAL INTERFACE METHODS
=over 4
=item RandomN()
=back
=cut
sub RandomN {
my $count = shift;
my @rows = $dbi->GetQuery('hash','GetImagesByType',2);
my $max = @rows;
my %blank = (link=>$settings{blank},tag=>'');
if($max <= $count) {
foreach my $inx (1..$max) { $tvars{"irand$inx"} = $rows[($inx-1)]; }
return;
}
my (%done,@random);
srand;
while(1) {
my $index = int((rand) * $max);
next if($done{$index});
push @random, $rows[$index];
$done{$index} = 1;
last if(@random >= $count);
}
foreach my $inx (1..$count) { $tvars{"irand$inx"} = $random[($inx-1)]; }
}
=head1 ADMIN INTERFACE METHODS
=over 4
=item List
=item Add
=item Edit
=item EditAmendments
=item Save
=item Delete
=item Gallery
=back
=cut
sub List {
return unless AccessUser($LEVEL);
my @delete = CGIArray('DELETE');
if(@delete) {
$cgiparams{'imageid'} = $_;
Delete();
}
my $key = $cgiparams{'searchmeta'} ? 'MetaImages' : 'AllImages';
$cgiparams{'searchmeta'} =~ s/[,\s]+/,/g;
my @where = ();
push @where, "i.type=$cgiparams{'stockid'}" if($cgiparams{'stockid'});
push @where, "m.tag IN ($cgiparams{'searchmeta'})" if($cgiparams{'searchmeta'});
my $where = @where ? 'WHERE '.join(' AND ',@where) : '';
my @rows = $dbi->GetQuery('hash',$key,{where=>$where});
foreach (@rows) { $_->{typename} = StockName($_->{type}); }
$tvars{data} = \@rows if(@rows);
$tvars{ddstock} = StockSelect();
}
sub Add {
return unless AccessUser($LEVEL);
my %data = (
imageid => 0,
tag => '',
metadata => '',
link => $settings{blank},
ddstock => StockSelect(),
);
$tvars{data} = \%data;
}
sub Edit {
return unless AccessUser($LEVEL);
return unless $cgiparams{'imageid'};
my @rows = $dbi->GetQuery('hash','GetImageByID',$cgiparams{'imageid'});
return unless(@rows);
$tvars{data} = $rows[0];
EditAmendments();
}
sub EditAmendments {
$tvars{data}->{metadata} = MetaGet($cgiparams{'imageid'},['Image']) if($cgiparams{'imageid'});
$tvars{data}->{typename} = StockName($tvars{data}->{type});
$tvars{data}->{ddstock} = StockSelect($tvars{data}->{type});
for(keys %fields) {
if($fields{$_}->{html} == 1) { $tvars{data}->{$_} = CleanHTML($tvars{data}->{$_}) }
elsif($fields{$_}->{html} == 2) { $tvars{data}->{$_} = SafeHTML($tvars{data}->{$_}) }
}
return unless($tvars{data}->{link});
# Get the size of image
my $size_x = 0;
($size_x) = split("x",$tvars{data}->{dimensions}) if($tvars{data}->{dimensions});
unless($size_x) {
my $file = "$settings{webdir}/$tvars{data}->{link}";
($size_x) = imgsize($file) if(-f $file);
}
$tvars{data}->{toobig} = 1 if($size_x > $tvars{maxpicwidth});
}
sub Save {
return unless AccessUser($LEVEL);
return unless AuthorCheck('GetImageByID','imageid',$LEVEL);
EditAmendments();
for(keys %fields) {
if($fields{$_}->{html} == 1) { $cgiparams{$_} = CleanHTML($cgiparams{$_}) }
elsif($fields{$_}->{html} == 2) { $cgiparams{$_} = CleanTags($cgiparams{$_}) }
elsif($fields{$_}->{html} == 3) { $cgiparams{$_} = CleanLink($cgiparams{$_}) }
}
my $link = $tvars{data}->{link};
return if FieldCheck(\@allfields,\@mandatory);
if($cgiparams{image}) {
my ($name,$filename) = CGIFile('image',$tvars{data}->{type});
unless($name) { # blank if anything goes wrong
$tvars{errcode} = 'ERROR';
return;
}
my $i = Labyrinth::DIUtils->new("$settings{webdir}/$filename");
$i->reduce(MaxDefaultWidth,MaxDefaultHeight);
$tvars{data}->{link} = $filename;
} else {
$tvars{data}->{link} = $link;
}
$cgiparams{imageid} = SaveImage( $cgiparams{imageid},
$tvars{data}->{tag},
$tvars{data}->{link},
$tvars{data}->{type},
$tvars{data}->{href});
my @metadata = $tvars{data}->{metadata} ? split(qr/[, ]+/,$tvars{data}->{metadata}) : ();
MetaSave($cgiparams{imageid},['Image'],@metadata);
}
sub Delete {
return unless AccessUser($LEVEL);
return unless($cgiparams{'imageid'});
# check whether image still referenced
if(ImageCheck($cgiparams{'imageid'})) {
$tvars{errcode} = 'MESSAGE';
$tvars{errmess} = 'Sorry cannot delete that image, it is used within other areas of the site.';
return;
}
my @rows = $dbi->GetQuery('hash','GetImageByID',$cgiparams{'imageid'});
# do the delete
if($dbi->DoQuery('DeleteImage',$cgiparams{'imageid'})) {
unlink "$settings{webdir}/" . $rows[0]->{link};
}
}
my @blanks = (
{ imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
{ imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
{ imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
{ imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
{ imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
{ imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
{ imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
{ imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
{ imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
);
sub Gallery {
return unless AccessUser(EDITOR);
my $start = $cgiparams{'start'} || 2;
my $key = '';
if($cgiparams{'searchmeta'}) {
$key = 'Meta';
$cgiparams{'searchmeta'} =~ s/[,\s]+/,/g;
$cgiparams{'searchmeta'} = join(",", map {"'$_'"} split(",",$cgiparams{'searchmeta'}));
}
my $where;
$where .= " AND i.type IN ($cgiparams{'imagetype'})" if($cgiparams{'imagetype'});
$where .= " AND m.tag IN ($cgiparams{'searchmeta'})" if($cgiparams{'searchmeta'});
my @rows = $dbi->GetQuery('hash',$key.'Gallery',{where=>$where},$start);
for(@rows) {
my ($x,$y);
if($_->{dimensions}) {
($x,$y) = split("x",$_->{dimensions});
} else {
($x,$y) = imgsize($settings{webdir}.'/'.$_->{link});
}
$_->{width} = ($x > $y) ? 100 : 0;
$_->{height} = ($x < $y) ? 100 : 0;
}
$tvars{next} = $rows[9]->{imageid} unless(@rows < 10);
push @rows, @blanks;
$tvars{data} = \@rows if(@rows);
my @prev = $dbi->GetQuery('hash',$key.'GalleryMin',{where=>$where},$start);
$tvars{prev} = $prev[8]->{imageid} unless(@prev < 9);
$tvars{imagetype} = $cgiparams{'imagetype'};
$tvars{ddstock} = StockSelect();
}
1;
__END__
=head1 SEE ALSO
L<Labyrinth>
=head1 AUTHOR
Barbie, <barbie@missbarbell.co.uk> for
Miss Barbell Productions, L<http://www.missbarbell.co.uk/>
=head1 COPYRIGHT & LICENSE
Copyright (C) 2002-2011 Barbie for Miss Barbell Productions
All Rights Reserved.
This module is free software; you can redistribute it and/or
modify it under the Artistic License 2.0.
=cut