added functions to get sql-data to Database-class, migrated backup-cron to new PDO database class, refs #1287
Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
@@ -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.":";
|
||||
|
||||
@@ -15,109 +15,140 @@
|
||||
*
|
||||
*/
|
||||
|
||||
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");
|
||||
|
||||
$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)){
|
||||
// get sql-root access data for mysqldump
|
||||
Database::needRoot(true);
|
||||
Database::needSqlData(true);
|
||||
$sql_root = Database::getSqlData();
|
||||
Database::needRoot(false);
|
||||
|
||||
$result_stmt = Database::query("
|
||||
SELECT customerid, loginname, guid, documentroot, backup_allowed, backup_enabled
|
||||
FROM `" . TABLE_PANEL_CUSTOMERS . "` ORDER BY `customerid` ASC;
|
||||
");
|
||||
|
||||
while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
|
||||
fwrite($debugHandler, 'backup for ' . $row['loginname'] . ' started...' . "\n");
|
||||
|
||||
// backup
|
||||
if($row['backup_allowed'] == '1' && $row['backup_enabled'] == '1'){
|
||||
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);
|
||||
$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']));
|
||||
|
||||
// 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');
|
||||
$_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');
|
||||
}
|
||||
|
||||
// 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 .');
|
||||
safe_exec('tar -C ' . escapeshellarg($row['documentroot']) . ' -c -z -f ' . escapeshellarg($_backupdir) . '/' . escapeshellarg($row['loginname']) . 'html.tar.gz .');
|
||||
|
||||
// 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)){
|
||||
$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[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' );
|
||||
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($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');
|
||||
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($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/' . escapeshellarg($dbs_row['databasename']) . '.sql');
|
||||
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($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 .');
|
||||
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($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) . '');
|
||||
$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 {
|
||||
} 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');
|
||||
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($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/*');
|
||||
safe_exec('chmod 0400 ' . escapeshellarg($settings['system']['backup_dir']) . escapeshellarg($row['loginname']) . '/*');
|
||||
safe_exec('chown ' . escapeshellarg($row['guid']) . ':' . escapeshellarg($row['guid']) . ' ' . escapeshellarg($_backupdir) . '/*');
|
||||
safe_exec('chmod 0400 ' . escapeshellarg($_backupdir) . '/*');
|
||||
|
||||
// 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']) . "')");
|
||||
$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']));
|
||||
|
||||
if($settings['system']['backup_ftp_enabled'] == '1'){
|
||||
$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
|
||||
$ftp_files = scandir($settings['system']['backup_dir'] . $row['loginname']);
|
||||
foreach ($ftp_files as $ftp_file){
|
||||
if(preg_match('/.*\.tar\.gz$/', $ftp_file)){
|
||||
$_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)
|
||||
{
|
||||
// Check whether to use passive mode or not
|
||||
if ($settings['system']['backup_ftp_passive'] == 1) {
|
||||
ftp_pasv($ftp_con, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ftp_pasv($ftp_con, false);
|
||||
}
|
||||
|
||||
$ftp_upload = ftp_put($ftp_con, $ftp_file, $settings['system']['backup_dir'] . $row['loginname'] . "/" . $ftp_file, FTP_BINARY);
|
||||
$_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'){
|
||||
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) . '');
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,5 +156,3 @@ if($settings['system']['backup_enabled'] == '1'){
|
||||
}
|
||||
fwrite($debugHandler, 'backup customers finished...' . "\n");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -1,161 +0,0 @@
|
||||
<?php if (!defined('MASTER_CRONJOB')) die('You cannot access this file directly!');
|
||||
|
||||
/**
|
||||
* 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 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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user