PirateControl
Member
- Apr 19, 2016
- 33
- 11
- 43
Is it possible? i want an Label on my Banner witch count the bann´s from the Server Bann list
Sorry for my bad english
Sorry for my bad english
<?php
/*
@author David
@copyright 2016 Multivitamin <multivitamin.wtf>
@license GPLv3 http://www.gnu.org/licenses/gpl.html
@version 1.2beta
Textfields Available Placeholders
TEAMSPEAK:
%status% - Displays online or offline
%sid% - Displays Server ID currently used
%sport% - Displays Server Port
%platform% - Displays on wich Platform the Server runs
%servername% - Displays the Server Name (top channel name)
%serverversion% - Displays the Server Version
%maxclients% - Displays Slots Available
%realclients% - Displays Clients only
%clientsonline% - Displays amount of Clients online (Couns Queries aswell!)
%channelcount% - Displays amount of channels
%packetloss% - Displays average Packetloss (Like: 4.2134)
%packetloss_00% - Displays average Packetloss (Like: 4.21)
%packetloss_floored% - Displays average Packetloss (Like: 4)
%ping% - Diplays Average Ping (Like: 64.2414)
%ping_floored% - Diplays Average Ping floored (Like: 64)
%nickname% - Displays the Nickname of the Client which is requesting the Banner
%groupcount[<servergroupids>]% - Counts the Groups in the Square Brackets write it like '2,6' where 2 and 6 is a Server Group ID
GENERAL
%timeHi% - Displays Time in Hour:Minute Format
%timeHis% - Displays Time in Hour:Minute:Second Format
%date% - Displays Date in day.month.Year Format
SOURCEQUERY
%sqinfo[<server>][<key>]% - Displays the specified Info Key for the specified Server you have entered
INFO for %nickname%:
If you have a Textfield where you want to use the placeholder %nickname% then you are not able to use
other Placeholders since this gets rendered individually for every client!
What is SourceQuery?
The SourceQuery PHP Library was written by xPAW (https://github.com/xPaw/PHP-Source-Query)
It can access Informations of a Server which uses the Source Protocol
You can find a List of Supported Games in the Link above!
Usage of %sqinfo[<server>][<key>]%
The First Parameter <server> defines the server you want to use, this will be explained later how to login to a Server
The Second Parameter <key> defines the Server Information you want to access, instructions on how to get all available Keys for a Server
is also explained later in this config file
IMPORTANT!
CURRENTLY SUPPORTED FORMAT FOR BACKGROUND IMAGE IS .PNG ONLY!
You need to give the folger cache/ write Permissions!
On Standard Debian Configuration you can do this while inside Banner Folder with the Command:
chown -R www-data cache
TEAMSPEAK:
Add in Teamspeak "Banner Gfx Url" the Web Path to your banner.php
Change the "Gfx Interval" to 60 so the Client requests a new Banner every 60 Seconds!
PERMISSIONS:
b_virtualserver_servergroup_list
b_serverinstance_version_view
b_serverinstance_info_view
b_virtualserver_connectioninfo_view
b_virtualserver_client_list
b_client_remoteaddress_view - For Nickname display
and probably some more.... I will edit this List later lol....
DIFFERENT FONTS:
Currently available Fonts inside the "font" Folder is
- arial.ttf
- KGAllofMe.ttf
- neuropoliticalrg.ttf
You can download fonts for example from http://www.dafont.com
REQUIREMENTS
PHP Version 5.5 or greater
Web Server with php5-gd installed (apt-get install php5-gd)
If SourceQuery is enabled it will need php5-gmp to operate!
*/
$config = array('textfield' => [], 'sourcequery' => []);
/*
** Teamspeak Configurations
*/
//Teamspeak Connection IP
$config['teamspeak']['ip'] = 'localhost';
//Teamspeak Query Port
$config['teamspeak']['queryport'] = '10011';
//Teamspeak Connection/Voice Port
$config['teamspeak']['serverport'] = '9987';
//Teamspeak Query Login Name
$config['teamspeak']['loginname'] = 'serveradmin';
//Teamspeak Query Password
$config['teamspeak']['loginpass'] = 'PASSWORD';
//Background Image to use
$config['backgroundimage'] = 'default.png';
//Refresh Intervall for the Image Generator in seconds
$config['syncintervall'] = 60;
//SourceQuery config START
//Initially enables SourceQuery
$config['sqenable'] = false;
//Font File Path for Listing all available Parameters for the given Source Query Server
$config['sqlistfont'] = 'font/arial.ttf';
/*
Source Query Integration
Uses Library from https://github.com/xPaw/PHP-Source-Query
A List of Supported Games in the link above
Example for SourceQuery*/
/*
$config['sourcequery']['server1'] = [
//IP from Server
'ip' => '5.104.104.99',
//Port for Source Query
'port' => '2303',
//Timeout
'timeout' => 1,
//with this on true it will List all Available fields you can use directly on the banner
'debug' => false,
];
*/
//SourceQuery config END
/*
** Textfields
** text = The Text which should be in the Field (all Available Placeholders on top of the config.php)
** xpos = The X Position where the Textfield is located (in Pixel)
** ypos = The Y Position where the Textfield is located (in Pixel)
** fontsize = The Fontsize the Text should have
** fontfile = The Font you want to use for the Text
** color = Hex Color of the Font (Like: #FF9E00)
** EMPTY PRESET
$config['textfield'][] = [
'text' => '',
'xpos' => '',
'ypos' => '',
'fontsize' => '',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '',
];
*/
date_default_timezone_set('Europe/Warsaw');
$patch = dirname(__FILE__).'/';
$color2 = imagecolorallocate($img, 0, 197, 255);
$time = date("H:i");
$config['textfield'][] = [
'text' => '%date%',
'xpos' => '14',
'ypos' => '205',
'fontsize' => '38',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '#FF9E00',
];
$config['textfield'][] = [
'text' => $time,
'xpos' => '14',
'ypos' => '315',
'fontsize' => '38',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '#FF9E00',
];
$config['textfield'][] = [
'text' => 'Server NAME',
'xpos' => '14',
'ypos' => '95',
'fontsize' => '38',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '#FF9E00',
];
$config['textfield'][] = [
'text' => '%groupcount[9,140]%',
'xpos' => '810',
'ypos' => '95',
'fontsize' => '38',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '#FF9E00',
];
$config['textfield'][] = [
'text' => '%realclients%/%maxclients%',
'xpos' => '765',
'ypos' => '205',
'fontsize' => '38',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '#FF9E00',
];
$config['textfield'][] = [
'text' => '%ping_floored% ms',
'xpos' => '750',
'ypos' => '315',
'fontsize' => '38',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '#FF9E00',
];
$config['textfield'][] = [
'text' => 'Info TEXT',
'xpos' => '14',
'ypos' => '430',
'fontsize' => '38',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '#FF9E00',
];
/*
EXAMPLE TEXTFIELD FOR SOURCE QUERY ArmA Server
*/
/*
$config['textfield'][] = [
'text' => 'SourceQuery Slots:',
'xpos' => '150',
'ypos' => '350',
'fontsize' => '24',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '#ecf0f1',
];
$config['textfield'][] = [
'text' => '%sqinfo[server1][Players]%/%sqinfo[server1][MaxPlayers]%',
'xpos' => '220',
'ypos' => '400',
'fontsize' => '24',
'fontfile' => 'font/KGAllofMe.ttf',
'color' => '#ecf0f1',
];
*/
<?php
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
require_once('libraries/SourceQuery/bootstrap.php');
use xPaw\SourceQuery\SourceQuery;
$replacer = [
"ts3" => [
"%status%" => "virtualserver_status",
"%sid%" => "virtualserver_id",
"%sport%" => "virtualserver_port",
"%platform%" => "virtualserver_platform",
"%servername%" => "virtualserver_name",
"%serverversion%" => "virtualserver_version",
"%maxclients%" => "virtualserver_maxclients",
"%clientsonline%" => "virtualserver_clientsonline",
"%channelcount%" => "virtualserver_channelsonline",
"%packetloss%" => "virtualserver_total_packetloss_total",
"%ping%" => "virtualserver_total_ping"
]
];
$package = [
'general' => [
'static' => [
'%timeHi%', '%timeHis%', '%date%'
]
],
'ts3' => [
'static' => [
'%status%', '%sid%', '%sport%', '%platform%', '%servername%', '%serverversion%', '%packetloss_floored%', '%ping_floored%',
'%packetloss_00%', '%maxclients%', '%clientsonline%', '%channelcount%', '%packetloss%', '%ping%', '%realclients%', '%nickname%'
],
'regex' => [
"%groupcount\[([0-9,]{0,})\]%",
],
],
];
function getIp() {
if (isset($_SERVER['HTTP_CLIENT_IP']))
return $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
return $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
return $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
return $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
return $_SERVER['REMOTE_ADDR'];
else
return NULL;
}
function paintText($image, $fontsize, $xpos, $ypos, $color, $fontfile, $text) {
$hex = str_replace("#", "", $color);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
imagettftext($image,$fontsize,0,$xpos,$ypos,imagecolorallocate($image, $r, $g, $b),$fontfile,$text);
return;
}
function getImage() {
global $config;
$packetmanager = json_decode(file_get_contents('cache/packages.json'), 1);
$image = imagecreatefrompng('cache/cached_img');
if (in_array('ts3', $packetmanager['packages'])) {
require_once('cache/clients.php');
if (!empty($nicklist[getIp()])) {
$nickname = $nicklist[getIp()];
foreach ($config['textfield'] as $field) {
if (strpos($field['text'], '%nickname%') !== FALSE) {
paintText($image, $field['fontsize'], $field['xpos'], $field['ypos'], $field['color'], $field['fontfile'], str_replace('%nickname%', $nickname, $field['text']));
}
}
}
}
return $image;
}
try {
if (!function_exists('imagettftext')) {
throw new Exception ('PHP-GD not installed >> http://php.net/manual/en/book.image.php');
}
if (!is_writable('cache/')) {
throw new Exception ('No Write Permission for Folder \'cache/\' in Root Directory of Generator');
}
if (!file_exists('config.php' )) {
throw new Exception ('config.php does not exist');
} else {
require_once('config.php' );
}
if (!file_exists('cache/packages.json') || filemtime('cache/packages.json') < filemtime('config.php')) {
$packages = [];
if (count($config['sourcequery']) > 0 and $config['sqenable']) $packages[] = 'sq';
foreach ($config['textfield'] as $txt) {
foreach ($package as $key => $pkg) {
if (array_key_exists('static', $pkg)) {
foreach ($pkg['static'] as $static) {
if (strpos($txt['text'], $static) !== false and !in_array($key, $packages)) $packages[] = $key;
}
}
if (array_key_exists('regex', $pkg)) {
foreach ($pkg['regex'] as $regex) {
preg_match_all('/'.$regex.'/i', $txt['text'], $out);
if (count($out[0]) > 0 and !in_array($key, $packages)) {
$packages[] = $key;
}
}
}
}
}
$packagefile = fopen('cache/packages.json', 'w+');
fwrite($packagefile, json_encode(['packages' => $packages],1));
fclose($packagefile);
}
$packetmanager = json_decode(file_get_contents('cache/packages.json'), 1);
if (
(
file_exists('cache/cached_img')
and filemtime('cache/cached_img') > (time() - $config['syncintervall'])
//and strpos('TeamSpeak3', $_SERVER['HTTP_USER_AGENT']) !== FALSE
) || (
file_exists('cache/cache.lock')
)
){
$i = 0;
while (file_exists('cache/cache.lock')) {
if ($i >= 10) throw new Exception ('Cache Lock exists... Please Remove the File \'cache.lock\' in Folder \'cache\' manually if it still exists after this Error!');
$i++;
sleep(1);
}
header('Content-Type: image/png');
imagepng(getImage());
die();
}
fclose(fopen("cache/cache.lock", "w+"));
if (!file_exists($config['backgroundimage'])) throw new Exception ('Error! Background Image not found! Check your config! Searched at '.$config['backgroundimage']);
$image = imagecreatefrompng($config['backgroundimage']);
if (in_array('ts3', $packetmanager['packages'])) {
try {
$ts3 = TeamSpeak3::factory(
"serverquery://". $config['teamspeak']['loginname'] .":". $config['teamspeak']['loginpass']
."@". $config['teamspeak']['ip'] .":". $config['teamspeak']['queryport']
."/?server_port=". $config['teamspeak']['serverport']
."&nickname=Banner%20Generator"
);
} catch (Exception $e) {
if (!strpos('TeamSpeak3-ImageFetcher', $_SERVER['HTTP_USER_AGENT'])) throw $e;
$i = 0;
while (file_exists('cache/cache.lock')) {
if ($i >= 10) throw new Exception ('Cache Lock exists... Please Remove the File \'cache.lock\' in Folder \'cache\' manually if it still exists after this Error!');
$i++;
sleep(1);
}
header('Content-Type: image/png');
imagepng(getImage());
die();
}
$groupcount = [];
$serverinfo = $ts3->getInfo();
$clients = $ts3->clientList(['client_type' => 0]);
foreach ($clients as $client) {
$ts3clients[htmlentities($client->connection_client_ip)] = htmlentities($client->client_nickname);
}
$clientcache = fopen('cache/clients.php', 'w+');
fwrite($clientcache, '<?php $nicklist = json_decode(\''.str_replace("'", "\'", json_encode($ts3clients, 1)).'\',1);');
fclose($clientcache);
}
if (in_array('sq', $packetmanager['packages'])) {
$sqinfo = [];
foreach ($config['sourcequery'] as $server => $conf) {
try {
$sq = new SourceQuery();
$sq->Connect($conf['ip'], $conf['port'], $conf['timeout'], SourceQuery::SOURCE);
$y = 12;
foreach ($sq->GetInfo() as $key => $value) {
$sqinfo[$server][$key] = $value;
if ($conf['debug']) {
$color = imagecolorallocate($image, 255, 0, 0);
imagettftext($image, 9, 0, 4, $y, $color, $config['sqlistfont'], 'Use "%sqinfo['.$server.']['.$key.']%" in Textfield to Display "'.$value.'"');
$y = $y + 11;
}
}
$sq->Disconnect();
} catch (Exception $e) {
$color = imagecolorallocate($image, 255, 0, 0);
imagettftext($image, 11, 0, 4, 15, $color, $config['sqlistfont'], 'SourceQuery Error on Server "'.$server.'":'.$e->getMessage()
);
}
}
}
foreach ($config['textfield'] as $field) {
if (!file_exists($field['fontfile'])) throw new Exception ('Font File not found! Searched at '.$field['fontfile'].PHP_EOL.'You may need to set the absolute path (from root directory /var/www/...)');
if (strpos($field['text'], '%nickname%') !== FALSE) continue;
if (in_array('sq', $packetmanager['packages'])) {
$field['text'] = preg_replace_callback('/\%sqinfo\[(.*?)\]\[(.*?)\]\%/', function($matches) {
global $sqinfo;
return $sqinfo[$matches[1]][$matches[2]];
}, $field['text']);
}
if (in_array('ts3', $packetmanager['packages'])) {
foreach ($replacer['ts3'] as $k => $v) {
$field['text'] = str_replace($k, $serverinfo[$v], $field['text']);
}
$field['text'] = preg_replace_callback('/\%groupcount\[([0-9,]{0,})\]\%/', function($match) {
global $ts3;
$count = 0;
$groups = explode(',',$match[1]);
foreach ($ts3->clientList(['client_type' => 0]) as $client) {
foreach (explode(',',$client->client_servergroups) as $g) {
if (in_array($g,$groups)) {
$count++;
break;
}
}
}
return $count;
}, $field['text']);
foreach ($groupcount as $k => $v) {
$field['text'] = str_replace($k, $groupcount[$k], $field['text']);
}
$field['text'] = str_replace('%realclients%', $serverinfo['virtualserver_clientsonline']-$serverinfo['virtualserver_queryclientsonline'], $field['text']);
$field['text'] = str_replace('%ping_floored%', floor(htmlentities($serverinfo['virtualserver_total_ping'])), $field['text']);
$field['text'] = str_replace('%packetloss_00%', round(htmlentities($serverinfo['virtualserver_total_packetloss_total']), 2, PHP_ROUND_HALF_DOWN), $field['text']);
$field['text'] = str_replace('%packetloss_floored%', floor(htmlentities($serverinfo['virtualserver_total_packetloss_total'])), $field['text']);
}
$field['text'] = str_replace('%timeHi%', date("H:i"), $field['text']);
$field['text'] = str_replace('%timeHis%', date("H:i:s"), $field['text']);
$field['text'] = str_replace('%date%', date("d.m.Y"), $field['text']);
paintText($image, $field['fontsize'], $field['xpos'], $field['ypos'], $field['color'], $field['fontfile'], $field['text']);
}
imagepng($image, 'cache/cached_img');
unlink('cache/cache.lock');
header('Content-Type: image/png');
imagepng(getImage());
imagedestroy($image);
} catch (Exception $e) {
echo $e->getMessage();
if (file_exists('cache/cache.lock')) unlink('cache/cache.lock');
}