started to refactor functions to classes and use PSR-4 autoloader and namespacing

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2018-12-18 08:38:34 +01:00
parent ba93265ac6
commit c3cc3d1f62
14 changed files with 660 additions and 661 deletions

View File

@@ -1,57 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Functions
*
*/
/**
* set the immutable flag for a file
*
* @param string $filename the file to set the flag for
*
* @return boolean
*/
function setImmutable($filename = null) {
safe_exec(_getImmutableFunction(false).escapeshellarg($filename));
}
/**
* removes the immutable flag for a file
*
* @param string $filename the file to set the flag for
*
* @return boolean
*/
function removeImmutable($filename = null) {
safe_exec(_getImmutableFunction(true).escapeshellarg($filename));
}
/**
* internal function to check whether
* to use chattr (Linux) or chflags (FreeBSD)
*
* @param boolean $remove whether to use +i|schg (false) or -i|noschg (true)
*
* @return string functionname + parameter (not the file)
*/
function _getImmutableFunction($remove = false) {
if (isFreeBSD()) {
// FreeBSD style
return 'chflags '.(($remove === true) ? 'noschg ' : 'schg ');
} else {
// Linux style
return 'chattr '.(($remove === true) ? '-i ' : '+i ');
}
}

View File

@@ -1,42 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2003-2009 the SysCP Team (see authors).
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Florian Lippert <flo@syscp.org> (2003-2009)
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Functions
*
*/
/**
* Function which returns a correct dirname, means to add slashes at the beginning and at the end if there weren't some
*
* @param string $dir
* The dirname
*
* @return string The corrected dirname
*/
function makeCorrectDir($dir)
{
if (is_string($dir) && strlen($dir) > 0) {
$dir = trim($dir);
if (substr($dir, - 1, 1) != '/') {
$dir .= '/';
}
if (substr($dir, 0, 1) != '/') {
$dir = '/' . $dir;
}
$dir = makeSecurePath($dir);
return $dir;
}
throw new Exception("Cannot validate directory in " . __FUNCTION__ . " which is very dangerous.");
}

View File

@@ -1,51 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2003-2009 the SysCP Team (see authors).
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Florian Lippert <flo@syscp.org> (2003-2009)
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Functions
*
*/
/**
* Function which returns a correct filename, means to add a slash at the beginning if there wasn't one
*
* @param string filename the filename
* @return string the corrected filename
* @author Florian Lippert <flo@syscp.org>
* @author Michael Russ <mr@edvruss.com>
* @author Martin Burchert <eremit@adm1n.de>
*/
function makeCorrectFile($filename)
{
if (!isset($filename)
|| trim($filename) == ''
) {
$error = 'Given filename for function '.__FUNCTION__.' is empty.'."\n";
$error.= 'This is very dangerous and should not happen.'."\n";
$error.= 'Please inform the Froxlor team about this issue so they can fix it.';
echo $error;
// so we can see WHERE this happened
debug_print_backtrace();
die();
}
if(substr($filename, 0, 1) != '/')
{
$filename = '/' . $filename;
}
$filename = makeSecurePath($filename);
return $filename;
}

View File

@@ -1,52 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2003-2009 the SysCP Team (see authors).
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Florian Lippert <flo@syscp.org> (2003-2009)
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Functions
*
*/
/**
* Function which returns a secure path, means to remove all multiple dots and slashes
*
* @param string The path
* @return string The corrected path
* @author Florian Lippert <flo@syscp.org>
*/
function makeSecurePath($path) {
// check for bad characters, some are allowed with escaping
// but we generally don't want them in our directory-names,
// thx to aaronmueller for this snipped
$badchars = array(':', ';', '|', '&', '>', '<', '`', '$', '~', '?', "\0");
foreach ($badchars as $bc) {
$path = str_replace($bc, "", $path);
}
$search = array(
'#/+#',
'#\.+#'
);
$replace = array(
'/',
'.'
);
$path = preg_replace($search, $replace, $path);
// don't just replace a space with an escaped space
// it might be escaped already
$path = str_replace("\ ", " ", $path);
$path = str_replace(" ", "\ ", $path);
return $path;
}

View File

@@ -1,109 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2003-2009 the SysCP Team (see authors).
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Florian Lippert <flo@syscp.org> (2003-2009)
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Functions
*
*/
/**
* Creates a directory below a users homedir and sets all directories,
* which had to be created below with correct Owner/Group
* (Copied from cron_tasks.php:rev1189 as we'll need this more often in future)
*
* @param string The homedir of the user
* @param string The dir which should be created
* @param int The uid of the user
* @param int The gid of the user
* @param bool Place standard-index.html into the new folder
* @param bool Allow creating a directory out of the customers docroot
*
* @return bool true if everything went okay, false if something went wrong
*
* @author Florian Lippert <flo@syscp.org>
* @author Martin Burchert <martin.burchert@syscp.org>
*/
function mkDirWithCorrectOwnership($homeDir, $dirToCreate, $uid, $gid, $placeindex = false, $allow_notwithinhomedir = false)
{
$returncode = true;
if($homeDir != ''
&& $dirToCreate != '')
{
$homeDir = makeCorrectDir($homeDir);
$dirToCreate = makeCorrectDir($dirToCreate);
if(substr($dirToCreate, 0, strlen($homeDir)) == $homeDir)
{
$subdir = substr($dirToCreate, strlen($homeDir) - 1);
$within_homedir = true;
}
else
{
$subdir = $dirToCreate;
$within_homedir = false;
}
$subdir = makeCorrectDir($subdir);
$subdirs = array();
if($within_homedir || !$allow_notwithinhomedir)
{
$subdirlen = strlen($subdir);
$offset = 0;
while($offset < $subdirlen)
{
$offset = strpos($subdir, '/', $offset);
$subdirelem = substr($subdir, 0, $offset);
$offset++;
array_push($subdirs, makeCorrectDir($homeDir . $subdirelem));
}
}
else
{
array_push($subdirs, $dirToCreate);
}
$subdirs = array_unique($subdirs);
sort($subdirs);
foreach($subdirs as $sdir)
{
if(!is_dir($sdir))
{
$sdir = makeCorrectDir($sdir);
safe_exec('mkdir -p ' . escapeshellarg($sdir));
/**
* #68
*/
if ($placeindex) {
$loginname = getLoginNameByUid($uid);
if ($loginname !== false) {
storeDefaultIndex($loginname, $sdir, null);
}
}
safe_exec('chown -R ' . (int)$uid . ':' . (int)$gid . ' ' . escapeshellarg($sdir));
}
}
}
else
{
$returncode = false;
}
return $returncode;
}

View File

@@ -1,57 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2003-2009 the SysCP Team (see authors).
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Florian Lippert <flo@syscp.org> (2003-2009)
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Functions
*
*/
/**
* Wrapper around the exec command.
*
* @param string $exec_string command to be executed
* @param string $return_value referenced variable where the output is stored
* @param array $allowedChars optional array of allowed characters in path/command
*
* @return string result of exec()
*/
function safe_exec($exec_string, &$return_value = false, $allowedChars = null) {
$disallowed = array(';', '|', '&', '>', '<', '`', '$', '~', '?');
$acheck = false;
if ($allowedChars != null && is_array($allowedChars) && count($allowedChars) > 0) {
$acheck = true;
}
foreach ($disallowed as $dc) {
if ($acheck && in_array($dc, $allowedChars)) continue;
// check for bad signs in execute command
if (stristr($exec_string, $dc)) {
die("SECURITY CHECK FAILED!\nThe execute string '" . $exec_string . "' is a possible security risk!\nPlease check your whole server for security problems by hand!\n");
}
}
// execute the command and return output
$return = '';
// -------------------------------------------------------------------------------
if ($return_value == false) {
exec($exec_string, $return);
} else {
exec($exec_string, $return, $return_value);
}
return $return;
}

View File

@@ -1,41 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2003-2009 the SysCP Team (see authors).
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Florian Lippert <flo@syscp.org> (2003-2009)
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Functions
*
*/
/**
* Returns Array, whose elements have been checked whether thay are empty or not
*
* @param array $source
* The array to trim
* @return array The trim'med array
* @author Florian Lippert <flo@syscp.org>
*/
function array_trim($source)
{
$returnval = array();
if (is_array($source)) {
foreach ($source as $var => $val) {
if ($val != ' ' && $val != '') {
$returnval[$var] = $val;
}
}
} else {
$returnval = $source;
}
return $returnval;
}

View File

@@ -1,33 +0,0 @@
<?php
/**
* ipv6 aware gethostbynamel function
*
* @param string $host
* @param boolean $try_a default true
* @return boolean|array
*/
function gethostbynamel6($host, $try_a = true)
{
$dns6 = dns_get_record($host, DNS_AAAA);
if ($try_a == true) {
$dns4 = dns_get_record($host, DNS_A);
$dns = array_merge($dns4, $dns6);
} else {
$dns = $dns6;
}
$ips = array();
foreach ($dns as $record) {
if ($record["type"] == "A") {
$ips[] = $record["ip"];
}
if ($record["type"] == "AAAA") {
$ips[] = $record["ipv6"];
}
}
if (count($ips) < 1) {
return false;
} else {
return $ips;
}
}

View File

@@ -1,58 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2003-2009 the SysCP Team (see authors).
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Florian Lippert <flo@syscp.org> (2003-2009)
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Functions
*
*/
/**
* Replaces all occurrences of variables defined in the second argument
* in the first argument with their values.
*
* @param string The string that should be searched for variables
* @param array The array containing the variables with their values
* @return string The submitted string with the variables replaced.
* @author Michael Duergner
*/
function replace_variables($text, $vars)
{
$pattern = "/\{([a-zA-Z0-9\-_]+)\}/";
// --- martin @ 08.08.2005 -------------------------------------------------------
// fixing usage of uninitialised variable
$matches = array();
// -------------------------------------------------------------------------------
if(count($vars) > 0
&& preg_match_all($pattern, $text, $matches))
{
for ($i = 0;$i < count($matches[1]);$i++)
{
$current = $matches[1][$i];
if(isset($vars[$current]))
{
$var = $vars[$current];
$text = str_replace("{" . $current . "}", $var, $text);
}
}
}
$text = str_replace('\n', "\n", $text);
return $text;
}

View File

@@ -1,36 +0,0 @@
<?php
/**
* Return human readable sizes
*
* @author Aidan Lister <aidan@php.net>
* @version 1.3.0
* @link http://aidanlister.com/2004/04/human-readable-file-sizes/
* @param int $size size in bytes
* @param string $max maximum unit
* @param string $system 'si' for SI, 'bi' for binary prefixes
* @param string $retstring return string format
*/
function size_readable($size, $max = null, $system = 'si', $retstring = '%01.2f %s')
{
// Pick units
$systems['si']['prefix'] = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
$systems['si']['size'] = 1000;
$systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
$systems['bi']['size'] = 1024;
$sys = isset($systems[$system]) ? $systems[$system] : $systems['si'];
// Max unit to display
$depth = count($sys['prefix']) - 1;
if ($max && false !== $d = array_search($max, $sys['prefix'])) {
$depth = $d;
}
// Loop
$i = 0;
while ($size >= $sys['size'] && $i < $depth) {
$size /= $sys['size'];
$i++;
}
return sprintf($retstring, $size, $sys['prefix'][$i]);
}