Reading external files - the safe way

PHP Result Center PHP Result Center เป็นหมวด ที่ไว้รวบรวม โปรแกรม Code php Javascript CSS CMS

Moderators: mindphp, ผู้ดูแลกระดาน

User avatar
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
Posts: 24728
Joined: 22/09/2008 6:18 pm
Contact:

Reading external files - the safe way

Post by mindphp »

Reading external files - the safe way

Code: Select all

<?php
/**
 * sicheres einlesen externer Dateien
 */
private function URLreadFsock($host, $file, &$errstr, $successonly=true, $port=80, $timeout=10) {
	if (!function_exists('fsockopen')) {
		$errstr = 'fsockopen() unavailable';
		return false;
	}
	if ($fp = @fsockopen($host, 80, $errno, $errstr, $timeout)) {
		$out  = 'GET '.$file.' HTTP/1.0'."\r\n";
		$out .= 'Host: '.$host."\r\n";
		$out .= 'Connection: Close'."\r\n\r\n";
		fwrite($fp, $out);

		$isHeader = true;
		$Data_header = '';
		$Data_body   = '';
		$header_newlocation = '';
		while (!feof($fp)) {
			$line = fgets($fp, 1024);
			if ($isHeader) {
				$Data_header .= $line;
			} else {
				$Data_body .= $line;
			}
			if (eregi('^HTTP/[\\.0-9]+ ([0-9]+) (.+)$', rtrim($line), $matches)) {
				list($dummy, $errno, $errstr) = $matches;
				$errno = intval($errno);
			} elseif (eregi('^Location: (.*)$', rtrim($line), $matches)) {
				$header_newlocation = $matches[1];
			}
			if ($isHeader && ($line == "\r\n")) {
				$isHeader = false;
				if ($successonly) {
					switch ($errno) {
						case 200:
							// great, continue
							break;

						default:
							$errstr = $errno.' '.$errstr.($header_newlocation ? '; Location: '.$header_newlocation : '');
							fclose($fp);
							return false;
							break;
					}
				}
			}
		}
		fclose($fp);
		return $Data_body;
	}
	return null;
}


private function ParseURLbetter($url) {
	$parsedURL = @parse_url($url);
	if (!@$parsedURL['port']) {
		switch (strtolower(@$parsedURL['scheme'])) {
			case 'ftp':
				$parsedURL['port'] = 21;
				break;
			case 'https':
				$parsedURL['port'] = 443;
				break;
			case 'http':
				$parsedURL['port'] = 80;
				break;
		}
	}
	return $parsedURL;
}

private function _getVersionFile($url, &$error, $timeout=10, $followredirects=true) {
	$error = '';

	$parsed_url = $this->ParseURLbetter($url);
	$alreadyLookedAtURLs[trim($url)] = true;

	while (true) {
		$tryagain = false;
		$rawData = $this->URLreadFsock(@$parsed_url['host'], @$parsed_url['path'].'?'.@$parsed_url['query'], $errstr, true, (@$parsed_url['port'] ? @$parsed_url['port'] : 80), $timeout);
		if (eregi('302 [a-z ]+; Location\\: (http.*)', $errstr, $matches)) {
			$matches[1] = trim(@$matches[1]);
			if (!@$alreadyLookedAtURLs[$matches[1]]) {
				// loop through and examine new URL
				$error .= 'URL "'.$url.'" redirected to "'.$matches[1].'"';

				$tryagain = true;
				$alreadyLookedAtURLs[$matches[1]] = true;
				$parsed_url = $this->ParseURLbetter($matches[1]);
			}
		}
		if (!$tryagain) {
			break;
		}
	}

	if ($rawData === false) {
		$error .= 'Error opening "'.$url.'":'."\n\n".$errstr;
		return false;
	} elseif ($rawData === null) {
		// fall through
		$error .= 'Error opening "'.$url.'":'."\n\n".$errstr;
	} else {
		return trim($rawData);
	}

	// curl
	if (function_exists('curl_version')) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		$rawData = curl_exec($ch);
		curl_close($ch);
		if (strlen($rawData) > 0) {
			$error .= 'CURL succeeded ('.strlen($rawData).' bytes); ';
			return trim($rawData);
		}
		$error .= 'CURL available but returned no data; ';
	} else {
		$error .= 'CURL unavailable; ';
	}

	// fopen
	$BrokenURLfopenPHPversions = array('4.4.2');
	if (in_array(phpversion(), $BrokenURLfopenPHPversions)) {
		$error .= 'fopen(URL) broken in PHP v'.phpversion().'; ';
	} elseif (@ini_get('allow_url_fopen')) {
		$rawData = '';
		$error_fopen = '';
		ob_start();
		if ($fp = fopen($url, 'rb')) {
			do {
				$buffer = fread($fp, 8192);
				$rawData .= $buffer;
			} while (strlen($buffer) > 0);
			fclose($fp);
		} else {
			$error_fopen .= trim(strip_tags(ob_get_contents()));
		}
		ob_end_clean();
		$error .= $error_fopen;
		if (!$error_fopen) {
			$error .= '; "allow_url_fopen" succeeded ('.strlen($rawData).' bytes); ';
			return trim($rawData);
		}
		$error .= '; "allow_url_fopen" enabled but returned no data ('.$error_fopen.'); ';
	} else {
		$error .= '"allow_url_fopen" disabled; ';
	}
	return false;
}
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042

  • Similar Topics
    Replies
    Views
    Last post

Return to “Free PHP Code Download script”

Who is online

Users browsing this forum: No registered users and 2 guests