-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.php
More file actions
57 lines (42 loc) · 1.75 KB
/
proxy.php
File metadata and controls
57 lines (42 loc) · 1.75 KB
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
<?php
session_start();
//set vars
$url = "http://osc.test/html2canvas2"; //path to application
$base_img_dir = 'images'; //folder to store the images in relative to this script
$img_dir_prefix = date('Y-m-d') . '_'; //just to make it easy to automate the cleanup of all the images that get saved to the server
!defined('DS') ? define('DS', '/') : '';
//parse the url sent by the proxy function
//TODO: scrub the input better
$submitted_img_url = trim(htmlentities(urldecode($_GET['url'])));
//TODO:catch cases where a filename isn't the last element eg. http://sub.domain.ext/page
$basename = basename($submitted_img_url);
//test file type
//TODO: test for other cases that don't have a '.'
$pos = strrpos($submitted_img_url, '.', -1);
$ext = substr($submitted_img_url, $pos);
//set a dir for this request
function randomNumber()
{
return substr(sha1(rand()), 0, 15);
}
if (!isset($_SESSION['html2canvas_proxy_img_path']))
{
//prevent a loop just in case....
$i = 0;
do{
$random = randomNumber();
$i++;
$i === 10 ? exit : '';
}while (is_dir($base_img_dir .DS . $img_dir_prefix . $random)); //TODO: think this part though better
$_SESSION['html2canvas_proxy_img_path'] = $random;
} else {
$random = $_SESSION['html2canvas_proxy_img_path'];
}
$new_dir = $img_dir_prefix . $random; //prepends the working image directory with the image directory prefix
is_dir($base_img_dir . DS . $new_dir) ? '' : mkdir($base_img_dir . DS . $new_dir, 0755);
$file_path = $base_img_dir . DS . $new_dir . DS . $basename;
//save the image
if (!copy($submitted_img_url, $file_path)) exit;
$new_location = $url . DS . $file_path;
header('Content-Type: application/javascript');
echo "{$_GET['callback']}(" . json_encode($new_location) . ")";