-
Notifications
You must be signed in to change notification settings - Fork 31
/
db_input.php
719 lines (541 loc) · 30.1 KB
/
db_input.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<?php
/**
* Coppermine Photo Gallery
*
* v1.0 originally written by Gregory Demar
*
* @copyright Copyright (c) 2003-2020 Coppermine Dev Team
* @license GNU General Public License version 3 or later; see LICENSE
*
* db_input.php
* @since 1.6.08
*/
define('IN_COPPERMINE', true);
define('DB_INPUT_PHP', true);
define('DISPLAYIMAGE_PHP', true);
require('include/init.inc.php');
require('include/picmgmt.inc.php');
require('include/mailer.inc.php');
require('include/smilies.inc.php');
/*known issue: code was edited to not count URL in comment character count. However
this resulted in the character count not being respected at all.
With the new code, the long urls don't affect the display of the hyperlinked word.
However, I can't figure out how to make the code respect the max comment word length and max comment length.
Formatted and unformatted words that are longer than the allowed setting do not get truncated. -Thu */
function check_comment(&$str)
{
global $CONFIG, $lang_bad_words;
// convert some entities
$str = str_replace(array('&', '"', '<', '>', ' ', '''), array('&', '"', '<', '>', ' ', "'"), $str);
// if '<' is no start of a tag (e.g. a lower than + equal), convert it to entity to prevent strip_tags() to clip the comment wrongly
$str = preg_replace("/<([0-9<>=])/", '<\\1', $str);
// strip tags and cut to max allowed length
$str = trim(substr(strip_tags($str), 0, $CONFIG['max_com_size']));
// re convert some entities
$str = str_replace(array('"', '<', '>', "'"), array('"', '<', '>', '''), $str);
if ($CONFIG['filter_bad_words']) {
$ercp = array();
foreach ($lang_bad_words as $word) {
$ercp[] = '/' . ($word[0] == '*' ? '' : '\b') . str_replace('*', '', $word) . ($word[(strlen($word) - 1)] == '*' ? '' : '\b') . '/i';
}
$str = preg_replace($ercp, '(...)', $str);
}
$com_words = explode(' ', strip_tags(bb_decode($str)));
$replacements = array();
foreach ($com_words as $key => $word) {
if (utf_strlen($word) > $CONFIG['max_com_wlength']) {
$replacements[] = $word;
}
}
$str = str_replace($replacements, '(...)', $str);
}
if (!$superCage->get->keyExists('event') && !$superCage->post->keyExists('event')) {
cpg_die(CRITICAL_ERROR, $lang_errors['param_missing'], __FILE__, __LINE__);
//Check if the form token is valid
} elseif (!checkFormToken()) {
cpg_die(ERROR, $lang_errors['invalid_form_token'], __FILE__, __LINE__);
}
if ($matches = $superCage->post->getMatched('event', '/^[a-z_]+$/')) {
$event = $matches[0];
} elseif ($matches = $superCage->get->getMatched('event', '/^[a-z_]+$/')) {
$event = $matches[0];
} else {
$event = '';
}
switch ($event) {
case 'comment_update':
if (!(USER_CAN_POST_COMMENTS)) {
if ($CONFIG['log_mode'] != 0) {
log_write('Denied privileged access to db_input.php (attempt to update a comment) for user '.$USER_DATA['user_name'].' at ' . $hdr_ip, CPG_SECURITY_LOG);
}
cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
}
$msg_body = $superCage->post->getEscaped('msg_body');
$msg_id = $superCage->post->getInt('msg_id');
check_comment($msg_body);
if (empty($msg_body)) {
cpg_die(ERROR, $lang_db_input_php['err_comment_empty'], __FILE__, __LINE__);
}
if (GALLERY_ADMIN_MODE) {
cpg_db_query("UPDATE {$CONFIG['TABLE_COMMENTS']} SET msg_body = '$msg_body' WHERE msg_id = '$msg_id'");
} elseif (USER_ID) {
if ($CONFIG['comment_approval'] == 1) {
cpg_db_query("UPDATE {$CONFIG['TABLE_COMMENTS']} SET msg_body = '$msg_body', approval = 'NO' WHERE msg_id = '$msg_id' AND author_id ='" . USER_ID . "' LIMIT 1");
} else {
cpg_db_query("UPDATE {$CONFIG['TABLE_COMMENTS']} SET msg_body = '$msg_body' WHERE msg_id = '$msg_id' AND author_id ='" . USER_ID . "' LIMIT 1");
}
} else {
if ($CONFIG['comment_approval'] != 0) {
cpg_db_query("UPDATE {$CONFIG['TABLE_COMMENTS']} SET msg_body = '$msg_body', approval = 'NO' WHERE msg_id = '$msg_id' AND author_md5_id = '{$USER['ID']}' AND author_id = 0 LIMIT 1");
} else {
cpg_db_query("UPDATE {$CONFIG['TABLE_COMMENTS']} SET msg_body = '$msg_body' WHERE msg_id = '$msg_id' AND author_md5_id = '{$USER['ID']}' AND author_id = 0 LIMIT 1");
}
}
CPGPluginAPI::action('comment_update', $msg_id);
$header_location = (@preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE'))) ? 'Refresh: 0; URL=' : 'Location: ';
$result = cpg_db_query("SELECT pid FROM {$CONFIG['TABLE_COMMENTS']} WHERE msg_id = '$msg_id'");
if (!$result->numRows()) {
cpgRedirectPage('index.php', $lang_common['information'], $lang_db_input_php['com_updated'], 1);
} else {
$comment_data = $result->fetchAssoc();
$redirect = "displayimage.php?pid=" . $comment_data['pid'];
cpgRedirectPage($redirect, $lang_common['information'], $lang_db_input_php['com_updated'], 1);
}
$result->free();
break;
case 'comment':
if (!(USER_CAN_POST_COMMENTS)) {
if ($CONFIG['log_mode'] != 0) {
log_write('Denied privileged access to db_input.php (attempt to post a comment) for user '.$USER_DATA['user_name'].' at ' . $hdr_ip, CPG_SECURITY_LOG);
}
cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
}
if (($CONFIG['comment_captcha'] == 1) || ($CONFIG['comment_captcha'] == 2 && !USER_ID)) {
if (!captcha_plugin_enabled('comment')) {
require("include/captcha.inc.php");
$matches = $superCage->post->getMatched('confirmCode', '/^[a-zA-Z0-9]+$/');
if (!$matches[0] || !PhpCaptcha::Validate($matches[0])) {
if ($CONFIG['log_mode'] != 0) {
log_write('Captcha authentication for comment failed for user '.$USER_DATA['user_name'].' at ' . $hdr_ip, CPG_SECURITY_LOG);
}
cpg_die(ERROR, $lang_errors['captcha_error'], __FILE__, __LINE__);
}
} else {
CPGPluginAPI::action('captcha_comment_validate', null);
}
}
$spam = 'NO';
$msg_author = $superCage->post->getEscaped('msg_author');
$msg_body = $superCage->post->getEscaped('msg_body');
$pid = $superCage->post->getInt('pid');
check_comment($msg_body);
check_comment($msg_author);
if (empty($msg_author) || empty($msg_body)) {
cpg_die(ERROR, $lang_db_input_php['empty_name_or_com'], __FILE__, __LINE__);
}
$result = cpg_db_query("SELECT comments FROM {$CONFIG['TABLE_PICTURES']} AS p INNER JOIN {$CONFIG['TABLE_ALBUMS']} AS a ON a.aid = p.aid WHERE pid = $pid");
if (!$result->numRows()) {
cpg_die(ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
}
$album_data = $result->fetchAssoc(true);
if ($album_data['comments'] != 'YES') {
cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
}
if (!$CONFIG['disable_comment_flood_protect']) {
$result = cpg_db_query("SELECT author_md5_id, author_id FROM {$CONFIG['TABLE_COMMENTS']} WHERE pid = $pid ORDER BY msg_id DESC LIMIT 1");
if ($result->numRows()) {
$last_com_data = $result->fetchAssoc();
if ((USER_ID && $last_com_data['author_id'] == USER_ID) || (!USER_ID && $last_com_data['author_md5_id'] == $USER['ID'])) {
if ($CONFIG['log_mode'] != 0) {
log_write('Attempt to comment-flood (PID: '.$pid.') denied for user '.$USER_DATA['user_name'].' at ' . $hdr_ip, CPG_GLOBAL_LOG);
}
cpg_die(ERROR, $lang_db_input_php['no_flood'], __FILE__, __LINE__);
}
}
$result->free();
}
$akismet_approval_needed = 0;
if ($CONFIG['comment_akismet_api_key'] != '') {
require_once('include/akismet.inc.php');
$comment_evaluation_array = array();
if ($superCage->server->keyExists('REMOTE_ADDR')) {
$comment_evaluation_array['user_ip'] = $superCage->server->getEscaped('REMOTE_ADDR');
} else {
$comment_evaluation_array['user_ip'] = '';
}
if ($superCage->server->keyExists('HTTP_USER_AGENT')) {
$comment_evaluation_array['user_agent'] = $superCage->server->getEscaped('HTTP_USER_AGENT');
} else {
$comment_evaluation_array['user_agent'] = '';
}
if ($superCage->server->keyExists('HTTP_REFERER')) {
$comment_evaluation_array['referrer'] = $superCage->server->getEscaped('HTTP_REFERER');;
} else {
$comment_evaluation_array['referrer'] = '';
}
if ($superCage->server->keyExists('REMOTE_PORT')) {
$comment_evaluation_array['REMOTE_PORT'] = $superCage->server->getEscaped('REMOTE_PORT');
} else {
$comment_evaluation_array['REMOTE_PORT'] = '';
}
if ($superCage->server->keyExists('REQUEST_METHOD')) {
$comment_evaluation_array['REQUEST_METHOD'] = $superCage->server->getEscaped('REQUEST_METHOD');
} else {
$comment_evaluation_array['REQUEST_METHOD'] = '';
}
$comment_evaluation_array['permalink'] = $CONFIG['site_url'] . 'displayimage.php?pid='.$pid;
$comment_evaluation_array['comment_type'] = 'comment';
$comment_evaluation_array['comment_author'] = $msg_author;
$comment_evaluation_array['comment_author_email'] = '';
$comment_evaluation_array['comment_author_url'] = '';
$comment_evaluation_array['comment_content'] = $msg_body;
}
if (!USER_ID) { // Anonymous users, we need to use META refresh to save the cookie
// check that the username the anonymous user entered (including prefix for anonymous comments authors) is not being used by a registered user
if ($cpg_udb->get_user_id($CONFIG['comments_anon_pfx'].$msg_author)) {
cpg_die(ERROR, $lang_db_input_php['com_author_error'], __FILE__, __LINE__);
}
// If username for comment is same as default username then display error message
if ($msg_author == $lang_display_comments['your_name']) {
cpg_die(ERROR, $lang_display_comments['default_username_message'], __FILE__, __LINE__);
}
// Perform Akismet check if applicable for guests
if ($CONFIG['comment_akismet_api_key'] != '') {
$akismet_result = cpg_akismet_submit_data($comment_evaluation_array);
if ($akismet_result == TRUE) { // returns true if Akismet thinks the comment is spam
// Increase the spam counter by one
$spam_count = $CONFIG['comment_akismet_counter'] + 1;
cpg_config_set('comment_akismet_counter', $spam_count);
if ($CONFIG['comment_akismet_enable'] == 0) {
$akismet_approval_needed = 1; // Temporarily just set comment approval to "on"
$spam = 'YES';
} elseif ($CONFIG['comment_akismet_enable'] == 1) {
$redirect = "displayimage.php?pid=$pid";
pageheader($lang_display_comments['comment_rejected'], "<meta http-equiv=\"refresh\" content=\"5;url=$redirect\" />");
msg_box($lang_db_input_php['info'], $lang_display_comments['comment_rejected'], $lang_common['continue'], $redirect);
pagefooter();
exit;
} else {
$redirect = "displayimage.php?pid=$pid";
pageheader($lang_db_input_php['com_added'], "<meta http-equiv=\"refresh\" content=\"1;url=$redirect\" />");
msg_box($lang_db_input_php['info'], $lang_db_input_php['com_added'], $lang_common['continue'], $redirect);
pagefooter();
exit;
}
}
}
if ($CONFIG['comment_approval'] != 0 || $akismet_approval_needed == 1) { // comments need approval, set approval status to "no"
$app = 'NO';
} else { //comments do not need approval, we can set approval status to "yes"
$app = 'YES';
}
cpg_db_query("INSERT INTO {$CONFIG['TABLE_COMMENTS']} (pid, msg_author, msg_body, msg_date, author_md5_id, author_id, msg_raw_ip, msg_hdr_ip, approval, spam) VALUES ('$pid', '{$CONFIG['comments_anon_pfx']}$msg_author', '$msg_body', NOW(), '{$USER['ID']}', '0', '$raw_ip', '$hdr_ip', '$app', '$spam')");
$USER['name'] = $msg_author;
CPGPluginAPI::action('comment_add', array('msg_id' => cpg_db_last_insert_id(), 'pid' => $pid, 'msg_author' => $msg_author, 'author_id' => '0', 'msg_body' => $msg_body, 'approval' => $app));
$redirect = "displayimage.php?pid=$pid";
if ($CONFIG['email_comment_notification']) {
$mail_body = '<p>' . bb_decode(process_smilies($msg_body, $CONFIG['ecards_more_pic_target'])) . '</p>' . $LINEBREAK .$lang_db_input_php['email_comment_body'] . ' ' . $CONFIG['ecards_more_pic_target'].(substr($CONFIG["ecards_more_pic_target"], -1) == '/' ? '' : '/').$redirect;
cpg_mail('admin', $lang_db_input_php['email_comment_subject'], make_clickable($mail_body));
}
pageheader($lang_db_input_php['com_added'], "<meta http-equiv=\"refresh\" content=\"1;url=$redirect\" />");
msg_box($lang_db_input_php['info'], $lang_db_input_php['com_added'], $lang_common['continue'], $redirect);
pagefooter();
exit;
} else { // Registered users, we can use Location to redirect
// Perform Akismet check if applicable for registered users
if ($CONFIG['comment_akismet_api_key'] != '' && $CONFIG['comment_akismet_group'] != 1) {
//$comment_evaluation_array['comment_author_email'] = '';// TODO: populate the email address from the user's profile
$akismet_result = cpg_akismet_submit_data($comment_evaluation_array);
if ($akismet_result == TRUE) { // returns true if Akismet thinks the comment is spam
// Increase the spam counter by one
$spam_count = $CONFIG['comment_akismet_counter'] + 1;
cpg_config_set('comment_akismet_counter', $spam_count);
if ($CONFIG['comment_akismet_enable'] == 0) {
$akismet_approval_needed = 1; // Temporarily just set comment approval to "on"
$spam = 'YES';
} elseif ($CONFIG['comment_akismet_enable'] == 1) {
$redirect = "displayimage.php?pid=$pid";
cpgRedirectPage($redirect, $lang_db_input_php['info'], $lang_display_comments['comment_rejected'], 5);
} else {
$redirect = "displayimage.php?pid=$pid";
cpgRedirectPage($redirect, $lang_db_input_php['info'], $lang_db_input_php['com_added'], 1);
}
}
}
if (($CONFIG['comment_approval'] == 1 && !USER_IS_ADMIN) || $akismet_approval_needed == 1) { // comments need approval, set approval status to "no"
$app = 'NO';
} else { //comments do not need approval, we can set approval status to "yes"
$app = 'YES';
}
cpg_db_query("INSERT INTO {$CONFIG['TABLE_COMMENTS']} (pid, msg_author, msg_body, msg_date, author_md5_id, author_id, msg_raw_ip, msg_hdr_ip, approval, spam) VALUES ('$pid', '" . addslashes(USER_NAME) . "', '$msg_body', NOW(), '', '" . USER_ID . "', '$raw_ip', '$hdr_ip', '$app', '$spam')");
CPGPluginAPI::action('comment_add', array('msg_id' => cpg_db_last_insert_id(), 'pid' => $pid, 'msg_author' => $msg_author, 'author_id' => USER_ID, 'msg_body' => $msg_body, 'approval' => $app));
$redirect = "displayimage.php?pid=$pid";
if ($CONFIG['email_comment_notification'] && !USER_IS_ADMIN ) {
$mail_body = "<p>" . bb_decode(process_smilies($msg_body, $CONFIG['ecards_more_pic_target'])) . '</p>' . $LINEBREAK .$lang_db_input_php['email_comment_body'] . ' ' . $CONFIG['ecards_more_pic_target'] . (substr($CONFIG["ecards_more_pic_target"], -1) == '/' ? '' : '/') . $redirect;
cpg_mail('admin', $lang_db_input_php['email_comment_subject'], make_clickable($mail_body));
}
cpgRedirectPage($redirect, $lang_db_input_php['info'], $lang_db_input_php['com_added'], 1);
}
break;
case 'album_update':
if (!(user_is_allowed() || GALLERY_ADMIN_MODE)) {
if ($CONFIG['log_mode'] != 0) {
log_write('Denied privileged access to db_input.php (album update) for user '.$USER_DATA['user_name'].' at ' . $hdr_ip, CPG_SECURITY_LOG);
}
cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
}
$aid = $superCage->post->getInt('aid');
$title = $superCage->post->getEscaped('title');
$category = $superCage->post->getInt('category');
$description = $superCage->post->getEscaped('description');
$keyword = $superCage->post->getEscaped('keyword');
$thumb = $superCage->post->getInt('thumb');
$visibility = $superCage->post->getInt('visibility');
$uploads = $superCage->post->getAlpha('uploads') == 'YES' ? 'YES' : 'NO';
$comments = $superCage->post->getAlpha('comments') == 'YES' ? 'YES' : 'NO';
$votes = $superCage->post->getAlpha('votes') == 'YES' ? 'YES' : 'NO';
// Get the old alb_password before update
$result = cpg_db_query("SELECT alb_password FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid = $aid");
$row = $result->fetchAssoc(true);
// If there is some value in alb_password then it means album was previously password protected
if ($row['alb_password']) {
$old_password = $row['alb_password'];
} else {
$old_password = null;
}
// Get the password only if password_protect checkbox is checked
if ($superCage->post->keyExists('password_protect')) {
$password = $superCage->post->getEscaped('alb_password');
$password_hint = $superCage->post->getEscaped('alb_password_hint');
// We will change or add the password only if it is not empty
if (trim($password)) {
$password = md5($password);
} elseif (!$old_password && !trim($password)) {
$password = null;
$password_hint = null;
} else {
$password = $old_password;
}
} else {
$password = null;
$password_hint = null;
}
$visibility = !empty($password) ? FIRST_USER_CAT + USER_ID : $visibility;
if (!$title) {
cpg_die(ERROR, $lang_db_input_php['alb_need_title'], __FILE__, __LINE__);
}
$query = "UPDATE {$CONFIG['TABLE_ALBUMS']} SET title = '$title', description = '$description', category = $category, thumb = $thumb, comments = '$comments', votes = '$votes', visibility = $visibility, alb_password = '$password', alb_password_hint = '$password_hint', keyword = '$keyword'";
if (GALLERY_ADMIN_MODE) {
/* TODO: re-enable and test feature when it's clear how it should work (see http://forum.coppermine-gallery.net/index.php/topic,64408.0.html)
$moderator_group = $superCage->post->getInt('moderator_group');
$query .= ", moderator_group = '$moderator_group'";
*/
$query .= ", uploads = '$uploads'";
}
$query .= " WHERE aid = $aid";
cpg_db_query($query);
if (!cpg_db_affected_rows()) {
cpgRedirectPage('modifyalb.php?album=' . $aid, $lang_db_input_php['info'], $lang_db_input_php['no_udp_needed'], 0);
} else {
cpgRedirectPage('modifyalb.php?album=' . $aid, $lang_db_input_php['info'], $lang_db_input_php['alb_updated'], 0);
}
break;
case 'album_reset':
if (!GALLERY_ADMIN_MODE) {
if ($CONFIG['log_mode'] != 0) {
log_write('Denied privileged access to db_input.php (album reset) for user '.$USER_DATA['user_name'].' at ' . $hdr_ip, CPG_SECURITY_LOG);
}
cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
}
$counter_affected_rows = 0;
$aid = $superCage->post->getInt('aid');
$reset_views = $superCage->post->getInt('reset_views');
$reset_rating = $superCage->post->getInt('reset_rating');
$delete_comments = $superCage->post->getInt('delete_comments');
$delete_files = $superCage->post->getInt('delete_files');
if ($reset_views) { // if reset_views start
cpg_db_query("UPDATE {$CONFIG['TABLE_PICTURES']} SET hits = 0 WHERE aid = $aid");
if (cpg_db_affected_rows()) {
$counter_affected_rows++;
}
cpg_db_query("DELETE FROM s USING {$CONFIG['TABLE_HIT_STATS']} AS s INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.pid = s.pid WHERE p.aid = $aid");
} // if reset_views end
if ($reset_rating) { // if reset_rating start
cpg_db_query("UPDATE {$CONFIG['TABLE_PICTURES']} SET pic_rating = 0, votes = 0 WHERE aid = $aid");
if (cpg_db_affected_rows()) {
$counter_affected_rows++;
}
cpg_db_query("DELETE FROM v USING {$CONFIG['TABLE_VOTE_STATS']} AS v INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.pid = v.pid WHERE p.aid = $aid");
cpg_db_query("DELETE FROM v USING {$CONFIG['TABLE_VOTES']} AS v INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.pid = v.pic_id WHERE p.aid = $aid");
} // if reset_rating end
if ($delete_comments) { // if delete_comments start
cpg_db_query("DELETE FROM c USING {$CONFIG['TABLE_COMMENTS']} AS c INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.pid = c.pid WHERE p.aid = $aid");
if (cpg_db_affected_rows()) {
$counter_affected_rows++;
}
} // if delete_comments end
if ($delete_files) { // if delete_files start
cpg_db_query("DELETE FROM {$CONFIG['TABLE_PICTURES']} WHERE aid = $aid");
if (cpg_db_affected_rows()) {
$counter_affected_rows++;
}
} // if delete_files end
if ($counter_affected_rows == 0) {
cpg_die(INFORMATION, $lang_db_input_php['no_udp_needed'], __FILE__, __LINE__);
}
pageheader($lang_db_input_php['alb_updated'], "<meta http-equiv=\"refresh\" content=\"1;url=modifyalb.php?album=$aid\" />");
msg_box($lang_db_input_php['info'], $lang_db_input_php['alb_updated'], $lang_common['continue'], "modifyalb.php?album=$aid");
pagefooter();
exit;
break;
case 'picture':
if (!USER_CAN_UPLOAD_PICTURES && !USER_CAN_CREATE_ALBUMS) {
if ($CONFIG['log_mode'] != 0) {
log_write('Denied privileged access to db_input.php (upload picture) for user '.$USER_DATA['user_name'].' at ' . $hdr_ip, CPG_SECURITY_LOG);
}
cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
}
$album = $superCage->post->getInt('album');
$title = $superCage->post->getEscaped('title');
$caption = $superCage->post->getEscaped('caption');
$keywords = $superCage->post->getEscaped('keywords');
$user1 = $superCage->post->getEscaped('user1');
$user2 = $superCage->post->getEscaped('user2');
$user3 = $superCage->post->getEscaped('user3');
$user4 = $superCage->post->getEscaped('user4');
// Check if the album id provided is valid
if (!(GALLERY_ADMIN_MODE || user_is_allowed())) {
$result = cpg_db_query("SELECT category FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid = $album AND (owner = " . USER_ID . " OR category = " . (USER_ID + FIRST_USER_CAT) . (USER_CAN_UPLOAD_PICTURES ? ' OR uploads = "YES"' : '') . ")");
if ($result->numRows() == 0) {
cpg_die(ERROR, $lang_db_input_php['unknown_album'], __FILE__, __LINE__);
}
$row = $result->fetchAssoc(true);
$category = $row['category'];
} else {
$result = cpg_db_query("SELECT category FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid = $album");
if ($result->numRows() == 0) {
cpg_die(ERROR, $lang_db_input_php['unknown_album'], __FILE__, __LINE__);
}
$row = $result->fetchAssoc(true);
$category = $row['category'];
}
// Test if the filename of the temporary uploaded picture is empty
// getRaw is safe here since this filename is generated by the server
if ($superCage->files->getRaw("/userpicture/tmp_name") == '') {
cpg_die(ERROR, $lang_db_input_php['no_pic_uploaded'], __FILE__, __LINE__);
}
// Pictures are moved in a directory named 10000 + USER_ID
if (USER_ID && $CONFIG['silly_safe_mode'] != 1) {
$filepath = $CONFIG['userpics'] . (USER_ID + FIRST_USER_CAT);
$dest_dir = $CONFIG['fullpath'] . $filepath;
if (!is_dir($dest_dir)) {
mkdir($dest_dir, octdec($CONFIG['default_dir_mode']));
if (!is_dir($dest_dir)) {
cpg_die(CRITICAL_ERROR, sprintf($lang_db_input_php['err_mkdir'], $dest_dir), __FILE__, __LINE__, true);
}
chmod($dest_dir, octdec($CONFIG['default_dir_mode']));
$fp = fopen($dest_dir . '/index.php', 'w');
fwrite($fp, ' ');
fclose($fp);
}
// Upload pictures in a sub-directory named according to the album ID
if ($CONFIG['upload_create_album_directory']) {
$filepath .= '/'.$album;
$dest_dir .= '/'.$album;
if (!is_dir($dest_dir)) {
mkdir($dest_dir, octdec($CONFIG['default_dir_mode']));
if (!is_dir($dest_dir)) {
cpg_die(CRITICAL_ERROR, sprintf($lang_db_input_php['err_mkdir'], $dest_dir), __FILE__, __LINE__, true);
}
chmod($dest_dir, octdec($CONFIG['default_dir_mode']));
$fp = fopen($dest_dir . '/index.php', 'w');
fwrite($fp, ' ');
fclose($fp);
}
}
$dest_dir .= '/';
$filepath .= '/';
} else {
$filepath = $CONFIG['userpics'];
$dest_dir = $CONFIG['fullpath'] . $filepath;
}
// Check that target dir is writable
if (!is_writable($dest_dir)) {
cpg_die(CRITICAL_ERROR, sprintf($lang_db_input_php['dest_dir_ro'], $dest_dir), __FILE__, __LINE__, true);
}
$picture_name = $superCage->files->getRaw("/userpicture/name");
$picture_name = CPGPluginAPI::filter('upload_file_name', $picture_name);
// Replace forbidden chars (including white spaces and special chars) with underscores
$picture_name = replace_forbidden($picture_name);
// Check that the file uploaded has a valid extension
if (!preg_match("/(.+)\.(.*?)\Z/", $picture_name, $matches)) {
$matches[1] = 'invalid_fname';
$matches[2] = 'xxx';
}
if ($matches[2] == '' || !is_known_filetype($matches)) {
cpg_die(ERROR, $lang_db_input_php['err_invalid_fext'] . ' ' . $CONFIG['allowed_file_extensions'], __FILE__, __LINE__);
}
// Create a unique name for the uploaded file
$nr = 0;
$picture_name = $matches[1] . '.' . $matches[2];
while (file_exists($dest_dir . $picture_name)) {
$picture_name = $matches[1] . '~' . $nr++ . '.' . $matches[2];
}
$uploaded_pic = $dest_dir . $picture_name;
CPGPluginAPI::action('upload_html_pre_move', $superCage->files->getRaw("/userpicture/tmp_name"));
// Move the picture into its final location
// getRaw is safe here since this filename is generated by the server
if (!move_uploaded_file($superCage->files->getRaw("/userpicture/tmp_name"), $uploaded_pic)) {
cpg_die(CRITICAL_ERROR, sprintf($lang_db_input_php['err_move'], $picture_name, $dest_dir), __FILE__, __LINE__, true);
}
// Change file permission
chmod($uploaded_pic, octdec($CONFIG['default_file_mode']));
// Get picture information
// Check that picture file size is lower than the maximum allowed
if (filesize($uploaded_pic) > ($CONFIG['max_upl_size'] * 1024)) {
@unlink($uploaded_pic);
cpg_die(ERROR, sprintf($lang_db_input_php['err_imgsize_too_large'], $CONFIG['max_upl_size']), __FILE__, __LINE__);
} elseif (is_image($picture_name)) {
$imginfo = cpg_getimagesize($uploaded_pic);
if ($imginfo == null) {
// getimagesize does not recognize the file as a picture
@unlink($uploaded_pic);
cpg_die(ERROR, $lang_db_input_php['err_invalid_img'], __FILE__, __LINE__, true);
} elseif ($imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG && $CONFIG['GIF_support'] == 0) {
// JPEG and PNG only are allowed with GD
@unlink($uploaded_pic);
cpg_die(ERROR, $lang_errors['gd_file_type_err'], __FILE__, __LINE__, true);
// Check that picture size (in pixels) is lower than the maximum allowed
} // Image is ok
}
// Upload is ok
// Create thumbnail and internediate image and add the image into the DB
$result = add_picture($album, $filepath, $picture_name, 0, $title, $caption, $keywords, $user1, $user2, $user3, $user4, $category, $raw_ip, $hdr_ip, $superCage->post->getInt('width'), $superCage->post->getInt('height'));
if ($result !== true) {
@unlink($uploaded_pic);
cpg_die(CRITICAL_ERROR, isset($result['error']) ? $result['error'] : sprintf($lang_db_input_php['err_insert_pic'], $uploaded_pic) . '<br /><br />' . $ERROR, __FILE__, __LINE__, true);
} elseif ($PIC_NEED_APPROVAL) {
pageheader($lang_common['information']);
msg_box($lang_common['information'], $lang_db_input_php['upload_success'], $lang_common['continue'], 'index.php');
if ($CONFIG['upl_notify_admin_email']) {
include_once('include/mailer.inc.php');
cpg_mail('admin', sprintf($lang_db_input_php['notify_admin_email_subject'], $CONFIG['gallery_name']), make_clickable(sprintf($lang_db_input_php['notify_admin_email_body'], USER_NAME, $CONFIG['ecards_more_pic_target'].(substr($CONFIG["ecards_more_pic_target"], -1) == '/' ? '' : '/') .'editpics.php?mode=upload_approval')));
}
pagefooter();
} else {
if (cpg_pw_protected_album_access($CURRENT_PIC_DATA['aid']) === 1) {
$redirect = "thumbnails.php?album=" . $CURRENT_PIC_DATA['aid'];
} else {
$redirect = "displayimage.php?pid=" . $CURRENT_PIC_DATA['pid'];
}
cpgRedirectPage($redirect, $lang_common['information'], $lang_db_input_php['upl_success'], 1);
}
break;
// Unknown event
default:
if ($CONFIG['log_mode'] != 0) {
log_write('Denied privileged access to db_input.php (unknown event) for user '.$USER_DATA['user_name'].' at ' . $hdr_ip, CPG_SECURITY_LOG);
}
cpg_die(CRITICAL_ERROR, $lang_errors['param_missing'], __FILE__, __LINE__);
}
//EOF