/
plupload.php
760 lines (593 loc) · 28.1 KB
/
plupload.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
<?php
header('Content-Type: application/json');
$files_utils = new \MicroweberPackages\Utils\System\Files();
$dangerous = $files_utils->get_dangerous_files_extentions();
if (!mw()->user_manager->session_id() or (mw()->user_manager->session_all() == false)) {
// //session_start();
}
$validate_token = false;
if (!isset($_SERVER['HTTP_REFERER'])) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code":97, "message": "You are not allowed to upload"}}');
} elseif (!stristr($_SERVER['HTTP_REFERER'], site_url())) {
// if (!is_logged()){
// die('{"jsonrpc" : "2.0", "error" : {"code":98, "message": "You cannot upload from remote domains"}}');
// }
}
//if (!is_admin()) {
//$validate_token = mw()->user_manager->csrf_validate($_GET);
// validation is now on middleware
// if ($validate_token == false) {
// header("HTTP/1.1 401 Unauthorized");
// die('{"jsonrpc" : "2.0", "error" : {"code":98, "message": "You are not allowed to upload"}}');
// }
$is_ajax = mw()->url_manager->is_ajax();
if (!$is_ajax) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code":99, "message": "You are not allowed to upload"}}');
}
//}
$host = (parse_url(site_url()));
//$host_dir = false;
$host_dir = 'default';
if(defined('MW_IS_MULTISITE') and MW_IS_MULTISITE){
if (isset($host['host'])) {
$host_dir = $host['host'];
$host_dir = str_ireplace('www.', '', $host_dir);
$host_dir = str_ireplace('.', '-', $host_dir);
}
}
$fileName_ext = isset($_REQUEST['name']) ? $_REQUEST['name'] : '';
$is_ext = get_file_extension($fileName_ext);
$is_ext = strtolower($is_ext);
$is_allowed_file = $files_utils->is_allowed_file($fileName_ext);
if ($is_allowed_file == false) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code":100, "message": "You cannot upload scripts or executable files"}}');
}
$allowed_to_upload = false;
if (is_admin() != false) {
$allowed_to_upload = true;
} else {
$uid = user_id();
if ($uid != 0) {
$user = mw()->user_manager->get_by_id($uid);
if (!empty($user) and isset($user['is_active']) and $user['is_active'] == 1) {
$are_allowed = 'img';
$_REQUEST['path'] = 'media/' . $host_dir . DS . 'user_uploads/user/' . $user['id'] . DS;
if (isset($_REQUEST['autopath']) and $_REQUEST['autopath'] == 'user_hash') {
$up_path = md5($user['id'] . $user['created_at']);
$_REQUEST['path'] = 'media/' . $host_dir . DS . 'user_uploads/user_hash/' . DS . $up_path . DS;
}
$allowed_to_upload = true;
}
} else {
$_REQUEST['path'] = 'media/' . $host_dir . DS . 'user_uploads/anonymous/';
$allowed_to_upload = true;
}
}
if ($allowed_to_upload == false) {
if (isset($_REQUEST['rel_type']) and isset($_REQUEST['custom_field_id']) and trim($_REQUEST['rel_type']) != '' and trim($_REQUEST['rel_type']) != 'false') {
$cfid = mw()->fields_manager->getById(intval($_REQUEST['custom_field_id']));
if ($cfid == false) {
die('{"jsonrpc" : "2.0", "error" : {"code": 90, "message": "Custom field is not found"}}');
} else {
$rel_error = false;
if (!isset($_REQUEST['rel_id'])) {
$rel_error = true;
}
if (!isset($cfid['rel_id'])) {
$rel_error = true;
}
if (($_REQUEST['rel_id']) != $cfid['rel_id']) {
$rel_error = true;
}
if ($rel_error) {
die('{"jsonrpc" : "2.0", "error" : {"code": 91, "message": "You are not allowed to upload"}}');
}
}
if ($cfid != false and isset($cfid['custom_field_type'])) {
if ($cfid['custom_field_type'] != 'upload') {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Custom field is not file upload type"}}');
}
if ($cfid != false and (!isset($cfid['options']) or !isset($cfid['options']['file_types']))) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "File types is not set."}}');
}
if ($cfid != false and isset($cfid['file_types']) and empty($cfid['file_types'])) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "File types cannot by empty."}}');
}
if ($cfid != false and isset($cfid['options']) and isset($cfid['options']['file_types'])) {
$alloled_ft = array_values(($cfid['options']['file_types']));
if (empty($alloled_ft)) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 104, "message": "File types cannot by empty."}}');
} else {
$are_allowed = '';
$fileName_ext = isset($_REQUEST['name']) ? $_REQUEST['name'] : '';
foreach ($alloled_ft as $allowed_file_type_item) {
if (trim($allowed_file_type_item) != '' and $fileName_ext != '') {
$is_ext = get_file_extension($fileName_ext);
$is_ext = strtolower($is_ext);
switch ($is_ext) {
case 'phtml':
case 'php':
case 'php12':
case 'php11':
case 'php10':
case 'php9':
case 'php8':
case 'php7':
case 'php6':
case 'php5':
case 'php4':
case 'php3':
case 'ptml':
case 'html':
case 'xhtml':
case 'phtml':
case 'shtml':
case 'htm':
case 'pl':
case 'cgi':
case 'rb':
case 'py':
case 'asp':
case 'htaccess':
case 'exe':
case 'msi':
case 'sh':
case 'bat':
case 'vbs':
$are_allowed = false;
die('{"jsonrpc" : "2.0", "error" : {"code":105, "message": "You cannot upload scripts or executables"}}');
break;
}
$are_allowed = $files_utils->get_allowed_files_extensions_for_upload($allowed_file_type_item);
$pass_type_check = false;
if ($are_allowed != false) {
$are_allowed_a = explode(',', $are_allowed);
if (!empty($are_allowed_a)) {
foreach ($are_allowed_a as $are_allowed_a_item) {
$are_allowed_a_item = strtolower(trim($are_allowed_a_item));
$is_ext = strtolower(trim($is_ext));
if ($are_allowed_a_item == '*') {
$pass_type_check = 1;
}
if ($are_allowed_a_item != '' and $are_allowed_a_item == $is_ext) {
$pass_type_check = 1;
}
}
}
}
if ($pass_type_check == false) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code":106, "message": "You can only upload ' . $are_allowed . ' files."}}');
} else {
if (!isset($_REQUEST['captcha'])) {
if (!$validate_token) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code":107, "message": "Please enter the captcha answer!"}}');
}
} else {
$cap = mw()->user_manager->session_get('captcha');
if ($cap == false) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code":108, "message": "You must load a captcha first!"}}');
}
$validate_captcha = $this->app->captcha_manager->validate($_REQUEST['captcha']);
if (!$validate_captcha) {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code":109, "message": "Invalid captcha answer! "}}');
} else {
if (!isset($_REQUEST['path'])) {
$_REQUEST['path'] = 'media/' . $host_dir . '/user_uploads' . DS . $_REQUEST['rel_type'] . DS;
}
}
}
//die('{"jsonrpc" : "2.0", "error" : {"code":98, "message": PECATA - Not finished yet."}}');
}
}
}
}
}
}
} else {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 110, "message": "Only admin can upload."}, "id" : "id"}');
}
}
if(!is_admin()){
die('{"jsonrpc" : "2.0", "error" : {"code": 111, "message": "Only admin can upload."}, "id" : "id"}');
return response(array(
'error' => _e('Please enter captcha answer!', true),
'captcha_error' => true,
'form_data_required' => 'captcha',
'form_data_required_params' => array('captcha_parent_for_id' => $_REQUEST['rel_id']),
'form_data_module' => 'captcha'
));
}
// Settings
//$target_path = media_base_path() . DS;
//$target_path = media_base_path() . DS . $host_dir . DS . 'uploaded' . DS;
$target_path = media_uploads_path();
$target_path = normalize_path($target_path, 0);
$path_restirct = userfiles_path(); // the path the script should access
if (isset($_REQUEST['path']) and trim($_REQUEST['path']) != '' and trim($_REQUEST['path']) != 'false') {
$path = urldecode($_REQUEST['path']);
$path = html_entity_decode($path);
$path = htmlspecialchars_decode($path, ENT_NOQUOTES);
//$path = urldecode($path);
$path = str_replace('%2F', '/', $path);
//$path = str_replace('%25252F','/',$path);
$path = normalize_path($path, 0);
$path = str_replace('..', '', $path);
$path = str_replace($path_restirct, '', $path);
// $target_path = userfiles_path() . DS . $path;
$target_path = media_uploads_path() . DS . $path;
$target_path = normalize_path($target_path, 1);
}
$targetDir = $target_path;
if (!is_dir($targetDir)) {
mkdir_recursive($targetDir);
}
//$targetDir = 'uploads';
$cleanupTargetDir = true;
// Remove old files
$maxFileAge = 5 * 3600;
// Temp file age in seconds
// 5 minutes execution time
@set_time_limit(5 * 60);
// Uncomment this one to fake upload time
// usleep(5000);
// Get parameters
$chunk = isset($_REQUEST['chunk']) ? intval($_REQUEST['chunk']) : 0;
$chunks = isset($_REQUEST['chunks']) ? intval($_REQUEST['chunks']) : 0;
$fileName = isset($_REQUEST['name']) ? $_REQUEST['name'] : '';
// Clean the fileName for security reasons
$fileNameExtension = get_file_extension($fileName);
$fileName = \MicroweberPackages\Helper\URLify::filter($fileName);
//$fileName = url_title($fileName);
//$fileName = preg_replace('/[\p{P}\p{Zs}\w\._]+/u', "", $fileName);
// $fileName = preg_replace('/[^\w\._]+/', '_', $fileName);
$fileName = preg_replace('/\s+\d+%|\)/', '', $fileName);
$fileName = preg_replace("/[\/\&%#\$]/", "_", $fileName);
$fileName = preg_replace("/[\"\']/", " ", $fileName);
$fileName = str_replace(array('(', ')', "'", "!", "`", "*", "#"), '_', $fileName);
$fileName = str_replace(' ', '_', $fileName);
$fileName = str_replace('..', '.', $fileName);
$fileName = strtolower($fileName);
$fileName = mw()->url_manager->clean_url_wrappers($fileName);
$fileName = substr($fileName, 0, -(strlen($fileNameExtension)));
$fileName = $fileName . '.' . $fileNameExtension;
$fileName_uniq = date('ymdhis') . uniqid() . $fileName;
// Make sure the fileName is unique but only if chunking is disabled
if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
$ext = strrpos($fileName, '.');
$fileName_a = substr($fileName, 0, $ext);
$fileName_b = substr($fileName, $ext);
$fileName_b = strtolower($fileName_b);
$count = 1;
while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b)) {
++$count;
}
$fileName = $fileName_a . '_' . $count . $fileName_b;
}
$filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
$filePath_uniq = $targetDir . DIRECTORY_SEPARATOR . $fileName_uniq;
// Create target dir
if (!is_dir($targetDir)) {
@mkdir_recursive($targetDir);
}
$has_index = $targetDir . DIRECTORY_SEPARATOR . 'index.html';
if (!is_file($has_index)) {
@touch($has_index);
}
// Remove old temp files
if ($cleanupTargetDir && is_dir($targetDir) && ($dir = opendir($targetDir))) {
while (($file = readdir($dir)) !== false) {
$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
// Remove temp file if it is older than the max age and is not the current file
if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge) && ($tmpfilePath != "{$filePath}.part")) {
@unlink($tmpfilePath);
}
}
closedir($dir);
} else {
die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
}
if (isset($_SERVER['CONTENT_LENGTH']) and isset($_FILES['file'])) {
$filename_log = mw()->url_manager->slug($fileName);
$check = mw()->log_manager->get('one=true&no_cache=true&is_system=y&created_at=[mt]30 min ago&field=upload_size&rel=uploader&rel_id=' . $filename_log . '&user_ip=' . user_ip());
$upl_size_log = $_SERVER['CONTENT_LENGTH'];
if (is_array($check) and isset($check['id'])) {
$upl_size_log = intval($upl_size_log) + intval($check['value']);
mw()->log_manager->save('no_cache=true&is_system=y&field=upload_size&rel=uploader&rel_id=' . $filename_log . '&value=' . $upl_size_log . '&user_ip=' . user_ip() . '&id=' . $check['id']);
} else {
mw()->log_manager->save('no_cache=true&is_system=y&field=upload_size&rel=uploader&rel_id=' . $filename_log . '&value=' . $upl_size_log . '&user_ip=' . user_ip());
}
}
// Look for the content type header
if (isset($_SERVER['HTTP_CONTENT_TYPE'])) {
$contentType = $_SERVER['HTTP_CONTENT_TYPE'];
}
if (isset($_SERVER['CONTENT_TYPE'])) {
$contentType = $_SERVER['CONTENT_TYPE'];
}
// Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
$is_image = false;
$engine = 'plupload';
if ($engine == 'plupload') {
if (isset($contentType)) {
if (strpos($contentType, 'multipart') !== false) {
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
//uploading successfully done
} else {
throw new UploadException($_FILES['file']['error']);
}
}
if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
// Open temp file
$out = fopen("{$filePath}.part", $chunk == 0 ? 'wb' : 'ab');
if ($out) {
// Read binary input stream and append it to temp file
$in = fopen($_FILES['file']['tmp_name'], 'rb');
if ($in) {
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
} else {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
}
fclose($in);
fclose($out);
@unlink($_FILES['file']['tmp_name']);
} else {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
}
} else {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
}
} else {
// Open temp file
$out = fopen("{$filePath}.part", $chunk == 0 ? 'wb' : 'ab');
if ($out) {
// Read binary input stream and append it to temp file
$in = fopen('php://input', 'rb');
if ($in) {
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
} else {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
}
fclose($in);
fclose($out);
} else {
header("HTTP/1.1 401 Unauthorized");
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
}
}
} else {
}
$rerturn = array();
// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
// Strip the temp .part suffix off
$newfile = $filePath;
if (is_file($newfile)) {
$newfile = $filePath_uniq;
}
rename("{$filePath}.part", $newfile);
$filePath = $newfile;
$automatic_image_resize_on_upload = get_option('automatic_image_resize_on_upload', 'website') == 'y';
$automatic_image_resize_on_upload_disabled = get_option('automatic_image_resize_on_upload', 'website') == 'd';
if (is_file($filePath) and !$chunks || $chunk == $chunks - 1) {
$ext = get_file_extension($filePath);
$ext = strtolower($ext);
if (function_exists('finfo_open') and function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$mime = @finfo_file($finfo, $filePath);
if ($mime) {
$upl_mime_ext = explode('/', $mime);
$upl_mime_ext = end($upl_mime_ext);
$upl_mime_ext = explode('-', $upl_mime_ext);
$upl_mime_ext = end($upl_mime_ext);
$upl_mime_ext = strtolower($upl_mime_ext);
if (in_array($upl_mime_ext, $dangerous)) {
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Cannot upload mime type ' . $upl_mime_ext . '"}, "id" : "id"}');
}
}
finfo_close($finfo);
}
if ($ext == 'gif' || $ext == 'jpg' || $ext == 'jpeg' || $ext === 'jpe' || $ext == 'png'|| $ext == 'svg') {
$valid = false;
if ($ext === 'jpg' || $ext === 'jpeg' || $ext === 'jpe') {
// This will clear exif data - security issue
$imgCreatedFromJpeg = @imagecreatefromjpeg($filePath);
if ($imgCreatedFromJpeg) {
imagejpeg($imgCreatedFromJpeg, $filePath); // this will create fresh new image without exif sensitive data
$valid = true;
}
} else if ($ext === 'png') {
$imgCreatedFromPng = @imagecreatefrompng($filePath);
if ($imgCreatedFromPng) {
// keep bg color transparent
imagealphablending($imgCreatedFromPng, false);
imagesavealpha($imgCreatedFromPng, true);
imagepng($imgCreatedFromPng, $filePath); // this will create fresh new image without exif sensitive data
$valid = true;
}
} else if ($ext === 'gif') {
$imgCreatedFromGif = @imagecreatefromgif($filePath);
if ($imgCreatedFromGif) {
$filePathOld = stream_get_meta_data(tmpfile())['uri'];
copy($filePath, $filePathOld);
remove_exif_data($filePathOld, $filePath);
unlink($filePathOld);
$valid = true;
}
} else if ($ext === 'svg') {
$valid = false;
if (is_file($filePath)) {
$sanitizer = new \enshrined\svgSanitize\Sanitizer();
// Load the dirty svg
$dirtySVG = file_get_contents($filePath);
// Pass it to the sanitizer and get it back clean
try {
$cleanSVG = $sanitizer->sanitize($dirtySVG);
$valid = true;
} catch (\Exception $e) {
$valid = false;
}
if ($valid) {
file_put_contents($filePath, $cleanSVG);
}
}
} else {
$valid = false;
}
if (!$valid) {
@unlink($filePath);
die('{"jsonrpc" : "2.0", "error" : {"code": 107, "message": "File is not an image"}, "id" : "id"}');
}
}
}
if ($is_ext == 'gif' || $is_ext == 'jpg' || $is_ext == 'jpeg' || $is_ext == 'png') {
try {
$size = getimagesize($filePath);
$is_image = true;
$filesize = filesize($filePath);
$rerturn['file_size'] = $filesize;
$rerturn['file_size_human'] = mw()->format->human_filesize($filesize);
$rerturn['image_size'] = $size;
// $auto_resize_treshold = 10000000; // 10MiB
$auto_resize_treshold = 2000000; // 2MiB
if ($is_ext == 'jpg' || $is_ext == 'jpeg' || $is_ext == 'png') {
$rerturn['automatic_image_resize_is_enabled'] = $automatic_image_resize_on_upload;
if (!$automatic_image_resize_on_upload and $filesize > $auto_resize_treshold) {
// if image is big, ask to enable resizing
$rerturn['ask_user_to_enable_auto_resizing'] = 1;
$rerturn['ask_user_to_enable_auto_resizing_filesize'] = $filesize;
}
if (!$automatic_image_resize_on_upload_disabled and $automatic_image_resize_on_upload and $filesize > $auto_resize_treshold) {
$maxDim = 1980;
//@ini_set('memory_limit', '256M');
list($width, $height, $type, $attr) = $size;
if ($width > $maxDim || $height > $maxDim) {
// $d1 = dirname($filePath);
$d2 = basename($filePath);
// $target_filename = $d1 . DS . 'auto_resized_' . $d2;
$target_filename = $filePath;
$fn = $filePath;
$ratio = $size[0] / $size[1]; // width/height
if ($ratio > 1) {
$width = $maxDim;
$height = $maxDim / $ratio;
} else {
$width = $maxDim * $ratio;
$height = $maxDim;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width, $height);
if ($is_ext == 'png') {
// save transparency in alpha channel
imagealphablending($dst, false);
imagesavealpha($dst, true);
}
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagedestroy($src);
if ($is_ext == 'png') {
imagepng($dst, $target_filename); // adjust format as needed
} else if ($is_ext == 'jpg' || $is_ext == 'jpeg') {
imagejpeg($dst, $target_filename); // adjust format as needed
}
$rerturn['image_was_auto_resized'] = 1;
$rerturn['image_was_auto_resized_msg'] = "Image was automatically resized because it was " . $rerturn['file_size_human'];
imagedestroy($dst);
}
}
}
} catch (Exception $e) {
@unlink($filePath);
die('{"jsonrpc" : "2.0", "error" : {"code": 107, "message": "File is not an image"}, "id" : "id"}');
}
}
mw()->log_manager->delete('is_system=y&rel=uploader&created_at=[lt]30 min ago');
mw()->log_manager->delete('is_system=y&rel=uploader&session_id=' . mw()->user_manager->session_id());
}
$f_name = explode(DS, $filePath);
$f_name = end($f_name);
$filePath = mw()->url_manager->link_to_file($filePath);
$rerturn['src'] = $filePath;
$rerturn['name'] = $f_name;
if (isset($upl_size_log) and $upl_size_log > 0) {
$rerturn['bytes_uploaded'] = $upl_size_log;
}
//$rerturn['ORIG_REQUEST'] = $_GET;
/*
* upload.php
*
* Copyright 2009, Moxiecode Systems AB
* Released under GPL License.
*
* License: http://www.plupload.com/license
* Contributing: http://www.plupload.com/contributing
*/
// HTTP headers for no cache etc
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
echo json_encode($rerturn);
if (mw()->user_manager->session_id() and !(mw()->user_manager->session_all() == false)) {
// @//session_write_close();
}
class UploadException extends Exception
{
public function __construct($code)
{
$message = $this->codeToMessage($code);
parent::__construct($message, $code);
}
private function codeToMessage($code)
{
switch ($code) {
case UPLOAD_ERR_INI_SIZE:
$message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case UPLOAD_ERR_FORM_SIZE:
$message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case UPLOAD_ERR_PARTIAL:
$message = 'The uploaded file was only partially uploaded';
break;
case UPLOAD_ERR_NO_FILE:
$message = 'No file was uploaded';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$message = 'Missing a temporary folder';
break;
case UPLOAD_ERR_CANT_WRITE:
$message = 'Failed to write file to disk';
break;
case UPLOAD_ERR_EXTENSION:
$message = 'File upload stopped by extension';
break;
default:
$message = 'Unknown upload error';
break;
}
return $message;
}
}
exit;
// Return JSON-RPC response
//die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');