Commit 5529d901 by Rowedahelicon

2.0.0

Initial commit
parent 40e8d8f4
#include <sourcemod>
#include <cURL>
#pragma semicolon 1
#define PLUGIN_VERSION "2.0.0"
new Handle:v_Enable = INVALID_HANDLE;
new Handle:v_Quota = INVALID_HANDLE;
new Handle:v_Days = INVALID_HANDLE;
new Handle:v_PathToPHP = INVALID_HANDLE;
new Handle:v_PathToReplays = INVALID_HANDLE;
new Handle:v_Log = INVALID_HANDLE;
new Handle:v_AutoPrune = INVALID_HANDLE;
new Handle:v_SecretKey = INVALID_HANDLE;
new bool:g_TimerRunning = false;
public Plugin:myinfo =
{
name = "[TF2] HTTP Replay Cleaner Redux",
author = "DarthNinja / Rowedahelicon",
description = "Delete old replay files when you exceed a set limit",
version = PLUGIN_VERSION,
url = "DarthNinja.com / Rowedahelicon.com"
};
public OnPluginStart()
{
//Cvars
CreateConVar("sm_replaycleanup_version", PLUGIN_VERSION, "Plugin Version", FCVAR_PLUGIN|FCVAR_NOTIFY|FCVAR_REPLICATED|FCVAR_SPONLY);
v_Enable = CreateConVar("sm_replaycleanup_enable", "1", "Enable/Disable the plugin", 0, true, 0.0, true, 1.0);
v_Quota = CreateConVar("sm_replaycleanup_cachesize", "1024", "The plugin will only begin pruning files after the folder exceeds this size. Sizes are in MB.");
v_Days = CreateConVar("sm_replaycleanup_days", "4", "When the folder exceeds the cache size cvar, files older then this many days will be deleted.");
v_PathToPHP = CreateConVar("sm_replaycleanup_link", "http://www.yoursite.com/tf2/replays/file.php", "The http link to open the php file.");
v_PathToReplays = CreateConVar("sm_replaycleanup_localpath", "tf/2/", "Path to the replay files from where the php script is located.");
v_Log = CreateConVar("sm_replaycleanup_save_logfile", "0", "Set to 1 to save a log on the webserver with a list of the deleted files", 0, true, 0.0, true, 1.0);
v_AutoPrune = CreateConVar("sm_replaycleanup_autoprune", "0", "Set this to a non-zero value to auto run a cleaning every X hours in addition to at round ends", 0, true, 0.0);
v_SecretKey = CreateConVar("sm_replaycleanup_secretkey", "", "Your secret key to keep people from posting to your page.");
LoadTranslations("common.phrases");
AutoExecConfig(true, "HTTPReplayCleanup");
HookEvent("teamplay_round_win", Cleanup, EventHookMode_Post);
}
public OnConfigsExecuted()
{
new Float:fTime = GetConVarFloat(v_AutoPrune);
if (fTime > 0.0 && !g_TimerRunning)
{
g_TimerRunning = true;
CreateTimer((fTime * 60.0 * 60.0 ), Timer_CleanUp, INVALID_HANDLE, TIMER_REPEAT);
}
}
public Action:Timer_CleanUp(Handle:timer, any:userid)
{
Cleanup2();
return Plugin_Continue;
}
public Cleanup(Handle:event, const String:name[], bool:dontBroadcast)
{
if (GetConVarBool(v_Enable))
{
Cleanup2();
}
}
Cleanup2()
{
decl String:sLink[100];
decl String:sPath[25];
decl String:sKey[25];
GetConVarString(v_PathToPHP, sLink, sizeof(sLink));
GetConVarString(v_PathToReplays, sPath, sizeof(sPath));
GetConVarString(v_SecretKey, sKey, sizeof(sKey));
new String:query[PLATFORM_MAX_PATH];
Format(query, sizeof(query), "max_size=%i&age_days=%i&path=%s&log=%i&key=%s", GetConVarInt(v_Quota), GetConVarInt(v_Days), sPath, GetConVarInt(v_Log), sKey);
new Handle:curl = curl_easy_init();
curl_easy_setopt_string(curl, CURLOPT_URL, sLink);
curl_easy_setopt_int(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt_int(curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_easy_setopt_string(curl, CURLOPT_POSTFIELDS, query);
curl_easy_perform_thread(curl, OnComplete);
}
public OnComplete(Handle:hndl, CURLcode: code)
{
CloseHandle(hndl);
}
\ No newline at end of file
<?php
$debug = false; // Print outf debugging text on page
//EDIT THIS ONLY
$secret_key = "";
//DO NOT EDIT ANYTHING ELSE
//Turn on errors
if ($debug)
{
error_reporting(E_ALL);
ini_set("display_errors", 1);
}
//Make sure the file isn't being accessed at random with no data passed
if (!isset($_POST["max_size"]) || !isset($_POST["age_days"]) || !isset($_POST["path"]) || !isset($_POST["log"]) || !isset($_POST["key"]))
{
die("Error: Inputs not specified!");
}
if ($_POST[key] != $secret_key)
{
die("Error : Secret key not correct!");
}
$maxMB = $_POST["max_size"]; //files will be deleted if folder exceeds this size in MB
$deleteAge = $_POST["age_days"]; //Files older then this will be deleted
$path = $_POST["path"]; // Local path to the replay files
$log = false;
if ($_POST["log"] == 1)
{
$log = true; //Whether or not to save a log file
//$logFile = "CleaningSessions.log";
$logFileHandle = fopen("CleaningSessions.log", "a");
//fwrite($logFileHandle, "SESSION IP : $_SERVER[REMOTE_ADDR] COMPLETED. \n"); //Logs the incoming IP, this could be used as added security to only allow access to certain people. I may include it later.
if ($_POST[key] != $secret_key)
{
write($logFileHandle, "ERROR : Secret key not correct! \n");
}
}
function dirSize($directory)
{
$size = 0;
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file)
{
$size+=$file->getSize();
}
return $size;
}
$filesize = dirSize("$path"); //Bytes
$filesizeKB = $filesize/1024; //KB
$filesizeMB = $filesizeKB/1024; //MB
if ($debug)
{
echo "The total filesize of \"$path\" is: ".round($filesizeMB, 2)."MB.<br />";
}
$filesDeleted = 0;
if ($filesizeMB > $maxMB)
{
if ($log)
{
fwrite($logFileHandle, "Session started - ". date(DATE_RFC822). "\n");
fwrite($logFileHandle, "The total filesize of \"$path\" is: ".round($filesizeMB, 2)." MB. Maximum $maxMB MB allowed \n");
}
if ($handle = opendir("$path"))
{
$deleteAgeHours = $deleteAge*24;
$deleteAgeMinutes = $deleteAgeHours*60;
$deleteAgeSeconds = $deleteAgeMinutes*60;
if ($debug)
{
echo "Deleting files older then $deleteAgeSeconds seconds<br />---------------------";
}
if ($log)
{
fwrite($logFileHandle, "Deleting files from more than $deleteAge days ago:\n---------------------\n");
}
while (false !== ($file = readdir($handle)))
{
$filelastmodified = filemtime($path . $file);
if(($filelastmodified) < (time()-$deleteAgeSeconds))
{
if (strcmp($file, "..") != 0 && strcmp($file, ".") != 0)
{
if ($debug)
{
echo "Deleting ". $path . $file ."<br />";
}
if ($log)
{
//fwrite($logFileHandle, "Deleting ". $path . $file ." - " . round((filesize($path . $file)/1024)/1024, 4) . "MB - " . date(DATE_RFC822, $filelastmodified). "\n"); // Show file sizes in MB
fwrite($logFileHandle, "Deleting ". $path . $file ." - " . round(filesize($path . $file)/1024, 2) . "KB - " . date(DATE_RFC822, $filelastmodified). "\n");
}
unlink($path . $file);
$filesDeleted++;
}
}
}
closedir($handle);
}
if ($log)
{
$filesize = dirSize("$path");
$filesizeKB = $filesize/1024;
$filesizeMB = $filesizeKB/1024;
fwrite($logFileHandle, "---------------------\nTotal files deleted: $filesDeleted - The NEW total filesize of \"$path\" is: ".round($filesizeMB, 2)."MB. \n\n\n");
fclose($logFileHandle);
}
}
if ($debug)
{
echo "---------------------<br />Files deleted: $filesDeleted <br />";
$filesize = dirSize("$path");
$filesizeKB = $filesize/1024;
$filesizeMB = $filesizeKB/1024;
echo "The NEW total filesize of \"$path\" is: ".round($filesizeMB, 2)."MB.<br />";
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment