/
main.php
446 lines (388 loc) · 12.8 KB
/
main.php
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/*
* Name: Image Manager
* Author: Shish <webmaster@shishnet.org>
* Modified by: jgen <jgen.tech@gmail.com>
* Link: http://code.shishnet.org/shimmie2/
* Description: Handle the image database
* Visibility: admin
*/
/**
* An image is being added to the database.
*/
class ImageAdditionEvent extends Event {
var $user, $image;
/**
* Inserts a new image into the database with its associated
* information. Also calls TagSetEvent to set the tags for
* this new image.
*
* @sa TagSetEvent
* @param $user The user adding the image
* @param $image The new image to add.
*/
public function ImageAdditionEvent(User $user, Image $image) {
$this->image = $image;
$this->user = $user;
}
}
class ImageAdditionException extends SCoreException {
var $error;
public function __construct($error) {
$this->error = $error;
}
}
/**
* An image is being deleted.
*/
class ImageDeletionEvent extends Event {
var $image;
/**
* Deletes an image.
* Used by things like tags and comments handlers to
* clean out related rows in their tables.
*
* @param $image The image being deleted
*/
public function ImageDeletionEvent(Image $image) {
$this->image = $image;
}
}
/**
* An image is being replaced.
*/
class ImageReplaceEvent extends Event {
var $id, $image;
/**
* Replaces an image.
* Updates an existing ID in the database to use a new image
* file, leaving the tags and such unchanged. Also removes
* the old image file and thumbnail from the disk.
*
* @param $id
* The ID of the image to replace
* @param $image
* The image object of the new image to use
*/
public function ImageReplaceEvent($id, Image $image) {
$this->id = $id;
$this->image = $image;
}
}
class ImageReplaceException extends SCoreException {
var $error;
public function __construct($error) {
$this->error = $error;
}
}
/**
* Request a thumbnail be made for an image object.
*/
class ThumbnailGenerationEvent extends Event {
var $hash, $type, $force;
/**
* Request a thumbnail be made for an image object
*
* @param $hash The unique hash of the image
* @param $type The type of the image
*/
public function ThumbnailGenerationEvent($hash, $type, $force=false) {
$this->hash = $hash;
$this->type = $type;
$this->force = $force;
}
}
/*
* ParseLinkTemplateEvent:
* $link -- the formatted link
* $original -- the formatting string, for reference
* $image -- the image who's link is being parsed
*/
class ParseLinkTemplateEvent extends Event {
var $link, $original, $image;
public function ParseLinkTemplateEvent($link, Image $image) {
$this->link = $link;
$this->original = $link;
$this->image = $image;
}
public function replace($needle, $replace) {
$this->link = str_replace($needle, $replace, $this->link);
}
}
/**
* A class to handle adding / getting / removing image files from the disk.
*/
class ImageIO extends Extension {
public function onInitExt(InitExtEvent $event) {
global $config;
$config->set_default_int('thumb_width', 192);
$config->set_default_int('thumb_height', 192);
$config->set_default_int('thumb_quality', 75);
$config->set_default_int('thumb_mem_limit', parse_shorthand_int('8MB'));
$config->set_default_string('thumb_convert_path', 'convert.exe');
if(function_exists("exif_read_data")) {
$config->set_default_bool('image_show_meta', false);
}
$config->set_default_string('image_ilink', '');
$config->set_default_string('image_tlink', '');
$config->set_default_string('image_tip', '$tags // $size // $filesize');
$config->set_default_string('upload_collision_handler', 'error');
$config->set_default_int('image_expires', (60*60*24*31) ); // defaults to one month
}
public function onPageRequest(PageRequestEvent $event) {
if($event->page_matches("image/delete")) {
global $page, $user;
if($user->can("delete_image") && isset($_POST['image_id']) && $user->check_auth_token()) {
$image = Image::by_id($_POST['image_id']);
if($image) {
send_event(new ImageDeletionEvent($image));
$page->set_mode("redirect");
if(isset($_SERVER['HTTP_REFERER']) && !strstr($_SERVER['HTTP_REFERER'], 'post/view')) {
$page->set_redirect($_SERVER['HTTP_REFERER']);
}
else {
$page->set_redirect(make_link("post/list"));
}
}
}
}
else if($event->page_matches("image/replace")) {
global $page, $user;
if($user->can("replace_image") && isset($_POST['image_id']) && $user->check_auth_token()) {
$image = Image::by_id($_POST['image_id']);
if($image) {
$page->set_mode("redirect");
$page->set_redirect(make_link('upload/replace/'.$image->id));
} else {
/* Invalid image ID */
throw new ImageReplaceException("Image to replace does not exist.");
}
}
}
else if($event->page_matches("image")) {
$num = int_escape($event->get_arg(0));
$this->send_file($num, "image");
}
else if($event->page_matches("thumb")) {
$num = int_escape($event->get_arg(0));
$this->send_file($num, "thumb");
}
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event) {
global $user;
global $config;
if($user->can("delete_image")) {
$event->add_part($this->theme->get_deleter_html($event->image->id));
}
/* In the future, could perhaps allow users to replace images that they own as well... */
if ($user->can("replace_image")) {
$event->add_part($this->theme->get_replace_html($event->image->id));
}
}
public function onImageAddition(ImageAdditionEvent $event) {
try {
$this->add_image($event->image);
}
catch(ImageAdditionException $e) {
throw new UploadException($e->error);
}
}
public function onImageDeletion(ImageDeletionEvent $event) {
$event->image->delete();
}
public function onImageReplace(ImageReplaceEvent $event) {
try {
$this->replace_image($event->id, $event->image);
}
catch(ImageReplaceException $e) {
throw new UploadException($e->error);
}
}
public function onUserPageBuilding(UserPageBuildingEvent $event) {
global $user;
global $config;
$u_id = url_escape($event->display_user->id);
$i_image_count = Image::count_images(array("user_id={$event->display_user->id}"));
$i_days_old = ((time() - strtotime($event->display_user->join_date)) / 86400) + 1;
$h_image_rate = sprintf("%.1f", ($i_image_count / $i_days_old));
$images_link = make_link("post/list/user_id=$u_id/1");
$event->add_stats("<a href='$images_link'>Images uploaded</a>: $i_image_count, $h_image_rate per day");
}
public function onSetupBuilding(SetupBuildingEvent $event) {
global $config;
$sb = new SetupBlock("Image Options");
$sb->position = 30;
// advanced only
//$sb->add_text_option("image_ilink", "Image link: ");
//$sb->add_text_option("image_tlink", "<br>Thumbnail link: ");
$sb->add_text_option("image_tip", "Image tooltip: ");
$sb->add_choice_option("upload_collision_handler", array('Error'=>'error', 'Merge'=>'merge'), "<br>Upload collision handler: ");
if(function_exists("exif_read_data")) {
$sb->add_bool_option("image_show_meta", "<br>Show metadata: ");
}
$event->panel->add_block($sb);
$thumbers = array();
$thumbers['Built-in GD'] = "gd";
$thumbers['ImageMagick'] = "convert";
$sb = new SetupBlock("Thumbnailing");
$sb->add_choice_option("thumb_engine", $thumbers, "Engine: ");
$sb->add_label("<br>Size ");
$sb->add_int_option("thumb_width");
$sb->add_label(" x ");
$sb->add_int_option("thumb_height");
$sb->add_label(" px at ");
$sb->add_int_option("thumb_quality");
$sb->add_label(" % quality ");
if($config->get_string("thumb_engine") == "gd") {
$sb->add_shorthand_int_option("thumb_mem_limit", "<br>Max memory use: ");
}
$event->panel->add_block($sb);
}
// add image {{{
private function add_image($image) {
global $page, $user, $database, $config;
/*
* Validate things
*/
if(strlen(trim($image->source)) == 0) {
$image->source = null;
}
/*
* Check for an existing image
*/
$existing = Image::by_hash($image->hash);
if(!is_null($existing)) {
$handler = $config->get_string("upload_collision_handler");
if($handler == "merge" || isset($_GET['update'])) {
$merged = array_merge($image->get_tag_array(), $existing->get_tag_array());
send_event(new TagSetEvent($existing, $merged));
if(isset($_GET['rating']) && isset($_GET['update']) && class_exists("Ratings")){
send_event(new RatingSetEvent($existing, $user, $_GET['rating']));
}
if(isset($_GET['source']) && isset($_GET['update'])){
send_event(new SourceSetEvent($existing, $_GET['source']));
}
return null;
}
else {
$error = "Image <a href='".make_link("post/view/{$existing->id}")."'>{$existing->id}</a> ".
"already has hash {$image->hash}:<p>".Themelet::build_thumb_html($existing);
throw new ImageAdditionException($error);
}
}
// actually insert the info
$database->Execute(
"INSERT INTO images(
owner_id, owner_ip, filename, filesize,
hash, ext, width, height, posted, source
)
VALUES (
:owner_id, :owner_ip, :filename, :filesize,
:hash, :ext, :width, :height, now(), :source
)",
array(
"owner_id"=>$user->id, "owner_ip"=>$_SERVER['REMOTE_ADDR'], "filename"=>$image->filename, "filesize"=>$image->filesize,
"hash"=>$image->hash, "ext"=>$image->ext, "width"=>$image->width, "height"=>$image->height, "source"=>$image->source
)
);
$image->id = $database->get_last_insert_id('images_id_seq');
log_info("image", "Uploaded Image #{$image->id} ({$image->hash})");
# at this point in time, the image's tags haven't really been set,
# and so, having $image->tag_array set to something is a lie (but
# a useful one, as we want to know what the tags are /supposed/ to
# be). Here we correct the lie, by first nullifying the wrong tags
# then using the standard mechanism to set them properly.
$tags_to_set = $image->get_tag_array();
$image->tag_array = array();
send_event(new TagSetEvent($image, $tags_to_set));
if($image->source) {
log_info("core-image", "Source for Image #{$image->id} set to: {$image->source}");
}
}
// }}} end add
// fetch image {{{
private function send_file($image_id, $type) {
global $config;
global $database;
$image = Image::by_id($image_id);
global $page;
if(!is_null($image)) {
$page->set_mode("data");
if($type == "thumb") {
$page->set_type("image/jpeg");
$file = $image->get_thumb_filename();
}
else {
$page->set_type($image->get_mime_type());
$file = $image->get_image_filename();
}
$page->set_data(file_get_contents($file));
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]);
}
else {
$if_modified_since = "";
}
$gmdate_mod = gmdate('D, d M Y H:i:s', filemtime($file)) . ' GMT';
if($if_modified_since == $gmdate_mod) {
$page->add_http_header("HTTP/1.0 304 Not Modified",3);
}
else {
$page->add_http_header("Last-Modified: $gmdate_mod");
if ( $config->get_int("image_expires") ) {
$expires = date(DATE_RFC1123, time() + $config->get_int("image_expires"));
} else {
$expires = 'Fri, 2 Sep 2101 12:42:42 GMT'; // War was beginning
}
$page->add_http_header('Expires: '.$expires);
}
}
else {
$page->set_title("Not Found");
$page->set_heading("Not Found");
$page->add_block(new Block("Navigation", "<a href='".make_link()."'>Index</a>", "left", 0));
$page->add_block(new Block("Image not in database",
"The requested image was not found in the database"));
}
}
// }}} end fetch
// replace image {{{
private function replace_image($id, $image) {
global $page;
global $user;
global $database;
global $config;
/* Check to make sure the image exists. */
$existing = Image::by_id($id);
if(is_null($existing)) {
throw new ImageReplaceException("Image to replace does not exist!");
}
if(strlen(trim($image->source)) == 0) {
$image->source = $existing->get_source();
}
/*
This step could be optional, ie: perhaps move the image somewhere
and have it stored in a 'replaced images' list that could be
inspected later by an admin?
*/
log_debug("image", "Removing image with hash ".$existing->hash);
$existing->remove_image_only(); // Actually delete the old image file from disk
// Update the data in the database.
$database->Execute(
"UPDATE images SET
filename = :filename, filesize = :filesize, hash = :hash,
ext = :ext, width = :width, height = :height, source = :source
WHERE
id = :id
",
array(
"filename"=>$image->filename, "filesize"=>$image->filesize, "hash"=>$image->hash,
"ext"=>$image->ext, "width"=>$image->width, "height"=>$image->height, "source"=>$image->source,
"id"=>$id
)
);
log_info("image", "Replaced Image #{$id} with ({$image->hash})");
}
// }}} end replace
} // end of class ImageIO
?>