Sources for file plugins/class/input.php in version 4.0 Beta 1
Click on a comment to hide it. Click here to show all comments.
/**
* Project: Xnyo 4: Bubbles
* File: plugins/class/input.php
*
* Version: 4.0-dev
* SVN Id: $Id: input.php 5 2007-05-18 03:49:07Z bok $
* SVN URL: $HeadURL:
http://svn.syd.wholesalebroadband.com.au/xnyo/trunk/plugins/class/input.php $
* Authors: Robert Amos <bok[at]odynia.org>
*
* Copyright (c) 2001-2007 Robert Amos <bok[at]odynia.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
/**
* Input Plugin
**/
class XnyoInput implements XnyoClassPlugin
{
// Plugin Information
public $_plugin_info = array
(
'name' => 'Xnyo Input Plugin',
'description' => 'Input filtering function s',
'action' => XNYO_PLUGIN_ACTION_GLOBAL,
'varname' => 'input'
);
/**
* Integers
**/
public static function int ($s)
{
$s = intval(self::number($s));
return $s;
}
public static function integer ($s)
{
return self::int($s);
}
public $number = array('safetext');
/**
* Numeric String
**/
public static function number ($s)
{
return preg_replace('/[^\-0-9.]/i', '', $s);
}
/**
* Floats / Doubles / Reals / Whatever you call them
**/
public static function float ($s)
{
return floatval(self::number($s));
}
public static function double ($s)
{
return self::float($s);
}
public static function real ($s)
{
return self::real($s);
}
/**
* Basic Text / Strings
**/
public static function text ($s)
{
// remove unnecessary control characters
return strval(preg_replace('/[\x00-\x07\x16-\x1f]/', '', $s));
}
public static function string ($s)
{
return self::text($s);
}
/**
* Safe Text
**/
public static function safetext ($s)
{
// only allow letters, numbers, underscores, hyphens
return preg_replace('/[^a-zA-Z0-9_-]/', '', $s);
}
public static function safestring ($s)
{
return self::safetext($s);
}
/**
* Filenames
**/
public static function filename ($s)
{
// no accessing parent directories
$s = preg_replace('/\.{2,}/', '', $s);
// clean up multiple slashies
$s = preg_replace('/\/{2,}/', '/', $s);
// definitely no absolute paths
$s = preg_replace('/^\//', '', $s);
// all good
return $s;
}
public static function abs_filename ($s)
{
$s = preg_replace('/\.{2,}/', '', $s);
$s = preg_replace('/\/{2,}/', '/', $s);
return $s;
}
/**
* Booleans
**/
public static function bool ($s)
{
$s = strtolower($s);
if ($s == 1 || $s == 'true' || $s == true || $s == 't' || $s == 'yes' || $s == 'y' || $s == 'on')
return true;
return false;
}
public static function boolean ($s)
{
return self::bool($s);
}
/**
* Usernames
**/
public static function username ($s)
{
// we allow alphanum, underscores, hyphens, dots, at-symbols
return preg_replace('/[^a-zA-Z0-9_\-.@\\\]/', '', $s);
}
/**
* Passwords are alot more lenient, just no control characters really
**/
public static function password ($s)
{
return self::text($s);
}
/**
* Email Address
**/
public static function email ($s)
{
// basic syntax checking
if
(!preg_match('/^[a-zA-Z0-9!#$%^\'*+\-\/=?\^_\`\{\}|~]{1,64}\@([a-zA-Z0-9\-.]{5,255})$/',
$s, $m))
return '';
// valid email address! so far
// if we have the MX record lookup function s available, lets just check to see if a record exists
if (!function_exists('getmxrr'))
return $s;
// we do. look it up
if (@getmxrr($m[1], $r) || @gethostbyname($m[2]))
// valid MX or A record found for that domain, thats all we need to deliver mail
// connecting to their mail server to check the local part is too expensive time-wise
return $s;
return '';
}
/**
* Alpha-Numeric Strings
**/
public static function alphanum ($s)
{
return preg_replace('/[^a-zA-Z0-9]/', '', $s);
}
/**
* Uppercase/Lowercase
**/
public static function uppercase ($s)
{
return strtoupper($s);
}
public static function lowercase ($s)
{
return strtolower($s);
}
/**
* Query String
**/
public static function query ($s)
{
$arr = $arr2 = array();
parse_str($s, $arr);
foreach ($arr as $key => $var)
$arr2[XnyoInput::underscore($key)] = $var;
return $arr2;
}
/**
* Takes any camelCase string and underscores it
**/
public static function underscore ($s)
{
return strtolower(preg_replace('/([A-Z])/', '_\\1', $s));
}
/**
* HTTP Header
**/
public static function header ($s)
{
return preg_replace('/[^a-zA-Z0-9\-\/*;\s=,()[\]]/', '', $s);
}
/**
* Hex
**/
public static function hex ($s)
{
return preg_replace('/[^0-9a-fA-F]/', '', $s);
}
/**
* Null
**/
public static function null ($s)
{
// not sure why anyone would want this, but who am I to argue?
return null;
}
/**
* Regexps
**/
public static function regexp ($s, $p)
{
return preg_replace($p, '', $s);
}
/**
* IP Address
**/
public static function ip ($s)
{
return join('.', array_map(array('self', 'int'), explode('.', $s)));
}
/**
* Host/Domain Name
**/
public static function host ($s)
{
return preg_replace('/[^a-zA-Z0-9\-.]/', '', $s);
}
/**
* Array Handling
**/
public static function _array ($s, $t)
{
if (!is_array($s))
$s = array($s);
$ns = array();
foreach ($s as $k => $v)
{
if (is_array($t))
{
$nt = $t[0];
$ns[$k] = self::_array($v, $nt);
} elseif (in_array($t, get_class_methods(__CLASS__)))
$ns[$k] = self::$t($v);
}
return $ns;
}
/**
* Date (any strtotime() parseable string)
* Note: This returns a unix timestamp
**/
public static function date ($s)
{
$t = strtotime($s);
if ($t !== -1)
return $t;
// what if its already a unix timestamp?
$t = self::int($s);
if ($t > 0)
return $t;
// nothing to return, return the epoch
return 0;
}
/**
* Years (0 - 9999)
**/
public static function year ($s)
{
$s = self::int($s);
if ($s > 0 && $s < 9999)
return $s;
return 0;
}
/**
* Months (1 - 12)
**/
public static function month ($s)
{
// integer month?
$m = self::int($s);
if ($m > 0 && $m < 13)
return $m;
// text month?
$s = strtolower(self::safetext($s));
if ($s == 'jan' || $s == 'january') return 1;
if ($s == 'feb' || $s == 'february') return 2;
if ($s == 'mar' || $s == 'march') return 3;
if ($s == 'apr' || $s == 'april') return 4;
if ($s == 'may') return 5;
if ($s == 'jun' || $s == 'june') return 6;
if ($s == 'jul' || $s == 'july') return 7;
if ($s == 'aug' || $s == 'august') return 8;
if ($s == 'sep' || $s == 'sept' || $s == 'september') return 9;
if ($s == 'oct' || $s == 'october') return 10;
if ($s == 'nov' || $s == 'november') return 11;
if ($s == 'dec' || $s == 'december') return 12;
// ruh-roh!
return 0;
}
/**
* Days (1 - 31)
**/
public static function day ($s)
{
$s = self::int($s);
if ($s > 0 && $s < 32)
return $s;
return 0;
}
/**
* "Safe" HTML
**/
public static function htmlsafe ($s)
{
return htmlentities($s, ENT_QUOTES);
}
/**
* "Safe" HTML + nl2br!
**/
public static function htmlnlsafe ($s)
{
return preg_replace('/\n/', '<br />'."\n", self::htmlsafe($s));
}
/**
* Reverse XnyoInput::htmlsafe()
**/
public static function unhtmlsafe ($s)
{
$t = array_flip(get_html_translation_table(HTML_ENTITIES));
return strtr($s, $t);
}
/**
* Reverse XnyoInput::htmlnlsafe()
**/
public static function unhtmlnlsafe ($s)
{
return self::unhtmlsafe(preg_replace('/<br \/>/', '', $s));
}
/**
* Trim Whitespace
**/
public static function trim_whitespace($s)
{
// match these html blocks
$bm = 'script|pre|style|textarea';
// pull the blocks out for safe keeping
preg_match_all('/<('.$bm.')[^>]*?>.*?<\/('.$bm.')>/is', $s, $m);
$b = $m[0];
$s = preg_replace('/<('.$bm.')[^>]*?>.*?<\/('.$bm.')>/is', '@@@XNYO:TRIM@@@', $s);
// clean up all whitespaces from the start of a line to a HTML tag
$s = preg_replace('/^\s+?</m', '<', $s);
// and the end
$s = preg_replace('/>\s+?$/m', '>', $s);
// compact empty lines
$s = preg_replace('/>\n+?</m', ">\n<", $s);
// replace the blocks
foreach ($b as $c)
$s = preg_replace('/@@@XNYO:TRIM@@@/', $c, $s, 1);
// finish
return $s;
}
/**
* Url Encode
**/
public static function urlencode ($s)
{
return urlencode($s);
}
public static function urldecode ($s)
{
return urldecode($s);
}
public static function pathurlencode ($s)
{
return join('/', array_map('rawurlencode', explode('/', $s)));
}
public static function pathurldecode($s)
{
return self::urldecode($s);
}
}
