<commit_before><?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright (C) PHP-Fusion Inc
| https://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: upgrade.php
| Author: <NAME> (Digitanium)
+--------------------------------------------------------+
| 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";

pageAccess("U");

require_once THEMES."templates/admin_header.php";

if (file_exists(LOCALE.LOCALESET."admin/upgrade.php")) {
	include LOCALE.LOCALESET."admin/upgrade.php";
} else {
	include LOCALE."English/admin/upgrade.php";
}

if (file_exists(LOCALE.LOCALESET."setup.php")) {
	include LOCALE.LOCALESET."setup.php";
} else {
	include LOCALE."English/setup.php";
}

opentable($locale['400']);
echo "<div style='text-align:center'><br />\n";

if (str_replace(".", "", $settings['version']) < "90001") { // 90001 for testing purposes
	//echo "<form id='upgradeform' name='upgradeform' type='post' action='".FUSION_SELF.$aidlink."'>";
	// Might not have required resources(SECRET_KEY etc.) to call this function in case of an upgrade from 7 to 9
	echo openform('upgradeform', 'post', FUSION_SELF.$aidlink); 
	$content = "";
	switch (filter_input(INPUT_POST, 'stage', FILTER_VALIDATE_INT) ? : 1) {
		case 1:
		default:
			// Check if maintainance mode is enabled
			if (fusion_get_settings("maintenance") == 0) {
				if (isset($_POST['enable_maintenance'])) {
					dbquery("UPDATE ".DB_SETTINGS." SET settings_value='1' WHERE settings_name='maintenance'");
					addNotice('success', 'Maintenance mode was enabled, you can now continue with the upgrade process');

					redirect(FUSION_SELF.$aidlink);
				}

				$locale['enable_maint_warning'] = "Please put your website into Maintenance mode before continuing in order to avoid any issues that might occur during the upgrade proccess.<br />
				You can either go to <a target='_blank' href='settings_security.php".$aidlink."'>Security Settings</a> and enable it or click the button below.";
				$locale['enable_maint'] = "Enable Maintenance";

				$content .= "<div class='well'>".$locale['enable_maint_warning']."</div>";
				$content .= "<input class='button btn btn-primary' type='submit' name='enable_maintenance' value='".$locale['enable_maint']."'>";
			
			// Check if $pdo_enabled, SECRET_KEY and SECRET_KEY_SALT are set
			} elseif (!isset($pdo_enabled) || !defined('SECRET_KEY') || !defined('SECRET_KEY_SALT')) {
				//Function to generate random token keys
				function createRandomToken($length = 32) {
					$chars = array("abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ", "123456789");
					$count = array((strlen($chars[0])-1), (strlen($chars[1])-1));
					$key = "";
					for ($i = 0; $i < $length; $i++) {
						$type = mt_rand(0, 1);
						$key .= substr($chars[$type], mt_rand(0, $count[$type]), 1);
					}
					return $key;
				}
				$secret_key = createRandomToken();
				$secret_key_salt = createRandomToken();
				$content .= "<div class='well'>\n";
				$content .= "Before continuing you need to insert into <strong>config.php</strong> the following lines:<br />\n";
				$content .= "<code class='panel panel-default display-inline-block' style='margin-top:10px; padding: 8px; text-align:left;'>\n";
				$content .= "\$pdo_enabled = \"0\"; <br />\n";
				$content .= "define(\"SECRET_KEY\", \"".$secret_key."\"); <br />\n";
				$content .= "define(\"SECRET_KEY_SALT\", \"".$secret_key_salt."\"); <br />\n";
				$content .= "</code><br />";
				$content .= "Please note that you need to change the <code>\$pdo_enabled = \"0\"</code> to <code>\$pdo_enabled = \"1\"</code> manually in order to enable PDO</div><br />\n";
			} else {
				$content .= sprintf($locale['500'], $locale['504'])."<br />\n".$locale['501']."<br /><br />\n";
				$content .= "<input type='hidden' name='stage' value='2'>\n";
				$content .= "<input type='submit' name='upgrade' value='".$locale['400']."' class='button btn btn-primary'><br /><br />\n";
			}
			break;
		case 2:
			$old_files = array(
					ADMIN."settings_links.php",
					ADMIN."updateuser.php",
					ADMIN."settings_links.php",
					THEMES."templates/header_mce.php",
					THEMES."templates/admin_header_mce.php",
					IMAGES."edit.gif",
					IMAGES."star.gif",
					IMAGES."tick.gif",
					INCLUDES."jscripts/tiny_mce/langs/hu.js",
					INCLUDES."jscripts/tiny_mce/plugins/compat2x/editor_plugin.js",
					INCLUDES."jscripts/tiny_mce/plugins/compat2x/editor_plugin_src.js",
					INCLUDES."jscripts/tiny_mce/plugins/paste/css/blank.css",
					INCLUDES."jscripts/tiny_mce/plugins/paste/css/pasteword.css",
					INCLUDES."jscripts/tiny_mce/plugins/paste/blank.htm",
					INCLUDES."jscripts/tiny_mce/plugins/safari/blank.htm",
					INCLUDES."jscripts/tiny_mce/plugins/safari/editor_plugin.js",
					INCLUDES."jscripts/tiny_mce/plugins/safari/editor_plugin_src.js",
					INCLUDES."jscripts/tiny_mce/plugins/xhtmlxtras/css/xhtmlxtras.css",
					INCLUDES."jscripts/tiny_mce/utils/mclayer.js",
					INCLUDES."securimage/index.php",
					INCLUDES."captcha_include.php",
					INCLUDES."jquery.js",
					INCLUDES."phpmailer_include.php",
					INCLUDES."smtp_include.php",
					INCLUDES."update_profile_include.php",
					CLASSES."SubCats.class.php",
					INFUSIONS."navigation_panel/index.php",
					LOCALE."English/admin/shoutbox.php",
					LOCALE."English/edit_profile.php",
					LOCALE."English/register.php",
					LOCALE."English/view_profile.php",
					BASEDIR."readarticle.php",
					INCLUDES."PHPMailerAutoload.php",
					INCLUDES."class.phpmailer.php",
					INCLUDES."class.smtp.php",
					INCLUDES."language/",
					);

			function rmdir_recursively($dir) {
				foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
					$path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
				}
				rmdir($dir);

				if (!file_exists($dir)) return TRUE;

				return FALSE;
			}

			$files_to_remove = "";
			$files_removed = "";
			foreach ($old_files as $key => $file) {
				if (file_exists($file)) {
					if (isset($_POST['remove_old_files']) && (is_dir($file) ? rmdir_recursively($file) : unlink($file))) {
						$files_removed .= "<li><span style='text-decoration: line-through'>".str_replace('../', '', $file)."</span> [ REMOVED ]</li>\n";
					} else {
						$files_to_remove .= "<li>".str_replace('../', '', $file)."</li>\n";
					}
				}
			}

			if (!empty($files_to_remove)) {
				$content .= "<div class='well'>\n";
				$content .= "The following files and/or folders that are no longer used in this version will be removed:<br />\n";
				$content .= "<code class='panel panel-default display-inline-block' style='margin-top:10px; padding: 5px; text-align:left;'>\n";
				$content .= "<ol type='1' style='list-style: normal; padding-left: 24px'>\n";
				$content .= $files_to_remove.$files_removed;
				$content .= "</ol>\n";
				$content .= "</code><br />\n";
				$content .= "<input type='hidden' name='stage' value='2'>\n";
				if (isset($_POST['remove_old_files'])) {
					$content .= "Some files or folders could not be removed, please delete them manually or try again.<br />";
				}
				$content .= "<input type='submit' name='remove_old_files' value='Remove Files' class='button btn btn-primary'>\n";
				$content .= "</div>\n";
				break; // we only break if there are files to be removed, if not continue to step 3
			} elseif (isset($_POST['remove_old_files'])) {
				/*$content .= "All old files and folders were removed, click the button below to continue.<br />";
				$content .= "<input type='hidden' name='stage' value='3'>\n";
				$content .= "<input type='submit' name='upgrade' value='Continue' class='button btn btn-primary'><br /><br />\n";
				break;*/
				addNotice('success', 'All unnecessary files were removed');
			}

		case 3:
			if (!isset($_POST['write_htaccess'])) {
				$content .= "<div class='well'>\n";
				$content .= "A new .htaccess file will be created with specific settings that are neccessary for PHP-Fusion to run properly<br />
				Please note that any changes previosuly made to .htaccess will be lost.";
				$content .= "</div>\n";
				$content .= "<input type='hidden' name='stage' value='3'>\n";
				$content .= "<input type='submit' name='write_htaccess' value='Continue' class='button btn btn-primary'><br /><br />\n";
				break;
			} else {
				// Wipe out all .htaccess rewrite rules and add error handler only
				$htc = "# Force utf-8 charset".PHP_EOL;
				$htc .= "AddDefaultCharset utf-8".PHP_EOL.PHP_EOL;
				$htc .= "# Security".PHP_EOL;
				$htc .= "ServerSignature Off".PHP_EOL.PHP_EOL;
				$htc .= "# Secure htaccess file".PHP_EOL;
				$htc .= "<Files .htaccess>".PHP_EOL;
				$htc .= "order allow,deny".PHP_EOL;
				$htc .= "deny from all".PHP_EOL;
				$htc .= "</Files>".PHP_EOL.PHP_EOL;
				$htc .= "# Protect config.php".PHP_EOL;
				$htc .= "<Files config.php>".PHP_EOL;
				$htc .= "order allow,deny".PHP_EOL;
				$htc .= "deny from all".PHP_EOL;
				$htc .= "</Files>".PHP_EOL.PHP_EOL;
				$htc .= "# Block Nasty Bots".PHP_EOL;
				$htc .= "<IfModule mod_setenvifno.c>".PHP_EOL;
				$htc .= "	SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT".PHP_EOL;
				$htc .= "	SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT".PHP_EOL;
				$htc .= "	Deny from env=HTTP_SAFE_BADBOT".PHP_EOL;
				$htc .= "</IfModule>".PHP_EOL.PHP_EOL;
				$htc .= "# Disable directory listing".PHP_EOL;
				$htc .= "Options -Indexes".PHP_EOL.PHP_EOL;
				$htc .= "ErrorDocument 400 ".$settings['site_path']."error.php?code=400".PHP_EOL;
				$htc .= "ErrorDocument 401 ".$settings['site_path']."error.php?code=401".PHP_EOL;
				$htc .= "ErrorDocument 403 ".$settings['site_path']."error.php?code=403".PHP_EOL;
				$htc .= "ErrorDocument 404 ".$settings['site_path']."error.php?code=404".PHP_EOL;
				$htc .= "ErrorDocument 500 ".$settings['site_path']."error.php?code=500".PHP_EOL;
				// Create the .htaccess file
				if (!file_exists(BASEDIR.".htaccess")) {
					if (file_exists(BASEDIR."_htaccess") && function_exists("rename")) {
						@rename(BASEDIR."_htaccess", BASEDIR.".htaccess");
					} else {
						touch(BASEDIR.".htaccess");
					}
				}
				// Write the contents to .htaccess
				$temp = fopen(BASEDIR.".htaccesss", "w");
				if (fwrite($temp, $htc)) {
					fclose($temp);

					addNotice('success', 'The contents of .htaccess were updated');
				}/* else {
					$content .= "<div class='well'>\n";
					$content .= "We weren't able to write the contents required to <strong>.htaccess</strong>,
					please open the file and paste the following contents:<br />";
					$content .= "<code class='panel panel-default display-inline-block' style='text-align: left; white-space: normal'>\n";
					$content .= nl2br(htmlentities($htc, ENT_QUOTES, 'UTF-8' ));
					$content .= "</code>\n";
					$content .= "</div>\n";
					break;
				}*/
			}

		case 4:
			$content .= "<div class='well'>\n";
			$content .= "Several changes will be made to the database.<br />
			We <strong>strongly</strong> recommend that you make a <a target='_blank' href='db_backup.php".$aidlink."'>Databse Backup</a> before proceeding!";
			$content .= "</div>\n";
			$content .= "<input type='hidden' name='stage' value='5'>\n";
			$content .= "<input type='submit' name='upgrade_database' value='Upgrade Database' class='button btn btn-primary'><br /><br />\n";
			
			if (1==2 && isset($_POST['upgrade_database'])) { // 1==2 to disable it for testing purposes
				// New access rights need a larger table for users
				$result = dbquery("ALTER TABLE ".DB_USERS." CHANGE user_level user_level TINYINT(4) NOT NULL DEFAULT '-101'");

				// Modify All Users Level > 0
				$result = dbquery("SELECT user_id, user_level FROM ".DB_USERS."");
				if (dbrows($result)>0) {
					while ($data = dbarray($result)) {
						if ($data['user_level']) { // will omit 0
							dbquery("UPDATE ".DB_USERS." SET user_level ='-".$data['user_level']."' WHERE user_id='".$data['user_id']."' ");
						}
					}
				}
				
				// change link_visibility
				$result = dbquery("ALTER TABLE ".DB_SITE_LINKS." CHANGE link_visibility link_visibility CHAR(4) NOT NULL DEFAULT ''");
				$link_result = dbquery("SELECT link_id, link_visibility FROM ".DB_SITE_LINKS."");
				if (dbrows($result)>0) {
					while ($data = dbarray($result)) {
						if ($data['link_visibility']) { // will omit 0
							dbquery("UPDATE ".DB_SITE_LINKS." SET user_visibility ='-".$data['link_visibility']."' WHERE link_id='".$data['link_id']."' ");
						}
					}
				}

				//Add language tables to infusions and main content
				$result = dbquery("ALTER TABLE ".DB_ARTICLE_CATS." ADD article_cat_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER article_cat_access");
				$result = dbquery("ALTER TABLE ".DB_CUSTOM_PAGES." ADD page_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER page_allow_ratings");
				$result = dbquery("ALTER TABLE ".DB_DOWNLOAD_CATS." ADD download_cat_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER download_cat_access");
				$result = dbquery("ALTER TABLE ".DB_FAQ_CATS." ADD faq_cat_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER faq_cat_description");
				$result = dbquery("ALTER TABLE ".DB_FORUM_RANKS." ADD rank_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER rank_apply");
				$result = dbquery("ALTER TABLE ".DB_FORUMS." ADD forum_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER forum_merge");<commit_msg>Inserted the database conversion script, cleaning.<commit_after><?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright (C) PHP-Fusion Inc
| https://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: upgrade.php
| Author: <NAME> (Digitanium)
+--------------------------------------------------------+
| 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";

pageAccess("U");

require_once THEMES."templates/admin_header.php";

if (file_exists(LOCALE.LOCALESET."admin/upgrade.php")) {
	include LOCALE.LOCALESET."admin/upgrade.php";
} else {
	include LOCALE."English/admin/upgrade.php";
}

if (file_exists(LOCALE.LOCALESET."setup.php")) {
	include LOCALE.LOCALESET."setup.php";
} else {
	include LOCALE."English/setup.php";
}

opentable($locale['400']);
echo "<div style='text-align:center'><br />\n";

if (str_replace(".", "", $settings['version']) < "90001") { // 90001 for testing purposes
	echo openform('upgradeform', 'post', FUSION_SELF.$aidlink); 
	$content = "";
	switch (filter_input(INPUT_POST, 'stage', FILTER_VALIDATE_INT) ? : 1) {
		case 1:
		default:
			// Check if maintainance mode is enabled
			if (fusion_get_settings("maintenance") == 0) {
				if (isset($_POST['enable_maintenance'])) {
					dbquery("UPDATE ".DB_SETTINGS." SET settings_value='1' WHERE settings_name='maintenance'");
					addNotice('success', 'Maintenance mode was enabled, you can now continue with the upgrade process');

					redirect(FUSION_SELF.$aidlink);
				}

				$locale['enable_maint_warning'] = "Please put your website into Maintenance mode before continuing in order to avoid any issues that might occur during the upgrade proccess.<br />
				You can either go to <a target='_blank' href='settings_security.php".$aidlink."'>Security Settings</a> and enable it or click the button below.";
				$locale['enable_maint'] = "Enable Maintenance";

				$content .= "<div class='well'>".$locale['enable_maint_warning']."</div>";
				$content .= "<input class='button btn btn-primary' type='submit' name='enable_maintenance' value='".$locale['enable_maint']."'>";
			
			// Check if $pdo_enabled, SECRET_KEY and SECRET_KEY_SALT are set
			} elseif (!isset($pdo_enabled) || !defined('SECRET_KEY') || !defined('SECRET_KEY_SALT')) {
				//Function to generate random token keys
				function createRandomToken($length = 32) {
					$chars = array("abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ", "123456789");
					$count = array((strlen($chars[0])-1), (strlen($chars[1])-1));
					$key = "";
					for ($i = 0; $i < $length; $i++) {
						$type = mt_rand(0, 1);
						$key .= substr($chars[$type], mt_rand(0, $count[$type]), 1);
					}
					return $key;
				}
				$secret_key = createRandomToken();
				$secret_key_salt = createRandomToken();
				$content .= "<div class='well'>\n";
				$content .= "Before continuing you need to insert into <strong>config.php</strong> the following lines:<br />\n";
				$content .= "<code class='panel panel-default display-inline-block' style='margin-top:10px; padding: 8px; text-align:left;'>\n";
				$content .= "\$pdo_enabled = \"0\"; <br />\n";
				$content .= "define(\"SECRET_KEY\", \"".$secret_key."\"); <br />\n";
				$content .= "define(\"SECRET_KEY_SALT\", \"".$secret_key_salt."\"); <br />\n";
				$content .= "</code><br />";
				$content .= "Please note that you need to change the <code>\$pdo_enabled = \"0\"</code> to <code>\$pdo_enabled = \"1\"</code> manually in order to enable PDO</div><br />\n";
			} else {
				$content .= sprintf($locale['500'], $locale['503'])."<br />\n".$locale['501']."<br /><br />\n";
				$content .= "<input type='hidden' name='stage' value='2'>\n";
				$content .= "<input type='submit' name='upgrade' value='".$locale['400']."' class='button btn btn-primary'><br /><br />\n";
			}
			break;
		case 2:
			$old_files = array(
					ADMIN."settings_links.php",
					ADMIN."updateuser.php",
					ADMIN."settings_links.php",
					THEMES."templates/header_mce.php",
					THEMES."templates/admin_header_mce.php",
					IMAGES."edit.gif",
					IMAGES."star.gif",
					IMAGES."tick.gif",
					INCLUDES."jscripts/tiny_mce/langs/hu.js",
					INCLUDES."jscripts/tiny_mce/plugins/compat2x/editor_plugin.js",
					INCLUDES."jscripts/tiny_mce/plugins/compat2x/editor_plugin_src.js",
					INCLUDES."jscripts/tiny_mce/plugins/paste/css/blank.css",
					INCLUDES."jscripts/tiny_mce/plugins/paste/css/pasteword.css",
					INCLUDES."jscripts/tiny_mce/plugins/paste/blank.htm",
					INCLUDES."jscripts/tiny_mce/plugins/safari/blank.htm",
					INCLUDES."jscripts/tiny_mce/plugins/safari/editor_plugin.js",
					INCLUDES."jscripts/tiny_mce/plugins/safari/editor_plugin_src.js",
					INCLUDES."jscripts/tiny_mce/plugins/xhtmlxtras/css/xhtmlxtras.css",
					INCLUDES."jscripts/tiny_mce/utils/mclayer.js",
					INCLUDES."securimage/index.php",
					INCLUDES."captcha_include.php",
					INCLUDES."jquery.js",
					INCLUDES."phpmailer_include.php",
					INCLUDES."smtp_include.php",
					INCLUDES."update_profile_include.php",
					CLASSES."SubCats.class.php",
					INFUSIONS."navigation_panel/index.php",
					LOCALE."English/admin/shoutbox.php",
					LOCALE."English/edit_profile.php",
					LOCALE."English/register.php",
					LOCALE."English/view_profile.php",
					BASEDIR."readarticle.php",
					INCLUDES."PHPMailerAutoload.php",
					INCLUDES."class.phpmailer.php",
					INCLUDES."class.smtp.php",
					INCLUDES."language/",
					);

			function rmdir_recursively($dir) {
				foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
					$path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
				}
				rmdir($dir);

				if (!file_exists($dir)) return TRUE;

				return FALSE;
			}

			$files_to_remove = "";
			$files_removed = "";
			foreach ($old_files as $key => $file) {
				if (file_exists($file)) {
					if (isset($_POST['remove_old_files']) && (is_dir($file) ? rmdir_recursively($file) : unlink($file))) {
						$files_removed .= "<li><span style='text-decoration: line-through'>".str_replace('../', '', $file)."</span> [ REMOVED ]</li>\n";
					} else {
						$files_to_remove .= "<li>".str_replace('../', '', $file)."</li>\n";
					}
				}
			}

			if (!empty($files_to_remove)) {
				$content .= "<div class='well'>\n";
				$content .= "The following files and/or folders that are no longer used in this version will be removed:<br />\n";
				$content .= "<code class='panel panel-default display-inline-block' style='margin-top:10px; padding: 5px; text-align:left;'>\n";
				$content .= "<ol type='1' style='list-style: normal; padding-left: 24px'>\n";
				$content .= $files_to_remove.$files_removed;
				$content .= "</ol>\n";
				$content .= "</code><br />\n";
				$content .= "<input type='hidden' name='stage' value='2'>\n";
				if (isset($_POST['remove_old_files'])) {
					$content .= "Some files or folders could not be removed, please delete them manually or try again.<br />";
				}
				$content .= "<input type='submit' name='remove_old_files' value='Remove Files' class='button btn btn-primary'>\n";
				$content .= "</div>\n";
				break; 
			} elseif (isset($_POST['remove_old_files'])) {
				addNotice('success', 'All unnecessary files were removed');
			}
		case 3:
			if (!isset($_POST['write_htaccess'])) {
				$content .= "<div class='well'>\n";
				$content .= "A new .htaccess file will be created with specific settings that are neccessary for PHP-Fusion to run properly<br />
				Please note that any changes previosuly made to .htaccess will be lost.";
				$content .= "</div>\n";
				$content .= "<input type='hidden' name='stage' value='3'>\n";
				$content .= "<input type='submit' name='write_htaccess' value='Continue' class='button btn btn-primary'><br /><br />\n";
				break;
			} else {
				// Wipe out all .htaccess rewrite rules and add error handler only
				$htc = "# Force utf-8 charset".PHP_EOL;
				$htc .= "AddDefaultCharset utf-8".PHP_EOL.PHP_EOL;
				$htc .= "# Security".PHP_EOL;
				$htc .= "ServerSignature Off".PHP_EOL.PHP_EOL;
				$htc .= "# Secure htaccess file".PHP_EOL;
				$htc .= "<Files .htaccess>".PHP_EOL;
				$htc .= "order allow,deny".PHP_EOL;
				$htc .= "deny from all".PHP_EOL;
				$htc .= "</Files>".PHP_EOL.PHP_EOL;
				$htc .= "# Protect config.php".PHP_EOL;
				$htc .= "<Files config.php>".PHP_EOL;
				$htc .= "order allow,deny".PHP_EOL;
				$htc .= "deny from all".PHP_EOL;
				$htc .= "</Files>".PHP_EOL.PHP_EOL;
				$htc .= "# Block Nasty Bots".PHP_EOL;
				$htc .= "<IfModule mod_setenvifno.c>".PHP_EOL;
				$htc .= "	SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT".PHP_EOL;
				$htc .= "	SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT".PHP_EOL;
				$htc .= "	Deny from env=HTTP_SAFE_BADBOT".PHP_EOL;
				$htc .= "</IfModule>".PHP_EOL.PHP_EOL;
				$htc .= "# Disable directory listing".PHP_EOL;
				$htc .= "Options -Indexes".PHP_EOL.PHP_EOL;
				$htc .= "ErrorDocument 400 ".$settings['site_path']."error.php?code=400".PHP_EOL;
				$htc .= "ErrorDocument 401 ".$settings['site_path']."error.php?code=401".PHP_EOL;
				$htc .= "ErrorDocument 403 ".$settings['site_path']."error.php?code=403".PHP_EOL;
				$htc .= "ErrorDocument 404 ".$settings['site_path']."error.php?code=404".PHP_EOL;
				$htc .= "ErrorDocument 500 ".$settings['site_path']."error.php?code=500".PHP_EOL;
				// Create the .htaccess file
				if (!file_exists(BASEDIR.".htaccess")) {
					if (file_exists(BASEDIR."_htaccess") && function_exists("rename")) {
						@rename(BASEDIR."_htaccess", BASEDIR.".htaccess");
					} else {
						touch(BASEDIR.".htaccess");
					}
				}
				// Write the contents to .htaccess
				$temp = fopen(BASEDIR.".htaccesss", "w");
				if (fwrite($temp, $htc)) {
					fclose($temp);
					addNotice('success', 'The contents of .htaccess were updated');
				}/* else {
					$content .= "<div class='well'>\n";
					$content .= "We weren't able to write the contents required to <strong>.htaccess</strong>,
					please open the file and paste the following contents:<br />";
					$content .= "<code class='panel panel-default display-inline-block' style='text-align: left; white-space: normal'>\n";
					$content .= nl2br(htmlentities($htc, ENT_QUOTES, 'UTF-8' ));
					$content .= "</code>\n";
					$content .= "</div>\n";
					break;
				}*/
			}
		case 4:
			$content .= "<div class='well'>\n";
			$content .= "Several changes will be made to the database.<br />
			We <strong>strongly</strong> recommend that you make a <a target='_blank' href='db_backup.php".$aidlink."'>Databse Backup</a> before proceeding!";
			$content .= "</div>\n";
			$content .= "<input type='hidden' name='stage' value='5'>\n";
			$content .= "<input type='submit' name='upgrade_database' value='Upgrade Database' class='button btn btn-primary'><br /><br />\n";
			
			if (1==2 && isset($_POST['upgrade_database'])) { // 1==2 to disable it for testing purposes

			// Force the database to UTF-8 because we'll convert to it
				dbquery("SET NAMES 'utf8'");
		 
			$result = dbquery("SHOW TABLES");
				while($row = dbarray($result)) {
				  foreach ($row as $key => $table) {
					dbquery("ALTER TABLE " . $table . " CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci");
					$result2 = dbquery("SHOW COLUMNS FROM ".$table);
					// We must change all data like find/replace in columns of broken chars, this may differ for each locales, please complete this list if you know what´s missing.
						while($column = dbarray($result2)) {
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field']." ,'Ã¥','Å')"); 
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field']." ,'Ã¤','Ä')"); 
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field']." ,'Ã¶','Ö')"); 
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'ð', 'ğ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'ý', 'ı')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'þ', 'ş')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ð', 'Ğ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ý', 'İ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Þ', 'Ş')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã‰','É')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'â€œ','\"')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'â€','\"')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã‡','Ç')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ãƒ','Ã')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã¥','Å')"); 
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã¤','Ä')"); 
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã¶','Ö')"); 
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã ','À')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ãº','ú')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'â€¢','-')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã˜','Ø')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ãµ','õ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã­','í')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã¢','â')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã£','ã')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ãª','ê')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã¡','á')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã©','é')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã³','ó')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'â€“','–')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã§','ç')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Âª','ª')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Âº','º')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", 'Ã ','à')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ccedil;','ç')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&atilde;','ã')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&aacute;','á')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&acirc;','â')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&eacute;','é')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&iacute;','í')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&otilde;','õ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&uacute;','ú')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ccedil;','ç')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Aacute;','Á')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Acirc;','Â')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Eacute;','É')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Iacute;','Í')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Otilde;','Õ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Uacute;','Ú')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Ccedil;','Ç')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Atilde;','Ã')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Agrave;','À')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Ecirc;','Ê')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Oacute;','Ó')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Ocirc;','Ô')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Uuml;','Ü')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&atilde;','ã')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&agrave;','à')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ecirc;','ê')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&oacute;','ó')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ocirc;','ô')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&uuml;','ü')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&amp;','&')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&gt;','>')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&lt;','<')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&circ;','ˆ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&tilde;','˜')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&uml;','¨')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&cute;','´')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&cedil;','¸')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&quot;','\"')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ldquo;','“')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&rdquo;','”')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&lsquo;','‘')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&rsquo;','’')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&lsaquo;','‹')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&rsaquo;','›')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&laquo;','«')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&raquo;','»')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ordm;','º')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ordf;','ª')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ndash;','–')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&mdash;','—')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&macr;','¯')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&hellip;','…')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&brvbar;','¦')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&bull;','•')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&para;','¶')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&sect;','§')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&sup1;','¹')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&sup2;','²')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&sup3;','³')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&frac12;','½')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&frac14;','¼')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&frac34;','¾')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&#8539;','⅛')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&#8540;','⅜')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&#8541;','⅝')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&#8542;','⅞')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&gt;','>')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&lt;','<')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&plusmn;','±')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&minus;','−')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&times;','×')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&divide;','÷')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&lowast;','∗')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&frasl;','⁄')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&permil;','‰')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&int;','∫')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&sum;','∑')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&prod;','∏')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&radic;','√')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&infin;','∞')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&asymp;','≈')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&cong;','≅')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&prop;','∝')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&equiv;','≡')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ne;','≠')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&le;','≤')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ge;','≥')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&there4;','∴')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&sdot;','⋅')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&middot;','·')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&part;','∂')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&image;','ℑ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&real;','ℜ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&prime;','′')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Prime;','″')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&deg;','°')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&ang;','∠')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&perp;','⊥')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&nabla;','∇')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&oplus;','⊕')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&otimes;','⊗')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&alefsym;','ℵ')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&oslash;','ø')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&Oslash;','Ø')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&isin;','∈')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&notin;','∉')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&cap;','∩')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&cup;','∪')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&sub;','⊂')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&sup;','⊃')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&sube;','⊆')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&supe;','⊇')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&exist;','∃')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&forall;','∀')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&empty;','∅')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&not;','¬')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&and;','∧')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&or;','∨')");
							dbquery("UPDATE ".$table." SET ".$column['Field']." = REPLACE(".$column['Field'].", '&crarr;','↵')");
					}
				  }
				}
				// New access rights need a larger table for users
				$result = dbquery("ALTER TABLE ".DB_USERS." CHANGE user_level user_level TINYINT(4) NOT NULL DEFAULT '-101'");

				// Modify All Users Level > 0
				$result = dbquery("SELECT user_id, user_level FROM ".DB_USERS."");
				if (dbrows($result)>0) {
					while ($data = dbarray($result)) {
						if ($data['user_level']) { // will omit 0
							dbquery("UPDATE ".DB_USERS." SET user_level ='-".$data['user_level']."' WHERE user_id='".$data['user_id']."' ");
						}
					}
				}
				
				// change link_visibility
				$result = dbquery("ALTER TABLE ".DB_SITE_LINKS." CHANGE link_visibility link_visibility CHAR(4) NOT NULL DEFAULT ''");
				$link_result = dbquery("SELECT link_id, link_visibility FROM ".DB_SITE_LINKS."");
				if (dbrows($result)>0) {
					while ($data = dbarray($result)) {
						if ($data['link_visibility']) { // will omit 0
							dbquery("UPDATE ".DB_SITE_LINKS." SET user_visibility ='-".$data['link_visibility']."' WHERE link_id='".$data['link_id']."' ");
						}
					}
				}

				//Add language tables to infusions and main content
				$result = dbquery("ALTER TABLE ".DB_ARTICLE_CATS." ADD article_cat_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER article_cat_access");
				$result = dbquery("ALTER TABLE ".DB_CUSTOM_PAGES." ADD page_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER page_allow_ratings");
				$result = dbquery("ALTER TABLE ".DB_DOWNLOAD_CATS." ADD download_cat_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER download_cat_access");
				$result = dbquery("ALTER TABLE ".DB_FAQ_CATS." ADD faq_cat_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER faq_cat_description");
				$result = dbquery("ALTER TABLE ".DB_FORUM_RANKS." ADD rank_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER rank_apply");
				$result = dbquery("ALTER TABLE ".DB_FORUMS." ADD forum_language VARCHAR(50) NOT NULL DEFAULT '".$settings['locale']."' AFTER forum_merge");
{'fraction_non_alphanumeric': 0.2552049556097234, 'fraction_numerical': 0.006619406489701903, 'mean_word_length': 3.8223875768359754, 'pattern_counts': {'":': 0, '<': 177, '<?xml version=': 0, '>': 188, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3918986', 'n_tokens_mistral': 17994, 'n_tokens_neox': 15178, 'n_words': 2997}