Kaynağa Gözat

file_get_contents wrapper

pecesama 18 yıl önce
ebeveyn
işleme
b6b983de98
2 değiştirilmiş dosya ile 99 ekleme ve 3 silme
  1. 98 2
      classes/functions.php
  2. 1 1
      classes/gelato.class.php

+ 98 - 2
classes/functions.php Dosyayı Görüntüle

@@ -63,7 +63,7 @@
63 63
 	
64 64
 	function getFile($remoteFileName) {		
65 65
 		$fileName = "../uploads/".getFileName($remoteFileName);
66
-		$str = file_get_contents($remoteFileName);
66
+		$str = _file_get_contents($remoteFileName);
67 67
 		if (!$handle = fopen($fileName, 'w')) {
68 68
 			return false;
69 69
 		}
@@ -151,5 +151,101 @@
151 151
  		}
152 152
  		closedir($handle);
153 153
  		return $dirs;
154
- 	}	
154
+ 	}
155
+	
156
+	function _file_get_contents($path) {
157
+		// Modified function from: 
158
+		//		http://work.dokoku.net/Anieto2k/_file_get_contents.phps
159
+		//		http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/
160
+		if (!preg_match('/^http/i', $path)) {
161
+			if ($fp = fopen($path, 'r')) {
162
+				return fread($fp, 1024);
163
+			} else {			
164
+				return false;
165
+			}
166
+		} else {
167
+			if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) {
168
+				$ch = curl_init();
169
+				$timeout = 5;
170
+				curl_setopt ($ch, CURLOPT_URL, $path);
171
+				curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
172
+				curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
173
+				$file_contents = curl_exec($ch);
174
+				curl_close($ch);
175
+				if (is_string($file_contents)) {
176
+					return $file_contents;
177
+				} else {
178
+					return false;
179
+				}
180
+			} else {
181
+				$data = parse_url($path);
182
+				if (!$data['host'] || $data['scheme'] != "http") {
183
+					return false;
184
+				}
185
+		
186
+				$f = fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3);
187
+				if (!$f) {
188
+					return false;
189
+				}
190
+		
191
+				$q = "GET " . $data['path'] . "?" . $data['query'] . " HTTP/1.1\r\n";
192
+				$q .= "Host: " . $data['host'] . "\r\n";
193
+				$q .= "Connection: close\r\n";
194
+				$q .= "Referer: http://www.gelatocms.com/\r\n\r\n";
195
+		
196
+				$recv = "";
197
+				fwrite($f, $q);
198
+				while (!feof($f)) {
199
+					$recv .= fread($f, 1024);
200
+				}
201
+		
202
+				$request = $q;
203
+				$response = substr($recv, 0, strpos($recv, "\r\n\r\n"));
204
+				$body = substr($recv, strpos($recv, "\r\n\r\n") + 4);
205
+		
206
+				if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) {
207
+					if ($res[1][0] != "2") {
208
+						return false;
209
+					}
210
+				} else {
211
+					return false;
212
+				}
213
+		
214
+				if (preg_match('/transfer-encoding:\s*chunked/i', $response)) {
215
+					$tmp_body = $body;
216
+					$new = "";
217
+					$exit = false;
218
+					while (!$exit) {
219
+						if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) {
220
+							$len = hexdec($res[1]);
221
+							if ($len == "0") {
222
+								$exit = true;
223
+								break;
224
+							}
225
+							$new .= substr($tmp_body, strlen($res[0]), $len);
226
+							$tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n"));
227
+						} else {
228
+							$exit = true;
229
+						}
230
+					}
231
+					$body = $new;
232
+				}	
233
+				return $body;
234
+			}
235
+		}
236
+	}
237
+	
238
+	function get_curl_version() {
239
+		$curl = 0;
240
+		if (is_array(curl_version())) {
241
+			$curl = curl_version();
242
+			$curl = $curl['version'];
243
+		} else {
244
+			$curl = curl_version();
245
+			$curl = explode(' ', $curl);
246
+			$curl = explode('/', $curl[0]);
247
+			$curl = $curl[1];
248
+		}
249
+		return $curl;
250
+	} 	
155 251
 ?>

+ 1 - 1
classes/gelato.class.php Dosyayı Görüntüle

@@ -111,7 +111,7 @@ class gelato extends Conexion_Mysql {
111 111
 			$id_video = getVimeoVideoUrl($url);
112 112
 			return "\t\t\t<object type=\"application/x-shockwave-flash\" style=\"width:500px;height:393px\" data=\"http://www.vimeo.com/moogaloop.swf?clip_id=".$id_video."\"><param name=\"movie\" value=\"http://www.vimeo.com/moogaloop.swf?clip_id=".$id_video."\" /></object>\n";
113 113
 		} elseif (isGoogleVideo($url)) {
114
-			$html = trim(file_get_contents($url));
114
+			$html = trim(_file_get_contents($url));
115 115
 			$start_code = strpos($html, "var flashObj =\n    \"");
116 116
 			$end_code = strpos($html, "\";\n  flashObj = flashObj.replace");
117 117
 			$start_code = $start_code + strlen("var flashObj =\n    \"");