diff --git a/lib/classes/database/class.Database.php b/lib/classes/database/class.Database.php index 7adc2094..fef3002f 100644 --- a/lib/classes/database/class.Database.php +++ b/lib/classes/database/class.Database.php @@ -53,6 +53,12 @@ class Database { */ private static $_dbname = null; + /** + * sql-access data + */ + private static $_needsqldata = false; + private static $_sqldata = null; + /** * Wrapper for PDOStatement::execute so we can catch the PDOException * and display the error nicely on the panel @@ -119,6 +125,40 @@ class Database { self::$_needroot = $needroot; } + /** + * enable the temporary access to sql-access data + * note: if you want root-sqldata you need to + * call needRoot(true) first. Also, this will + * only give you the data ONCE as it disable itself + * after the first access to the data + * + * @param bool $needsql + */ + public static function needSqlData($needsql = false) { + self::$_needsqldata = $needsql; + self::$_sqldata = array(); + self::$_link = null; + } + + /** + * returns the sql-access data as array using indeces + * 'user', 'passwd' and 'host'. Returns false if not enabled + * + * @return array|bool + */ + public static function getSqlData() { + if (self::$_sqldata !== null + && is_array(self::$_sqldata) + && isset(self::$_sqldata['user']) + ) { + return self::$_sqldata; + // automatically disable sql-data + self::$_sqldata = null; + self::$_needsqldata = false; + } + return false; + } + /** * let's us interact with the PDO-Object by using static * call like "Database::function()" @@ -194,6 +234,15 @@ class Database { $host = $sql["host"]; } + // save sql-access-data if needed + if (self::$_needsqldata) { + self::$_sqldata = array( + 'user' => $user, + 'passwd' => $password, + 'host' => $host + ); + } + // build up connection string $driver = 'mysql'; $dsn = $driver.":"; diff --git a/scripts/jobs/cron_backup.php b/scripts/jobs/cron_backup.php index fa58db07..76ebee09 100644 --- a/scripts/jobs/cron_backup.php +++ b/scripts/jobs/cron_backup.php @@ -15,115 +15,144 @@ * */ -if(@php_sapi_name() != 'cli'){ - die('This script will only work in the shell'); -} - -openRootDB($debugHandler, $lockfile); - /** * Backup - */ +*/ +if ($settings['system']['backup_enabled'] == '1') { -if($settings['system']['backup_enabled'] == '1'){ + fwrite($debugHandler, 'backup customers started...' . "\n"); - fwrite($debugHandler, 'backup customers started...' . "\n"); + // get sql-root access data for mysqldump + Database::needRoot(true); + Database::needSqlData(true); + $sql_root = Database::getSqlData(); + Database::needRoot(false); - $result = $db->query("SELECT customerid, loginname, guid, documentroot, backup_allowed, backup_enabled FROM `" . TABLE_PANEL_CUSTOMERS . "` ORDER BY `customerid` ASC;"); - while($row = $db->fetch_array($result)){ - fwrite($debugHandler, 'backup for ' . $row['loginname'] . ' started...' . "\n"); + $result_stmt = Database::query(" + SELECT customerid, loginname, guid, documentroot, backup_allowed, backup_enabled + FROM `" . TABLE_PANEL_CUSTOMERS . "` ORDER BY `customerid` ASC; + "); - // backup - if($row['backup_allowed'] == '1' && $row['backup_enabled'] == '1'){ - // get uid & gid from ftp table - $ftp_result = $db->query("SELECT uid, gid FROM `" . TABLE_FTP_USERS . "` WHERE `username` = '" . $db->escape($row['loginname']) . "';"); - $ftp_row = mysql_fetch_array($ftp_result); + while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { - // create backup dir an set rights - if(!file_exists($settings['system']['backup_dir'] . $row['loginname'])) { - safe_exec('install -d ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . ' -o ' . escapeshellarg($ftp_row['uid']) . ' -g ' . escapeshellarg($ftp_row['gid']) . ' -m ' . '0500'); - } + fwrite($debugHandler, 'backup for ' . $row['loginname'] . ' started...' . "\n"); - // create customers html backup - safe_exec('tar -C ' . escapeshellarg($row['documentroot']) . ' -c -z -f ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/' . escapeshellarg($row['loginname']) . 'html.tar.gz .'); + // backup + if ($row['backup_allowed'] == '1' + && $row['backup_enabled'] == '1' + ) { + // get uid & gid from ftp table + $ftp_result_stmt = Database::prepare(" + SELECT uid, gid FROM `" . TABLE_FTP_USERS . "` + WHERE `username` = :loginname + "); + $ftp_row = Database::pexecute_first($ftp_result_stmt, array('loginname' => $row['loginname'])); - // get customer dbs - $dbs_result = $db->query("SELECT databasename FROM `" . TABLE_PANEL_DATABASES . "` WHERE `customerid` = '" . $db->escape($row['customerid']) . "';"); - while($dbs_row = $db->fetch_array($dbs_result)){ - // create customers sql backup - safe_exec(escapeshellcmd($settings['system']['backup_mysqldump_path']) . ' --opt --force --allow-keywords -u ' . escapeshellarg($sql_root[0]['user']) . ' -p' . escapeshellarg($sql_root[0]['password']) . ' -h ' . $sql_root[0]['host'] . ' -B ' . escapeshellarg($dbs_row['databasename']) . ' -r ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/' . escapeshellarg($dbs_row['databasename']) . '.sql' ); - // compress sql backup - safe_exec('tar -C ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . ' -c -z -f ' . escapeshellarg($settings['system']['backup_dir']) . $row['loginname'] . '/' . escapeshellarg($dbs_row['databasename']) . '.tar.gz ' . escapeshellarg($dbs_row['databasename']) . '.sql'); - // remove uncompresed sql files - safe_exec('rm ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/' . escapeshellarg($dbs_row['databasename']) . '.sql'); - } - - // create 1 big file with html & db - if($settings['system']['backup_bigfile'] == 1){ - safe_exec('tar -C ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/' . ' --exclude=' . escapeshellarg($row['loginname']) . '.tar.gz -c -z -f ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/' . escapeshellarg($row['loginname']) . '.tar.gz .'); - // remove separated files - $tmp_files = scandir($settings['system']['backup_dir'] . $row['loginname']); - foreach ($tmp_files as $tmp_file){ - if(preg_match('/.*(html|sql|aps).*\.tar\.gz$/', $tmp_file) && !preg_match('/^' . $row['loginname'] . '\.tar\.gz$/', $tmp_file)){ - safe_exec('rm ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/' . escapeshellarg($tmp_file) . ''); - } - } - } - else { - //remove big file if separated backups are used - if (file_exists($settings['system']['backup_dir'] . $row['loginname'] . '/' . $row['loginname'] . '.tar.gz')) { - safe_exec('rm ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/' . escapeshellarg($row['loginname']) . '.tar.gz'); - } - } - - // chown & chmod files to prevent manipulation - safe_exec('chown ' . escapeshellarg($row['guid']) . ':' . escapeshellarg($row['guid']) . ' ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/*'); - safe_exec('chmod 0400 ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/*'); - - // create ftp backup user - $user_result = $db->query("SELECT username, password FROM `" . TABLE_FTP_USERS . "` WHERE `customerid` = '" . $db->escape($row['customerid']) . "' AND `username` = '" . $db->escape($row['loginname']) . "';"); - $user_row = mysql_fetch_array($user_result); - $db->query("REPLACE INTO `" . TABLE_FTP_USERS . "` (`customerid`, `username`, `password`, `homedir`, `login_enabled`, `uid`, `gid`) VALUES ('" . $db->escape($row['customerid']) . "', '" . $db->escape($row['loginname']) . "_backup', '" . $db->escape($user_row['password']) . "', '" . $db->escape($settings['system']['backup_dir']) . $db->escape($row['loginname']) . "/', 'y', '" . $db->escape($row['guid']) . "', '" . $db->escape($row['guid']) . "')"); - - if($settings['system']['backup_ftp_enabled'] == '1'){ - // upload backup to customers ftp server - $ftp_files = scandir($settings['system']['backup_dir'] . $row['loginname']); - foreach ($ftp_files as $ftp_file){ - if(preg_match('/.*\.tar\.gz$/', $ftp_file)){ - $ftp_con = ftp_connect($settings['system']['backup_ftp_server']); - $ftp_login = ftp_login($ftp_con, $settings['system']['backup_ftp_user'], $settings['system']['backup_ftp_pass']); - - /* Check whether to use passive mode or not */ - if($settings['system']['backup_ftp_passive'] == 1) - { - ftp_pasv($ftp_con, true); + // create backup dir an set rights + $_backupdir = makeCorrectDir($settings['system']['backup_dir'] . $row['loginname']); + if (!file_exists($_backupdir)) { + safe_exec('install -d ' . escapeshellarg($_backupdir) . ' -o ' . escapeshellarg($ftp_row['uid']) . ' -g ' . escapeshellarg($ftp_row['gid']) . ' -m ' . '0500'); } - else - { - ftp_pasv($ftp_con, false); - } - - $ftp_upload = ftp_put($ftp_con, $ftp_file, $settings['system']['backup_dir'] . $row['loginname'] . "/" . $ftp_file, FTP_BINARY); - } - } - } - fwrite($debugHandler, 'backup for ' . $row['loginname'] . ' finished...' . "\n"); - } - - // delete old backup data (deletes backup if customer or admin disables backup) - elseif($row['backup_allowed'] == '0' || $row['backup_enabled'] == '0'){ - if (file_exists($settings['system']['backup_dir'] . $row['loginname'] . '/')){ - $files = scandir($settings['system']['backup_dir'] . $row['loginname'] . '/'); - foreach ($files as $file){ - if(preg_match('/.*\.tar\.gz$/', $file)){ - safe_exec('rm ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/' . escapeshellarg($file) . ''); - } + // create customers html backup + safe_exec('tar -C ' . escapeshellarg($row['documentroot']) . ' -c -z -f ' . escapeshellarg($_backupdir) . '/' . escapeshellarg($row['loginname']) . 'html.tar.gz .'); + + // get customer dbs + $dbs_result_stmt = Database::prepare(" + SELECT `databasename` FROM `" . TABLE_PANEL_DATABASES . "` + WHERE `customerid` = :customerid + "); + Database::pexecute($dbs_result_stmt, array('customerid' => $row['customerid'])); + + while ($dbs_row = $dbs_result_stmt->fetch(PDO::FETCH_ASSOC)){ + // create customers sql backup + safe_exec(escapeshellcmd($settings['system']['backup_mysqldump_path']) . ' --opt --force --allow-keywords -u ' . escapeshellarg($sql_root['user']) . ' -p' . escapeshellarg($sql_root['passwd']) . ' -h ' . $sql_root['host'] . ' -B ' . escapeshellarg($dbs_row['databasename']) . ' -r ' . escapeshellarg($_backupdir) . '/' . escapeshellarg($dbs_row['databasename']) . '.sql' ); + // compress sql backup + safe_exec('tar -C ' . escapeshellarg($_backupdir) . ' -c -z -f ' . escapeshellarg($settings['system']['backup_dir']) . $row['loginname'] . '/' . escapeshellarg($dbs_row['databasename']) . '.tar.gz ' . escapeshellarg($dbs_row['databasename']) . '.sql'); + // remove uncompresed sql files + safe_exec('rm ' . escapeshellarg($_backupdir) . '/' . escapeshellarg($dbs_row['databasename']) . '.sql'); + } + + // create 1 big file with html & db + if ($settings['system']['backup_bigfile'] == 1) { + safe_exec('tar -C ' . escapeshellarg($_backupdir) . '/' . ' --exclude=' . escapeshellarg($row['loginname']) . '.tar.gz -c -z -f ' . escapeshellarg($_backupdir) . '/' . escapeshellarg($row['loginname']) . '.tar.gz .'); + // remove separated files + $tmp_files = scandir($_backupdir); + foreach ($tmp_files as $tmp_file) { + if (preg_match('/.*(html|sql|aps).*\.tar\.gz$/', $tmp_file) && !preg_match('/^' . $row['loginname'] . '\.tar\.gz$/', $tmp_file)) { + safe_exec('rm ' . escapeshellarg($_backupdir) . '/' . escapeshellarg($tmp_file)); + } + } + } else { + //remove big file if separated backups are used + if (file_exists(makeCorrectFile($_backupdir . '/' . $row['loginname'] . '.tar.gz'))) { + safe_exec('rm ' . escapeshellarg($_backupdir) . '/' . escapeshellarg($row['loginname']) . '.tar.gz'); + } + } + + // chown & chmod files to prevent manipulation + safe_exec('chown ' . escapeshellarg($row['guid']) . ':' . escapeshellarg($row['guid']) . ' ' . escapeshellarg($_backupdir) . '/*'); + safe_exec('chmod 0400 ' . escapeshellarg($_backupdir) . '/*'); + + // create ftp backup user + $user_result_stmt = Database::prepare(" + SELECT username, password FROM `" . TABLE_FTP_USERS . "` + WHERE `customerid` = :customerid AND `username` = :username; + "); + $user_row = Database::pexecute_first($user_result_stmt, array('customerid' => $row['customerid'], 'username' => $row['loginname'])); + + $ins_stmt = Database::prepare(" + REPLACE INTO `" . TABLE_FTP_USERS . "` + (`customerid`, `username`, `password`, `homedir`, `login_enabled`, `uid`, `gid`) + VALUES + (:customerid, :username, :password, :homedir, 'y', :guid, :guid) + "); + $ins_data = array( + 'customerid' => $row['customerid'], + 'username' => $row['loginname']."_backup", + 'password' => $user_row['password'], + 'homedir' => makeCorrectDir($settings['system']['backup_dir'].'/'.$row['loginname'].'/'), + 'guid' => $row['guid'] + ); + Database::pexecute($ins_stmt, $ins_data); + + if ($settings['system']['backup_ftp_enabled'] == '1') { + // upload backup to customers ftp server + $_ftpdir = makeCorrectDir($settings['system']['backup_dir'].'/'.$row['loginname'].'/'); + $ftp_files = scandir($_ftpdir); + + foreach ($ftp_files as $ftp_file) { + if (preg_match('/.*\.tar\.gz$/', $ftp_file)) { + + $ftp_con = ftp_connect($settings['system']['backup_ftp_server']); + $ftp_login = ftp_login($ftp_con, $settings['system']['backup_ftp_user'], $settings['system']['backup_ftp_pass']); + + // Check whether to use passive mode or not + if ($settings['system']['backup_ftp_passive'] == 1) { + ftp_pasv($ftp_con, true); + } else { + ftp_pasv($ftp_con, false); + } + $_file = makeCorrectFile($_ftpdir.'/'.$ftp_file); + $ftp_upload = ftp_put($ftp_con, $ftp_file, $_file, FTP_BINARY); + } + } + } + fwrite($debugHandler, 'backup for ' . $row['loginname'] . ' finished...' . "\n"); + } + // delete old backup data (deletes backup if customer or admin disables backup) + elseif ($row['backup_allowed'] == '0' || $row['backup_enabled'] == '0') { + $_ftpdir = makeCorrectDir($settings['system']['backup_dir'].'/'.$row['loginname'].'/'); + if (file_exists($_ftpdir)){ + $files = scandir($_ftpdir); + foreach ($files as $file) { + if (preg_match('/.*\.tar\.gz$/', $file)){ + $_file = makeCorrectFile($_ftpdir.'/'.$file); + safe_exec('rm -f ' . escapeshellarg($_file)); + } + } + } } - } } - } - fwrite($debugHandler, 'backup customers finished...' . "\n"); + fwrite($debugHandler, 'backup customers finished...' . "\n"); } - -?> diff --git a/scripts/jobs/cron_lighttp.htaccess.php b/scripts/jobs/cron_lighttp.htaccess.php deleted file mode 100644 index eecfcc19..00000000 --- a/scripts/jobs/cron_lighttp.htaccess.php +++ /dev/null @@ -1,161 +0,0 @@ - (2003-2009) - * @author Froxlor team (2010-) - * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt - * @package Cron - * - */ - -/** - * LOOK INTO EVERY CUSTOMER DIR TO SEE IF THERE ARE ANY .HTACCESS FILE TO "TRANSLATE" - */ - -if($settings['system']['webserver'] == 'lighttpd') -{ - fwrite($debugHandler, ' cron_lighttp.htaccess: Searching for .htaccess files to translate' . "\n"); - $lpath = makeCorrectDir(strrchr($settings['system']['apacheconf_vhost'], '/')); - $htaccessfh = @fopen($lpath . 'syscp-htaccess.conf', 'w'); - - if($htaccessfh !== false) - { - read_directory($settings['system']['documentroot_prefix'], 25, $htaccessfh); - } - else - { - fwrite($debugHandler, ' ERROR: Cannot open file ' . $lpath . 'syscp-htaccess.conf' . "\n"); - } -} -else -{ - fwrite($debugHandler, ' cron_lighttp.htaccess: You don\'t use Lighttpd, you do not have to run this cronscript!' . "\n"); -} - -/** - * FUNCTIONS - */ - -function read_directory($dir1 = null, $min_depth = 25, $htaccessfh = null) -{ - global $htaccessfh, $theme; - - if(!is_string($dir1)) - { - return false; - } - - $depth = explode("/", $dir1); - $current_depth = sizeof($depth); - - if($current_depth < $min_depth) - { - $min_depth = $current_depth; - } - - $dir = $dir1; - $dh = opendir($dir); - - while($file = readdir($dh)) - { - if(($file != ".") - && ($file != "..")) - { - $file = $dir . "/" . $file; - for ($i = 0;$i <= ($current_depth - $min_depth);$i++) - - // $file is sub-directory - - if($ddh = @opendir($file)) - { - read_directory($file); - } - else - { - if(strtolower($file) == '.htaccess') - { - parseHtaccess($file); - } - } - } - } - - closedir($dh); - return true; -} - -function parseHtaccess($file = null) -{ - global $debugHandler, $htaccessfh, $theme; - $htacc = @file_get_contents($file); - - if($htacc != "") - { - $htlines = array(); - $htlines = explode("\n", $htacc); - $userhasrewrites = false; - $userrewrites = array(); - $rule = array(); - foreach($htlines as $htl) - { - if(preg_match('/^RewriteEngine\ on$/si', $htl) !== null) - { - $userhasrewrites = true; - } - elseif(preg_match('/^RewriteRule\ +\^(.*)\$\(.*)$/si', $htl, $rule) !== null) - { - $regex = isset($rule[0]) ? $rule[0] : ''; - $relativeuri = isset($rule[1]) ? $rule[1] : ''; - - if($regex != '' - && $relativeuri != '') - { - $userrewrites[]['regex'] = $regex; - $userrewrites[]['relativeuri'] = $relativeuri; - } - } - } - - if($userhasrewrites) - { - fwrite($htaccessfh, '$PHYSICAL["path"] == "' . dirname($file) . '" {' . "\n"); - fwrite($htaccessfh, ' url.rewrite-once = (' . "\n"); - $count = 1; - $max = count($userrewrites); - foreach($userrewrites as $usrrw) - { - fwrite($htaccessfh, ' "^' . $usrrw['regex'] . '$" => "' . $usrrw['relativeuri'] . '"'); - - if($count < $max) - { - fwrite($htaccessfh, ',' . "\n"); - } - else - { - fwrite($htaccessfh, "\n"); - } - - $count++; - } - - fwrite($htaccessfh, ' )' . "\n"); - fwrite($htaccessfh, '}' . "\n"); - } - } - else - { - fwrite($debugHandler, ' WARNING: file ' . $file . ' seems to be empty or there was an error' . "\n"); - return; - } -} - -?>