Skip to content

Instantly share code, notes, and snippets.

@cl0ne
Created July 31, 2011 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cl0ne/1117172 to your computer and use it in GitHub Desktop.
Save cl0ne/1117172 to your computer and use it in GitHub Desktop.
Simple "HLTV Demos archive" script pack
<?php
/*
Simple "HLTV Demos archive" databaseless script
Copyright (C) 2011 CL0NE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Configure server archives
[subfolder name without trailing '/']
title = [server title]
prefix = [demo file prefixes without trailing '-']
*/
$servers = array (
'classic-1' => array(
"title" => "[CL0NE] Classic #1",
"prefix" => "hltv"
),
'classic-2' => array(
"title" => "[CL0NE] Classic #2",
"prefix" => "cs"
)
);
$filesPerPage = 20;
# Size in bytes
function format_size($size, $round = 0)
{
$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
for ($i=0; $size > 1024 && isset($sizes[$i+1]); $i++)
$size /= 1024;
return round($size, $round)." ".$sizes[$i];
}
function browse($path)
{
global $servers;
global $filesPerPage;
$data = array();
$page = $_GET['page'];
$page = !isset($page) || $page < 1 ? 0 : $page - 1;
$prefix = $servers[$path]['prefix'];
$prefix_sz = strlen($prefix) + 1;
$offsets = array(
year => $prefix_sz,
month => $prefix_sz + 2,
day => $prefix_sz + 4,
hour => $prefix_sz + 6,
minute => $prefix_sz + 8,
map => $prefix_sz + 11
);
if(is_dir($path))
{
$files = glob($path."/*.dem.gz");
$count = count($files);
$pages = ceil( $count / $filesPerPage );
$page = $page >= $pages ? $pages - 1 : $page;
$begin = $count - ($page + 1) * $filesPerPage;
$end = $count - $page * $filesPerPage;
for($i = $begin < 0 ? 0 : $begin; $i < $end; ++$i)
{
$file = end( explode('/', $files[$i]) );
$name_end = strrpos($file, ".dem");
$name_end = $name_end === FALSE ? strrpos($file, ".gz") : $name_end;
$tmpfilesize = @filesize($files[$i]);
if($name_end === FALSE)
continue;
$tmp = array();
$tmp[year] = substr($file, $offsets[year], 2);
$tmp[month] = substr($file, $offsets[month], 2);
$tmp[day] = substr($file, $offsets[day], 2);
$tmp[hour] = substr($file, $offsets[hour], 2);
$tmp[minute] = substr($file, $offsets[minute], 2);
$tmp[map] = substr($file, $offsets[map], $name_end - $offsets[map]);
$tmp[file] = $file;
$tmp[size] = format_size($tmpfilesize);
$tmp[path] = $files[$i];
$data[] = $tmp;
}
$files = array();
}
echo '<table class="list" cellspacing="0" cellpadding="2">
<tr>
<th style="width: 10%; text-align: center;"> Date</th>
<th style="width: 9%; text-align: center;"> Time</th>
<th style="width: 25%;"> Map</th>
<th style="width: 10%;"> Size</th>
<th style=""> Link</th>
</tr>';
if($count > 0)
{
$ll = 0;
foreach($data as $field)
{
echo '<tr'.($ll?' class="ll"':'').'>
<td style="text-align: center;">'.$field[day].'.'.$field[month].'.'.$field[year].'</td>
<td style="text-align: center;">'.$field[hour].':'.$field[minute].'</td>
<td>'.$field[map].'</td>
<td>'.$field[size].'</td>
<td><a href="'.$field[path].'">'.$field[file].'</a></td>
</tr>';
$ll = !$ll;
}
}
else
{
echo '<tr><td class="ll" colspan="5" align="center">- Нет демо -</td></tr>';
}
echo '</table>';
if($pages > 1)
{
echo '<div class="pagination">';
for($i = 0; $i < $pages; ++$i)
{
if($pages > 10)
{
if($i > 2 && $i < $page - 1)
{
echo '...';
$i = $page - 1;
}
else
if($i > $page + 1 && $i > 2 && $i < $pages - 3)
{
echo '...';
$i = $pages - 3;
}
}
if($i != $page)
echo '<a href="?page='.($i + 1).'&amp;server='.$path.'">'.($i + 1).'</a>';
else
echo '<span>'.($page + 1).'</span>';
}
echo '</div>';
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<style type="text/css">
body
{
padding: 0px;
margin: 0px;
text-align: center;
background-color: #1F1F1F;
color: #6BA9BF;
font-size: 9pt;
font-family: sans-serif;
}
#wrapper
{
margin: 0 auto;
padding: 0em 0.5em;
max-width: 960px;
min-width: 760px;
width: auto !important;
}
#header
{
padding-bottom: 1em;
}
ul.menu
{
list-style-type: none;
margin: 0em;
margin-left: 1em;
border-left: solid 3px #3f6fEF;
padding: 0em;
font-size: 11pt;
float: left;
}
ul.menu li
{
padding-bottom: 1px;
border-right: solid 1px #313A3F;
border-bottom: solid 1px #313A3F;
float: left;
}
ul.menu li:hover
{
border-bottom: solid 1px #6BA9BF;
padding-bottom: 0px;
}
ul.menu li a
{
display: block;
padding: 3px 1em;
}
ul.menu a:hover
{
padding: 3px 1em 4px 1em;
background-color: #313A3F;
}
a
{
color: #6BA9BF !important;
text-decoration:none;
}
h1.title
{
clear: left;
text-shadow: 1px 1px 1px #666, -1px -1px 1px #000;
}
#content
{
min-height: 450px;
}
.list
{
width: 99%;
text-align: left;
border: #313A3F solid 1px;
}
.list th
{
border-bottom: solid 1px #313A3F;
color: #4F6F7F;
background: url('bgth.gif') repeat-x scroll center bottom #000;
color: #EFEFEF;
text-shadow: 1px 1px 1px #555, -1px -1px 1px #000;
}
.list tr:hover
{
background-color: #272F2F;
}
.list .ll
{
background-color: #313A3F;
}
.pagination
{
padding: 0.5em;
font-size: 1.5em;
}
.pagination a
{
text-decoration: underline;
margin: 0 0.2em;
}
.pagination span
{
margin: 0 0.1em;
padding: 0 0.1em;
background-color: #272F2F;
}
#footer
{
background-color: #272F2F;
border: 1px solid #3f3f3f;
padding: 1em;
margin: 1em 0em 0.25em 0em;
}
</style>
</head>
<body>
<div id="wrapper" align="center">
<div id="header">
<ul class="menu">
<li><a href="http://127.0.0.1/">На сайт</a></li>
<li><a href="http://127.0.0.1/">Банлист</a></li>
<li><a href="http://google.com./">В гугл?</a></li>
</ul>
<h1 class="title">HLTV Demos</h1>
</div>
<div id="content">
<?php
if(is_array($servers))
{
$count = count($servers);
if($count > 1)
{
?>
<form action="" method="get">
<span style="font-weight: bold; font-size: 1.1em;">Server: </span>
<select name="server">
<?php
foreach ($servers as $key => $value)
{
echo '<option'.( $key == $_GET['server'] ? ' selected' : '').' value="'.$key.'">'.$value['title'].'</option>';
}
?>
</select>
<button type="submit">Go</button>
</form>
<?php
}
if($count == 1)
{
end($servers);
browse( key($servers) );
}
else if($count == 0)
{
echo "- Список серверов пуст -";
}
elseif( isset( $servers[$_GET['server']] ) )
browse($_GET['server']);
}
else
{
echo "- Скрипт не сконфигурирован -";
}
?>
</div>
<div id="footer">HLTV demos archive. [C] CL0NE</div>
</div>
</body>
</html>
<?php
/*
Simple "HLTV Demos archive" databaseless script
Copyright (C) 2011 CL0NE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Configure server archives
[subfolder name without trailing '/']
title = [server title]
prefix = [demo file prefixes without trailing '-']
*/
$servers = array (
'classic-1' => array(
"title" => "[CL0NE] Classic #1",
"prefix" => "hltv"
),
'classic-2' => array(
"title" => "[CL0NE] Classic #2",
"prefix" => "cs"
)
);
# Size in bytes
function format_size($size, $round = 0)
{
$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
for ($i=0; $size > 1024 && isset($sizes[$i+1]); $i++)
$size /= 1024;
return round($size, $round)." ".$sizes[$i];
}
function browse($path)
{
global $servers;
$data = array();
$prefix = $servers[$path]['prefix'];
$prefix_sz = strlen($prefix) + 1;
$offsets = array (
year => $prefix_sz,
month => $prefix_sz + 2,
day => $prefix_sz + 4,
hour => $prefix_sz + 6,
minute => $prefix_sz + 8,
map => $prefix_sz + 11
);
if(is_dir($path))
{
if($dir = opendir($path))
{
while( ($file = readdir($dir)) !== FALSE )
{
if (!is_dir($file) && $file[0] != '.')
{
$name_end = strrpos($file, ".dem");
$name_end = $name_end === FALSE ? strrpos($file, ".gz") : $name_end;
if($name_end === FALSE)
continue;
$tmpfilesize = @filesize($path == '.' ? $file : $path.'/'.$file);
$tmp = array();
$tmp[year] = substr($file, $offsets[year], 2);
$tmp[month] = substr($file, $offsets[month], 2);
$tmp[day] = substr($file, $offsets[day], 2);
$tmp[hour] = substr($file, $offsets[hour], 2);
$tmp[minute] = substr($file, $offsets[minute], 2);
$tmp[map] = substr($file, $offsets[map], $name_end - $offsets[map]);
$tmp[file] = $file;
$tmp[size] = format_size($tmpfilesize);
$tmp[path] = $path == '.' ? $file : $path.'/'.$file;
$data[] = $tmp;
}
}
closedir($dir);
}
}
echo '<table class="list" cellspacing="0" cellpadding="2">
<tr>
<th style="width: 10%; text-align: center;"> Date</th>
<th style="width: 9%; text-align: center;"> Time</th>
<th style="width: 25%;"> Map</th>
<th style="width: 10%;"> Size</th>
<th style=""> Link</th>
</tr>';
if(count($data) > 0)
{
sort($data);
$data = array_reverse($data);
$ll = 0;
foreach($data as $field)
{
echo '<tr'.($ll?' class="ll"':'').'>
<td style="text-align: center;">'.$field[day].'.'.$field[month].'.'.$field[year].'</td>
<td style="text-align: center;">'.$field[hour].':'.$field[minute].'</td>
<td>'.$field[map].'</td>
<td>'.$field[size].'</td>
<td><a href="'.$field[path].'">'.$field[file].'</a></td>
</tr>';
$ll = !$ll;
}
}
else
{
echo '<tr><td class="ll" colspan="5" align="center">- Нет демо -</td></tr>';
}
echo '</table>';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<style type="text/css">
body
{
padding: 0px;
margin: 0px;
text-align: center;
background-color: #1F1F1F;
color: #6BA9BF;
font-size: 9pt;
font-family: sans-serif;
}
#wrapper
{
margin: 0 auto;
padding: 0em 0.5em;
max-width: 960px;
min-width: 760px;
width: auto !important;
}
#header
{
padding-bottom: 1em;
}
ul.menu
{
list-style-type: none;
margin: 0em;
margin-left: 1em;
border-left: solid 3px #3f6fEF;
padding: 0em;
font-size: 11pt;
float: left;
}
ul.menu li
{
padding-bottom: 1px;
border-right: solid 1px #313A3F;
border-bottom: solid 1px #313A3F;
float: left;
}
ul.menu li:hover
{
border-bottom: solid 1px #6BA9BF;
padding-bottom: 0px;
}
ul.menu li a
{
display: block;
padding: 3px 1em;
}
ul.menu a:hover
{
padding: 3px 1em 4px 1em;
background-color: #313A3F;
}
a
{
color: #6BA9BF !important;
text-decoration:none;
}
h1.title
{
clear: left;
text-shadow: 1px 1px 1px #666, -1px -1px 1px #000;
}
#content
{
min-height: 450px;
}
.list
{
width: 99%;
text-align: left;
border: #313A3F solid 1px;
}
.list th
{
border-bottom: solid 1px #313A3F;
color: #4F6F7F;
background: url('bgth.gif') repeat-x scroll center bottom #000;
color: #EFEFEF;
text-shadow: 1px 1px 1px #555, -1px -1px 1px #000;
}
.list tr:hover
{
background-color: #272F2F;
}
.list .ll
{
background-color: #313A3F;
}
#footer
{
background-color: #272F2F;
border: 1px solid #3f3f3f;
padding: 1em;
margin: 1em 0em 0.25em 0em;
}
</style>
</head>
<body>
<div id="wrapper" align="center">
<div id="header">
<ul class="menu">
<li><a href="http://127.0.0.1/">На сайт</a></li>
<li><a href="http://127.0.0.1/">Банлист</a></li>
<li><a href="http://google.com./">В гугл?</a></li>
</ul>
<h1 class="title">HLTV Demos</h1>
</div>
<div id="content">
<?php
if(is_array($servers))
{
$count = count($servers);
if($count > 1)
{
?>
<form action="" method="get">
<span style="font-weight: bold; font-size: 1.1em;">Server: </span>
<select name="server">
<?php
foreach ($servers as $key => $value)
{
echo '<option'.( $key == $_GET['server'] ? ' selected' : '').' value="'.$key.'">'.$value['title'].'</option>';
}
?>
</select>
<button type="submit">Go</button>
</form>
<?php
}
if($count == 1)
{
end($servers);
browse( key($servers) );
}
else if($count == 0)
{
echo "- Список серверов пуст -";
}
elseif( isset( $servers[$_GET['server']] ) )
browse($_GET['server']);
}
else
{
echo "- Скрипт не сконфигурирован -";
}
?>
</div>
<div id="footer">HLTV demos archive. [C] CL0NE</div>
</div>
</body>
</html>
#!/bin/bash
##
## Скрипт архивирует демки и удаляет старые [c] CL0NE
##
PATH_TO_DEMOS[0]="/very_long_path1/cstrike/demos/";
PATH_TO_DEMOS[1]="/very_long_path2/cstrike/demos/";
PATH_TO_DEMOS[2]="/very_long_path3/cstrike/demos/";
OLDER_THAN=21;
IFS=$'\n'
echo
echo "[ Removing old and archiving new demos ]"
echo
for i in "${PATH_TO_DEMOS[@]}";
do
echo "Dir: $i";
find "$i" \( -name '*.dem' -o -name '*.gz' \) -mtime +$OLDER_THAN -exec rm {} \;
echo "Cleaned";
for file in `find "$i" -mmin +5 -name '*.dem' -print`
do
gzip -9 $file
echo " `basename $file`"
done
echo "Archived";
echo
done
##
##
var pathToDemos = new Array();
var outPath = new Array();
// Configure your paths
pathToDemos[0]="C:/very_long_path1/cstrike/demos/";
outPath[0]="C:/very_long_path/www/demos/";
pathToDemos[1]="C:/very_long_path2/cstrike/demos/";
outPath[1]="D:/very_long_path/www/demos/";
pathToDemos[2]="C:/very_long_path3/cstrike/demos/";
outPath[2]="E:/very_long_path/www/demos/";
// Keep demos last OlderThan days
var OlderThan = 21;
// Min modification time in minutes for demos to treat treat them as finished
var MinMod = 5;
var today = new Date();
var DaySec = 24 * 60 * 60;
function endsWith(string, suffix)
{
var ind = string.lastIndexOf(suffix);
return ( ind != -1 && ind == (string.length - suffix.length) );
}
function Clean()
{
this.callback = null;
}
Clean.prototype.processFile = function(file)
{
if( (today - file.DateCreated) / (1000 * DaySec) >= OlderThan
&& ( endsWith(file.Name, ".gz") || endsWith(file.Name, ".dem") ) )
{
WScript.Echo("Deleting \"" + file.Name + "\"");
file.Delete(true);
}
if(this.callback != null)
this.callback.processFile(file);
}
function Archive()
{
this.callback = null;
this.outPath = [];
}
Archive.prototype.processFile = function(file)
{
if(endsWith(file.Name, ".dem") && (today - file.DateLastModified) / 1000 >= MinMod * 60)
{
var errcode = WshShell.Run("7z a -tgzip \"" + this.outPath + file.Name + ".gz\" \"" + file.Name + "\"", 0, true);
if(!errcode)
{
WScript.Echo("Archived \"" + file.Name + "\"");
file.Delete(true);
}
else
{
WScript.Echo("Archiving error[" + errcode + "] on \"" + file.Name + "\"");
}
}
if(this.callback != null)
this.callback.processFile(file);
}
var FileSystem = WScript.CreateObject("Scripting.FileSystemObject");
var WshShell = WScript.CreateObject("WScript.Shell");
function processPath(path, callback)
{
var directory = FileSystem.GetFolder(path);
var files = new Enumerator(directory.files);
for(; files.atEnd() == false; files.moveNext())
{
callback.processFile(files.item());
}
}
WScript.Echo();
WScript.Echo("[ Removing old and archiving new demos ]");
WScript.Echo();
var clean = new Clean();
var archive = new Archive();
var cd = WshShell.CurrentDirectory;
for(var i = 0; i < pathToDemos.length; ++i)
{
WScript.Echo("Dir: " + pathToDemos[i]);
WScript.Echo("Out: " + outPath[i]);
clean.callback = null;
processPath(outPath[i], clean);
clean.callback = archive;
archive.outPath = outPath[i];
WshShell.CurrentDirectory = pathToDemos[i];
processPath(pathToDemos[i], clean);
WScript.Echo();
}
WshShell.CurrentDirectory = cd;
#!/bin/bash
##
## Скрипт архивирует демки, складывает в отдельный
## каталог и удаляет старые [c] CL0NE
##
PATH_TO_DEMOS[0]="/very_long_path1/cstrike/demos/";
PATH_TO_DEMOS[1]="/very_long_path2/cstrike/demos/";
PATH_TO_DEMOS[2]="/very_long_path3/cstrike/demos/";
OUT_PATH[0]="/var/www/demos1/";
OUT_PATH[1]="/var/www/demos2/";
OUT_PATH[2]="/var/www/demos3/";
OLDER_THAN=21;
IFS=$'\n'
echo
echo "[ Removing old and archiving new demos ]"
echo
for (( i = 0; i < "${#PATH_TO_DEMOS[@]}"; ++i));
do
DIR=${PATH_TO_DEMOS[$i]};
OUT=${OUT_PATH[$i]};
echo "Dir: $DIR";
echo "Out: $OUT";
find "$DIR" -name '*.dem' -mtime +$OLDER_THAN -exec rm {} \;
find "$OUT" -name '*.gz' -mtime +$OLDER_THAN -exec rm {} \;
echo "Cleaned";
mkdir -p $OUT
for file in `find "$DIR" -mmin +5 -name '*.dem' -print`
do
gzip -9 $file
echo " `basename $file`"
mv -ft "$OUT" "$file".gz
done
echo "Archived";
echo
done
##
##
@uGamers
Copy link

uGamers commented Feb 19, 2017

Thanks Clone did not see this comment section loving the script thanks for the hard work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment