forked from benallfree/laravel-stapler-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codesleeve-stapler-curl-open-basedir-fix.patch
91 lines (88 loc) · 2.96 KB
/
codesleeve-stapler-curl-open-basedir-fix.patch
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
diff --git a/vendor/codesleeve/stapler/src/Factories/File.php b/vendor/codesleeve/stapler/src/Factories/File.php
index 8d43010..ee7a2a4 100644
--- a/vendor/codesleeve/stapler/src/Factories/File.php
+++ b/vendor/codesleeve/stapler/src/Factories/File.php
@@ -83,9 +83,8 @@ class File
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $rawFile = curl_exec($ch);
+ $rawFile = self::curl_exec_follow($ch);
curl_close($ch);
// Get the original name of the file
@@ -108,6 +107,75 @@ class File
return new StaplerFile($filePath);
}
+
+ static function curl_exec_follow($ch, &$maxredirect = null) {
+
+ // we emulate a browser here since some websites detect
+ // us as a bot and don't let us do our job
+ $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)".
+ " Gecko/20041107 Firefox/1.0";
+ curl_setopt($ch, CURLOPT_USERAGENT, $user_agent );
+
+ $mr = $maxredirect === null ? 5 : intval($maxredirect);
+ if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
+
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
+ curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+
+ } else {
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
+
+ if ($mr > 0)
+ {
+ $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
+ $newurl = $original_url;
+
+ $rch = curl_copy_handle($ch);
+
+ curl_setopt($rch, CURLOPT_HEADER, true);
+ curl_setopt($rch, CURLOPT_NOBODY, true);
+ curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
+ do
+ {
+ curl_setopt($rch, CURLOPT_URL, $newurl);
+ $header = curl_exec($rch);
+ if (curl_errno($rch)) {
+ $code = 0;
+ } else {
+ $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
+ if ($code == 301 || $code == 302) {
+ preg_match('/Location:(.*?)\n/i', $header, $matches);
+ $newurl = trim(array_pop($matches));
+
+ // if no scheme is present then the new url is a
+ // relative path and thus needs some extra care
+ if(!preg_match("/^https?:/i", $newurl)){
+ $newurl = $original_url . $newurl;
+ }
+ } else {
+ $code = 0;
+ }
+ }
+ } while ($code && --$mr);
+
+ curl_close($rch);
+
+ if (!$mr)
+ {
+ if ($maxredirect === null)
+ trigger_error('Too many redirects.', E_USER_WARNING);
+ else
+ $maxredirect = 0;
+
+ return false;
+ }
+ curl_setopt($ch, CURLOPT_URL, $newurl);
+ }
+ }
+ return curl_exec($ch);
+ }
/**
* Fetch a local file using a string location and convert it into