Source for file DataUtil.class.php
Documentation is available at DataUtil.class.php
* Zikula Application Framework
* @copyright Robert Gasch
* @link http://www.zikula.org
* @version $Id: DataUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @author Robert Gasch rgasch@gmail.com
* @uses generic data utililty class
* Clean a variable, remove slashes. This method is recursive array safe.
* @param var The variable to clean
* @return The formatted variable
foreach ($var as $k => $v) {
* Decode a character a previously encoded character
* @param value The value we wish to encode
* @return The decoded value
* Decrypt the given value using the mcrypt library function. If the mcrypt
* functions do not exist, we fallback to the RC4 implementation which is
* @param value The value we wish to decrypt
* @param key The encryption key to use (optional) (default=null)
* @param alg The encryption algirthm to use (only used with mcrypt functions) (optional) (default=null, signifies MCRYPT_RIJNDAEL_128)
* @param encoded Whether or not the value is base64 encoded (optional) (default=true)
* @return The decrypted value
function decrypt ($value, $key= null, $alg= null, $encoded= true)
$key = ($key ? $key : 'ZikulaEncryptionKey');
$alg = ($alg ? $alg : MCRYPT_RIJNDAEL_128);
$iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, MCRYPT_MODE_ECB), crc32($key));
$res = mcrypt_decrypt($alg, $key, $val, MCRYPT_MODE_CBC);
* Encode a character sting such that it's 8-bit clean. It maps to base64_encode().
* @param value The value we wish to encode
* @return The encoded value
* Encrypt the given value using the mcrypt library function. If the mcrypt
* functions do not exist, we fallback to the RC4 implementation which is
* @param value The value we wish to decrypt
* @param key The encryption key to use (optional) (default=null)
* @param alg The encryption algirthm to use (only used with mcrypt functions) (optional) (default=null, signifies MCRYPT_RIJNDAEL_128)
* @param encoded Whether or not the value is base64 encoded (optional) (default=true)
* @return The encrypted value
function encrypt ($value, $key= null, $alg= null, $encoded= true)
$key = ($key ? $key : 'ZikulaEncryptionKey');
$alg = ($alg ? $alg : MCRYPT_RIJNDAEL_128);
$iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, MCRYPT_MODE_ECB), crc32($key));
$res = mcrypt_encrypt($alg, $key, $value, MCRYPT_MODE_CBC);
* Format a variable for display. This method is recursive array safe.
* @param var The variable to format
* @return The formatted variable
// This search and replace finds the text 'x@y' and replaces
// it with HTML entities, this provides protection against
static $search = array('/(.)@(.)/se');
static $replace = array('"&#" .
sprintf("%03d", ord("\\1")) .
sprintf("%03d", ord("\\2")) . ";";');
foreach ($var as $k => $v) {
$var[$k] = DataUtil::formatForDisplay($v);
$var = htmlspecialchars((string) $var);
$var = preg_replace($search, $replace, $var);
* Format a variable for HTML display. This method is recursive array safe.
* @param var The variable to format
* @return The formatted variable
// This search and replace finds the text 'x@y' and replaces
// it with HTML entities, this provides protection against
// Note that the use of \024 and \022 are needed to ensure that
// this does not break HTML tags that might be around either
// the username or the domain name
static $search = array('/([^\024])@([^\022])/se');
static $replace = array('"&#" .
sprintf("%03d", ord("\\1")) .
sprintf("%03d", ord("\\2")) . ";";');
static $allowedtags = NULL;
if (!isset ($allowedtags)) {
foreach($allowableHTML as $k => $v) {
$allowedHTML[] = "$k.*?--";
$allowedHTML[] = "/?$k\s*/?";
// intelligent regex to deal with > in parameters, bug #1782 credits to jln
$allowedHTML[] = "/?\s*$k" . "(\s+[\w:]+\s*=\s*(\"[^\"]*\"|'[^']*'))*" . '\s*/?';
if (count($allowedHTML) > 0) {
// 2nd part of bugfix #1782
$allowedtags = '~<\s*(' . join('|',$allowedHTML) . ')\s*>~is';
if (!isset ($outputfilter)) {
foreach ($var as $k => $v) {
// Run additional filters
$var = pnModAPIFunc('SecurityCenter', 'user', 'secureoutput', array('var' => $var, 'filter' => $outputfilter));
// Preparse var to mark the HTML that we want
if (!empty($allowedtags)) {
// Encode email addresses
// Fix the HTML that we want
// Fix entities if required
$var = preg_replace('/&([a-z#0-9]+);/i', "&\\1;", $var);
* Format a variable for DB-storage. This method is recursive array safe.
* @param var The variable to format
* @return The formatted variable
foreach ($var as $k=> $v) {
if ($dbType== 'mssql' || $dbType== 'oci8' || $dbType== 'oracle') {
* Format a variable for operating-system usage. This method is recursive array safe.
* @param var The variable to format
* @param absolute Allow absolute paths (default=false) (optional)
* @return The formatted variable
foreach ($var as $k=> $v) {
if (isset ($cached[$var]))
//if we're supporting absolute paths and the first charater is a slash and , then
//an absolute path is passed
$absolutepathused = ($absolute && substr($var,0,1) == '/');
// Split the path at possible path delimiters.
// Setting PREG_SPLIT_NOEMPTY eliminates double delimiters on the fly.
$dirty_array = preg_split('#[:/\\\\]#', $var, - 1, PREG_SPLIT_NO_EMPTY);
// now walk the path and do the relevant things
foreach ($dirty_array as $current) {
// current path element is a dot, so we don't do anything
} elseif ($current == '..') {
// current path element is .., so we remove the last path in case of relative paths
// current path element is valid, so we add it to the path
$clean_array[] = $current;
// should we use DIRECTORY_SEPARATOR here?
//if an absolute path was passed to the function, we need to make it absolute again
if (!$GLOBALS['PNRuntime']['magic_quotes_runtime']) {
static $permalinksseparator;
if (!isset ($permalinksseparator)) {
$var = preg_replace("/&[#a-z0-9]+;/iU", "", $var); // remove &....;
$var = strtr($var," ",$permalinksseparator); //words separation
$var = strtr($var,"ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ","aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn"); //accents deletion
$var = preg_replace("/[^a-z0-9_{$permalinksseparator}]/i", '', $var);
$var = trim($var, $permalinksseparator);
$res = ini_get('mbstring.func_overload');
// any mb charsets and permalinks won't work
// add: PHP_VALUE mbstring.func_overload 6
// to your .htaccess or php.ini file
// sure, a hack - needs to be replaced with a more generic check
$msg = "put 'PHP_VALUE mbstring.func_overload 4' into .htaccess or php.ini - otherwise short urls won't work";
$var = ereg_replace("[[:space:]]", $permalinksseparator, $var);
* Censor variable contents. This method is recursive array safe.
* @param var The variable to censor
* @return The censored variable
foreach ($var as $k=> $v) {
$var = pnModAPIFunc('MultiHook','user','censor', array('word' => $var)); // preg_replace($search, $replace, $var);
* Perform SHA1 or SHA256 hashing on a string using native
* PHP functions if available and if not uses own classes.
* @param $string string to be hashed
* @param $type string md5, sha1 (default), sha256
* @return string hex hash
function hash ($string, $type= 'sha1')
else if ($type == 'sha256') {
return bin2hex(mhash(MHASH_SHA256, $string));
else if ($type == 'md5') {
* This method converts the several possible return values from
* allegedly "boolean" ini settings to proper booleans
* Properly converted input values are: 'off', 'on', 'false', 'true', '0', '1'
* If the ini_value doesn't match any of those, the value is returned as-is.
* @param string $ini_key the ini_key you need the value of
* check for serialization
* converts a string or an array (recursivly) to utf-8
* @param input - string or array to convert to utf-8
* @return converted string or array
* @author Frank Schummertz
foreach($input as $key => $value) {
return mb_convert_encoding($input, 'UTF-8', _CHARSET);
* converts a string from utf-8
* @param input - string or array to convert from utf-8
* @return converted string
* @author Frank Schummertz
foreach($input as $key => $value) {
return mb_convert_encoding($input, _CHARSET, 'UTF-8');
* Callback function for pnVarPrepHTMLDisplay
* @author Xaraya development team
function DataUtil_pnVarPrepHTMLDisplay__callback($matches)
$res = '<' . strtr($matches[1],
'&' => '&'*/)) . '>';
|