Archive
Archive for June, 2007
<?php
/**
* @author Brian Schmidt
* @version 1.0
* @return string
* @param string $mail
* @access public
* @desc Converts a mail address into HTML entities for simple encryption.
* @example $str_crypted = crypt_mail('some.mail.address@example.com');
*/
function crypt_mail
($mail)
{
$i = 0;
do {
$char = substr($mail, $i, 1);
$code = ord(substr($mail, $i, 1));
if($code != "�") {
$crypt .= '&#'.$code.';';
}
$i++;
} while ($char != "");
return $crypt;
}
?>
<?php
/**
* @author Brian Schmidt
* @version 1.0
* @return string
* @param $str_input
* @access public
* @desc returns the extension of a file
* @example $str_extension = file_ext('image.png');
*/
function file_ext
($str_input)
{
return substr(strtolower(strrchr($str_input, '.')), 1);
}
?>
<?php
/**
* @author Brian Schmidt
* @version 1.0
* @return array
* @param $url The webpage that is to be parsed for elements
* @param $search Array of search parameters
* @access public
* @desc Fetches a list/array of elements/strings from a given webpage.
* @example
* $url = 'http://www.librarything.com/jswidget.php?reporton=brianman&show=recent&header=&num=200&covers=none&text=all&tag=wishlist&css=0&style=1&version=1';
* $search = array(
* 'book_url' => array(
* 's' => '<div class="LTitem LTodd"><a href=\"',
* 'e' => '\" target=\"_top\">'
* ),
* 'book_title' => array(
* 's' => '<span class=\\'LTtitle\\'>',
* 'e' => '</span>'
* ),
* 'author_url' => array(
* 's' => ' by <a href=\"',
* 'e' => '\" target=\"_top\">'
* ),
* 'author_name' => array(
* 's' => '<span class=\\'LTauthor\\'>',
* 'e' => '</span>'
* )
* );
* $arrayList = HTTPFetchList($url, $search);
* print_r($arrayList);
*/
function HTTPFetchList
($url, $search)
{
if (!class_exists('HTTP_Request')) {
die(__FUNCTION__ . ' function requires the PEAR::HTTP_Request Class.');
}
$req =& new HTTP_Request('');
$req->addHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
$req->setURL($url);
if (!PEAR
::isError($req->sendRequest())) {
$content = explode("\n", $req->getResponseBody());
foreach ($content as $line) {
$pos1 = $pos2 = 0;
foreach ($search as $k => $v) {
if (stristr($line, $v['s'])) {
$found = TRUE;
$pos1 = ($pos2) ?
strpos($line, $v['s'], $pos2)+strlen($v['s']) : strpos($line, $v['s'])+strlen($v['s']);
$pos2 = strpos($line, $v['e'], $pos1);
$element[$k] = stripslashes(substr($line, $pos1, $pos2-$pos1));
}
}
if($found) {
$found = FALSE;
$elements[] = $element;
}
}
}
return $elements;
}
?>
<?php
/**
* @author Taken from the comments in the PHP manuals imageCopyResampled, by ron at korving dot demon dot nl
* @version 1.0
* @return void
* @param string $dst_img
* @param string $src_img
* @param integer $dst_x
* @param integer $dst_y
* @param integer $src_x
* @param integer $src_y
* @param integer $dst_w
* @param integer $dst_h
* @param integer $src_w
* @param integer $src_h
* @access public
* @desc Rezised/resamples an image. Faster and higher quality than the regular imageCopyResampled
* @example imageCopyResampleBicubic('new_img.png', 'old_img.png', 0, 0, 0, 0, 100, 100, 1000, 1000);
*/
function imageCopyResampleBicubic
($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
$scaleX = ($src_w - 1) / $dst_w;
$scaleY = ($src_h - 1) / $dst_h;
$scaleX2 = $scaleX / 2.0;
$scaleY2 = $scaleY / 2.0;
$tc = imageIsTrueColor($src_img);
for ($y = $src_y; $y < $src_y + $dst_h; $y++) {
$sY = $y * $scaleY;
$siY = (int
) $sY;
$siY2 = (int
) $sY + $scaleY2;
for ($x = $src_x; $x < $src_x + $dst_w; $x++) {
$sX = $x * $scaleX;
$siX = (int
) $sX;
$siX2 = (int
) $sX + $scaleX2;
if ($tc) {
$c1 = imageColorAt($src_img, $siX, $siY2);
$c2 = imageColorAt($src_img, $siX, $siY);
$c3 = imageColorAt($src_img, $siX2, $siY2);
$c4 = imageColorAt($src_img, $siX2, $siY);
$r = (($c1 + $c2 + $c3 + $c4) >> 2) & 0xFF0000;
$g = ((($c1 & 0xFF00) + ($c2 & 0xFF00) + ($c3 & 0xFF00) + ($c4 & 0xFF00)) >> 2) & 0xFF00;
$b = ((($c1 & 0xFF) + ($c2 & 0xFF) + ($c3 & 0xFF) + ($c4 & 0xFF)) >> 2);
imageSetPixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
} else {
$c1 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX, $siY2));
$c2 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX, $siY));
$c3 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX2, $siY2));
$c4 = imageColorsForIndex($src_img, imageColorAt($src_img, $siX2, $siY));
$r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red'] ) << 14;
$g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
$b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue'] ) >> 2;
imageSetPixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
}
}
}
}
?>
<?php
/**
* @author Brian Schmidt
* @version 1.0
* @return boolean
* @param string $str_file
* @access public
* @desc Checks if $str_file is a binary file or not.
* @example $bool_binary = is_binary('example.tar.gz');
*/
function is_binary
($str_file)
{
$str_tmp = '';
@$src_file = fopen($str_file, 'rb');
@$str_tmp = fread($src_file, 256);
@fclose($src_file);
if ($str_tmp != '') {
$str_tmp = str_replace(chr(10), '', $str_tmp);
$str_tmp = str_replace(chr(13), '', $str_tmp);
$int_tmp = 0;
for ($i = 0; $i < strlen($str_tmp); $i++) {
if (extension_loaded('ctype') && !ctype_print($str_tmp[$i])) {
$int_tmp++;
}
elseif(!eregi("[[:print:]]+", $str_tmp[$i])) {
$int_tmp++;
}
}
if ($int_tmp > 5) {
return TRUE;
}
else {
return FALSE;
}
}
else {
return TRUE;
}
}
?>
<?php
/**
* @author Brian Schmidt
* @version 1.0
* @return boolean
* @param string $str_mail
* @access public
* @desc Checks if $str_mail is a valid e-mail address with simple regexp
* @example $bool_mailcheck = is_mail('some.mail.address@example.com');
*/
function is_mail
($str_mail)
{
return (preg_match('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i', trim($str_mail)));
}
?>
<?php
/**
* @author Brian Schmidt
* @version 1.0
* @return array
* @param array $arr_extensions
* @param boolean $bool_inverse
* @param boolean $bool_showdirs
* @param string $str_hidestring
* @access public
* @desc Returns an array of files in the current folder.
* @example $arr_files = list_dir(array('.png', '.jpg'));
*/
function list_dir
($str_dir = '.', $arr_extensions = NULL, $bool_inverse = FALSE, $bool_showdirs = TRUE, $str_hidestring = NULL)
{
$arr_dirs = array();
$arr_files = array();
$dir = dir($str_dir);
while ($str_file = $dir->read()) {
$str_full_path = str_replace(array('//', '/./'), '/', $str_dir . '/' . $str_file);
if (($str_file == '.') || ((!$bool_showdirs) && is_dir($str_full_path)) || (isset($str_hidestring) && (substr($str_file, 0, strlen($str_hidestring)) == $str_hidestring) && (($bool_showdirs) && ($str_file != '..'))) || (isset($arr_extensions) && is_array($arr_extensions) && !is_dir($str_full_path) && (((!$bool_inverse) && !in_array(strtolower(strrchr($str_file, '.')), $arr_extensions)) || (($bool_inverse) && in_array(strtolower(strrchr($str_file, '.')), $arr_extensions))))) {
continue;
}
if (($bool_showdirs) && is_dir($str_full_path)) {
$arr_dirs[] = $str_file;
} else {
$arr_files[] = $str_file;
}
}
if ($bool_showdirs) {
natcasesort($arr_dirs);
}
natcasesort($arr_files);
$arr_sorted = ($bool_showdirs) ?
array_merge($arr_dirs, $arr_files) : $arr_files;
return (count($arr_sorted) < 1) ?
0 : $arr_sorted;
}
?>
<?php
/**
* @author Brian Schmidt
* @version 1.0
* @return string
* @param string $str_mib
* @access public
* @desc Converts a MIB (Management Information Base, or SNMP object) to a Cisco style MAC address.
* @example $str_mac = mib2mac('.1.3.6.1.4.1.9.10.59.1.2.1.1.3.0.8.14.30.222.238 = Gauge32: 5740');
*/
function mib2mac
($str_mib)
{
$str_rev = strrev($str_mib);
$arr_sep = explode('.', trim(substr($str_rev, (strpos($str_rev, '=') + 1))));
for ($i = 0; $i < 6; $i++) {
$str_tmp = dechex(strrev($arr_sep[$i]));
$arr_tmp[] = (strlen($str_tmp) < 2) ?
'0' . $str_tmp : $str_tmp;
}
$arr_hex = array_reverse($arr_tmp);
for ($i = 0; $i < count($arr_hex); $i++) {
if ($i == 2 || $i == 4) {
$str_hex .= '.';
}
$str_hex .= $arr_hex[$i];
}
return $str_hex;
}
?>
<?php
/**
* @author Brian Schmidt
* @version 1.0
* @return string
* @param integer $length
* @access public
* @desc Generates a random string of $int_length ASCII characters.
* @example $str_pass = random_ascii(10);
*/
function rand_ascii
($length = 8)
{
$chars = array(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
);
for ($i = 0; $i < $length; $i++) {
srand((double
)microtime()*1000000);
$char = rand(0, count($chars));
$string .= $chars[$char];
}
return $string;
}
?>
<?php
/**
* @author Brian Schmidt
* @version 1.0
* @return bool
* @param string $str_mail
* @access public
* @desc Checks if $str_mail is a valid e-mail address, both with regular expression and DNS/MX lookup
* @example $bool_mailcheck = valid_mail('some.mail.address@example.com');
*/
function valid_mail
($str_mail)
{
if (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-wyz][a-z](g|l|m|pa|t|u|v|z|fo|seum|me|ro|op|o)?$", $str_mail, $check)) {
if (getmxrr(substr(strstr($check[0], '@'), 1), $validate_email_temp)) {
return TRUE;
}
if(checkdnsrr(substr(strstr($check[0], '@'), 1), "ANY")) {
return TRUE;
}
}
return FALSE;
}
?>