Browse Source

fix deep validation of urls

euromark 12 years ago
parent
commit
3795b1a4ce
3 changed files with 16 additions and 5 deletions
  1. 12 1
      Lib/Utility/Utility.php
  2. 3 3
      Model/MyModel.php
  3. 1 1
      Test/Case/Model/MyModelTest.php

+ 12 - 1
Lib/Utility/Utility.php

@@ -206,8 +206,19 @@ class Utility {
 		}
 
 		$path .= (isset($url['query'])) ? "?$url[query]" : '';
+
+		$defaults = array(
+			'http' => array(
+				'header' => "Accept: text/html\r\n" .
+					"Connection: Close\r\n" .
+					"User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64)\r\n",
+			)
+		);
+		stream_context_get_default($defaults);
+
 		if (isset($url['host']) && $url['host'] !== gethostbyname($url['host'])) {
-			$headers = @get_headers("$url[scheme]://$url[host]:$url[port]$path");
+			$url = "$url[scheme]://$url[host]$url[port]$path";
+			$headers = get_headers($url);
 			if (is_array($headers)) {
 				return $headers;
 			}

+ 3 - 3
Model/MyModel.php

@@ -1091,7 +1091,7 @@ class MyModel extends Model {
 		# same domain?
 		if (!empty($options['sameDomain']) && !empty($_SERVER['HTTP_HOST'])) {
 			$is = parse_url($url, PHP_URL_HOST);
-			$expected = $_SERVER['HTTP_HOST'];
+			$expected = env('HTTP_HOST');
 			if (mb_strtolower($is) !== mb_strtolower($expected)) {
 				return false;
 			}
@@ -1133,10 +1133,10 @@ class MyModel extends Model {
 		}
 		$headers = implode("\n", $headers);
 		$protocol = mb_strpos($url, 'https://') === 0 ? 'HTTP' : 'HTTP';
-		if (!(bool)preg_match('#^' . $protocol . '/.*\s+[(200|301|302)]+\s#i', $headers)) {
+		if (!preg_match('#^' . $protocol . '/.*?\s+[(200|301|302)]+\s#i', $headers)) {
 			return false;
 		}
-		if ((bool)preg_match('#^' . $protocol . '/.*\s+[(404|999)]+\s#i', $headers)) {
+		if (preg_match('#^' . $protocol . '/.*?\s+[(404|999)]+\s#i', $headers)) {
 			return false;
 		}
 		return true;

+ 1 - 1
Test/Case/Model/MyModelTest.php

@@ -631,7 +631,7 @@ class MyModelTest extends MyCakeTestCase {
 
 	public function testValidateUnique() {
 		$this->out($this->_header(__FUNCTION__), true);
-		//die(returns($this->Model->schema()));
+
 		$this->Model->validate['title'] = array(
 			'validateUnique' => array(
 				'rule' => 'validateUnique',