From: <gem...@li...> - 2011-11-22 13:33:02
|
Revision: 261 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=261&view=rev Author: michieltcs Date: 2011-11-22 13:32:52 +0000 (Tue, 22 Nov 2011) Log Message: ----------- Refs #307 - add Gems_Util::isAllowedIP() and associated unit tests Modified Paths: -------------- trunk/library/classes/Gems/Util.php Added Paths: ----------- trunk/test/classes/Gems/UtilTest.php Modified: trunk/library/classes/Gems/Util.php =================================================================== --- trunk/library/classes/Gems/Util.php 2011-11-22 12:13:40 UTC (rev 260) +++ trunk/library/classes/Gems/Util.php 2011-11-22 13:32:52 UTC (rev 261) @@ -215,4 +215,51 @@ { return $this->_getClass('translated'); } + + /** + * Checks if a given IP is allowed according to a set + * of IP addresses / ranges. + * + * Multiple addresses/ranges are separated by a colon, + * an individual range takes the form of + * 10.0.0.0-10.0.0.255 (subnet masks are not supported) + * + * @param string $ip + * @param string $ipRanges + * @return bool + */ + public static function isAllowedIP($ip, $ipRanges = "") + { + if (!strlen($ipRanges)) { + return true; + } + + $ipLong = ip2long($ip); + + $ranges = explode(':', $ipRanges); + + foreach ($ranges as $range) { + if (($sep = strpos($range, '-')) !== false) { + $min = ip2long(substr($range, 0, $sep)); + $max = ip2long(substr($range, $sep + 1)); + + $validate = new Zend_Validate_Between( + array( + 'min' => $min, + 'max' => $max + ) + ); + + if ($min <= $ipLong && $ipLong <= $max) { + return true; + } + } else { + if ($ipLong == ip2long($range)) { + return true; + } + } + } + + return false; + } } Added: trunk/test/classes/Gems/UtilTest.php =================================================================== --- trunk/test/classes/Gems/UtilTest.php (rev 0) +++ trunk/test/classes/Gems/UtilTest.php 2011-11-22 13:32:52 UTC (rev 261) @@ -0,0 +1,82 @@ +<?php + +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package Gems + * @subpackage Util + * @author Michiel Rook <mi...@to...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ + */ + +/** + * Test class for Gems_Util + * + * @author Michiel Rook <mi...@to...> + * @package Gems + * @subpackage Util + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.0 + */ +class Gems_UtilTest extends PHPUnit_Framework_TestCase +{ + public function testAllowedIP1() { + $this->assertTrue(Gems_Util::isAllowedIP('10.0.0.1', '10.0.0.0-10.0.0.255')); + } + + public function testAllowedIP2() { + $this->assertFalse(Gems_Util::isAllowedIP('10.0.1.1', '10.0.0.0-10.0.0.255')); + } + + public function testAllowedIP3() { + $this->assertTrue(Gems_Util::isAllowedIP('127.0.0.1', '127.0.0.1')); + } + + public function testAllowedIP4() { + $this->assertFalse(Gems_Util::isAllowedIP('127.0.0.1', '192.168.0.1')); + } + + public function testAllowedIP5() { + $this->assertTrue(Gems_Util::isAllowedIP('127.0.0.1', '192.168.0.1:127.0.0.1')); + } + + public function testAllowedIPEmptyRange() { + $this->assertTrue(Gems_Util::isAllowedIP('127.0.0.1', '')); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + + } +} \ No newline at end of file Property changes on: trunk/test/classes/Gems/UtilTest.php ___________________________________________________________________ Added: svn:keywords + Id Rev Revision Date Author Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |