Changeset 937
- Timestamp:
- 12/16/06 02:56:46 (2 years ago)
- Files:
-
- branches/wheeljack/lib/MT/App/CMS.pm (modified) (15 diffs)
- branches/wheeljack/lib/MT/Asset.pm (modified) (3 diffs)
- branches/wheeljack/lib/MT/Asset/Image.pm (modified) (4 diffs)
- branches/wheeljack/tmpl/cms/asset_image_options.tmpl (added)
- branches/wheeljack/tmpl/cms/dialog_list_assets.tmpl (modified) (3 diffs)
- branches/wheeljack/tmpl/cms/upload_complete.tmpl (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/wheeljack/lib/MT/App/CMS.pm
r936 r937 920 920 my $blog; 921 921 unless ($blog_id) { 922 $blog = $blogs{$obj->blog_id} ||= MT::Blog->load($obj->blog_id, { cached_ok => 1 }); 923 } else { 924 $blog = $app->blog; 922 $blog = $blogs{$obj->blog_id} ||= $obj->blog; 925 923 } 926 924 $row->{blog_name} = $blog ? $blog->name : '-'; … … 997 995 can_delete_files => ($blog ? $app->{perms}->can_edit_assets : $app->user->is_superuser), 998 996 nav_assets => 1, 997 panel_searchable => 1, 998 search_prompt => $app->translate("Search Assets") . ":", 999 #search_type => $app->translate("Assets"), 999 1000 }, 1000 1001 }); … … 5490 5491 5491 5492 sub asset_insert { 5492 my ($app, %args) = @_;5493 my $app = shift; 5493 5494 5494 5495 # Just insert text if there are arguments, unless one is 5495 5496 # 'is_image' otherwise we are definitely uploading. 5496 my $text = !keys %args || $args{is_image} 5497 ? $app->_process_post_upload() 5498 : $app->asset_insert_text($args{asset_id}); 5497 my $text = $app->_process_post_upload(); 5499 5498 return unless defined $text; 5500 5499 5501 5500 $app->build_page('asset_insert.tmpl', { 5502 upload_html => $text ,5503 edit_field => $app->param('edit_field'),5501 upload_html => $text || '', 5502 edit_field => scalar $app->param('edit_field') || '', 5504 5503 }, 5505 5504 ); … … 5512 5511 defined(my $text = $app->_process_post_upload) or return; 5513 5512 $q->param('text', $text); 5513 # strip any asset id 5514 $q->param('id', 0); 5514 5515 $app->edit_object; 5515 5516 } … … 5517 5518 sub _process_post_upload { 5518 5519 my $app = shift; 5520 my %param = $app->param_hash; 5521 my $asset; 5522 require MT::Asset; 5523 $param{id} && ($asset = MT::Asset->load($param{id})) 5524 or return $app->errtrans("Invalid request."); 5525 $asset->on_upload(\%param); 5526 return $app->asset_insert_text(\%param); 5527 } 5528 5529 sub asset_insert_text { 5530 my $app = shift; 5531 my ($param) = @_; 5519 5532 my $q = $app->param; 5520 my($url, $width, $height) = map $q->param($_), qw( url width height ); 5521 my ($base_url, $fname) = $url =~ m|(.*)/([^/]*)|; 5522 $url = $base_url . '/' . $fname; # no need to re-encode filename; url is already encoded 5523 my $blog_id = $q->param('blog_id'); 5524 require MT::Blog; 5525 my $blog = MT::Blog->load($blog_id, {cached_ok=>1}); 5526 my($thumb, $thumb_width, $thumb_height); 5527 # Save new defaults if requested. 5528 if($q->param('image_defaults')) { 5529 return $app->error($app->translate( 5530 'Permission denied setting image defaults for blog #[_2]', $blog_id 5531 )) unless $app->{perms}->can_save_image_defaults; 5532 $blog->image_default_set(1); 5533 $blog->image_default_wrap_text($q->param('wrap_text') ? 1 : 0); 5534 $blog->image_default_align($q->param('align') || MT::Blog::ALIGN()); 5535 $blog->image_default_thumb($q->param('thumb') ? 1 : 0); 5536 $blog->image_default_width($q->param('thumb_width') || MT::Blog::WIDTH()); 5537 $blog->image_default_wunits($q->param('thumb_width_type') || MT::Blog::UNITS()); 5538 $blog->image_default_height($q->param('thumb_height') || MT::Blog::WIDTH()); 5539 $blog->image_default_hunits($q->param('thumb_height_type') || MT::Blog::UNITS()); 5540 $blog->image_default_constrain($q->param('constrain') ? 1 : 0); 5541 $blog->image_default_popup($q->param('popup') ? 1 : 0); 5542 } 5543 else { 5544 $blog->image_default_set(0); 5545 $blog->image_default_wrap_text(0); 5546 $blog->image_default_align(MT::Blog::ALIGN()); 5547 $blog->image_default_thumb(0); 5548 $blog->image_default_width(0); 5549 $blog->image_default_wunits(MT::Blog::UNITS()); 5550 $blog->image_default_height(0); 5551 $blog->image_default_hunits(MT::Blog::UNITS()); 5552 $blog->image_default_constrain(1); 5553 $blog->image_default_popup(0); 5554 } 5555 $blog->save; 5556 if ($thumb = $q->param('thumb')) { 5557 require MT::Image; 5558 my $base_path = $q->param('site_path') ? 5559 $blog->site_path : $blog->archive_path; 5560 my $file = $q->param('fname'); 5561 if ($file =~ m!\.\.|\0|\|!) { 5562 return $app->error($app->translate("Invalid filename '[_1]'", $file)); 5563 } 5564 my $i_file = File::Spec->catfile($base_path, $file); 5565 ## Untaint. We checked $file for security holes above. 5566 ($i_file) = $i_file =~ /(.+)/s; 5567 my $fmgr = $blog->file_mgr; 5568 my $data = $fmgr->get_data($i_file, 'upload') 5569 or return $app->error($app->translate( 5570 "Reading '[_1]' failed: [_2]", $i_file, $fmgr->errstr)); 5571 my $image_type = scalar $q->param('image_type'); 5572 my $img = MT::Image->new( Data => $data, 5573 Type => $image_type ) 5574 or return $app->error($app->translate( 5575 "Thumbnail failed: [_1]", MT::Image->errstr)); 5576 my($w, $h) = map $q->param($_), qw( thumb_width thumb_height ); 5577 (my($blob), $thumb_width, $thumb_height) = 5578 $img->scale( Width => $w, Height => $h ) 5579 or return $app->error($app->translate("Thumbnail failed: [_1]", 5580 $img->errstr)); 5581 require File::Basename; 5582 my($base, $path, $ext) = File::Basename::fileparse($i_file, '\.[^.]*'); 5583 my $t_file = $path . $base . '-thumb' . $ext; 5584 my $basename = $base . '-thumb' . $ext; 5585 $fmgr->put_data($blob, $t_file, 'upload') 5586 or return $app->error($app->translate( 5587 "Error writing to '[_1]': [_2]", $t_file, $fmgr->errstr)); 5588 5589 $file =~ s/\Q$base$ext\E$//; 5590 my $url = $q->param('site_path') ? $blog->site_url : $blog->archive_url; 5591 $url .= '/' unless $url =~ m!/$!; 5592 $url .= $file; 5593 $thumb = $url . encode_url($base . '-thumb' . $ext); 5594 5595 require MT::Asset; 5596 my $img_pkg = MT::Asset->class_handler('image'); 5597 my $asset = new $img_pkg; 5598 my $original = $asset->clone; 5599 $asset->blog_id($blog_id); 5600 #$asset->url($thumb); 5601 $asset->file_path($t_file); 5602 $asset->file_name($basename); 5603 my $ext2 = $ext; 5604 $ext2 =~ s/^\.//; 5605 $asset->file_ext($ext2); 5606 $asset->image_width($thumb_width); 5607 $asset->image_height($thumb_height); 5608 $asset->created_by($app->user->id); 5609 $asset->save; 5610 MT->run_callbacks('CMSPostSave.asset', $app, $asset, $original); 5611 5612 $app->param('thumb_asset_id' => $asset->id); 5613 5614 MT->run_callbacks('CMSUploadFile', 5615 File => $t_file, Url => $thumb, Size => length($blob), 5616 Asset => $asset, 5617 Type => 'thumbnail', 5618 Blog => $blog); 5619 5620 MT->run_callbacks('CMSUploadImage', 5621 File => $t_file, Url => $thumb, 5622 Asset => $asset, 5623 Width => $thumb_width, Height => $thumb_height, 5624 ImageType => $image_type, 5625 Size => length($blob), 5626 Type => 'thumbnail', 5627 Blog => $blog); 5628 } 5629 if ($q->param('popup')) { 5630 require MT::Template; 5631 if (my $tmpl = MT::Template->load({ blog_id => $blog_id, 5632 type => 'popup_image' })) { 5633 (my $rel_path = $q->param('fname')) =~ s!\.[^.]*$!!; 5634 if ($rel_path =~ m!\.\.|\0|\|!) { 5635 return $app->error($app->translate( 5636 "Invalid basename '[_1]'", $rel_path)); 5637 } 5638 my $ext = $blog->file_extension || ''; 5639 $ext = '.' . $ext if $ext ne ''; 5640 require MT::Template::Context; 5641 my $ctx = MT::Template::Context->new; 5642 $ctx->stash('image_url', $url); 5643 $ctx->stash('image_width', $width); 5644 $ctx->stash('image_height', $height); 5645 my $popup = $tmpl->build($ctx); 5646 my $fmgr = $blog->file_mgr; 5647 my $root_path = $q->param('site_path') ? 5648 $blog->site_path : $blog->archive_path; 5649 my $abs_file_path = File::Spec->catfile($root_path, $rel_path . $ext); 5650 5651 ## If the popup filename already exists, we don't want to overwrite 5652 ## it, because it could contain valuable data; so we'll just make 5653 ## sure to generate the name uniquely. 5654 my($i, $rel_path_ext) = (0, $rel_path . $ext); 5655 while ($fmgr->exists($abs_file_path)) { 5656 $rel_path_ext = $rel_path . ++$i . $ext; 5657 $abs_file_path = File::Spec->catfile($root_path, $rel_path_ext); 5658 } 5659 my ($vol, $dirs, $basename) = File::Spec->splitpath($rel_path_ext); 5660 my $rel_url_ext = File::Spec->catpath($vol, $dirs, encode_url($basename)); 5661 5662 ## Untaint. We have checked for security holes above, so we 5663 ## should be safe. 5664 ($abs_file_path) = $abs_file_path =~ /(.+)/s; 5665 $fmgr->put_data($popup, $abs_file_path, 'upload') 5666 or return $app->error($app->translate( 5667 "Error writing to '[_1]': [_2]", $abs_file_path, 5668 $fmgr->errstr)); 5669 $url = $q->param('site_path') ? 5670 $blog->site_url : $blog->archive_url; 5671 $url .= '/' unless $url =~ m!/$!; 5672 $rel_url_ext =~ s!^/!!; 5673 $url .= $rel_url_ext; 5674 5675 my $img_pkg = MT::Asset->class_handler('image'); 5676 my $asset = new $img_pkg; 5677 my $original = $asset->clone; 5678 $asset->blog_id($blog_id); 5679 $asset->url($url); 5680 $asset->file_path($abs_file_path); 5681 $asset->file_name($basename); 5682 $asset->file_ext($blog->file_extension); 5683 $asset->created_by($app->user->id); 5684 $asset->save; 5685 MT->run_callbacks('CMSPostSave.asset', $app, $asset, $original); 5686 5687 MT->run_callbacks('CMSUploadFile', 5688 File => $abs_file_path, Url => $url, 5689 Asset => $asset, 5690 Size => length($popup), 5691 Type => 'popup', 5692 Blog => $blog); 5693 } 5694 } 5695 5696 return $app->asset_insert_text(); 5697 } 5698 5699 sub asset_insert_text { 5700 my $app = shift; 5701 my $q = $app->param; 5702 my $id = shift || $q->param('id'); 5533 my $id = $app->param('id') 5534 or return $app->errtrans("Invalid request."); 5703 5535 require MT::Asset; 5704 my $asset = MT::Asset->load($id) || 5705 return $app->errtrans("Can't load asset, $id."); 5706 my $text = $asset->as_html($q); 5707 return $q->param('popup') || $q->param('link') 5708 ? $app->translate_templatized($text) 5709 : $text; 5536 my $asset = MT::Asset->load($id) 5537 or return $app->errtrans("Can't load asset #[_1].", $id); 5538 return $asset->as_html($param); 5710 5539 } 5711 5540 … … 9203 9032 9204 9033 sub complete_insert { 9205 my ($app, %args) = @_; 9034 my $app = shift; 9035 my (%args) = @_; 9206 9036 9207 9037 my $asset = $args{asset}; … … 9209 9039 require MT::Asset; 9210 9040 $asset = MT::Asset->load($app->param('id')) || 9211 return $app->errtrans("Can't load asset , ". $app->param('id') .'.');9212 } 9213 return $app->errtrans(' No asset to upload.') unless $asset;9041 return $app->errtrans("Can't load asset #[_1].", $app->param('id')); 9042 } 9043 return $app->errtrans('Invalid request.') unless $asset; 9214 9044 9215 9045 $args{is_image} = $asset->isa('MT::Asset::Image') ? 1 : 0 … … 9217 9047 9218 9048 require MT::Blog; 9219 my $blog = $a rgs{blog} || MT::Blog->load($app->param('blog_id')) ||9220 return $app->errtrans("Can't load blog , ". $app->param('blog_id') .'.');9221 my $perms = $a rgs{perms} || $app->{perms} ||9049 my $blog = $asset->blog or 9050 return $app->errtrans("Can't load blog #[_1].", $app->param('blog_id') ); 9051 my $perms = $app->{perms} or 9222 9052 return $app->errtrans('No permissions'); 9223 9053 9224 if ($args{is_image}) { 9225 my $param = { 9226 asset_id => $asset->id, 9227 bytes => $args{bytes}, 9228 direct_asset_insert => scalar $app->param('direct_asset_insert'), 9229 edit_field => scalar $app->param('edit_field'), 9230 entry_insert => scalar $app->param('entry_insert'), 9231 fname => $asset->file_name, 9232 height => $asset->image_height, 9233 is_image => $args{is_image}, 9234 site_path => scalar $app->param('site_path'), 9235 url => $asset->url, 9236 width => $asset->image_width, 9237 }; 9238 9239 eval { require MT::Image; MT::Image->new or die; }; 9240 $param->{do_thumb} = $@ ? 0 : 1; 9241 9242 $param->{can_save_image_defaults} = $perms->can_save_image_defaults ? 1 : 0; 9243 $param->{constrain} = $blog->image_default_constrain ? 1 : 0; 9244 $param->{popup} = $blog->image_default_popup ? 1 : 0; 9245 $param->{image_defaults} = $blog->image_default_set ? 1 : 0; 9246 $param->{wrap_text} = $blog->image_default_wrap_text ? 1 : 0; 9247 $param->{make_thumb} = $blog->image_default_thumb ? 1 : 0; 9248 $param->{'align_'.$_} = $blog->image_default_align eq $_ ? 1 : 0 for qw(left center right); 9249 $param->{'unit_w'.$_} = $blog->image_default_wunits eq $_ ? 1 : 0 for qw(percent pixels); 9250 $param->{'unit_h'.$_} = $blog->image_default_hunits eq $_ ? 1 : 0 for qw(percent pixels); 9251 $param->{thumb_width} = $blog->image_default_width || $asset->image_width || 0; 9252 $param->{thumb_height} = $blog->image_default_height || $asset->image_height || 0; 9253 9254 $app->build_page('upload_complete.tmpl', $param); 9255 } 9256 else { 9257 $app->asset_insert( 9258 asset_id => $asset->id, 9259 is_image => $args{is_image}, 9260 ); 9261 } 9054 my $param = { 9055 asset_id => $asset->id, 9056 bytes => $args{bytes}, 9057 direct_asset_insert => scalar $app->param('direct_asset_insert') || 0, 9058 edit_field => scalar $app->param('edit_field') || '', 9059 entry_insert => scalar $app->param('entry_insert') || 0, 9060 fname => $asset->file_name, 9061 height => $asset->image_height, 9062 is_image => $args{is_image} || 0, 9063 site_path => scalar $app->param('site_path') || '', 9064 url => $asset->url, 9065 width => $asset->image_width, 9066 }; 9067 my $html = $asset->insert_options($param); 9068 unless ($html) { 9069 return $app->asset_insert(); 9070 } 9071 $param->{options_snippet} = $html; 9072 $app->build_page('upload_complete.tmpl', $param); 9262 9073 } 9263 9074 … … 9482 9293 9483 9294 if ($is_image) { 9484 MT->run_callbacks('CMSUploadFile ',9295 MT->run_callbacks('CMSUploadFile.' . $asset->class, 9485 9296 File => $local_file, Url => $url, Size => $bytes, 9486 9297 Asset => $asset, … … 9495 9306 Blog => $blog); 9496 9307 } else { 9497 MT->run_callbacks('CMSUploadFile ',9308 MT->run_callbacks('CMSUploadFile.' . $asset->class, 9498 9309 File => $local_file, Url => $url, Size => $bytes, 9499 9310 Asset => $asset, … … 9504 9315 $app->complete_insert( 9505 9316 asset => $asset, 9506 blog => $blog,9507 9317 bytes => $bytes, 9508 perms => $perms,9509 9318 ); 9510 9319 } … … 9533 9342 my $res = $app->do_search_replace(@_); 9534 9343 $app->add_breadcrumb($app->translate('Search & Replace')); 9344 $res->{nav_search} = 1; 9535 9345 $app->build_page('search_replace.tmpl', $res); 9536 9346 } … … 9553 9363 9554 9364 my $search_api = { 9365 'asset' => { 9366 'perm_check' => sub { 9367 1; 9368 }, 9369 'search_cols' => [ qw(file_name description) ], 9370 'replace_cols' => [], 9371 'can_replace' => 0, 9372 'can_search_by_date' => 1, 9373 }, 9555 9374 'entry' => { 9556 9375 'perm_check' => sub { … … 9880 9699 if (@data) { 9881 9700 my $meth = 'build_' . $type . '_table'; 9882 $app->$meth( items => \@data, param => \%param ); 9701 if ($app->can($meth)) { 9702 $app->$meth( items => \@data, param => \%param ); 9703 } else { 9704 my @objects; 9705 push @objects, { object => $_ } for @data; 9706 $param{object_loop} = \@objects; 9707 } 9883 9708 } 9884 9709 if ($is_dateranged) { … … 9922 9747 $res{'tab_junk'} = 1 if $is_junk; 9923 9748 $res{'search_cols_' . $_} = 1 foreach @cols; 9924 $res{nav_search} = 1;9925 9749 \%res; 9926 9750 } branches/wheeljack/lib/MT/Asset.pm
r935 r937 189 189 my $blog_id = $asset->blog_id or return undef; 190 190 require MT::Blog; 191 MT::Blog->load($blog_id, { cached_ok => 1 }); 191 MT::Blog->load($blog_id, { cached_ok => 1 }) 192 or return $asset->error("Failed to load blog for asset"); 192 193 }); 193 194 } … … 199 200 # undef is returned from fileparse if the extension is not known. 200 201 require File::Basename; 201 return (File::Basename::fileparse($filename, @{ $pkg->extensions }))[2] ? 1 : 0; 202 my $ext = $pkg->extensions || []; 203 return (File::Basename::fileparse($filename, @$ext))[2] ? 1 : 0; 202 204 } 203 205 … … 258 260 259 261 sub as_html { 260 my ($self, $q) = @_; 261 (my $fname = $self->file_name) =~ s/'/\\'/g; 262 my $text = sprintf '<a href="%s">%s</a>', $self->url, $fname; 262 my $asset = shift; 263 my ($param) = @_; 264 my $fname = $asset->file_name; 265 require MT::Util; 266 my $text = sprintf '<a href="%s">%s</a>', 267 MT::Util::encode_html($asset->url), 268 MT::Util::encode_html($fname); 263 269 return $text; 270 } 271 272 # Return a HTML snippet of form options for inserting this asset 273 # into a web page. Default behavior is no options. 274 sub insert_options { 275 my $asset = shift; 276 my ($param) = @_; 277 return undef; 278 } 279 280 sub on_upload { 281 my $asset = shift; 282 my ($param) = @_; 283 1; 264 284 } 265 285 branches/wheeljack/lib/MT/Asset/Image.pm
r928 r937 7 7 package MT::Asset::Image; 8 8 9 use strict; 9 10 use base 'MT::Asset'; 10 11 … … 39 40 my $file = $asset->file_name; 40 41 $file =~ s!\.[a-z]+$!!i; 41 $thumbnail = File::Spec->catfile($path, $file . '-thumb-' . $h . 'x' . $w . '.' . $asset->file_ext);42 my $thumbnail = File::Spec->catfile($path, $file . '-thumb-' . $h . 'x' . $w . '.' . $asset->file_ext); 42 43 my @thumbinfo = stat($thumbnail); 43 44 … … 48 49 49 50 # stale or non-existent thumbnail. let's create one! 50 my $blog = $param{Blog}; 51 $blog ||= MT::Blog->load($asset->blog_id, { cached_ok => 1 }); 51 my $blog = $param{Blog} || $asset->blog; 52 52 return undef unless $blog; 53 53 my $fmgr = $blog->file_mgr; … … 103 103 104 104 sub as_html { 105 my ($self, $q) = @_; 105 my $asset = shift; 106 my ($param) = @_; 106 107 107 108 my $text = ''; 108 109 109 (my $fname = $self->file_name) =~ s/'/\\'/g; 110 my $fname = $asset->file_name; 111 require MT::Util; 110 112 111 113 my $thumb = undef; 112 if ($q->param('thumb')) { 113 $thumb = MT::Asset->load($q->param('thumb_asset_id')) || 114 return MT::App->errtrans( 115 "Can't load asset, ". $q->param('thumb_asset_id') .'.' 114 if ($param->{thumb}) { 115 $thumb = MT::Asset->load($param->{thumb_asset_id}) || 116 return $asset->error( 117 MT->translate("Can't load asset #[_1]", 118 $param->{thumb_asset_id}) 116 119 ); 117 120 } 118 121 119 if ($q->param('popup')) { 122 if ($param->{popup}) { 123 my $dimensions = ''; 124 if ($thumb->image_width && $thumb->image_height) { 125 $dimensions = sprintf('width="%s" height="%s"', 126 $thumb->image_width, $thumb->image_height); 127 } 120 128 my $link = $thumb 121 ? sprintf('<img src="%s" width="%d" height="%d" alt="%s" />', 122 $thumb->url, $thumb->image_width, $thumb->image_height, $fname, 129 ? sprintf('<img src="%s" %s alt="%s" />', 130 MT::Util::encode_html($thumb->url), 131 $dimensions, 132 MT::Util::encode_html($fname), 123 133 ) 124 : '<MT_TRANS phrase="View image">';134 : MT->translate('View image'); 125 135 $text = sprintf( 126 136 q|<a href="%s" onclick="window.open('%s','popup','width=%d,height=%d,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false">%s</a>|, 127 $self->url, $self->url, $self->image_width, $self->image_height, $link, 137 MT::Util::encode_html($asset->url), 138 MT::Util::encode_html($asset->url), 139 $asset->image_width, $asset->image_height, $link, 128 140 ); 129 } 130 elsif ($q->param('include')) { 131 my $wrap_style = $q->param('wrap_text') && $q->param('align') 132 ? 'class="display_img_' . $q->param('align') . '" ' : ''; 133 if ($q->param('thumb')) { 141 } elsif ($param->{include}) { 142 my $wrap_style = $param->{wrap_text} && $param->{align} 143 ? 'class="display_img_' . $param->{align} . '" ' : ''; 144 if ($param->{thumb}) { 145 my $dimensions = ''; 146 if ($thumb->image_width && $thumb->image_height) { 147 $dimensions = sprintf('width="%s" height="%s"', 148 $thumb->image_width, $thumb->image_height); 149 } 134 150 $text = sprintf( 135 '<a href="%s"><img alt="%s" src="%s" width="%d" height="%d" %s/></a>', 136 $self->url, $fname, $thumb->url, $thumb->image_width, $thumb->image_height, $wrap_style, 151 '<a href="%s"><img alt="%s" src="%s" %s %s/></a>', 152 MT::Util::encode_html($asset->url), 153 MT::Util::encode_html($fname), 154 MT::Util::encode_html($thumb->url), 155 $dimensions, $wrap_style, 137 156 ); 138 } 139 else { 157 } else { 158 my $dimensions = ''; 159 if ($asset->image_width && $asset->image_height) { 160 $dimensions = sprintf('width="%s" height="%s"', 161 $asset->image_width, $asset->image_height); 162 } 140 163 $text = sprintf( 141 '<img alt="%s" src="%s" width="%d" height="%d" %s/>', 142 $fname, $self->url, $self->image_width, $self->image_height, $wrap_style, 164 '<img alt="%s" src="%s" %s %s/>', 165 MT::Util::encode_html($fname), 166 MT::Util::encode_html($asset->url), 167 $dimensions, $wrap_style, 143 168 ); 144 169 } 145 } 146 elsif ($q->param('link')) { 170 } else { 147 171 $text = sprintf( 148 '<a href="%s"><MT_TRANS phrase="Download file"></a>', $self->url, 172 '<a href="%s">' . MT->translate('Download file') . '</a>', 173 MT::Util::encode_html($asset->url), 149 174 ); 150 175 } 151 176 152 177 return $text; 178 } 179 180 # Return a HTML snippet of form options for inserting this asset 181 # into a web page. Default behavior is no options. 182 sub insert_options { 183 my $asset = shift; 184 my ($param) = @_; 185 186 my $app = MT->instance; 187 my $perms = $app->{perms}; 188 my $blog = $asset->blog or return; 189 190 eval { require MT::Image; MT::Image->new or die; }; 191 $param->{do_thumb} = $@ ? 0 : 1; 192 193 $param->{can_save_image_defaults} = $perms->can_save_image_defaults ? 1 : 0; 194 $param->{constrain} = $blog->image_default_constrain ? 1 : 0; 195 $param->{popup} = $blog->image_default_popup ? 1 : 0; 196 $param->{image_defaults} = $blog->image_default_set ? 1 : 0; 197 $param->{wrap_text} = $blog->image_default_wrap_text ? 1 : 0; 198 $param->{make_thumb} = $blog->image_default_thumb ? 1 : 0; 199 $param->{'align_'.$_} = $blog->image_default_align eq $_ ? 1 : 0 200 for qw(left center right); 201 $param->{'unit_w'.$_} = $blog->image_default_wunits eq $_ ? 1 : 0 202 for qw(percent pixels); 203 $param->{'unit_h'.$_} = $blog->image_default_hunits eq $_ ? 1 : 0 204 for qw(percent pixels); 205 $param->{thumb_width} = $blog->image_default_width || $asset->image_width || 0; 206 $param->{thumb_height} = $blog->image_default_height || $asset->image_height || 0; 207 208 return $app->build_page('asset_image_options.tmpl', $param); 209 } 210 211 sub on_upload { 212 my $asset = shift; 213 my ($param) = @_; 214 215 my $app = MT->instance; 216 require MT::Util; 217 218 my $url = $asset->url; 219 my $width = $asset->image_width; 220 my $height = $asset->image_height; 221 222 my ($base_url, $fname) = $url =~ m|(.*)/([^/]*)|; 223 $url = $base_url . '/' . $fname; # no need to re-encode filename; url is already encoded 224 my $blog = $asset->blog or return; 225 my $blog_id = $blog->id; 226 227 my($thumb, $thumb_width, $thumb_height); 228 # Save new defaults if requested. 229 if($param->{image_defaults}) { 230 return $app->error($app->translate( 231 'Permission denied setting image defaults for blog #[_2]', $blog_id 232 )) unless $app->{perms}->can_save_image_defaults; 233 $blog->image_default_set(1); 234 $blog->image_default_wrap_text($param->{wrap_text} ? 1 : 0); 235 $blog->image_default_align($param->{align} || MT::Blog::ALIGN()); 236 $blog->image_default_thumb($param->{thumb} ? 1 : 0); 237 $blog->image_default_width($param->{thumb_width} || MT::Blog::WIDTH()); 238 $blog->image_default_wunits($param->{thumb_width_type} || MT::Blog::UNITS()); 239 $blog->image_default_height($param->{thumb_height} || MT::Blog::WIDTH()); 240 $blog->image_default_hunits($param->{thumb_height_type} || MT::Blog::UNITS()); 241 $blog->image_default_constrain($param->{constrain} ? 1 : 0); 242 $blog->image_default_popup($param->{popup} ? 1 : 0); 243 $blog->save; 244 } 245 246 # Thumbnail creation 247 if ($thumb = $param->{thumb}) { 248 require MT::Image; 249 my $base_path = $param->{site_path} ? 250 $blog->site_path : $blog->archive_path; 251 my $file = $param->{fname}; 252 if ($file =~ m!\.\.|\0|\|!) { 253 return $app->error($app->translate("Invalid filename '[_1]'", $file)); 254 } 255 my $i_file = File::Spec->catfile($base_path, $file); 256 ## Untaint. We checked $file for security holes above. 257 ($i_file) = $i_file =~ /(.+)/s; 258 my $fmgr = $blog->file_mgr; 259 my $data = $fmgr->get_data($i_file, 'upload') 260 or return $app->error($app->translate( 261 "Reading '[_1]' failed: [_2]", $i_file, $fmgr->errstr)); 262 my $image_type = scalar $param->{image_type}; 263 my $img = MT::Image->new( Data => $data, 264 Type => $image_type ) 265 or return $app->error($app->translate( 266 "Thumbnail failed: [_1]", MT::Image->errstr)); 267 my($w, $h) = map $param->{$_}, qw( thumb_width thumb_height ); 268 (my($blob), $thumb_width, $thumb_height) = 269 $img->scale( Width => $w, Height => $h ) 270 or return $app->error($app->translate("Thumbnail failed: [_1]", 271 $img->errstr)); 272 require File::Basename; 273 my($base, $path, $ext) = File::Basename::fileparse($i_file, '\.[^.]*'); 274 my $t_file = $path . $base . '-thumb' . $ext; 275 my $basename = $base . '-thumb' . $ext; 276 ## If the thumbnail filename already exists, we don't want to overwrite 277 ## it, because it could contain valuable data; so we'll just make 278 ## sure to generate the name uniquely. 279 my $i = 0; 280 while ($fmgr->exists($t_file)) { 281 $t_file = File::Spec->catfile($path . $base . '-thumb' . (++$i) . $ext); 282 } 283 $fmgr->put_data($blob, $t_file, 'upload') 284 or return $app->error($app->translate( 285 "Error writing to '[_1]': [_2]", $t_file, $fmgr->errstr)); 286 287 $file =~ s/\Q$base$ext\E$//; 288 my $url = $param->{site_path} ? $blog->site_url : $blog->archive_url; 289 $url .= '/' unless $url =~ m!/$!; 290 $url .= $file; 291 $thumb = $url . MT::Util::encode_url($base . '-thumb' . $ext); 292 293 my $img_pkg = MT::Asset->class_handler('image'); 294 my $asset_thumb = new $img_pkg; 295 my $original = $asset_thumb->clone; 296 $asset_thumb->blog_id($blog_id); 297 $asset_thumb->url($thumb); 298 $asset_thumb->file_path($t_file); 299 $asset_thumb->file_name($basename); 300 my $ext2 = $ext; 301 $ext2 =~ s/^\.//; 302 $asset_thumb->file_ext($ext2); 303 $asset_thumb->image_width($thumb_width); 304 $asset_thumb->image_height($thumb_height); 305 $asset_thumb->created_by($app->user->id); 306 $asset_thumb->save; 307 MT->run_callbacks('CMSPostSave.asset', $app, $asset_thumb, $original); 308 309 $param->{thumb_asset_id} = $asset_thumb->id; 310 311 MT->run_callbacks('CMSUploadFile.thumbnail', 312 File => $t_file, Url => $thumb, Size => length($blob), 313 Asset => $asset_thumb, 314 Type => 'thumbnail', 315 Blog => $blog); 316 317 MT->run_callbacks('CMSUploadImage.thumbnail', 318 File => $t_file, Url => $thumb, 319 Asset => $asset_thumb, 320 Width => $thumb_width, Height => $thumb_height, 321 ImageType => $image_type, 322 Size => length($blob), 323 Type => 'thumbnail', 324 Blog => $blog); 325 } 326 if ($param->{popup}) { 327 require MT::Template; 328 if (my $tmpl = MT::Template->load({ blog_id => $blog_id, 329 type => 'popup_image' })) { 330 (my $rel_path = $param->{fname}) =~ s!\.[^.]*$!!; 331 if ($rel_path =~ m!\.\.|\0|\|!) { 332 return $app->error($app->translate( 333 "Invalid basename '[_1]'", $rel_path)); 334 } 335 my $ext = $blog->file_extension || ''; 336 $ext = '.' . $ext if $ext ne ''; 337 require MT::Template::Context; 338 my $ctx = MT::Template::Context->new; 339 $ctx->stash('blog', $blog); 340 $ctx->stash('blog_id', $blog->id); 341 $ctx->stash('asset', $asset); 342 $ctx->stash('image_url', $url); 343 $ctx->stash('image_width', $width); 344 $ctx->stash('image_height', $height); 345 my $popup = $tmpl->build($ctx) or die $tmpl->errstr; 346 my $fmgr = $blog->file_mgr; 347 my $root_path = $param->{site_path} ? 348 $blog->site_path : $blog->archive_path; 349 my $abs_file_path = File::Spec->catfile($root_path, $rel_path . $ext); 350 351 ## If the popup filename already exists, we don't want to overwrite 352 ## it, because it could contain valuable data; so we'll just make 353 ## sure to generate the name uniquely. 354 my($i, $rel_path_ext) = (0, $rel_path . $ext); 355 while ($fmgr->exists($abs_file_path)) { 356 $rel_path_ext = $rel_path . ++$i . $ext; 357 $abs_file_path = File::Spec->catfile($root_path, $rel_path_ext); 358 } 359 my ($vol, $dirs, $basename) = File::Spec->splitpath($rel_path_ext); 360 my $rel_url_ext = File::Spec->catpath($vol, $dirs, MT::Util::encode_url($basename)); 361 362 ## Untaint. We have checked for security holes above, so we 363 ## should be safe. 364 ($abs_file_path) = $abs_file_path =~ /(.+)/s; 365 $fmgr->put_data($popup, $abs_file_path, 'upload') 366 or return $app->error($app->translate( 367 "Error writing to '[_1]': [_2]", $abs_file_path, 368 $fmgr->errstr)); 369 $url = $param->{site_path} ? 370 $blog->site_url : $blog->archive_url; 371 $url .= '/' unless $url =~ m!/$!; 372 $rel_url_ext =~ s!^/!!; 373 $url .= $rel_url_ext; 374 375 my $html_pkg = MT::Asset->handler_for_file($abs_file_path); 376 my $asset_html = new $html_pkg; 377 my $original = $asset_html->clone; 378 $asset_html->blog_id($blog_id); 379 $asset_html->url($url); 380 $asset_html->file_path($abs_file_path); 381 $asset_html->file_name($basename); 382 $asset_html->file_ext($blog->file_extension); 383 $asset_html->created_by($app->user->id); 384 $asset_html->save; 385 MT->run_callbacks('CMSPostSave.asset', $app, $asset_html, $original); 386 387 MT->run_callbacks('CMSUploadFile.popup', 388 File => $abs_file_path, Url => $url, 389 Asset => $asset_html, 390 Size => length($popup), 391 Type => 'popup', 392 Blog => $blog); 393 } 394 } 395 1; 153 396 } 154 397 branches/wheeljack/tmpl/cms/dialog_list_assets.tmpl
r935 r937 33 33 // setup 34 34 dlg = new Dialog.Simple("list-assets"); 35 var panel = new ListingPanel("asset s");35 var panel = new ListingPanel("asset"); 36 36 dlg.panel = panel; 37 38 // hook into view updates so we can close any opened detail panel 37 39 var old_update = panel.datasource.onUpdate; 38 40 panel.datasource.onUpdate = function(ds) { viewChange(ds, old_update) }; 41 39 42 panel.pager.setState(<TMPL_VAR NAME=PAGER_JSON>); 40 43 panel.parent = dlg; … … 45 48 </script> 46 49 47 <div id="asset s-panel" class="panel">50 <div id="asset-panel" class="panel"> 48 51 49 52 <h2><span class="weblog-title-highlight"><TMPL_IF NAME=EDIT_BLOG_ID><TMPL_VAR NAME=BLOG_NAME ESCAPE=HTML><TMPL_ELSE><MT_TRANS phrase="System-wide"></TMPL_IF>: </span> <MT_TRANS phrase="Assets"></h2> … … 59 62 </TMPL_IF> 60 63 </p> 64 65 <div class="search-box"> 66 <TMPL_IF PANEL_SEARCHABLE> 67 <form action="" method="get"> 68 <p class="page-desc"><TMPL_VAR NAME=SEARCH_PROMPT> 69 <input type="text" class="search-input"<TMPL_UNLESS PANEL_SEARCHABLE> disabled="disabled"</TMPL_UNLESS> /> 70 <input type="image" class="search-command" src="<TMPL_VAR NAME=STATIC_URI>images/nav_icons/color/search.gif" title="<MT_TRANS phrase="Search">" /> 71 <input type="button" class="search-reset hidden" value="<MT_TRANS phrase="View All">" /> 72 </p></form> 73 <TMPL_ELSE> 74 <p class="page-desc"> <img border="0" src="<TMPL_VAR NAME=STATIC_URI>images/spacer.gif" width="17" height="20"></p> 75 </TMPL_IF> 76 </div> 61 77 62 78 <div class="list-wrapper"> branches/wheeljack/tmpl/cms/upload_complete.tmpl
r935 r937 4 4 <!-- 5 5 6 function doClick (f, trigger) {6 function presubmit(f) { 7 7 var mode = 'list_assets'; 8 8 <TMPL_IF NAME=ENTRY_INSERT> … … 10 10 <TMPL_ELSE> 11 11 <TMPL_IF NAME=CAN_POST> 12 if (f.new_entry .checked) mode = 'start_upload_entry';12 if (f.new_entry && f.new_entry.checked) mode = 'start_upload_entry'; 13 13 </TMPL_IF> 14 14 </TMPL_IF> 15 doHandleMode(f, trigger, mode); 16 } 17 18 function doHandleMode (f, trigger, mode) { 19 var url = '<TMPL_VAR NAME=SCRIPT_URL>?__mode=' + mode + trigger; 20 url += '&blog_id=<TMPL_VAR NAME=BLOG_ID>&site_path=<TMPL_VAR NAME=SITE_PATH>&fname=<TMPL_VAR NAME=FNAME ESCAPE=URL>&url=<TMPL_VAR NAME=URL ESCAPE=URL>'; 21 <TMPL_IF NAME=DO_THUMB> 22 if (f.thumb.checked) { 23 url += '&thumb=1' + calcGeometry(f); 24 url += '&thumb_width_type=pixels&thumb_height_type=pixels&constrain=1'; 15 if (mode != 'asset_insert') { 16 f.target = "_top"; 25 17 } 26 </TMPL_IF> 27 <TMPL_IF NAME=IS_IMAGE> 28 url += '&width=<TMPL_VAR NAME=WIDTH>&height=<TMPL_VAR NAME=HEIGHT>&image_type=<TMPL_VAR NAME=IMAGE_TYPE>'; 29 if (f.include.checked) { 30 url += '&include=1'; 31 } else { 32 url += '&link=1'; 33 } 34 if (f.include.checked) { 35 url += '&wrap_text=1&align='; 36 for (i = 0; i < f.align.length; i++) { 37 if (f.align[i].checked) url += f.align[i].value; 38 } 39 } 40 41 if (f.popup.checked) url += '&popup=1'; 42 if (f.image_defaults.checked) url += '&image_defaults=1'; 43 </TMPL_IF> 44 <TMPL_IF NAME=ENTRY_INSERT> 45 url += '&id=<TMPL_VAR NAME=ASSET_ID>&edit_field=<TMPL_VAR NAME=EDIT_FIELD>'; 46 location.href = url; 47 <TMPL_ELSE> 48 closeDialog(url); 49 </TMPL_IF> 50 } 51 52 <TMPL_IF NAME=DO_THUMB> 53 54 function calcGeometry (f) { 55 var w = calcDim(f.thumb_width, f.thumb_width_type, f.full_width.value); 56 var h = calcDim(f.thumb_height, f.thumb_height_type, f.full_height.value); 57 return '&thumb_width=' + w + '&thumb_height=' + h; 58 } 59 60 function calcDim (val, type, full) { 61 return val.value; 62 } 63 64 function adjustPixelsPct (f, width) { 65 // if (!f.constrain.checked) return; 66 var e, s, full; 67 if (width) { 68 e = f.thumb_width;
