<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright (c) 2002 - 2011 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Title: Gr_Radiostatus v2 for PHP-Fusion 7
| Author: Ralf Thieme
| Webseite: www.granade.eu
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
require_once '../../maincore.php';

if (!checkrights('GRRS') || !defined('iAUTH') || $_GET['aid'] != iAUTH) { redirect(BASEDIR.'index.php'); }

include INFUSIONS.'gr_radiostatus_panel/infusion.php';
$result = dbquery("SELECT inf_id, inf_version FROM ".DB_INFUSIONS." WHERE inf_folder='".$inf_folder."'");
if (dbrows($result)) {
	$data = dbarray($result);
	$install_version = str_replace('.', '', $data['inf_version']);
	if (str_replace('.', '', $inf_version) > $install_version) {
		if (20 > $install_version) {
			$result2 = dbquery("CREATE TABLE ".DB_GR_RADIOSTATUS."_new (
			rs_id							SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
			rs_name						VARCHAR(20) NOT NULL DEFAULT 'Stream',
			rs_ip							VARCHAR(255) NOT NULL DEFAULT '0.0.0.0',
			rs_port						VARCHAR(5) NOT NULL DEFAULT '8000',
			rs_pw							VARCHAR(255) NOT NULL DEFAULT '',
			rs_apw						VARCHAR(255) NOT NULL DEFAULT '',
			rs_ps							VARCHAR(255) NOT NULL DEFAULT '',
			rs_tele						TEXT NOT NULL,
			rs_flash					VARCHAR(255) NOT NULL DEFAULT '',
			rs_cache					VARCHAR(255) NOT NULL DEFAULT '',
			rs_usertyp				TINYINT(1) UNSIGNED NOT NULL DEFAULT '1',
			rs_theme					TINYINT(1) UNSIGNED NOT NULL DEFAULT '1',
			rs_panel					TINYINT(1) UNSIGNED NOT NULL DEFAULT '1',
			rs_order					SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0',
			rs_status					TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
			rs_status_gb			TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
			rs_status_gb_dj		TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
			rs_gb_max					SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0',
			rs_access					TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
			rs_access_l				TINYINT(3) UNSIGNED NOT NULL DEFAULT '102',
			rs_access_a				TINYINT(3) UNSIGNED NOT NULL DEFAULT '102',
			rs_access_gb			TINYINT(3) UNSIGNED NOT NULL DEFAULT '102',
			PRIMARY KEY (rs_id)
			) ENGINE=MyISAM;");
			$result_old = dbquery("SELECT * FROM ".DB_GR_RADIOSTATUS." ORDER BY rs_order");
			if (dbrows($result_old)) {
				$i = 1;
				while($data_old = dbarray($result_old)) {
					$result2 = dbquery("INSERT INTO ".DB_GR_RADIOSTATUS."_new (rs_name, rs_ip, rs_port, rs_pw, rs_status_gb, rs_ps, rs_tele, rs_usertyp, rs_status, rs_order, rs_access_gb, rs_access) VALUES ('".$data_old['rs_name']."', '".$data_old['rs_ip']."', '".$data_old['rs_port']."', '".$data_old['rs_pw']."', '".$data_old['rs_gb']."', '".$data_old['rs_ps']."', '".$data_old['rs_tele']."', '".$data_old['rs_usertyp']."', '".$data_old['rs_status']."', '".$i."', '".$data_old['rs_gaccess']."', '".$data_old['rs_access']."')");
					$i++;
				}
				@mysql_free_result($result_old);
			}
			$result2 = dbquery("DROP TABLE IF EXISTS ".DB_GR_RADIOSTATUS);
			$result2 = dbquery("RENAME TABLE ".DB_GR_RADIOSTATUS."_new TO ".DB_GR_RADIOSTATUS);
			
			$result2 = dbquery("DROP TABLE IF EXISTS ".DB_GR_RADIOSTATUS_GRUSSBOX);
			$result2 = dbquery("CREATE TABLE ".DB_GR_RADIOSTATUS_GRUSSBOX." (
			rsgb_id						BIGINT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
			rsgb_stream				SMALLINT(5) NOT NULL DEFAULT '1',
			rsgb_user_ip			VARCHAR(255) NOT NULL DEFAULT '0.0.0.0',
			rsgb_user_name		VARCHAR(255) NOT NULL DEFAULT '',
			rsgb_user_ort			VARCHAR(255) NOT NULL DEFAULT '',
			rsgb_title				VARCHAR(255) NOT NULL DEFAULT '',
			rsgb_interpret		VARCHAR(255) NOT NULL DEFAULT '',
			rsgb_gruss				TEXT NOT NULL,
			rsgb_time					INT(10) NOT NULL DEFAULT '0',
			rsgb_status				TINYINT(1) NOT NULL DEFAULT '0',
			PRIMARY KEY (rsgb_id)
			) ENGINE=MyISAM;");
			
			$result2 = dbquery("CREATE TABLE ".DB_GR_RADIOSTATUS_ACTION." (
			rsa_stream				SMALLINT(5) NOT NULL DEFAULT '1',
			rsa_user_id				MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '1',
			rsa_sction				TINYINT(1) UNSIGNED NOT NULL DEFAULT '1',
			rsa_time					INT(10) UNSIGNED NOT NULL DEFAULT '0'
			) ENGINE=MyISAM;");
			
			$link_order = dbresult(dbquery("SELECT MAX(link_order) FROM ".DB_SITE_LINKS),0) + 1;
			$result2 = dbquery("INSERT INTO ".DB_SITE_LINKS." (link_name, link_url, link_visibility, link_position, link_window, link_order) VALUES ('".$inf_sitelink[1]['title']."', '".str_replace("../","",INFUSIONS).$inf_folder."/".$inf_sitelink[1]['url']."', '".$inf_sitelink[1]['visibility']."', '1', '0', '".$link_order."')");
		}
		if (25 > $install_version) {
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS." ADD rs_status_boxen TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' AFTER rs_status_gb_dj");
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS." ADD rs_gb_max_user SMALLINT(5) UNSIGNED NOT NULL DEFAULT '1' AFTER rs_gb_max");
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS." ADD rs_gb_popup TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' AFTER rs_gb_max_user");
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS." ADD rs_access_gb_user TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER rs_access_gb");
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS_GRUSSBOX." ADD rsgb_user_id MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0' AFTER rsgb_stream");
			
			$result2 = dbquery("CREATE TABLE ".DB_GR_RADIOSTATUS_TITLE." (
			rst_interpret			VARCHAR(255) NOT NULL DEFAULT '',
			rst_title					VARCHAR(255) NOT NULL DEFAULT '',
			rst_time					INT(10) UNSIGNED NOT NULL DEFAULT '0'
			) ENGINE=MyISAM;");
			
			$link_order = dbresult(dbquery("SELECT MAX(link_order) FROM ".DB_SITE_LINKS),0) + 1;
			$result2 = dbquery("INSERT INTO ".DB_SITE_LINKS." (link_name, link_url, link_visibility, link_position, link_window, link_order) VALUES ('".$locale['grrs_link2']."', '".str_replace("../","",INFUSIONS)."gr_radiostatus_boxen/', '0', '1', '0', '".$link_order."')");
		}
		if (26 > $install_version) {
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS." ADD rs_server_typ TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' AFTER rs_id");
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS." ADD rs_server_id SMALLINT(5) UNSIGNED NOT NULL DEFAULT '1' AFTER rs_server_typ");
			
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS." ADD INDEX rs_panel (rs_panel, rs_access, rs_status)");
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS." ADD INDEX rs_boxen (rs_access, rs_status, rs_status_boxen)");
			$result2 = dbquery("ALTER TABLE ".DB_GR_RADIOSTATUS." ADD INDEX rs_boxen_id (rs_id, rs_access, rs_status, rs_status_boxen)");
		}
		$result2 = dbquery("UPDATE ".DB_INFUSIONS." SET inf_version='".$inf_version."' WHERE inf_id='".$data['inf_id']."'");
	}
}
@unlink(FUSION_SELF);
if (file_exists(FUSION_SELF)) {
	echo 'Bitte die "update.php" vom Server l&ouml;schen. Danach kannst du diesen Link benutzen: <a href="'.INFUSIONS.'gr_radiostatus_panel/gr_radiostatus_admin.php'.$aidlink.'">Zum Radiostatus Adminbereich</a>';
} else {
	redirect(INFUSIONS.'gr_radiostatus_panel/gr_radiostatus_admin.php'.$aidlink);
}
?>