-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.php
More file actions
368 lines (339 loc) · 12.5 KB
/
library.php
File metadata and controls
368 lines (339 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
* libWebThumbnail
*
* @desc api.webthumbnail.org
* @author Lukasz Cepowski <lukasz[at]cepowski.pl>
* @author Ralf Hertsch <ralf.hertsch@phpmanufaktur.de>
* @link http://phpmanufaktur.de
* @copyright 2012 Ralf Hertsch, phpManufaktur
* @copyright 2012 Ognisco Software
* @license MIT License (MIT) http://www.opensource.org/licenses/MIT
*/
// include class.secure.php to protect this file and the whole CMS!
if (defined('WB_PATH')) {
if (defined('LEPTON_VERSION'))
include(WB_PATH.'/framework/class.secure.php');
}
else {
$oneback = "../";
$root = $oneback;
$level = 1;
while (($level < 10) && (!file_exists($root.'/framework/class.secure.php'))) {
$root .= $oneback;
$level += 1;
}
if (file_exists($root.'/framework/class.secure.php')) {
include($root.'/framework/class.secure.php');
}
else {
trigger_error(sprintf("[ <b>%s</b> ] Can't include class.secure.php!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
}
}
// end include class.secure.php
// wb2lepton compatibility
if (!defined('LEPTON_PATH')) require_once WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/wb2lepton.php';
// use LEPTON 2.x I18n for access to language files
if (!class_exists('LEPTON_Helper_I18n')) require_once LEPTON_PATH.'/modules/'.basename(dirname(__FILE__)).'/framework/LEPTON/Helper/I18n.php';
// load language depending onfiguration
if (!file_exists(LEPTON_PATH.'/modules/'.basename(dirname(__FILE__)).'/languages/'.LANGUAGE.'.cfg.php')) {
require_once(LEPTON_PATH.'/modules/'.basename(dirname(__FILE__)).'/languages/EN.cfg.php');
}
else {
require_once(LEPTON_PATH.'/modules/'.basename(dirname(__FILE__)).'/languages/'.LANGUAGE.'.cfg.php');
}
global $I18n;
if (!is_object($I18n)) {
$I18n = new LEPTON_Helper_I18n();
}
else {
$I18n->addFile('DE.php', LEPTON_PATH.'/modules/'.basename(dirname(__FILE__)).'/languages/');
}
require_once LEPTON_PATH.'/modules/'.basename(dirname(__FILE__)).'/webthumbnail/webthumbnail.php';
class libWebThumbnail {
const PARAM_URL = 'url';
const PARAM_BROWSER = 'browser';
const PARAM_FORMAT = 'format';
const PARAM_WIDTH = 'width';
const PARAM_HEIGHT = 'height';
const PARAM_ALT = 'alt';
const PARAM_TITLE = 'title';
const PARAM_TARGET = 'target';
const PARAM_DO_LINK = 'link';
const PARAM_HTML_STRICT = 'strict';
const PARAM_CLASS_LINK = 'class_link';
const PARAM_CLASS_IMAGE = 'class_image';
const PARAM_PAGE_ID = 'page_id';
private $params = array(
self::PARAM_URL => 'https://phpmanufaktur.de',
self::PARAM_BROWSER => webthumbnail::BROWSER_CHROME,
self::PARAM_FORMAT => webthumbnail::FORMAT_PNG,
self::PARAM_HEIGHT => webthumbnail::MIN_HEIGHT,
self::PARAM_WIDTH => webthumbnail::MIN_WIDTH,
self::PARAM_ALT => '',
self::PARAM_TITLE => '',
self::PARAM_TARGET => '_blank',
self::PARAM_DO_LINK => true,
self::PARAM_HTML_STRICT => false,
self::PARAM_CLASS_IMAGE => '',
self::PARAM_CLASS_LINK => '',
self::PARAM_PAGE_ID => -1
);
const IMAGE_DIRECTORY = '/webthumbnail/';
// the update cyclus in hours, default: 24*7 = 168 => each week
const UPDATE_CYCLE = 168;
private $error;
protected $lang;
protected $url_image_directory;
protected $path_image_directory;
protected static $config_file = 'config.json';
protected static $table_prefix = TABLE_PREFIX;
/**
* Constructor for libWebThumbnail
* Set $error on problems while initializing the library
*/
public function __construct() {
global $I18n;
$this->lang = $I18n;
date_default_timezone_set(CFG_TIME_ZONE);
$this->url_image_directory = LEPTON_URL.MEDIA_DIRECTORY.self::IMAGE_DIRECTORY;
$this->path_image_directory = LEPTON_PATH.MEDIA_DIRECTORY.self::IMAGE_DIRECTORY;
if (!file_exists($this->path_image_directory)) {
if (!mkdir($this->path_image_directory, 0755)) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__,
$this->lang->translate('Error: Can\'t create the directory {{ directory }}.',
array('directory' => MEDIA_DIRECTORY.self::IMAGE_DIRECTORY))));
}
}
if (file_exists(LEPTON_PATH.'/modules/lib_webthumbnail/config.json')) {
$config = json_decode(file_get_contents(LEPTON_PATH.'/modules/lib_webthumbnail/config.json'), true);
if (isset($config['table_prefix']))
self::$table_prefix = $config['table_prefix'];
}
} // __construct()
/**
* Get the parameters - this function is important for the droplet usage.
*
* @return array $params
*/
public function getParams() {
return $this->params;
} // getParams()
/**
* Set the parameters - this function will be called by droplets.
*
* @param $params array
* @return boolean true on success
*/
public function setParams($params = array()) {
$this->params = $params;
$this->params[self::PARAM_PAGE_ID] = PAGE_ID;
return true;
} // setParams()
/**
* Set $this->error to $error
*
* @param $error string
*/
protected function setError($error) {
$this->error = $error;
} // setError()
/**
* Get Error from $this->error;
*
* @return string $this->error
*/
public function getError() {
return $this->error;
} // getError()
/**
* Check if $this->error is empty
*
* @return boolean
*/
public function isError() {
return (bool) !empty($this->error);
} // isError
/**
* Create a unique GUID
*
* @return string
*/
public static function createGUID() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
} // createGUID()
/**
* Check if the desired URL for the specified browser, image type, width and
* height is registered in the database and if the Thumbnail exists.
* Return TRUE, the data record and the filename or FALSE and the proposed
* filename.
*
* @param array $params all parameters
* @param string reference $img_name existing or proposed image name
* @return boolean
*/
protected function checkThumbnailExists($params, &$img_name) {
global $database;
$SQL = sprintf("SELECT * FROM `%smod_webthumbnail` WHERE `url`='%s' AND ".
"`browser`='%s' AND `img_format`='%s' AND `width`='%d' AND `height`='%d'",
self::$table_prefix, $params[self::PARAM_URL], $params[self::PARAM_BROWSER],
$params[self::PARAM_FORMAT], $params[self::PARAM_WIDTH],
$params[self::PARAM_HEIGHT]);
if (false === ($query = $database->query($SQL))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()));
return false;
}
if ($query->numRows() > 0) {
// hit in the database records
$data = $query->fetchRow(MYSQL_ASSOC);
if (file_exists($this->path_image_directory.$data['img_name'])) {
// file already exists
$img_name = $data['img_name'];
return true;
}
else {
// url is registered but missing the file
$img_name = $data['img_name'];
return false;
}
}
else {
// this url is not registered
$img_name = sprintf('%s.%s', self::createGUID(), $params[self::PARAM_FORMAT]);
return false;
}
} // checkThumbnailExists()
/**
* Create a linked image tag from the $params.
*
* @param array $params
* @param string $image_name
* @return string complete image tag
*/
protected function createImageTag($params, $image_name) {
$img_tag = '';
if ($params[self::PARAM_DO_LINK]) {
$img_tag .= sprintf('<a %shref="%s"',
!empty($params[self::PARAM_CLASS_LINK]) ? sprintf('class="%s" ',
$params[self::PARAM_CLASS_LINK]) : '',
$params[self::PARAM_URL]);
if (!$params[self::PARAM_HTML_STRICT])
$img_tag .= sprintf(' target="%s"', $params[self::PARAM_TARGET]);
if (!empty($params[self::PARAM_TITLE]))
$img_tag .= sprintf(' title="%s"', $params[self::PARAM_TITLE]);
$img_tag .= '>';
}
$img_tag .= sprintf('<img %ssrc="%s%s" width="%d" height="%d" alt="%s"',
!empty($params[self::PARAM_CLASS_IMAGE]) ? sprintf('class="%s" ',
$params[self::PARAM_CLASS_LINK]) : '',
$this->url_image_directory, $image_name, $params[self::PARAM_WIDTH],
$params[self::PARAM_HEIGHT], $params[self::PARAM_ALT]);
if (!empty($params[self::PARAM_TITLE]))
$img_tag .= sprintf(' title="%s"', $params[self::PARAM_TITLE]);
$img_tag .= '>';
if ($params[self::PARAM_DO_LINK]) $img_tag .= '</a>';
return $img_tag;
} // createImageTag()
/**
* Write the WebThumbthunbnail to the file $image_name and create a data record
*
* @param array $params
* @param string $image_name
* @return boolean
*/
protected function createThumbnail($params, $image_name) {
global $database;
// first step: create the thumbnail
try {
$thumb = new Webthumbnail($params[self::PARAM_URL]);
$thumb->setBrowser($params[self::PARAM_BROWSER]);
$thumb->setFormat($params[self::PARAM_FORMAT]);
$thumb->setHeight($params[self::PARAM_HEIGHT]);
$thumb->setWidth($params[self::PARAM_WIDTH]);
$thumb->captureToFile($this->path_image_directory.$image_name);
} catch (Exception $e) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, $e->getLine(), $e->getMessage()));
return false;
}
// second step: write the data into to the database
$SQL = sprintf("INSERT INTO `%smod_webthumbnail` (`url`,`browser`,`img_name`,".
"`img_format`,`width`,`height`,`page_id`) VALUES ('%s','%s','%s','%s','%d',".
"'%d','%d') ON DUPLICATE KEY UPDATE `url`='%s',`browser`='%s',`img_format`='%s',".
"`width`='%d',`height`='%d',`page_id`='%d',`timestamp`='%s'",
self::$table_prefix,
$params[self::PARAM_URL],
$params[self::PARAM_BROWSER],
$image_name,
$params[self::PARAM_FORMAT],
$params[self::PARAM_WIDTH],
$params[self::PARAM_HEIGHT],
$params[self::PARAM_PAGE_ID],
$params[self::PARAM_URL],
$params[self::PARAM_BROWSER],
$params[self::PARAM_FORMAT],
$params[self::PARAM_WIDTH],
$params[self::PARAM_HEIGHT],
$params[self::PARAM_PAGE_ID],
date('Y-m-d H:i:s')
);
if (!$database->query($SQL)) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()));
return false;
}
return true;
} // createThumbnail()
/**
* Called by the droplet [[webthumbnail]] to create and return a local saved
* WebThumbnail of a website as complete image tag.
*
* @return boolean false or string image tag
*/
public function getImageTag() {
if (empty($this->params[self::PARAM_URL])) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__,
$this->lang->translate('Error: No URL specified!')));
return false;
}
// check if already a thumbnail exists
$image_name = '';
if ($this->checkThumbnailExists($this->params, $image_name)) {
return $this->createImageTag($this->params, $image_name);
}
if (!$this->createThumbnail($this->params, $image_name)) return false;
return $this->createImageTag($this->params, $image_name);
} // getImageTag()
/**
* This function is called by cronjob.php and update each run one thumbnail
* which is older than in self::UPDATE_CYCLE specified
*
* @return boolean
*/
public function execUpdate() {
global $database;
// get the date in the past
$past_date = date('Y-m-d H:i:s', mktime(date('H')-self::UPDATE_CYCLE,date('i'),date('s'),date('m'),date('d'),date('Y')));
$SQL = sprintf("SELECT * FROM `%smod_webthumbnail` WHERE `timestamp`<'%s' ORDER BY `timestamp` ASC LIMIT 1",
self::$table_prefix, $past_date);
if (false === ($query = $database->query($SQL))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()));
return false;
}
if ($query->numRows() > 0) {
$data = $query->fetchRow(MYSQL_ASSOC);
$params = array(
self::PARAM_URL => $data['url'],
self::PARAM_BROWSER => $data['browser'],
self::PARAM_FORMAT => $data['img_format'],
self::PARAM_WIDTH => $data['width'],
self::PARAM_HEIGHT => $data['height'],
self::PARAM_PAGE_ID => $data['page_id']
);
if (!$this->createThumbnail($params, $data['img_name']))
return false;
}
return true;
} // execUpdate()
} // class libWebThumbnail