forked from scottchiefbaker/EasyCapture
/
ec.class.php
1474 lines (1128 loc) · 43.7 KB
/
ec.class.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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class ec_page {
function __construct() {
$this->version = "0.6.6";
$this->auth_delete = 1;
$this->auth_rename = 1;
$this->auth_capture = 0;
$this->debug = 1;
$this->footer = "";
umask(0133);
$this->image_extension = array(
"gif" => 1,
"jpg" => 1,
"png" => 1,
"jpeg" => 1,
);
session_start();
}
function html($html) {
$content = file_get_contents('html-template.html');
if (!$this->valid_admin_login(0)) {
$login_bar = $this->login_bar();
} else {
$login_bar = "";
}
#$style = $this->default_style();
$script = $this->script;
if (!is_array($script)) { $script = array($script); }
$body = $html;
#$body = preg_replace("/&/","&",$body);
if (isset($this->warning)) {
$body = "<div class=\"warning\"><b>Warning:</b> $this->warning</div>\n\n" . $body;
}
if (isset($this->information)) {
$body = "<div class=\"information\">$this->information</div>\n\n" . $body;
}
$script_html = "";
foreach ($script as $file) {
$script_html .= "<script type=\"text/javascript\" src=\"$file\"></script>\n";
}
if (isset($style)) {
$content = preg_replace("/\{style\}/",$style,$content);
}
$content = preg_replace("/\{script\}/",$script_html,$content);
$content = preg_replace("/\{header\}/",$login_bar,$content);
$content = preg_replace("/\{footer\}/",$this->footer,$content);
$content = preg_replace("/\{body\}/",$body,$content);
$content = preg_replace("/\{title\}/",$this->title,$content);
$content = preg_replace("/\{link\}/",$this->link,$content);
if ($this->valid_admin_login(0)) {
$content = preg_replace("/\{body_opts\}/","style=\"background: #fffee8\"",$content);
} else {
$content = preg_replace("/\{body_opts\}/","",$content);
}
print $content;
exit;
}
function error($html) {
$content = join("",file('html-template.html'));
#$style = $this->default_style();
if ($this->script) {
foreach ($this->script as $file) {
$script_html .= "<script type=\"text/javascript\" src=\"$file\"></script>\n";
}
}
$body = "<h2 style=\"text-align: center; margin-top: 10em;\">$html</h2>";
#$body = preg_replace("/&/","&",$body);
$content = preg_replace("/\{style\}/",$style,$content);
$content = preg_replace("/\{script\}/",$script_html,$content);
$content = preg_replace("/\{body\}/",$body,$content);
$content = preg_replace("/\{title\}/",$this->title,$content);
$content = preg_replace("/\{link\}/",$this->link,$content);
$content = preg_replace("/{\w+}/","",$content);
print $content;
exit;
}
function get_files($offset = 0, $limit = 30, $filter = "") {
if ($this->debug) { print "get_files: offset=$offset limit=$limit filter=$filter<br />\n"; }
$offset = intval($offset);
$full_html_dir = $this->full_dir;
$thumb_html_dir = $this->thumb_dir;
$images = glob($this->full_dir . "/*");
// If we're getting binaries too, then get those as well
if ($this->enable_binary_capture) {
$binaries = glob($this->binary_dir . "/*");
} else {
// Without an array, array merge fails
$binaries = array();
}
$list = array();
// These two foreach statements makes sure we're not adding the . and .. dirs
foreach ($binaries as &$file) {
if (is_readable($file) && !is_dir($file)) {
$list[] = $file;
}
}
foreach ($images as &$file) {
if (is_readable($file) && !is_dir($file)) {
$list[] = $file;
}
}
#$list = array_merge($images,$binaries);
//print_r($list);
// This just gets ALL the files and sorts them according to the times
foreach ($list as $file) {
// If there is a filter and it matches it
if ($filter && preg_match("/$filter/",$file)) {
} elseif ($filter) {
// there is a filter and this file doesn't match it
//print "Skipping $file it doesn't match the filter<br />\n";
continue;
}
$stat = stat($file);
$mtime = $stat[9];
$file = basename($file);
$for_sorting["$mtime-$file"] = $file;
}
if (!$for_sorting) {
$for_sorting = [];
}
if (isset($_GET['random'])) {
$i = array_rand($for_sorting,$limit);
foreach ($i as $key) {
$value = $for_sorting[$key];
$done[$key] = $value;
}
$sorted = $done;
} else {
// Sort the newly created array
krsort($for_sorting);
$sorted = $for_sorting;
}
// Filter out what's not needed after the filter and the limits etc...
$count = 0;
$shown_count = 0;
foreach ($sorted as $filename) {
$count++;
$filename = basename($filename);
if ($count >= $offset && $shown_count < $limit) {
//print "$filename<br />\n";
$info = $this->get_file_info($filename);
$ret[$filename] = $info;
$shown_count++;
}
}
// print_r($ret);
return $ret;
}
function show_gallery($include_header = 1, $filter = "") {
$full_html_dir = $this->full_dir;
$thumb_html_dir = $this->thumb_dir;
$start = microtime(1);
$offset = $_GET['offset'] ?? 0;
$limit = $_GET['limit'] ?? 24;
if (!empty($_GET['delete'])) {
$this->delete_file($_GET['delete']);
} elseif (isset($_GET['old_name']) && isset($_GET['new_name'])) {
$this->rename_file($_GET['old_name'],$_GET['new_name']);
}
$show_stats = 1;
if ($show_stats) {
$glob = glob($this->full_dir . "/*");
$info = array();
// Only count files (not dirs)
foreach ($glob as $i) {
if (is_file($i)) {
$info[] = $i;
}
}
// Calc the size for each file (in bytes)
$size = 0;
foreach ($info as $file) {
$size += filesize($file);
}
// Convert bytes to megs
$size = $size / (1024 * 1024);
$size = sprintf("%.02f",$size);
$total = sizeof($info); // number of files in the dir
$url = $_SERVER['PHP_SELF'];
$stats = "$total files (<a href=\"$url?show=gallery&random=true\">Random</a>) - {$size} megs - ";
$stat_line = "<div class=\"sub_heading\">$stats(<a href=\"index.php\">Back to Menu</a>)</div>\n\n";
}
$out = "";
if ($include_header) {
$version = $this->version;
$out .= "<h2 class=\"gallery_header\">Easy Capture Version $version</h2>\n\n";
$out .= $stat_line;
}
$files_to_show = $this->get_files($offset,$limit,$filter);
if (!$files_to_show) {
$files_to_show = [];
}
$PHP_SELF = $_SERVER['PHP_SELF'];
if ($include_header) {
$out .= "<form method=\"get\" action=\"$PHP_SELF\" style=\"text-align: center; border: 0px solid; margin-bottom: 10px; display: none;\" id=\"form\">
<!--
-->
<input type=\"text\" size=\"50\" name=\"old_name_text\" value=\"old_name.jpg\" id=\"old_name_text\" disabled=\"disabled\" />
<input type=\"hidden\" size=\"30\" name=\"old_name\" value=\"old_name.jpg\" id=\"old_name\" />
rename to
<input type=\"text\" size=\"50\" name=\"new_name\" value=\"new_name.jpg\" id=\"new_name\" />
<input type=\"hidden\" size=\"30\" name=\"show\" value=\"gallery\" />
<input type=\"submit\" id=\"rename_button\" value=\"Rename\" onclick=\"javascript: return final_submit();\" />
<span id=\"placeholder\"></span>
</form>\n\n";
}
if ($include_header) {
$out .= "<div class=\"gallery_wrapper\">\n";
}
// This outputs what's in the $full array
$shown_images = 0;
foreach(array_keys($files_to_show) as $filename) {
$footer = "<div class=\"image_footer\">";
$footer .= "<div class=\"image_actions\">";
$b64_filename = base64_encode($filename);
$footer .= "\t<div class=\"image_link\"><a href=\"$PHP_SELF?show=$filename\"><b>$filename</b></a></div>\n";
$footer .= "\t<ul class=\"dropdown action_menu\">\n";
$footer .= "\t<li class=\"li_action_menu\">▼ Actions\n";
$footer .= "\t<ul>\n";
$footer .= "\t\t<li title=\"Create a link that obscures the file name\"><span class=\"b64_image_link\"><a href=\"$PHP_SELF?show64=$b64_filename\">🔍 Show Masked</a></span></li>\n";
if ($this->authorized("rename")) {
$footer .= "\t\t<li title=\"Change the name of the file\"><a href=\"$PHP_SELF?show=gallery\" onclick=\"javascript: return rename_file('$filename',''); \">↷ Rename</a></li>\n";
$footer .= "\t\t<li title=\"Resave the image to shrink file size\"><a href=\"$PHP_SELF?action=resample&filename=$filename\">⚝ Resample</a></li>\n";
$footer .= "\t\t<li title=\"Shrink large images to a more usable size\"><a href=\"$PHP_SELF?action=resize&filename=$filename\">⇩ Resize</a></li>\n";
}
if ($this->authorized("delete")) {
$footer .= "\t" . $this->get_delete_link($filename);
}
$footer .= "\t</ul>\n";
$footer .= "\t</li>\n";
$footer .= "\t</ul>\n";
$footer .= "</div>"; // Closes image_footer
$footer .= "</div>"; // Closes image_actions
//print_r($files_to_show);
//print "$filename<br />";
// Is a binary
if (isset($files_to_show[$filename]['binary_size'])) {
$path = $this->clean_url($files_to_show[$filename]['thumb_html_path']);
$link_path = $this->clean_url($this->binary_dir . "/" . $filename);
$PHP_SELF = $_SERVER['PHP_SELF'];
$file_size = number_format($files_to_show[$filename]['binary_size']);
$footer = "\t<a href=\"$PHP_SELF?show=$u_filename\"><b>$filename</b></a><br />\n<div class=\"file_size\">$file_size bytes</div>\n";
if ($this->authorized("delete")) {
$footer .= $this->get_delete_link($filename);
}
if ($this->authorized("rename")) {
$footer .= "\n\t| <a href=\"$PHP_SELF?show=gallery\" onclick=\"javascript: return rename_file('$filename',''); \">Rename</a>";
}
$out .= "<div class=\"image\">\n\t<a href=\"$link_path\"><img src=\"binary-icon.png\" style=\"border: 0px solid green;\" alt=\"$filename\" /></a>\n\t<br />\n$footer\n</div>\n\n";
// Has thumbnail
} elseif (isset($files_to_show[$filename]['thumb_html_path'])) {
//$path = $this->clean_url($thumb_html_dir . "/" . $filename);
$path = $this->clean_url($files_to_show[$filename]['thumb_html_path']);
$link_path = $this->clean_url($full_html_dir . "/" . $filename);
$PHP_SELF = $_SERVER['PHP_SELF'];
#$footer .= "\n\t| <a href=\"$PHP_SELF?show=gallery\" onclick=\"javascript: return rename_file('$filename',''); \">Rename</a>";
$out .= "<div class=\"image image_wrapper\">\n\t<a class=\"clickable\" href=\"$link_path\"><img src=\"$path\" alt=\"$filename\" /></a>\n\t$footer\n</div>\n\n";
//$out .= "<div class=\"image\">\n\t<a class=\"clickable\" href=\"$link_path\"><img src=\"$path\" style=\"border: 1px solid green;\" alt=\"$filename\" /></a>\n\t$footer\n</div>\n\n";
// No thumbnail
} else {
$path = $full_html_dir . "/" . $filename;
$out .= "<div class=\"image\">\n\t<a href=\"$path\"><img src=\"$path\" style=\"border: 0px solid;\" alt=\"$filename\" /></a>\n<br />\n$footer\n</div>\n\n";
}
$shown_images++;
}
if ($include_header) {
$out .= "</div>\n";
}
//print sprintf("%.3f seconds<br />",microtime(1) - $start);
// Only show the more if the page is full (i.e. there are more images)
if ($shown_images >= $limit) {
$new_offset = $offset + $limit + 1;
$older_link = "$PHP_SELF?show=gallery&offset=$new_offset";
// If there's a filter put it in the older link
if (!empty($filter)) {
$older_link .= '&filter=' . $filter;
}
$older_html = "<a href=\"$older_link\">← Older</a>\n";
} else {
$older_html = "← Older\n";
}
// Setup the new offset link
if ($offset > 0) {
$new_offset = $offset - $limit;
$newer_link = "$PHP_SELF?show=gallery&offset=$new_offset";
// If there's a filter add the filter to the newer link.
if (!empty($filter)) {
$newer_link .= '&filter=' . $filter;
}
if ($new_offset < 0) { $new_offset = 0; }
$newer_html = "<a href=\"$newer_link\">Newer →</a>";
} else {
$newer_html = "Newer →";
}
$PHP_SELF = $_SERVER['PHP_SELF'];
$middle = $stat_line;
$middle = $this->get_filter_bar_html();
$this->footer .= "<div class=\"gallery_footer\">
<div class=\"footer_left\">$older_html</div>
<div class=\"footer_middle\">$middle</div>
<div class=\"footer_right\">$newer_html</div>
</div>\n";
return $out;
}
function get_filter_bar_html() {
$ret = "<div class=\"filter_bar\"><form method=\"get\" action=\"$PHP_SELF\"><input type=\"input\" placeholder=\"File name filter...\" class=\"filter_input\" id=\"filter\" name=\"filter\" /><input type=\"hidden\" name=\"show\" value=\"gallery\" /></form></div>";
return $ret;
}
function get_file_info($filename) {
if (is_array($filename)) {
if ($this->debug) { print "Image info for an Array!?! Return 0<br />\n\n"; }
return 0;
}
if ($this->debug) { print "get_file_info for <b>$filename</b><br />\n"; }
$ret['filename'] = $filename;
if ($this->is_binary_file($filename)) {
$ret['binary_html_path'] = $this->clean_url($this->binary_dir . "/$filename");
$stat = stat($ret['binary_html_path']);
$ret['mtime'] = $stat[10];
$ret['binary_size'] = filesize($ret['binary_html_path']);
return $ret;
}
if ($this->debug > 1) { print "Image info for <b>$filename</b><br />\n\n"; }
$full_html_dir = $this->full_dir;
$thumb_html_dir = $this->thumb_dir;
if (is_readable($this->full_dir . "/$filename")) {
#print "Is readable";
} else {
#print "$filename not found";
//exit;
return 0;
}
$ret['full_html_path'] = $this->clean_url("$full_html_dir/$filename");
$ret['full_file_size'] = @filesize($ret['full_html_path']);
if (is_readable($this->clean_url("$thumb_html_dir/$filename"))) {
$ret['thumb_html_path'] = $this->clean_url("$thumb_html_dir/$filename");
$ret['thumb_file_size'] = filesize($ret['thumb_html_path']);
} else {
$thumb_filename = $this->check_thumb_other($filename);
if ($thumb_filename) {
$ret['thumb_html_path'] = $this->clean_url("$thumb_html_dir/$thumb_filename");
$ret['thumb_file_size'] = filesize($ret['thumb_html_path']);
if ($this->debug) { print "<b>$filename</b> has alternate thumbnail extension <b>$thumb_filename</b><br />\n"; }
} else {
if ($this->debug) { print "<b>$filename</b> has no thumbnail<br />\n"; }
}
}
$stat = stat($ret['full_html_path']);
$ret['mtime'] = $stat[9];
return $ret;
}
// Used if the thumb and fullsize don't have the same extension (thumbs are always .jpg)
function check_thumb_other($filename) {
$wo_ext = preg_replace("/^(.+)\.(.+)/","$1",$filename);
// $wo_ext = substr($filename,0,strlen($filename) - 4);
if (!$wo_ext) { $this->error("Filename has no extension!?! '$filename'"); }
foreach (array_keys($this->image_extension) as $ext) {
$path = $this->thumb_dir . $wo_ext . ".$ext";
if (is_readable($path)) {
return basename($path);
}
}
// If you get this far, it didn't find anything
return 0;
}
function clean_url($url,$remove_periods = 0) {
if (!$url) { return ""; }
$split = preg_split("|/|",$url);
if ($this->debug > 1) { print "URL Before: $url<br />\n"; }
// If there are a bunch of ../../ or ./ in the url, remove them
if ($remove_periods) {
foreach ($split as $item) {
// Add the new item to the list since it's not a ..
if ($item != ".." && $item != "." && $item != "") {
$ret[] = $item;
// If it's a ./ then it's nothing (just that dir) so don't add/delete anything
} elseif ($item == "." || $item == "") {
} elseif ($item == ".." && is_array($ret)) {
// Remove the last item added since .. negates it.
array_pop($ret);
}
}
// Rebuild the string
$ret = join("/",$ret);
if (substr($url,0,1) == "/") { $ret = "/" . $ret; }
} else {
$ret = $url;
}
// Remove dupe /
$ret = preg_replace("!/+!","/",$ret);
// Restore the double / after the HTTP:
$ret = preg_replace("!(https?:/)!","$1/",$ret);
if ($this->debug > 1) { print "URL After: $ret<br />\n"; }
return $ret;
}
function show_image($file_list,$show_all_link = 1) {
if (!$file_list) { $this->error("No files sent to show_image"); }
if (!is_array($file_list)) {
$filename = $file_list;
$file_list = [];
$file_list[] = $filename;
}
foreach ($file_list as $filename) {
$img_list[] = $this->get_file_info($filename);
}
$head = "";
if ($show_all_link) {
$head = "<h2 style=\"text-align: center;\"><a href=\"" . $_SERVER['PHP_SELF'] . "?show=gallery\">Show all gallery images</a></h2>\n\n";
}
$head .= "<div style=\"text-align: center;\">\n\n";
$raw_html = "";
foreach ($img_list as $img_info) {
// Is a directory
if (substr($_SERVER['REQUEST_URI'],-1,1) == "/" ) { $directory = $_SERVER['REQUEST_URI']; }
// Is a file
else { $directory = dirname($_SERVER['REQUEST_URI']) . "/" ; }
$server_name = "http://" . $_SERVER['SERVER_NAME'] . $directory;
$html_path = $server_name . $img_info['full_html_path'];
$filename = basename($html_path);
$thumb_html_path = $img_info['thumb_html_path'] ?? null;
$filename = $img_info['filename'];
$u_filename = urlencode($img_info['filename']);
// Binary file
if (isset($img_info['binary_size'])) {
$path = dirname($img_info['binary_html_path']);
$filesize = number_format($img_info['binary_size']);
$filename = basename($img_info['binary_html_path']);
$file_link = $this->clean_url($server_name . $path . "/" . urlencode($filename),1);
$img_html = "<div style=\"margin-bottom: 10px;\">\n\t<a href=\"$file_link\"><img src=\"binary-icon.png\" style=\"border: 0px solid;\" alt=\"$filename\" /></a>\n</div>\n<div class=\"file_size\">$filename - $filesize bytes</div>\n\n";
// No thumbnail
} elseif (!$thumb_html_path) {
$thumb_html_path = $server_name . "/" . $img_info['full_html_path'];
$thumb_html_path = $this->clean_url($thumb_html_path,1);
$img_html = "<div style=\"margin-bottom: 10px;\">\n\t<img src=\"$thumb_html_path\" style=\"border: 0px solid;\" alt=\"$filename\" />\n</div>\n\n";
// It has a thumbnail
} else {
$thumb_html_path = $server_name . $thumb_html_path;
$thumb_html_path = $this->clean_url($thumb_html_path,1);
$html_path = $this->clean_url($html_path,1);
$img_html = "<div style=\"margin-bottom: 0px;\">\n\t<a href=\"$html_path\"><img src=\"$thumb_html_path\" style=\"border: 1px solid;\" alt=\"$filename\" /></a>\n</div>\n\n";
}
//print "<div><img src=\"$html_path\"><br />$filename</div>\n";
if ($this->freetype && isset($img_info['thumb_html_path'])) {
$PHP_SELF = $_SERVER['PHP_SELF'];
$remove_link = "$PHP_SELF?action=remove_tag&filename=$filename";
$add_link = "$PHP_SELF?action=add_tag&filename=$filename";
if ($this->authorized('delete')) {
$del_link = $this->get_delete_link($filename,"Delete this file");
}
if ($this->valid_admin_login(0)) {
$raw_html = "\n\t<div style=\"margin-bottom: 10px; font-size: 0.7em;\">Info Tag: <a href=\"$add_link\">On</a>/<a href=\"$remove_link\">Off</a></div>$del_link\n\n";
} else {
$raw_html = "<br />";
}
}
}
$text_area_content = preg_replace("/[\t\n\r]+/","",$img_html);
$text_area_content = htmlspecialchars($text_area_content);
$raw_html .= "<form action=\"post\"><textarea rows=\"5\" cols=\"100\">" . $text_area_content . "</textarea></form>\n";
// Close the div that centers everything
$raw_html .= "</div>";
$PHP_SELF = $_SERVER['PHP_SELF'];
$out = $head . $img_html . $raw_html;
return $out;
}
function capture_image($url) {
if (!$url) { return 0; }
if ($this->debug) { print "You want to capture: <b>$url</b><br />\n"; }
// Remove anything after a ? like foo.jpg?name=bar&baz=whatever
$url = preg_replace("/\\?.+/","",$url);
if (!$this->authorized('capture')) {
$PHP_SELF = $_SERVER['PHP_SELF'];
$this->error("You are not allowed to capture images<br />Please <a href=\"$PHP_SELF?login=1\">login</a> first");
}
$full_dir = $this->full_dir;
$thumb_dir = $this->thumb_dir;
$full_html_dir = $full_dir;
$thumb_html_dir = $thumb_dir;
#$filename = str_replace("%20","_",basename($url));
$filename = urldecode(basename($url));
// If it's an image name that already exists ask for overwrite
$info = $this->get_file_info($filename);
$confirm = $_POST['confirm'] ?? null;
$action = $_GET['action'] ?? null;
// If it's an action (add/remove info tag) auto confirm
if ($action) { $confirm = 1; }
// If the file already exists, and they haven't confirmed
if ($info['full_html_path'] && !$confirm) {
$this->confirm_overwrite($url);
}
if ($this->debug) { print "Filename: <b>$filename</b><br />\n"; }
$filename = preg_replace("/\s/","_",$filename);
$thumb_filename = $this->file_without_ext($filename) . ".jpg";
$extension = $this->get_extension($filename);
// Binary capture is OFF and it's not an image
if (!$this->image_extension[$extension] && !$this->enable_binary_capture) {
$this->error("Unknown extension '$extension'");
// Binary capture is on and it's not an image
} elseif (!$this->image_extension[$extension] && $this->enable_binary_capture) {
$bytes = $this->binary_capture($url);
if ($bytes <= 0) {
$this->error("Something went wrong trying to capture <b>$filename</b>");
} else {
$ret = $this->get_file_info($filename);
}
return $ret;
}
$filename = strtolower($filename);
$thumb_filename = strtolower($thumb_filename);
if ($this->debug) { print "Filename for the thumbnail: <b>$thumb_filename</b><br />\n"; }
//$file_contents = @join("",file($url));
$file_contents = file_get_contents($url);
if (!$file_contents) { $this->error("Could not download that file '$url'"); }
$filesize = strlen($file_contents);
$filesize = $this->human_filesize($filesize);
$img = @imagecreatefromstring($file_contents);
if (!$img) { $this->error("Something went wrong trying to capture <b>$filename</b>"); }
$time = time();
$full_path = $this->clean_url("$full_dir/$filename");
$full_thumb_path = "$thumb_dir/$thumb_filename";
if ($this->debug) { print "Writing file to disk: <b>$full_path</b><br />\n"; }
if (!is_writable(dirname($full_path))) {
$this->error("Unable to write <span style=\"color: darkred\">$full_path</span>, permission denied");
}
$fp = fopen($full_path,"w");
$bytes = intval(fwrite($fp,$file_contents));
fclose($fp);
if (!$bytes) {
$this->error("Tried writing the image to disk but it didn't work ($bytes written)");
}
$file_contents = "";
if ($this->debug) { print "Creating thumbnail for: <b>$full_path</b> size: $filesize<br />\n"; }
// If they don't have freetype installed then just don't add the info
if ($this->freetype == 0) {
$filesize = 0;
$this->warning = "You don't have the FreeType library compiled in, some functionality will be limited.";
// Or if the specify no image info (don't add the footer to the image)
} elseif (!$this->include_info_tag) {
$filesize = 0;
}
$thumb = $this->create_thumbnail($img,$filesize);
if ($thumb) {
imagejpeg($thumb,$full_thumb_path,$this->jpeg_quality);
//$thumb_filesize = filesize($full_thumb_path);
//$ret['thumb_html_path'] = preg_replace("|//|","/","$thumb_html_dir/$thumb_filename");
//$ret['thumb_disk_path'] = $full_thumb_path;
}
//list($width, $height, $type, $attr) = getimagesize($full_path);
//print "That image is $width x $height pixels<br />";
$ret = $this->get_file_info($filename);
return $ret;
}
function delete_file($filename) {
if (!$this->authorized('delete')) {
$PHP_SELF = $_SERVER['PHP_SELF'];
$this->error("You are not allowed to delete images<br />Please <a href=\"$PHP_SELF?login=1\">login</a> first");
}
if (!$filename) { $this->error("No file to delete"); }
$out = "";
// If there is a thumb we need to figure out where it is so we can delete it
$info = $this->get_file_info($filename);
$thumb_file = $info['thumb_html_path'];
if ($this->is_binary_file($filename)) {
if (@unlink($this->binary_dir . "/$filename")) {
$out .= "Info: Deleted binary file <b>$filename</b><br />\n";
}
} else {
// print_r($info);
if (@unlink($this->full_dir . "/$filename")) {
$out .= "Info: Deleted fullsize <b>$filename</b><br />\n";
}
if (isset($thumb_file)) {
unlink($thumb_file);
$out .= "Info: Deleted thumbnail <b>$filename</b><br />\n";
}
}
$this->information = $out;
return 1;
}
function rename_file($old_name,$new_name) {
if (!$this->authorized('rename')) {
$PHP_SELF = $_SERVER['PHP_SELF'];
$this->error("You are not allowed to rename images<br />Please <a href=\"$PHP_SELF?login=1\">login</a> first");
}
// You reloaded the rename page, and thus the old name isn't there
// if (!is_readable($this->full_dir . "/$old_name")) {
if (!$this->get_file_info($old_name)) {
//$this->error("adsfasd");
return 0;
}
$file_info = $this->get_file_info($old_name);
if (!$file_info) { return 0; }
// Make sure it's an allowed extension and not .txt or something weird
$ext = $this->get_extension($new_name);
// if (!$this->image_extension[$ext]) { $this->error("Can't rename a file unless the extension is valid ('$ext')"); }
// Prevent someone from renaming a file to /etc/foo.cfg or something
$old_name = basename($old_name);
$new_name = basename($new_name);
// Make sure they're not trying to rename from .jpg to .png
if ($this->get_extension($old_name) != $this->get_extension($new_name)) {
$this->error("Sorry the extensions don't match on your rename, they must stay the same!");
}
// Don't overwrite a file that's already there
if (is_readable($this->full_dir . "/$new_name")) {
$this->error("Can't rename to $new_name, it's already used");
}
// It's a binary file
if ($this->is_binary_file($old_name)) {
rename($this->binary_dir . "/$old_name",$this->binary_dir . "/$new_name");
// It's an image
} else {
//print_r($img_info);
// Rename the full size
rename($this->full_dir . "/$old_name",$this->full_dir . "/$new_name");
// Only rename the thumb if it has one
if ($file_info['thumb_html_path']) {
$thumb_name = basename($file_info['thumb_html_path']);
$new_thumb = $this->file_without_ext($new_name) . "." . $this->get_extension($thumb_name);
//print "$thumb_name $new_thumb $old_name";
rename($this->thumb_dir . "/$thumb_name",$this->thumb_dir . "/$new_thumb");
}
}
return 1;
}
function sanity_check() {
if ($this->debug) { $this->image_types(); }
if ($this->debug) { $this->show_php_settings(); }
if (!is_dir($this->thumb_dir)) {
$this->error("Thumbnail directory does not exist<br />$this->thumb_dir");
}
if (!is_dir($this->full_dir)) {
$this->error("Image directory does not exist<br />$this->full_dir");
}
if (!is_writeable($this->thumb_dir)) {
$path = realpath($this->thumb_dir);
$this->error("Thumbnail directory in not writeable<br /><code>chmod a+w $path</code>");
}
if (!is_writeable($this->full_dir)) {
$path = realpath($this->full_dir);
$this->error("Image directory is not writeable<br /><code>chmod a+w $path</code>");
}
if ($this->enable_binary_capture && !is_writeable($this->binary_dir)) {
$this->error("Binary directory is not writeable<br />$this->binary_dir");
}
if (!function_exists("imagecopyresampled")) {
$this->error("Easy Captures requires the GD Library but it's not present in this PHP install.<br />Please install the <span style=\"color: darkred\">php-gd</span> module.");
}
#if (!function_exists('imagecreatefromjpeg')) {
if (!imagetypes() & IMG_JPG) {
$this->error("Easy Capture requires JPEG support, please enable JPEG support and try again. (--with-jpeg-dir)");
}
if (!function_exists("imagettftext")) {
#$this->error("Easy Captures requires the Freetype support but it's not present in this PHP install.<br />Please recompile PHP with Freetype enabled (--with-freetype)");
$this->freetype = 0;
} else {
$this->freetype = 1;
}
if ($this->thumb_dir == $this->full_dir) {
$this->error("Thumb dir and Full dir cannot be the same.<br />Change them in index.php and retry!");
}
}
function authorized($action) {
#$valid_login = $this->valid_admin_login(0);
// Check if auth is required, if it is check for a valid login
if ($this->auth_delete && ($action == "delete" && !$this->valid_admin_login(0))) { return 0; }
elseif ($this->auth_rename && ($action == "rename" && !$this->valid_admin_login(0))) { return 0; }
elseif ($this->auth_capture && ($action == "capture" && !$this->valid_admin_login(0))) { return 0; }
else { return 1; }
}
function get_extension($filename) {
//print "!$filename!";
if (!$filename) { return 0; }
preg_match("/.*\.(\w{1,5})$/",$filename,$match);
$ret = $match[1];
$ret = strtolower($ret);
return $ret;
}
function file_without_ext($filename) {
//print "!$filename!";
if (!$filename) { return 0; }
preg_match("/(.*)\./",$filename,$match);
$ret = $match[1];
return $ret;
}
function get_tar_list($filename,$images_only = 0) {
if (!is_readable($filename)) {
die("File not found '$filename'");
}
if (preg_match("/.tar.gz$/",$filename)) {
$tar_opt = "-tvzf";
} elseif (preg_match("/.tar.bz2$/",$filename)) {
$tar_opt = "-tvjf";
} else {
$this->error("Not a tar file <b>$filename</b>");
}
$cmd = "tar $tar_opt $filename";
$foo = preg_split("/\n/",exec($cmd,$out));
foreach ($out as $line) {
//print "$line<br />\n";
preg_match("/\d{2}:\d{2}:\d{2} (.+)/",$line,$match);
$filename = $match[1];
// If they only want to see the images, only return those
if ($images_only) {
$ext = $this->get_extension($filename);
// If the extension if one of the ones listed in images, return it
if ($this->image_extension[$ext]) {
$ret[] = $filename;
}
} else {
$ret[] = $filename;
}
}
return $ret;
}
function extract_tar_file($filename,$out_dir = "/tmp/",$file_list = "") {
//$out_dir .= "/easy_capture";
if (!is_readable($filename) || !is_writable($out_dir)) { return 0; }
if (preg_match("/.tar.gz$/",$filename)) {
$tar_opt = "-xvzf";
} elseif (preg_match("/.tar.bz2$/",$filename)) {
$tar_opt = "-xvjf";
} else {
$this->error("Not a tar file <b>$filename</b>");
}
if ($file_list) {
foreach ($file_list as $item) {
$file_list2[] = "\"" . trim($item) ."\"";
}
$files_to_extract = join(" ",$file_list2);
} else {
$files_to_extract = "";
}
$cmd = "tar -C $out_dir $tar_opt $filename $files_to_extract";
if ($this->debug) { print "tar command: $cmd<br />\n"; }
$foo = preg_split("/\n/",exec($cmd,$out));
foreach ($out as $line) {
$ret[] = $this->clean_url($out_dir . trim($line),1);
}
return $ret;
}
function remove_tag($filename) {
if (!$this->valid_admin_login(1)) {
$this->error("You must be logged in to change images");
}
if (!$filename) { return 0; }
$info = $this->get_file_info($filename);
if (!$info) { return 0; }
$url = $info['full_html_path'];
$this->include_info_tag = 0;
$this->capture_image($url);
return 1;
}
function add_tag($filename) {
if (!$this->valid_admin_login(1)) {
$this->error("You must be logged in to change images");
}
if (!$filename) { return 0; }
$info = $this->get_file_info($filename);
if (!$info) { return 0; }
$url = $info['full_html_path'];
$this->include_info_tag = 1;
$this->capture_image($url);
return 1;
}
function create_thumbnail(&$img,$file_size = 0) {
$width = imagesx($img);