Ejemplo n.º 1
0
function rotateLogs()
{
    // --------------
    // Rotate the tables
    // net2ftp_log_access          = active table
    // net2ftp_log_access_YYYYMM   = archive table with information of month MM and year YYYY
    // net2ftp_log_access_template = template table (empty table)
    //
    // To avoid that the log rotation actions would be executed multiple times at
    // the end of the period, a "log rotation status" is used:
    // 0 = no rotation needed
    // 1 = step 1 not yet started (renaming active tables to archived tables)
    // 2 = step 1 in progress
    // 3 = step 2 not yet started (copying template tables to the active tables)
    // 4 = step 2 in progress
    // 5 = step 3 not yet started (dropping oldest archive tables)
    // 6 = step 3 in progress
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_globals, $net2ftp_settings, $net2ftp_result, $net2ftp_output;
    $toreturn = true;
    // -------------------------------------------------------------------------
    // Verify if a database is used. If not: don't continue.
    // -------------------------------------------------------------------------
    if ($net2ftp_settings["use_database"] != "yes") {
        return true;
    }
    // -------------------------------------------------------------------------
    // Check if the setting is within the allowed range; if not, set it to 12 months
    // -------------------------------------------------------------------------
    if (!($net2ftp_settings["log_length_months"] > 1 && $net2ftp_settings["log_length_months"] < 99)) {
        $net2ftp_settings["log_length_months"] = 12;
    }
    // -------------------------------------------------------------------------
    // Current month, next month, previous month
    // -------------------------------------------------------------------------
    $currentmonth = date("Ym");
    // e.g. 201207
    $lastmonth = date("Ym", mktime(0, 0, 0, date("m") - 1, date("d"), date("Y")));
    $nextmonth = date("Ym", mktime(0, 0, 0, date("m") + 1, date("d"), date("Y")));
    $dropmonth = date("Ym", mktime(0, 0, 0, date("m") - $net2ftp_settings["log_length_months"] - 1, date("d"), date("Y")));
    // -------------------------------------------------------------------------
    // Connect to the database
    // -------------------------------------------------------------------------
    $mydb = connect2db();
    if ($net2ftp_result["success"] == false) {
        return false;
    }
    // -------------------------------------------------------------------------
    // Get the log rotation status
    // -------------------------------------------------------------------------
    $logStatus = getLogStatus();
    if ($net2ftp_result["success"] == false) {
        return false;
    }
    // No log rotation needed
    if ($logStatus === 0) {
        return true;
    }
    // -------------------------------------------------------------------------
    // Table names and SQL queries to create the tables
    // -------------------------------------------------------------------------
    $tables[1]["name"] = "net2ftp_log_access";
    $tables[2]["name"] = "net2ftp_log_error";
    $tables[3]["name"] = "net2ftp_log_consumption_ftpserver";
    $tables[4]["name"] = "net2ftp_log_consumption_ipaddress";
    // -------------------------------------------------------------------------
    // step 1 of rotation: rename active tables to archived tables
    // -------------------------------------------------------------------------
    if ($logStatus == 1) {
        // Set the log status to indicate this step is in progress
        putLogStatus(2);
        if ($net2ftp_result["success"] == false) {
            return false;
        }
        // Execute the step
        for ($i = 1; $i <= sizeof($tables); $i++) {
            $table = $tables[$i]["name"];
            // Example: net2ftp_log_access
            $table_archive = $table . "_" . $lastmonth;
            // Example: net2ftp_log_access_201206
            $table_archive_drop = $table . "_" . $dropmonth;
            // Example: net2ftp_log_access_201106
            $sqlquery_rename = "RENAME TABLE {$table} TO {$table_archive}";
            $result_rename[$i] = mysql_query("{$sqlquery_rename}");
            if ($result_rename[$i] == true) {
                $net2ftp_output["rotateLogs"][] = __("The log tables were renamed successfully.");
            } else {
                $toreturn = false;
                $net2ftp_output["rotateLogs"][] = __("The log tables could not be renamed.");
            }
        }
        // end for
        // Set the log status to indicate this step is in done and the next can start
        putLogStatus(3);
        if ($net2ftp_result["success"] == false) {
            return false;
        }
    } elseif ($logStatus == 3) {
        // Set the log status to indicate this step is in progress
        putLogStatus(4);
        if ($net2ftp_result["success"] == false) {
            return false;
        }
        // Execute the step
        for ($i = 1; $i <= sizeof($tables); $i++) {
            $table = $tables[$i]["name"];
            // Example: net2ftp_log_access
            $table_archive = $table . "_" . $lastmonth;
            // Example: net2ftp_log_access_201206
            $table_archive_drop = $table . "_" . $dropmonth;
            // Example: net2ftp_log_access_201106
            $sqlquery_copy = "CREATE TABLE {$table} LIKE {$table_archive}";
            $result_copy[$i] = mysql_query("{$sqlquery_copy}");
            if ($result_copy[$i] == true) {
                $net2ftp_output["rotateLogs"][] = __("The log tables were copied successfully.");
            } else {
                $toreturn = false;
                $net2ftp_output["rotateLogs"][] = __("The log tables could not be copied.");
            }
        }
        // end for
        // Set the log status to indicate this step is in done and the next can start
        putLogStatus(5);
        if ($net2ftp_result["success"] == false) {
            return false;
        }
    } elseif ($logStatus == 5) {
        // Set the log status to indicate this step is in progress
        putLogStatus(6);
        if ($net2ftp_result["success"] == false) {
            return false;
        }
        // Execute the step
        for ($i = 1; $i <= sizeof($tables); $i++) {
            $table = $tables[$i]["name"];
            // Example: net2ftp_log_access
            $table_archive = $table . "_" . $lastmonth;
            // Example: net2ftp_log_access_201206
            $table_archive_drop = $table . "_" . $dropmonth;
            // Example: net2ftp_log_access_201106
            $sqlquery_drop = "DROP TABLE IF EXISTS {$table_archive_drop};";
            $result_drop[$i] = mysql_query("{$sqlquery_drop}");
            if ($result_drop[$i] == true) {
                $net2ftp_output["rotateLogs"][] = __("The oldest log table was dropped successfully.");
            } else {
                $toreturn = false;
                $net2ftp_output["rotateLogs"][] = __("The oldest log table could not be dropped.");
            }
        }
        // end for
        // Set the log status to indicate this step is in done and the rotation is over
        putLogStatus(0);
        if ($net2ftp_result["success"] == false) {
            return false;
        }
    }
    return $toreturn;
}
Ejemplo n.º 2
0
function putConsumption()
{
    // --------------
    // This function writes the consumption to the database.
    // It is run at the end of the script.
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
    // -------------------------------------------------------------------------
    // Initial checks
    // -------------------------------------------------------------------------
    // Verify if a database is used, and if consumption checking is turned on. If not: don't continue.
    if ($net2ftp_settings["use_database"] != "yes" || $net2ftp_settings["check_consumption"] != "yes") {
        return true;
    }
    // When user is not logged in, the FTP server is not set
    if ($net2ftp_globals["ftpserver"] == "") {
        return true;
    }
    // If the REMOTE_ADDR is not filled in, then there is a problem (IP spoofing), so return an error
    if ($net2ftp_globals["REMOTE_ADDR"] == "") {
        setErrorVars(false, __("Unable to determine your IP address."), debug_backtrace(), __FILE__, __LINE__);
        return false;
    }
    // If the database has already been updated, don't do it a second time.
    // This is to avoid updating the database twice. The putConsumption() function
    // is called from index.php and from shutdown() in filesystem.inc.php. On Windows
    // the shutdown() function is called after *every* script execution.
    if ($net2ftp_globals["consumption_database_updated"] == 1) {
        return true;
    }
    // Add slashes to variables which are used in a SQL query, and which are
    // potentially unsafe (supplied by the user).
    // $date is calculated in this function
    // $time is calculated in this function
    $REMOTE_ADDR_safe = addslashes($net2ftp_globals["REMOTE_ADDR"]);
    $net2ftp_ftpserver_safe = addslashes($net2ftp_globals["ftpserver"]);
    // ----------------------------------------------
    // Do not log accesses, errors and consumption while the logs are being rotated
    // ----------------------------------------------
    $logStatus = getLogStatus();
    if ($net2ftp_result["success"] == false) {
        return false;
    }
    if ($logStatus != 0) {
        return true;
    }
    // -------------------------------------------------------------------------
    // Check the input
    // -------------------------------------------------------------------------
    //	if (preg_match("/^[0-9]+$/", $net2ftp_globals["consumption_ipaddress_datatransfer) == FALSE) {
    //			setErrorVars(false, __("The variable <b>consumption_ipaddress_datatransfer</b> is not numeric."), debug_backtrace(), __FILE__, __LINE__);
    //			return false;
    //	}
    // -------------------------------------------------------------------------
    // Connect
    // -------------------------------------------------------------------------
    $mydb = connect2db();
    if ($net2ftp_result["success"] == false) {
        return false;
    }
    // -------------------------------------------------------------------------
    // Get date
    // -------------------------------------------------------------------------
    $date = date("Y-m-d");
    // -------------------------------------------------------------------------
    // Put consumed data volume and execution time by the current IP address
    // -------------------------------------------------------------------------
    $sqlquery1 = "SELECT * FROM net2ftp_log_consumption_ipaddress WHERE date = '{$date}' AND ipaddress = '{$REMOTE_ADDR_safe}';";
    $result1 = mysql_query("{$sqlquery1}");
    $nrofrows1 = mysql_num_rows($result1);
    if ($nrofrows1 == 1) {
        $sqlquery2 = "UPDATE net2ftp_log_consumption_ipaddress SET datatransfer = '" . $net2ftp_globals["consumption_ipaddress_datatransfer"] . "', executiontime = '" . round($net2ftp_globals["consumption_ipaddress_executiontime"]) . "' WHERE date = '{$date}' AND ipaddress = '{$REMOTE_ADDR_safe}';";
        $result2 = mysql_query("{$sqlquery2}");
        $nrofrows2 = mysql_affected_rows($mydb);
        // Don't check on the UPDATE nr of rows, because when the values in the variables and in the table are the same,
        // the $nrofrows2 is set to 0. (This happens on the Browse screen, when the loading is fast: the datatransfer is 0
        // and the executiontime is the same as in the table.)
        //		if ($nrofrows2 != 1) {
        //			setErrorVars(false, __("Table net2ftp_log_consumption_ipaddress could not be updated."), debug_backtrace(), __FILE__, __LINE__);
        //			return false;
        //		}
    } elseif ($nrofrows1 == 0) {
        $sqlquery3 = "INSERT INTO net2ftp_log_consumption_ipaddress VALUES('{$date}', '{$REMOTE_ADDR_safe}', '" . $net2ftp_globals["consumption_ipaddress_datatransfer"] . "', '" . round($net2ftp_globals["consumption_ipaddress_executiontime"]) . "');";
        $result3 = mysql_query("{$sqlquery3}");
        $nrofrows3 = mysql_affected_rows($mydb);
        if ($nrofrows3 != 1) {
            setErrorVars(false, __("Table net2ftp_log_consumption_ipaddress could not be updated."), debug_backtrace(), __FILE__, __LINE__);
            return false;
        }
    } else {
        setErrorVars(false, __("Table net2ftp_log_consumption_ipaddress contains duplicate entries."), debug_backtrace(), __FILE__, __LINE__);
        return false;
    }
    // MySQL > 4.1.0
    //	$sqlquery1 = "INSERT INTO net2ftp_log_consumption_ipaddress VALUES('$date', '$REMOTE_ADDR_safe', '" . $net2ftp_globals["consumption_ipaddress_datatransfer"] . "', '" . round($net2ftp_globals["consumption_ipaddress_executiontime"])  . "') ON DUPLICATE KEY UPDATE datatransfer = '" . $net2ftp_globals["consumption_ipaddress_datatransfer"] . "', executiontime = '" . round($net2ftp_globals["consumption_ipaddress_executiontime"]) . "';";
    // -------------------------------------------------------------------------
    // Put consumed data volume and execution time to the current FTP server
    // -------------------------------------------------------------------------
    $sqlquery4 = "SELECT * FROM net2ftp_log_consumption_ftpserver WHERE date = '{$date}' AND ftpserver = '{$net2ftp_ftpserver_safe}';";
    $result4 = mysql_query("{$sqlquery4}");
    $nrofrows4 = mysql_num_rows($result4);
    if ($nrofrows4 == 1) {
        $sqlquery5 = "UPDATE net2ftp_log_consumption_ftpserver SET datatransfer = '" . $net2ftp_globals["consumption_ftpserver_datatransfer"] . "', executiontime = '" . round($net2ftp_globals["consumption_ftpserver_executiontime"]) . "' WHERE date = '{$date}' AND ftpserver = '{$net2ftp_ftpserver_safe}';";
        $result5 = mysql_query("{$sqlquery5}");
        $nrofrows5 = mysql_affected_rows($mydb);
        // Don't check on the UPDATE nr of rows, because when the values in the variables and in the table are the same,
        // the $nrofrows2 is set to 0. (This happens on the Browse screen, when the loading is fast: the datatransfer is 0
        // and the executiontime is the same as in the table.)
        //		if ($nrofrows5 != 1) {
        //			setErrorVars(false, __("Table net2ftp_log_consumption_ftpserver could not be updated."), debug_backtrace(), __FILE__, __LINE__);
        //			return false;
        //		}
    } elseif ($nrofrows4 == 0) {
        $sqlquery6 = "INSERT INTO net2ftp_log_consumption_ftpserver VALUES('{$date}', '{$net2ftp_ftpserver_safe}', '" . $net2ftp_globals["consumption_ftpserver_datatransfer"] . "', '" . round($net2ftp_globals["consumption_ftpserver_executiontime"]) . "');";
        $result6 = mysql_query("{$sqlquery6}");
        $nrofrows6 = mysql_affected_rows($mydb);
        if ($nrofrows6 != 1) {
            setErrorVars(false, __("Table net2ftp_log_consumption_ftpserver could not be updated."), debug_backtrace(), __FILE__, __LINE__);
            return false;
        }
    } else {
        setErrorVars(false, __("Table net2ftp_log_consumption_ftpserver contains duplicate entries."), debug_backtrace(), __FILE__, __LINE__);
        return false;
    }
    // -------------------------------------------------------------------------
    // Update the net2ftp_log_access record with the consumed data volume and execution time
    // -------------------------------------------------------------------------
    $sqlquery7 = "SELECT * FROM net2ftp_log_access WHERE id = '" . $net2ftp_globals["log_access_id"] . "';";
    $result7 = mysql_query("{$sqlquery7}");
    $nrofrows7 = mysql_num_rows($result7);
    if ($nrofrows7 == 1) {
        $sqlquery8 = "UPDATE net2ftp_log_access SET datatransfer = '" . $net2ftp_globals["consumption_datatransfer"] . "', executiontime = '" . round($net2ftp_globals["consumption_executiontime"]) . "' WHERE id = '" . $net2ftp_globals["log_access_id"] . "'";
        $result8 = mysql_query("{$sqlquery8}");
        $nrofrows8 = mysql_affected_rows($mydb);
        // Don't check on the UPDATE nr of rows, because when the values in the variables and in the table are the same,
        // the $nrofrows2 is set to 0. (This happens on the Browse screen, when the loading is fast: the datatransfer is 0
        // and the executiontime is the same as in the table.)
        //		if ($nrofrows8 != 1) {
        //			setErrorVars(false, __("Table net2ftp_log_access could not be updated."), debug_backtrace(), __FILE__, __LINE__);
        //			return false;
        //		}
    } elseif ($nrofrows7 == 0) {
        $sqlquery9 = "INSERT INTO net2ftp_log_access VALUES('{$date}', '{$REMOTE_ADDR_safe}', '" . $net2ftp_globals["consumption_ipaddress_datatransfer"] . "', '" . round($net2ftp_globals["consumption_ipaddress_executiontime"]) . "');";
        $result9 = mysql_query("{$sqlquery9}");
        $nrofrows9 = mysql_affected_rows($mydb);
        if ($nrofrows9 != 1) {
            setErrorVars(false, __("Table net2ftp_log_access could not be updated."), debug_backtrace(), __FILE__, __LINE__);
            return false;
        }
    } else {
        setErrorVars(false, __("Table net2ftp_log_access contains duplicate entries."), debug_backtrace(), __FILE__, __LINE__);
        return false;
    }
    // -------------------------------------------------------------------------
    // If all 3 tables have been updated, set the flag to 1
    // -------------------------------------------------------------------------
    $net2ftp_globals["consumption_database_updated"] = 1;
    // Return true
    return true;
}