/**
 * This file is part of the Froxlor project.
 * Copyright (c) 2011- 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 <*****@*****.**> (2011-)
 * @license    GPLv2 http://files.froxlor.org/misc/COPYING.txt
 * @package    Functions
 *
 */
function getFilesystemQuota()
{
    // enabled at all?
    if (Settings::Get('system.diskquota_enabled')) {
        // set linux defaults
        $repquota_params = "-np";
        //$quota_line_regex = "/^#([0-9]+)\s*[+-]{2}\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)/i";
        $quota_line_regex = "/^#([0-9]+)\\s+[+-]{2}\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)/i";
        // check for freebsd - which needs other values
        if (isFreeBSD()) {
            $repquota_params = "-nu";
            $quota_line_regex = "/^([0-9]+)\\s+[+-]{2}\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\S+)/i";
        }
        // Fetch all quota in the desired partition
        exec(Settings::Get('system.diskquota_repquota_path') . " " . $repquota_params . " " . escapeshellarg(Settings::Get('system.diskquota_customer_partition')), $repquota);
        $usedquota = array();
        foreach ($repquota as $tmpquota) {
            // Let's see if the line matches a quota - line
            if (preg_match($quota_line_regex, $tmpquota, $matches)) {
                // It matches - put it into an array with userid as key (for easy lookup later)
                $usedquota[$matches[1]] = array('block' => array('used' => $matches[2], 'soft' => $matches[3], 'hard' => $matches[4], 'grace' => isFreeBSD() ? '0' : $matches[5]), 'file' => array('used' => $matches[6], 'soft' => $matches[7], 'hard' => $matches[8], 'grace' => isFreeBSD() ? '0' : $matches[9]));
            }
        }
        return $usedquota;
    }
    return false;
}
/**
 * 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 ');
    }
}
Exemple #3
0
function getInterfaceSentBytes($interface)
{
    if (isLinux()) {
        $filepath = '/sys/class/net/%s/statistics/tx_bytes';
        $output = file_get_contents(sprintf($filepath, $interface));
        return $output;
    }
    if (isFreeBSD() || isOSX()) {
        $command = "%s -ibn| grep %s | grep Link | awk '{print \$10}'";
        exec(sprintf($command, PATH_NETSTAT, $interface), $output);
        return array_shift($output);
    }
    throw new Exception('Unable to guess OS');
}
/**
 * 1st: check for task of generation
 * 2nd: if task found, generate cron.d-file
 * 3rd: maybe restart cron?
 */
function checkCrondConfigurationFile()
{
    // check for task
    $result_tasks_stmt = Database::query("\n\t\t\tSELECT * FROM `" . TABLE_PANEL_TASKS . "` WHERE `type` = '99'\n\t\t\t");
    $num_results = Database::num_rows();
    // is there a task for re-generating the cron.d-file?
    if ($num_results > 0) {
        // get all crons and their intervals
        if (isFreeBSD()) {
            // FreeBSD does not need a header as we are writing directly to the crontab
            $cronfile = "\n";
        } else {
            $cronfile = "# automatically generated cron-configuration by froxlor\n";
            $cronfile .= "# do not manually edit this file as it will be re-generated periodically.\n";
            $cronfile .= "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n#\n";
        }
        // get all the crons
        $result_stmt = Database::query("\n\t\t\t\tSELECT * FROM `" . TABLE_PANEL_CRONRUNS . "` WHERE `isactive` = '1'\n\t\t\t\t");
        $hour_delay = 0;
        $day_delay = 5;
        $month_delay = 7;
        while ($row_cronentry = $result_stmt->fetch(PDO::FETCH_ASSOC)) {
            // create cron.d-entry
            if (preg_match("/(\\d+) (MINUTE|HOUR|DAY|WEEK|MONTH)/", $row_cronentry['interval'], $matches)) {
                if ($matches[1] == 1) {
                    $minvalue = "*";
                } else {
                    $minvalue = "*/" . $matches[1];
                }
                switch ($matches[2]) {
                    case "MINUTE":
                        $cronfile .= $minvalue . " * * * * ";
                        break;
                    case "HOUR":
                        $cronfile .= $hour_delay . " " . $minvalue . " * * * ";
                        $hour_delay += 3;
                        break;
                    case "DAY":
                        if ($row_cronentry['cronfile'] == 'traffic') {
                            // traffic at exactly 0:00 o'clock
                            $cronfile .= "0 0 " . $minvalue . " * * ";
                        } else {
                            $cronfile .= $day_delay . " 0 " . $minvalue . " * * ";
                            $day_delay += 5;
                        }
                        break;
                    case "MONTH":
                        $cronfile .= $month_delay . " 0 1 " . $minvalue . " * ";
                        $month_delay += 7;
                        break;
                    case "WEEK":
                        $cronfile .= $day_delay . " 0 " . $matches[1] * 7 . " * * ";
                        $day_delay += 5;
                        break;
                }
                // create entry-line
                $binpath = Settings::Get("system.croncmdline");
                // fallback as it is important
                if ($binpath === null) {
                    $binpath = "/usr/bin/nice -n 5 /usr/bin/php5 -q";
                }
                $cronfile .= "root " . $binpath . " " . FROXLOR_INSTALL_DIR . "/scripts/froxlor_master_cronjob.php --" . $row_cronentry['cronfile'] . " 1> /dev/null\n";
            }
        }
        if (isFreeBSD()) {
            // FreeBSD handles the cron-stuff in another way. We need to directly
            // write to the crontab file as there is not cron.d/froxlor file
            // (settings for system.cronconfig should be set correctly of course)
            $crontab = file_get_contents(Settings::Get("system.cronconfig"));
            if ($crontab === false) {
                die("Oh snap, we cannot read the crontab file. This should not happen.\nPlease check the path and permissions, the cron will keep trying if you don't stop the cron-service.\n\n");
            }
            // now parse out / replace our entries
            $crontablines = explode("\n", $crontab);
            $newcrontab = "";
            foreach ($crontablines as $ctl) {
                $ctl = trim($ctl);
                if (!empty($ctl) && !preg_match("/(.*)froxlor_master_cronjob\\.php(.*)/", $ctl)) {
                    $newcrontab .= $ctl . "\n";
                }
            }
            // re-assemble old-content + new froxlor-content
            $newcrontab .= $cronfile;
            // now continue with writing the file
            $cronfile = $newcrontab;
        }
        // write the file
        if (file_put_contents(Settings::Get("system.cronconfig"), $cronfile) === false) {
            // oh snap cannot create new crond-file
            die("Oh snap, we cannot create the cron-file. This should not happen.\nPlease check the path and permissions, the cron will keep trying if you don't stop the cron-service.\n\n");
        }
        // correct permissions
        chmod(Settings::Get("system.cronconfig"), 0640);
        // remove all re-generation tasks
        Database::query("DELETE FROM `" . TABLE_PANEL_TASKS . "` WHERE `type` = '99'");
        // run reload command
        safe_exec(escapeshellcmd(Settings::Get('system.crondreload')));
    }
    return true;
}
Exemple #5
0
            // Select all customers Froxlor knows about
            $result_stmt = Database::query("SELECT `guid`, `loginname`, `diskspace` FROM `" . TABLE_PANEL_CUSTOMERS . "`;");
            while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) {
                // We do not want to set a quota for root by accident
                if ($row['guid'] != 0) {
                    // The user has no quota in Froxlor, but on the filesystem
                    if (($row['diskspace'] == 0 || $row['diskspace'] == -1024) && $usedquota[$row['guid']]['block']['hard'] != 0) {
                        $cronlog->logAction(CRON_ACTION, LOG_NOTICE, "Disabling quota for " . $row['loginname']);
                        if (isFreeBSD()) {
                            safe_exec(Settings::Get('system.diskquota_quotatool_path') . " -e " . escapeshellarg(Settings::Get('system.diskquota_customer_partition')) . ":0:0 " . $row['guid']);
                        } else {
                            safe_exec(Settings::Get('system.diskquota_quotatool_path') . " -u " . $row['guid'] . " -bl 0 -q 0 " . escapeshellarg(Settings::Get('system.diskquota_customer_partition')));
                        }
                    } elseif ($row['diskspace'] != $usedquota[$row['guid']]['block']['hard'] && $row['diskspace'] != -1024) {
                        $cronlog->logAction(CRON_ACTION, LOG_NOTICE, "Setting quota for " . $row['loginname'] . " from " . $usedquota[$row['guid']]['block']['hard'] . " to " . $row['diskspace']);
                        if (isFreeBSD()) {
                            safe_exec(Settings::Get('system.diskquota_quotatool_path') . " -e " . escapeshellarg(Settings::Get('system.diskquota_customer_partition')) . ":" . $row['diskspace'] . ":" . $row['diskspace'] . " " . $row['guid']);
                        } else {
                            safe_exec(Settings::Get('system.diskquota_quotatool_path') . " -u " . $row['guid'] . " -bl " . $row['diskspace'] . " -q " . $row['diskspace'] . " " . escapeshellarg(Settings::Get('system.diskquota_customer_partition')));
                        }
                    }
                }
            }
        }
    }
}
if ($num_results != 0) {
    $where = array();
    $where_data = array();
    foreach ($resultIDs as $id) {
        $where[] = "`id` = :id_" . (int) $id;