/ 软件 / 1048浏览

危险的TimThumb裁剪显示WordPress缩略图方法

昨日写了《WordPress文章显示第一张图或随机图片》,实现了在首页不使用特色图像功能显示自定义外链图片功能,但有一个问题便是图片裁切使用了比较简单粗暴的方法。

<!--裁切高大于宽的图片-->
<div style="overflow:hidden;width:100%;max-height:100px;">

以至于只能显示图片上半段而不能显示图片的中间地带。

试了clip(rect)函数不太灵光,找了一圈,看到有文章《wordpress缩略图尺寸重新裁剪timthumb》、《wordpress缩略图裁剪timthumb(二)》,介绍了比较流行的办法是使用TimThumb来裁切图片。其具体方法为:

1、下载timthumb.php文件(版本号为2.8.13,链接:百度网盘或github)。

2、将timthumb.php打开,在如下代码中的允许网站列表中添加自己的网站:

// If ALLOW_EXTERNAL is true and ALLOW_ALL_EXTERNAL_SITES is false, then external images will only be fetched from these domains and their subdomains. 
if(! isset($ALLOWED_SITES)){
	$ALLOWED_SITES = array (
		'flickr.com',
		'staticflickr.com',
		'picasa.com',
		'img.youtube.com',
		'upload.wikimedia.org',
		'photobucket.com',
		'imgur.com',
		'imageshack.us',
		'tinypic.com',
//添加在这里,比如使用163相册的,就添加'126.net',。同时,上面的都可以删掉。
	);
}

3、把timthumb.php上传到当前主题文件夹根目录下。在根目录新建文件夹cache,并改权限为755甚至是777。

4、参考昨日我写的《WordPress文章显示第一张图或随机图片》中的第1、2步,将相关代码放入functions.php,并新建图片文件夹。

5、将相关缩略图代码修订为:

<img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo catch_first_image(); ?>&w=267&h=130&zc=1" alt="<?php the_title(); ?>" class="thumbnail"/>

其中&w=267&h=130是要显示的图片大小。插入要显示的首页处。

6、刷新页面后即可。运行效果挺完美:

timthumb
timthumb

本以为就此便可高枕无忧,但看到说本代码不安全,需要nginx与Apache禁止目录执行php文件权限。一搜后发现包括知更鸟在内的很多大咖都曾著文警告,且本代码的1.0版在2010年爆发过安全漏洞,虽说如今代码已更新至2.8.4,但仍然感觉不能掉以轻心,想着再看看有没有更新的版本,便毅然翻到谷歌找到原作者在BinaryMoon的TimThumb项目网址,又循着找到原作者的博客——不看不知道,一看吓一跳,作者自爆今年6月底又发现了新的安全漏洞(见《New TimThumb Exploit Found》)并做了紧急补丁!继续谷歌搜索,又在网上搜索到了一段攻击代码,主要是利用$ALLOWED_SITES的漏洞,实现将伪装为比如http://flickr.com.falsesite.com这样的网站加入信任列表并任意上传恶意文件,具体网址不贴了以免作恶。作者最后在10月份正式宣布不再支持更新TimThumb项目:

2 MONTHS AGO

TimThumb is No Longer Supported or Maintained

TIMTHUMB

A long time ago – when making our first premium WordPress theme, Darren and I made TimThumb. TimThumb has been amazing – but it’s also not been without it’s share of problems.

In particular in 2010 there was a major security exploit found and it hurt a lot of websites, my own included. There are still people who are suffering because of it. I’ve felt incredibly guilty about this for years now, and so my enthusiasm for TimThumb has dropped to nothing.

Because of this lack of enthusiasm, and a fear of doing something else wrong, I have barely touched the code in years. In fact a couple of months ago I wrote about why I don’t use TimThumb (and what I do instead). If you’re a WordPress developer and still using TimThumb then you are ‘doing it wrong’. As such I am dropping all future support and maintenance for TimThumb.

To be honest this has been the situation for a while now, I’ve just not announced it before. If you want to use TimThumb then you do so at your own risk.

It feels a little sad to be writing this – but it’s also a huge weight off my mind. Now I can go back to making WordPress themes and video games in peace :).

作者在文中一再表示对从前爆发的漏洞很有负罪感,且自己不再使用这些代码,并警告所有使用此代码的博主at your own risk。作者并建议使用Jetpack插件和Photon扩展来实现。不过不幸的是,在我朝因为WordPress.com的服务被禁,因此这些涉及到WordPress.com的CDN服务的插件也就没办法使用了。作者还推荐使用Regenerate Thumbnails plugin这样的插件,但总体来说仍然属于隔靴瘙痒。

目前来看裁切图片的方法可能仍然需要回归到最本源的办法,即通过CSS综合运用或利用WordPress自带的函数来达到目的。本文写完自己也吓出一身冷汗,幸好没有很贸然的向读者们推荐,否则岂非又害了很多博主?只是目前在网上仍然大量的存在使用TimThumb的博客、相关主题和推荐文章,非常有误导性。在这里也呼吁,如果一定要使用,请记得妥善设置你的cache文件夹权限及$ALLOW_SITES列表里尽量把无用的网址都删掉,并将timthumb.php改一个文件名。所有曾经转载过TimThumb代码的博主,在本代码未来可能仍存在一定风险的前提下,在转载时应提醒安装者代码的风险自负,并欢迎转载本文。

路慢慢其修远兮,吾将上下而求索。

2

  1. 网上抄代码就是有这个风险,现在我都尽量只用内建函数并及时升级,网上代码找不到原作者还非常复杂的一概不用。

    Google Chrome 39 · Android 4.1.1
    1. S
      Firefox 33 · Ubuntu

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注