diff --git a/lib/Cake/Console/ConsoleOutput.php b/lib/Cake/Console/ConsoleOutput.php index 759e953ce..c3f326445 100644 --- a/lib/Cake/Console/ConsoleOutput.php +++ b/lib/Cake/Console/ConsoleOutput.php @@ -186,7 +186,7 @@ class ConsoleOutput { return preg_replace('##', '', $text); } return preg_replace_callback( - '/<(?[a-z0-9-_]+)>(?.*?)<\/(\1)>/ims', array($this, '_replaceTags'), $text + '/<(?P[a-z0-9-_]+)>(?P.*?)<\/(\1)>/ims', array($this, '_replaceTags'), $text ); } diff --git a/lib/Cake/Controller/Component/PaginatorComponent.php b/lib/Cake/Controller/Component/PaginatorComponent.php index 14343cefc..bd3d31c18 100644 --- a/lib/Cake/Controller/Component/PaginatorComponent.php +++ b/lib/Cake/Controller/Component/PaginatorComponent.php @@ -185,6 +185,7 @@ class PaginatorComponent extends Component { $count = $object->find('count', array_merge($parameters, $extra)); } $pageCount = intval(ceil($count / $limit)); + $page = max(min($page, $pageCount), 1); $paging = array( 'page' => $page, @@ -375,7 +376,7 @@ class PaginatorComponent extends Component { if (empty($options['limit']) || $options['limit'] < 1) { $options['limit'] = 1; } - $options['limit'] = min((int)$options['limit'], $options['maxLimit']); + $options['limit'] = min($options['limit'], $options['maxLimit']); return $options; } diff --git a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php index 2da48c869..1908e926e 100644 --- a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php @@ -804,6 +804,35 @@ class PaginatorComponentTest extends CakeTestCase { $this->assertEquals('asc', $result['order']['model.something']); } +/** + * Test that a really large page number gets clamped to the max page size. + */ + public function testOutOfRangePageNumberGetsClamped() { + $Controller = new PaginatorTestController($this->request); + $Controller->uses = array('PaginatorControllerPost'); + $Controller->params['named'] = array( + 'page' => 3000, + ); + $Controller->constructClasses(); + $Controller->PaginatorControllerPost->recursive = 0; + $Controller->Paginator->paginate('PaginatorControllerPost'); + $this->assertEquals( + 1, + $Controller->request->params['paging']['PaginatorControllerPost']['page'], + 'Super big page number should be capped to max number of pages' + ); + + $Controller->paginate = array( + 'conditions' => array('PaginatorControllerPost.id >' => 100) + ); + $Controller->Paginator->paginate('PaginatorControllerPost'); + $this->assertEquals( + 1, + $Controller->request->params['paging']['PaginatorControllerPost']['page'], + 'Page number should not be 0' + ); + } + /** * test that fields not in whitelist won't be part of order conditions. *