From e5fc1858f93857d64ddb8bbb6efe1d723b567310 Mon Sep 17 00:00:00 2001 From: Kaz Watanabe Date: Tue, 30 Dec 2014 09:24:31 +0900 Subject: [PATCH 01/36] fix error at change type of text field to integer in PostgreSQL --- .../Model/Datasource/Database/Postgres.php | 6 +++- .../Datasource/Database/PostgresTest.php | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Datasource/Database/Postgres.php b/lib/Cake/Model/Datasource/Database/Postgres.php index 87b26798f..acac1ff17 100644 --- a/lib/Cake/Model/Datasource/Database/Postgres.php +++ b/lib/Cake/Model/Datasource/Database/Postgres.php @@ -558,7 +558,11 @@ class Postgres extends DboSource { $colList[] = 'ALTER COLUMN ' . $fieldName . ' SET DEFAULT NULL'; $colList[] = 'ALTER COLUMN ' . $fieldName . ' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col)) . ' USING CASE WHEN TRUE THEN 1 ELSE 0 END'; } else { - $colList[] = 'ALTER COLUMN ' . $fieldName . ' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col)); + if ($original['type'] === 'text' && $col['type'] === 'integer') { + $colList[] = 'ALTER COLUMN ' . $fieldName . ' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col)) . " USING cast({$fieldName} as INTEGER)"; + } else { + $colList[] = 'ALTER COLUMN ' . $fieldName . ' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col)); + } } if (isset($nullable)) { diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php index e41567150..97cf4c4ab 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php @@ -731,6 +731,34 @@ class PostgresTest extends CakeTestCase { $this->Dbo->query($this->Dbo->dropSchema($Old)); } +/** + * Test the alterSchema changing text to integer + * + * @return void + */ + public function testAlterSchemaTextToIntegerField() { + $default = array( + 'connection' => 'test', + 'name' => 'TextField', + 'text_fields' => array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'name' => array('type' => 'string', 'length' => 50), + 'active' => array('type' => 'text', 'null' => false), + ) + ); + $Old = new CakeSchema($default); + $result = $this->Dbo->query($this->Dbo->createSchema($Old)); + $this->assertTrue($result); + + $modified = $default; + $modified['text_fields']['active'] = array('type' => 'integer', 'null' => true); + + $New = new CakeSchema($modified); + $query = $this->Dbo->alterSchema($New->compare($Old)); + $result = $this->Dbo->query($query); + $this->Dbo->query($this->Dbo->dropSchema($Old)); + } + /** * Test the alter index capabilities of postgres * From 05aba7afb6190c04bad9201655bacf6e06509b9b Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Fri, 13 Mar 2015 15:52:22 +0000 Subject: [PATCH 02/36] Assert CakeException throw when json_encode fails --- lib/Cake/Test/Case/View/JsonViewTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index 5a661c78b..5fc9ff088 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -306,4 +306,25 @@ class JsonViewTest extends CakeTestCase { $this->assertSame($expected, $output); $this->assertSame('application/javascript', $Response->type()); } + +/** + * JsonViewTest::testRenderInvalidJSON() + * + * @expectedException CakeException + * @return void + */ + public function testRenderInvalidJSON() { + $Request = new CakeRequest(); + $Response = new CakeResponse(); + $Controller = new Controller($Request, $Response); + + // non utf-8 stuff + $data = array('data' => array('foo' => 'bar' . chr('0x97'))); + + $Controller->set($data); + $Controller->set('_serialize', 'data'); + $View = new JsonView($Controller); + $output = $View->render(); + } + } From 08620704bef31dc9baeda4d0eec4d16a461f2999 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Fri, 13 Mar 2015 16:12:21 +0000 Subject: [PATCH 03/36] Handle json_encode failure --- lib/Cake/Test/Case/View/JsonViewTest.php | 20 ++++++++++++++++++++ lib/Cake/View/JsonView.php | 20 ++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index 5fc9ff088..ed974c0d9 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -326,5 +326,25 @@ class JsonViewTest extends CakeTestCase { $View = new JsonView($Controller); $output = $View->render(); } + +/** + * JsonViewTest::testRenderJSONBoolFalse() + * + * @return void + */ + public function testRenderJSONBoolFalse() { + $Request = new CakeRequest(); + $Response = new CakeResponse(); + $Controller = new Controller($Request, $Response); + + // non utf-8 stuff + $data = false; + + $Controller->set($data); + $Controller->set('_serialize', 'data'); + $View = new JsonView($Controller); + $output = $View->render(); + $this->assertSame('null', $output); + } } diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index 2f184139d..eb8eb9114 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -124,6 +124,10 @@ class JsonView extends View { /** * Serialize view vars + * For JSON documents, any errors are output using; + * PHP 5 >= 5.5.0 : json_last_error_msg() + * PHP 5 >= 5.3.0 : json_last_error() + * PHP 5 <= 5.2.9 : Generic fall back error * * @param array $serialize The viewVars that need to be serialized * @return string The serialized data @@ -145,10 +149,22 @@ class JsonView extends View { } if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) { - return json_encode($data, JSON_PRETTY_PRINT); + $json = json_encode($data, JSON_PRETTY_PRINT); + } else { + $json = json_encode($data); } - return json_encode($data); + if (function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE) { + if (function_exists('json_last_error_msg')) { + $error = json_last_error_msg(); + } else { + $error = __('JSON encoding failed: Error code %s', json_last_error()); + } + throw new CakeException($error); + } else if ($json === false) { + throw new CakeException(__('Failed to parse JSON')); + } + return $json; } } From ddfcb06d023f7596816c35452f655f6e05eca599 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Fri, 13 Mar 2015 16:13:29 +0000 Subject: [PATCH 04/36] Coding standards --- lib/Cake/Test/Case/View/JsonViewTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index ed974c0d9..cd9d6728a 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -345,6 +345,6 @@ class JsonViewTest extends CakeTestCase { $View = new JsonView($Controller); $output = $View->render(); $this->assertSame('null', $output); - } - + } + } From 2ba9f3b8fb906d1d392e6ca11b09df32003a39c2 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Fri, 13 Mar 2015 16:21:54 +0000 Subject: [PATCH 05/36] Encoding false is OK - should return string null --- lib/Cake/Test/Case/View/JsonViewTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index cd9d6728a..11a11f994 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -337,7 +337,7 @@ class JsonViewTest extends CakeTestCase { $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); - // non utf-8 stuff + // encoding a false, ensure this doesn't trigger exception $data = false; $Controller->set($data); From b880714231fdb1c9b23770f66cb6c267fee67f69 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Fri, 13 Mar 2015 16:27:24 +0000 Subject: [PATCH 06/36] coding standards elseif --- lib/Cake/View/JsonView.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index eb8eb9114..edfa926da 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -161,7 +161,7 @@ class JsonView extends View { $error = __('JSON encoding failed: Error code %s', json_last_error()); } throw new CakeException($error); - } else if ($json === false) { + } elseif ($json === false) { throw new CakeException(__('Failed to parse JSON')); } return $json; From 3b01c5f8423437104d1adb8fa4ac4f527bdbdbd4 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Fri, 13 Mar 2015 16:58:29 +0000 Subject: [PATCH 07/36] suppress warnings so test runner doesn't fart --- lib/Cake/View/JsonView.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index edfa926da..3aa97f5a3 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -149,9 +149,9 @@ class JsonView extends View { } if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) { - $json = json_encode($data, JSON_PRETTY_PRINT); + $json = @json_encode($data, JSON_PRETTY_PRINT); } else { - $json = json_encode($data); + $json = @json_encode($data); } if (function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE) { From 842cdf58f8208bdc33df1610dd11874a82085c2c Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sat, 14 Mar 2015 08:34:06 +0000 Subject: [PATCH 08/36] Don't translate errors --- lib/Cake/View/JsonView.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index 3aa97f5a3..631054b44 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -158,11 +158,11 @@ class JsonView extends View { if (function_exists('json_last_error_msg')) { $error = json_last_error_msg(); } else { - $error = __('JSON encoding failed: Error code %s', json_last_error()); + $error = sprintf('JSON encoding failed: Error code %s', json_last_error()); } throw new CakeException($error); } elseif ($json === false) { - throw new CakeException(__('Failed to parse JSON')); + throw new CakeException('Failed to parse JSON'); } return $json; } From bcb6549565760bb1fcaf6140448c82416b8ba74f Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sat, 14 Mar 2015 08:45:18 +0000 Subject: [PATCH 09/36] add throws tag to function comment --- lib/Cake/View/JsonView.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index 631054b44..c501fae66 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -130,6 +130,7 @@ class JsonView extends View { * PHP 5 <= 5.2.9 : Generic fall back error * * @param array $serialize The viewVars that need to be serialized + * @throws CakeException * @return string The serialized data */ protected function _serialize($serialize) { From 10cfb878be2b2195aa914ea3b341255896e0f5d4 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sat, 14 Mar 2015 09:13:31 +0000 Subject: [PATCH 10/36] fallback implementation of json_last_error_msg --- lib/Cake/View/JsonView.php | 4 ++-- lib/Cake/basics.php | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index c501fae66..d7d75e82d 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -150,9 +150,9 @@ class JsonView extends View { } if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) { - $json = @json_encode($data, JSON_PRETTY_PRINT); + $json = json_encode($data, JSON_PRETTY_PRINT); } else { - $json = @json_encode($data); + $json = json_encode($data); } if (function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE) { diff --git a/lib/Cake/basics.php b/lib/Cake/basics.php index 8dc20a015..9ca5a93db 100644 --- a/lib/Cake/basics.php +++ b/lib/Cake/basics.php @@ -1061,3 +1061,25 @@ if (!function_exists('convertSlash')) { } } + +if (!function_exists('json_last_error_msg')) { + +/** + * Provides the fallback implementation of json_last_error_msg() available in PHP 5.5 and above. + * + * @return string Error message. + */ + function json_last_error_msg() { + static $errors = array( + JSON_ERROR_NONE => '', + JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', + JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', + JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', + JSON_ERROR_SYNTAX => 'Syntax error', + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' + ); + $error = json_last_error(); + return array_key_exists($error, $errors) ? $errors[$error] : "Unknown error ({$error})"; + } + +} From 5002bd4dbecae6c8782728476c08b1de08827eb2 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sat, 14 Mar 2015 09:21:15 +0000 Subject: [PATCH 11/36] Use fall back json_last_error_msg --- lib/Cake/View/JsonView.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index d7d75e82d..fb25a34a1 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -124,10 +124,6 @@ class JsonView extends View { /** * Serialize view vars - * For JSON documents, any errors are output using; - * PHP 5 >= 5.5.0 : json_last_error_msg() - * PHP 5 >= 5.3.0 : json_last_error() - * PHP 5 <= 5.2.9 : Generic fall back error * * @param array $serialize The viewVars that need to be serialized * @throws CakeException @@ -156,12 +152,7 @@ class JsonView extends View { } if (function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE) { - if (function_exists('json_last_error_msg')) { - $error = json_last_error_msg(); - } else { - $error = sprintf('JSON encoding failed: Error code %s', json_last_error()); - } - throw new CakeException($error); + throw new CakeException(sprintf('JSON_ERROR: %s', json_last_error_msg())); } elseif ($json === false) { throw new CakeException('Failed to parse JSON'); } From d3c24be84b38cabef19892f4b2dbf6d838a4fc2c Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sat, 14 Mar 2015 13:12:09 +0000 Subject: [PATCH 12/36] add a custom error handler for tests of json_encode --- lib/Cake/Test/Case/View/JsonViewTest.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index 11a11f994..3a772b853 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -156,6 +156,20 @@ class JsonViewTest extends CakeTestCase { ); } +/** + * Custom error handler for use while testing methods that use json_encode + * @param int $errno + * @param string $errstr + * @param string $errfile + * @param int $errline + * @param array $errcontext + * @return void + * @throws CakeException + **/ + public function jsonEncodeErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) { + throw new CakeException($errstr, 0, $errno, $errfile, $errline); + } + /** * Test render with a valid string in _serialize. * @@ -310,7 +324,7 @@ class JsonViewTest extends CakeTestCase { /** * JsonViewTest::testRenderInvalidJSON() * - * @expectedException CakeException + * expectedException CakeException * @return void */ public function testRenderInvalidJSON() { @@ -320,6 +334,9 @@ class JsonViewTest extends CakeTestCase { // non utf-8 stuff $data = array('data' => array('foo' => 'bar' . chr('0x97'))); + + // Use a custom error handler + $phpUnitErrorHandler = set_error_handler(array($this, 'jsonEncodeErrorHandler')); $Controller->set($data); $Controller->set('_serialize', 'data'); From 56c6f02efc34e59dcf3625ce1c57eedf920b7c4b Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sat, 14 Mar 2015 13:16:05 +0000 Subject: [PATCH 13/36] Remove tabs --- lib/Cake/Test/Case/View/JsonViewTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index 3a772b853..fb7955c1a 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -334,7 +334,7 @@ class JsonViewTest extends CakeTestCase { // non utf-8 stuff $data = array('data' => array('foo' => 'bar' . chr('0x97'))); - + // Use a custom error handler $phpUnitErrorHandler = set_error_handler(array($this, 'jsonEncodeErrorHandler')); From fe0ddf21711042b8eea435e18024493bdc677fb6 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sat, 14 Mar 2015 13:27:34 +0000 Subject: [PATCH 14/36] Assert exception is thrown, reset error handler in tearDown --- lib/Cake/Test/Case/View/JsonViewTest.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index fb7955c1a..06c4f0208 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -28,11 +28,26 @@ App::uses('JsonView', 'View'); */ class JsonViewTest extends CakeTestCase { +/** + * setUp method + * + * @return void + **/ public function setUp() { parent::setUp(); Configure::write('debug', 0); } +/** + * tearDown method + * + * @return void + **/ + public function tearDown() { + parent::tearDown(); + restore_error_handler(); + } + /** * Generates testRenderWithoutView data. * @@ -324,7 +339,7 @@ class JsonViewTest extends CakeTestCase { /** * JsonViewTest::testRenderInvalidJSON() * - * expectedException CakeException + * @expectedException CakeException * @return void */ public function testRenderInvalidJSON() { @@ -336,7 +351,7 @@ class JsonViewTest extends CakeTestCase { $data = array('data' => array('foo' => 'bar' . chr('0x97'))); // Use a custom error handler - $phpUnitErrorHandler = set_error_handler(array($this, 'jsonEncodeErrorHandler')); + set_error_handler(array($this, 'jsonEncodeErrorHandler')); $Controller->set($data); $Controller->set('_serialize', 'data'); From b3c961bc9e2b8760fa0d10336819c12d0794fefc Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sat, 14 Mar 2015 13:45:50 +0000 Subject: [PATCH 15/36] Tickle Travis From c32e5559bb01c043ec45ab8e25b2307de721cff3 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 14 Mar 2015 22:23:17 -0400 Subject: [PATCH 16/36] Add test to ensure column was converted to integer. Refs #5512 --- lib/Cake/Model/Datasource/Database/Postgres.php | 2 -- .../Case/Model/Datasource/Database/PostgresTest.php | 12 ++++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Postgres.php b/lib/Cake/Model/Datasource/Database/Postgres.php index acac1ff17..21d82b191 100644 --- a/lib/Cake/Model/Datasource/Database/Postgres.php +++ b/lib/Cake/Model/Datasource/Database/Postgres.php @@ -1,7 +1,5 @@ 'integer', 'null' => true); $New = new CakeSchema($modified); - $query = $this->Dbo->alterSchema($New->compare($Old)); - $result = $this->Dbo->query($query); + $this->Dbo->query($this->Dbo->alterSchema($New->compare($Old))); + $result = $this->Dbo->describe('text_fields'); + $this->Dbo->query($this->Dbo->dropSchema($Old)); + $expected = array( + 'type' => 'integer', + 'null' => true, + 'default' => null, + 'length' => null, + ); + $this->assertEquals($expected, $result['active']); } /** From a59a05713301e2038d54d623d521763ba58f5a36 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sun, 15 Mar 2015 10:36:06 +0000 Subject: [PATCH 17/36] assert correct exception msg thrown --- lib/Cake/Test/Case/View/JsonViewTest.php | 33 +++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index 06c4f0208..8317ac5b5 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -38,16 +38,6 @@ class JsonViewTest extends CakeTestCase { Configure::write('debug', 0); } -/** - * tearDown method - * - * @return void - **/ - public function tearDown() { - parent::tearDown(); - restore_error_handler(); - } - /** * Generates testRenderWithoutView data. * @@ -339,7 +329,6 @@ class JsonViewTest extends CakeTestCase { /** * JsonViewTest::testRenderInvalidJSON() * - * @expectedException CakeException * @return void */ public function testRenderInvalidJSON() { @@ -350,13 +339,27 @@ class JsonViewTest extends CakeTestCase { // non utf-8 stuff $data = array('data' => array('foo' => 'bar' . chr('0x97'))); - // Use a custom error handler - set_error_handler(array($this, 'jsonEncodeErrorHandler')); - $Controller->set($data); $Controller->set('_serialize', 'data'); $View = new JsonView($Controller); - $output = $View->render(); + + // Use a custom error handler + set_error_handler(array($this, 'jsonEncodeErrorHandler')); + + try { + $View->render(); + restore_error_handler(); + $this->fail('Failed asserting that exception of type "CakeException" is thrown.'); + } catch (CakeException $e) { + $expected = array( + 'Failed to parse JSON', + 'Malformed UTF-8 characters, possibly incorrectly encoded' + ); + $this->assertContains($e->getMessage(), $expected); + restore_error_handler(); + return; + + } } /** From d94e05cf7683bd89067b9b440592108b98e1ff78 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sun, 15 Mar 2015 11:01:51 +0000 Subject: [PATCH 18/36] Assert exception msg contains UTF-8 The different versions of PHP throw several various messages for UTF-8 sequences, so this just performs a basic regex check --- lib/Cake/Test/Case/View/JsonViewTest.php | 6 +----- lib/Cake/View/JsonView.php | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index 8317ac5b5..f3a5902d5 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -351,12 +351,8 @@ class JsonViewTest extends CakeTestCase { restore_error_handler(); $this->fail('Failed asserting that exception of type "CakeException" is thrown.'); } catch (CakeException $e) { - $expected = array( - 'Failed to parse JSON', - 'Malformed UTF-8 characters, possibly incorrectly encoded' - ); - $this->assertContains($e->getMessage(), $expected); restore_error_handler(); + $this->assertRegExp('/UTF-8/', $e->getMessage()); return; } diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index fb25a34a1..7953803de 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -152,7 +152,7 @@ class JsonView extends View { } if (function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE) { - throw new CakeException(sprintf('JSON_ERROR: %s', json_last_error_msg())); + throw new CakeException(json_last_error_msg()); } elseif ($json === false) { throw new CakeException('Failed to parse JSON'); } From 4741abcd63b75a9e842773727c9dcaeb72882390 Mon Sep 17 00:00:00 2001 From: Rob McVey Date: Sun, 15 Mar 2015 11:14:49 +0000 Subject: [PATCH 19/36] Remove whitespace --- lib/Cake/Test/Case/View/JsonViewTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index f3a5902d5..4e4eb2574 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -354,7 +354,6 @@ class JsonViewTest extends CakeTestCase { restore_error_handler(); $this->assertRegExp('/UTF-8/', $e->getMessage()); return; - } } From 02c40d5ca5f5b839c056f250fd9036b2e09de571 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 15 Mar 2015 22:06:30 -0400 Subject: [PATCH 20/36] Update version number to 2.6.3 --- lib/Cake/VERSION.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/VERSION.txt b/lib/Cake/VERSION.txt index 4c3efdfeb..9706040e1 100644 --- a/lib/Cake/VERSION.txt +++ b/lib/Cake/VERSION.txt @@ -17,4 +17,4 @@ // @license http://www.opensource.org/licenses/mit-license.php MIT License // +--------------------------------------------------------------------------------------------+ // //////////////////////////////////////////////////////////////////////////////////////////////////// -2.6.2 +2.6.3 From b444fe7b81cf03a508872b7629231c4d88a1dc11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgaras=20Janu=C5=A1auskas?= Date: Mon, 16 Mar 2015 23:16:50 +0200 Subject: [PATCH 21/36] Do not warn about i18n functions definitions on extract task --- lib/Cake/Console/Command/Task/ExtractTask.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Console/Command/Task/ExtractTask.php b/lib/Cake/Console/Command/Task/ExtractTask.php index c9f5712cc..913c5680e 100644 --- a/lib/Cake/Console/Command/Task/ExtractTask.php +++ b/lib/Cake/Console/Command/Task/ExtractTask.php @@ -448,7 +448,7 @@ class ExtractTask extends AppShell { if ($categoryName !== 'LC_TIME') { $this->_addTranslation($categoryName, $domain, $singular, $details); } - } else { + } elseif (!is_array($this->_tokens[$count - 1]) || $this->_tokens[$count - 1][0] != T_FUNCTION) { $this->_markerError($this->_file, $line, $functionName, $count); } } From 6df7bf9c2161d600c0d4da6712cd68932171e9c8 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 17 Mar 2015 22:18:50 -0400 Subject: [PATCH 22/36] Fix notice errors in pagination link generation. No errors should be emitted when creating links for models that were not paginated. Refs #6090 --- .../Case/View/Helper/PaginatorHelperTest.php | 41 +++++++++++++++++++ lib/Cake/View/Helper/PaginatorHelper.php | 11 ++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/PaginatorHelperTest.php b/lib/Cake/Test/Case/View/Helper/PaginatorHelperTest.php index 8c59e5bf8..5b37bf4f0 100644 --- a/lib/Cake/Test/Case/View/Helper/PaginatorHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/PaginatorHelperTest.php @@ -1260,6 +1260,14 @@ class PaginatorHelperTest extends CakeTestCase { 'paramType' => 'named' ) ); + $result = $this->Paginator->sort('title', 'Title', array('model' => 'Client')); + $expected = array( + 'a' => array('href' => '/index/sort:title/direction:asc'), + 'Title', + '/a' + ); + $this->assertTags($result, $expected); + $result = $this->Paginator->next('Next', array('model' => 'Client')); $expected = array( 'span' => array('class' => 'next'), @@ -1277,6 +1285,39 @@ class PaginatorHelperTest extends CakeTestCase { $this->assertTags($result, $expected); } +/** + * Test creating paging links for missing models. + * + * @return void + */ + public function testPagingLinksMissingModel() { + $result = $this->Paginator->sort('title', 'Title', array('model' => 'Missing')); + $expected = array( + 'a' => array('href' => '/index/sort:title/direction:asc'), + 'Title', + '/a' + ); + $this->assertTags($result, $expected); + + $result = $this->Paginator->next('Next', array('model' => 'Missing')); + $expected = array( + 'span' => array('class' => 'next'), + 'a' => array('href' => '/index/page:2', 'rel' => 'next'), + 'Next', + '/a', + '/span' + ); + $this->assertTags($result, $expected); + + $result = $this->Paginator->prev('Prev', array('model' => 'Missing')); + $expected = array( + 'span' => array('class' => 'prev'), + 'Prev', + '/span' + ); + $this->assertTags($result, $expected); + } + /** * testGenericLinks method * diff --git a/lib/Cake/View/Helper/PaginatorHelper.php b/lib/Cake/View/Helper/PaginatorHelper.php index f79e09e7e..f5e3e72f2 100644 --- a/lib/Cake/View/Helper/PaginatorHelper.php +++ b/lib/Cake/View/Helper/PaginatorHelper.php @@ -120,7 +120,7 @@ class PaginatorHelper extends AppHelper { * Gets the current paging parameters from the resultset for the given model * * @param string $model Optional model name. Uses the default if none is specified. - * @return array|null The array of paging parameters for the paginated resultset. + * @return array The array of paging parameters for the paginated resultset. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::params */ public function params($model = null) { @@ -128,7 +128,14 @@ class PaginatorHelper extends AppHelper { $model = $this->defaultModel(); } if (!isset($this->request->params['paging']) || empty($this->request->params['paging'][$model])) { - return null; + return array( + 'prevPage' => false, + 'nextPage' => true, + 'paramType' => 'named', + 'pageCount' => 1, + 'options' => array(), + 'page' => 1 + ); } return $this->request->params['paging'][$model]; } From 359c0ab816f62eaa6d4e590dbe9ccbcef099f3ed Mon Sep 17 00:00:00 2001 From: Walther Lalk Date: Thu, 19 Mar 2015 11:42:14 +0200 Subject: [PATCH 23/36] Fix issue with memcache and domains starting with letter "u" --- lib/Cake/Cache/Engine/MemcacheEngine.php | 2 +- lib/Cake/Cache/Engine/MemcachedEngine.php | 2 +- .../Test/Case/Cache/Engine/MemcacheEngineTest.php | 11 +++++++++++ .../Test/Case/Cache/Engine/MemcachedEngineTest.php | 11 +++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Cache/Engine/MemcacheEngine.php b/lib/Cake/Cache/Engine/MemcacheEngine.php index 2f7e5e1f4..250f02ad8 100644 --- a/lib/Cake/Cache/Engine/MemcacheEngine.php +++ b/lib/Cake/Cache/Engine/MemcacheEngine.php @@ -104,7 +104,7 @@ class MemcacheEngine extends CacheEngine { * @return array Array containing host, port */ protected function _parseServerString($server) { - if ($server[0] === 'u') { + if (strpos($server, 'unix://') === 0) { return array($server, 0); } if (substr($server, 0, 1) === '[') { diff --git a/lib/Cake/Cache/Engine/MemcachedEngine.php b/lib/Cake/Cache/Engine/MemcachedEngine.php index 74ea7c1c3..a2611396a 100644 --- a/lib/Cake/Cache/Engine/MemcachedEngine.php +++ b/lib/Cake/Cache/Engine/MemcachedEngine.php @@ -185,7 +185,7 @@ class MemcachedEngine extends CacheEngine { * @return array Array containing host, port */ protected function _parseServerString($server) { - if ($server[0] === 'u') { + if (strpos($server, 'unix://') === 0) { return array($server, 0); } if (substr($server, 0, 1) === '[') { diff --git a/lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php b/lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php index fbe99dbbb..36f8f0d8b 100644 --- a/lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php +++ b/lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php @@ -161,6 +161,17 @@ class MemcacheEngineTest extends CakeTestCase { $this->assertTrue($result); } +/** + * test domain starts with u + * + * @return void + */ + public function testParseServerStringWithU() { + $Memcached = new TestMemcachedEngine(); + $result = $Memcached->parseServerString('udomain.net:13211'); + $this->assertEquals(array('udomain.net', '13211'), $result); + } + /** * test non latin domains. * diff --git a/lib/Cake/Test/Case/Cache/Engine/MemcachedEngineTest.php b/lib/Cake/Test/Case/Cache/Engine/MemcachedEngineTest.php index 4f940ef41..17f1f9972 100644 --- a/lib/Cake/Test/Case/Cache/Engine/MemcachedEngineTest.php +++ b/lib/Cake/Test/Case/Cache/Engine/MemcachedEngineTest.php @@ -400,6 +400,17 @@ class MemcachedEngineTest extends CakeTestCase { $this->assertTrue($result); } +/** + * test domain starts with u + * + * @return void + */ + public function testParseServerStringWithU() { + $Memcached = new TestMemcachedEngine(); + $result = $Memcached->parseServerString('udomain.net:13211'); + $this->assertEquals(array('udomain.net', '13211'), $result); + } + /** * test non latin domains. * From 744952e3442652d8fa49ef0d0c926a36156ebfa8 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 19 Mar 2015 21:15:20 -0400 Subject: [PATCH 24/36] Fix FormHelper::create() dropping 0 value parameter. When 0 is the first passed parameter we shouldn't drop it. Fixes #6107 --- lib/Cake/Test/Case/View/Helper/FormHelperTest.php | 7 ++++--- lib/Cake/View/Helper/FormHelper.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 30d25eacc..4b6cfb7e1 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -8642,6 +8642,7 @@ class FormHelperTest extends CakeTestCase { 'escape' => false, 'url' => array( 'action' => 'edit', + '0', 'myparam' ) )); @@ -8649,7 +8650,7 @@ class FormHelperTest extends CakeTestCase { 'form' => array( 'id' => 'ContactAddForm', 'method' => 'post', - 'action' => '/contacts/edit/myparam', + 'action' => '/contacts/edit/0/myparam', 'accept-charset' => $encoding ), 'div' => array('style' => 'display:none;'), @@ -8667,8 +8668,8 @@ class FormHelperTest extends CakeTestCase { public function testCreateNoErrorsWithMockModel() { $encoding = strtolower(Configure::read('App.encoding')); $ContactMock = $this->getMockBuilder('Contact') - ->disableOriginalConstructor() - ->getMock(); + ->disableOriginalConstructor() + ->getMock(); ClassRegistry::removeObject('Contact'); ClassRegistry::addObject('Contact', $ContactMock); $result = $this->Form->create('Contact', array('type' => 'GET')); diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 22fb6551d..880f5fd3f 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -408,7 +408,7 @@ class FormHelper extends AppHelper { 'action' => $options['action'], ); $options['action'] = array_merge($actionDefaults, (array)$options['url']); - if (empty($options['action'][0]) && !empty($id)) { + if (!isset($options['action'][0]) && !empty($id)) { $options['action'][0] = $id; } } elseif (is_string($options['url'])) { From 0003296f4262e9c292680582551d75d143e47e7b Mon Sep 17 00:00:00 2001 From: Dave Hensley Date: Fri, 20 Mar 2015 17:35:19 -0400 Subject: [PATCH 25/36] Add CAKE directory fallthrough for plugins/vendors Fixes issue #6124. --- lib/Cake/Core/App.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Core/App.php b/lib/Cake/Core/App.php index 361d36e42..f31bfc937 100644 --- a/lib/Cake/Core/App.php +++ b/lib/Cake/Core/App.php @@ -875,10 +875,12 @@ class App { 'Vendor' => array( '%s' . 'Vendor' . DS, ROOT . DS . 'vendors' . DS, + dirname(dirname(CAKE)) . DS . 'vendors' . DS ), 'Plugin' => array( APP . 'Plugin' . DS, - ROOT . DS . 'plugins' . DS + ROOT . DS . 'plugins' . DS, + dirname(dirname(CAKE)) . DS . 'plugins' . DS ) ); } From 5a6a74e2f8bbc329eb25926fce61ef55596604b1 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 20 Mar 2015 20:35:30 -0400 Subject: [PATCH 26/36] Fix failing tests. The test was taking the wrong index when creating mock path choices. Refs #6125 --- lib/Cake/Console/Command/Task/PluginTask.php | 2 -- .../Case/Console/Command/Task/PluginTaskTest.php | 16 +++++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/Cake/Console/Command/Task/PluginTask.php b/lib/Cake/Console/Command/Task/PluginTask.php index 95c3b3039..7cd507942 100644 --- a/lib/Cake/Console/Command/Task/PluginTask.php +++ b/lib/Cake/Console/Command/Task/PluginTask.php @@ -1,7 +1,5 @@ _testPath = array_push($paths, TMP . 'tests' . DS); + $this->_testPath = array_push($paths, TMP . 'tests' . DS) - 1; App::build(array('plugins' => $paths)); } @@ -80,17 +80,23 @@ class PluginTaskTest extends CakeTestCase { * @return void */ public function testBakeFoldersAndFiles() { - $this->Task->expects($this->at(0))->method('in')->will($this->returnValue($this->_testPath)); - $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y')); + $this->Task->expects($this->at(0)) + ->method('in') + ->will($this->returnValue($this->_testPath)); + $this->Task->expects($this->at(1)) + ->method('in') + ->will($this->returnValue('y')); $path = $this->Task->path . 'BakeTestPlugin'; $file = $path . DS . 'Controller' . DS . 'BakeTestPluginAppController.php'; - $this->Task->expects($this->at(2))->method('createFile') + $this->Task->expects($this->at(2)) + ->method('createFile') ->with($file, new PHPUnit_Framework_Constraint_IsAnything()); $file = $path . DS . 'Model' . DS . 'BakeTestPluginAppModel.php'; - $this->Task->expects($this->at(3))->method('createFile') + $this->Task->expects($this->at(3)) + ->method('createFile') ->with($file, new PHPUnit_Framework_Constraint_IsAnything()); $this->Task->bake('BakeTestPlugin'); From 54a3f8724b63c8dad46a35baad9f11f997cc3c62 Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Tue, 17 Mar 2015 13:40:36 +0100 Subject: [PATCH 27/36] Addapted quick hack from issue #2057 for 2.6.3 --- lib/Cake/Network/CakeSocket.php | 30 +++++++++++++++++++++++++++- lib/Cake/Network/Http/HttpSocket.php | 1 + 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Network/CakeSocket.php b/lib/Cake/Network/CakeSocket.php index d428a74cb..8b3c92e5f 100644 --- a/lib/Cake/Network/CakeSocket.php +++ b/lib/Cake/Network/CakeSocket.php @@ -130,7 +130,7 @@ class CakeSocket { } $scheme = null; - if (!empty($this->config['protocol']) && strpos($this->config['host'], '://') === false) { + if (!empty($this->config['protocol']) && strpos($this->config['host'], '://') === false && empty($this->config['proxy'])) { $scheme = $this->config['protocol'] . '://'; } @@ -170,6 +170,34 @@ class CakeSocket { if ($this->connected) { stream_set_timeout($this->connection, $this->config['timeout']); } + + + if (!empty($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https' && !empty($this->config['proxy'])) { + $req = array(); + $req[] = 'CONNECT '. $this->config['request']['uri']['host'] . ':' . $this->config['request']['uri']['port'] . ' HTTP/1.1'; + $req[] = 'Host: ' . $this->config['host']; + $req[] = 'User-Agent: php proxy'; + + fwrite($this->connection, implode("\r\n", $req)."\r\n\r\n"); + + while(true) { + $s = rtrim(fgets($this->connection, 4096)); + if(preg_match('/^$/', $s)) { + break; + } + } + + $modes = array(STREAM_CRYPTO_METHOD_TLS_CLIENT, + STREAM_CRYPTO_METHOD_SSLv3_CLIENT, + STREAM_CRYPTO_METHOD_SSLv23_CLIENT, + STREAM_CRYPTO_METHOD_SSLv2_CLIENT); + $success = false; + foreach($modes as $mode) { + $success = stream_socket_enable_crypto($this->connection, true, $mode); + if ($success) break; + } + } + return $this->connected; } diff --git a/lib/Cake/Network/Http/HttpSocket.php b/lib/Cake/Network/Http/HttpSocket.php index 16fc0edcf..ff176359f 100644 --- a/lib/Cake/Network/Http/HttpSocket.php +++ b/lib/Cake/Network/Http/HttpSocket.php @@ -652,6 +652,7 @@ class HttpSocket extends CakeSocket { } $this->config['host'] = $this->_proxy['host']; $this->config['port'] = $this->_proxy['port']; + $this->config['proxy'] = true; if (empty($this->_proxy['method']) || !isset($this->_proxy['user'], $this->_proxy['pass'])) { return; From bb8e0ae83524f2ab7ffbd9b330cb39f34ed640a7 Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Wed, 18 Mar 2015 16:10:04 +0100 Subject: [PATCH 28/36] Use enableCrypto() --- lib/Cake/Network/CakeSocket.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/Cake/Network/CakeSocket.php b/lib/Cake/Network/CakeSocket.php index 8b3c92e5f..7ded14569 100644 --- a/lib/Cake/Network/CakeSocket.php +++ b/lib/Cake/Network/CakeSocket.php @@ -171,7 +171,6 @@ class CakeSocket { stream_set_timeout($this->connection, $this->config['timeout']); } - if (!empty($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https' && !empty($this->config['proxy'])) { $req = array(); $req[] = 'CONNECT '. $this->config['request']['uri']['host'] . ':' . $this->config['request']['uri']['port'] . ' HTTP/1.1'; @@ -187,15 +186,7 @@ class CakeSocket { } } - $modes = array(STREAM_CRYPTO_METHOD_TLS_CLIENT, - STREAM_CRYPTO_METHOD_SSLv3_CLIENT, - STREAM_CRYPTO_METHOD_SSLv23_CLIENT, - STREAM_CRYPTO_METHOD_SSLv2_CLIENT); - $success = false; - foreach($modes as $mode) { - $success = stream_socket_enable_crypto($this->connection, true, $mode); - if ($success) break; - } + $this->enableCrypto('tls', 'client'); } return $this->connected; From 3995c70046537f53b5530537de5140676b06ee38 Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Wed, 18 Mar 2015 19:27:57 +0100 Subject: [PATCH 29/36] Strict === and space --- lib/Cake/Network/CakeSocket.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Network/CakeSocket.php b/lib/Cake/Network/CakeSocket.php index 7ded14569..01e074589 100644 --- a/lib/Cake/Network/CakeSocket.php +++ b/lib/Cake/Network/CakeSocket.php @@ -171,7 +171,7 @@ class CakeSocket { stream_set_timeout($this->connection, $this->config['timeout']); } - if (!empty($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https' && !empty($this->config['proxy'])) { + if (!empty($this->config['request']) && $this->config['request']['uri']['scheme'] === 'https' && !empty($this->config['proxy'])) { $req = array(); $req[] = 'CONNECT '. $this->config['request']['uri']['host'] . ':' . $this->config['request']['uri']['port'] . ' HTTP/1.1'; $req[] = 'Host: ' . $this->config['host']; @@ -181,7 +181,7 @@ class CakeSocket { while(true) { $s = rtrim(fgets($this->connection, 4096)); - if(preg_match('/^$/', $s)) { + if (preg_match('/^$/', $s)) { break; } } From 7704efdb282cf7742a93f002fec561f2ad58196e Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Thu, 19 Mar 2015 09:03:26 +0100 Subject: [PATCH 30/36] Use feof() in while loop --- lib/Cake/Network/CakeSocket.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Network/CakeSocket.php b/lib/Cake/Network/CakeSocket.php index 01e074589..8b134a9f3 100644 --- a/lib/Cake/Network/CakeSocket.php +++ b/lib/Cake/Network/CakeSocket.php @@ -177,9 +177,9 @@ class CakeSocket { $req[] = 'Host: ' . $this->config['host']; $req[] = 'User-Agent: php proxy'; - fwrite($this->connection, implode("\r\n", $req)."\r\n\r\n"); + fwrite($this->connection, implode("\r\n", $req) . "\r\n\r\n"); - while(true) { + while (!feof($this->connection)) { $s = rtrim(fgets($this->connection, 4096)); if (preg_match('/^$/', $s)) { break; From 15c80f7c3d5214a7312fe8d71297f69b1dd63d6d Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Thu, 19 Mar 2015 09:13:14 +0100 Subject: [PATCH 31/36] Move proxy code inside if (->connected) --- lib/Cake/Network/CakeSocket.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/Cake/Network/CakeSocket.php b/lib/Cake/Network/CakeSocket.php index 8b134a9f3..02b9b317f 100644 --- a/lib/Cake/Network/CakeSocket.php +++ b/lib/Cake/Network/CakeSocket.php @@ -169,26 +169,25 @@ class CakeSocket { $this->connected = is_resource($this->connection); if ($this->connected) { stream_set_timeout($this->connection, $this->config['timeout']); - } - if (!empty($this->config['request']) && $this->config['request']['uri']['scheme'] === 'https' && !empty($this->config['proxy'])) { - $req = array(); - $req[] = 'CONNECT '. $this->config['request']['uri']['host'] . ':' . $this->config['request']['uri']['port'] . ' HTTP/1.1'; - $req[] = 'Host: ' . $this->config['host']; - $req[] = 'User-Agent: php proxy'; + if (!empty($this->config['request']) && $this->config['request']['uri']['scheme'] === 'https' && !empty($this->config['proxy'])) { + $req = array(); + $req[] = 'CONNECT '. $this->config['request']['uri']['host'] . ':' . $this->config['request']['uri']['port'] . ' HTTP/1.1'; + $req[] = 'Host: ' . $this->config['host']; + $req[] = 'User-Agent: php proxy'; - fwrite($this->connection, implode("\r\n", $req) . "\r\n\r\n"); + fwrite($this->connection, implode("\r\n", $req) . "\r\n\r\n"); - while (!feof($this->connection)) { - $s = rtrim(fgets($this->connection, 4096)); - if (preg_match('/^$/', $s)) { - break; + while (!feof($this->connection)) { + $s = rtrim(fgets($this->connection, 4096)); + if (preg_match('/^$/', $s)) { + break; + } } + + $this->enableCrypto('tls', 'client'); } - - $this->enableCrypto('tls', 'client'); } - return $this->connected; } From 23d4d1155adeb5470625fe8ce9052476e4d53917 Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Thu, 19 Mar 2015 16:19:49 +0100 Subject: [PATCH 32/36] Do not use full uri in request line for HTTPS requests via proxy --- lib/Cake/Network/Http/HttpSocket.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Network/Http/HttpSocket.php b/lib/Cake/Network/Http/HttpSocket.php index ff176359f..92ed56121 100644 --- a/lib/Cake/Network/Http/HttpSocket.php +++ b/lib/Cake/Network/Http/HttpSocket.php @@ -927,7 +927,7 @@ class HttpSocket extends CakeSocket { $request['uri'] = $this->_parseUri($request['uri']); $request += array('method' => 'GET'); - if (!empty($this->_proxy['host'])) { + if (!empty($this->_proxy['host']) && $request['uri']['scheme'] !== 'https') { $request['uri'] = $this->_buildUri($request['uri'], '%scheme://%host:%port/%path?%query'); } else { $request['uri'] = $this->_buildUri($request['uri'], '/%path?%query'); From 9e6b1b6930faf73573d135efab5fa90280967d97 Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Fri, 20 Mar 2015 11:27:27 +0100 Subject: [PATCH 33/36] Support for SSL Server Name Indication --- lib/Cake/Network/Http/HttpSocket.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/Cake/Network/Http/HttpSocket.php b/lib/Cake/Network/Http/HttpSocket.php index 92ed56121..fe1d2bccc 100644 --- a/lib/Cake/Network/Http/HttpSocket.php +++ b/lib/Cake/Network/Http/HttpSocket.php @@ -718,6 +718,20 @@ class HttpSocket extends CakeSocket { } unset($this->config[$key]); } + if (version_compare(PHP_VERSION, '5.3.2', '>=')) { + if (empty($this->config['context']['ssl']['SNI_enabled'])) { + $this->config['context']['ssl']['SNI_enabled'] = true; + } + if (version_compare(PHP_VERSION, '5.6.0', '>=')) { + if (empty($this->config['context']['ssl']['peer_name'])) { + $this->config['context']['ssl']['peer_name'] = $this->request['uri']['host']; + } + } else { + if (empty($this->config['context']['ssl']['SNI_server_name'])) { + $this->config['context']['ssl']['SNI_server_name'] = $this->request['uri']['host']; + } + } + } if (empty($this->config['context']['ssl']['cafile'])) { $this->config['context']['ssl']['cafile'] = CAKE . 'Config' . DS . 'cacert.pem'; } From 1f7b78723650a189a631ac32bdc9deba89be04e7 Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Fri, 20 Mar 2015 11:30:15 +0100 Subject: [PATCH 34/36] Use $host parameter --- lib/Cake/Network/Http/HttpSocket.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Network/Http/HttpSocket.php b/lib/Cake/Network/Http/HttpSocket.php index fe1d2bccc..44b514d9f 100644 --- a/lib/Cake/Network/Http/HttpSocket.php +++ b/lib/Cake/Network/Http/HttpSocket.php @@ -724,11 +724,11 @@ class HttpSocket extends CakeSocket { } if (version_compare(PHP_VERSION, '5.6.0', '>=')) { if (empty($this->config['context']['ssl']['peer_name'])) { - $this->config['context']['ssl']['peer_name'] = $this->request['uri']['host']; + $this->config['context']['ssl']['peer_name'] = $host; } } else { if (empty($this->config['context']['ssl']['SNI_server_name'])) { - $this->config['context']['ssl']['SNI_server_name'] = $this->request['uri']['host']; + $this->config['context']['ssl']['SNI_server_name'] = $host; } } } From 1d0d20e974f327e2c6ef3582a844f96c6dcd9f7a Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Mon, 23 Mar 2015 23:10:32 +0100 Subject: [PATCH 35/36] Account for SNI changes in HttpSocketTest --- lib/Cake/Test/Case/Network/Http/HttpSocketTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/Cake/Test/Case/Network/Http/HttpSocketTest.php b/lib/Cake/Test/Case/Network/Http/HttpSocketTest.php index 6456a0c21..de04f18ed 100644 --- a/lib/Cake/Test/Case/Network/Http/HttpSocketTest.php +++ b/lib/Cake/Test/Case/Network/Http/HttpSocketTest.php @@ -317,11 +317,18 @@ class HttpSocketTest extends CakeTestCase { 'verify_peer' => true, 'allow_self_signed' => false, 'verify_depth' => 5, + 'SNI_enabled' => true, 'CN_match' => 'www.cakephp.org', 'cafile' => CAKE . 'Config' . DS . 'cacert.pem' ) ); + if (version_compare(PHP_VERSION, '5.6.0', '>=')) { + $context['ssl']['peer_name'] = 'www.cakephp.org'; + } else { + $context['ssl']['SNI_server_name'] = 'www.cakephp.org'; + } + $tests = array( array( 'request' => 'http://www.cakephp.org/?foo=bar', From b80e02c11435dc15977e2325b234380b2fa83cc2 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 23 Mar 2015 22:28:16 -0400 Subject: [PATCH 36/36] Fix coding standards errors. * Line lengths * Whitespace. Refs #2057 --- lib/Cake/Network/CakeSocket.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Network/CakeSocket.php b/lib/Cake/Network/CakeSocket.php index 02b9b317f..b4c378900 100644 --- a/lib/Cake/Network/CakeSocket.php +++ b/lib/Cake/Network/CakeSocket.php @@ -170,9 +170,13 @@ class CakeSocket { if ($this->connected) { stream_set_timeout($this->connection, $this->config['timeout']); - if (!empty($this->config['request']) && $this->config['request']['uri']['scheme'] === 'https' && !empty($this->config['proxy'])) { + if (!empty($this->config['request']) && + $this->config['request']['uri']['scheme'] === 'https' && + !empty($this->config['proxy']) + ) { $req = array(); - $req[] = 'CONNECT '. $this->config['request']['uri']['host'] . ':' . $this->config['request']['uri']['port'] . ' HTTP/1.1'; + $req[] = 'CONNECT ' . $this->config['request']['uri']['host'] . ':' . + $this->config['request']['uri']['port'] . ' HTTP/1.1'; $req[] = 'Host: ' . $this->config['host']; $req[] = 'User-Agent: php proxy';