1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
<?php
/**
***********************************************************************************************
* Common functions that manipulate strings
*
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* In case the multibyte functions are not supported, we fallback to a no-multibyte function
* IMPORTANT: If the fallback is used, the conversion of umlauts not work!
* admStrToLower\(([\w$\[\]()]+)\) -> mb_strtolower($1, 'UTF-8')
* @param string $string
* @return string
*/
function admStrToLower($string)
{
if(function_exists('mb_strtolower'))
{
return mb_strtolower($string, 'UTF-8');
}
return strtolower($string);
}
/**
* In case the multibyte functions are not supported, we fallback to a no-multibyte function
* IMPORTANT: If the fallback is used, the conversion of umlauts not work!
* @param string $string
* @return string
*/
function admStrToUpper($string)
{
if(function_exists('mb_strtoupper'))
{
return mb_strtoupper($string, 'UTF-8');
}
return strtoupper($string);
}
/**
* removes html, php code and blancs at beginning and end
* of string or all elements of array without ckeditor variables !!!
* @param string[] $srcArray
* @return string[]
*/
function admStrStripTagsSpecial(array $srcArray)
{
foreach($srcArray as $key => $value)
{
if($key !== 'ecard_message' // ckeditor-variable
&& $key !== 'ann_description'
&& $key !== 'dat_description'
&& $key !== 'gbc_text'
&& $key !== 'gbo_text'
&& $key !== 'lnk_description'
&& $key !== 'msg_body'
&& $key !== 'plugin_CKEditor'
&& $key !== 'room_description'
&& $key !== 'usf_description'
&& $key !== 'mail_smtp_password')
{
$srcArray[$key] = strStripTags($value);
}
}
return $srcArray;
}
/**
* removes html, php code and whitespaces at beginning and end of string or all elements of array
* @param string|string[] $value
* @return string|string[]
*/
function strStripTags($value)
{
if(is_array($value))
{
// call function for every array element
$value = array_map('strStripTags', $value);
}
else
{
// remove whitespaces at beginning and end
$value = trim($value);
// removes html and php code
$value = strip_tags($value);
}
return $value;
}
/**
* fuegt Quotes einem mittels addslashes() gequoteten Array und String hinzu
* @param string|string[] $value
* @return string|string[]
*/
function strAddSlashesDeep($value)
{
if(is_array($value))
{
// call function for every array element
$value = array_map('strAddSlashesDeep', $value);
}
else
{
$value = addslashes($value);
}
return $value;
}
/**
* Entfernt Quotes aus einem mittels addslashes() gequoteten Array und String
* @param string|string[] $value
* @return string|string[]
*/
function strStripSlashesDeep($value)
{
if(is_array($value))
{
// call function for every array element
$value = array_map('strStripSlashesDeep', $value);
}
else
{
$value = stripslashes($value);
}
return $value;
}
/**
* Determines the previous or next letter in the alphabet
*
* reverse = false -> naechster Buchstabe
* reverse = true -> vorheriger Buchstabe
* Example: g -> h g -> f
*
* @param string $letter
* @param bool $reverse
* @return string
*/
function strNextLetter($letter, $reverse = false)
{
$ascii = ord($letter);
$aLowerCase = ord('a');
$zLowerCase = ord('z');
$aUpperCase = ord('A');
$zUpperCase = ord('Z');
if ($ascii === $aLowerCase || $ascii === $zLowerCase || $ascii === $aUpperCase || $ascii === $zUpperCase)
{
if (!$reverse && ($ascii === $aLowerCase || $ascii === $aUpperCase))
{
++$ascii;
}
if ($reverse && ($ascii === $zLowerCase || $ascii === $zUpperCase))
{
--$ascii;
}
}
else
{
if ($reverse)
{
--$ascii;
}
else
{
++$ascii;
}
}
return chr($ascii);
}
/**
* Check if a string contains only valid characters. Therefore the string is
* compared with a hard coded list of valid characters for each datatype.
* @param string $string The string that should be checked.
* @param string $checkType The type @b email, @b file, @b noSpecialChar, @b phone or @b url that will be checked.
* Each type has a different valid character list.
* @return bool Returns @b true if all characters of @b string match the internal character list.
*/
function strValidCharacters($string, $checkType)
{
if(trim($string) !== '')
{
switch ($checkType)
{
case 'email':
$validRegex = '/^[áàâåäæcccçéèeênnñóòôöõøœúùûüß\w\.@+-]+$/';
break;
case 'file':
$validRegex = '/^[áàâåäæcccçéèeênnñóòôöõøœúùûüß\w\.@$&!?() +-]+$/';
break;
case 'noSpecialChar': // eine einfache E-Mail-Adresse sollte dennoch moeglich sein (Benutzername)
$validRegex = '/^[\w\.@+-]+$/';
break;
case 'phone':
$validRegex = '/^[\d\/() +-]+$/';
break;
case 'url':
$validRegex = '/^[áàâåäæcccçéèeênnñóòôöõøœúùûüß\w\.\/@$&!?%=#:() +-~]+$/';
break;
default:
return false;
}
// check if string contains only valid characters
if(preg_match($validRegex, admStrToLower($string)))
{
switch ($checkType)
{
case 'email':
return filter_var(trim($string), FILTER_VALIDATE_EMAIL) !== false;
case 'url':
return filter_var(trim($string), FILTER_VALIDATE_URL) !== false;
default:
return true;
}
}
}
return false;
}
/**
* Check if a filename contains invalid characters. The characters will be checked with strValidCharacters.
* In addition the function checks if the name contains .. or a . at the beginning.
* @param string $filename Name of the file that should be checked.
* @param bool $checkExtension If set to @b true then the extension will be checked against a blacklist of extensions:
* php, php3, php4, php5, html, htm, htaccess, htpasswd, pl, js, vbs, asp, cgi, ssi
* @throws AdmException SYS_FILENAME_EMPTY : Filename was empty
* BAC_FILE_NAME_INVALID : Filename contains invalid characters
* DOW_FILE_EXTENSION_INVALID : Filename contains invalid extension
* @return true Returns @true if filename contains only valid characters. Otherwise an AdmException is thrown
*/
function admStrIsValidFileName($filename, $checkExtension = false)
{
// If the filename was not empty
if (trim($filename) === '')
{
throw new AdmException('SYS_FILENAME_EMPTY');
}
// filename should only contains valid characters and don't start with a dot
if (basename($filename) !== $filename || !strValidCharacters($filename, 'file') || strpos($filename, '.') === 0)
{
throw new AdmException('BAC_FILE_NAME_INVALID');
}
if ($checkExtension)
{
// check if the extension is not blacklisted
$extensionBlacklist = array('php', 'php3', 'php4', 'php5', 'html', 'htm', 'htaccess', 'htpasswd', 'pl',
'js', 'vbs', 'asp', 'cgi', 'ssi');
$fileExtension = substr($filename, strrpos($filename, '.') + 1);
if (in_array(strtolower($fileExtension), $extensionBlacklist, true))
{
throw new AdmException('DOW_FILE_EXTENSION_INVALID');
}
}
return true;
}