<?php
/*========================================================================*\
|| ###################################################################### ||
|| # vBulletin 5.4.1
|| # ------------------------------------------------------------------ # ||
|| # Copyright 2000-2018 vBulletin Solutions Inc. All Rights Reserved.  # ||
|| # This file may not be redistributed in whole or significant part.   # ||
|| # ----------------- VBULLETIN IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html   # ||
|| ###################################################################### ||
\*========================================================================*/
/*
if (!isset($GLOBALS['vbulletin']->db))
{
	exit;
}
*/

class vB_Upgrade_541b2 extends vB_Upgrade_Version
{
	/*Constants=====================================================================*/

	/*Properties====================================================================*/

	/**
	* The short version of the script
	*
	* @var	string
	*/
	public $SHORT_VERSION = '541b2';

	/**
	* The long version of the script
	*
	* @var	string
	*/
	public $LONG_VERSION  = '5.4.1 Beta 2';

	/**
	* Versions that can upgrade to this script
	*
	* @var	string
	*/
	public $PREV_VERSION = '5.4.1 Beta 1';

	/**
	* Beginning version compatibility
	*
	* @var	string
	*/
	public $VERSION_COMPAT_STARTS = '';

	/**
	* Ending version compatibility
	*
	* @var	string
	*/
	public $VERSION_COMPAT_ENDS   = '';

	// Reinstall twitterlogin package to fix the externallogin settinggroup displayorder
	// and to pick up any other product XML changes (e.g. modified/new templates & phrases)
	// in alpha/beta versions
	public function step_1()
	{
		$package = 'twitterlogin';
		$assertor = vB::getDbAssertor();
		$check = $assertor->getRow('product', array('productid' => $package));

		$datastore = vB::getDatastore();
		$hooksEnabled = $datastore->getOption('enablehooks');
		if (!empty($check))
		{
			vB_Upgrade::createAdminSession();


			if (!defined('VB_PKG_PATH'))
			{
				$packagesDir = realpath(DIR . '/packages') . '/';
			}
			else
			{
				$packagesDir = VB_PKG_PATH;
			}


			$xmlDir = $packagesDir . "$package/xml";
			$class = $package . '_Product';

			$overwrite = true;

			if (class_exists($class) AND property_exists($class, 'AutoInstall') AND $class::$AutoInstall)
			{
				$this->show_message($this->phrase['version']['541b2']['updating_twitterlogin_package']);
				$info = vB_Library_Functions::installProduct($package, $xmlDir, '', $overwrite);
				vB_Library::instance('product')->buildProductDatastore();
				if (!$hooksEnabled)
				{
					/*
						Re-installing the twitterlogin package automatically enables the hook system.
						If they had it disabled earlier, set it back to disabled.
						We could consider just skipping the re-install altogether if the hook system is
						disabled, but I worry that doing so would lead to data inconsistencies in the
						tester DBs. Note that we check for the existence of the twitterlogin package first,
						which implies (I think) that at *some* point, the product got installed and the
						hook/product system was enabled, then disabled by the admin.
					 */
					$assertor->update('setting', array('value' => 0), array('varname' => 'enablehooks'));
					vB::getDatastore()->build_options();
				}
			}
			else
			{
				$this->skip_message();
			}
		}
		else
		{
			$this->skip_message();
		}
	}
}

/*======================================================================*\
|| ####################################################################
|| # NulleD By - vBSupport.org
|| # CVS: $RCSfile$ - $Revision$
|| ####################################################################
\*======================================================================*/