2013-08-28 22:47:04 +02:00
< ? php
/**
* File : reboot . php .
* Author : Ulrich Block
* Contact : < ulrich . block @ easy - wi . com >
*
* This file is part of Easy - WI .
*
* Easy - WI 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 .
*
* Easy - WI 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 Easy - WI . If not , see < http :// www . gnu . org / licenses />.
*
* Diese Datei ist Teil von Easy - WI .
*
* Easy - WI ist Freie Software : Sie koennen es unter den Bedingungen
* der GNU General Public License , wie von der Free Software Foundation ,
* Version 3 der Lizenz oder ( nach Ihrer Wahl ) jeder spaeteren
* veroeffentlichten Version , weiterverbreiten und / oder modifizieren .
*
* Easy - WI wird in der Hoffnung , dass es nuetzlich sein wird , aber
* OHNE JEDE GEWAEHELEISTUNG , bereitgestellt ; sogar ohne die implizite
* Gewaehrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK .
* Siehe die GNU General Public License fuer weitere Details .
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben . Wenn nicht , siehe < http :// www . gnu . org / licenses />.
*/
if ( isset ( $_SERVER [ 'REMOTE_ADDR' ])) {
2013-10-05 13:39:56 +02:00
$ip = $_SERVER [ 'REMOTE_ADDR' ];
$timelimit = ( isset ( $_GET [ 'timeout' ]) and is_numeric ( $_GET [ 'timeout' ])) ? $_GET [ 'timeout' ] : ini_get ( 'max_execution_time' ) - 10 ;
2013-08-28 22:47:04 +02:00
} else {
$timelimit = 600 ;
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
set_time_limit ( $timelimit );
2013-10-05 13:39:56 +02:00
if ( ! isset ( $ip ) or $_SERVER [ 'SERVER_ADDR' ] == $ip ) {
2013-09-27 08:22:09 +02:00
define ( 'EASYWIDIR' , dirname ( __FILE__ ));
include ( EASYWIDIR . '/stuff/vorlage.php' );
include ( EASYWIDIR . '/stuff/class_validator.php' );
include ( EASYWIDIR . '/stuff/functions.php' );
include ( EASYWIDIR . '/stuff/settings.php' );
include ( EASYWIDIR . '/stuff/ssh_exec.php' );
include ( EASYWIDIR . '/stuff/class_masterserver.php' );
include ( EASYWIDIR . '/stuff/class_voice.php' );
2013-10-05 13:39:56 +02:00
include ( EASYWIDIR . '/stuff/queries_updates.php' );
include ( EASYWIDIR . '/stuff/keyphrasefile.php' );
echo " Reboot and Updater started \r \n " ;
if ( version_compare ( PHP_VERSION , '5.3.0' ) >= 0 ){
$currentTime = new DateTime ( date ( 'Y-m-d H:i:s' ));
2013-08-28 22:47:04 +02:00
} else {
2013-10-05 13:39:56 +02:00
$currentDay = date ( 'j' );
$currentDays = date ( 't' );
$currentHour = date ( 'G' );
}
$now = date ( 'Y-m-d' , strtotime ( 'now' ));
$sprache = getlanguagefile ( 'gserver' , 'uk' , 0 );
echo " Fetch version for Minecraft and Bukkit Server \r \n " ;
$query = $sql -> prepare ( " SELECT t.`shorten` FROM `servertypes` t LEFT JOIN `rservermasterg` r ON t.`id`=r.`servertypeid` WHERE r.`id` IS NOT NULL AND t.`qstat`='minecraft' GROUP BY t.`shorten` ORDER BY t.`shorten` " );
$query2 = $sql -> prepare ( " UPDATE `servertypes` SET `steamVersion`=?,`downloadPath`=? WHERE `shorten`=? " );
$query -> execute ();
foreach ( $query -> fetchAll ( PDO :: FETCH_ASSOC ) as $row ) {
echo 'Retrieving Version for ' . $row [ 'shorten' ] . " \r \n " ;
2013-10-05 16:01:51 +02:00
$reply = ( $row [ 'shorten' ] == 'bukkit' ) ? getCraftBukkitVersion () : getMinecraftVersion ();
2013-10-05 13:39:56 +02:00
if ( is_array ( $reply )) {
echo 'Version for ' . $row [ 'shorten' ] . ' is: ' . $reply [ 'version' ] . " \r \n " ;
if ( strlen ( $reply [ 'version' ]) > 1 ) {
$query2 -> execute ( array ( $reply [ 'version' ], $reply [ 'downloadPath' ], $row [ 'shorten' ]));
}
}
2013-08-28 22:47:04 +02:00
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
echo " Fetch version for valves appIDs \r \n " ;
2013-10-05 13:39:56 +02:00
2013-09-29 15:29:58 +02:00
$query = $sql -> prepare ( " SELECT t.`appID`,t.`shorten` FROM `servertypes` t LEFT JOIN `rservermasterg` r ON t.`id`=r.`servertypeid` WHERE r.`id` IS NOT NULL AND t.`appID` IS NOT NULL AND t.`steamgame`!='N' GROUP BY t.`appID` ORDER BY t.`appID` " );
$query2 = $sql -> prepare ( " UPDATE `servertypes` SET `steamVersion`=? WHERE `appID`=? " );
2013-08-28 22:47:04 +02:00
$query -> execute ();
2013-09-29 15:29:58 +02:00
$steamVersion = array ();
2013-08-28 22:47:04 +02:00
foreach ( $query -> fetchAll ( PDO :: FETCH_ASSOC ) as $row ) {
2013-10-05 13:39:56 +02:00
2013-10-03 12:49:13 +02:00
if ( ! in_array ( $row [ 'appID' ], array ( null , '' , false ))) {
2013-10-05 13:39:56 +02:00
$lookUpAppID = workAroundForValveChaos ( $row [ 'appID' ], $row [ 'shorten' ]);
$json = webhostRequest ( 'api.steampowered.com' , 'easy-wi.com' , '/ISteamApps/UpToDateCheck/v0001/?appid=' . $lookUpAppID . '&version=0.0.0.0&format=json' );
$decoded = @ json_decode ( $json );
2013-08-28 22:47:04 +02:00
if ( $decoded and ! isset ( $decoded -> response -> error ) and isset ( $decoded -> response -> required_version )) {
2013-10-03 12:49:13 +02:00
$query2 -> execute ( array ( $decoded -> response -> required_version , $row [ 'appID' ]));
2013-10-05 13:39:56 +02:00
echo 'Version for appID ' . $row [ 'appID' ] . ' is: ' . $decoded -> response -> required_version . " \r \n " ;
2013-08-28 22:47:04 +02:00
} else if ( isset ( $decoded -> response -> error )) {
2013-10-05 13:39:56 +02:00
echo 'Error for appID ' . $row [ 'appID' ] . ' is: ' . $decoded -> response -> error . " \r \n " ;
2013-08-28 22:47:04 +02:00
} else {
2013-10-05 13:39:56 +02:00
echo 'Error for appID ' . $row [ 'appID' ] . ' is: Could not retrieve JSON string' . " \r \n " ;
2013-08-28 22:47:04 +02:00
}
}
}
2013-10-05 13:39:56 +02:00
$webhostdomain = webhostdomain ( 0 );
2013-09-29 15:29:58 +02:00
$query = $sql -> prepare ( " SELECT `timezone`,`voice_autobackup`,`voice_autobackup_intervall`,`voice_maxbackup`,`down_checks`,`resellerid` FROM `settings` " );
2013-08-28 22:47:04 +02:00
$query -> execute ();
foreach ( $query -> fetchAll ( PDO :: FETCH_ASSOC ) as $row ) {
2013-10-05 13:39:56 +02:00
$resellerstimezone = $row [ 'timezone' ];
$resellerid = $row [ 'resellerid' ];
$voice_autobackup = $row [ 'voice_autobackup' ];
$voice_autobackup_intervall = $row [ 'voice_autobackup_intervall' ];
$voice_maxbackup = $row [ 'voice_maxbackup' ];
$down_checks = $row [ 'down_checks' ];
$stunde = date ( 'G' , strtotime ( $resellerstimezone . ' hour' ));
$next = date ( 'Y-m-d' , strtotime ( '-' . $voice_autobackup_intervall . ' day' ));
2013-09-29 15:29:58 +02:00
$query2 = $sql -> prepare ( " SELECT *,AES_DECRYPT(`querypassword`,:aeskey) AS `decryptedquerypassword`,AES_DECRYPT(`ssh2port`,:aeskey) AS `decryptedssh2port`,AES_DECRYPT(`ssh2user`,:aeskey) AS `decryptedssh2user`,AES_DECRYPT(`ssh2password`,:aeskey) AS `decryptedssh2password` FROM `voice_masterserver` WHERE `active`='Y' AND `resellerid`=:reseller_id " );
2013-10-05 13:39:56 +02:00
$query2 -> execute ( array ( ':aeskey' => $aeskey , ':reseller_id' => $resellerid ));
2013-08-28 22:47:04 +02:00
foreach ( $query2 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row2 ) {
2013-10-05 13:39:56 +02:00
$ts3masterid = $row2 [ 'id' ];
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT `id` FROM `voice_server` WHERE `masterserver`=? LIMIT 1 " );
2013-08-28 22:47:04 +02:00
$query3 -> execute ( array ( $ts3masterid ));
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( $query3 -> rowcount () > 0 ) {
2013-10-05 13:39:56 +02:00
unset ( $connect_ssh2 , $ssh2 , $badLogin );
$ts3masternotified = $row2 [ 'notified' ];
$addedby = $row2 [ 'addedby' ];
$queryport = $row2 [ 'queryport' ];
$querypassword = $row2 [ 'decryptedquerypassword' ];
$resellerid = $row2 [ 'resellerid' ];
$autorestart = $row2 [ 'autorestart' ];
2013-10-03 12:49:13 +02:00
if ( $addedby == '2' ) {
2013-10-05 13:39:56 +02:00
$queryip = $row2 [ 'ssh2ip' ];
2013-10-03 12:49:13 +02:00
} else if ( $addedby == '1' ) {
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT `ip` FROM `rserverdata` WHERE `id`=? AND `resellerid`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $row2 [ 'rootid' ], $resellerid ));
2013-08-28 22:47:04 +02:00
$queryip = $query3 -> fetchColumn ();
}
2013-10-05 13:39:56 +02:00
2013-09-29 15:29:58 +02:00
$tsdnsExternalActive = false ;
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( isid ( $row2 [ 'tsdnsServerID' ], 19 )) {
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT `id` FROM `voice_tsdns` WHERE `active`='Y' AND `id`=? LIMIT 1 " );
2013-08-28 22:47:04 +02:00
$query3 -> execute ( array ( $row2 [ 'tsdnsServerID' ]));
if ( $query3 -> rowCount () > 0 ) {
2013-10-03 12:49:13 +02:00
$tsdnsExternalActive = true ;
2013-08-28 22:47:04 +02:00
}
}
2013-09-29 15:29:58 +02:00
$ssh2 = false ;
2013-10-03 12:49:13 +02:00
if ( $row2 [ 'publickey' ] == 'Y' ) {
2013-09-22 12:00:33 +02:00
# https://github.com/easy-wi/developer/issues/70
$sshkey = removePub ( $row2 [ 'keyname' ]);
2013-09-28 13:12:29 +02:00
$pubkey = EASYWIDIR . '/keys/' . $sshkey . '.pub' ;
$key = EASYWIDIR . '/keys/' . $sshkey ;
2013-09-22 12:00:33 +02:00
2013-08-28 22:47:04 +02:00
if ( file_exists ( $pubkey ) and file_exists ( $key )) {
2013-10-05 13:39:56 +02:00
$ssh2 = @ ssh2_connect ( $queryip , $row2 [ 'decryptedssh2port' ], array ( 'hostkey' => 'ssh-rsa' ));
2013-08-28 22:47:04 +02:00
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
} else {
2013-10-03 12:49:13 +02:00
$ssh2 = @ ssh2_connect ( $queryip , $row2 [ 'decryptedssh2port' ]);
2013-08-28 22:47:04 +02:00
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( $ssh2 ) {
2013-10-05 13:39:56 +02:00
$connect_ssh2 = ( $row2 [ 'publickey' ] == 'Y' ) ? @ ssh2_auth_pubkey_file ( $ssh2 , $row2 [ 'decryptedssh2user' ], $pubkey , $key ) : @ ssh2_auth_password ( $ssh2 , $row2 [ 'decryptedssh2user' ], $row2 [ 'decryptedssh2password' ]);
2013-08-28 22:47:04 +02:00
if ( $connect_ssh2 ) {
2013-10-03 12:49:13 +02:00
$split_config = preg_split ( '/\//' , $row2 [ 'serverdir' ], - 1 , PREG_SPLIT_NO_EMPTY );
2013-08-28 22:47:04 +02:00
$folderfilecount = count ( $split_config ) - 1 ;
2013-09-29 15:29:58 +02:00
$i = 0 ;
2013-10-03 12:49:13 +02:00
$folders = ( substr ( $row2 [ 'serverdir' ], 0 , 1 ) == '/' ) ? 'cd /' : 'cd ' ;
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
while ( $i <= $folderfilecount ) {
2013-09-08 17:21:34 +02:00
$folders = $folders . $split_config [ $i ] . " / " ;
2013-08-28 22:47:04 +02:00
$i ++ ;
}
2013-10-05 13:39:56 +02:00
$folders = ( $folders == 'cd ' ) ? '' : $folders . ' && ' ;
2013-08-28 22:47:04 +02:00
} else {
2013-10-03 12:49:13 +02:00
$badLogin = true ;
2013-08-28 22:47:04 +02:00
print " Error: Bad logindata \r \n " ;
}
} else {
2013-10-03 12:49:13 +02:00
$badLogin = true ;
2013-08-28 22:47:04 +02:00
print " Error: Can not connect via ssh2 \r \n " ;
}
2013-10-05 13:39:56 +02:00
2013-09-22 12:00:33 +02:00
if ( ! isset ( $badLogin ) and isset ( $connect_ssh2 ) and $connect_ssh2 ) {
2013-10-05 13:39:56 +02:00
2013-09-29 15:29:58 +02:00
$tsdown = false ;
$tsdnsdown = false ;
2013-10-05 13:39:56 +02:00
$connection = new TS3 ( $queryip , $queryport , 'serveradmin' , $querypassword );
2013-08-28 22:47:04 +02:00
$errorcode = $connection -> errorcode ;
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( strpos ( $errorcode , 'error id=0' ) === false ) {
$connection -> CloseConnection ();
unset ( $connection );
sleep ( 1 );
2013-10-05 13:39:56 +02:00
$connection = new TS3 ( $queryip , $queryport , 'serveradmin' , $querypassword );
2013-08-28 22:47:04 +02:00
$errorcode = $connection -> errorcode ;
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( strpos ( $errorcode , 'error id=0' ) === false ) {
$connection -> CloseConnection ();
unset ( $connection );
2013-10-03 12:49:13 +02:00
$tsdown = true ;
2013-10-05 13:39:56 +02:00
print " TS3 Query Error: " . $errorcode . " \r \n " ;
2013-08-28 22:47:04 +02:00
$restartreturn = " TS3 " ;
}
2013-10-05 13:39:56 +02:00
2013-10-03 12:49:13 +02:00
if ( $row2 [ 'usedns' ] == 'Y' and $tsdnsExternalActive == false ) {
2013-10-05 13:39:56 +02:00
$tsdnscheck =@ fsockopen ( $queryip , 41144 , $errno , $errstr , 5 );
2013-08-28 22:47:04 +02:00
if ( ! is_resource ( $tsdnscheck )) {
sleep ( 1 );
2013-10-05 13:39:56 +02:00
$tsdnscheck =@ fsockopen ( $queryip , 41144 , $errno , $errstr , 5 );
2013-08-28 22:47:04 +02:00
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( ! is_resource ( $tsdnscheck )) {
print " TSDNS Error: " . $errno . ' (' . $errstr . " ) \r \n " ;
2013-10-03 12:49:13 +02:00
$tsdnsdown = true ;
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( isset ( $restartreturn )) {
$restartreturn .= " and internal TSDNS " ;
} else {
$restartreturn = " internal TSDNS " ;
}
}
2013-10-05 13:39:56 +02:00
2013-10-03 12:49:13 +02:00
} else if ( $row2 [ 'usedns' ] == 'Y' and $tsdnsExternalActive == true ) {
2013-08-28 22:47:04 +02:00
print " Skip TSDNS since external is used \r \n " ;
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( $tsdown == true or $tsdnsdown == true ) {
$ts3masternotified ++ ;
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( $ts3masternotified == $down_checks ) {
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( $resellerid == 0 ) {
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT `id`,`mail_serverdown` FROM `userdata` WHERE `accounttype`='a' AND `resellerid`=0 " );
2013-08-28 22:47:04 +02:00
$query3 -> execute ();
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
} else {
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT `id`,`mail_serverdown` FROM `userdata` WHERE `id`=? LIMIT 1 " );
2013-08-28 22:47:04 +02:00
$query3 -> execute ( array ( $resellerid ));
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
foreach ( $query3 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row3 ) {
2013-10-03 12:49:13 +02:00
if ( $row3 [ 'mail_serverdown' ] == 'Y' ) {
2013-10-05 13:39:56 +02:00
sendmail ( 'emaildownrestart' , $row3 [ 'id' ], $queryip . ' (' . $restartreturn . ')' , '' );
2013-08-28 22:47:04 +02:00
}
}
}
2013-10-05 13:39:56 +02:00
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " UPDATE `voice_masterserver` SET `notified`=? WHERE `id`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $ts3masternotified , $ts3masterid ));
2013-10-03 12:49:13 +02:00
if ( $autorestart == 'Y' and $ts3masternotified >= $down_checks ) {
2013-10-05 13:39:56 +02:00
2013-10-03 12:49:13 +02:00
if ( $row2 [ 'bitversion' ] == '32' ) {
2013-08-28 22:47:04 +02:00
$tsbin = 'ts3server_linux_x86' ;
$tsdnsbin = 'tsdnsserver_linux_x86' ;
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
} else {
$tsbin = 'ts3server_linux_amd64' ;
$tsdnsbin = 'tsdnsserver_linux_amd64' ;
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
$ssh2cmd = $folders . 'function restart1 () { if [ "`ps fx | grep ' . $tsbin . ' | grep -v grep`" == "" ]; then ./ts3server_startscript.sh start > /dev/null & else ./ts3server_startscript.sh restart > /dev/null & fi }; restart1& ' ;
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( $tsdown == true ) {
2013-10-05 13:39:56 +02:00
echo ssh2_exec ( $ssh2 , $ssh2cmd );
2013-08-28 22:47:04 +02:00
}
2013-10-05 13:39:56 +02:00
2013-10-03 12:49:13 +02:00
if ( $row2 [ 'usedns' ] == 'Y' ) {
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
if ( $tsdnsdown == true ) {
$ssh2cmd2 = $folders . 'cd tsdns && function restart2 () { if [ "`ps fx | grep ' . $tsdnsbin . ' | grep -v grep`" == "" ]; then ./' . $tsdnsbin . ' > /dev/null & else ./' . $tsdnsbin . ' --update > /dev/null & fi }; restart2& ' ;
2013-10-05 13:39:56 +02:00
echo ssh2_exec ( $ssh2 , $ssh2cmd2 );
2013-08-28 22:47:04 +02:00
}
}
print " Restarting: $restartreturn\r\n " ;
}
} else {
if ( $ts3masternotified > 0 ) {
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " UPDATE `voice_masterserver` SET `notified`=0 WHERE `id`=? LIMIT 1 " );
2013-08-28 22:47:04 +02:00
$query3 -> execute ( array ( $ts3masterid ));
}
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT `active`,`backup`,`id`,`ip`,`port`,`slots`,`userid`,`localserverid`,`max_download_total_bandwidth`,`max_upload_total_bandwidth`,`serverCreated` FROM `voice_server` WHERE `masterserver`=? AND `resellerid`=? " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $ts3masterid , $resellerid ));
2013-08-28 22:47:04 +02:00
foreach ( $query3 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row3 ) {
2013-10-05 13:39:56 +02:00
$ts3id = $row3 [ 'id' ];
$serverCreated = $row3 [ 'serverCreated' ];
$ts3userid = $row3 [ 'userid' ];
$localserverid = $row3 [ 'localserverid' ];
2013-10-03 12:49:13 +02:00
if ( $stunde == '00' or $serverCreated == null ) {
2013-09-29 15:29:58 +02:00
$resetTraffic = false ;
2013-08-28 22:47:04 +02:00
if ( version_compare ( PHP_VERSION , '5.3.0' ) >= 0 ){
$createdTime = new DateTime ( $serverCreated );
$interval = $createdTime -> diff ( $currentTime );
if ( $interval -> d == 0 and $interval -> m > 0 ) {
2013-10-03 12:49:13 +02:00
$resetTraffic = true ;
2013-08-28 22:47:04 +02:00
}
} else {
$createdDay = date ( 'j' , strtotime ( $serverCreated ));
$createdDays = date ( 't' , strtotime ( $serverCreated ));
2013-10-03 12:49:13 +02:00
if (( $createdDay == $createdDays and $currentDays == $currentDay ) or ( $createdDay != $createdDays and $createdDay == $currentDay )) {
$resetTraffic = true ;
2013-08-28 22:47:04 +02:00
}
}
2013-10-03 12:49:13 +02:00
if ( $resetTraffic == true and $serverCreated != null ) {
2013-09-29 15:29:58 +02:00
$query4 = $sql -> prepare ( " UPDATE `voice_server` SET `filetraffic`=0,`lastfiletraffic`=0 WHERE `id`=? LIMIT 1 " );
2013-08-28 22:47:04 +02:00
$query4 -> execute ( array ( $ts3id ));
2013-10-10 08:06:37 +02:00
$connection -> ImportModServer ( $localserverid , $row3 [ 'slots' ], $row3 [ 'ip' ], $row3 [ 'port' ], array ( 'virtualserver_max_download_total_bandwidth' => $row3 [ 'max_download_total_bandwidth' ], 'virtualserver_max_upload_total_bandwidth' => $row3 [ 'max_upload_total_bandwidth' ]));
2013-08-28 22:47:04 +02:00
} else if ( $serverCreated == null ) {
2013-09-29 15:29:58 +02:00
$query4 = $sql -> prepare ( " UPDATE `voice_server` SET `filetraffic`=0,`lastfiletraffic`=0,`serverCreated`=NOW() WHERE `id`=? LIMIT 1 " );
2013-08-28 22:47:04 +02:00
$query4 -> execute ( array ( $ts3id ));
2013-10-10 08:06:37 +02:00
$connection -> ImportModServer ( $localserverid , $row3 [ 'slots' ], $row3 [ 'ip' ], $row3 [ 'port' ], array ( 'virtualserver_max_download_total_bandwidth' => $row3 [ 'max_download_total_bandwidth' ], 'virtualserver_max_upload_total_bandwidth' => $row3 [ 'max_upload_total_bandwidth' ]));
2013-08-28 22:47:04 +02:00
}
}
2013-10-03 12:49:13 +02:00
if ( $voice_autobackup == 'Y' and $stunde == '5' and $row3 [ 'active' ] == 'Y' and $row3 [ 'backup' ] == 'Y' ) {
2013-08-28 22:47:04 +02:00
$name = 'Autobackup' ;
2013-09-29 15:29:58 +02:00
$backupcount = 0 ;
2013-08-28 22:47:04 +02:00
unset ( $last );
2013-09-29 15:29:58 +02:00
$query4 = $sql -> prepare ( " SELECT `id`,`date`,`name` FROM `voice_server_backup` WHERE `sid`=? AND `uid`=? AND `resellerid`=? ORDER BY `id` ASC " );
2013-10-05 13:39:56 +02:00
$query4 -> execute ( array ( $ts3id , $ts3userid , $resellerid ));
2013-08-28 22:47:04 +02:00
foreach ( $query4 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row4 ) {
$backupcount ++ ;
2013-10-05 13:39:56 +02:00
$date = $row4 [ 'date' ];
2013-10-03 12:49:13 +02:00
if ( $row4 [ 'name' ] == 'Autobackup' ) {
2013-08-28 22:47:04 +02:00
$last = date ( 'Y-m-d' , strtotime ( $date ));
}
}
$stunde = date ( 'G' , strtotime ( " $resellerstimezone hour " ));
if ( $backupcount == 0 or ! isset ( $last ) or ( isset ( $last ) and $last < $next )) {
$toomuch = $backupcount + 1 - $voice_maxbackup ;
if ( $toomuch > 0 ) {
2013-09-29 15:29:58 +02:00
$query4 = $sql -> prepare ( " SELECT `id` FROM `voice_server_backup` WHERE `sid`=? AND `uid`=? AND `resellerid`=? ORDER BY `id` ASC LIMIT $toomuch " );
2013-10-05 13:39:56 +02:00
$query4 -> execute ( array ( $ts3id , $ts3userid , $resellerid ));
2013-08-28 22:47:04 +02:00
foreach ( $query4 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row4 ) {
2013-09-29 15:29:58 +02:00
$query5 = $sql -> prepare ( " DELETE FROM `voice_server_backup` WHERE `id`=? AND `uid`=? AND `resellerid`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query5 -> execute ( array ( $row4 [ 'id' ], $ts3userid , $resellerid ));
2013-08-28 22:47:04 +02:00
$backupfolder = 'backups/virtualserver_' . $localserverid . '/' ;
$delcmd = 'cd ' . $folders . ' && function backup () { nice -n +19 rm -f ' . $backupfolder . $row4 [ 'id' ] . '.tar.bz2; }; backup& ' ;
2013-10-05 13:39:56 +02:00
$shell = ssh2_exec ( $ssh2 , $delcmd );
2013-08-28 22:47:04 +02:00
}
}
$rawsnapshot = $connection -> Snapshotcreate ( $localserverid );
$snapshot = gzcompress ( $rawsnapshot , 9 );
2013-09-29 15:29:58 +02:00
$query4 = $sql -> prepare ( " INSERT INTO `voice_server_backup` (`sid`,`uid`,`name`,`snapshot`,`date`,`resellerid`) VALUES(?,?,?,?,NOW(),?) " );
2013-10-05 13:39:56 +02:00
$query4 -> execute ( array ( $ts3id , $ts3userid , $name , $snapshot , $resellerid ));
2013-09-29 15:29:58 +02:00
$query4 = $sql -> prepare ( " SELECT `id` FROM `voice_server_backup` WHERE `sid`=? AND `uid`=? AND `resellerid`=? ORDER BY `id` DESC LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query4 -> execute ( array ( $ts3id , $ts3userid , $resellerid ));
2013-08-28 22:47:04 +02:00
foreach ( $query4 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row4 ) {
$filefolder = 'files/virtualserver_' . $localserverid . '/' ;
$backupfolder = 'backups/virtualserver_' . $localserverid . '/' ;
$createcmd = 'cd ' . $folders . ' && function backup () { mkdir -p ' . $backupfolder . ' && nice -n +19 tar cfj ' . $backupfolder . $row4 [ 'id' ] . '.tar.bz2 ' . $filefolder . '; }; backup& ' ;
2013-10-05 13:39:56 +02:00
$shell = ssh2_exec ( $ssh2 , $createcmd );
2013-10-03 12:49:13 +02:00
print " Creating backup for ts3 server: " . $row3 [ 'ip' ] . ':' . $row3 [ 'port' ] . " \r \n " ;
2013-08-28 22:47:04 +02:00
usleep ( 500000 );
}
}
}
}
}
$ssh2 = null ;
$connection -> CloseConnection ();
usleep ( 500000 );
} else {
print " Error: Cannot connect to masterserver $queryip\r\n " ;
}
}
}
2013-10-03 12:49:13 +02:00
$currenttime = strtolower ( date ( 'D' , strtotime ( " $resellerstimezone hour " ))) . '_' . date ( 'G' , strtotime ( " $resellerstimezone hour " ));
2013-08-29 23:37:49 +02:00
$query1 = $sql -> prepare ( " SELECT `id` FROM `rserverdata` WHERE `active`='Y' " );
$query1 -> execute ();
foreach ( $query1 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row1 ) {
2013-10-05 13:39:56 +02:00
$rootID = $row1 [ 'id' ];
2013-09-29 15:29:58 +02:00
$cmds = array ();
$query2 = $sql -> prepare ( " SELECT *,AES_DECRYPT(`ftppassword`,?) AS `decryptedftppass`,AES_DECRYPT(`ppassword`,?) AS `decryptedppassword` FROM `gsswitch` WHERE `rootID`=? AND `resellerid`=? AND `active`='Y' AND `lendserver`='N' ORDER BY `userid`,`serverid` " );
2013-10-05 13:39:56 +02:00
$query2 -> execute ( array ( $aeskey , $aeskey , $rootID , $resellerid ));
2013-08-29 23:37:49 +02:00
foreach ( $query2 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row2 ) {
unset ( $restart );
2013-10-05 13:39:56 +02:00
$serverip = $row2 [ 'serverip' ];
$gsport = $row2 [ 'port' ];
$gsswitchID = $row2 [ 'id' ];
$useridID = $row2 [ 'userid' ];
$newlayout = $row2 [ 'newlayout' ];
2013-10-03 12:49:13 +02:00
$gsfolder = $serverip . '_' . $gsport ;
2013-10-05 13:39:56 +02:00
$ftppass = $row2 [ 'decryptedftppass' ];
$decryptedftppass = $row2 [ 'decryptedppassword' ];
$protected_old = $row2 [ 'protected' ];
2013-10-03 16:48:54 +02:00
if ( $row2 [ 'serverid' ] == null ) {
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT `id` FROM `serverlist` WHERE `switchID`=? LIMIT 1 " );
2013-08-29 23:37:49 +02:00
$query3 -> execute ( array ( $gsswitchID ));
$runID_old = $query3 -> fetchColumn ();
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " UPDATE `gsswitch` SET `serverid`=? WHERE `id`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $runID_old , $gsswitchID ));
2013-08-29 23:37:49 +02:00
} else {
2013-10-05 13:39:56 +02:00
$runID_old = $row2 [ 'serverid' ];
2013-08-29 23:37:49 +02:00
}
2013-10-03 12:49:13 +02:00
$server = $serverip . ':' . $gsport ;
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT * FROM `gserver_restarts` WHERE `switchID`=? AND `restarttime`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $gsswitchID , $currenttime ));
2013-08-28 22:47:04 +02:00
foreach ( $query3 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row3 ) {
2013-10-05 13:39:56 +02:00
$template = $row3 [ 'template' ];
$anticheat = $row3 [ 'anticheat' ];
$shorten = $row3 [ 'gsswitch' ];
$map = $row3 [ 'map' ];
$mapGroup = $row3 [ 'mapGroup' ];
$protected = $row3 [ 'protected' ];
$restart = $row3 [ 'restart' ];
$backup = $row3 [ 'backup' ];
$worldsafe = $row3 [ 'worldsafe' ];
$upload = $row3 [ 'upload' ];
2013-08-29 23:37:49 +02:00
}
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT `active`,`cname` FROM `userdata` WHERE `id`=? AND `resellerid`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $row2 [ 'userid' ], $resellerid ));
2013-08-29 23:37:49 +02:00
foreach ( $query3 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row3 ) {
2013-10-05 13:39:56 +02:00
$user_active = $row3 [ 'active' ];
$SSH2customer = $row3 [ 'cname' ];
2013-08-29 23:37:49 +02:00
}
2013-10-03 12:49:13 +02:00
if ( $newlayout == 'Y' ) $SSH2customer = $SSH2customer . '-' . $gsswitchID ;
if ( isset ( $restart ) and $user_active == 'Y' ) {
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT s.`id`,s.`upload`,s.`map`,s.`servertemplate`,s.`mapGroup`,t.`qstat` FROM `serverlist` s LEFT JOIN `servertypes` t ON s.`servertype`=t.`id` WHERE t.`shorten`=? AND s.`switchID`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $shorten , $gsswitchID ));
2013-08-29 23:37:49 +02:00
foreach ( $query3 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row3 ) {
2013-10-05 13:39:56 +02:00
$runID = $row3 [ 'id' ];
$qstat = $row3 [ 'qstat' ];
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " UPDATE `gsswitch` SET `serverid`=? WHERE `id`=? AND `resellerid`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $runID , $gsswitchID , $resellerid ));
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " UPDATE `serverlist` SET `anticheat`=?,`map`=?,`mapGroup`=?,`servertemplate`=? WHERE `id`=? AND `resellerid`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $anticheat , $map , $mapGroup , $template , $runID , $resellerid ));
2013-09-29 15:29:58 +02:00
$shortens = '' ;
2013-10-03 12:49:13 +02:00
if ( $restart == 'Y' ) {
2013-08-29 23:37:49 +02:00
unset ( $newProtected );
2013-10-03 12:49:13 +02:00
if ( $protected == 'Y' and $protected_old == 'N' ) {
2013-10-05 13:39:56 +02:00
$tmp = gsrestart ( $gsswitchID , 'so' , $aeskey , $resellerid );
if ( is_array ( $tmp )) foreach ( $tmp as $t ) $cmds [] = $t ;
2013-08-29 23:37:49 +02:00
echo " Stopping unprotected server: $server\r\n " ;
$gamestring = '1_' . $shorten ;
$randompass = passwordgenerate ( 20 );
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " UPDATE `gsswitch` SET `ppassword`=AES_ENCRYPT(?,?) WHERE `id`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $randompass , $aeskey , $gsswitchID ));
2013-10-03 12:49:13 +02:00
$cmds [] = './control.sh mod ' . $SSH2customer . ' ' . $ftppass . ' ' . $randompass ;
2013-08-29 23:37:49 +02:00
$SSH2customer = $SSH2customer . '-p' ;
$cmds [] = " sudo -u ${ SSH2customer } ./control.sh reinstserver ${ SSH2customer } ${ gamestring } ${ gsfolder } protected " ;
echo 'Reinstall protected server: ' . $server . " \r \n " ;
2013-10-03 12:49:13 +02:00
$newProtected = true ;
2013-08-29 23:37:49 +02:00
}
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " UPDATE `gsswitch` SET `serverid`=?,`stopped`='N',`protected`=? WHERE `id`=? AND `resellerid`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $runID , $protected , $gsswitchID , $resellerid ));
2013-08-29 23:37:49 +02:00
if ( ! isset ( $newProtected )) {
echo 'Restarting server: ' . $server . " \r \n " ;
2013-10-05 13:39:56 +02:00
$tmp = gsrestart ( $gsswitchID , 're' , $aeskey , $resellerid );
if ( is_array ( $tmp )) foreach ( $tmp as $t ) $cmds [] = $t ;
2013-08-29 23:37:49 +02:00
}
2013-10-03 12:49:13 +02:00
} else if ( $restart == 'N' and $qstat == 'minecraft' and $worldsafe == 'Y' ) {
2013-08-29 23:37:49 +02:00
$cmds [] = " sudo -u ${ SSH2customer } ./control.sh mc_ws $gsfolder " ;
echo " Minecraft worlsafe: $server\r\n " ;
2013-10-03 12:49:13 +02:00
} else if ( $restart == 'N' and $qstat == 'a2s' and ( $uploadtype == '2' or $uploadtype == '3' ) and $upload == 'Y' ) {
2013-10-05 13:39:56 +02:00
$tmp = gsrestart ( $gsswitchID , 'du' , $aeskey , $resellerid );
if ( is_array ( $tmp )) foreach ( $tmp as $t ) $cmds [] = $t ;
2013-08-28 22:47:04 +02:00
}
2013-10-03 12:49:13 +02:00
if ( $backup == 'Y' ) {
2013-09-29 15:29:58 +02:00
$query3 = $sql -> prepare ( " SELECT AES_DECRYPT(`ftpbackup`,?) AS `backup` FROM `userdata` WHERE `id`=? LIMIT 1 " );
2013-10-05 13:39:56 +02:00
$query3 -> execute ( array ( $aeskey , $useridID ));
2013-08-29 23:37:49 +02:00
$ftpbackup = $query3 -> fetchColumn ();
2013-09-29 15:29:58 +02:00
$shortens = array ();
$query3 = $sql -> prepare ( " SELECT DISTINCT(t.`shorten`) FROM `serverlist` s LEFT JOIN `servertypes` t ON s.`servertype`=t.`id` WHERE s.`switchID`=? " );
2013-08-29 23:37:49 +02:00
$query3 -> execute ( array ( $gsswitchID ));
foreach ( $query3 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row3 ) {
2013-10-05 13:39:56 +02:00
$shortens [] = $row3 [ 'shorten' ];
2013-08-29 23:37:49 +02:00
}
2013-10-05 13:39:56 +02:00
$cmds [] = " sudo -u ${ SSH2customer } ./control.sh backup ${ gsfolder } \" " . implode ( ' ' , $shortens ) . " \" \" ${ webhostdomain } \" \" ${ ftpbackup } \" " ;
2013-08-29 23:37:49 +02:00
echo " Backup started for $server\r\n " ;
2013-08-28 22:47:04 +02:00
}
}
2013-08-29 23:37:49 +02:00
} else {
echo " $currenttime Not the restart time for $server\r\n " ;
2013-08-28 22:47:04 +02:00
}
}
2013-10-05 13:39:56 +02:00
if ( count ( $cmds ) > 0 ) ssh2_execute ( 'gs' , $rootID , $cmds );
2013-08-28 22:47:04 +02:00
}
echo " Executing Updates if neccessary \r \n " ;
2013-09-29 15:29:58 +02:00
$query2 = $sql -> prepare ( " SELECT `id` FROM `rserverdata` WHERE `resellerid`=? AND `updates`!=3 AND (`updateMinute`=0 OR `updateMinute` IS NULL) " );
2013-08-28 22:47:04 +02:00
$query2 -> execute ( array ( $resellerid ));
foreach ( $query2 -> fetchAll ( PDO :: FETCH_ASSOC ) as $row2 ) {
2013-10-05 13:39:56 +02:00
$rootServer = new masterServer ( $row2 [ 'id' ], $aeskey );
2013-08-28 22:47:04 +02:00
$rootServer -> collectData ();
2013-08-30 22:01:38 +02:00
$sshcmd = ( 4 == $stunde ) ? $rootServer -> returnCmds ( 'update' , 'all' ) : $rootServer -> returnCmds ();
2013-08-28 22:47:04 +02:00
if ( $rootServer -> sshcmd !== null ) {
echo " Starting updates for " . $rootServer -> sship . " \r \n " ;
2013-10-12 08:40:09 +02:00
if ( ssh2_execute ( 'gs' , $row2 [ 'id' ], $rootServer -> sshcmd ) !== false ) {
2013-08-28 22:47:04 +02:00
$rootServer -> setUpdating ();
echo " Updater started for " . $rootServer -> sship . " \r \n " ;
} else {
echo " Updating failed for: " . $rootServer -> sship . " \r \n " ;
}
}
unset ( $rootServer );
}
}
2013-10-05 13:39:56 +02:00
2013-10-03 12:49:13 +02:00
$newsInclude = true ;
$printToConsole = true ;
2013-08-28 22:47:04 +02:00
print " Check for new news feeds \r \n " ;
2013-09-27 08:22:09 +02:00
include ( EASYWIDIR . '/stuff/feeds_function.php' );
2013-10-05 13:39:56 +02:00
if ( isset ( $template_file )) {
print $template_file . " \r \n " ;
2013-08-28 22:47:04 +02:00
}
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
print " Cleaning Logs \r \n " ;
2013-09-29 15:29:58 +02:00
$query = $sql -> prepare ( " DELETE FROM `userlog` WHERE DATEDIFF(NOW(),`logdate`)>31 " );
2013-08-28 22:47:04 +02:00
$query -> execute ();
2013-09-29 15:29:58 +02:00
$query = $sql -> prepare ( " DELETE FROM `mail_log` WHERE DATEDIFF(NOW(),`date`)>31 " );
2013-08-28 22:47:04 +02:00
$query -> execute ();
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
print " Reparing tables \r \n " ;
2013-09-29 15:29:58 +02:00
$query = $sql -> prepare ( " REPAIR TABLE `addons`,`addons_installed`,`api_external_auth`,`api_ips`,`api_settings`,`badips`,`dhcpdata`,`eac`,`easywi_version`,`gserver_restarts`,`gsstatus`,`gsswitch`,`imprints`,`jobs`,`lendedserver`,`lendsettings`,`lendstats`,`mail_log`,`mysql_external_dbs`,`mysql_external_servers`,`page_pages`,`page_pages_text`,`page_settings`,`page_terms`,`page_terms_used`,`qstatshorten`,`resellerdata`,`resellerimages`,`rserverdata`,`rservermasterg`,`serverlist`,`servertypes`,`settings`,`test`,`tickets`,`ticket_topics`,`traffic_data`,`traffic_data_day`,`traffic_settings`,`userdata`,`usergroups`,`userlog`,`userpermissions`,`virtualcontainer`,`virtualhosts`,`voice_masterserver`,`voice_server`,`voice_server_backup`,`voice_server_stats`,`voice_server_stats_hours`,`voice_stats_settings` " );
2013-08-28 22:47:04 +02:00
$query -> execute ();
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
print " Optimizing tables \r \n " ;
2013-09-29 15:29:58 +02:00
$query = $sql -> prepare ( " OPTIMIZE TABLE `addons`,`addons_installed`,`api_external_auth`,`api_ips`,`api_settings`,`badips`,`dhcpdata`,`eac`,`easywi_version`,`gserver_restarts`,`gsstatus`,`gsswitch`,`imprints`,`jobs`,`lendedserver`,`lendsettings`,`lendstats`,`mail_log`,`mysql_external_dbs`,`mysql_external_servers`,`page_pages`,`page_pages_text`,`page_settings`,`page_terms`,`page_terms_used`,`qstatshorten`,`resellerdata`,`resellerimages`,`rserverdata`,`rservermasterg`,`serverlist`,`servertypes`,`settings`,`test`,`tickets`,`ticket_topics`,`traffic_data`,`traffic_data_day`,`traffic_settings`,`userdata`,`usergroups`,`userlog`,`userpermissions`,`virtualcontainer`,`virtualhosts`,`voice_masterserver`,`voice_server`,`voice_server_backup`,`voice_server_stats`,`voice_server_stats_hours`,`voice_stats_settings` " );
2013-08-28 22:47:04 +02:00
$query -> execute ();
2013-09-29 15:29:58 +02:00
$query = $sql -> prepare ( " UPDATE `settings` SET `lastCronReboot`=UNIX_TIMESTAMP() WHERE `resellerid`=0 LIMIT 1 " );
2013-08-28 22:47:04 +02:00
$query -> execute ();
2013-10-05 13:39:56 +02:00
2013-08-28 22:47:04 +02:00
} else {
header ( 'Location: login.php' );
die ( 'Statuscheck can only be run via console and or a cronjob' );
}