-
Notifications
You must be signed in to change notification settings - Fork 22
/
getRemoteImage.php
72 lines (65 loc) · 1.97 KB
/
getRemoteImage.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
<?php
include ("includes/vars.php");
include ("includes/functions.php");
$url = rawurldecode($_REQUEST['url']);
if (!$url) {
logger::error("GETREMOTEIMAGE", "Asked to download image but no URL given!");
http_response_code(404);
} else {
$outfile = 'prefs/imagecache/'.md5($url);
if (!is_file($outfile)) {
logger::debug("GETREMOTEIMAGE", "Downloading Remote Image ".$url);
if (download_image_file($url, $outfile)) {
output_file($outfile);
} else {
send_backup_image();
}
} else {
output_file($outfile);
}
}
function output_file($outfile) {
$imagehandler = new imageHandler($outfile);
$size = array_key_exists('rompr_resize_size', $_REQUEST) ? $_REQUEST['rompr_resize_size'] : 'asdownloaded';
if ($imagehandler->checkImage()) {
$imagehandler->outputResizedFile($size);
} else {
send_backup_image();
}
}
function download_image_file($url, $outfile) {
if (substr($url, 0, 10) == 'data:image') {
logger::core("GETREMOTEIMAGE", " ... Decoding Base64 Data");
imageFunctions::create_image_from_base64($url, $outfile);
} else {
logger::core("GETREMOTEIMAGE", " ... Downloading it");
$d = new url_downloader(array('url' => $url));
if ($d->get_data_to_file($outfile, true)) {
logger::core("GETREMOTEIMAGE", "Cached Image ".$outfile);
$content_type = $d->get_content_type();
logger::core("GETREMOTEIMAGE", " ... Content Type is ".$content_type);
if (substr($content_type,0,5) != 'image' && $content_type != 'application/octet-stream') {
logger::warn("GETREMOTEIMAGE", $url,"is not an image file! ");
unlink($outfile);
return false;
}
} else {
return false;
}
}
return true;
}
function send_backup_image() {
if (array_key_exists('rompr_backup_type', $_REQUEST)) {
switch ($_REQUEST['rompr_backup_type']) {
case 'stream':
logger::debug("GETREMOTEIMAGE","Sending backup image for stream");
header('Content-type: image/svg+xml');
readfile('newimages/broadcast.svg');
break;
}
} else {
http_response_code(404);
}
}
?>