BJ Lazy Load - Version 1.0

Version Description

Download this release

Release Info

Developer bjornjohansen
Plugin Icon 128x128 BJ Lazy Load
Version 1.0
Comparing to
See all releases

Code changes from version 0.7.5 to 1.0

admin.php DELETED
@@ -1,127 +0,0 @@
1
- <?php
2
-
3
- class BJLL_Admin_Page extends scbAdminPage {
4
-
5
- function setup() {
6
- $this->args = array(
7
- 'menu_title' => 'BJ Lazy Load',
8
- 'page_title' => __( 'BJ Lazy Load Options', 'bj-lazy-load' ),
9
- );
10
- }
11
-
12
- function page_content() {
13
-
14
-
15
- $optionfields = array(
16
- array(
17
- 'title' => __( 'Apply to content', 'bj-lazy-load' ),
18
- 'type' => 'radio',
19
- 'name' => 'filter_content',
20
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
21
- ),
22
- array(
23
- 'title' => __( 'Apply to text widgets', 'bj-lazy-load' ),
24
- 'type' => 'radio',
25
- 'name' => 'filter_widget_text',
26
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
27
- ),
28
- array(
29
- 'title' => __( 'Apply to post thumbnails', 'bj-lazy-load' ),
30
- 'type' => 'radio',
31
- 'name' => 'filter_post_thumbnails',
32
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
33
- ),
34
- array(
35
- 'title' => __( 'Apply to gravatars', 'bj-lazy-load' ),
36
- 'type' => 'radio',
37
- 'name' => 'filter_gravatars',
38
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
39
- ),
40
- array(
41
- 'title' => __( 'Lazy load images', 'bj-lazy-load' ),
42
- 'type' => 'radio',
43
- 'name' => 'lazy_load_images',
44
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
45
- ),
46
- array(
47
- 'title' => __( 'Lazy load iframes', 'bj-lazy-load' ),
48
- 'type' => 'radio',
49
- 'name' => 'lazy_load_iframes',
50
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
51
- ),
52
- array(
53
- 'title' => __( 'Theme loader function', 'bj-lazy-load' ),
54
- 'type' => 'select',
55
- 'name' => 'theme_loader_function',
56
- 'value' => array( 'wp_footer', 'wp_head' ),
57
- ),
58
- array(
59
- 'title' => __( 'Placeholder Image URL', 'bj-lazy-load' ),
60
- 'type' => 'text',
61
- 'name' => 'placeholder_url',
62
- 'desc' => sprintf( '<p class="description">%s</p>', __( 'Leave blank for default', 'bj-lazy-load' ) ),
63
- ),
64
- array(
65
- 'title' => __( 'Skip images with classes', 'bj-lazy-load' ),
66
- 'type' => 'text',
67
- 'name' => 'skip_classes',
68
- 'desc' => sprintf( '<p class="description">%s</p>', __( 'Comma separated. Example: "no-lazy, lazy-ignore, image-235"', 'bj-lazy-load' ) ),
69
- ),
70
- array(
71
- 'title' => __( 'Threshold', 'bj-lazy-load' ),
72
- 'type' => 'text',
73
- 'name' => 'threshold',
74
- 'desc' => sprintf( '<p class="description">%s</p>', __( 'How close to the viewport the element should be when we load it. In pixels. Example: 200', 'bj-lazy-load' ) ),
75
- )
76
- );
77
-
78
- $optionfields[] = array(
79
- 'title' => __( 'Infinite scroll', 'bj-lazy-load' ),
80
- 'type' => 'radio',
81
- 'name' => 'infinite_scroll',
82
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
83
- 'desc' => sprintf( '<p class="description">%s</p>', __( 'Enable if your theme uses infinite scroll.', 'bj-lazy-load' ) ),
84
- );
85
-
86
- $optionfields[] = array(
87
- 'title' => __( 'Load hiDPI (retina) images', 'bj-lazy-load' ),
88
- 'type' => 'radio',
89
- 'name' => 'load_hidpi',
90
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
91
- 'desc' => sprintf( '<p class="description">%s</p>', __( 'Will load hiDPI version of the images if the current browser/screen supports them. (Experimental feature. Do NOT enable if you are using a CDN)', 'bj-lazy-load' ) ),
92
- );
93
-
94
- $optionfields[] = array(
95
- 'title' => __( 'Load responsive images', 'bj-lazy-load' ),
96
- 'type' => 'radio',
97
- 'name' => 'load_responsive',
98
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
99
- 'desc' => sprintf( '<p class="description">%s</p>', __( 'Will load scaled down version of the images if the image is scaled down in the theme. (Experimental feature. Do NOT enable if you are using a CDN)', 'bj-lazy-load' ) ),
100
- );
101
-
102
-
103
- if ( BJLL::has_wptouch() ) {
104
- $optionfields[] = array(
105
- 'title' => __( 'Disable on WPTouch', 'bj-lazy-load' ),
106
- 'type' => 'radio',
107
- 'name' => 'disable_on_wptouch',
108
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
109
- 'desc' => sprintf( '<p class="description">%s</p>', __( 'Disables BJ Lazy Load when the WPTouch mobile theme is used', 'bj-lazy-load' ) ),
110
- );
111
- }
112
-
113
- if ( BJLL::has_mobilepress() ) {
114
- $optionfields[] = array(
115
- 'title' => __( 'Disable on MobilePress', 'bj-lazy-load' ),
116
- 'type' => 'radio',
117
- 'name' => 'disable_on_mobilepress',
118
- 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
119
- 'desc' => sprintf( '<p class="description">%s</p>', __( 'Disables BJ Lazy Load when the MobilePress mobile theme is used', 'bj-lazy-load' ) ),
120
- );
121
- }
122
-
123
- echo $this->form_table( $optionfields );
124
-
125
- }
126
-
127
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bj-lazy-load.php CHANGED
@@ -1,320 +1,40 @@
1
- <?php
2
- /*
3
- Plugin Name: BJ Lazy Load
4
- Plugin URI: http://wordpress.org/extend/plugins/bj-lazy-load/
5
- Description: Lazy image loading makes your site load faster and saves bandwidth.
6
- Version: 0.7.5
7
- Author: Bjørn Johansen
8
- Author URI: http://twitter.com/bjornjohansen
9
- Text Domain: bj-lazy-load
10
- License: GPL2
11
-
12
- Copyright 2011–2014 Bjørn Johansen (email : post@bjornjohansen.no)
13
-
14
- This program is free software; you can redistribute it and/or modify
15
- it under the terms of the GNU General Public License, version 2, as
16
- published by the Free Software Foundation.
17
-
18
- This program is distributed in the hope that it will be useful,
19
- but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
- GNU General Public License for more details.
22
-
23
- You should have received a copy of the GNU General Public License
24
- along with this program; if not, write to the Free Software
25
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
-
27
- */
28
-
29
- require_once( dirname(__FILE__) . '/scb/load.php' );
30
- require_once( dirname(__FILE__) . '/inc/lang.php' );
31
- require_once( dirname(__FILE__) . '/inc/class-bjll-skip-post.php' );
32
-
33
- if ( ! class_exists( 'BJLL' ) ) {
34
- class BJLL {
35
-
36
- const version = '0.7.5';
37
- protected $_placeholder_url;
38
- protected $_skip_classes;
39
-
40
- protected static $_instance;
41
-
42
- function __construct() {
43
-
44
- // Disable when viewing printable page from WP-Print
45
- if ( intval( get_query_var( 'print' ) ) == 1 || intval( get_query_var( 'printpage' ) ) == 1 ) {
46
- return;
47
- }
48
-
49
- // Disable on Opera Mini
50
- if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' ) !== false ) {
51
- return;
52
- }
53
-
54
- $options = self::_get_options();
55
-
56
- if ( 'yes' == $options->get( 'disable_on_wptouch' ) && self::is_wptouch() ) {
57
- return;
58
- }
59
-
60
- if ( 'yes' == $options->get( 'disable_on_mobilepress' ) && self::is_mobilepress() ) {
61
- return;
62
- }
63
-
64
- add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
65
- add_filter( 'bj_lazy_load_html', array( __CLASS__, 'filter' ), 10, 1 );
66
-
67
- $skip_classes = $options->get( 'skip_classes' );
68
- if ( strlen( trim( $skip_classes ) ) ) {
69
- $this->_skip_classes = array_map( 'trim', explode( ',', $options->get( 'skip_classes' ) ) );
70
- }
71
-
72
- $this->_placeholder_url = $options->get( 'placeholder_url' );
73
- if ( ! strlen( $this->_placeholder_url ) ) {
74
- //$this->_placeholder_url = plugins_url( '/img/placeholder.gif', __FILE__ );
75
- $this->_placeholder_url = 'data:image/gif;base64,R0lGODdhAQABAPAAAP///wAAACwAAAAAAQABAEACAkQBADs=';
76
- }
77
-
78
- if ( $options->get( 'filter_content' ) == 'yes' ) {
79
- add_filter( 'the_content', array( $this, 'filter' ), 200 );
80
- }
81
- if ( $options->get( 'filter_widget_text' ) == 'yes' ) {
82
- add_filter( 'widget_text', array( $this, 'filter' ), 200 );
83
- }
84
- if ( $options->get( 'filter_post_thumbnails' ) == 'yes' ) {
85
- add_filter( 'post_thumbnail_html', array( $this, 'filter' ), 200 );
86
- }
87
- if ( $options->get( 'filter_gravatars' ) == 'yes' ) {
88
- add_filter( 'get_avatar', array( $this, 'filter' ), 200 );
89
- }
90
- }
91
-
92
- static function singleton() {
93
- if ( ! isset( self::$_instance ) ) {
94
- $className = __CLASS__;
95
- self::$_instance = new $className;
96
- }
97
- return self::$_instance;
98
- }
99
-
100
- static function enqueue_scripts() {
101
-
102
- $in_footer = true;
103
-
104
- $options = self::_get_options();
105
- $theme_loader_function = $options->get( 'theme_loader_function' );
106
-
107
- if ( $theme_loader_function == 'wp_head' ) {
108
- $in_footer = false;
109
- }
110
-
111
-
112
-
113
- if ( defined( 'SCRIPT_DEBUG') && SCRIPT_DEBUG ) {
114
- wp_enqueue_script( 'jquery.sonar', plugins_url( '/js/jquery.sonar.js', __FILE__ ), array( 'jquery' ), self::version, $in_footer );
115
- wp_enqueue_script( 'BJLL', plugins_url( '/js/bj-lazy-load.js', __FILE__ ), array( 'jquery', 'jquery.sonar' ), self::version, $in_footer );
116
- } else {
117
- wp_enqueue_script( 'BJLL', plugins_url( '/js/combined.min.js', __FILE__ ), array( 'jquery' ), self::version, $in_footer );
118
- }
119
-
120
- $bjll_options = array();
121
-
122
- if ( $options->get('load_hidpi') == 'yes' || $options->get('load_responsive') == 'yes' ) {
123
- $bjll_options['thumb_base'] = plugins_url( '/thumb.php', __FILE__ ) . '?src=';
124
- $bjll_options['load_hidpi'] = $options->get('load_hidpi');
125
- $bjll_options['load_responsive'] = $options->get('load_responsive');
126
-
127
- if ( is_multisite() ) {
128
- $bjll_options['site_url'] = get_site_url();
129
- $bjll_options['network_site_url'] = network_site_url();
130
- }
131
- }
132
-
133
- if ( $options->get('infinite_scroll') == 'yes' ) {
134
- $bjll_options['infinite_scroll'] = $options->get('infinite_scroll');
135
- }
136
-
137
- if ( intval( $options->get('threshold') ) != 200 ) {
138
- $bjll_options['threshold'] = intval( $options->get('threshold') );
139
- }
140
-
141
-
142
- if ( count( $bjll_options ) ) {
143
- wp_localize_script( 'BJLL', 'BJLL', $bjll_options );
144
- }
145
-
146
- }
147
-
148
- static function filter( $content ) {
149
-
150
- $run_filter = true;
151
- $run_filter = apply_filters( 'bj_lazy_load_run_filter', $content );
152
-
153
- if ( ! $run_filter ) {
154
- return $content;
155
- }
156
-
157
- $BJLL = BJLL::singleton();
158
-
159
- $options = self::_get_options();
160
-
161
- if ( $options->get('lazy_load_images') == 'yes' ) {
162
- $content = $BJLL->_filter_images( $content );
163
- }
164
-
165
- if ( $options->get('lazy_load_iframes') == 'yes' ) {
166
- $content = $BJLL->_filter_iframes( $content );
167
- }
168
-
169
- return $content;
170
- }
171
-
172
- protected function _filter_images( $content ) {
173
-
174
- $matches = array();
175
- preg_match_all( '/<img[\s\r\n]+.*?>/is', $content, $matches );
176
-
177
- $search = array();
178
- $replace = array();
179
-
180
- if ( is_array( $this->_skip_classes ) ) {
181
- $skip_images_preg_quoted = array_map( 'preg_quote', $this->_skip_classes );
182
- $skip_images_regex = sprintf( '/class=".*(%s).*"/s', implode( '|', $skip_images_preg_quoted ) );
183
- }
184
-
185
- foreach ( $matches[0] as $imgHTML ) {
186
-
187
- // don't to the replacement if a skip class is provided and the image has the class, or if the image is a data-uri
188
- if ( ! ( is_array( $this->_skip_classes ) && preg_match( $skip_images_regex, $imgHTML ) ) && ! preg_match( "/src=['\"]data:image/is", $imgHTML ) ) {
189
- // replace the src and add the data-src attribute
190
- $replaceHTML = preg_replace( '/<img(.*?)src=/is', '<img$1src="' . $this->_placeholder_url . '" data-lazy-type="image" data-lazy-src=', $imgHTML );
191
-
192
- // add the lazy class to the img element
193
- if ( preg_match( '/class=["\']/i', $replaceHTML ) ) {
194
- $replaceHTML = preg_replace( '/class=(["\'])(.*?)["\']/is', 'class=$1lazy lazy-hidden $2$1', $replaceHTML );
195
- } else {
196
- $replaceHTML = preg_replace( '/<img/is', '<img class="lazy lazy-hidden"', $replaceHTML );
197
- }
198
-
199
- $replaceHTML .= '<noscript>' . $imgHTML . '</noscript>';
200
-
201
- array_push( $search, $imgHTML );
202
- array_push( $replace, $replaceHTML );
203
- }
204
- }
205
-
206
- $content = str_replace( $search, $replace, $content );
207
-
208
-
209
- return $content;
210
- }
211
-
212
- protected function _filter_iframes( $content ) {
213
-
214
- $matches = array();
215
- preg_match_all( '/<iframe\s+.*?>/', $content, $matches );
216
-
217
- $search = array();
218
- $replace = array();
219
-
220
- foreach ( $matches[0] as $iframeHTML ) {
221
-
222
- // Don't mess with the Gravity Forms ajax iframe
223
- if ( strpos( $iframeHTML, 'gform_ajax_frame' ) ) {
224
- continue;
225
- }
226
-
227
- $replaceHTML = '<img src="' . $this->_placeholder_url . '" class="lazy lazy-hidden" data-lazy-type="iframe" data-lazy-src="' . base64_encode($iframeHTML) . '" alt="">';
228
-
229
- $replaceHTML .= '<noscript>' . $iframeHTML . '</noscript>';
230
-
231
- array_push( $search, $iframeHTML );
232
- array_push( $replace, $replaceHTML );
233
- }
234
-
235
- $content = str_replace( $search, $replace, $content );
236
-
237
- return $content;
238
- }
239
-
240
- protected static function _get_options() {
241
- return new scbOptions( 'bj_lazy_load_options', __FILE__, array(
242
- 'filter_content' => 'yes',
243
- 'filter_widget_text' => 'yes',
244
- 'filter_post_thumbnails' => 'yes',
245
- 'filter_gravatars' => 'yes',
246
- 'lazy_load_images' => 'yes',
247
- 'lazy_load_iframes' => 'yes',
248
- 'theme_loader_function' => 'wp_footer',
249
- 'placeholder_url' => '',
250
- 'skip_classes' => '',
251
- 'load_hidpi' => 'no',
252
- 'load_responsive' => 'no',
253
- 'disable_on_wptouch' => 'yes',
254
- 'disable_on_mobilepress' => 'yes',
255
- 'infinite_scroll' => 'no',
256
- 'threshold' => '200'
257
- ) );
258
- }
259
-
260
- static function options_init() {
261
-
262
- $options = self::_get_options();
263
-
264
- // Creating settings page objects
265
- if ( is_admin() ) {
266
- require_once( dirname( __FILE__ ) . '/admin.php' );
267
- new BJLL_Admin_Page( __FILE__, $options );
268
- }
269
- }
270
-
271
- static function is_wptouch() {
272
- if ( function_exists( 'bnc_wptouch_is_mobile' ) && bnc_wptouch_is_mobile() ) {
273
- return true;
274
- }
275
-
276
- global $wptouch_pro;
277
-
278
- if ( defined( 'WPTOUCH_VERSION' ) || is_object( $wptouch_pro ) ) {
279
-
280
- if ( $wptouch_pro->showing_mobile_theme ) {
281
- return true;
282
- }
283
- }
284
-
285
- return false;
286
- }
287
-
288
- static function has_wptouch() {
289
- if ( function_exists( 'bnc_wptouch_is_mobile' ) || defined( 'WPTOUCH_VERSION' ) ) {
290
- return true;
291
- }
292
-
293
- return false;
294
- }
295
-
296
- static function is_mobilepress() {
297
-
298
- if ( function_exists( 'mopr_get_option' ) && WP_CONTENT_DIR . mopr_get_option( 'mobile_theme_root', 1 ) == get_theme_root() ) {
299
- return true;
300
- }
301
-
302
- return false;
303
- }
304
-
305
- static function has_mobilepress() {
306
- if ( class_exists( 'Mobilepress_core' ) ) {
307
- return true;
308
- }
309
-
310
- return false;
311
- }
312
-
313
- }
314
- }
315
-
316
-
317
- add_action( 'wp', create_function('', 'if ( ! is_feed() ) { BJLL::singleton(); }'), 10, 0 );
318
-
319
- scb_init( array( 'BJLL', 'options_init' ) );
320
-
1
+ <?php
2
+ /*
3
+ Plugin Name: BJ Lazy Load
4
+ Plugin URI: https://wordpress.org/plugins/bj-lazy-load/
5
+ Description: Lazy image loading makes your site load faster and saves bandwidth.
6
+ Version: 1.0
7
+ Author: Bjørn Johansen
8
+ Author URI: http://twitter.com/bjornjohansen
9
+ Text Domain: bj-lazy-load
10
+ License: GPL2
11
+
12
+ Copyright 2011–2015 Bjørn Johansen (email : post@bjornjohansen.no)
13
+
14
+ This program is free software; you can redistribute it and/or modify
15
+ it under the terms of the GNU General Public License, version 2, as
16
+ published by the Free Software Foundation.
17
+
18
+ This program is distributed in the hope that it will be useful,
19
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ GNU General Public License for more details.
22
+
23
+ You should have received a copy of the GNU General Public License
24
+ along with this program; if not, write to the Free Software
25
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
+
27
+ */
28
+
29
+ include 'inc/class-bjll-options.php';
30
+ include 'inc/class-bjll-skip-post.php';
31
+ include 'inc/class-bjll.php';
32
+
33
+ function bj_lazy_load() {
34
+ load_plugin_textdomain( 'bj-lazy-load', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
35
+
36
+ $bjll_options = new BJLL_Options();
37
+ $bjll = new BJLL( $bjll_options );
38
+ }
39
+
40
+ add_action( 'plugins_loaded', 'bj_lazy_load' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
img/index.html DELETED
File without changes
img/placeholder.gif DELETED
Binary file
inc/class-bjll-adminpage.php ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+
4
+ class BJLL_AdminPage extends scbAdminPage {
5
+
6
+ function setup() {
7
+ $this->args = array(
8
+ 'menu_title' => 'BJ Lazy Load',
9
+ 'page_title' => __( 'BJ Lazy Load Options', 'bj-lazy-load' ),
10
+ );
11
+ }
12
+
13
+ function page_content() {
14
+
15
+
16
+ $optionfields = array(
17
+ array(
18
+ 'title' => __( 'Apply to content', 'bj-lazy-load' ),
19
+ 'type' => 'radio',
20
+ 'name' => 'filter_content',
21
+ 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
22
+ ),
23
+ array(
24
+ 'title' => __( 'Apply to text widgets', 'bj-lazy-load' ),
25
+ 'type' => 'radio',
26
+ 'name' => 'filter_widget_text',
27
+ 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
28
+ ),
29
+ array(
30
+ 'title' => __( 'Apply to post thumbnails', 'bj-lazy-load' ),
31
+ 'type' => 'radio',
32
+ 'name' => 'filter_post_thumbnails',
33
+ 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
34
+ ),
35
+ array(
36
+ 'title' => __( 'Apply to gravatars', 'bj-lazy-load' ),
37
+ 'type' => 'radio',
38
+ 'name' => 'filter_gravatars',
39
+ 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
40
+ ),
41
+ array(
42
+ 'title' => __( 'Lazy load images', 'bj-lazy-load' ),
43
+ 'type' => 'radio',
44
+ 'name' => 'lazy_load_images',
45
+ 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
46
+ ),
47
+ array(
48
+ 'title' => __( 'Lazy load iframes', 'bj-lazy-load' ),
49
+ 'type' => 'radio',
50
+ 'name' => 'lazy_load_iframes',
51
+ 'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
52
+ ),
53
+ array(
54
+ 'title' => __( 'Placeholder Image URL', 'bj-lazy-load' ),
55
+ 'type' => 'text',
56
+ 'name' => 'placeholder_url',
57
+ 'desc' => sprintf( '<p class="description">%s</p>', __( 'Leave blank for default', 'bj-lazy-load' ) ),
58
+ ),
59
+ array(
60
+ 'title' => __( 'Skip images with classes', 'bj-lazy-load' ),
61
+ 'type' => 'text',
62
+ 'name' => 'skip_classes',
63
+ 'desc' => sprintf( '<p class="description">%s</p>', __( 'Comma separated. Example: "no-lazy, lazy-ignore, image-235"', 'bj-lazy-load' ) ),
64
+ ),
65
+ array(
66
+ 'title' => __( 'Threshold', 'bj-lazy-load' ),
67
+ 'type' => 'text',
68
+ 'name' => 'threshold',
69
+ 'desc' => sprintf( '<p class="description">%s</p>', __( 'How close to the viewport the element should be when we load it. In pixels. Example: 200', 'bj-lazy-load' ) ),
70
+ )
71
+ );
72
+
73
+ echo $this->form_table( $optionfields );
74
+
75
+ }
76
+
77
+ }
inc/class-bjll-options.php ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ include 'scb/load.php';
4
+
5
+ class BJLL_Options {
6
+
7
+ protected $_options;
8
+
9
+ function __construct() {
10
+ scb_init( array( $this, 'options_init' ) );
11
+ }
12
+
13
+ public function options_init() {
14
+
15
+ $this->_options = new scbOptions( 'bj_lazy_load_options', __FILE__, array(
16
+ 'filter_content' => 'yes',
17
+ 'filter_widget_text' => 'yes',
18
+ 'filter_post_thumbnails' => 'yes',
19
+ 'filter_gravatars' => 'yes',
20
+ 'lazy_load_images' => 'yes',
21
+ 'lazy_load_iframes' => 'yes',
22
+ 'placeholder_url' => '',
23
+ 'skip_classes' => '',
24
+ 'threshold' => '200',
25
+ ) );
26
+
27
+ if ( is_admin() ) {
28
+ include 'class-bjll-adminpage.php';
29
+ new BJLL_AdminPage( __FILE__, $this->_options );
30
+ }
31
+ }
32
+
33
+ public function get( $option_key ) {
34
+ return $this->_options->get( $option_key );
35
+ }
36
+
37
+ }
inc/class-bjll.php ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ License: GPL2
4
+
5
+ Copyright 2011–2015 Bjørn Johansen (email : post@bjornjohansen.no)
6
+
7
+ This program is free software; you can redistribute it and/or modify
8
+ it under the terms of the GNU General Public License, version 2, as
9
+ published by the Free Software Foundation.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program; if not, write to the Free Software
18
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+
20
+ */
21
+
22
+ /**
23
+ * The class that handles rewriting of content so we can lazy load it
24
+ */
25
+ class BJLL {
26
+
27
+ protected static $_options;
28
+
29
+
30
+ function __construct( $options = null ) {
31
+
32
+ if ( is_a( $options, 'BJLL_Options' ) ) {
33
+ self::$_options = $options;
34
+ }
35
+
36
+ add_action( 'wp', array( $this, 'init' ), 99 ); // run this as late as possible
37
+
38
+ }
39
+
40
+ /**
41
+ * Initialize the setup
42
+ */
43
+ public function init() {
44
+
45
+ self::_bjll_compat();
46
+ do_action( 'bjll/compat' );
47
+
48
+ /**
49
+ * Filter to let plugins decide whether the plugin should run for this request or not
50
+ *
51
+ * Returning false will effectively short-circuit the plugin
52
+ *
53
+ * @param bool $enabled Whether the plugin should run for this request
54
+ */
55
+ $enabled = apply_filters( 'bjll/enabled', true );
56
+
57
+ if ( $enabled ) {
58
+ add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
59
+ add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
60
+
61
+ $this->_setup_filtering();
62
+ }
63
+ }
64
+
65
+
66
+ /**
67
+ * Load compat script
68
+ */
69
+ protected function _bjll_compat() {
70
+
71
+ $dirname = trailingslashit( __DIR__ ) . 'compat';
72
+ $d = dir( $dirname );
73
+ while ( $entry = $d->read() ) {
74
+ if ( '.' != $entry[0] && '.php' == substr( $entry, -4) ) {
75
+ include trailingslashit( $dirname ) . $entry;
76
+ }
77
+ }
78
+
79
+ }
80
+
81
+ /**
82
+ * Enqueue styles
83
+ */
84
+ public function enqueue_styles() {
85
+
86
+ }
87
+
88
+ /**
89
+ * Enqueue scripts
90
+ */
91
+ public function enqueue_scripts() {
92
+ //$mtime = filemtime( dirname( __DIR__ ) . '/js/bj-lazy-load.js' );
93
+ //wp_enqueue_script( 'BJLL', plugins_url( 'js/bj-lazy-load.js', __DIR__ ), null, $mtime, true );
94
+ //$mtime = filemtime( dirname( __DIR__ ) . '/js/bj-lazy-load.v1.min.js' );
95
+ $mtime = 1;
96
+ wp_enqueue_script( 'BJLL', plugins_url( 'js/bj-lazy-load.min.js', __DIR__ ), null, $mtime, true );
97
+
98
+ $bjll_options = array();
99
+ $threshold = intval( self::_get_option('threshold') );
100
+ if ( 200 != $threshold ) {
101
+ $bjll_options['threshold'] = $threshold;
102
+ }
103
+ if ( count( $bjll_options ) ) {
104
+ wp_localize_script( 'BJLL', 'BJLL_options', $bjll_options );
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Set up filtering for certain content
110
+ */
111
+ protected function _setup_filtering() {
112
+
113
+ if ( ! is_admin() ) {
114
+
115
+ if ( 'yes' == self::_get_option('lazy_load_images') ) {
116
+ add_filter( 'bjll/filter', array( __CLASS__, 'filter_images' ) );
117
+ }
118
+
119
+ if ( 'yes' == self::_get_option('lazy_load_iframes') ) {
120
+ add_filter( 'bjll/filter', array( __CLASS__, 'filter_iframes' ) );
121
+ }
122
+
123
+ if ( 'yes' == self::_get_option( 'filter_content' ) ) {
124
+ add_filter( 'the_content', array( __CLASS__, 'filter' ), 200 );
125
+ }
126
+
127
+ if ( 'yes' == self::_get_option( 'filter_widget_text' ) ) {
128
+ add_filter( 'widget_text', array( __CLASS__, 'filter' ), 200 );
129
+ }
130
+
131
+ if ( 'yes' == self::_get_option( 'filter_post_thumbnails' ) ) {
132
+ add_filter( 'post_thumbnail_html', array( __CLASS__, 'filter' ), 200 );
133
+ }
134
+
135
+ if ( 'yes' == self::_get_option( 'filter_gravatars' ) ) {
136
+ add_filter( 'get_avatar', array( __CLASS__, 'filter' ), 200 );
137
+ }
138
+
139
+ add_filter( 'bj_lazy_load_html', array( __CLASS__, 'filter' ) );
140
+ }
141
+
142
+ }
143
+
144
+ /**
145
+ * Filter HTML content. Replace supported content with placeholders.
146
+ *
147
+ * @param string $content The HTML string to filter
148
+ * @return string The filtered HTML string
149
+ */
150
+ public static function filter( $content ) {
151
+
152
+ // Last chance to bail out before running the filter
153
+ $run_filter = apply_filters( 'bj_lazy_load_run_filter', true );
154
+ if ( ! $run_filter ) {
155
+ return $content;
156
+ }
157
+
158
+ /**
159
+ * Filter the content
160
+ *
161
+ * @param string $content The HTML string to filter
162
+ */
163
+ $content = apply_filters( 'bjll/filter', $content );
164
+
165
+ return $content;
166
+ }
167
+
168
+
169
+ /**
170
+ * Replace images with placeholders in the content
171
+ *
172
+ * @param string $content The HTML to do the filtering on
173
+ * @return string The HTML with the images replaced
174
+ */
175
+ public static function filter_images( $content ) {
176
+
177
+ $placeholder_url = self::_get_option( 'placeholder_url' );
178
+ $placeholder_url = apply_filters( 'bjll/placeholder_url', $placeholder_url, 'image' );
179
+ if ( ! strlen( $placeholder_url ) ) {
180
+ $placeholder_url = 'data:image/gif;base64,R0lGODdhAQABAPAAAP///wAAACwAAAAAAQABAEACAkQBADs=';
181
+ }
182
+
183
+ $match_content = self::_get_content_haystack( $content );
184
+
185
+ $matches = array();
186
+ preg_match_all( '/<img[\s\r\n]+.*?>/is', $match_content, $matches );
187
+
188
+ $search = array();
189
+ $replace = array();
190
+
191
+ foreach ( $matches[0] as $imgHTML ) {
192
+
193
+ // don't to the replacement if the image is a data-uri
194
+ if ( ! preg_match( "/src=['\"]data:image/is", $imgHTML ) ) {
195
+
196
+ // replace the src and add the data-src attribute
197
+ $replaceHTML = preg_replace( '/<img(.*?)src=/is', '<img$1src="' . esc_url( $placeholder_url ) . '" data-lazy-type="image" data-lazy-src=', $imgHTML );
198
+
199
+ // also replace the srcset (responsive images)
200
+ $replaceHTML = str_replace( 'srcset', 'data-lazy-srcset', $replaceHTML );
201
+
202
+ // add the lazy class to the img element
203
+ if ( preg_match( '/class=["\']/i', $replaceHTML ) ) {
204
+ $replaceHTML = preg_replace( '/class=(["\'])(.*?)["\']/is', 'class=$1lazy lazy-hidden $2$1', $replaceHTML );
205
+ } else {
206
+ $replaceHTML = preg_replace( '/<img/is', '<img class="lazy lazy-hidden"', $replaceHTML );
207
+ }
208
+
209
+ $replaceHTML .= '<noscript>' . $imgHTML . '</noscript>';
210
+
211
+ array_push( $search, $imgHTML );
212
+ array_push( $replace, $replaceHTML );
213
+ }
214
+ }
215
+
216
+ $content = str_replace( $search, $replace, $content );
217
+
218
+ return $content;
219
+
220
+ }
221
+
222
+ /**
223
+ * Replace iframes with placeholders in the content
224
+ *
225
+ * @param string $content The HTML to do the filtering on
226
+ * @return string The HTML with the iframes replaced
227
+ */
228
+ public static function filter_iframes( $content ) {
229
+
230
+ $placeholder_url = self::_get_option( 'placeholder_url' );
231
+ $placeholder_url = apply_filters( 'bjll/placeholder_url', $placeholder_url, 'image' );
232
+ if ( ! strlen( $placeholder_url ) ) {
233
+ $placeholder_url = 'data:image/gif;base64,R0lGODdhAQABAPAAAP///wAAACwAAAAAAQABAEACAkQBADs=';
234
+ }
235
+
236
+ $match_content = self::_get_content_haystack( $content );
237
+
238
+ $matches = array();
239
+ preg_match_all( '|<iframe\s+.*?</iframe>|siU', $match_content, $matches );
240
+
241
+ $search = array();
242
+ $replace = array();
243
+
244
+ foreach ( $matches[0] as $iframeHTML ) {
245
+
246
+ // Don't mess with the Gravity Forms ajax iframe
247
+ if ( strpos( $iframeHTML, 'gform_ajax_frame' ) ) {
248
+ continue;
249
+ }
250
+
251
+ $replaceHTML = '<img src="' . esc_url( $placeholder_url ) . '" class="lazy lazy-hidden" data-lazy-type="iframe" data-lazy-src="' . esc_attr( $iframeHTML ) . '" alt="">';
252
+
253
+ $replaceHTML .= '<noscript>' . $iframeHTML . '</noscript>';
254
+
255
+ array_push( $search, $iframeHTML );
256
+ array_push( $replace, $replaceHTML );
257
+ }
258
+
259
+ $content = str_replace( $search, $replace, $content );
260
+
261
+ return $content;
262
+
263
+ }
264
+
265
+ /**
266
+ * Remove elements we don’t want to filter from the HTML string
267
+ *
268
+ * We’re reducing the haystack by removing the hay we know we don’t want to look for needles in
269
+ *
270
+ * @param string $content The HTML string
271
+ * @return string The HTML string without the unwanted elements
272
+ */
273
+ protected static function _get_content_haystack( $content ) {
274
+ $content = self::remove_noscript( $content );
275
+ $content = self::remove_skip_classes_elements( $content );
276
+
277
+ return $content;
278
+ }
279
+
280
+ /**
281
+ * Remove <noscript> elements from HTML string
282
+ *
283
+ * @author sigginet
284
+ * @param string $content The HTML string
285
+ * @return string The HTML string without <noscript> elements
286
+ */
287
+ public static function remove_noscript( $content ) {
288
+ return preg_replace( '/<noscript.*?(\/noscript>)/i', '', $content );
289
+ }
290
+
291
+ /**
292
+ * Remove HTML elements with certain classnames (or IDs) from HTML string
293
+ *
294
+ * @param string $content The HTML string
295
+ * @return string The HTML string without the unwanted elements
296
+ */
297
+ public static function remove_skip_classes_elements( $content ) {
298
+
299
+ $skip_classes = self::_get_skip_classes( 'html' );
300
+
301
+ /*
302
+ http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
303
+ We can’t do this, but we still do it.
304
+ */
305
+ $skip_classes_quoted = array_map( 'preg_quote', $skip_classes );
306
+ $skip_classes_ORed = implode( '|', $skip_classes_quoted );
307
+ $regex = '/<(\w+)\s[^>]*(?:class|id)\s*=\s*([\'"]).*?[^\-]\b(?:' . $skip_classes_ORed . ')\b[^\-].*?\2[^>]*>.*<\/\\1>/isU';
308
+ return preg_replace( $regex, '', $content );
309
+ }
310
+
311
+ /**
312
+ * Get an option value
313
+ *
314
+ * @param string $option_key The name of the option
315
+ * @return string The option value
316
+ */
317
+ protected static function _get_option( $option_key ) {
318
+ return self::$_options->get( $option_key );
319
+ }
320
+
321
+ /**
322
+ * Get the skip classes
323
+ *
324
+ * @param string $content_type The content type (image/iframe etc)
325
+ * @return array An array of strings with the class names
326
+ */
327
+ protected static function _get_skip_classes( $content_type ) {
328
+
329
+ $skip_classes = array();
330
+
331
+ $skip_classes_str = self::_get_option( 'skip_classes' );
332
+
333
+ if ( strlen( trim( $skip_classes_str ) ) ) {
334
+ $skip_classes = array_map( 'trim', explode( ',', $skip_classes_str ) );
335
+ }
336
+
337
+ if ( ! in_array( 'lazy', $skip_classes ) ) {
338
+ $skip_classes[] = 'lazy';
339
+ }
340
+
341
+ /**
342
+ * Filter the class names to skip
343
+ *
344
+ * @param array $skip_classes The current classes to skip
345
+ * @param string $content_type The current content type
346
+ */
347
+ $skip_classes = apply_filters( 'bjll/skip_classes', $skip_classes, $content_type );
348
+
349
+ return $skip_classes;
350
+ }
351
+
352
+ }
353
+
inc/compat/mobilepress.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ function bjll_compat_mobilepress() {
4
+ if ( function_exists( 'mopr_get_option' ) && WP_CONTENT_DIR . mopr_get_option( 'mobile_theme_root', 1 ) == get_theme_root() ) {
5
+ add_filter( 'bjll/enabled', '__return_false' );
6
+ }
7
+ }
8
+
9
+ add_action( 'bjll/compat', 'bjll_compat_mobilepress' );
inc/compat/opera-mini.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ function bjll_compat_operamini() {
4
+ if ( false !== strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' ) ) {
5
+ add_filter( 'bjll/enabled', '__return_false' );
6
+ }
7
+ }
8
+
9
+ add_action( 'bjll/compat', 'bjll_compat_operamini' );
inc/compat/wp-print.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ function bjll_compat_wpprint() {
4
+ if ( 1 == intval( get_query_var( 'print' ) ) || 1 == intval( get_query_var( 'printpage' ) ) ) {
5
+ add_filter( 'bjll/enabled', '__return_false' );
6
+ }
7
+ }
8
+
9
+ add_action( 'bjll/compat', 'bjll_compat_wpprint' );
inc/compat/wptouch.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ function bjll_compat_wptouch() {
4
+ if ( function_exists( 'bnc_wptouch_is_mobile' ) || defined( 'WPTOUCH_VERSION' ) ) {
5
+ add_filter( 'bjll/enabled', '__return_false' );
6
+ }
7
+ }
8
+
9
+ add_action( 'bjll/compat', 'bjll_compat_wptouch' );
inc/index.html DELETED
File without changes
inc/lang.php DELETED
@@ -1,6 +0,0 @@
1
- <?php
2
-
3
- function bjll_load_plugin_textdomain() {
4
- load_plugin_textdomain( 'bj-lazy-load', false, 'bj-lazy-load/lang/' );
5
- }
6
- add_action( 'plugins_loaded', 'bjll_load_plugin_textdomain' );
 
 
 
 
 
 
{scb → inc/scb}/AdminPage.php RENAMED
File without changes
{scb → inc/scb}/BoxesPage.php RENAMED
File without changes
{scb → inc/scb}/Cron.php RENAMED
File without changes
{scb → inc/scb}/Forms.php RENAMED
File without changes
{scb → inc/scb}/Hooks.php RENAMED
File without changes
{scb → inc/scb}/Options.php RENAMED
File without changes
{scb → inc/scb}/PostMetabox.php RENAMED
File without changes
{scb → inc/scb}/Table.php RENAMED
File without changes
{scb → inc/scb}/Util.php RENAMED
File without changes
{scb → inc/scb}/Widget.php RENAMED
File without changes
{cache → inc/scb}/index.html RENAMED
File without changes
{scb → inc/scb}/load.php RENAMED
File without changes
inc/timthumb.php DELETED
@@ -1,1251 +0,0 @@
1
- <?php
2
- /**
3
- * TimThumb by Ben Gillbanks and Mark Maunder
4
- * Based on work done by Tim McDaniels and Darren Hoyt
5
- * http://code.google.com/p/timthumb/
6
- *
7
- * GNU General Public License, version 2
8
- * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
9
- *
10
- * Examples and documentation available on the project homepage
11
- * http://www.binarymoon.co.uk/projects/timthumb/
12
- *
13
- * $Rev$
14
- */
15
-
16
- /*
17
- * --- TimThumb CONFIGURATION ---
18
- * To edit the configs it is best to create a file called timthumb-config.php
19
- * and define variables you want to customize in there. It will automatically be
20
- * loaded by timthumb. This will save you having to re-edit these variables
21
- * everytime you download a new version
22
- */
23
- define ('VERSION', '2.8.11'); // Version of this script
24
- //Load a config file if it exists. Otherwise, use the values below
25
- if( file_exists(dirname(__FILE__) . '/timthumb-config.php')) require_once('timthumb-config.php');
26
- if(! defined('DEBUG_ON') ) define ('DEBUG_ON', false); // Enable debug logging to web server error log (STDERR)
27
- if(! defined('DEBUG_LEVEL') ) define ('DEBUG_LEVEL', 1); // Debug level 1 is less noisy and 3 is the most noisy
28
- if(! defined('MEMORY_LIMIT') ) define ('MEMORY_LIMIT', '30M'); // Set PHP memory limit
29
- if(! defined('BLOCK_EXTERNAL_LEECHERS') ) define ('BLOCK_EXTERNAL_LEECHERS', false); // If the image or webshot is being loaded on an external site, display a red "No Hotlinking" gif.
30
-
31
- //Image fetching and caching
32
- if(! defined('ALLOW_EXTERNAL') ) define ('ALLOW_EXTERNAL', TRUE); // Allow image fetching from external websites. Will check against ALLOWED_SITES if ALLOW_ALL_EXTERNAL_SITES is false
33
- if(! defined('ALLOW_ALL_EXTERNAL_SITES') ) define ('ALLOW_ALL_EXTERNAL_SITES', false); // Less secure.
34
- if(! defined('FILE_CACHE_ENABLED') ) define ('FILE_CACHE_ENABLED', TRUE); // Should we store resized/modified images on disk to speed things up?
35
- if(! defined('FILE_CACHE_TIME_BETWEEN_CLEANS')) define ('FILE_CACHE_TIME_BETWEEN_CLEANS', 86400); // How often the cache is cleaned
36
-
37
- if(! defined('FILE_CACHE_MAX_FILE_AGE') ) define ('FILE_CACHE_MAX_FILE_AGE', 86400); // How old does a file have to be to be deleted from the cache
38
- if(! defined('FILE_CACHE_SUFFIX') ) define ('FILE_CACHE_SUFFIX', '.timthumb.txt'); // What to put at the end of all files in the cache directory so we can identify them
39
- if(! defined('FILE_CACHE_PREFIX') ) define ('FILE_CACHE_PREFIX', 'timthumb'); // What to put at the beg of all files in the cache directory so we can identify them
40
- if(! defined('FILE_CACHE_DIRECTORY') ) define ('FILE_CACHE_DIRECTORY', './cache'); // Directory where images are cached. Left blank it will use the system temporary directory (which is better for security)
41
- if(! defined('MAX_FILE_SIZE') ) define ('MAX_FILE_SIZE', 10485760); // 10 Megs is 10485760. This is the max internal or external file size that we'll process.
42
- if(! defined('CURL_TIMEOUT') ) define ('CURL_TIMEOUT', 20); // Timeout duration for Curl. This only applies if you have Curl installed and aren't using PHP's default URL fetching mechanism.
43
- if(! defined('WAIT_BETWEEN_FETCH_ERRORS') ) define ('WAIT_BETWEEN_FETCH_ERRORS', 3600); // Time to wait between errors fetching remote file
44
-
45
- //Browser caching
46
- if(! defined('BROWSER_CACHE_MAX_AGE') ) define ('BROWSER_CACHE_MAX_AGE', 864000); // Time to cache in the browser
47
- if(! defined('BROWSER_CACHE_DISABLE') ) define ('BROWSER_CACHE_DISABLE', false); // Use for testing if you want to disable all browser caching
48
-
49
- //Image size and defaults
50
- if(! defined('MAX_WIDTH') ) define ('MAX_WIDTH', 1500); // Maximum image width
51
- if(! defined('MAX_HEIGHT') ) define ('MAX_HEIGHT', 1500); // Maximum image height
52
- if(! defined('NOT_FOUND_IMAGE') ) define ('NOT_FOUND_IMAGE', ''); // Image to serve if any 404 occurs
53
- if(! defined('ERROR_IMAGE') ) define ('ERROR_IMAGE', ''); // Image to serve if an error occurs instead of showing error message
54
- if(! defined('PNG_IS_TRANSPARENT') ) define ('PNG_IS_TRANSPARENT', FALSE); // Define if a png image should have a transparent background color. Use False value if you want to display a custom coloured canvas_colour
55
- if(! defined('DEFAULT_Q') ) define ('DEFAULT_Q', 90); // Default image quality. Allows overrid in timthumb-config.php
56
- if(! defined('DEFAULT_ZC') ) define ('DEFAULT_ZC', 1); // Default zoom/crop setting. Allows overrid in timthumb-config.php
57
- if(! defined('DEFAULT_F') ) define ('DEFAULT_F', ''); // Default image filters. Allows overrid in timthumb-config.php
58
- if(! defined('DEFAULT_S') ) define ('DEFAULT_S', 0); // Default sharpen value. Allows overrid in timthumb-config.php
59
- if(! defined('DEFAULT_CC') ) define ('DEFAULT_CC', 'ffffff'); // Default canvas colour. Allows overrid in timthumb-config.php
60
-
61
-
62
- //Image compression is enabled if either of these point to valid paths
63
-
64
- //These are now disabled by default because the file sizes of PNGs (and GIFs) are much smaller than we used to generate.
65
- //They only work for PNGs. GIFs and JPEGs are not affected.
66
- if(! defined('OPTIPNG_ENABLED') ) define ('OPTIPNG_ENABLED', false);
67
- if(! defined('OPTIPNG_PATH') ) define ('OPTIPNG_PATH', '/usr/bin/optipng'); //This will run first because it gives better compression than pngcrush.
68
- if(! defined('PNGCRUSH_ENABLED') ) define ('PNGCRUSH_ENABLED', false);
69
- if(! defined('PNGCRUSH_PATH') ) define ('PNGCRUSH_PATH', '/usr/bin/pngcrush'); //This will only run if OPTIPNG_PATH is not set or is not valid
70
-
71
- /*
72
- -------====Website Screenshots configuration - BETA====-------
73
-
74
- If you just want image thumbnails and don't want website screenshots, you can safely leave this as is.
75
-
76
- If you would like to get website screenshots set up, you will need root access to your own server.
77
-
78
- Enable ALLOW_ALL_EXTERNAL_SITES so you can fetch any external web page. This is more secure now that we're using a non-web folder for cache.
79
- Enable BLOCK_EXTERNAL_LEECHERS so that your site doesn't generate thumbnails for the whole Internet.
80
-
81
- Instructions to get website screenshots enabled on Ubuntu Linux:
82
-
83
- 1. Install Xvfb with the following command: sudo apt-get install subversion libqt4-webkit libqt4-dev g++ xvfb
84
- 2. Go to a directory where you can download some code
85
- 3. Check-out the latest version of CutyCapt with the following command: svn co https://cutycapt.svn.sourceforge.net/svnroot/cutycapt
86
- 4. Compile CutyCapt by doing: cd cutycapt/CutyCapt
87
- 5. qmake
88
- 6. make
89
- 7. cp CutyCapt /usr/local/bin/
90
- 8. Test it by running: xvfb-run --server-args="-screen 0, 1024x768x24" CutyCapt --url="http://markmaunder.com/" --out=test.png
91
- 9. If you get a file called test.png with something in it, it probably worked. Now test the script by accessing it as follows:
92
- 10. http://yoursite.com/path/to/timthumb.php?src=http://markmaunder.com/&webshot=1
93
-
94
- Notes on performance:
95
- The first time a webshot loads, it will take a few seconds.
96
- From then on it uses the regular timthumb caching mechanism with the configurable options above
97
- and loading will be very fast.
98
-
99
- --ADVANCED USERS ONLY--
100
- If you'd like a slight speedup (about 25%) and you know Linux, you can run the following command which will keep Xvfb running in the background.
101
- nohup Xvfb :100 -ac -nolisten tcp -screen 0, 1024x768x24 > /dev/null 2>&1 &
102
- Then set WEBSHOT_XVFB_RUNNING = true below. This will save your server having to fire off a new Xvfb server and shut it down every time a new shot is generated.
103
- You will need to take responsibility for keeping Xvfb running in case it crashes. (It seems pretty stable)
104
- You will also need to take responsibility for server security if you're running Xvfb as root.
105
-
106
-
107
- */
108
- if(! defined('WEBSHOT_ENABLED') ) define ('WEBSHOT_ENABLED', false); //Beta feature. Adding webshot=1 to your query string will cause the script to return a browser screenshot rather than try to fetch an image.
109
- if(! defined('WEBSHOT_CUTYCAPT') ) define ('WEBSHOT_CUTYCAPT', '/usr/local/bin/CutyCapt'); //The path to CutyCapt.
110
- if(! defined('WEBSHOT_XVFB') ) define ('WEBSHOT_XVFB', '/usr/bin/xvfb-run'); //The path to the Xvfb server
111
- if(! defined('WEBSHOT_SCREEN_X') ) define ('WEBSHOT_SCREEN_X', '1024'); //1024 works ok
112
- if(! defined('WEBSHOT_SCREEN_Y') ) define ('WEBSHOT_SCREEN_Y', '768'); //768 works ok
113
- if(! defined('WEBSHOT_COLOR_DEPTH') ) define ('WEBSHOT_COLOR_DEPTH', '24'); //I haven't tested anything besides 24
114
- if(! defined('WEBSHOT_IMAGE_FORMAT') ) define ('WEBSHOT_IMAGE_FORMAT', 'png'); //png is about 2.5 times the size of jpg but is a LOT better quality
115
- if(! defined('WEBSHOT_TIMEOUT') ) define ('WEBSHOT_TIMEOUT', '20'); //Seconds to wait for a webshot
116
- if(! defined('WEBSHOT_USER_AGENT') ) define ('WEBSHOT_USER_AGENT', "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18"); //I hate to do this, but a non-browser robot user agent might not show what humans see. So we pretend to be Firefox
117
- if(! defined('WEBSHOT_JAVASCRIPT_ON') ) define ('WEBSHOT_JAVASCRIPT_ON', true); //Setting to false might give you a slight speedup and block ads. But it could cause other issues.
118
- if(! defined('WEBSHOT_JAVA_ON') ) define ('WEBSHOT_JAVA_ON', false); //Have only tested this as fase
119
- if(! defined('WEBSHOT_PLUGINS_ON') ) define ('WEBSHOT_PLUGINS_ON', true); //Enable flash and other plugins
120
- if(! defined('WEBSHOT_PROXY') ) define ('WEBSHOT_PROXY', ''); //In case you're behind a proxy server.
121
- if(! defined('WEBSHOT_XVFB_RUNNING') ) define ('WEBSHOT_XVFB_RUNNING', false); //ADVANCED: Enable this if you've got Xvfb running in the background.
122
-
123
-
124
- // 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.
125
- if(! isset($ALLOWED_SITES)){
126
- $ALLOWED_SITES = array (
127
- 'flickr.com',
128
- 'staticflickr.com',
129
- 'picasa.com',
130
- 'img.youtube.com',
131
- 'upload.wikimedia.org',
132
- 'photobucket.com',
133
- 'imgur.com',
134
- 'imageshack.us',
135
- 'tinypic.com',
136
- );
137
- }
138
- // -------------------------------------------------------------
139
- // -------------- STOP EDITING CONFIGURATION HERE --------------
140
- // -------------------------------------------------------------
141
-
142
- timthumb::start();
143
-
144
- class timthumb {
145
- protected $src = "";
146
- protected $is404 = false;
147
- protected $docRoot = "";
148
- protected $lastURLError = false;
149
- protected $localImage = "";
150
- protected $localImageMTime = 0;
151
- protected $url = false;
152
- protected $myHost = "";
153
- protected $isURL = false;
154
- protected $cachefile = '';
155
- protected $errors = array();
156
- protected $toDeletes = array();
157
- protected $cacheDirectory = '';
158
- protected $startTime = 0;
159
- protected $lastBenchTime = 0;
160
- protected $cropTop = false;
161
- protected $salt = "";
162
- protected $fileCacheVersion = 1; //Generally if timthumb.php is modifed (upgraded) then the salt changes and all cache files are recreated. This is a backup mechanism to force regen.
163
- protected $filePrependSecurityBlock = "<?php die('Execution denied!'); //"; //Designed to have three letter mime type, space, question mark and greater than symbol appended. 6 bytes total.
164
- protected static $curlDataWritten = 0;
165
- protected static $curlFH = false;
166
- public static function start(){
167
- $tim = new timthumb();
168
- $tim->handleErrors();
169
- $tim->securityChecks();
170
- if($tim->tryBrowserCache()){
171
- exit(0);
172
- }
173
- $tim->handleErrors();
174
- if(FILE_CACHE_ENABLED && $tim->tryServerCache()){
175
- exit(0);
176
- }
177
- $tim->handleErrors();
178
- $tim->run();
179
- $tim->handleErrors();
180
- exit(0);
181
- }
182
- public function __construct(){
183
- global $ALLOWED_SITES;
184
- $this->startTime = microtime(true);
185
- date_default_timezone_set('UTC');
186
- $this->debug(1, "Starting new request from " . $this->getIP() . " to " . $_SERVER['REQUEST_URI']);
187
- $this->calcDocRoot();
188
- //On windows systems I'm assuming fileinode returns an empty string or a number that doesn't change. Check this.
189
- $this->salt = @filemtime(__FILE__) . '-' . @fileinode(__FILE__);
190
- $this->debug(3, "Salt is: " . $this->salt);
191
- if(FILE_CACHE_DIRECTORY){
192
- if(! is_dir(FILE_CACHE_DIRECTORY)){
193
- @mkdir(FILE_CACHE_DIRECTORY);
194
- if(! is_dir(FILE_CACHE_DIRECTORY)){
195
- $this->error("Could not create the file cache directory.");
196
- return false;
197
- }
198
- }
199
- $this->cacheDirectory = FILE_CACHE_DIRECTORY;
200
- if (!touch($this->cacheDirectory . '/index.html')) {
201
- $this->error("Could not create the index.html file - to fix this create an empty file named index.html file in the cache directory.");
202
- }
203
- } else {
204
- $this->cacheDirectory = sys_get_temp_dir();
205
- }
206
- //Clean the cache before we do anything because we don't want the first visitor after FILE_CACHE_TIME_BETWEEN_CLEANS expires to get a stale image.
207
- $this->cleanCache();
208
-
209
- $this->myHost = preg_replace('/^www\./i', '', $_SERVER['HTTP_HOST']);
210
- $this->src = $this->param('src');
211
- $this->url = parse_url($this->src);
212
- $this->src = preg_replace('/https?:\/\/(?:www\.)?' . $this->myHost . '/i', '', $this->src);
213
-
214
- if(strlen($this->src) <= 3){
215
- $this->error("No image specified");
216
- return false;
217
- }
218
- if(BLOCK_EXTERNAL_LEECHERS && array_key_exists('HTTP_REFERER', $_SERVER) && (! preg_match('/^https?:\/\/(?:www\.)?' . $this->myHost . '(?:$|\/)/i', $_SERVER['HTTP_REFERER']))){
219
- // base64 encoded red image that says 'no hotlinkers'
220
- // nothing to worry about! :)
221
- $imgData = base64_decode("R0lGODlhUAAMAIAAAP8AAP///yH5BAAHAP8ALAAAAABQAAwAAAJpjI+py+0Po5y0OgAMjjv01YUZ\nOGplhWXfNa6JCLnWkXplrcBmW+spbwvaVr/cDyg7IoFC2KbYVC2NQ5MQ4ZNao9Ynzjl9ScNYpneb\nDULB3RP6JuPuaGfuuV4fumf8PuvqFyhYtjdoeFgAADs=");
222
- header('Content-Type: image/gif');
223
- header('Content-Length: ' . sizeof($imgData));
224
- header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
225
- header("Pragma: no-cache");
226
- header('Expires: ' . gmdate ('D, d M Y H:i:s', time()));
227
- echo $imgData;
228
- return false;
229
- exit(0);
230
- }
231
- if(preg_match('/^https?:\/\/[^\/]+/i', $this->src)){
232
- $this->debug(2, "Is a request for an external URL: " . $this->src);
233
- $this->isURL = true;
234
- } else {
235
- $this->debug(2, "Is a request for an internal file: " . $this->src);
236
- }
237
- if($this->isURL && (! ALLOW_EXTERNAL)){
238
- $this->error("You are not allowed to fetch images from an external website.");
239
- return false;
240
- }
241
- if($this->isURL){
242
- if(ALLOW_ALL_EXTERNAL_SITES){
243
- $this->debug(2, "Fetching from all external sites is enabled.");
244
- } else {
245
- $this->debug(2, "Fetching only from selected external sites is enabled.");
246
- $allowed = false;
247
- foreach($ALLOWED_SITES as $site){
248
- if ((strtolower(substr($this->url['host'],-strlen($site)-1)) === strtolower(".$site")) || (strtolower($this->url['host'])===strtolower($site))) {
249
- $this->debug(3, "URL hostname {$this->url['host']} matches $site so allowing.");
250
- $allowed = true;
251
- }
252
- }
253
- if(! $allowed){
254
- return $this->error("You may not fetch images from that site. To enable this site in timthumb, you can either add it to \$ALLOWED_SITES and set ALLOW_EXTERNAL=true. Or you can set ALLOW_ALL_EXTERNAL_SITES=true, depending on your security needs.");
255
- }
256
- }
257
- }
258
-
259
- $cachePrefix = ($this->isURL ? '_ext_' : '_int_');
260
- if($this->isURL){
261
- $arr = explode('&', $_SERVER ['QUERY_STRING']);
262
- asort($arr);
263
- $this->cachefile = $this->cacheDirectory . '/' . FILE_CACHE_PREFIX . $cachePrefix . md5($this->salt . implode('', $arr) . $this->fileCacheVersion) . FILE_CACHE_SUFFIX;
264
- } else {
265
- $this->localImage = $this->getLocalImagePath($this->src);
266
- if(! $this->localImage){
267
- $this->debug(1, "Could not find the local image: {$this->localImage}");
268
- $this->error("Could not find the internal image you specified.");
269
- $this->set404();
270
- return false;
271
- }
272
- $this->debug(1, "Local image path is {$this->localImage}");
273
- $this->localImageMTime = @filemtime($this->localImage);
274
- //We include the mtime of the local file in case in changes on disk.
275
- $this->cachefile = $this->cacheDirectory . '/' . FILE_CACHE_PREFIX . $cachePrefix . md5($this->salt . $this->localImageMTime . $_SERVER ['QUERY_STRING'] . $this->fileCacheVersion) . FILE_CACHE_SUFFIX;
276
- }
277
- $this->debug(2, "Cache file is: " . $this->cachefile);
278
-
279
- return true;
280
- }
281
- public function __destruct(){
282
- foreach($this->toDeletes as $del){
283
- $this->debug(2, "Deleting temp file $del");
284
- @unlink($del);
285
- }
286
- }
287
- public function run(){
288
- if($this->isURL){
289
- if(! ALLOW_EXTERNAL){
290
- $this->debug(1, "Got a request for an external image but ALLOW_EXTERNAL is disabled so returning error msg.");
291
- $this->error("You are not allowed to fetch images from an external website.");
292
- return false;
293
- }
294
- $this->debug(3, "Got request for external image. Starting serveExternalImage.");
295
- if($this->param('webshot')){
296
- if(WEBSHOT_ENABLED){
297
- $this->debug(3, "webshot param is set, so we're going to take a webshot.");
298
- $this->serveWebshot();
299
- } else {
300
- $this->error("You added the webshot parameter but webshots are disabled on this server. You need to set WEBSHOT_ENABLED == true to enable webshots.");
301
- }
302
- } else {
303
- $this->debug(3, "webshot is NOT set so we're going to try to fetch a regular image.");
304
- $this->serveExternalImage();
305
-
306
- }
307
- } else {
308
- $this->debug(3, "Got request for internal image. Starting serveInternalImage()");
309
- $this->serveInternalImage();
310
- }
311
- return true;
312
- }
313
- protected function handleErrors(){
314
- if($this->haveErrors()){
315
- if(NOT_FOUND_IMAGE && $this->is404()){
316
- if($this->serveImg(NOT_FOUND_IMAGE)){
317
- exit(0);
318
- } else {
319
- $this->error("Additionally, the 404 image that is configured could not be found or there was an error serving it.");
320
- }
321
- }
322
- if(ERROR_IMAGE){
323
- if($this->serveImg(ERROR_IMAGE)){
324
- exit(0);
325
- } else {
326
- $this->error("Additionally, the error image that is configured could not be found or there was an error serving it.");
327
- }
328
- }
329
- $this->serveErrors();
330
- exit(0);
331
- }
332
- return false;
333
- }
334
- protected function tryBrowserCache(){
335
- if(BROWSER_CACHE_DISABLE){ $this->debug(3, "Browser caching is disabled"); return false; }
336
- if(!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ){
337
- $this->debug(3, "Got a conditional get");
338
- $mtime = false;
339
- //We've already checked if the real file exists in the constructor
340
- if(! is_file($this->cachefile)){
341
- //If we don't have something cached, regenerate the cached image.
342
- return false;
343
- }
344
- if($this->localImageMTime){
345
- $mtime = $this->localImageMTime;
346
- $this->debug(3, "Local real file's modification time is $mtime");
347
- } else if(is_file($this->cachefile)){ //If it's not a local request then use the mtime of the cached file to determine the 304
348
- $mtime = @filemtime($this->cachefile);
349
- $this->debug(3, "Cached file's modification time is $mtime");
350
- }
351
- if(! $mtime){ return false; }
352
-
353
- $iftime = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
354
- $this->debug(3, "The conditional get's if-modified-since unixtime is $iftime");
355
- if($iftime < 1){
356
- $this->debug(3, "Got an invalid conditional get modified since time. Returning false.");
357
- return false;
358
- }
359
- if($iftime < $mtime){ //Real file or cache file has been modified since last request, so force refetch.
360
- $this->debug(3, "File has been modified since last fetch.");
361
- return false;
362
- } else { //Otherwise serve a 304
363
- $this->debug(3, "File has not been modified since last get, so serving a 304.");
364
- header ($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
365
- $this->debug(1, "Returning 304 not modified");
366
- return true;
367
- }
368
- }
369
- return false;
370
- }
371
- protected function tryServerCache(){
372
- $this->debug(3, "Trying server cache");
373
- if(file_exists($this->cachefile)){
374
- $this->debug(3, "Cachefile {$this->cachefile} exists");
375
- if($this->isURL){
376
- $this->debug(3, "This is an external request, so checking if the cachefile is empty which means the request failed previously.");
377
- if(filesize($this->cachefile) < 1){
378
- $this->debug(3, "Found an empty cachefile indicating a failed earlier request. Checking how old it is.");
379
- //Fetching error occured previously
380
- if(time() - @filemtime($this->cachefile) > WAIT_BETWEEN_FETCH_ERRORS){
381
- $this->debug(3, "File is older than " . WAIT_BETWEEN_FETCH_ERRORS . " seconds. Deleting and returning false so app can try and load file.");
382
- @unlink($this->cachefile);
383
- return false; //to indicate we didn't serve from cache and app should try and load
384
- } else {
385
- $this->debug(3, "Empty cachefile is still fresh so returning message saying we had an error fetching this image from remote host.");
386
- $this->set404();
387
- $this->error("An error occured fetching image.");
388
- return false;
389
- }
390
- }
391
- } else {
392
- $this->debug(3, "Trying to serve cachefile {$this->cachefile}");
393
- }
394
- if($this->serveCacheFile()){
395
- $this->debug(3, "Succesfully served cachefile {$this->cachefile}");
396
- return true;
397
- } else {
398
- $this->debug(3, "Failed to serve cachefile {$this->cachefile} - Deleting it from cache.");
399
- //Image serving failed. We can't retry at this point, but lets remove it from cache so the next request recreates it
400
- @unlink($this->cachefile);
401
- return true;
402
- }
403
- }
404
- }
405
- protected function error($err){
406
- $this->debug(3, "Adding error message: $err");
407
- $this->errors[] = $err;
408
- return false;
409
-
410
- }
411
- protected function haveErrors(){
412
- if(sizeof($this->errors) > 0){
413
- return true;
414
- }
415
- return false;
416
- }
417
- protected function serveErrors(){
418
- header ($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
419
- $html = '<ul>';
420
- foreach($this->errors as $err){
421
- $html .= '<li>' . htmlentities($err) . '</li>';
422
- }
423
- $html .= '</ul>';
424
- echo '<h1>A TimThumb error has occured</h1>The following error(s) occured:<br />' . $html . '<br />';
425
- echo '<br />Query String : ' . htmlentities ($_SERVER['QUERY_STRING']);
426
- echo '<br />TimThumb version : ' . VERSION . '</pre>';
427
- }
428
- protected function serveInternalImage(){
429
- $this->debug(3, "Local image path is $this->localImage");
430
- if(! $this->localImage){
431
- $this->sanityFail("localImage not set after verifying it earlier in the code.");
432
- return false;
433
- }
434
- $fileSize = filesize($this->localImage);
435
- if($fileSize > MAX_FILE_SIZE){
436
- $this->error("The file you specified is greater than the maximum allowed file size.");
437
- return false;
438
- }
439
- if($fileSize <= 0){
440
- $this->error("The file you specified is <= 0 bytes.");
441
- return false;
442
- }
443
- $this->debug(3, "Calling processImageAndWriteToCache() for local image.");
444
- if($this->processImageAndWriteToCache($this->localImage)){
445
- $this->serveCacheFile();
446
- return true;
447
- } else {
448
- return false;
449
- }
450
- }
451
- protected function cleanCache(){
452
- if (FILE_CACHE_TIME_BETWEEN_CLEANS < 0) {
453
- return;
454
- }
455
- $this->debug(3, "cleanCache() called");
456
- $lastCleanFile = $this->cacheDirectory . '/timthumb_cacheLastCleanTime.touch';
457
-
458
- //If this is a new timthumb installation we need to create the file
459
- if(! is_file($lastCleanFile)){
460
- $this->debug(1, "File tracking last clean doesn't exist. Creating $lastCleanFile");
461
- if (!touch($lastCleanFile)) {
462
- $this->error("Could not create cache clean timestamp file.");
463
- }
464
- return;
465
- }
466
- if(@filemtime($lastCleanFile) < (time() - FILE_CACHE_TIME_BETWEEN_CLEANS) ){ //Cache was last cleaned more than 1 day ago
467
- $this->debug(1, "Cache was last cleaned more than " . FILE_CACHE_TIME_BETWEEN_CLEANS . " seconds ago. Cleaning now.");
468
- // Very slight race condition here, but worst case we'll have 2 or 3 servers cleaning the cache simultaneously once a day.
469
- if (!touch($lastCleanFile)) {
470
- $this->error("Could not create cache clean timestamp file.");
471
- }
472
- $files = glob($this->cacheDirectory . '/*' . FILE_CACHE_SUFFIX);
473
- if ($files) {
474
- $timeAgo = time() - FILE_CACHE_MAX_FILE_AGE;
475
- foreach($files as $file){
476
- if(@filemtime($file) < $timeAgo){
477
- $this->debug(3, "Deleting cache file $file older than max age: " . FILE_CACHE_MAX_FILE_AGE . " seconds");
478
- @unlink($file);
479
- }
480
- }
481
- }
482
- return true;
483
- } else {
484
- $this->debug(3, "Cache was cleaned less than " . FILE_CACHE_TIME_BETWEEN_CLEANS . " seconds ago so no cleaning needed.");
485
- }
486
- return false;
487
- }
488
- protected function processImageAndWriteToCache($localImage){
489
- $sData = getimagesize($localImage);
490
- $origType = $sData[2];
491
- $mimeType = $sData['mime'];
492
-
493
- $this->debug(3, "Mime type of image is $mimeType");
494
- if(! preg_match('/^image\/(?:gif|jpg|jpeg|png)$/i', $mimeType)){
495
- return $this->error("The image being resized is not a valid gif, jpg or png.");
496
- }
497
-
498
- if (!function_exists ('imagecreatetruecolor')) {
499
- return $this->error('GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library');
500
- }
501
-
502
- if (function_exists ('imagefilter') && defined ('IMG_FILTER_NEGATE')) {
503
- $imageFilters = array (
504
- 1 => array (IMG_FILTER_NEGATE, 0),
505
- 2 => array (IMG_FILTER_GRAYSCALE, 0),
506
- 3 => array (IMG_FILTER_BRIGHTNESS, 1),
507
- 4 => array (IMG_FILTER_CONTRAST, 1),
508
- 5 => array (IMG_FILTER_COLORIZE, 4),
509
- 6 => array (IMG_FILTER_EDGEDETECT, 0),
510
- 7 => array (IMG_FILTER_EMBOSS, 0),
511
- 8 => array (IMG_FILTER_GAUSSIAN_BLUR, 0),
512
- 9 => array (IMG_FILTER_SELECTIVE_BLUR, 0),
513
- 10 => array (IMG_FILTER_MEAN_REMOVAL, 0),
514
- 11 => array (IMG_FILTER_SMOOTH, 0),
515
- );
516
- }
517
-
518
- // get standard input properties
519
- $new_width = (int) abs ($this->param('w', 0));
520
- $new_height = (int) abs ($this->param('h', 0));
521
- $zoom_crop = (int) $this->param('zc', DEFAULT_ZC);
522
- $quality = (int) abs ($this->param('q', DEFAULT_Q));
523
- $align = $this->cropTop ? 't' : $this->param('a', 'c');
524
- $filters = $this->param('f', DEFAULT_F);
525
- $sharpen = (bool) $this->param('s', DEFAULT_S);
526
- $canvas_color = $this->param('cc', DEFAULT_CC);
527
- $canvas_trans = (bool) $this->param('ct', '1');
528
-
529
- // set default width and height if neither are set already
530
- if ($new_width == 0 && $new_height == 0) {
531
- $new_width = 100;
532
- $new_height = 100;
533
- }
534
-
535
- // ensure size limits can not be abused
536
- $new_width = min ($new_width, MAX_WIDTH);
537
- $new_height = min ($new_height, MAX_HEIGHT);
538
-
539
- // set memory limit to be able to have enough space to resize larger images
540
- $this->setMemoryLimit();
541
-
542
- // open the existing image
543
- $image = $this->openImage ($mimeType, $localImage);
544
- if ($image === false) {
545
- return $this->error('Unable to open image.');
546
- }
547
-
548
- // Get original width and height
549
- $width = imagesx ($image);
550
- $height = imagesy ($image);
551
- $origin_x = 0;
552
- $origin_y = 0;
553
-
554
- // generate new w/h if not provided
555
- if ($new_width && !$new_height) {
556
- $new_height = floor ($height * ($new_width / $width));
557
- } else if ($new_height && !$new_width) {
558
- $new_width = floor ($width * ($new_height / $height));
559
- }
560
-
561
- // scale down and add borders
562
- if ($zoom_crop == 3) {
563
-
564
- $final_height = $height * ($new_width / $width);
565
-
566
- if ($final_height > $new_height) {
567
- $new_width = $width * ($new_height / $height);
568
- } else {
569
- $new_height = $final_height;
570
- }
571
-
572
- }
573
-
574
- // create a new true color image
575
- $canvas = imagecreatetruecolor ($new_width, $new_height);
576
- imagealphablending ($canvas, false);
577
-
578
- if (strlen($canvas_color) == 3) { //if is 3-char notation, edit string into 6-char notation
579
- $canvas_color = str_repeat(substr($canvas_color, 0, 1), 2) . str_repeat(substr($canvas_color, 1, 1), 2) . str_repeat(substr($canvas_color, 2, 1), 2);
580
- } else if (strlen($canvas_color) != 6) {
581
- $canvas_color = DEFAULT_CC; // on error return default canvas color
582
- }
583
-
584
- $canvas_color_R = hexdec (substr ($canvas_color, 0, 2));
585
- $canvas_color_G = hexdec (substr ($canvas_color, 2, 2));
586
- $canvas_color_B = hexdec (substr ($canvas_color, 4, 2));
587
-
588
- // Create a new transparent color for image
589
- // If is a png and PNG_IS_TRANSPARENT is false then remove the alpha transparency
590
- // (and if is set a canvas color show it in the background)
591
- if(preg_match('/^image\/png$/i', $mimeType) && !PNG_IS_TRANSPARENT && $canvas_trans){
592
- $color = imagecolorallocatealpha ($canvas, $canvas_color_R, $canvas_color_G, $canvas_color_B, 127);
593
- }else{
594
- $color = imagecolorallocatealpha ($canvas, $canvas_color_R, $canvas_color_G, $canvas_color_B, 0);
595
- }
596
-
597
-
598
- // Completely fill the background of the new image with allocated color.
599
- imagefill ($canvas, 0, 0, $color);
600
-
601
- // scale down and add borders
602
- if ($zoom_crop == 2) {
603
-
604
- $final_height = $height * ($new_width / $width);
605
-
606
- if ($final_height > $new_height) {
607
-
608
- $origin_x = $new_width / 2;
609
- $new_width = $width * ($new_height / $height);
610
- $origin_x = round ($origin_x - ($new_width / 2));
611
-
612
- } else {
613
-
614
- $origin_y = $new_height / 2;
615
- $new_height = $final_height;
616
- $origin_y = round ($origin_y - ($new_height / 2));
617
-
618
- }
619
-
620
- }
621
-
622
- // Restore transparency blending
623
- imagesavealpha ($canvas, true);
624
-
625
- if ($zoom_crop > 0) {
626
-
627
- $src_x = $src_y = 0;
628
- $src_w = $width;
629
- $src_h = $height;
630
-
631
- $cmp_x = $width / $new_width;
632
- $cmp_y = $height / $new_height;
633
-
634
- // calculate x or y coordinate and width or height of source
635
- if ($cmp_x > $cmp_y) {
636
-
637
- $src_w = round ($width / $cmp_x * $cmp_y);
638
- $src_x = round (($width - ($width / $cmp_x * $cmp_y)) / 2);
639
-
640
- } else if ($cmp_y > $cmp_x) {
641
-
642
- $src_h = round ($height / $cmp_y * $cmp_x);
643
- $src_y = round (($height - ($height / $cmp_y * $cmp_x)) / 2);
644
-
645
- }
646
-
647
- // positional cropping!
648
- if ($align) {
649
- if (strpos ($align, 't') !== false) {
650
- $src_y = 0;
651
- }
652
- if (strpos ($align, 'b') !== false) {
653
- $src_y = $height - $src_h;
654
- }
655
- if (strpos ($align, 'l') !== false) {
656
- $src_x = 0;
657
- }
658
- if (strpos ($align, 'r') !== false) {
659
- $src_x = $width - $src_w;
660
- }
661
- }
662
-
663
- imagecopyresampled ($canvas, $image, $origin_x, $origin_y, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h);
664
-
665
- } else {
666
-
667
- // copy and resize part of an image with resampling
668
- imagecopyresampled ($canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
669
-
670
- }
671
-
672
- if ($filters != '' && function_exists ('imagefilter') && defined ('IMG_FILTER_NEGATE')) {
673
- // apply filters to image
674
- $filterList = explode ('|', $filters);
675
- foreach ($filterList as $fl) {
676
-
677
- $filterSettings = explode (',', $fl);
678
- if (isset ($imageFilters[$filterSettings[0]])) {
679
-
680
- for ($i = 0; $i < 4; $i ++) {
681
- if (!isset ($filterSettings[$i])) {
682
- $filterSettings[$i] = null;
683
- } else {
684
- $filterSettings[$i] = (int) $filterSettings[$i];
685
- }
686
- }
687
-
688
- switch ($imageFilters[$filterSettings[0]][1]) {
689
-
690
- case 1:
691
-
692
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1]);
693
- break;
694
-
695
- case 2:
696
-
697
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2]);
698
- break;
699
-
700
- case 3:
701
-
702
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3]);
703
- break;
704
-
705
- case 4:
706
-
707
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3], $filterSettings[4]);
708
- break;
709
-
710
- default:
711
-
712
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0]);
713
- break;
714
-
715
- }
716
- }
717
- }
718
- }
719
-
720
- // sharpen image
721
- if ($sharpen && function_exists ('imageconvolution')) {
722
-
723
- $sharpenMatrix = array (
724
- array (-1,-1,-1),
725
- array (-1,16,-1),
726
- array (-1,-1,-1),
727
- );
728
-
729
- $divisor = 8;
730
- $offset = 0;
731
-
732
- imageconvolution ($canvas, $sharpenMatrix, $divisor, $offset);
733
-
734
- }
735
- //Straight from Wordpress core code. Reduces filesize by up to 70% for PNG's
736
- if ( (IMAGETYPE_PNG == $origType || IMAGETYPE_GIF == $origType) && function_exists('imageistruecolor') && !imageistruecolor( $image ) && imagecolortransparent( $image ) > 0 ){
737
- imagetruecolortopalette( $canvas, false, imagecolorstotal( $image ) );
738
- }
739
-
740
- $imgType = "";
741
- $tempfile = tempnam($this->cacheDirectory, 'timthumb_tmpimg_');
742
- if(preg_match('/^image\/(?:jpg|jpeg)$/i', $mimeType)){
743
- $imgType = 'jpg';
744
- imagejpeg($canvas, $tempfile, $quality);
745
- } else if(preg_match('/^image\/png$/i', $mimeType)){
746
- $imgType = 'png';
747
- imagepng($canvas, $tempfile, floor($quality * 0.09));
748
- } else if(preg_match('/^image\/gif$/i', $mimeType)){
749
- $imgType = 'gif';
750
- imagegif($canvas, $tempfile);
751
- } else {
752
- return $this->sanityFail("Could not match mime type after verifying it previously.");
753
- }
754
-
755
- if($imgType == 'png' && OPTIPNG_ENABLED && OPTIPNG_PATH && @is_file(OPTIPNG_PATH)){
756
- $exec = OPTIPNG_PATH;
757
- $this->debug(3, "optipng'ing $tempfile");
758
- $presize = filesize($tempfile);
759
- $out = `$exec -o1 $tempfile`; //you can use up to -o7 but it really slows things down
760
- clearstatcache();
761
- $aftersize = filesize($tempfile);
762
- $sizeDrop = $presize - $aftersize;
763
- if($sizeDrop > 0){
764
- $this->debug(1, "optipng reduced size by $sizeDrop");
765
- } else if($sizeDrop < 0){
766
- $this->debug(1, "optipng increased size! Difference was: $sizeDrop");
767
- } else {
768
- $this->debug(1, "optipng did not change image size.");
769
- }
770
- } else if($imgType == 'png' && PNGCRUSH_ENABLED && PNGCRUSH_PATH && @is_file(PNGCRUSH_PATH)){
771
- $exec = PNGCRUSH_PATH;
772
- $tempfile2 = tempnam($this->cacheDirectory, 'timthumb_tmpimg_');
773
- $this->debug(3, "pngcrush'ing $tempfile to $tempfile2");
774
- $out = `$exec $tempfile $tempfile2`;
775
- $todel = "";
776
- if(is_file($tempfile2)){
777
- $sizeDrop = filesize($tempfile) - filesize($tempfile2);
778
- if($sizeDrop > 0){
779
- $this->debug(1, "pngcrush was succesful and gave a $sizeDrop byte size reduction");
780
- $todel = $tempfile;
781
- $tempfile = $tempfile2;
782
- } else {
783
- $this->debug(1, "pngcrush did not reduce file size. Difference was $sizeDrop bytes.");
784
- $todel = $tempfile2;
785
- }
786
- } else {
787
- $this->debug(3, "pngcrush failed with output: $out");
788
- $todel = $tempfile2;
789
- }
790
- @unlink($todel);
791
- }
792
-
793
- $this->debug(3, "Rewriting image with security header.");
794
- $tempfile4 = tempnam($this->cacheDirectory, 'timthumb_tmpimg_');
795
- $context = stream_context_create ();
796
- $fp = fopen($tempfile,'r',0,$context);
797
- file_put_contents($tempfile4, $this->filePrependSecurityBlock . $imgType . ' ?' . '>'); //6 extra bytes, first 3 being image type
798
- file_put_contents($tempfile4, $fp, FILE_APPEND);
799
- fclose($fp);
800
- @unlink($tempfile);
801
- $this->debug(3, "Locking and replacing cache file.");
802
- $lockFile = $this->cachefile . '.lock';
803
- $fh = fopen($lockFile, 'w');
804
- if(! $fh){
805
- return $this->error("Could not open the lockfile for writing an image.");
806
- }
807
- if(flock($fh, LOCK_EX)){
808
- @unlink($this->cachefile); //rename generally overwrites, but doing this in case of platform specific quirks. File might not exist yet.
809
- rename($tempfile4, $this->cachefile);
810
- flock($fh, LOCK_UN);
811
- fclose($fh);
812
- @unlink($lockFile);
813
- } else {
814
- fclose($fh);
815
- @unlink($lockFile);
816
- @unlink($tempfile4);
817
- return $this->error("Could not get a lock for writing.");
818
- }
819
- $this->debug(3, "Done image replace with security header. Cleaning up and running cleanCache()");
820
- imagedestroy($canvas);
821
- imagedestroy($image);
822
- return true;
823
- }
824
- protected function calcDocRoot(){
825
- $docRoot = @$_SERVER['DOCUMENT_ROOT'];
826
- if (defined('LOCAL_FILE_BASE_DIRECTORY')) {
827
- $docRoot = LOCAL_FILE_BASE_DIRECTORY;
828
- }
829
- if(!isset($docRoot)){
830
- $this->debug(3, "DOCUMENT_ROOT is not set. This is probably windows. Starting search 1.");
831
- if(isset($_SERVER['SCRIPT_FILENAME'])){
832
- $docRoot = str_replace( '\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF'])));
833
- $this->debug(3, "Generated docRoot using SCRIPT_FILENAME and PHP_SELF as: $docRoot");
834
- }
835
- }
836
- if(!isset($docRoot)){
837
- $this->debug(3, "DOCUMENT_ROOT still is not set. Starting search 2.");
838
- if(isset($_SERVER['PATH_TRANSLATED'])){
839
- $docRoot = str_replace( '\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0-strlen($_SERVER['PHP_SELF'])));
840
- $this->debug(3, "Generated docRoot using PATH_TRANSLATED and PHP_SELF as: $docRoot");
841
- }
842
- }
843
- if($docRoot && $_SERVER['DOCUMENT_ROOT'] != '/'){ $docRoot = preg_replace('/\/$/', '', $docRoot); }
844
- $this->debug(3, "Doc root is: " . $docRoot);
845
- $this->docRoot = $docRoot;
846
-
847
- }
848
- protected function getLocalImagePath($src){
849
- $src = ltrim($src, '/'); //strip off the leading '/'
850
- if(! $this->docRoot){
851
- $this->debug(3, "We have no document root set, so as a last resort, lets check if the image is in the current dir and serve that.");
852
- //We don't support serving images outside the current dir if we don't have a doc root for security reasons.
853
- $file = preg_replace('/^.*?([^\/\\\\]+)$/', '$1', $src); //strip off any path info and just leave the filename.
854
- if(is_file($file)){
855
- return $this->realpath($file);
856
- }
857
- return $this->error("Could not find your website document root and the file specified doesn't exist in timthumbs directory. We don't support serving files outside timthumb's directory without a document root for security reasons.");
858
- } //Do not go past this point without docRoot set
859
-
860
- //Try src under docRoot
861
- if(file_exists ($this->docRoot . '/' . $src)) {
862
- $this->debug(3, "Found file as " . $this->docRoot . '/' . $src);
863
- $real = $this->realpath($this->docRoot . '/' . $src);
864
- if(stripos($real, $this->docRoot) === 0){
865
- return $real;
866
- } else {
867
- $this->debug(1, "Security block: The file specified occurs outside the document root.");
868
- //allow search to continue
869
- }
870
- }
871
- //Check absolute paths and then verify the real path is under doc root
872
- $absolute = $this->realpath('/' . $src);
873
- if($absolute && file_exists($absolute)){ //realpath does file_exists check, so can probably skip the exists check here
874
- $this->debug(3, "Found absolute path: $absolute");
875
- if(! $this->docRoot){ $this->sanityFail("docRoot not set when checking absolute path."); }
876
- if(stripos($absolute, $this->docRoot) === 0){
877
- return $absolute;
878
- } else {
879
- $this->debug(1, "Security block: The file specified occurs outside the document root.");
880
- //and continue search
881
- }
882
- }
883
-
884
- $base = $this->docRoot;
885
-
886
- // account for Windows directory structure
887
- if (strstr($_SERVER['SCRIPT_FILENAME'],':')) {
888
- $sub_directories = explode('\\', str_replace($this->docRoot, '', $_SERVER['SCRIPT_FILENAME']));
889
- } else {
890
- $sub_directories = explode('/', str_replace($this->docRoot, '', $_SERVER['SCRIPT_FILENAME']));
891
- }
892
-
893
- foreach ($sub_directories as $sub){
894
- $base .= $sub . '/';
895
- $this->debug(3, "Trying file as: " . $base . $src);
896
- if(file_exists($base . $src)){
897
- $this->debug(3, "Found file as: " . $base . $src);
898
- $real = $this->realpath($base . $src);
899
- if(stripos($real, $this->realpath($this->docRoot)) === 0){
900
- return $real;
901
- } else {
902
- $this->debug(1, "Security block: The file specified occurs outside the document root.");
903
- //And continue search
904
- }
905
- }
906
- }
907
- return false;
908
- }
909
- protected function realpath($path){
910
- //try to remove any relative paths
911
- $remove_relatives = '/\w+\/\.\.\//';
912
- while(preg_match($remove_relatives,$path)){
913
- $path = preg_replace($remove_relatives, '', $path);
914
- }
915
- //if any remain use PHP realpath to strip them out, otherwise return $path
916
- //if using realpath, any symlinks will also be resolved
917
- return preg_match('#^\.\./|/\.\./#', $path) ? realpath($path) : $path;
918
- }
919
- protected function toDelete($name){
920
- $this->debug(3, "Scheduling file $name to delete on destruct.");
921
- $this->toDeletes[] = $name;
922
- }
923
- protected function serveWebshot(){
924
- $this->debug(3, "Starting serveWebshot");
925
- $instr = "Please follow the instructions at http://code.google.com/p/timthumb/ to set your server up for taking website screenshots.";
926
- if(! is_file(WEBSHOT_CUTYCAPT)){
927
- return $this->error("CutyCapt is not installed. $instr");
928
- }
929
- if(! is_file(WEBSHOT_XVFB)){
930
- return $this->Error("Xvfb is not installed. $instr");
931
- }
932
- $cuty = WEBSHOT_CUTYCAPT;
933
- $xv = WEBSHOT_XVFB;
934
- $screenX = WEBSHOT_SCREEN_X;
935
- $screenY = WEBSHOT_SCREEN_Y;
936
- $colDepth = WEBSHOT_COLOR_DEPTH;
937
- $format = WEBSHOT_IMAGE_FORMAT;
938
- $timeout = WEBSHOT_TIMEOUT * 1000;
939
- $ua = WEBSHOT_USER_AGENT;
940
- $jsOn = WEBSHOT_JAVASCRIPT_ON ? 'on' : 'off';
941
- $javaOn = WEBSHOT_JAVA_ON ? 'on' : 'off';
942
- $pluginsOn = WEBSHOT_PLUGINS_ON ? 'on' : 'off';
943
- $proxy = WEBSHOT_PROXY ? ' --http-proxy=' . WEBSHOT_PROXY : '';
944
- $tempfile = tempnam($this->cacheDirectory, 'timthumb_webshot');
945
- $url = $this->src;
946
- if(! preg_match('/^https?:\/\/[a-zA-Z0-9\.\-]+/i', $url)){
947
- return $this->error("Invalid URL supplied.");
948
- }
949
- $url = preg_replace('/[^A-Za-z0-9\-\.\_\~:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=]+/', '', $url); //RFC 3986
950
- //Very important we don't allow injection of shell commands here. URL is between quotes and we are only allowing through chars allowed by a the RFC
951
- // which AFAIKT can't be used for shell injection.
952
- if(WEBSHOT_XVFB_RUNNING){
953
- putenv('DISPLAY=:100.0');
954
- $command = "$cuty $proxy --max-wait=$timeout --user-agent=\"$ua\" --javascript=$jsOn --java=$javaOn --plugins=$pluginsOn --js-can-open-windows=off --url=\"$url\" --out-format=$format --out=$tempfile";
955
- } else {
956
- $command = "$xv --server-args=\"-screen 0, {$screenX}x{$screenY}x{$colDepth}\" $cuty $proxy --max-wait=$timeout --user-agent=\"$ua\" --javascript=$jsOn --java=$javaOn --plugins=$pluginsOn --js-can-open-windows=off --url=\"$url\" --out-format=$format --out=$tempfile";
957
- }
958
- $this->debug(3, "Executing command: $command");
959
- $out = `$command`;
960
- $this->debug(3, "Received output: $out");
961
- if(! is_file($tempfile)){
962
- $this->set404();
963
- return $this->error("The command to create a thumbnail failed.");
964
- }
965
- $this->cropTop = true;
966
- if($this->processImageAndWriteToCache($tempfile)){
967
- $this->debug(3, "Image processed succesfully. Serving from cache");
968
- return $this->serveCacheFile();
969
- } else {
970
- return false;
971
- }
972
- }
973
- protected function serveExternalImage(){
974
- if(! preg_match('/^https?:\/\/[a-zA-Z0-9\-\.]+/i', $this->src)){
975
- $this->error("Invalid URL supplied.");
976
- return false;
977
- }
978
- $tempfile = tempnam($this->cacheDirectory, 'timthumb');
979
- $this->debug(3, "Fetching external image into temporary file $tempfile");
980
- $this->toDelete($tempfile);
981
- #fetch file here
982
- if(! $this->getURL($this->src, $tempfile)){
983
- @unlink($this->cachefile);
984
- touch($this->cachefile);
985
- $this->debug(3, "Error fetching URL: " . $this->lastURLError);
986
- $this->error("Error reading the URL you specified from remote host." . $this->lastURLError);
987
- return false;
988
- }
989
-
990
- $mimeType = $this->getMimeType($tempfile);
991
- if(! preg_match("/^image\/(?:jpg|jpeg|gif|png)$/i", $mimeType)){
992
- $this->debug(3, "Remote file has invalid mime type: $mimeType");
993
- @unlink($this->cachefile);
994
- touch($this->cachefile);
995
- $this->error("The remote file is not a valid image. Mimetype = '" . $mimeType . "'" . $tempfile);
996
- return false;
997
- }
998
- if($this->processImageAndWriteToCache($tempfile)){
999
- $this->debug(3, "Image processed succesfully. Serving from cache");
1000
- return $this->serveCacheFile();
1001
- } else {
1002
- return false;
1003
- }
1004
- }
1005
- public static function curlWrite($h, $d){
1006
- fwrite(self::$curlFH, $d);
1007
- self::$curlDataWritten += strlen($d);
1008
- if(self::$curlDataWritten > MAX_FILE_SIZE){
1009
- return 0;
1010
- } else {
1011
- return strlen($d);
1012
- }
1013
- }
1014
- protected function serveCacheFile(){
1015
- $this->debug(3, "Serving {$this->cachefile}");
1016
- if(! is_file($this->cachefile)){
1017
- $this->error("serveCacheFile called in timthumb but we couldn't find the cached file.");
1018
- return false;
1019
- }
1020
- $fp = fopen($this->cachefile, 'rb');
1021
- if(! $fp){ return $this->error("Could not open cachefile."); }
1022
- fseek($fp, strlen($this->filePrependSecurityBlock), SEEK_SET);
1023
- $imgType = fread($fp, 3);
1024
- fseek($fp, 3, SEEK_CUR);
1025
- if(ftell($fp) != strlen($this->filePrependSecurityBlock) + 6){
1026
- @unlink($this->cachefile);
1027
- return $this->error("The cached image file seems to be corrupt.");
1028
- }
1029
- $imageDataSize = filesize($this->cachefile) - (strlen($this->filePrependSecurityBlock) + 6);
1030
- $this->sendImageHeaders($imgType, $imageDataSize);
1031
- $bytesSent = @fpassthru($fp);
1032
- fclose($fp);
1033
- if($bytesSent > 0){
1034
- return true;
1035
- }
1036
- $content = file_get_contents ($this->cachefile);
1037
- if ($content != FALSE) {
1038
- $content = substr($content, strlen($this->filePrependSecurityBlock) + 6);
1039
- echo $content;
1040
- $this->debug(3, "Served using file_get_contents and echo");
1041
- return true;
1042
- } else {
1043
- $this->error("Cache file could not be loaded.");
1044
- return false;
1045
- }
1046
- }
1047
- protected function sendImageHeaders($mimeType, $dataSize){
1048
- if(! preg_match('/^image\//i', $mimeType)){
1049
- $mimeType = 'image/' . $mimeType;
1050
- }
1051
- if(strtolower($mimeType) == 'image/jpg'){
1052
- $mimeType = 'image/jpeg';
1053
- }
1054
- $gmdate_expires = gmdate ('D, d M Y H:i:s', strtotime ('now +10 days')) . ' GMT';
1055
- $gmdate_modified = gmdate ('D, d M Y H:i:s') . ' GMT';
1056
- // send content headers then display image
1057
- header ('Content-Type: ' . $mimeType);
1058
- header ('Accept-Ranges: none'); //Changed this because we don't accept range requests
1059
- header ('Last-Modified: ' . $gmdate_modified);
1060
- header ('Content-Length: ' . $dataSize);
1061
- if(BROWSER_CACHE_DISABLE){
1062
- $this->debug(3, "Browser cache is disabled so setting non-caching headers.");
1063
- header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
1064
- header("Pragma: no-cache");
1065
- header('Expires: ' . gmdate ('D, d M Y H:i:s', time()));
1066
- } else {
1067
- $this->debug(3, "Browser caching is enabled");
1068
- header('Cache-Control: max-age=' . BROWSER_CACHE_MAX_AGE . ', must-revalidate');
1069
- header('Expires: ' . $gmdate_expires);
1070
- }
1071
- return true;
1072
- }
1073
- protected function securityChecks(){
1074
- }
1075
- protected function param($property, $default = ''){
1076
- if (isset ($_GET[$property])) {
1077
- return $_GET[$property];
1078
- } else {
1079
- return $default;
1080
- }
1081
- }
1082
- protected function openImage($mimeType, $src){
1083
- switch ($mimeType) {
1084
- case 'image/jpeg':
1085
- $image = imagecreatefromjpeg ($src);
1086
- break;
1087
-
1088
- case 'image/png':
1089
- $image = imagecreatefrompng ($src);
1090
- imagealphablending( $image, true );
1091
- imagesavealpha( $image, true );
1092
- break;
1093
-
1094
- case 'image/gif':
1095
- $image = imagecreatefromgif ($src);
1096
- break;
1097
-
1098
- default:
1099
- $this->error("Unrecognised mimeType");
1100
- }
1101
-
1102
- return $image;
1103
- }
1104
- protected function getIP(){
1105
- $rem = @$_SERVER["REMOTE_ADDR"];
1106
- $ff = @$_SERVER["HTTP_X_FORWARDED_FOR"];
1107
- $ci = @$_SERVER["HTTP_CLIENT_IP"];
1108
- if(preg_match('/^(?:192\.168|172\.16|10\.|127\.)/', $rem)){
1109
- if($ff){ return $ff; }
1110
- if($ci){ return $ci; }
1111
- return $rem;
1112
- } else {
1113
- if($rem){ return $rem; }
1114
- if($ff){ return $ff; }
1115
- if($ci){ return $ci; }
1116
- return "UNKNOWN";
1117
- }
1118
- }
1119
- protected function debug($level, $msg){
1120
- if(DEBUG_ON && $level <= DEBUG_LEVEL){
1121
- $execTime = sprintf('%.6f', microtime(true) - $this->startTime);
1122
- $tick = sprintf('%.6f', 0);
1123
- if($this->lastBenchTime > 0){
1124
- $tick = sprintf('%.6f', microtime(true) - $this->lastBenchTime);
1125
- }
1126
- $this->lastBenchTime = microtime(true);
1127
- error_log("TimThumb Debug line " . __LINE__ . " [$execTime : $tick]: $msg");
1128
- }
1129
- }
1130
- protected function sanityFail($msg){
1131
- return $this->error("There is a problem in the timthumb code. Message: Please report this error at <a href='http://code.google.com/p/timthumb/issues/list'>timthumb's bug tracking page</a>: $msg");
1132
- }
1133
- protected function getMimeType($file){
1134
- $info = getimagesize($file);
1135
- if(is_array($info) && $info['mime']){
1136
- return $info['mime'];
1137
- }
1138
- return '';
1139
- }
1140
- protected function setMemoryLimit(){
1141
- $inimem = ini_get('memory_limit');
1142
- $inibytes = timthumb::returnBytes($inimem);
1143
- $ourbytes = timthumb::returnBytes(MEMORY_LIMIT);
1144
- if($inibytes < $ourbytes){
1145
- ini_set ('memory_limit', MEMORY_LIMIT);
1146
- $this->debug(3, "Increased memory from $inimem to " . MEMORY_LIMIT);
1147
- } else {
1148
- $this->debug(3, "Not adjusting memory size because the current setting is " . $inimem . " and our size of " . MEMORY_LIMIT . " is smaller.");
1149
- }
1150
- }
1151
- protected static function returnBytes($size_str){
1152
- switch (substr ($size_str, -1))
1153
- {
1154
- case 'M': case 'm': return (int)$size_str * 1048576;
1155
- case 'K': case 'k': return (int)$size_str * 1024;
1156
- case 'G': case 'g': return (int)$size_str * 1073741824;
1157
- default: return $size_str;
1158
- }
1159
- }
1160
-
1161
- protected function getURL($url, $tempfile){
1162
- $this->lastURLError = false;
1163
- $url = preg_replace('/ /', '%20', $url);
1164
- if(function_exists('curl_init')){
1165
- $this->debug(3, "Curl is installed so using it to fetch URL.");
1166
- self::$curlFH = fopen($tempfile, 'w');
1167
- if(! self::$curlFH){
1168
- $this->error("Could not open $tempfile for writing.");
1169
- return false;
1170
- }
1171
- self::$curlDataWritten = 0;
1172
- $this->debug(3, "Fetching url with curl: $url");
1173
- $curl = curl_init($url);
1174
- curl_setopt ($curl, CURLOPT_TIMEOUT, CURL_TIMEOUT);
1175
- curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30");
1176
- curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE);
1177
- curl_setopt ($curl, CURLOPT_HEADER, 0);
1178
- curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
1179
- curl_setopt ($curl, CURLOPT_WRITEFUNCTION, 'timthumb::curlWrite');
1180
- @curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
1181
- @curl_setopt ($curl, CURLOPT_MAXREDIRS, 10);
1182
-
1183
- $curlResult = curl_exec($curl);
1184
- fclose(self::$curlFH);
1185
- $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
1186
- if($httpStatus == 404){
1187
- $this->set404();
1188
- }
1189
- if($httpStatus == 302){
1190
- $this->error("External Image is Redirecting. Try alternate image url");
1191
- return false;
1192
- }
1193
- if($curlResult){
1194
- curl_close($curl);
1195
- return true;
1196
- } else {
1197
- $this->lastURLError = curl_error($curl);
1198
- curl_close($curl);
1199
- return false;
1200
- }
1201
- } else {
1202
- $img = @file_get_contents ($url);
1203
- if($img === false){
1204
- $err = error_get_last();
1205
- if(is_array($err) && $err['message']){
1206
- $this->lastURLError = $err['message'];
1207
- } else {
1208
- $this->lastURLError = $err;
1209
- }
1210
- if(preg_match('/404/', $this->lastURLError)){
1211
- $this->set404();
1212
- }
1213
-
1214
- return false;
1215
- }
1216
- if(! file_put_contents($tempfile, $img)){
1217
- $this->error("Could not write to $tempfile.");
1218
- return false;
1219
- }
1220
- return true;
1221
- }
1222
-
1223
- }
1224
- protected function serveImg($file){
1225
- $s = getimagesize($file);
1226
- if(! ($s && $s['mime'])){
1227
- return false;
1228
- }
1229
- header ('Content-Type: ' . $s['mime']);
1230
- header ('Content-Length: ' . filesize($file) );
1231
- header ('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
1232
- header ("Pragma: no-cache");
1233
- $bytes = @readfile($file);
1234
- if($bytes > 0){
1235
- return true;
1236
- }
1237
- $content = @file_get_contents ($file);
1238
- if ($content != FALSE){
1239
- echo $content;
1240
- return true;
1241
- }
1242
- return false;
1243
-
1244
- }
1245
- protected function set404(){
1246
- $this->is404 = true;
1247
- }
1248
- protected function is404(){
1249
- return $this->is404;
1250
- }
1251
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
js/bj-lazy-load.js CHANGED
@@ -1,118 +1,82 @@
1
-
2
- var BJLL = BJLL || {};
3
-
4
- (function($) {
5
-
6
- function bj_lazy_load_init() {
7
-
8
- var threshold = 200;
9
-
10
- if ( 'undefined' != typeof ( BJLL.threshold ) ) {
11
- threshold = parseInt( BJLL.threshold );
12
- }
13
-
14
- $('.lazy-hidden').not('.data-lazy-ready').one( 'scrollin.bj_lazy_load', { distance: threshold }, function() {
15
-
16
- var $el = $( this ),
17
- data_lazy_type = $el.attr( 'data-lazy-type' );
18
-
19
- if ( data_lazy_type == 'image' ) {
20
-
21
- var imgurl = $el.attr( 'data-lazy-src' );
22
-
23
- if ( BJLL.load_responsive == 'yes' || BJLL.load_hidpi == 'yes' ) {
24
- var l = document.createElement( 'a' );
25
- l.href = $el.attr( 'data-lazy-src' );
26
-
27
- if ( ! l.hostname.length || l.hostname == window.location.hostname ) {
28
- var loadimgwidth = parseInt( $el.css( 'width' ) );
29
- if ( window.devicePixelRatio > 1 && BJLL.load_hidpi == 'yes' ) {
30
- loadimgwidth = Math.ceil( window.devicePixelRatio * loadimgwidth );
31
- }
32
- var srcimgurl = $el.attr( 'data-lazy-src' );
33
- if ( 'undefined' != typeof ( BJLL.site_url ) && 'undefined' != typeof ( BJLL.network_site_url ) ) {
34
- srcimgurl = srcimgurl.replace( BJLL.site_url, BJLL.network_site_url );
35
- }
36
- imgurl = BJLL.thumb_base + encodeURIComponent( srcimgurl ) + '&w=' + loadimgwidth;
37
- }
38
-
39
- }
40
-
41
- $el.hide()
42
- .attr( 'src', imgurl )
43
- .removeClass( 'lazy-hidden' )
44
- .fadeIn();
45
- } else if ( data_lazy_type == 'iframe' ) {
46
- $el.replaceWith(
47
- bj_lazy_load_base64_decode(
48
- $el.attr( 'data-lazy-src' )
49
- )
50
- );
51
- }
52
- }).addClass( 'data-lazy-ready' );
53
-
54
- }
55
-
56
- function bj_lazy_load_base64_decode (data) {
57
- // http://kevin.vanzonneveld.net
58
- // + original by: Tyler Akins (http://rumkin.com)
59
- // + improved by: Thunder.m
60
- // + input by: Aman Gupta
61
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
62
- // + bugfixed by: Onno Marsman
63
- // + bugfixed by: Pellentesque Malesuada
64
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
65
- // + input by: Brett Zamir (http://brett-zamir.me)
66
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
67
- // * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
68
- // * returns 1: 'Kevin van Zonneveld'
69
- // mozilla has this native
70
- // - but breaks in 2.0.0.12!
71
- //if (typeof this.window['atob'] == 'function') {
72
- // return atob(data);
73
- //}
74
- var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
75
- var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
76
- ac = 0,
77
- dec = "",
78
- tmp_arr = [];
79
-
80
- if (!data) {
81
- return data;
82
- }
83
-
84
- data += '';
85
-
86
- do { // unpack four hexets into three octets using index points in b64
87
- h1 = b64.indexOf(data.charAt(i++));
88
- h2 = b64.indexOf(data.charAt(i++));
89
- h3 = b64.indexOf(data.charAt(i++));
90
- h4 = b64.indexOf(data.charAt(i++));
91
-
92
- bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
93
-
94
- o1 = bits >> 16 & 0xff;
95
- o2 = bits >> 8 & 0xff;
96
- o3 = bits & 0xff;
97
-
98
- if (h3 == 64) {
99
- tmp_arr[ac++] = String.fromCharCode(o1);
100
- } else if (h4 == 64) {
101
- tmp_arr[ac++] = String.fromCharCode(o1, o2);
102
- } else {
103
- tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
104
- }
105
- } while (i < data.length);
106
-
107
- dec = tmp_arr.join('');
108
-
109
- return dec;
110
- }
111
-
112
- $( document ).on( 'ready', bj_lazy_load_init );
113
- if ( 'yes' == BJLL.infinite_scroll ) {
114
- $( window ).on( 'scroll', bj_lazy_load_init );
115
- }
116
- $( window ).on( 'resize', function() { $( document ).trigger( 'scroll' ); } );
117
-
118
- })(jQuery);
1
+ var BJLL_options = BJLL_options || {};
2
+
3
+ var BJLL = {
4
+
5
+ check: function () {
6
+
7
+ if ( 'undefined' == typeof ( BJLL.threshold ) ) {
8
+ if ( 'undefined' != typeof ( BJLL_options.threshold ) ) {
9
+ BJLL.threshold = parseInt( BJLL_options.threshold );
10
+ } else {
11
+ BJLL.threshold = 200;
12
+ }
13
+ }
14
+
15
+ var winH = document.documentElement.clientHeight || body.clientHeight;
16
+
17
+ var els = document.getElementsByClassName('lazy-hidden');
18
+ [].forEach.call( els, function( el, index, array ) {
19
+
20
+ var elemRect = el.getBoundingClientRect();
21
+
22
+ if ( winH - elemRect.top + BJLL.threshold > 0 ) {
23
+ BJLL.show( el );
24
+ }
25
+
26
+ } );
27
+ },
28
+
29
+ show: function( el ) {
30
+
31
+ el.className = el.className.replace( /(?:^|\s)lazy-hidden(?!\S)/g , '' );
32
+
33
+ el.addEventListener( 'load', function() {
34
+ BJLL.customEvent( el, 'lazyloaded' );
35
+ }, false );
36
+
37
+ var type = el.getAttribute('data-lazy-type');
38
+
39
+ if ( 'image' == type ) {
40
+ el.setAttribute( 'src', el.getAttribute('data-lazy-src') );
41
+ if ( null != el.getAttribute('data-srcset') ) {
42
+ el.setAttribute( 'srcset', el.getAttribute('data-srcset') );
43
+ }
44
+ } else if ( 'iframe' == type ) {
45
+ var s = el.getAttribute('data-lazy-src'),
46
+ div = document.createElement('div');
47
+
48
+ div.innerHTML = s;
49
+ var iframe = div.firstChild;
50
+ el.parentNode.replaceChild( iframe, el );
51
+ }
52
+
53
+ },
54
+
55
+ customEvent: function( el, eventName ) {
56
+ var event;
57
+
58
+ if ( document.createEvent ) {
59
+ event = document.createEvent( "HTMLEvents" );
60
+ event.initEvent( eventName, true, true );
61
+ } else {
62
+ event = document.createEventObject();
63
+ event.eventType = eventName;
64
+ }
65
+
66
+ event.eventName = eventName;
67
+
68
+ if ( document.createEvent ) {
69
+ el.dispatchEvent( event );
70
+ } else {
71
+ el.fireEvent( "on" + event.eventType, event );
72
+ }
73
+ }
74
+
75
+ }
76
+
77
+ window.addEventListener( 'load', BJLL.check, false );
78
+ window.addEventListener( 'scroll', BJLL.check, false );
79
+ window.addEventListener( 'resize', BJLL.check, false );
80
+ document.getElementsByTagName( 'body' ).item( 0 ).addEventListener( 'post-load', BJLL.check, false );
81
+
82
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
js/bj-lazy-load.min.js ADDED
@@ -0,0 +1 @@
 
1
+ var BJLL_options=BJLL_options||{},BJLL={check:function(){"undefined"==typeof BJLL.threshold&&(BJLL.threshold="undefined"!=typeof BJLL_options.threshold?parseInt(BJLL_options.threshold):200);var winH=document.documentElement.clientHeight||body.clientHeight,els=document.getElementsByClassName("lazy-hidden");[].forEach.call(els,function(el){var elemRect=el.getBoundingClientRect();winH-elemRect.top+BJLL.threshold>0&&BJLL.show(el)})},show:function(el){el.className=el.className.replace(/(?:^|\s)lazy-hidden(?!\S)/g,""),el.addEventListener("load",function(){BJLL.customEvent(el,"lazyloaded")},!1);var type=el.getAttribute("data-lazy-type");if("image"==type)el.setAttribute("src",el.getAttribute("data-lazy-src")),null!=el.getAttribute("data-srcset")&&el.setAttribute("srcset",el.getAttribute("data-srcset"));else if("iframe"==type){var s=el.getAttribute("data-lazy-src"),div=document.createElement("div");div.innerHTML=s;var iframe=div.firstChild;el.parentNode.replaceChild(iframe,el)}},customEvent:function(el,eventName){var event;document.createEvent?(event=document.createEvent("HTMLEvents"),event.initEvent(eventName,!0,!0)):(event=document.createEventObject(),event.eventType=eventName),event.eventName=eventName,document.createEvent?el.dispatchEvent(event):el.fireEvent("on"+event.eventType,event)}};window.addEventListener("load",BJLL.check,!1),window.addEventListener("scroll",BJLL.check,!1),window.addEventListener("resize",BJLL.check,!1),document.getElementsByTagName("body").item(0).addEventListener("post-load",BJLL.check,!1);
js/combined.min.js DELETED
@@ -1 +0,0 @@
1
- (function($,win,doc,undefined){$.fn.sonar=function(distance,full){if(typeof distance==="boolean"){full=distance;distance=undefined}return $.sonar(this[0],distance,full)};var body=doc.body,$win=$(win),onScreenEvent="scrollin",offScreenEvent="scrollout",detect=function(elem,distance,full){if(elem){body||(body=doc.body);var parentElem=elem,elemTop=0,bodyHeight=body.offsetHeight,screenHeight=win.innerHeight||doc.documentElement.clientHeight||body.clientHeight||0,scrollTop=doc.documentElement.scrollTop||win.pageYOffset||body.scrollTop||0,elemHeight=elem.offsetHeight||0;if(!elem.sonarElemTop||elem.sonarBodyHeight!==bodyHeight){if(parentElem.offsetParent){do{elemTop+=parentElem.offsetTop}while(parentElem=parentElem.offsetParent)}elem.sonarElemTop=elemTop;elem.sonarBodyHeight=bodyHeight}distance=distance===undefined?0:distance;return!(elem.sonarElemTop+(full?0:elemHeight)<scrollTop-distance)&&!(elem.sonarElemTop+(full?elemHeight:0)>scrollTop+screenHeight+distance)}},pollQueue={},pollActive=0,pollId,poll=function(){pollId&&clearTimeout(pollId);pollId=setTimeout(function(){var elem,elems,screenEvent,options,detected,i,l;for(screenEvent in pollQueue){elems=pollQueue[screenEvent];for(i=0,l=elems.length;i<l;i++){options=elems[i];elem=options.elem;detected=detect(elem,options.px,options.full);if(screenEvent===offScreenEvent?!detected:detected){if(!options.tr){if(elem["_"+screenEvent]){$(elem).triggerHandler(screenEvent);options.tr=1}else{elems.splice(i,1);i--;l--}}}else{options.tr=0}}}},0)},removeSonar=function(elem,screenEvent){elem["_"+screenEvent]=0},addSonar=function(elem,options){var distance=options.px,full=options.full,screenEvent=options.evt,parent=win,detected=detect(elem,distance,full),triggered=0;elem["_"+screenEvent]=1;if(screenEvent===offScreenEvent?!detected:detected){setTimeout(function(){$(elem).triggerHandler(screenEvent===offScreenEvent?offScreenEvent:onScreenEvent)},0);triggered=1}pollQueue[screenEvent].push({elem:elem,px:distance,full:full,tr:triggered});if(!pollActive){$win.bind("scroll",poll);pollActive=1}};$.sonar=detect;pollQueue[onScreenEvent]=[];$.event.special[onScreenEvent]={add:function(handleObj){var data=handleObj.data||{},elem=this;if(!elem[onScreenEvent]){addSonar(this,{px:data.distance,full:data.full,evt:onScreenEvent})}},remove:function(handleObj){removeSonar(this,onScreenEvent)}};pollQueue[offScreenEvent]=[];$.event.special[offScreenEvent]={add:function(handleObj){var data=handleObj.data||{},elem=this;if(!elem[offScreenEvent]){addSonar(elem,{px:data.distance,full:data.full,evt:offScreenEvent})}},remove:function(handleObj){removeSonar(this,offScreenEvent)}}})(jQuery,window,document);var BJLL=BJLL||{};!function($){function bj_lazy_load_init(){var threshold=200;"undefined"!=typeof BJLL.threshold&&(threshold=parseInt(BJLL.threshold)),$(".lazy-hidden").not(".data-lazy-ready").one("scrollin.bj_lazy_load",{distance:threshold},function(){var $el=$(this),data_lazy_type=$el.attr("data-lazy-type");if("image"==data_lazy_type){var imgurl=$el.attr("data-lazy-src");if("yes"==BJLL.load_responsive||"yes"==BJLL.load_hidpi){var l=document.createElement("a");if(l.href=$el.attr("data-lazy-src"),!l.hostname.length||l.hostname==window.location.hostname){var loadimgwidth=parseInt($el.css("width"));window.devicePixelRatio>1&&"yes"==BJLL.load_hidpi&&(loadimgwidth=Math.ceil(window.devicePixelRatio*loadimgwidth));var srcimgurl=$el.attr("data-lazy-src");"undefined"!=typeof BJLL.site_url&&"undefined"!=typeof BJLL.network_site_url&&(srcimgurl=srcimgurl.replace(BJLL.site_url,BJLL.network_site_url)),imgurl=BJLL.thumb_base+encodeURIComponent(srcimgurl)+"&w="+loadimgwidth}}$el.hide().attr("src",imgurl).removeClass("lazy-hidden").fadeIn()}else"iframe"==data_lazy_type&&$el.replaceWith(bj_lazy_load_base64_decode($el.attr("data-lazy-src")))}).addClass("data-lazy-ready")}function bj_lazy_load_base64_decode(data){var o1,o2,o3,h1,h2,h3,h4,bits,b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",i=0,ac=0,dec="",tmp_arr=[];if(!data)return data;data+="";do h1=b64.indexOf(data.charAt(i++)),h2=b64.indexOf(data.charAt(i++)),h3=b64.indexOf(data.charAt(i++)),h4=b64.indexOf(data.charAt(i++)),bits=h1<<18|h2<<12|h3<<6|h4,o1=bits>>16&255,o2=bits>>8&255,o3=255&bits,tmp_arr[ac++]=64==h3?String.fromCharCode(o1):64==h4?String.fromCharCode(o1,o2):String.fromCharCode(o1,o2,o3);while(i<data.length);return dec=tmp_arr.join("")}$(document).on("ready",bj_lazy_load_init),"yes"==BJLL.infinite_scroll&&$(window).on("scroll",bj_lazy_load_init),$(window).on("resize",function(){$(document).trigger("scroll")})}(jQuery);
 
js/index.html DELETED
File without changes
js/jquery.sonar.js DELETED
@@ -1,421 +0,0 @@
1
- /*
2
- An elem for determining if an elem is within a certain
3
- distance from the edge above or below the screen, and attaching
4
- a function to execute once the elem is in view.
5
-
6
- General Usage:
7
-
8
- * Place the library anywhere in your JavaScript code before you
9
- intend to call the function.
10
-
11
- * To initialize Sonar with a different default distance, modify
12
- the sonar = Sonar() line immediately following the Sonar
13
- library definition. Example:
14
-
15
- sonar=Sonar(100); // Initializes Sonar with a 100px default distance.
16
-
17
- Note:
18
-
19
- * The default distance is 0 pixels.
20
-
21
-
22
- sonar.detect() Usage
23
-
24
- * Use sonar.detect(elem, distance) to check if the
25
- elem is within screen boundaries.
26
-
27
- @elem - The elem you want to detect visibility.
28
- @distance - The distance from the screen edge that should
29
- count in the check. Uses default distance if not specified.
30
-
31
- * Note: sonar.detect() adds a property to
32
- ojbects called sonarElemTop. Test to ensure there
33
- aren't any conflicts with your code. If there
34
- are, rename sonarElemTop to something else in the code.
35
-
36
- * sonar.detect() returns:
37
- true if the elem is within the screen boundaries
38
- false if th elem is out of the screen boundaries
39
-
40
- Example:
41
-
42
- Here's how to check if an advertisment is visible on a
43
- page that has the id, "ad".
44
-
45
- if (sonar.detect(document.getElementById("ad")))
46
- {
47
- alert('The ad is visible on screen!');
48
- }
49
- else
50
- {
51
- alert ('The ad is not on screen!);
52
- }
53
-
54
- sonar.add() Usage
55
-
56
- * This method stores elems that are then polled
57
- on user scroll by the Sonar.detect() method.
58
-
59
- * Polling initializes once the sonar.add() method is passed
60
- an elem with the following properties:
61
-
62
- obj : A reference to the elem to observe until it is within
63
- the specified distance (px).
64
-
65
- id : An alternative to the obj parameter, an "id" can be used
66
- to grab the elem to observe.
67
-
68
- call: The function to call when the elem is within the
69
- specified distance (px). The @elem argument will
70
- include the elem that triggered the callback.
71
-
72
- px : The specified distance to include as being visible on
73
- screen. This property is optional (default is 0).
74
-
75
- Example:
76
-
77
- sonar.add(
78
- {
79
- obj: document.getElementById("0026-get-out-the-way"),
80
- call: function(elem) // elem will include the elem that triggered the function.
81
- {
82
- swfelem.embedSWF("../player.swf", "0026-get-out-the-way", "640", "500", "9.0.0",
83
- {}, {file: "0026-get-out-the-way.flv", fullscreen: true},
84
- {allowfullscreen: true, allowscriptaccess: "always"});
85
- },
86
- px: 400
87
- });
88
-
89
- You can also specify an id tag to be grabbed instead of the elem:
90
-
91
- sonar.add(
92
- {
93
- id: "0026-get-out-the-way",
94
- call: function(elem) // elem will include the elem that triggered the function.
95
- {
96
- swfelem.embedSWF("../player.swf", "0026-get-out-the-way", "640", "500", "9.0.0",
97
- {}, {file: "0026-get-out-the-way.flv", fullscreen: true},
98
- {allowfullscreen: true, allowscriptaccess: "always"});
99
- },
100
- px: 400
101
- });
102
-
103
- Notes:
104
-
105
- * Setting the body or html of your page to 100% will cause sonar to have
106
- an invalid height calculation in Firefox. It is recommended that you
107
- do not set this CSS property.
108
-
109
- Example:
110
-
111
- html, body {
112
- height:100%; // Do not do this.
113
- }
114
-
115
- * If you want to set the default distance to something other
116
- than 0, either update the property directly in the code or
117
- you can do this:
118
-
119
- sonar.blip.d = 100; // Where 100 = 100 pixels above and below the screen edge.
120
-
121
- * Sleep well at night knowing Sonar automatically cleans up the
122
- event listeners on the scroll event once all calls have executed.
123
-
124
- Code History:
125
-
126
- v3 :: 8/14/2009 - David Artz (david.artz@corp.aol.com)
127
- * Fixed a bug in the polling code where splicing caused our
128
- for loop to skip over the next iteration in the loop. This
129
- caused some images in the poll to be detected when they
130
- should have been.
131
- * Re-factored Sonar to use the "Module" JavaScript library
132
- pattern, making our private variables and functions more
133
- private and inaccessible from the public interface.
134
- * Updated the sonar.add() function to return true or false,
135
- useful for determining if Sonar added the elem to the
136
- poll or executed its callback immediately.
137
-
138
- v2 :: 3/24/2009 - David Artz (david.artz@corp.aol.com)
139
- * Added support for IE 8.
140
- * Updated the way scroll top and screen height are detected, now
141
- works in IE/FF/Safari quirks mode.
142
- * Added null check for IE, it was polling for an elem that had recently
143
- been spliced out of the array. Nasty.
144
- * Modified for loop to use standard syntax. for (i in x) is known to be
145
- buggy with JS frameworks that override arrays.
146
- * Added sonar.b property to cache the body element (improving lookup time).
147
-
148
- v1 :: 11/18/2008 - David Artz (david.artz@corp.aol.com)
149
- * Officially released code for general use.
150
-
151
- */
152
-
153
- (function( $, win, doc, undefined ){
154
-
155
- $.fn.sonar = function( distance, full ){
156
- // No callbacks, return the results from Sonar for
157
- // the first element in the stack.
158
- if ( typeof distance === "boolean" ) {
159
- full = distance;
160
- distance = undefined;
161
- }
162
-
163
- return $.sonar( this[0], distance, full );
164
- };
165
-
166
- var body = doc.body,
167
- $win = $(win),
168
-
169
- onScreenEvent = "scrollin",
170
- offScreenEvent = "scrollout",
171
-
172
- detect = function( elem, distance, full ){
173
-
174
- if ( elem ) {
175
-
176
- // Cache the body elem in our private global.
177
- body || ( body = doc.body );
178
-
179
- var parentElem = elem, // Clone the elem for use in our loop.
180
-
181
- elemTop = 0, // The resets the calculated elem top to 0.
182
-
183
- // Used to recalculate elem.sonarElemTop if body height changes.
184
- bodyHeight = body.offsetHeight,
185
-
186
- // NCZ: I don't think you need innerHeight, I believe all major browsers support clientHeight.
187
- screenHeight = win.innerHeight || doc.documentElement.clientHeight || body.clientHeight || 0, // Height of the screen.
188
-
189
- // NCZ: I don't think you need pageYOffset, I believe all major browsers support scrollTop.
190
- scrollTop = doc.documentElement.scrollTop || win.pageYOffset || body.scrollTop || 0, // How far the user scrolled down.
191
- elemHeight = elem.offsetHeight || 0; // Height of the element.
192
-
193
- // If our custom "sonarTop" variable is undefined, or the document body
194
- // height has changed since the last time we ran sonar.detect()...
195
- if ( !elem.sonarElemTop || elem.sonarBodyHeight !== bodyHeight ) {
196
-
197
- // Loop through the offsetParents to calculate it.
198
- if ( parentElem.offsetParent ) {
199
- do {
200
- elemTop += parentElem.offsetTop;
201
- }
202
- while ( parentElem = parentElem.offsetParent );
203
- }
204
-
205
- // Set the custom property (sonarTop) to avoid future attempts to calculate
206
- // the distance on this elem from the top of the page.
207
- elem.sonarElemTop = elemTop;
208
-
209
- // Along the same lines, store the body height when we calculated
210
- // the elem's top.
211
- elem.sonarBodyHeight = bodyHeight;
212
- }
213
-
214
- // If no distance was given, assume 0.
215
- distance = distance === undefined ? 0 : distance;
216
-
217
- // Dump all calculated variables.
218
- /*
219
- console.dir({
220
- elem: elem,
221
- sonarElemTop: elem.sonarElemTop,
222
- elemHeight: elemHeight,
223
- scrollTop: scrollTop,
224
- screenHeight: screenHeight,
225
- distance: distance,
226
- full: full
227
- });
228
- */
229
-
230
- // If elem bottom is above the screen top and
231
- // the elem top is below the screen bottom, it's false.
232
- // If full is specified, it si subtracted or added
233
- // as needed from the element's height.
234
- return (!(elem.sonarElemTop + (full ? 0 : elemHeight) < scrollTop - distance) &&
235
- !(elem.sonarElemTop + (full ? elemHeight : 0) > scrollTop + screenHeight + distance));
236
- }
237
- },
238
-
239
- // Container for elems needing to be polled.
240
- pollQueue = {},
241
-
242
- // Indicates if scroll events are bound to the poll.
243
- pollActive = 0,
244
-
245
- // Used for debouncing.
246
- pollId,
247
-
248
- // Function that handles polling when the user scrolls.
249
- poll = function(){
250
-
251
- // Debouncing speed optimization. Essentially prevents
252
- // poll requests from queue'ing up and overloading
253
- // the scroll event listener.
254
- pollId && clearTimeout( pollId );
255
- pollId = setTimeout(function(){
256
-
257
- var elem,
258
- elems,
259
- screenEvent,
260
- options,
261
- detected,
262
- i, l;
263
-
264
- for ( screenEvent in pollQueue ) {
265
-
266
- elems = pollQueue[ screenEvent ];
267
-
268
- for (i = 0, l = elems.length; i < l; i++) {
269
-
270
- options = elems[i];
271
- elem = options.elem;
272
-
273
- // console.log("Polling " + elem.id);
274
-
275
- detected = detect( elem, options.px, options.full );
276
-
277
- // If the elem is not detected (offscreen) or detected (onscreen)
278
- // remove the elem from the queue and fire the callback.
279
- if ( screenEvent === offScreenEvent ? !detected : detected ) {
280
- // // console.log(screenEvent);
281
- if (!options.tr) {
282
-
283
- if ( elem[ '_' + screenEvent ] ) {
284
- // console.log("triggered:" + elem.id);
285
- // Trigger the onscreen or offscreen event depending
286
- // on the desired event.
287
- $(elem).triggerHandler( screenEvent );
288
-
289
- options.tr = 1;
290
-
291
- // removeSonar was called on this element, clean it up
292
- // instead of triggering the event.
293
- } else {
294
- // console.log("Deleting " + elem.id);
295
-
296
- // Remove this object from the elem poll container.
297
- elems.splice(i, 1);
298
-
299
- // Decrement the counter and length because we just removed
300
- // one from it.
301
- i--;
302
- l--;
303
- }
304
- }
305
- } else {
306
- options.tr = 0;
307
- }
308
- }
309
- }
310
-
311
- }, 0 ); // End setTimeout performance tweak.
312
- },
313
-
314
- removeSonar = function( elem, screenEvent ){
315
- // console.log("Removing " + elem.id);
316
- elem[ '_' + screenEvent ] = 0;
317
- },
318
-
319
- addSonar = function( elem, options ) {
320
- // console.log("Really adding " + elem.id);
321
- // Prepare arguments.
322
- var distance = options.px,
323
- full = options.full,
324
- screenEvent = options.evt,
325
- parent = win, // Getting ready to accept parents: options.parent || win,
326
- detected = detect( elem, distance, full /*, parent */ ),
327
- triggered = 0;
328
-
329
- elem[ '_' + screenEvent ] = 1;
330
-
331
- // If the elem is not detected (offscreen) or detected (onscreen)
332
- // trigger the event and fire the callback immediately.
333
- if ( screenEvent === offScreenEvent ? !detected : detected ) {
334
- // console.log("Triggering " + elem.id + " " + screenEvent );
335
- // Trigger the onscreen event at the next possible cycle.
336
- // Artz: Ask the jQuery team why I needed to do this.
337
- setTimeout(function(){
338
- $(elem).triggerHandler( screenEvent === offScreenEvent ? offScreenEvent : onScreenEvent );
339
- }, 0);
340
- triggered = 1;
341
- // Otherwise, add it to the polling queue.
342
- }
343
-
344
- // console.log("Adding " + elem.id + " to queue.");
345
- // Push the element and its callback into the poll queue.
346
- pollQueue[ screenEvent ].push({
347
- elem: elem,
348
- px: distance,
349
- full: full,
350
- tr: triggered/* ,
351
- parent: parent */
352
- });
353
-
354
- // Activate the poll if not currently activated.
355
- if ( !pollActive ) {
356
- $win.bind( "scroll", poll );
357
- pollActive = 1;
358
- }
359
-
360
-
361
- // Call the prepare function if there, used to
362
- // prepare the element if we detected it.
363
- // Artz: Not implemented yet...used to preprocess elements in same loop.
364
- /*
365
- if ( prepCallback ) {
366
- prepCallback.call( elem, elem, detected );
367
- }
368
- */
369
- };
370
-
371
- // Open sonar function up to the public.
372
- $.sonar = detect;
373
-
374
- pollQueue[ onScreenEvent ] = [];
375
- $.event.special[ onScreenEvent ] = {
376
-
377
- add: function( handleObj ) {
378
- var data = handleObj.data || {},
379
- elem = this;
380
-
381
- if (!elem[onScreenEvent]){
382
- addSonar(this, {
383
- px: data.distance,
384
- full: data.full,
385
- evt: onScreenEvent /*,
386
- parent: data.parent */
387
- });
388
- }
389
- },
390
-
391
- remove: function( handleObj ) {
392
- removeSonar( this, onScreenEvent );
393
- }
394
-
395
- };
396
-
397
- pollQueue[ offScreenEvent ] = [];
398
- $.event.special[ offScreenEvent ] = {
399
-
400
- add: function( handleObj ) {
401
-
402
- var data = handleObj.data || {},
403
- elem = this;
404
-
405
- if (!elem[offScreenEvent]){
406
- addSonar(elem, {
407
- px: data.distance,
408
- full: data.full,
409
- evt: offScreenEvent /*,
410
- parent: data.parent */
411
- });
412
- }
413
- },
414
-
415
- remove: function( handleObj ) {
416
- removeSonar( this, offScreenEvent );
417
- }
418
- };
419
-
420
- // console.log(pollQueue);
421
- })( jQuery, window, document );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
lang/bj-lazy-load-he_IL.mo ADDED
Binary file
lang/bj-lazy-load-he_IL.po ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ msgid ""
2
+ msgstr ""
3
+ "MIME-Version: 1.0\n"
4
+ "Content-Type: text/plain; charset=UTF-8\n"
5
+ "Content-Transfer-Encoding: 8bit\n"
6
+ "X-Generator: Poedit 1.7.6\n"
7
+ "Project-Id-Version: BJ Lazy Load\n"
8
+ "Language: he\n"
9
+ "POT-Creation-Date: \n"
10
+ "PO-Revision-Date: \n"
11
+ "Last-Translator: שגיב SEO <Info@sagive.co.il>\n"
12
+ "Language-Team: Sagive SEO <info@sagive.co.il>\n"
13
+ "Plural-Forms: nplurals=2; plural=(n != 1);\n"
14
+
15
+ #: admin.php:8
16
+ msgid "BJ Lazy Load Options"
17
+ msgstr "אפשרויות תוסף BJ Lazy Load"
18
+
19
+ #: admin.php:17
20
+ msgid "Apply to content"
21
+ msgstr "החל על התוכן"
22
+
23
+ #: admin.php:20 admin.php:26 admin.php:32 admin.php:38 admin.php:44
24
+ #: admin.php:76 admin.php:84 admin.php:92 admin.php:102 admin.php:112
25
+ msgid "Yes"
26
+ msgstr "כן"
27
+
28
+ #: admin.php:20 admin.php:26 admin.php:32 admin.php:38 admin.php:44
29
+ #: admin.php:76 admin.php:84 admin.php:92 admin.php:102 admin.php:112
30
+ msgid "No"
31
+ msgstr "לא"
32
+
33
+ #: admin.php:23
34
+ msgid "Apply to post thumbnails"
35
+ msgstr "החל על תמונות פוסט ראשיות (Thumbnails)"
36
+
37
+ #: admin.php:29
38
+ msgid "Apply to gravatars"
39
+ msgstr "החל על תמונות משתמש (גראבטר)"
40
+
41
+ #: admin.php:35
42
+ msgid "Lazy load images"
43
+ msgstr "החל על תמונות"
44
+
45
+ #: admin.php:41
46
+ msgid "Lazy load iframes"
47
+ msgstr "החל על איפריימים"
48
+
49
+ #: admin.php:47
50
+ msgid "Theme loader function"
51
+ msgstr "מיקום הפונקציה"
52
+
53
+ #: admin.php:53
54
+ msgid "Placeholder Image URL"
55
+ msgstr "תמונת ממלאת מקום"
56
+
57
+ #: admin.php:56
58
+ msgid "Leave blank for default"
59
+ msgstr "השאירו ריק כדי להשתמש באפשרות ברירת מחדל"
60
+
61
+ #: admin.php:59
62
+ msgid "Skip images with classes"
63
+ msgstr "לדלג על תמונות עם הקלאס"
64
+
65
+ #: admin.php:62
66
+ msgid "Comma separated. Example: \"no-lazy, lazy-ignore, image-235\""
67
+ msgstr ""
68
+ "רשימה מופרדת בעזרת פסיקים - לדוגמה: \"no-lazy, lazy-ignore, image-235\""
69
+
70
+ #: admin.php:65
71
+ msgid "Threshold"
72
+ msgstr "נקודת הפעלה"
73
+
74
+ #: admin.php:68
75
+ msgid ""
76
+ "How close to the viewport the element should be when we load it. In pixels. "
77
+ "Example: 200"
78
+ msgstr ""
79
+ "מה המרחק של חלון התצוגה מהאלמנט (תמונה / איפריי) בה תתחיל טעינה של האלמנט"
80
+
81
+ #: admin.php:73
82
+ msgid "Infinite scroll"
83
+ msgstr "גלילה אינסופית"
84
+
85
+ #: admin.php:77
86
+ msgid "Enable if your theme uses infinite scroll."
87
+ msgstr "בחרו אפשרות זאת אם התבנית שלכם משתמשת בגלילה אינסופית"
88
+
89
+ #: admin.php:81
90
+ msgid "Load hiDPI (retina) images"
91
+ msgstr "טען תמונות hiDPI (באיכות גבוהה)"
92
+
93
+ #: admin.php:85
94
+ msgid ""
95
+ "Will load hiDPI version of the images if the current browser/screen supports "
96
+ "them. (Experimental feature. Do NOT enable if you are using a CDN)"
97
+ msgstr ""
98
+ "יאפשר טעינה של תמונות באיכות hiDPI אם הדפדפן / המכשיר שלכם תומך בהם. (תכונה "
99
+ "ניסיונית. אל תאפשרו אם אתם משתמשים בCDN)"
100
+
101
+ #: admin.php:89
102
+ msgid "Load responsive images"
103
+ msgstr "טען תמונות רספונסיביות"
104
+
105
+ #: admin.php:93
106
+ msgid ""
107
+ "Will load scaled down version of the images if the image is scaled down in "
108
+ "the theme. (Experimental feature. Do NOT enable if you are using a CDN)"
109
+ msgstr ""
110
+ "יטען גרסאות מוקטנות של התמונות אם קיימת גרסה מוקטנת באתר שלכם. (תכונה "
111
+ "ניסיונית. אל תאפשרו אם אתם משתמשים בCDN)"
112
+
113
+ #: admin.php:99
114
+ msgid "Disable on WPTouch"
115
+ msgstr "כבה בWPTouch"
116
+
117
+ #: admin.php:103
118
+ msgid "Disables BJ Lazy Load when the WPTouch mobile theme is used"
119
+ msgstr "כבה את BJ Lazy Load כאשר אתם הגולש צופה בגרסת WPTouch של האתר שלכם."
120
+
121
+ #: admin.php:109
122
+ msgid "Disable on MobilePress"
123
+ msgstr "כבה בMobilePress"
124
+
125
+ #: admin.php:113
126
+ msgid "Disables BJ Lazy Load when the MobilePress mobile theme is used"
127
+ msgstr "כבה את MobilePress כאשר אתם הגולש צופה בגרסת MobilePress של האתר שלכם."
128
+
129
+ #: inc/class-bjll-skip-post.php:37
130
+ msgid "Lazy Loading"
131
+ msgstr "טעינה מושהית"
132
+
133
+ #: inc/class-bjll-skip-post.php:50
134
+ msgid "Skip lazy loading for this page"
135
+ msgstr "כבה טעינה מושהית (Lazy Loading) בדףזה"
136
+
137
+ #: inc/class-bjll-skip-post.php:50
138
+ msgid "Skip lazy loading for this post"
139
+ msgstr "כבה טעינה מושהית (Lazy Loading) בפוסט זה"
140
+
141
+ #: scb/AdminPage.php:227
142
+ msgid "Settings <strong>saved</strong>."
143
+ msgstr "ההגדרות <strong>נשמרו</strong>."
144
+
145
+ #: scb/AdminPage.php:466
146
+ msgid "Settings"
147
+ msgstr "הגדרות"
148
+
149
+ #. Plugin Name of the plugin/theme
150
+ msgid "BJ Lazy Load"
151
+ msgstr "BJ Lazy Load"
152
+
153
+ #. Plugin URI of the plugin/theme
154
+ msgid "http://wordpress.org/extend/plugins/bj-lazy-load/"
155
+ msgstr "http://wordpress.org/extend/plugins/bj-lazy-load/"
156
+
157
+ #. Description of the plugin/theme
158
+ msgid "Lazy image loading makes your site load faster and saves bandwidth."
159
+ msgstr "טעינה מושהית של תמונות עוזרת להאיץ את האתר שלכם ולחסוך תעבורה"
160
+
161
+ #. Author of the plugin/theme
162
+ msgid "Bjørn Johansen"
163
+ msgstr "Bjørn Johansen"
164
+
165
+ #. Author URI of the plugin/theme
166
+ msgid "http://twitter.com/bjornjohansen"
167
+ msgstr "http://twitter.com/bjornjohansen"
readme.txt CHANGED
@@ -1,13 +1,13 @@
1
  === BJ Lazy Load ===
2
  Contributors: bjornjohansen
3
  Donate link: http://www.kiva.org/
4
- Tags: images, iframes, lazy loading, jquery, javascript, optimize, performance, bandwidth, responsive design, hidpi, retina
5
  Author URI: http://twitter.com/bjornjohansen
6
  Requires at least: 3.5
7
  Tested up to: 4.3
8
- Stable tag: 0.7.5
9
 
10
- Lazy loading makes your site load faster and saves bandwidth. Uses jQuery and degrades gracefully for non-js users. Works with both images and iframes.
11
 
12
  == Description ==
13
  Lazy loading makes your site load faster and saves bandwidth.
@@ -18,11 +18,14 @@ You can also lazy load other images and iframes in your theme, by using a simple
18
 
19
  Non-javascript visitors gets the original element in noscript.
20
 
21
- = Size optimized images =
22
- * Automaticly serve scaled down images in responsive designs
23
- * Automaticly serve hiDPI images for hiDPI screens (like Apples Retina Display)
24
 
25
- Please let me know if you have any issues. Fastest way to get a response is by Twitter: http://twitter.com/bjornjohansen
 
 
 
 
 
26
 
27
  == Installation ==
28
  1. Download and unzip plugin
@@ -46,22 +49,25 @@ Note for developers: The filter has a priority of 10.
46
  = Whoa, this plugin is using JavaScript. What about visitors without JS? =
47
  No worries. They get the original element in a noscript element. No Lazy Loading for them, though.
48
 
49
- = Which browsers are supported? =
50
- The included JavaScript is tested in Firefox 2+, Safari 3+, Opera 9+, Chrome 5+, Internet Explorer 6+
51
-
52
  = I'm using a CDN. Will this plugin interfere? =
53
  Lazy loading works just fine. The images will still load from your CDN.
54
 
55
  As of version 0.6.0, serving responsive and hiDPI images will not work if you're using a CDN. Pull zones will be supported in the near future.
56
 
57
- = The plugin doesn't work/doesn't replace my images =
58
- Probably, your theme does not call wp_footer(). Edit the plugin settings to load in wp_head() instead.
59
-
60
  = How can I verify that the plugin is working? =
61
  Check your HTML source or see the magic at work in Web Inspector, FireBug or similar.
62
 
63
  == Changelog ==
64
 
 
 
 
 
 
 
 
 
 
65
  = Version 0.7.5 =
66
  * Also applies to text widgets (Thanks to Sigurður Guðbrandsson)
67
 
1
  === BJ Lazy Load ===
2
  Contributors: bjornjohansen
3
  Donate link: http://www.kiva.org/
4
+ Tags: images, iframes, lazy loading, javascript, optimize, performance, bandwidth
5
  Author URI: http://twitter.com/bjornjohansen
6
  Requires at least: 3.5
7
  Tested up to: 4.3
8
+ Stable tag: 1.0
9
 
10
+ Lazy loading makes your site load faster and saves bandwidth. Uses no external JS libraries and degrades gracefully for non-js users. Works with both images and iframes (like embedded YouTube videos).
11
 
12
  == Description ==
13
  Lazy loading makes your site load faster and saves bandwidth.
18
 
19
  Non-javascript visitors gets the original element in noscript.
20
 
21
+ Compatible with the <a href="https://wordpress.org/plugins/ricg-responsive-images/">RICG Responsive Images</a> plugin for responsive images.
 
 
22
 
23
+ Please let me know if you have any issues. Fastest way to get a response is by Twitter: https://twitter.com/bjornjohansen
24
+
25
+ = Translations =
26
+ * Russian (ru_RU) by Elvisrk
27
+ * Hebrew (he_IL) by Imri Sagive
28
+ * Norwegian Bokmål (nb_NO) by Bjørn Johansen
29
 
30
  == Installation ==
31
  1. Download and unzip plugin
49
  = Whoa, this plugin is using JavaScript. What about visitors without JS? =
50
  No worries. They get the original element in a noscript element. No Lazy Loading for them, though.
51
 
 
 
 
52
  = I'm using a CDN. Will this plugin interfere? =
53
  Lazy loading works just fine. The images will still load from your CDN.
54
 
55
  As of version 0.6.0, serving responsive and hiDPI images will not work if you're using a CDN. Pull zones will be supported in the near future.
56
 
 
 
 
57
  = How can I verify that the plugin is working? =
58
  Check your HTML source or see the magic at work in Web Inspector, FireBug or similar.
59
 
60
  == Changelog ==
61
 
62
+ = Version 1.0 =
63
+ * Internal rewrite. Code cleanup.
64
+ * Supports 3rd party filters
65
+ * Supports most infinite scroll (or content lazy loading) plugins (all that triggers post-load)
66
+ * No more TimThumb
67
+ * No more dependant on 3rd party JS, not even jQuery
68
+ * Removed custom responsice/hidpi image handling in favour of compatibility with the RICG Responsive Images plugin
69
+ * Added translations to Hebrew (he_IL) by Imri Sagive
70
+
71
  = Version 0.7.5 =
72
  * Also applies to text widgets (Thanks to Sigurður Guðbrandsson)
73
 
scb/index.html DELETED
File without changes
thumb.php DELETED
@@ -1,25 +0,0 @@
1
- <?php
2
-
3
- /*
4
-
5
- Yeah, we're using TimThumb!
6
-
7
- Are you worried about the security because you heard that TimThumb is unsecure?
8
- There was a major security flaw published in August 2011. Since then, TimThumb got rewritten from the ground up with version 2.0.
9
-
10
- References:
11
- Mark Maunder who rewrote TimThumb as WordThumb: http://markmaunder.com/2011/08/04/a-secure-rewrite-of-timthumb-php-as-wordthumb/
12
- Matt Mullenweg, creator of WordPress, on the whole issue: http://ma.tt/2011/08/the-timthumb-saga/
13
-
14
- */
15
-
16
- define( 'FILE_CACHE_DIRECTORY', dirname( __FILE__ ) . '/../cache' );
17
- define( 'FILE_CACHE_MAX_FILE_AGE', 2592000 );
18
- define( 'FILE_CACHE_SUFFIX', '.img' );
19
- define( 'FILE_CACHE_PREFIX', 'thumb' );
20
- define( 'MAX_FILE_SIZE', 20971520 );
21
- define( 'BROWSER_CACHE_MAX_AGE', 31536000 );
22
- define( 'MAX_WIDTH', 3000 );
23
- define( 'MAX_HEIGHT', 3000 );
24
-
25
- require( dirname( __FILE__ ) . '/inc/timthumb.php' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
timthumb.php DELETED
@@ -1,1251 +0,0 @@
1
- <?php
2
- /**
3
- * TimThumb by Ben Gillbanks and Mark Maunder
4
- * Based on work done by Tim McDaniels and Darren Hoyt
5
- * http://code.google.com/p/timthumb/
6
- *
7
- * GNU General Public License, version 2
8
- * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
9
- *
10
- * Examples and documentation available on the project homepage
11
- * http://www.binarymoon.co.uk/projects/timthumb/
12
- *
13
- * $Rev$
14
- */
15
-
16
- /*
17
- * --- TimThumb CONFIGURATION ---
18
- * To edit the configs it is best to create a file called timthumb-config.php
19
- * and define variables you want to customize in there. It will automatically be
20
- * loaded by timthumb. This will save you having to re-edit these variables
21
- * everytime you download a new version
22
- */
23
- define ('VERSION', '2.8.11'); // Version of this script
24
- //Load a config file if it exists. Otherwise, use the values below
25
- if( file_exists(dirname(__FILE__) . '/timthumb-config.php')) require_once('timthumb-config.php');
26
- if(! defined('DEBUG_ON') ) define ('DEBUG_ON', false); // Enable debug logging to web server error log (STDERR)
27
- if(! defined('DEBUG_LEVEL') ) define ('DEBUG_LEVEL', 1); // Debug level 1 is less noisy and 3 is the most noisy
28
- if(! defined('MEMORY_LIMIT') ) define ('MEMORY_LIMIT', '30M'); // Set PHP memory limit
29
- if(! defined('BLOCK_EXTERNAL_LEECHERS') ) define ('BLOCK_EXTERNAL_LEECHERS', false); // If the image or webshot is being loaded on an external site, display a red "No Hotlinking" gif.
30
-
31
- //Image fetching and caching
32
- if(! defined('ALLOW_EXTERNAL') ) define ('ALLOW_EXTERNAL', TRUE); // Allow image fetching from external websites. Will check against ALLOWED_SITES if ALLOW_ALL_EXTERNAL_SITES is false
33
- if(! defined('ALLOW_ALL_EXTERNAL_SITES') ) define ('ALLOW_ALL_EXTERNAL_SITES', false); // Less secure.
34
- if(! defined('FILE_CACHE_ENABLED') ) define ('FILE_CACHE_ENABLED', TRUE); // Should we store resized/modified images on disk to speed things up?
35
- if(! defined('FILE_CACHE_TIME_BETWEEN_CLEANS')) define ('FILE_CACHE_TIME_BETWEEN_CLEANS', 86400); // How often the cache is cleaned
36
-
37
- if(! defined('FILE_CACHE_MAX_FILE_AGE') ) define ('FILE_CACHE_MAX_FILE_AGE', 86400); // How old does a file have to be to be deleted from the cache
38
- if(! defined('FILE_CACHE_SUFFIX') ) define ('FILE_CACHE_SUFFIX', '.timthumb.txt'); // What to put at the end of all files in the cache directory so we can identify them
39
- if(! defined('FILE_CACHE_PREFIX') ) define ('FILE_CACHE_PREFIX', 'timthumb'); // What to put at the beg of all files in the cache directory so we can identify them
40
- if(! defined('FILE_CACHE_DIRECTORY') ) define ('FILE_CACHE_DIRECTORY', './cache'); // Directory where images are cached. Left blank it will use the system temporary directory (which is better for security)
41
- if(! defined('MAX_FILE_SIZE') ) define ('MAX_FILE_SIZE', 10485760); // 10 Megs is 10485760. This is the max internal or external file size that we'll process.
42
- if(! defined('CURL_TIMEOUT') ) define ('CURL_TIMEOUT', 20); // Timeout duration for Curl. This only applies if you have Curl installed and aren't using PHP's default URL fetching mechanism.
43
- if(! defined('WAIT_BETWEEN_FETCH_ERRORS') ) define ('WAIT_BETWEEN_FETCH_ERRORS', 3600); // Time to wait between errors fetching remote file
44
-
45
- //Browser caching
46
- if(! defined('BROWSER_CACHE_MAX_AGE') ) define ('BROWSER_CACHE_MAX_AGE', 864000); // Time to cache in the browser
47
- if(! defined('BROWSER_CACHE_DISABLE') ) define ('BROWSER_CACHE_DISABLE', false); // Use for testing if you want to disable all browser caching
48
-
49
- //Image size and defaults
50
- if(! defined('MAX_WIDTH') ) define ('MAX_WIDTH', 1500); // Maximum image width
51
- if(! defined('MAX_HEIGHT') ) define ('MAX_HEIGHT', 1500); // Maximum image height
52
- if(! defined('NOT_FOUND_IMAGE') ) define ('NOT_FOUND_IMAGE', ''); // Image to serve if any 404 occurs
53
- if(! defined('ERROR_IMAGE') ) define ('ERROR_IMAGE', ''); // Image to serve if an error occurs instead of showing error message
54
- if(! defined('PNG_IS_TRANSPARENT') ) define ('PNG_IS_TRANSPARENT', FALSE); // Define if a png image should have a transparent background color. Use False value if you want to display a custom coloured canvas_colour
55
- if(! defined('DEFAULT_Q') ) define ('DEFAULT_Q', 90); // Default image quality. Allows overrid in timthumb-config.php
56
- if(! defined('DEFAULT_ZC') ) define ('DEFAULT_ZC', 1); // Default zoom/crop setting. Allows overrid in timthumb-config.php
57
- if(! defined('DEFAULT_F') ) define ('DEFAULT_F', ''); // Default image filters. Allows overrid in timthumb-config.php
58
- if(! defined('DEFAULT_S') ) define ('DEFAULT_S', 0); // Default sharpen value. Allows overrid in timthumb-config.php
59
- if(! defined('DEFAULT_CC') ) define ('DEFAULT_CC', 'ffffff'); // Default canvas colour. Allows overrid in timthumb-config.php
60
-
61
-
62
- //Image compression is enabled if either of these point to valid paths
63
-
64
- //These are now disabled by default because the file sizes of PNGs (and GIFs) are much smaller than we used to generate.
65
- //They only work for PNGs. GIFs and JPEGs are not affected.
66
- if(! defined('OPTIPNG_ENABLED') ) define ('OPTIPNG_ENABLED', false);
67
- if(! defined('OPTIPNG_PATH') ) define ('OPTIPNG_PATH', '/usr/bin/optipng'); //This will run first because it gives better compression than pngcrush.
68
- if(! defined('PNGCRUSH_ENABLED') ) define ('PNGCRUSH_ENABLED', false);
69
- if(! defined('PNGCRUSH_PATH') ) define ('PNGCRUSH_PATH', '/usr/bin/pngcrush'); //This will only run if OPTIPNG_PATH is not set or is not valid
70
-
71
- /*
72
- -------====Website Screenshots configuration - BETA====-------
73
-
74
- If you just want image thumbnails and don't want website screenshots, you can safely leave this as is.
75
-
76
- If you would like to get website screenshots set up, you will need root access to your own server.
77
-
78
- Enable ALLOW_ALL_EXTERNAL_SITES so you can fetch any external web page. This is more secure now that we're using a non-web folder for cache.
79
- Enable BLOCK_EXTERNAL_LEECHERS so that your site doesn't generate thumbnails for the whole Internet.
80
-
81
- Instructions to get website screenshots enabled on Ubuntu Linux:
82
-
83
- 1. Install Xvfb with the following command: sudo apt-get install subversion libqt4-webkit libqt4-dev g++ xvfb
84
- 2. Go to a directory where you can download some code
85
- 3. Check-out the latest version of CutyCapt with the following command: svn co https://cutycapt.svn.sourceforge.net/svnroot/cutycapt
86
- 4. Compile CutyCapt by doing: cd cutycapt/CutyCapt
87
- 5. qmake
88
- 6. make
89
- 7. cp CutyCapt /usr/local/bin/
90
- 8. Test it by running: xvfb-run --server-args="-screen 0, 1024x768x24" CutyCapt --url="http://markmaunder.com/" --out=test.png
91
- 9. If you get a file called test.png with something in it, it probably worked. Now test the script by accessing it as follows:
92
- 10. http://yoursite.com/path/to/timthumb.php?src=http://markmaunder.com/&webshot=1
93
-
94
- Notes on performance:
95
- The first time a webshot loads, it will take a few seconds.
96
- From then on it uses the regular timthumb caching mechanism with the configurable options above
97
- and loading will be very fast.
98
-
99
- --ADVANCED USERS ONLY--
100
- If you'd like a slight speedup (about 25%) and you know Linux, you can run the following command which will keep Xvfb running in the background.
101
- nohup Xvfb :100 -ac -nolisten tcp -screen 0, 1024x768x24 > /dev/null 2>&1 &
102
- Then set WEBSHOT_XVFB_RUNNING = true below. This will save your server having to fire off a new Xvfb server and shut it down every time a new shot is generated.
103
- You will need to take responsibility for keeping Xvfb running in case it crashes. (It seems pretty stable)
104
- You will also need to take responsibility for server security if you're running Xvfb as root.
105
-
106
-
107
- */
108
- if(! defined('WEBSHOT_ENABLED') ) define ('WEBSHOT_ENABLED', false); //Beta feature. Adding webshot=1 to your query string will cause the script to return a browser screenshot rather than try to fetch an image.
109
- if(! defined('WEBSHOT_CUTYCAPT') ) define ('WEBSHOT_CUTYCAPT', '/usr/local/bin/CutyCapt'); //The path to CutyCapt.
110
- if(! defined('WEBSHOT_XVFB') ) define ('WEBSHOT_XVFB', '/usr/bin/xvfb-run'); //The path to the Xvfb server
111
- if(! defined('WEBSHOT_SCREEN_X') ) define ('WEBSHOT_SCREEN_X', '1024'); //1024 works ok
112
- if(! defined('WEBSHOT_SCREEN_Y') ) define ('WEBSHOT_SCREEN_Y', '768'); //768 works ok
113
- if(! defined('WEBSHOT_COLOR_DEPTH') ) define ('WEBSHOT_COLOR_DEPTH', '24'); //I haven't tested anything besides 24
114
- if(! defined('WEBSHOT_IMAGE_FORMAT') ) define ('WEBSHOT_IMAGE_FORMAT', 'png'); //png is about 2.5 times the size of jpg but is a LOT better quality
115
- if(! defined('WEBSHOT_TIMEOUT') ) define ('WEBSHOT_TIMEOUT', '20'); //Seconds to wait for a webshot
116
- if(! defined('WEBSHOT_USER_AGENT') ) define ('WEBSHOT_USER_AGENT', "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18"); //I hate to do this, but a non-browser robot user agent might not show what humans see. So we pretend to be Firefox
117
- if(! defined('WEBSHOT_JAVASCRIPT_ON') ) define ('WEBSHOT_JAVASCRIPT_ON', true); //Setting to false might give you a slight speedup and block ads. But it could cause other issues.
118
- if(! defined('WEBSHOT_JAVA_ON') ) define ('WEBSHOT_JAVA_ON', false); //Have only tested this as fase
119
- if(! defined('WEBSHOT_PLUGINS_ON') ) define ('WEBSHOT_PLUGINS_ON', true); //Enable flash and other plugins
120
- if(! defined('WEBSHOT_PROXY') ) define ('WEBSHOT_PROXY', ''); //In case you're behind a proxy server.
121
- if(! defined('WEBSHOT_XVFB_RUNNING') ) define ('WEBSHOT_XVFB_RUNNING', false); //ADVANCED: Enable this if you've got Xvfb running in the background.
122
-
123
-
124
- // 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.
125
- if(! isset($ALLOWED_SITES)){
126
- $ALLOWED_SITES = array (
127
- 'flickr.com',
128
- 'staticflickr.com',
129
- 'picasa.com',
130
- 'img.youtube.com',
131
- 'upload.wikimedia.org',
132
- 'photobucket.com',
133
- 'imgur.com',
134
- 'imageshack.us',
135
- 'tinypic.com',
136
- );
137
- }
138
- // -------------------------------------------------------------
139
- // -------------- STOP EDITING CONFIGURATION HERE --------------
140
- // -------------------------------------------------------------
141
-
142
- timthumb::start();
143
-
144
- class timthumb {
145
- protected $src = "";
146
- protected $is404 = false;
147
- protected $docRoot = "";
148
- protected $lastURLError = false;
149
- protected $localImage = "";
150
- protected $localImageMTime = 0;
151
- protected $url = false;
152
- protected $myHost = "";
153
- protected $isURL = false;
154
- protected $cachefile = '';
155
- protected $errors = array();
156
- protected $toDeletes = array();
157
- protected $cacheDirectory = '';
158
- protected $startTime = 0;
159
- protected $lastBenchTime = 0;
160
- protected $cropTop = false;
161
- protected $salt = "";
162
- protected $fileCacheVersion = 1; //Generally if timthumb.php is modifed (upgraded) then the salt changes and all cache files are recreated. This is a backup mechanism to force regen.
163
- protected $filePrependSecurityBlock = "<?php die('Execution denied!'); //"; //Designed to have three letter mime type, space, question mark and greater than symbol appended. 6 bytes total.
164
- protected static $curlDataWritten = 0;
165
- protected static $curlFH = false;
166
- public static function start(){
167
- $tim = new timthumb();
168
- $tim->handleErrors();
169
- $tim->securityChecks();
170
- if($tim->tryBrowserCache()){
171
- exit(0);
172
- }
173
- $tim->handleErrors();
174
- if(FILE_CACHE_ENABLED && $tim->tryServerCache()){
175
- exit(0);
176
- }
177
- $tim->handleErrors();
178
- $tim->run();
179
- $tim->handleErrors();
180
- exit(0);
181
- }
182
- public function __construct(){
183
- global $ALLOWED_SITES;
184
- $this->startTime = microtime(true);
185
- date_default_timezone_set('UTC');
186
- $this->debug(1, "Starting new request from " . $this->getIP() . " to " . $_SERVER['REQUEST_URI']);
187
- $this->calcDocRoot();
188
- //On windows systems I'm assuming fileinode returns an empty string or a number that doesn't change. Check this.
189
- $this->salt = @filemtime(__FILE__) . '-' . @fileinode(__FILE__);
190
- $this->debug(3, "Salt is: " . $this->salt);
191
- if(FILE_CACHE_DIRECTORY){
192
- if(! is_dir(FILE_CACHE_DIRECTORY)){
193
- @mkdir(FILE_CACHE_DIRECTORY);
194
- if(! is_dir(FILE_CACHE_DIRECTORY)){
195
- $this->error("Could not create the file cache directory.");
196
- return false;
197
- }
198
- }
199
- $this->cacheDirectory = FILE_CACHE_DIRECTORY;
200
- if (!touch($this->cacheDirectory . '/index.html')) {
201
- $this->error("Could not create the index.html file - to fix this create an empty file named index.html file in the cache directory.");
202
- }
203
- } else {
204
- $this->cacheDirectory = sys_get_temp_dir();
205
- }
206
- //Clean the cache before we do anything because we don't want the first visitor after FILE_CACHE_TIME_BETWEEN_CLEANS expires to get a stale image.
207
- $this->cleanCache();
208
-
209
- $this->myHost = preg_replace('/^www\./i', '', $_SERVER['HTTP_HOST']);
210
- $this->src = $this->param('src');
211
- $this->url = parse_url($this->src);
212
- $this->src = preg_replace('/https?:\/\/(?:www\.)?' . $this->myHost . '/i', '', $this->src);
213
-
214
- if(strlen($this->src) <= 3){
215
- $this->error("No image specified");
216
- return false;
217
- }
218
- if(BLOCK_EXTERNAL_LEECHERS && array_key_exists('HTTP_REFERER', $_SERVER) && (! preg_match('/^https?:\/\/(?:www\.)?' . $this->myHost . '(?:$|\/)/i', $_SERVER['HTTP_REFERER']))){
219
- // base64 encoded red image that says 'no hotlinkers'
220
- // nothing to worry about! :)
221
- $imgData = base64_decode("R0lGODlhUAAMAIAAAP8AAP///yH5BAAHAP8ALAAAAABQAAwAAAJpjI+py+0Po5y0OgAMjjv01YUZ\nOGplhWXfNa6JCLnWkXplrcBmW+spbwvaVr/cDyg7IoFC2KbYVC2NQ5MQ4ZNao9Ynzjl9ScNYpneb\nDULB3RP6JuPuaGfuuV4fumf8PuvqFyhYtjdoeFgAADs=");
222
- header('Content-Type: image/gif');
223
- header('Content-Length: ' . sizeof($imgData));
224
- header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
225
- header("Pragma: no-cache");
226
- header('Expires: ' . gmdate ('D, d M Y H:i:s', time()));
227
- echo $imgData;
228
- return false;
229
- exit(0);
230
- }
231
- if(preg_match('/^https?:\/\/[^\/]+/i', $this->src)){
232
- $this->debug(2, "Is a request for an external URL: " . $this->src);
233
- $this->isURL = true;
234
- } else {
235
- $this->debug(2, "Is a request for an internal file: " . $this->src);
236
- }
237
- if($this->isURL && (! ALLOW_EXTERNAL)){
238
- $this->error("You are not allowed to fetch images from an external website.");
239
- return false;
240
- }
241
- if($this->isURL){
242
- if(ALLOW_ALL_EXTERNAL_SITES){
243
- $this->debug(2, "Fetching from all external sites is enabled.");
244
- } else {
245
- $this->debug(2, "Fetching only from selected external sites is enabled.");
246
- $allowed = false;
247
- foreach($ALLOWED_SITES as $site){
248
- if ((strtolower(substr($this->url['host'],-strlen($site)-1)) === strtolower(".$site")) || (strtolower($this->url['host'])===strtolower($site))) {
249
- $this->debug(3, "URL hostname {$this->url['host']} matches $site so allowing.");
250
- $allowed = true;
251
- }
252
- }
253
- if(! $allowed){
254
- return $this->error("You may not fetch images from that site. To enable this site in timthumb, you can either add it to \$ALLOWED_SITES and set ALLOW_EXTERNAL=true. Or you can set ALLOW_ALL_EXTERNAL_SITES=true, depending on your security needs.");
255
- }
256
- }
257
- }
258
-
259
- $cachePrefix = ($this->isURL ? '_ext_' : '_int_');
260
- if($this->isURL){
261
- $arr = explode('&', $_SERVER ['QUERY_STRING']);
262
- asort($arr);
263
- $this->cachefile = $this->cacheDirectory . '/' . FILE_CACHE_PREFIX . $cachePrefix . md5($this->salt . implode('', $arr) . $this->fileCacheVersion) . FILE_CACHE_SUFFIX;
264
- } else {
265
- $this->localImage = $this->getLocalImagePath($this->src);
266
- if(! $this->localImage){
267
- $this->debug(1, "Could not find the local image: {$this->localImage}");
268
- $this->error("Could not find the internal image you specified.");
269
- $this->set404();
270
- return false;
271
- }
272
- $this->debug(1, "Local image path is {$this->localImage}");
273
- $this->localImageMTime = @filemtime($this->localImage);
274
- //We include the mtime of the local file in case in changes on disk.
275
- $this->cachefile = $this->cacheDirectory . '/' . FILE_CACHE_PREFIX . $cachePrefix . md5($this->salt . $this->localImageMTime . $_SERVER ['QUERY_STRING'] . $this->fileCacheVersion) . FILE_CACHE_SUFFIX;
276
- }
277
- $this->debug(2, "Cache file is: " . $this->cachefile);
278
-
279
- return true;
280
- }
281
- public function __destruct(){
282
- foreach($this->toDeletes as $del){
283
- $this->debug(2, "Deleting temp file $del");
284
- @unlink($del);
285
- }
286
- }
287
- public function run(){
288
- if($this->isURL){
289
- if(! ALLOW_EXTERNAL){
290
- $this->debug(1, "Got a request for an external image but ALLOW_EXTERNAL is disabled so returning error msg.");
291
- $this->error("You are not allowed to fetch images from an external website.");
292
- return false;
293
- }
294
- $this->debug(3, "Got request for external image. Starting serveExternalImage.");
295
- if($this->param('webshot')){
296
- if(WEBSHOT_ENABLED){
297
- $this->debug(3, "webshot param is set, so we're going to take a webshot.");
298
- $this->serveWebshot();
299
- } else {
300
- $this->error("You added the webshot parameter but webshots are disabled on this server. You need to set WEBSHOT_ENABLED == true to enable webshots.");
301
- }
302
- } else {
303
- $this->debug(3, "webshot is NOT set so we're going to try to fetch a regular image.");
304
- $this->serveExternalImage();
305
-
306
- }
307
- } else {
308
- $this->debug(3, "Got request for internal image. Starting serveInternalImage()");
309
- $this->serveInternalImage();
310
- }
311
- return true;
312
- }
313
- protected function handleErrors(){
314
- if($this->haveErrors()){
315
- if(NOT_FOUND_IMAGE && $this->is404()){
316
- if($this->serveImg(NOT_FOUND_IMAGE)){
317
- exit(0);
318
- } else {
319
- $this->error("Additionally, the 404 image that is configured could not be found or there was an error serving it.");
320
- }
321
- }
322
- if(ERROR_IMAGE){
323
- if($this->serveImg(ERROR_IMAGE)){
324
- exit(0);
325
- } else {
326
- $this->error("Additionally, the error image that is configured could not be found or there was an error serving it.");
327
- }
328
- }
329
- $this->serveErrors();
330
- exit(0);
331
- }
332
- return false;
333
- }
334
- protected function tryBrowserCache(){
335
- if(BROWSER_CACHE_DISABLE){ $this->debug(3, "Browser caching is disabled"); return false; }
336
- if(!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ){
337
- $this->debug(3, "Got a conditional get");
338
- $mtime = false;
339
- //We've already checked if the real file exists in the constructor
340
- if(! is_file($this->cachefile)){
341
- //If we don't have something cached, regenerate the cached image.
342
- return false;
343
- }
344
- if($this->localImageMTime){
345
- $mtime = $this->localImageMTime;
346
- $this->debug(3, "Local real file's modification time is $mtime");
347
- } else if(is_file($this->cachefile)){ //If it's not a local request then use the mtime of the cached file to determine the 304
348
- $mtime = @filemtime($this->cachefile);
349
- $this->debug(3, "Cached file's modification time is $mtime");
350
- }
351
- if(! $mtime){ return false; }
352
-
353
- $iftime = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
354
- $this->debug(3, "The conditional get's if-modified-since unixtime is $iftime");
355
- if($iftime < 1){
356
- $this->debug(3, "Got an invalid conditional get modified since time. Returning false.");
357
- return false;
358
- }
359
- if($iftime < $mtime){ //Real file or cache file has been modified since last request, so force refetch.
360
- $this->debug(3, "File has been modified since last fetch.");
361
- return false;
362
- } else { //Otherwise serve a 304
363
- $this->debug(3, "File has not been modified since last get, so serving a 304.");
364
- header ($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
365
- $this->debug(1, "Returning 304 not modified");
366
- return true;
367
- }
368
- }
369
- return false;
370
- }
371
- protected function tryServerCache(){
372
- $this->debug(3, "Trying server cache");
373
- if(file_exists($this->cachefile)){
374
- $this->debug(3, "Cachefile {$this->cachefile} exists");
375
- if($this->isURL){
376
- $this->debug(3, "This is an external request, so checking if the cachefile is empty which means the request failed previously.");
377
- if(filesize($this->cachefile) < 1){
378
- $this->debug(3, "Found an empty cachefile indicating a failed earlier request. Checking how old it is.");
379
- //Fetching error occured previously
380
- if(time() - @filemtime($this->cachefile) > WAIT_BETWEEN_FETCH_ERRORS){
381
- $this->debug(3, "File is older than " . WAIT_BETWEEN_FETCH_ERRORS . " seconds. Deleting and returning false so app can try and load file.");
382
- @unlink($this->cachefile);
383
- return false; //to indicate we didn't serve from cache and app should try and load
384
- } else {
385
- $this->debug(3, "Empty cachefile is still fresh so returning message saying we had an error fetching this image from remote host.");
386
- $this->set404();
387
- $this->error("An error occured fetching image.");
388
- return false;
389
- }
390
- }
391
- } else {
392
- $this->debug(3, "Trying to serve cachefile {$this->cachefile}");
393
- }
394
- if($this->serveCacheFile()){
395
- $this->debug(3, "Succesfully served cachefile {$this->cachefile}");
396
- return true;
397
- } else {
398
- $this->debug(3, "Failed to serve cachefile {$this->cachefile} - Deleting it from cache.");
399
- //Image serving failed. We can't retry at this point, but lets remove it from cache so the next request recreates it
400
- @unlink($this->cachefile);
401
- return true;
402
- }
403
- }
404
- }
405
- protected function error($err){
406
- $this->debug(3, "Adding error message: $err");
407
- $this->errors[] = $err;
408
- return false;
409
-
410
- }
411
- protected function haveErrors(){
412
- if(sizeof($this->errors) > 0){
413
- return true;
414
- }
415
- return false;
416
- }
417
- protected function serveErrors(){
418
- header ($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
419
- $html = '<ul>';
420
- foreach($this->errors as $err){
421
- $html .= '<li>' . htmlentities($err) . '</li>';
422
- }
423
- $html .= '</ul>';
424
- echo '<h1>A TimThumb error has occured</h1>The following error(s) occured:<br />' . $html . '<br />';
425
- echo '<br />Query String : ' . htmlentities ($_SERVER['QUERY_STRING']);
426
- echo '<br />TimThumb version : ' . VERSION . '</pre>';
427
- }
428
- protected function serveInternalImage(){
429
- $this->debug(3, "Local image path is $this->localImage");
430
- if(! $this->localImage){
431
- $this->sanityFail("localImage not set after verifying it earlier in the code.");
432
- return false;
433
- }
434
- $fileSize = filesize($this->localImage);
435
- if($fileSize > MAX_FILE_SIZE){
436
- $this->error("The file you specified is greater than the maximum allowed file size.");
437
- return false;
438
- }
439
- if($fileSize <= 0){
440
- $this->error("The file you specified is <= 0 bytes.");
441
- return false;
442
- }
443
- $this->debug(3, "Calling processImageAndWriteToCache() for local image.");
444
- if($this->processImageAndWriteToCache($this->localImage)){
445
- $this->serveCacheFile();
446
- return true;
447
- } else {
448
- return false;
449
- }
450
- }
451
- protected function cleanCache(){
452
- if (FILE_CACHE_TIME_BETWEEN_CLEANS < 0) {
453
- return;
454
- }
455
- $this->debug(3, "cleanCache() called");
456
- $lastCleanFile = $this->cacheDirectory . '/timthumb_cacheLastCleanTime.touch';
457
-
458
- //If this is a new timthumb installation we need to create the file
459
- if(! is_file($lastCleanFile)){
460
- $this->debug(1, "File tracking last clean doesn't exist. Creating $lastCleanFile");
461
- if (!touch($lastCleanFile)) {
462
- $this->error("Could not create cache clean timestamp file.");
463
- }
464
- return;
465
- }
466
- if(@filemtime($lastCleanFile) < (time() - FILE_CACHE_TIME_BETWEEN_CLEANS) ){ //Cache was last cleaned more than 1 day ago
467
- $this->debug(1, "Cache was last cleaned more than " . FILE_CACHE_TIME_BETWEEN_CLEANS . " seconds ago. Cleaning now.");
468
- // Very slight race condition here, but worst case we'll have 2 or 3 servers cleaning the cache simultaneously once a day.
469
- if (!touch($lastCleanFile)) {
470
- $this->error("Could not create cache clean timestamp file.");
471
- }
472
- $files = glob($this->cacheDirectory . '/*' . FILE_CACHE_SUFFIX);
473
- if ($files) {
474
- $timeAgo = time() - FILE_CACHE_MAX_FILE_AGE;
475
- foreach($files as $file){
476
- if(@filemtime($file) < $timeAgo){
477
- $this->debug(3, "Deleting cache file $file older than max age: " . FILE_CACHE_MAX_FILE_AGE . " seconds");
478
- @unlink($file);
479
- }
480
- }
481
- }
482
- return true;
483
- } else {
484
- $this->debug(3, "Cache was cleaned less than " . FILE_CACHE_TIME_BETWEEN_CLEANS . " seconds ago so no cleaning needed.");
485
- }
486
- return false;
487
- }
488
- protected function processImageAndWriteToCache($localImage){
489
- $sData = getimagesize($localImage);
490
- $origType = $sData[2];
491
- $mimeType = $sData['mime'];
492
-
493
- $this->debug(3, "Mime type of image is $mimeType");
494
- if(! preg_match('/^image\/(?:gif|jpg|jpeg|png)$/i', $mimeType)){
495
- return $this->error("The image being resized is not a valid gif, jpg or png.");
496
- }
497
-
498
- if (!function_exists ('imagecreatetruecolor')) {
499
- return $this->error('GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library');
500
- }
501
-
502
- if (function_exists ('imagefilter') && defined ('IMG_FILTER_NEGATE')) {
503
- $imageFilters = array (
504
- 1 => array (IMG_FILTER_NEGATE, 0),
505
- 2 => array (IMG_FILTER_GRAYSCALE, 0),
506
- 3 => array (IMG_FILTER_BRIGHTNESS, 1),
507
- 4 => array (IMG_FILTER_CONTRAST, 1),
508
- 5 => array (IMG_FILTER_COLORIZE, 4),
509
- 6 => array (IMG_FILTER_EDGEDETECT, 0),
510
- 7 => array (IMG_FILTER_EMBOSS, 0),
511
- 8 => array (IMG_FILTER_GAUSSIAN_BLUR, 0),
512
- 9 => array (IMG_FILTER_SELECTIVE_BLUR, 0),
513
- 10 => array (IMG_FILTER_MEAN_REMOVAL, 0),
514
- 11 => array (IMG_FILTER_SMOOTH, 0),
515
- );
516
- }
517
-
518
- // get standard input properties
519
- $new_width = (int) abs ($this->param('w', 0));
520
- $new_height = (int) abs ($this->param('h', 0));
521
- $zoom_crop = (int) $this->param('zc', DEFAULT_ZC);
522
- $quality = (int) abs ($this->param('q', DEFAULT_Q));
523
- $align = $this->cropTop ? 't' : $this->param('a', 'c');
524
- $filters = $this->param('f', DEFAULT_F);
525
- $sharpen = (bool) $this->param('s', DEFAULT_S);
526
- $canvas_color = $this->param('cc', DEFAULT_CC);
527
- $canvas_trans = (bool) $this->param('ct', '1');
528
-
529
- // set default width and height if neither are set already
530
- if ($new_width == 0 && $new_height == 0) {
531
- $new_width = 100;
532
- $new_height = 100;
533
- }
534
-
535
- // ensure size limits can not be abused
536
- $new_width = min ($new_width, MAX_WIDTH);
537
- $new_height = min ($new_height, MAX_HEIGHT);
538
-
539
- // set memory limit to be able to have enough space to resize larger images
540
- $this->setMemoryLimit();
541
-
542
- // open the existing image
543
- $image = $this->openImage ($mimeType, $localImage);
544
- if ($image === false) {
545
- return $this->error('Unable to open image.');
546
- }
547
-
548
- // Get original width and height
549
- $width = imagesx ($image);
550
- $height = imagesy ($image);
551
- $origin_x = 0;
552
- $origin_y = 0;
553
-
554
- // generate new w/h if not provided
555
- if ($new_width && !$new_height) {
556
- $new_height = floor ($height * ($new_width / $width));
557
- } else if ($new_height && !$new_width) {
558
- $new_width = floor ($width * ($new_height / $height));
559
- }
560
-
561
- // scale down and add borders
562
- if ($zoom_crop == 3) {
563
-
564
- $final_height = $height * ($new_width / $width);
565
-
566
- if ($final_height > $new_height) {
567
- $new_width = $width * ($new_height / $height);
568
- } else {
569
- $new_height = $final_height;
570
- }
571
-
572
- }
573
-
574
- // create a new true color image
575
- $canvas = imagecreatetruecolor ($new_width, $new_height);
576
- imagealphablending ($canvas, false);
577
-
578
- if (strlen($canvas_color) == 3) { //if is 3-char notation, edit string into 6-char notation
579
- $canvas_color = str_repeat(substr($canvas_color, 0, 1), 2) . str_repeat(substr($canvas_color, 1, 1), 2) . str_repeat(substr($canvas_color, 2, 1), 2);
580
- } else if (strlen($canvas_color) != 6) {
581
- $canvas_color = DEFAULT_CC; // on error return default canvas color
582
- }
583
-
584
- $canvas_color_R = hexdec (substr ($canvas_color, 0, 2));
585
- $canvas_color_G = hexdec (substr ($canvas_color, 2, 2));
586
- $canvas_color_B = hexdec (substr ($canvas_color, 4, 2));
587
-
588
- // Create a new transparent color for image
589
- // If is a png and PNG_IS_TRANSPARENT is false then remove the alpha transparency
590
- // (and if is set a canvas color show it in the background)
591
- if(preg_match('/^image\/png$/i', $mimeType) && !PNG_IS_TRANSPARENT && $canvas_trans){
592
- $color = imagecolorallocatealpha ($canvas, $canvas_color_R, $canvas_color_G, $canvas_color_B, 127);
593
- }else{
594
- $color = imagecolorallocatealpha ($canvas, $canvas_color_R, $canvas_color_G, $canvas_color_B, 0);
595
- }
596
-
597
-
598
- // Completely fill the background of the new image with allocated color.
599
- imagefill ($canvas, 0, 0, $color);
600
-
601
- // scale down and add borders
602
- if ($zoom_crop == 2) {
603
-
604
- $final_height = $height * ($new_width / $width);
605
-
606
- if ($final_height > $new_height) {
607
-
608
- $origin_x = $new_width / 2;
609
- $new_width = $width * ($new_height / $height);
610
- $origin_x = round ($origin_x - ($new_width / 2));
611
-
612
- } else {
613
-
614
- $origin_y = $new_height / 2;
615
- $new_height = $final_height;
616
- $origin_y = round ($origin_y - ($new_height / 2));
617
-
618
- }
619
-
620
- }
621
-
622
- // Restore transparency blending
623
- imagesavealpha ($canvas, true);
624
-
625
- if ($zoom_crop > 0) {
626
-
627
- $src_x = $src_y = 0;
628
- $src_w = $width;
629
- $src_h = $height;
630
-
631
- $cmp_x = $width / $new_width;
632
- $cmp_y = $height / $new_height;
633
-
634
- // calculate x or y coordinate and width or height of source
635
- if ($cmp_x > $cmp_y) {
636
-
637
- $src_w = round ($width / $cmp_x * $cmp_y);
638
- $src_x = round (($width - ($width / $cmp_x * $cmp_y)) / 2);
639
-
640
- } else if ($cmp_y > $cmp_x) {
641
-
642
- $src_h = round ($height / $cmp_y * $cmp_x);
643
- $src_y = round (($height - ($height / $cmp_y * $cmp_x)) / 2);
644
-
645
- }
646
-
647
- // positional cropping!
648
- if ($align) {
649
- if (strpos ($align, 't') !== false) {
650
- $src_y = 0;
651
- }
652
- if (strpos ($align, 'b') !== false) {
653
- $src_y = $height - $src_h;
654
- }
655
- if (strpos ($align, 'l') !== false) {
656
- $src_x = 0;
657
- }
658
- if (strpos ($align, 'r') !== false) {
659
- $src_x = $width - $src_w;
660
- }
661
- }
662
-
663
- imagecopyresampled ($canvas, $image, $origin_x, $origin_y, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h);
664
-
665
- } else {
666
-
667
- // copy and resize part of an image with resampling
668
- imagecopyresampled ($canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
669
-
670
- }
671
-
672
- if ($filters != '' && function_exists ('imagefilter') && defined ('IMG_FILTER_NEGATE')) {
673
- // apply filters to image
674
- $filterList = explode ('|', $filters);
675
- foreach ($filterList as $fl) {
676
-
677
- $filterSettings = explode (',', $fl);
678
- if (isset ($imageFilters[$filterSettings[0]])) {
679
-
680
- for ($i = 0; $i < 4; $i ++) {
681
- if (!isset ($filterSettings[$i])) {
682
- $filterSettings[$i] = null;
683
- } else {
684
- $filterSettings[$i] = (int) $filterSettings[$i];
685
- }
686
- }
687
-
688
- switch ($imageFilters[$filterSettings[0]][1]) {
689
-
690
- case 1:
691
-
692
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1]);
693
- break;
694
-
695
- case 2:
696
-
697
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2]);
698
- break;
699
-
700
- case 3:
701
-
702
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3]);
703
- break;
704
-
705
- case 4:
706
-
707
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3], $filterSettings[4]);
708
- break;
709
-
710
- default:
711
-
712
- imagefilter ($canvas, $imageFilters[$filterSettings[0]][0]);
713
- break;
714
-
715
- }
716
- }
717
- }
718
- }
719
-
720
- // sharpen image
721
- if ($sharpen && function_exists ('imageconvolution')) {
722
-
723
- $sharpenMatrix = array (
724
- array (-1,-1,-1),
725
- array (-1,16,-1),
726
- array (-1,-1,-1),
727
- );
728
-
729
- $divisor = 8;
730
- $offset = 0;
731
-
732
- imageconvolution ($canvas, $sharpenMatrix, $divisor, $offset);
733
-
734
- }
735
- //Straight from Wordpress core code. Reduces filesize by up to 70% for PNG's
736
- if ( (IMAGETYPE_PNG == $origType || IMAGETYPE_GIF == $origType) && function_exists('imageistruecolor') && !imageistruecolor( $image ) && imagecolortransparent( $image ) > 0 ){
737
- imagetruecolortopalette( $canvas, false, imagecolorstotal( $image ) );
738
- }
739
-
740
- $imgType = "";
741
- $tempfile = tempnam($this->cacheDirectory, 'timthumb_tmpimg_');
742
- if(preg_match('/^image\/(?:jpg|jpeg)$/i', $mimeType)){
743
- $imgType = 'jpg';
744
- imagejpeg($canvas, $tempfile, $quality);
745
- } else if(preg_match('/^image\/png$/i', $mimeType)){
746
- $imgType = 'png';
747
- imagepng($canvas, $tempfile, floor($quality * 0.09));
748
- } else if(preg_match('/^image\/gif$/i', $mimeType)){
749
- $imgType = 'gif';
750
- imagegif($canvas, $tempfile);
751
- } else {
752
- return $this->sanityFail("Could not match mime type after verifying it previously.");
753
- }
754
-
755
- if($imgType == 'png' && OPTIPNG_ENABLED && OPTIPNG_PATH && @is_file(OPTIPNG_PATH)){
756
- $exec = OPTIPNG_PATH;
757
- $this->debug(3, "optipng'ing $tempfile");
758
- $presize = filesize($tempfile);
759
- $out = `$exec -o1 $tempfile`; //you can use up to -o7 but it really slows things down
760
- clearstatcache();
761
- $aftersize = filesize($tempfile);
762
- $sizeDrop = $presize - $aftersize;
763
- if($sizeDrop > 0){
764
- $this->debug(1, "optipng reduced size by $sizeDrop");
765
- } else if($sizeDrop < 0){
766
- $this->debug(1, "optipng increased size! Difference was: $sizeDrop");
767
- } else {
768
- $this->debug(1, "optipng did not change image size.");
769
- }
770
- } else if($imgType == 'png' && PNGCRUSH_ENABLED && PNGCRUSH_PATH && @is_file(PNGCRUSH_PATH)){
771
- $exec = PNGCRUSH_PATH;
772
- $tempfile2 = tempnam($this->cacheDirectory, 'timthumb_tmpimg_');
773
- $this->debug(3, "pngcrush'ing $tempfile to $tempfile2");
774
- $out = `$exec $tempfile $tempfile2`;
775
- $todel = "";
776
- if(is_file($tempfile2)){
777
- $sizeDrop = filesize($tempfile) - filesize($tempfile2);
778
- if($sizeDrop > 0){
779
- $this->debug(1, "pngcrush was succesful and gave a $sizeDrop byte size reduction");
780
- $todel = $tempfile;
781
- $tempfile = $tempfile2;
782
- } else {
783
- $this->debug(1, "pngcrush did not reduce file size. Difference was $sizeDrop bytes.");
784
- $todel = $tempfile2;
785
- }
786
- } else {
787
- $this->debug(3, "pngcrush failed with output: $out");
788
- $todel = $tempfile2;
789
- }
790
- @unlink($todel);
791
- }
792
-
793
- $this->debug(3, "Rewriting image with security header.");
794
- $tempfile4 = tempnam($this->cacheDirectory, 'timthumb_tmpimg_');
795
- $context = stream_context_create ();
796
- $fp = fopen($tempfile,'r',0,$context);
797
- file_put_contents($tempfile4, $this->filePrependSecurityBlock . $imgType . ' ?' . '>'); //6 extra bytes, first 3 being image type
798
- file_put_contents($tempfile4, $fp, FILE_APPEND);
799
- fclose($fp);
800
- @unlink($tempfile);
801
- $this->debug(3, "Locking and replacing cache file.");
802
- $lockFile = $this->cachefile . '.lock';
803
- $fh = fopen($lockFile, 'w');
804
- if(! $fh){
805
- return $this->error("Could not open the lockfile for writing an image.");
806
- }
807
- if(flock($fh, LOCK_EX)){
808
- @unlink($this->cachefile); //rename generally overwrites, but doing this in case of platform specific quirks. File might not exist yet.
809
- rename($tempfile4, $this->cachefile);
810
- flock($fh, LOCK_UN);
811
- fclose($fh);
812
- @unlink($lockFile);
813
- } else {
814
- fclose($fh);
815
- @unlink($lockFile);
816
- @unlink($tempfile4);
817
- return $this->error("Could not get a lock for writing.");
818
- }
819
- $this->debug(3, "Done image replace with security header. Cleaning up and running cleanCache()");
820
- imagedestroy($canvas);
821
- imagedestroy($image);
822
- return true;
823
- }
824
- protected function calcDocRoot(){
825
- $docRoot = @$_SERVER['DOCUMENT_ROOT'];
826
- if (defined('LOCAL_FILE_BASE_DIRECTORY')) {
827
- $docRoot = LOCAL_FILE_BASE_DIRECTORY;
828
- }
829
- if(!isset($docRoot)){
830
- $this->debug(3, "DOCUMENT_ROOT is not set. This is probably windows. Starting search 1.");
831
- if(isset($_SERVER['SCRIPT_FILENAME'])){
832
- $docRoot = str_replace( '\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF'])));
833
- $this->debug(3, "Generated docRoot using SCRIPT_FILENAME and PHP_SELF as: $docRoot");
834
- }
835
- }
836
- if(!isset($docRoot)){
837
- $this->debug(3, "DOCUMENT_ROOT still is not set. Starting search 2.");
838
- if(isset($_SERVER['PATH_TRANSLATED'])){
839
- $docRoot = str_replace( '\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0-strlen($_SERVER['PHP_SELF'])));
840
- $this->debug(3, "Generated docRoot using PATH_TRANSLATED and PHP_SELF as: $docRoot");
841
- }
842
- }
843
- if($docRoot && $_SERVER['DOCUMENT_ROOT'] != '/'){ $docRoot = preg_replace('/\/$/', '', $docRoot); }
844
- $this->debug(3, "Doc root is: " . $docRoot);
845
- $this->docRoot = $docRoot;
846
-
847
- }
848
- protected function getLocalImagePath($src){
849
- $src = ltrim($src, '/'); //strip off the leading '/'
850
- if(! $this->docRoot){
851
- $this->debug(3, "We have no document root set, so as a last resort, lets check if the image is in the current dir and serve that.");
852
- //We don't support serving images outside the current dir if we don't have a doc root for security reasons.
853
- $file = preg_replace('/^.*?([^\/\\\\]+)$/', '$1', $src); //strip off any path info and just leave the filename.
854
- if(is_file($file)){
855
- return $this->realpath($file);
856
- }
857
- return $this->error("Could not find your website document root and the file specified doesn't exist in timthumbs directory. We don't support serving files outside timthumb's directory without a document root for security reasons.");
858
- } //Do not go past this point without docRoot set
859
-
860
- //Try src under docRoot
861
- if(file_exists ($this->docRoot . '/' . $src)) {
862
- $this->debug(3, "Found file as " . $this->docRoot . '/' . $src);
863
- $real = $this->realpath($this->docRoot . '/' . $src);
864
- if(stripos($real, $this->docRoot) === 0){
865
- return $real;
866
- } else {
867
- $this->debug(1, "Security block: The file specified occurs outside the document root.");
868
- //allow search to continue
869
- }
870
- }
871
- //Check absolute paths and then verify the real path is under doc root
872
- $absolute = $this->realpath('/' . $src);
873
- if($absolute && file_exists($absolute)){ //realpath does file_exists check, so can probably skip the exists check here
874
- $this->debug(3, "Found absolute path: $absolute");
875
- if(! $this->docRoot){ $this->sanityFail("docRoot not set when checking absolute path."); }
876
- if(stripos($absolute, $this->docRoot) === 0){
877
- return $absolute;
878
- } else {
879
- $this->debug(1, "Security block: The file specified occurs outside the document root.");
880
- //and continue search
881
- }
882
- }
883
-
884
- $base = $this->docRoot;
885
-
886
- // account for Windows directory structure
887
- if (strstr($_SERVER['SCRIPT_FILENAME'],':')) {
888
- $sub_directories = explode('\\', str_replace($this->docRoot, '', $_SERVER['SCRIPT_FILENAME']));
889
- } else {
890
- $sub_directories = explode('/', str_replace($this->docRoot, '', $_SERVER['SCRIPT_FILENAME']));
891
- }
892
-
893
- foreach ($sub_directories as $sub){
894
- $base .= $sub . '/';
895
- $this->debug(3, "Trying file as: " . $base . $src);
896
- if(file_exists($base . $src)){
897
- $this->debug(3, "Found file as: " . $base . $src);
898
- $real = $this->realpath($base . $src);
899
- if(stripos($real, $this->realpath($this->docRoot)) === 0){
900
- return $real;
901
- } else {
902
- $this->debug(1, "Security block: The file specified occurs outside the document root.");
903
- //And continue search
904
- }
905
- }
906
- }
907
- return false;
908
- }
909
- protected function realpath($path){
910
- //try to remove any relative paths
911
- $remove_relatives = '/\w+\/\.\.\//';
912
- while(preg_match($remove_relatives,$path)){
913
- $path = preg_replace($remove_relatives, '', $path);
914
- }
915
- //if any remain use PHP realpath to strip them out, otherwise return $path
916
- //if using realpath, any symlinks will also be resolved
917
- return preg_match('#^\.\./|/\.\./#', $path) ? realpath($path) : $path;
918
- }
919
- protected function toDelete($name){
920
- $this->debug(3, "Scheduling file $name to delete on destruct.");
921
- $this->toDeletes[] = $name;
922
- }
923
- protected function serveWebshot(){
924
- $this->debug(3, "Starting serveWebshot");
925
- $instr = "Please follow the instructions at http://code.google.com/p/timthumb/ to set your server up for taking website screenshots.";
926
- if(! is_file(WEBSHOT_CUTYCAPT)){
927
- return $this->error("CutyCapt is not installed. $instr");
928
- }
929
- if(! is_file(WEBSHOT_XVFB)){
930
- return $this->Error("Xvfb is not installed. $instr");
931
- }
932
- $cuty = WEBSHOT_CUTYCAPT;
933
- $xv = WEBSHOT_XVFB;
934
- $screenX = WEBSHOT_SCREEN_X;
935
- $screenY = WEBSHOT_SCREEN_Y;
936
- $colDepth = WEBSHOT_COLOR_DEPTH;
937
- $format = WEBSHOT_IMAGE_FORMAT;
938
- $timeout = WEBSHOT_TIMEOUT * 1000;
939
- $ua = WEBSHOT_USER_AGENT;
940
- $jsOn = WEBSHOT_JAVASCRIPT_ON ? 'on' : 'off';
941
- $javaOn = WEBSHOT_JAVA_ON ? 'on' : 'off';
942
- $pluginsOn = WEBSHOT_PLUGINS_ON ? 'on' : 'off';
943
- $proxy = WEBSHOT_PROXY ? ' --http-proxy=' . WEBSHOT_PROXY : '';
944
- $tempfile = tempnam($this->cacheDirectory, 'timthumb_webshot');
945
- $url = $this->src;
946
- if(! preg_match('/^https?:\/\/[a-zA-Z0-9\.\-]+/i', $url)){
947
- return $this->error("Invalid URL supplied.");
948
- }
949
- $url = preg_replace('/[^A-Za-z0-9\-\.\_\~:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=]+/', '', $url); //RFC 3986
950
- //Very important we don't allow injection of shell commands here. URL is between quotes and we are only allowing through chars allowed by a the RFC
951
- // which AFAIKT can't be used for shell injection.
952
- if(WEBSHOT_XVFB_RUNNING){
953
- putenv('DISPLAY=:100.0');
954
- $command = "$cuty $proxy --max-wait=$timeout --user-agent=\"$ua\" --javascript=$jsOn --java=$javaOn --plugins=$pluginsOn --js-can-open-windows=off --url=\"$url\" --out-format=$format --out=$tempfile";
955
- } else {
956
- $command = "$xv --server-args=\"-screen 0, {$screenX}x{$screenY}x{$colDepth}\" $cuty $proxy --max-wait=$timeout --user-agent=\"$ua\" --javascript=$jsOn --java=$javaOn --plugins=$pluginsOn --js-can-open-windows=off --url=\"$url\" --out-format=$format --out=$tempfile";
957
- }
958
- $this->debug(3, "Executing command: $command");
959
- $out = `$command`;
960
- $this->debug(3, "Received output: $out");
961
- if(! is_file($tempfile)){
962
- $this->set404();
963
- return $this->error("The command to create a thumbnail failed.");
964
- }
965
- $this->cropTop = true;
966
- if($this->processImageAndWriteToCache($tempfile)){
967
- $this->debug(3, "Image processed succesfully. Serving from cache");
968
- return $this->serveCacheFile();
969
- } else {
970
- return false;
971
- }
972
- }
973
- protected function serveExternalImage(){
974
- if(! preg_match('/^https?:\/\/[a-zA-Z0-9\-\.]+/i', $this->src)){
975
- $this->error("Invalid URL supplied.");
976
- return false;
977
- }
978
- $tempfile = tempnam($this->cacheDirectory, 'timthumb');
979
- $this->debug(3, "Fetching external image into temporary file $tempfile");
980
- $this->toDelete($tempfile);
981
- #fetch file here
982
- if(! $this->getURL($this->src, $tempfile)){
983
- @unlink($this->cachefile);
984
- touch($this->cachefile);
985
- $this->debug(3, "Error fetching URL: " . $this->lastURLError);
986
- $this->error("Error reading the URL you specified from remote host." . $this->lastURLError);
987
- return false;
988
- }
989
-
990
- $mimeType = $this->getMimeType($tempfile);
991
- if(! preg_match("/^image\/(?:jpg|jpeg|gif|png)$/i", $mimeType)){
992
- $this->debug(3, "Remote file has invalid mime type: $mimeType");
993
- @unlink($this->cachefile);
994
- touch($this->cachefile);
995
- $this->error("The remote file is not a valid image. Mimetype = '" . $mimeType . "'" . $tempfile);
996
- return false;
997
- }
998
- if($this->processImageAndWriteToCache($tempfile)){
999
- $this->debug(3, "Image processed succesfully. Serving from cache");
1000
- return $this->serveCacheFile();
1001
- } else {
1002
- return false;
1003
- }
1004
- }
1005
- public static function curlWrite($h, $d){
1006
- fwrite(self::$curlFH, $d);
1007
- self::$curlDataWritten += strlen($d);
1008
- if(self::$curlDataWritten > MAX_FILE_SIZE){
1009
- return 0;
1010
- } else {
1011
- return strlen($d);
1012
- }
1013
- }
1014
- protected function serveCacheFile(){
1015
- $this->debug(3, "Serving {$this->cachefile}");
1016
- if(! is_file($this->cachefile)){
1017
- $this->error("serveCacheFile called in timthumb but we couldn't find the cached file.");
1018
- return false;
1019
- }
1020
- $fp = fopen($this->cachefile, 'rb');
1021
- if(! $fp){ return $this->error("Could not open cachefile."); }
1022
- fseek($fp, strlen($this->filePrependSecurityBlock), SEEK_SET);
1023
- $imgType = fread($fp, 3);
1024
- fseek($fp, 3, SEEK_CUR);
1025
- if(ftell($fp) != strlen($this->filePrependSecurityBlock) + 6){
1026
- @unlink($this->cachefile);
1027
- return $this->error("The cached image file seems to be corrupt.");
1028
- }
1029
- $imageDataSize = filesize($this->cachefile) - (strlen($this->filePrependSecurityBlock) + 6);
1030
- $this->sendImageHeaders($imgType, $imageDataSize);
1031
- $bytesSent = @fpassthru($fp);
1032
- fclose($fp);
1033
- if($bytesSent > 0){
1034
- return true;
1035
- }
1036
- $content = file_get_contents ($this->cachefile);
1037
- if ($content != FALSE) {
1038
- $content = substr($content, strlen($this->filePrependSecurityBlock) + 6);
1039
- echo $content;
1040
- $this->debug(3, "Served using file_get_contents and echo");
1041
- return true;
1042
- } else {
1043
- $this->error("Cache file could not be loaded.");
1044
- return false;
1045
- }
1046
- }
1047
- protected function sendImageHeaders($mimeType, $dataSize){
1048
- if(! preg_match('/^image\//i', $mimeType)){
1049
- $mimeType = 'image/' . $mimeType;
1050
- }
1051
- if(strtolower($mimeType) == 'image/jpg'){
1052
- $mimeType = 'image/jpeg';
1053
- }
1054
- $gmdate_expires = gmdate ('D, d M Y H:i:s', strtotime ('now +10 days')) . ' GMT';
1055
- $gmdate_modified = gmdate ('D, d M Y H:i:s') . ' GMT';
1056
- // send content headers then display image
1057
- header ('Content-Type: ' . $mimeType);
1058
- header ('Accept-Ranges: none'); //Changed this because we don't accept range requests
1059
- header ('Last-Modified: ' . $gmdate_modified);
1060
- header ('Content-Length: ' . $dataSize);
1061
- if(BROWSER_CACHE_DISABLE){
1062
- $this->debug(3, "Browser cache is disabled so setting non-caching headers.");
1063
- header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
1064
- header("Pragma: no-cache");
1065
- header('Expires: ' . gmdate ('D, d M Y H:i:s', time()));
1066
- } else {
1067
- $this->debug(3, "Browser caching is enabled");
1068
- header('Cache-Control: max-age=' . BROWSER_CACHE_MAX_AGE . ', must-revalidate');
1069
- header('Expires: ' . $gmdate_expires);
1070
- }
1071
- return true;
1072
- }
1073
- protected function securityChecks(){
1074
- }
1075
- protected function param($property, $default = ''){
1076
- if (isset ($_GET[$property])) {
1077
- return $_GET[$property];
1078
- } else {
1079
- return $default;
1080
- }
1081
- }
1082
- protected function openImage($mimeType, $src){
1083
- switch ($mimeType) {
1084
- case 'image/jpeg':
1085
- $image = imagecreatefromjpeg ($src);
1086
- break;
1087
-
1088
- case 'image/png':
1089
- $image = imagecreatefrompng ($src);
1090
- imagealphablending( $image, true );
1091
- imagesavealpha( $image, true );
1092
- break;
1093
-
1094
- case 'image/gif':
1095
- $image = imagecreatefromgif ($src);
1096
- break;
1097
-
1098
- default:
1099
- $this->error("Unrecognised mimeType");
1100
- }
1101
-
1102
- return $image;
1103
- }
1104
- protected function getIP(){
1105
- $rem = @$_SERVER["REMOTE_ADDR"];
1106
- $ff = @$_SERVER["HTTP_X_FORWARDED_FOR"];
1107
- $ci = @$_SERVER["HTTP_CLIENT_IP"];
1108
- if(preg_match('/^(?:192\.168|172\.16|10\.|127\.)/', $rem)){
1109
- if($ff){ return $ff; }
1110
- if($ci){ return $ci; }
1111
- return $rem;
1112
- } else {
1113
- if($rem){ return $rem; }
1114
- if($ff){ return $ff; }
1115
- if($ci){ return $ci; }
1116
- return "UNKNOWN";
1117
- }
1118
- }
1119
- protected function debug($level, $msg){
1120
- if(DEBUG_ON && $level <= DEBUG_LEVEL){
1121
- $execTime = sprintf('%.6f', microtime(true) - $this->startTime);
1122
- $tick = sprintf('%.6f', 0);
1123
- if($this->lastBenchTime > 0){
1124
- $tick = sprintf('%.6f', microtime(true) - $this->lastBenchTime);
1125
- }
1126
- $this->lastBenchTime = microtime(true);
1127
- error_log("TimThumb Debug line " . __LINE__ . " [$execTime : $tick]: $msg");
1128
- }
1129
- }
1130
- protected function sanityFail($msg){
1131
- return $this->error("There is a problem in the timthumb code. Message: Please report this error at <a href='http://code.google.com/p/timthumb/issues/list'>timthumb's bug tracking page</a>: $msg");
1132
- }
1133
- protected function getMimeType($file){
1134
- $info = getimagesize($file);
1135
- if(is_array($info) && $info['mime']){
1136
- return $info['mime'];
1137
- }
1138
- return '';
1139
- }
1140
- protected function setMemoryLimit(){
1141
- $inimem = ini_get('memory_limit');
1142
- $inibytes = timthumb::returnBytes($inimem);
1143
- $ourbytes = timthumb::returnBytes(MEMORY_LIMIT);
1144
- if($inibytes < $ourbytes){
1145
- ini_set ('memory_limit', MEMORY_LIMIT);
1146
- $this->debug(3, "Increased memory from $inimem to " . MEMORY_LIMIT);
1147
- } else {
1148
- $this->debug(3, "Not adjusting memory size because the current setting is " . $inimem . " and our size of " . MEMORY_LIMIT . " is smaller.");
1149
- }
1150
- }
1151
- protected static function returnBytes($size_str){
1152
- switch (substr ($size_str, -1))
1153
- {
1154
- case 'M': case 'm': return (int)$size_str * 1048576;
1155
- case 'K': case 'k': return (int)$size_str * 1024;
1156
- case 'G': case 'g': return (int)$size_str * 1073741824;
1157
- default: return $size_str;
1158
- }
1159
- }
1160
-
1161
- protected function getURL($url, $tempfile){
1162
- $this->lastURLError = false;
1163
- $url = preg_replace('/ /', '%20', $url);
1164
- if(function_exists('curl_init')){
1165
- $this->debug(3, "Curl is installed so using it to fetch URL.");
1166
- self::$curlFH = fopen($tempfile, 'w');
1167
- if(! self::$curlFH){
1168
- $this->error("Could not open $tempfile for writing.");
1169
- return false;
1170
- }
1171
- self::$curlDataWritten = 0;
1172
- $this->debug(3, "Fetching url with curl: $url");
1173
- $curl = curl_init($url);
1174
- curl_setopt ($curl, CURLOPT_TIMEOUT, CURL_TIMEOUT);
1175
- curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30");
1176
- curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE);
1177
- curl_setopt ($curl, CURLOPT_HEADER, 0);
1178
- curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
1179
- curl_setopt ($curl, CURLOPT_WRITEFUNCTION, 'timthumb::curlWrite');
1180
- @curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
1181
- @curl_setopt ($curl, CURLOPT_MAXREDIRS, 10);
1182
-
1183
- $curlResult = curl_exec($curl);
1184
- fclose(self::$curlFH);
1185
- $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
1186
- if($httpStatus == 404){
1187
- $this->set404();
1188
- }
1189
- if($httpStatus == 302){
1190
- $this->error("External Image is Redirecting. Try alternate image url");
1191
- return false;
1192
- }
1193
- if($curlResult){
1194
- curl_close($curl);
1195
- return true;
1196
- } else {
1197
- $this->lastURLError = curl_error($curl);
1198
- curl_close($curl);
1199
- return false;
1200
- }
1201
- } else {
1202
- $img = @file_get_contents ($url);
1203
- if($img === false){
1204
- $err = error_get_last();
1205
- if(is_array($err) && $err['message']){
1206
- $this->lastURLError = $err['message'];
1207
- } else {
1208
- $this->lastURLError = $err;
1209
- }
1210
- if(preg_match('/404/', $this->lastURLError)){
1211
- $this->set404();
1212
- }
1213
-
1214
- return false;
1215
- }
1216
- if(! file_put_contents($tempfile, $img)){
1217
- $this->error("Could not write to $tempfile.");
1218
- return false;
1219
- }
1220
- return true;
1221
- }
1222
-
1223
- }
1224
- protected function serveImg($file){
1225
- $s = getimagesize($file);
1226
- if(! ($s && $s['mime'])){
1227
- return false;
1228
- }
1229
- header ('Content-Type: ' . $s['mime']);
1230
- header ('Content-Length: ' . filesize($file) );
1231
- header ('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
1232
- header ("Pragma: no-cache");
1233
- $bytes = @readfile($file);
1234
- if($bytes > 0){
1235
- return true;
1236
- }
1237
- $content = @file_get_contents ($file);
1238
- if ($content != FALSE){
1239
- echo $content;
1240
- return true;
1241
- }
1242
- return false;
1243
-
1244
- }
1245
- protected function set404(){
1246
- $this->is404 = true;
1247
- }
1248
- protected function is404(){
1249
- return $this->is404;
1250
- }
1251
- }