Plugin/ImageGallery.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Plugin/Search/Indexer.pm ↑ Summary Plugin/Captcha.pm »
1 package App::ZofCMS::Plugin::ImageGallery;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '1.001003'; # VERSION
7  
8 1 ▶use base 'App::ZofCMS::Plugin::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Image::Size;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use Image::Resize;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use File::Spec;
13 use File::Copy;
14 use DBI;
15 use HTML::Entities;
16  
17 sub _key { 'plug_image_gallery' }
18 sub _defaults {
19 return (
20 # dsn => "DBI:mysql:database=test;host=localhost",
21 user => '',
22 pass => '',
23 opt => { RaiseError => 1, AutoCommit => 1 },
24 table => 'photos',
25 photo_dir => 'photos/',
26 filename => '[:rand:]',
27 thumb_dir => 'photos/thumbs/',
28 create_table => 0,
29 t_name => 'plug_image_gallery',
30 no_form => 1,
31 no_list => 0,
32 no_thumb_desc => 0,
33 allow_edit => 0,
34 thumb_size => { 200, 200 },
35 # photo_size => [ 600, 600 ],
36 has_view => 1,
37 want_lightbox => 0,
38 lightbox_rel => 'lightbox',
39 lightbox_desc => 1,
40 );
41 }
42  
43 sub _do {
44 my ( $self, $conf, $template, $query, $config ) = @_;
45  
46 return
47 if $conf->{no_form}
48 and $conf->{no_list};
49  
50 $self->conf( $conf );
51 $self->template( $template );
52 $self->query( $query );
53 $self->config( $config );
54  
55 my $dbh = DBI->connect_cached(
56 @$conf{ qw/dsn user pass opt/ },
57 );
58 $self->dbh( $dbh );
59  
60 if ( $conf->{create_table} ) {
61 $dbh->do(
62 "CREATE TABLE $conf->{table} (
63 photo TEXT,
64 width SMALLINT,
65 height SMALLINT,
66 thumb_width SMALLINT,
67 thumb_height SMALLINT,
68 description TEXT,
69 time VARCHAR(10),
70 id TEXT
71 );"
72 );
73 }
74  
75 if ( $conf->{filename} eq '[:rand:]' ) {
76 {
77 $conf->{filename} = rand() . time() . rand();
78 $conf->{filename} =~ tr/.//d;
79 redo
80 if -e File::Spec->catfile( $conf->{photo_dir}, $conf->{filename} )
81 or -e File::Spec->catfile( $conf->{thumb_dir}, $conf->{filename} );
82 }
83 }
84  
85 my %t_name_for = (
86 map +( $_ => $conf->{t_name} . "_$_" ),
87 qw/form list/,
88 );
89 unless ( $conf->{no_form} ) {
90 my $t = HTML::Template->new_scalar_ref( \ _form_template() );
91  
92 if ( defined $query->{plug_image_gallery_action}
93 and $query->{plug_image_gallery_action} eq 'Edit'
94 ) {
95 my $entry = $dbh->selectall_arrayref(
96 "SELECT id, description FROM $conf->{table} WHERE id = ?",
97 { Slice => {} },
98 $query->{plug_image_gallery_id},
99 ) || [{}];
100  
101 $query->{plug_image_gallery_description} = $entry->[0]{description};
102 $t->param(
103 is_edit => 1,
104 id => $entry->[0]{id},
105 );
106 }
107  
108 if ( defined $query->{plug_image_gallery_action}
109 and $query->{plug_image_gallery_action} eq 'Delete'
110 ) {
111 my $entry = $dbh->selectall_arrayref(
112 "SELECT photo FROM $conf->{table} WHERE id = ?",
113 { Slice => {} },
114 $query->{plug_image_gallery_id},
115 ) || [{}];
116  
117 my $file = $entry->[0]{photo};
118 if ( defined $file ) {
119 unlink File::Spec->catfile( $conf->{photo_dir}, $file );
120 unlink File::Spec->catfile( $conf->{thumb_dir}, $file );
121 }
122  
123 $dbh->do(
124 "DELETE FROM $conf->{table} WHERE id = ?",
125 undef,
126 $query->{plug_image_gallery_id},
127 );
128 }
129  
130 $t->param(
131 success_href => "/index.pl?page=$query->{page}&dir=$query->{dir}",
132 map +( $_ => $query->{$_} ),
133 qw/page dir plug_image_gallery_description/,
134 );
135  
136 $self->_process_form( $t )
137 if ( (
138 defined $query->{plug_image_gallery_file}
139 and length $query->{plug_image_gallery_file}
140 ) or (
141 defined $query->{plug_image_gallery_id}
142 and length $query->{plug_image_gallery_id}
143 )
144 )
145 and defined $query->{plug_image_gallery_submit}
146 and length $query->{plug_image_gallery_submit};
147  
148 $template->{t}{ $t_name_for{form} } = $t->output;
149 }
150  
151 unless ( $conf->{no_list} ) {
152 my $t = HTML::Template->new_scalar_ref( \ _list_template() );
153  
154 if ( defined $query->{plug_image_gallery_photo_id}
155 and length $query->{plug_image_gallery_photo_id}
156 ) {
157 my $image = $dbh->selectall_arrayref(
158 "SELECT * FROM $conf->{table} WHERE id = ?;",
159 { Slice => {} },
160 $query->{plug_image_gallery_photo_id},
161 ) || [{}];
162 $image = $image->[0];
163 $t->param(
164 is_view => 1,
165 photo => File::Spec->catfile( $conf->{photo_dir}, $image->{photo} ),
166 has_description => (
167 ( defined $image->{description} and length $image->{description} )
168 ? 1 : 0,
169 ),
170 description => $image->{description},
171 width => $image->{width},
172 height => $image->{height},
173 page => $query->{page},
174 dir => $query->{dir},
175 );
176 }
177 else {
178 my $images = $dbh->selectall_arrayref(
179 "SELECT * FROM $conf->{table};",
180 { Slice => {} },
181 ) || [];
182  
183 @$images = sort { $b->{time} <=> $a->{time} } @$images;
184  
185 $t->param(
186 has_images => scalar(@$images),
187 images => [
188 map +{
189 want_lightbox => $conf->{want_lightbox},
190 src => File::Spec->catfile( $conf->{thumb_dir}, $images->[$_]{photo} ),
191  
192 ( $conf->{want_lightbox}
193 ? (
194 lightbox_src => File::Spec->catfile(
195 $conf->{photo_dir},
196 $images->[$_]{photo}
197 ),
198 lightbox_rel => $conf->{lightbox_rel},
199 lightbox_desc => (
200 defined $images->[$_]{description}
201 ? $images->[$_]{description} : ''
202 ),
203 )
204 : ()
205 ),
206  
207 width => $images->[$_]{thumb_width},
208 height => $images->[$_]{thumb_height},
209 id => $images->[$_]{id},
210 page => $query->{page},
211 dir => $query->{dir},
212 edit => $conf->{allow_edit},
213 alt => $_%2,
214 has_view => $conf->{has_view},
215 has_description => (
216 ( defined $images->[$_]{description}
217 and length $images->[$_]{description}
218 and not $conf->{no_thumb_desc}
219 ) ? 1 : 0,
220 ),
221 description => defined $images->[$_]{description}
222 ? _process_description( $images->[$_]{description} )
223 : '',
224 }, 0 .. $#$images,
225 ],
226 );
227 }
228 $template->{t}{ $t_name_for{list} } = $t->output;
229 }
230 }
231  
232 sub _process_description {
233 my $d = shift;
234 encode_entities $d;
235 $d =~ s/\r?\n\r?/<br>/g;
236 return $d;
237 }
238  
239 sub _process_form {
240 my ( $self, $t ) = @_;
241 my ( $conf, $query, $dbh ) = map $self->$_, qw/conf query dbh/;
242  
243 if ( defined $query->{plug_image_gallery_id}
244 and length $query->{plug_image_gallery_id}
245 ) {
246 $dbh->do(
247 "UPDATE $conf->{table} SET description = ? WHERE id = ?",
248 undef,
249 $query->{plug_image_gallery_description},
250 $query->{plug_image_gallery_id},
251 );
252 $t->param( success => 1 );
253 return;
254 }
255  
256 my $cgi = $self->config->cgi;
257  
258 my $filename = $cgi->param('plug_image_gallery_file');
259  
260 $filename = 'foo.jpg'
261 unless defined $filename;
262  
263 my ( $ext ) = $filename =~ /([.][^.]+)$/;
264  
265 $ext = '.jpg'
266 unless defined $ext;
267  
268 my $photo_file = File::Spec->catfile( $conf->{photo_dir}, $conf->{filename} . $ext );
269 my $thumb_file = File::Spec->catfile( $conf->{thumb_dir}, $conf->{filename} . $ext );
270  
271 my $fh = $cgi->upload('plug_image_gallery_file')
272 or do {
273 $t->param(
274 error => 'Failed to upload file' . (
275 $cgi->cgi_error ? ': ' . $cgi->cgi_error : ''
276 )
277 );
278 $t->param( success => 1 );
279 return;
280 };
281  
282 open my $fh_out, '>', $photo_file
283 or do { $t->param( error => "Failed to create local file [$!]" ); return; };
284  
285 binmode $fh_out;
286  
287 {
288 local $/ = \102400;
289 while (<$fh>) {
290 print $fh_out $_;
291 }
292 }
293 close $fh_out;
294 close $fh;
295  
296 my ( $x, $y ) = imgsize $photo_file;
297  
298 if ( ref $conf->{thumb_size} eq 'ARRAY'
299 or ( ref $conf->{thumb_size} eq 'HASH'
300 and ( (%{ $conf->{thumb_size} })[0] < $x
301 or (%{ $conf->{thumb_size} })[1] < $y
302 )
303 )
304 ) {
305 $conf->{thumb_size} = [ %{ $conf->{thumb_size} } ]
306 if ref $conf->{thumb_size} eq 'HASH';
307  
308 my $thumb = Image::Resize->new( $photo_file );
309 my $gd = $thumb->resize( @{ $conf->{thumb_size} } );
310  
311 open my $fh_thumb, '>', $thumb_file
312 or $t->param( error => "Failed to create thumbnail [$!]" )
313 and return;
314  
315 my $img_type = substr $ext, 1;
316 $img_type = 'jpeg'
317 unless $img_type eq 'png' or $img_type eq 'gif';
318  
319 binmode $fh_thumb;
320 print $fh_thumb $gd->$img_type();
321 close $fh_thumb;
322 }
323 else {
324 copy $photo_file, $thumb_file;
325 }
326  
327 if ( ref $conf->{photo_size} eq 'ARRAY'
328 or ( ref $conf->{photo_size} eq 'HASH'
329 and ( (%{ $conf->{photo_size} })[0] < $x
330 or (%{ $conf->{photo_size} })[1] < $y
331 )
332 )
333 ) {
334 $conf->{photo_size} = [ %{ $conf->{photo_size} } ]
335 if ref $conf->{photo_size} eq 'HASH';
336  
337 my $photo = Image::Resize->new( $photo_file );
338 my $gd = $photo->resize( @{ $conf->{photo_size} } );
339  
340 open my $fh_photo, '>', $photo_file
341 or $t->param( error => "Failed to resize photo [$!]" )
342 and return;
343  
344 my $img_type = substr $ext, 1;
345 $img_type = 'jpeg'
346 unless $img_type eq 'png' or $img_type eq 'gif';
347  
348 binmode $fh_photo;
349 print $fh_photo $gd->$img_type();
350 close $fh_photo;
351  
352 ( $x, $y ) = ( $gd->width, $gd->height );
353 }
354 my ( $thumb_x, $thumb_y ) = imgsize $thumb_file;
355  
356 $t->param( success => 1 );
357  
358 $dbh->do(
359 "INSERT INTO $conf->{table} VALUES(?, ?, ?, ?, ?, ?, ?, ?);",
360 undef,
361 $conf->{filename} . $ext,
362 $x,
363 $y,
364 $thumb_x,
365 $thumb_y,
366 $query->{plug_image_gallery_description},
367 time(),
368 do { my $id = rand() . time() . rand(); $id =~ tr/.//d; $id },
369 );
370  
371 return 1;
372 }
373  
374 sub _form_template {
375 return <<'END_TEMPLATE';
376 <tmpl_if name='success'>
377 <p>Your image has been successfully uploaded.</p>
378 <p><a href="<tmpl_var escape='html' name='success_href'>">Upload another image</a></p>
379 <tmpl_else>
380 <form action="" method="POST" id="plug_image_gallery_form" enctype="multipart/form-data">
381 <div>
382 <tmpl_if name='error'><p class="error"><tmpl_var escape='html' name='error'></p></tmpl_if>
383 <input type="hidden" name="page" value="<tmpl_var escape='html' name='page'>">
384 <input type="hidden" name="dir" value="<tmpl_var escape='html' name='dir'>">
385 <tmpl_if name='is_edit'><input type="hidden" name="plug_image_gallery_id" value="<tmpl_var escape='html' name='id'>"></tmpl_if>
386 <ul>
387 <tmpl_unless name='is_edit'><li>
388 <label for="plug_image_gallery_file">Image: </label
389 ><input type="file" name="plug_image_gallery_file" id="plug_image_gallery_file">
390 </li></tmpl_unless>
391 <li>
392 <label for="plug_image_gallery_description">Description: </label
393 ><textarea name="plug_image_gallery_description" id="plug_image_gallery_description" cols="60" rows="5"><tmpl_var escape='html' name='plug_image_gallery_description'></textarea>
394 </li>
395 </ul>
396 <input type="submit" name="plug_image_gallery_submit" value="<tmpl_if name='is_edit'>Update<tmpl_else>Upload</tmpl_if>">
397 </div>
398 </form>
399 </tmpl_if>
400 END_TEMPLATE
401 }
402  
403 sub _list_template {
404 return <<'END_TEMPLATE';
405 <tmpl_if name='is_view'>
406 <a class="plug_image_gallery_return_to_image_list" href="/index.pl?page=<tmpl_var escape='html' name='page'>&amp;dir=<tmpl_var escape='html' name='dir'>">Return to image list.</a>
407 <div id="plug_image_gallery_photo"><img src="/<tmpl_var escape='html' name='photo'>" width="<tmpl_var escape='html' name='width'>" height="<tmpl_var escape='html' name='height'>" alt=""><tmpl_if name='has_description'><p class="plug_image_gallery_description"><tmpl_var name='description'></p></tmpl_if></div>
408 <tmpl_else>
409 <tmpl_if name='has_images'>
410 <ul class="plug_image_gallery_list">
411 <tmpl_loop name='images'>
412 <li<tmpl_if name='alt'> class="alt"</tmpl_if>>
413 <tmpl_if name='has_view'><a <tmpl_if name='want_lightbox'>rel="<tmpl_var escape='html' name='lightbox_rel'>" href="/<tmpl_var escape='html' name='lightbox_src'>" title="<tmpl_var escape='html' name='lightbox_desc'>"<tmpl_else>href="/index.pl?page=<tmpl_var escape='html' name='page'>&amp;dir=<tmpl_var escape='html' name='dir'>&amp;plug_image_gallery_photo_id=<tmpl_var escape='html' name='id'>"</tmpl_if>></tmpl_if><img src="/<tmpl_var escape='html' name='src'>" width="<tmpl_var escape='html' name='width'>" height="<tmpl_var escape='html' name='height'>" alt=""><tmpl_if name='has_view'></a></tmpl_if>
414 <tmpl_if name="has_description"><p><tmpl_var name='description'></p></tmpl_if>
415 <tmpl_if name="edit">
416 <form action="" method="POST">
417 <div>
418 <input type="hidden" name="plug_image_gallery_id" value="<tmpl_var escape='html' name='id'>">
419 <input type="hidden" name="page" value="<tmpl_var escape='html' name='page'>">
420 <input type="hidden" name="dir" value="<tmpl_var escape='html' name='dir'>">
421 <input type="submit" class="input_submit" name="plug_image_gallery_action" value="Edit">
422 <input type="submit" class="input_submit" name="plug_image_gallery_action" value="Delete">
423 </div>
424 </form>
425 </tmpl_if>
426 </li>
427 </tmpl_loop>
428 </ul>
429 <tmpl_else>
430 <p>Currently there are no images.</p>
431 </tmpl_if>
432 </tmpl_if>
433 END_TEMPLATE
434 }
435  
436 sub conf {
437 my $self = shift;
438 @_ and $self->{CONF} = shift;
439 return $self->{CONF};
440 }
441  
442 sub template {
443 my $self = shift;
444 @_ and $self->{TEMPLATE} = shift;
445 return $self->{TEMPLATE};
446 }
447  
448 sub query {
449 my $self = shift;
450 @_ and $self->{QUERY} = shift;
451 return $self->{QUERY};
452 }
453  
454 sub config {
455 my $self = shift;
456 @_ and $self->{CONFIG} = shift;
457 return $self->{CONFIG};
458 }
459  
460 sub dbh {
461 my $self = shift;
462 @_ and $self->{dbh} = shift;
463 return $self->{dbh};
464 }
465  
466 1;
467 __END__
« Plugin/Search/Indexer.pm ↑ Summary Plugin/Captcha.pm »