Location: PHPKode > scripts > Minecraft Admin > minecraft-admin/includes/mc-core/mc-core-functions.php
<?php
function parse_minecraft_output($parse, $time = true)
{
		// var_dump($parse);
		$parse = str_replace("", "", $parse);
		$parse = str_replace("", "", $parse);
		if (!$time) {
				$parse = explode('[INFO]', $parse);
				$zahl  = count($parse) - 1;
				$parse = str_replace("", "", $parse[$zahl]);
				$parse = explode('[WARNING]', $parse);
				$zahl  = count($parse) - 1;
				$parse = str_replace("[m", "", $parse[$zahl]);
		} //!$time
		$parse = str_replace("[m", "", $parse);
		$parse = str_replace("", "", $parse);
		$parse = str_replace("", "", $parse);
		$parse = str_replace("", "", $parse);
		$parse = str_replace("[37;1m", "", $parse);
		$parse = str_replace("[m", "", $parse);
		$parse = str_replace("<", "[", $parse);
		$parse = str_replace(">", "]", $parse);
		return $parse;
}
function create_installer($options)
{
		$installer = '
#!/bin/bash

#Diese Datei wurde automatisch generiert von MC Admin fuer Wordpress um ' . date('Y-m-d H:i:s') . '. Fuer mehr Informationen besuche http://profenter.de/portal/gruppen/minecraft-admin-fur-wordpress/frontpage/
#This file was automatically generated by MC Admin for WordPress at ' . date('Y-m-d H:i:s') . ' . For more information visit http://profenter.de/portal/gruppen/minecraft-admin-fur-wordpress/frontpage/

echo \'' . __('Welcome to Minecraft installer. This installer installs all required packages and files. To abort execution, press CTRL + C.', 'minecraft-admin') . '\'
read -p "' . __('press [Enter] to continue...', 'minecraft-admin') . '" readEnterKey
mv ./minecraft_init_d.sh ./minecraft
cp ./minecraft /etc/init.d/
rm ./minecraft
chmod a+x /etc/init.d/minecraft
chmod 0777  /etc/init.d/
chmod 0777  /etc/init.d/minecraft
chown ' . $options['webservernutzer'] . ':' . $options['webservernutzer'] . ' -R /etc/init.d/minecraft
update-rc.d minecraft defaults
mkdir ' . $options['pfad'] . '
mkdir ' . $options['backup'] . '
cd ' . $options['pfad'] . '
wget \'' . $options["down"] . '\' -O ' . $options['jar'] . '
chown ' . $options['webservernutzer'] . ':' . $options['webservernutzer'] . ' -R ' . $options['pfad'] . '
echo \'' . __('permissions was succssesfull set', 'minecraft-admin') . '\'
';
		if ($options["os"] == "deb") {
				$inst_befehl = "apt-get install screen openjdk-6-jre";
		} //$options["os"] == "deb"
		elseif ($options["os"] == "yum") {
				$inst_befehl = "yum install screen java-1.6.0-openjdk";
		} //$options["os"] == "yum"
				elseif ($options["os"] == "no" or empty($options["os"])) {
				$inst_befehl = "";
		} //$options["os"] == "no" or empty($options["os"])
		$installer .= $inst_befehl . '
echo \'' . __('Starting Minecraft .....', 'minecraft-admin') . '\'
echo \'' . __('you can now login', 'minecraft-admin') . '\'
/etc/init.d/minecraft start';
		return $installer;
}
function MC_installer($os = "")
{
		$installer = '#!/bin/bash
echo \'' . __('Welcome to Minecraft installer. This installer installs all required packages and files. To abort execution, press CTRL + C.', 'minecraft-admin') . '\'
read -p "' . __('press [Enter] to continue...', 'minecraft-admin') . '" readEnterKey
rm /etc/init.d/minecraft
mv ' . MC_CORE_MINECRAFT_DIR . 'initd.sh /etc/init.d/minecraft
chmod a+x /etc/init.d/minecraft
chmod 0777  /etc/init.d/
chmod 0777  /etc/init.d/minecraft
chown ' . get_current_user() . ':' . get_current_user() . ' /etc/init.d/minecraft
update-rc.d minecraft defaults
touch ' . MC_CORE_MINECRAFT_DIR . '.installed
';
		if ($os == "deb") {
				$inst_befehl = "apt-get install screen openjdk-6-jre-headless";
		} //$options["os"] == "deb"
		elseif ($os == "yum") {
				$inst_befehl = "yum install screen java-1.6.0-openjdk";
		} //$options["os"] == "yum"
				elseif ($os == "no" or empty($os)) {
				$inst_befehl = 'echo \'' . __('Install now Java!', 'minecraft-admin') . '\'';
		} //$options["os"] == "no" or empty($options["os"])
		$installer .= $inst_befehl;
		$created = create_file('installer.sh', $installer, MC_CORE_MINECRAFT_DIR);
		return $created;
}
function MC_initd()
{
		$script  = '#!/bin/bash
# /etc/init.d/minecraft
# version ' . MC_VERSION . ' ' . date("Y-m-d") . ' (YYYY-MM-DD)

### BEGIN INIT INFO
# Provides:minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop:$local_fs $remote_fs
# Should-Start:$network
# Should-Stop: $network
# Default-Start:2 3 4 5
# Default-Stop:0 1 6
# Short-Description: Minecraft server
# Description: the start/stop file by minecraft admin for wordpress
### END INIT INFO


MCPATH=\'' . MC_CORE_MINECRAFT_DIR . '\'

BLOG=$1
SERVER=$2
mc_start() {
sh ${MCPATH}${BLOG}/${SERVER}/initd.sh start
}
mc_stop() {
sh ${MCPATH}${BLOG}/${SERVER}/initd.sh stop
}
mc_backup() {
sh ${MCPATH}${BLOG}/${SERVER}/initd.sh backup
}
mc_status() {
sh ${MCPATH}${BLOG}/${SERVER}/initd.sh status
}
mc_command() {
sh ${MCPATH}${BLOG}/${SERVER}/initd.sh command
}
case "$3" in
start)
 mc_start
 ;;
command)
    if [ $# -gt 3 ]; then
      mc_command "$4"
     else
echo "' . __('Please type in a command (try \'help\' or ?)', 'minecraft-admin') . '"
 fi

 ;;

status)
 mc_status
 ;;
stop)
 mc_stop
 ;;
restart)
 mc_stop
 mc_start
 ;;
backup)
 mc_backup
 ;;
*)
echo "' . __('Use:', 'minecraft-admin') . ' $0 \"blog_id\" \"server_id\" {start|stop|backup|status|restart|command \"server command\"}"
  exit 1
  ;;
esac

exit 0';
		$created = create_file('initd.sh', $script, MC_CORE_MINECRAFT_DIR);
		return $created;
}
function mc_core_info($frage)
{
		switch ($frage) {
				case ("CPU"):
						return __("Here you can set how much CPU's are used by Minecraft. If you do not know how many you have, enter &bdquo;1&rdquo;.", 'minecraft-admin');
						break;
				case ("RAM"):
						return __("Here you can set how much RAM (memory) is used by Minecraft. If you do not know how much you have, ask your hoster. If you do not know how much memory is reasonable, leave the default value of &bdquo;1024M&rdquo;", 'minecraft-admin');
						break;
				case ("Pfad"):
						return __("You can find the minecraft server under this path.", 'minecraft-admin');
						break;
				case ("jar"):
						return __("Gebe hier den Name der Datei ein, die zum Start des Servers ausgeführt werden soll.", 'minecraft-admin');
						break;
				case ("users"):
						return __("Enter the names of WordPress users (separated by commas) that have access to this interface. Administrators always have access.", 'minecraft-admin');
						break;
				case ("worlds"):
						return __("Enter the names of all Minecraft worlds. They are needed to use the backup system.", 'minecraft-admin');
						break;
				case ("webserver-nutzer"):
						return __("Enter the name of the web server user.", 'minecraft-admin');
						break;
				case ("log"):
						return __("Enter the number of logs you want to view.", 'minecraft-admin');
						break;
				case ("backup"):
						return __("You can find the minecraft backup directory under this path", 'minecraft-admin');
						break;
				case ("server"):
						return __("You can choose whether you want an original Minecraft server or a Bukkit server.", 'minecraft-admin');
						break;
		} //$frage
}
function create_file($datei, $inhalt, $ordner)
{
		$fp = @fopen($ordner . $datei, "w+");
		@fwrite($fp, $inhalt);
		@fclose($fp);
		if (file_exists($ordner . $datei)) {
				return sprintf(__("The file '%s' was succssesfull created.<br>", 'minecraft-admin'), $datei);
		} //file_exists($ordner . $datei)
		else {
				return sprintf(__("The directory isn't writeable. Create the file '%s' into the directory '%s' and copy the following text into this file.<br>", 'minecraft-admin'), $datei, $ordner);
		}
}
function generate_init($options)
{
		global $blog_id;
		$script = '#!/bin/bash
# /etc/init.d/minecraft
# version ' . MC_VERSION . ' ' . date("Y-m-d") . ' (YYYY-MM-DD)

### BEGIN INIT INFO
# Provides:minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop:$local_fs $remote_fs
# Should-Start:$network
# Should-Stop: $network
# Default-Start:2 3 4 5
# Default-Stop:0 1 6
# Short-Description: Minecraft server
# Description: the start/stop file for a minecraft server by minecraft admin for wordpress
### END INIT INFO

#Settings
#Diese Datei wurde automatisch generiert von MC Admin fuer Wordpress um ' . date('Y-m-d H:i:s') . '. Fuer mehr Informationen besuche http://profenter.de/portal/gruppen/minecraft-admin-fur-wordpress/frontpage/
#This file was automatically generated by MC Admin for WordPress at ' . date('Y-m-d H:i:s') . ' . For more information visit http://profenter.de/portal/gruppen/minecraft-admin-fur-wordpress/frontpage/
USERNAME=\'' . get_current_user() . '\'
WORLDS=\'' . $options->worlds . '\'
MCPATH=\'' . $options->pfad . '\'
BACKUPPATH=\'' . $options->pfad . 'backup\'
SCREEN="minecraft_' . $blog_id . '_' . $options->id . '"
INVOCATION="java -Xmx' . $options->ram . ' -Xms' . $options->ram . ' -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=' . $options->cpu . ' -XX:+AggressiveOpts -jar server.jar nogui"

ME=`whoami`
as_user() {
if [ $ME = $USERNAME ] ; then
 bash -c "$1"
else
 su - $USERNAME -c "$1"
fi
}

mc_start() {
  if  pgrep -u $USERNAME -f $SCREEN > /dev/null
then
 echo "$SCREEN' . __(' is already running!', 'minecraft-admin') . '"
else
 echo "' . __('starts', 'minecraft-admin') . ' $SCREEN..."
 cd $MCPATH
 as_user "cd $MCPATH && screen -dmS $SCREEN $INVOCATION"
 sleep 7
 if pgrep -u $USERNAME -f $SCREEN > /dev/null
 then
echo "$SCREEN ' . __('is now running.', 'minecraft-admin') . '"
 else
echo "' . sprintf(__('Error! %s couldn\'t be started!', 'minecraft-admin'), '$SCREEN') . '"
 fi
fi
}

mc_saveoff() {
if pgrep -u $USERNAME -f $SCREEN > /dev/null
then
 echo "$SCREEN ' . __('is running... save the worlds.', 'minecraft-admin') . '"
 as_user "screen -p 0 -S $SCREEN -X eval \'stuff \"say ' . __('SERVER BACKUP STARTED. Server goes into read mode...', 'minecraft-admin') . '\"\015\'"
 as_user "screen -p 0 -S $SCREEN -X eval \'stuff \"save-off\"\015\'"
 as_user "screen -p 0 -S $SCREEN -X eval \'stuff \"save-all\"\015\'"
 sync
 sleep 10
else
 echo "$SCREEN ' . __('doesn\'t run. Nothing to save.', 'minecraft-admin') . '"
fi
}

mc_saveon() {
if pgrep -u $USERNAME -f $SCREEN > /dev/null
then
 echo "$SCREEN ' . __('runs... Activate the maps', 'minecraft-admin') . '"
 as_user "screen -p 0 -S $SCREEN -X eval \'stuff \"save-on\"\015\'"
 as_user "screen -p 0 -S $SCREEN -X eval \'stuff \"say ' . __('SERVER BACKUP END. Server goes into write mode...', 'minecraft-admin') . '\"\015\'"
else
 echo "$SCREEN ' . __('doesn\'t run. Nothing to activate.', 'minecraft-admin') . '"
fi
}

mc_stop() {
if pgrep -u $USERNAME -f $SCREEN > /dev/null
then
 echo "' . __('stop', 'minecraft-admin') . ' $SCREEN"
 as_user "screen -p 0 -S $SCREEN -X eval \'stuff \"say ' . __('This server will be stop until 10 Sekunden. Save the maps...', 'minecraft-admin') . '\"\015\'"
 as_user "screen -p 0 -S $SCREEN -X eval \'stuff \"save-all\"\015\'"
 sleep 10
 as_user "screen -p 0 -S $SCREEN -X eval \'stuff \"stop\"\015\'"
 sleep 7
else
 echo "$SCREEN ' . __('doesn\'t run.', 'minecraft-admin') . '"
fi
if pgrep -u $USERNAME -f $SCREEN > /dev/null
then
 echo "' . sprintf(__('Error! %s couldn\'t be stopped!', 'minecraft-admin'), '$SCREEN') . '"
else
 echo "$SCREEN ' . __('was stopped.', 'minecraft-admin') . '"
fi
}
mc_backup() {
echo "' . __('remove old files...', 'minecraft-admin') . '"
as_user "cd $BACKUPPATH && find $BACKUPPATH -ctime +14 -exec rm -rf {} \;"
echo "' . __('save the minecraft worlds...', 'minecraft-admin') . '"
if [ -d $BACKUPPATH/${WORLD}_`date "+%Y.%m.%d_%H.%M"` ]
then
for i in 1 2 3 4 5 6
do
 if [ -d $BACKUPPATH/${WORLD}_`date "+%Y.%m.%d_%H.%M"`-$i ]
 then
continue
 else
as_user "cd $MCPATH && tar -czf $BACKUPPATH/_`date "+%Y.%m.%d_%H.%M"`-$i.tar.gz $WORLDS"
break
 fi
done
else
as_user "cd $MCPATH && tar -czf $BACKUPPATH/`date "+%Y.%m.%d_%H.%M"`.tar.gz $WORLDS"
echo "' . __('saved worlds', 'minecraft-admin') . '"
fi
echo "' . __('backup finished', 'minecraft-admin') . '"
}
mc_command() {
  command="$1";
  if pgrep -u $USERNAME -f $SCREEN > /dev/null
  then
    pre_log_len=`wc -l "$MCPATH/server.log" | awk \'{print $1}\'`
    as_user "screen -p 0 -S $SCREEN -X eval \'stuff \"$command\"\015\'"
    sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
    # print output
    # tail -n $[`wc -l "$MCPATH/server.log" | awk \'{print $1}\'`-$pre_log_len] "$MCPATH/server.log"
    tail -n 1 "$MCPATH/server.log"
  fi
}

#Start-Stop here
case "$1" in
start)
 mc_start
 ;;
stop)
 mc_stop
 ;;
restart)
 mc_stop
 mc_start
 ;;
backup)
 mc_saveoff
 mc_backup
 mc_saveon
 ;;
status)
 if pgrep -u $USERNAME -f $SCREEN > /dev/null
 then
echo "$SCREEN ' . __("runs.", 'minecraft-admin') . '"
 else
echo "$SCREEN ' . __("doesn't run.", 'minecraft-admin') . '"
    fi
    ;;
  command)
    if [ $# -gt 1 ]; then
      shift
mc_command "$*"
 else
echo "' . __('Please type in a command (try \'help\' or ?)', 'minecraft-admin') . '"
 fi
 ;;

*)
echo "' . __('Use:', 'minecraft-admin') . ' $0 {start|stop|backup|status|restart|command \"server command\"}"
  exit 1
  ;;
esac

exit 0';
		return create_file('initd.sh', $script, MC_CORE_MINECRAFT_DIR . $blog_id . '/' . $options->id . '/');
}
function lochen_verzeichnis($path)
{
		if (!is_dir($path)) {
				if (is_file($path)) {
						unlink($path);
				} //is_file($path)
				else {
						return -1;
				}
		} //!is_dir($path)
		$dir = @opendir($path);
		if (!$dir) {
				return -2;
		} //!$dir
		while (($entry = @readdir($dir)) !== false) {
				if ($entry == '.' || $entry == '..')
						continue;
				if (is_dir($path . '/' . $entry)) {
						$res = lochen_verzeichnis($path . '/' . $entry);
						if ($res == -1) {
								@closedir($dir);
								return -2;
						} //$res == -1
						else if ($res == -2) {
								@closedir($dir);
								return -2;
						} //$res == -2
						else if ($res == -3) {
								@closedir($dir);
								return -3;
						} //$res == -3
						else if ($res != 0) {
								@closedir($dir);
								return -2;
						} //$res != 0
				} //is_dir($path . '/' . $entry)
				else if (is_file($path . '/' . $entry) || is_link($path . '/' . $entry)) {
						$res = @unlink($path . '/' . $entry);
						if (!$res) {
								@closedir($dir);
								return -2;
						} //!$res
				} //is_file($path . '/' . $entry) || is_link($path . '/' . $entry)
				else {
						@closedir($dir);
						return -3;
				}
		} //($entry = @readdir($dir)) !== false
		@closedir($dir);
		$res = @rmdir($path);
		if (!$res) {
				return -2;
		} //!$res
		return 0;
}
function binary_multiples($size, $praefix = true, $short = true)
{
		if ($praefix === true) {
				if ($short === true) {
						$norm = array(
								'B',
								'kB',
								'MB',
								'GB',
								'TB',
								'PB',
								'EB',
								'ZB',
								'YB'
						);
				} //$short === true
				else {
						$norm = array(
								'Byte',
								'Kilobyte',
								'Megabyte',
								'Gigabyte',
								'Terabyte',
								'Petabyte',
								'Exabyte',
								'Zettabyte',
								'Yottabyte'
						);
				}
				$factor = 1000;
		} //$praefix === true
		else {
				if ($short === true) {
						$norm = array(
								'B',
								'KiB',
								'MiB',
								'GiB',
								'TiB',
								'PiB',
								'EiB',
								'ZiB',
								'YiB'
						);
				} //$short === true
				else {
						$norm = array(
								'Byte',
								'Kibibyte',
								'Mebibyte',
								'Gibibyte',
								'Tebibyte',
								'Pebibyte',
								'Exbibyte',
								'Zebibyte',
								'Yobibyte'
						);
				}
				$factor = 1024;
		}
		$count = count($norm) - 1;
		$x     = 0;
		while ($size >= $factor && $x < $count) {
				$size /= $factor;
				$x++;
		} //$size >= $factor && $x < $count
		$size = sprintf("%01.2f", $size) . ' ' . $norm[$x];
		return $size;
}
function cmp($a, $b)
{
		return strcmp($a["file"], $b["file"]);
}
function install_server($id, $info, $down = false)
{
		global $blog_id;
		$info->id = $id;
		if (!is_dir(MC_CORE_MINECRAFT_DIR . $blog_id . '/' . $id)) {
				mkdir(MC_CORE_MINECRAFT_DIR . $blog_id . '/' . $id);
		} //!is_dir(WP_PLUGIN_DIR . '/' . MC_PLUGIN_DIR . '/minecraft.dir/' . $blog_id . '/' . $ak_mine)
		if (!isset($info->server) OR $info->server == "orginal") {
				$url = 'https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar?';
		} //$info->server'] == "orginal"
		elseif ($info->server == "bukkit") {
				$url = 'http://dl.bukkit.org/latest-rb/craftbukkit.jar';
		} //$info->server'] == "bukkit"
		if ($down === true) {
				exec('wget \'' . $url . '\' -O ' . MC_CORE_MINECRAFT_DIR . $blog_id . '/' . $id . '/server.jar');
		} //$down === true
		echo generate_init($info);
}
function mc_url($component = "", $other_url = array())
{
		$other = "";
		if (!empty($other_url)) {
				$start = true;
				foreach ($other_url as $name => $parameter) {
						if (!$start) {
								$other .= "&amp;" . $name . "=" . $parameter;
						} //!$start
						else {
								$start = false;
								$other = $name . "=" . $parameter;
						}
				} //$other_url as $name => $parameter
		} //!empty($other_url)
		if (!empty($component)) {
				return site_url() . "/wp-admin/admin.php?page=minecraft-admin?mc=" . $component . "&amp;" . $other;
		} //!empty($component)
		else {
				return site_url() . "/wp-admin/admin.php?page=minecraft-admin&" . $other;
		}
}
function mc_url_menu_page($component = "", $other_url = array())
{
		if (!empty($other_url)) {
				$start = true;
				foreach ($other_url as $name => $parameter) {
						if (!$start) {
								$other .= "&amp;" . $name . "=" . $parameter;
						} //!$start
						else {
								$start = false;
								$other = $name . "=" . $parameter;
						}
				} //$other_url as $name => $parameter
		} //!empty($other_url)
		if (!empty($component)) {
				return "minecraft-admin?mc=" . $component;
		} //!empty($component)
		else {
				return "minecraft-admin";
		}
}
Return current item: Minecraft Admin