<?php
/*
String.php, contains handy string utility functions.
Copyright (C) 2004 Arend van Beelen, Auton Rijnsburg
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For any questions, comments or whatever, you may mail me at: hide@address.com
*/
/**
* @brief String utility functions.
*
* A class with handy string utility functions. You know, those functions you
* all wish you'd had but couldn't find them in PHP.
*/
class String
{
/**
* Returns the substring of @p string before the first occurence of
* @p substring. Returns an empty string if @p substring is not found.
*
* If @p reverse is @p true, the function will look for the @e last
* occurence of @p substring instead.
*/
public static function substringBefore($string, $substring, $reverse = false)
{
return ($reverse == false ?
substr($string, 0, strpos($string, $substring)) :
substr($string, 0, strrpos($string, $substring)));
}
/**
* Returns the substring of @p string after the first occurence of
* @p substring. Returns the whole @p string if @p substring is not
* found.
*
* If @p reverse is @p true, the function will look for the @e last
* occurence of @p substring instead.
*/
public static function substringAfter($string, $substring, $reverse = false)
{
$pos = ($reverse == false ? strpos($string, $substring) :
strrpos($string, $substring));
if($pos === false)
{
return $string;
}
else
{
return substr($string, $pos + strlen($substring));
}
}
/**
* Returns whether @p string starts with @p substring.
*/
public static function startsWith($string, $substring)
{
return (substr($string, 0, strlen($substring)) == $substring);
}
/**
* Returns whether @p string ends with @p substring.
*/
public static function endsWith($string, $substring)
{
return (substr($string, -strlen($substring)) == $substring);
}
/**
* Returns @p string with all special characters stripped. Useful if
* you need to use a string as filename.
*/
public static function stripSpecialChars($string)
{
$result = '';
for($i = 0; $i < strlen($string); $i++)
{
$char = $string{$i};
if(ctype_alnum($char) || $char == ' ' || $char == '.' ||
$char == '_' || $char == '-')
{
$result .= $char;
}
}
return $result;
}
/**
* Returns @p string with all double slashes removed. Slashes at the end
* of @p string are removed as well.
*/
public static function normalizeSlashes($string)
{
$result = '';
$slash = false;
$char = '';
for($i = 0; $i < strlen($string); $i++)
{
$char = $string{$i};
if($slash == false || $char != '/')
{
$result .= $char;
}
$slash = ($char == '/');
}
if($char == '/')
{
$result = substr($result, 0, -1);
}
return $result;
}
/**
* This function is a variation on normalizeSlashes() which will also
* remove . and .. directory references from paths.
*
* Unlike the PHP realpath() function it does not require the directory
* to exist and symbolic links are not dereferenced.
*/
public static function simplifyPath($path)
{
if($path == '')
{
return '';
}
$dirs = explode('/', $path);
for($i = ($dirs[0] == '' ? 1 : 0); $i < sizeof($dirs); $i++)
{
if($dirs[$i] == '' ||
$dirs[$i] == '.')
{
array_splice($dirs, $i, 1);
$i--;
}
else if($dirs[$i] == '..' && $i > 0)
{
array_splice($dirs, $i - 1, 2);
$i -= 2;
}
}
return ''.implode('/', $dirs);
}
/**
* Determines whether an IP address is in a given IP range.
*
* @param range A string giving the IP range. You can use semi-colons to
* seperate multiple IP's or IP ranges and you should use
* a dash to specify a range. Asterisks may be used in single IP
* addresses. Examples of valid ranges are: "127.0.0.1",
* "192.168.0.1-192.168.0.100", "192.168.0.*" and
* "192.168.0.1-192.168.0.100;127.0.0.1". If the range is an
* empty string, this function will always return @p true.
* @param ip A string giving the IP address.
* @return @p true if the address is in the given range, @p false otherwise.
*
* @since Aukyla 1.1
*/
public static function isIpInRange($range, $ip)
{
if($range == '')
{
return true;
}
$ranges = explode(';', $range);
$ipFields = explode('.', $ip);
$ipFields[0] = (int) $ipFields[0];
$ipFields[1] = (int) $ipFields[1];
$ipFields[2] = (int) $ipFields[2];
$ipFields[3] = (int) $ipFields[3];
foreach($ranges as $range)
{
if(strchr($range, '-'))
{
list($range1, $range2) = explode('-', $range, 2);
$range1Fields = explode('.', $range1);
$range1Fields[0] = (int) $range1Fields[0];
$range1Fields[1] = (int) $range1Fields[1];
$range1Fields[2] = (int) $range1Fields[2];
$range1Fields[3] = (int) $range1Fields[3];
$range2Fields = explode('.', $range2);
$range2Fields[0] = (int) $range2Fields[0];
$range2Fields[1] = (int) $range2Fields[1];
$range2Fields[2] = (int) $range2Fields[2];
$range2Fields[3] = (int) $range2Fields[3];
$match = true;
for($i = 0; $i < 4; $i++)
{
if($ipFields[$i] < $range1Fields[$i] ||
$ipFields[$i] > $range2Fields[$i])
{
$match = false;
break;
}
}
if($match == true)
{
return true;
}
}
else
{
$rangeFields = explode('.', $range);
$rangeFields[0] = ($rangeFields[0] == '*' ? '*' : (int) $rangeFields[0]);
$rangeFields[1] = ($rangeFields[1] == '*' ? '*' : (int) $rangeFields[1]);
$rangeFields[2] = ($rangeFields[2] == '*' ? '*' : (int) $rangeFields[2]);
$rangeFields[3] = ($rangeFields[3] == '*' ? '*' : (int) $rangeFields[3]);
$match = true;
for($i = 0; $i < 4; $i++)
{
if($rangeFields[$i] == '*')
{
continue;
}
if($ipFields[$i] != $rangeFields[$i])
{
$match = false;
break;
}
}
if($match == true)
{
return true;
}
}
}
return false;
}
}
?>