Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Store forced state in a cookie #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 69 additions & 57 deletions class.ext_update.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
use TYPO3\CMS\Backend\Utility\IconUtility;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/***************************************************************
* Copyright notice
*
Expand Down Expand Up @@ -37,19 +41,19 @@ class ext_update {
* @var integer
*/
protected $majorVersion;

/**
* Holds the current localconf
* @var array
*/
protected $extConf;

/**
* Holds the current ext_conf_template
* @var array
*/
protected $extConfTemplateArr;

/**
* The constructor
* @return ext_update
Expand All @@ -62,7 +66,7 @@ public function __construct()
$this->extConf = $this->getExtConf();
$this->extConfTemplateArr = $this->getExtConfTemplateArr();
}

/**
* Main function, returning the HTML content of the module
*
Expand All @@ -71,20 +75,20 @@ public function __construct()
public function main() {
if(!$this->checkConfigs()) {
$out = 'Either your local configuration or the ext_conf_template.txt of this extension is empty or broken!';

return $out;
}

$differences = $this->getExtConfDifferences();
$out = '';

if(count($differences) > 0) {
if (t3lib_div::_GP('do_update')) {
if($this->updateExtConf(t3lib_div::_GP('func'))) {
if (GeneralUtility::_GP('do_update')) {
if($this->updateExtConf(GeneralUtility::_GP('func'))) {
$out .= 'Your local configuration has been updated. Thanks for chosing cwmobileredirect!<br>';
$out .= '<button onclick="location.href=\''
. t3lib_div::linkThisScript(array('do_update' => '', 'func' => ''))
. '\'" value="" name="do_update" type="submit"><img style="vertical-align:bottom;" '
. t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/refresh_n.gif', 'width="18" height="16"')
$out .= '<button onclick="location.href=\''
. GeneralUtility::linkThisScript(array('do_update' => '', 'func' => ''))
. '\'" value="" name="do_update" type="submit"><img style="vertical-align:bottom;" '
. IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/refresh_n.gif', 'width="18" height="16"')
. '>Check for more updates</button><br><br><br>';
} else {
$out .= 'Your local configuration has been NOT updated. This most likely means that your cwmobileredirect installation is screwed. You should consider removing and installing it from scratch.';
Expand All @@ -94,39 +98,39 @@ public function main() {

if(isset($differences['redirect_exceptions'])) {
$out .= $differences['redirect_exceptions'];
$out .= '<button onclick="location.href=\''
. t3lib_div::linkThisScript(array('do_update' => 'update', 'func' => 'redirect_exceptions'))
. '\'" value="update" name="do_update" type="submit"><img style="vertical-align:bottom;" '
. t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/refresh_n.gif', 'width="18" height="16"')
$out .= '<button onclick="location.href=\''
. GeneralUtility::linkThisScript(array('do_update' => 'update', 'func' => 'redirect_exceptions'))
. '\'" value="update" name="do_update" type="submit"><img style="vertical-align:bottom;" '
. IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/refresh_n.gif', 'width="18" height="16"')
. '>Update redirect_exceptions</button><br><br><br>';
}

if(isset($differences['regexp1'])) {
$out .= $differences['regexp1'];
$out .= 'This will OVERWRITE your existing mobile browser detection settings (First RegExp) in your local '
. 'configuration with the current extension default settings.<br><br>';
$out .= '<button onclick="location.href=\''
. t3lib_div::linkThisScript(array('do_update' => 'update', 'func' => 'regexp1'))
. '\'" value="update" name="do_update" type="submit"><img style="vertical-align:bottom;" '
. t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/refresh_n.gif', 'width="18" height="16"')
$out .= '<button onclick="location.href=\''
. GeneralUtility::linkThisScript(array('do_update' => 'update', 'func' => 'regexp1'))
. '\'" value="update" name="do_update" type="submit"><img style="vertical-align:bottom;" '
. IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/refresh_n.gif', 'width="18" height="16"')
. '>Update regexp1</button><br><br><br>';
}

if(isset($differences['regexp2'])) {
$out .= $differences['regexp2'];
$out .= 'This will OVERWRITE your existing mobile browser detection settings (Second RegExp) in your local '
. 'configuration with the current extension default settings.<br><br>';
$out .= '<button onclick="location.href=\''
. t3lib_div::linkThisScript(array('do_update' => 'update', 'func' => 'regexp2'))
. '\'" value="update" name="do_update" type="submit"><img style="vertical-align:bottom;" '
. t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/refresh_n.gif', 'width="18" height="16"')
$out .= '<button onclick="location.href=\''
. GeneralUtility::linkThisScript(array('do_update' => 'update', 'func' => 'regexp2'))
. '\'" value="update" name="do_update" type="submit"><img style="vertical-align:bottom;" '
. IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/refresh_n.gif', 'width="18" height="16"')
. '>Update regexp1</button><br><br><br>';
}
}
} else {
$out .= 'Your localconf does not need to be updated!';
}

return $out;
}

Expand All @@ -137,14 +141,16 @@ public function main() {
* @param string $what: what should be updated
* @return boolean
*/
public function access($what = 'all')
{
public function access(
/** @noinspection PhpUnusedParameterInspection */
$what = 'all'
) {
return true;
}

/**
* Checks whether all configs are ok
*
* Checks whether all configs are ok
*
* @return boolean
*/
protected function checkConfigs()
Expand All @@ -159,35 +165,38 @@ protected function checkConfigs()

return true;
}

/**
* Returns the content of ext_conf_template.txt as handy array
* @return boolean|array
*/
protected function getExtConfTemplateArr()
{
$extConfTemplateArr = array();

// Fetch the current reg exps from the ext_conf_template.txt
if ($this->majorVersion >= 6) {
// Typo3 >= 6.0
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$configurationUtility = $objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');

$extConfTemplateArr = $configurationUtility->getDefaultConfigurationFromExtConfTemplateAsValuedArray($this->extKey);
} else {
// Typo3 <= 4.7
/** @noinspection PhpUndefinedClassInspection */
$absPath = tx_em_Tools::getExtPath($this->extKey, 'L');
/** @noinspection PhpUndefinedClassInspection */
$relPath = tx_em_Tools::typeRelPath('L') . $this->extKey . '/';

if (t3lib_extMgm::isLoaded($this->extKey)
/** @noinspection PhpUndefinedMethodInspection */
if (ExtensionManagementUtility::isLoaded($this->extKey)
&& (@is_file($absPath . 'ext_conf_template.txt') || $this->extensionHasCacheConfiguration($absPath))
) {
// Load tsStyleConfig class and parse configuration template:
$tsStyleConfig = t3lib_div::makeInstance('t3lib_tsStyleConfig');
$tsStyleConfig = GeneralUtility::makeInstance('t3lib_tsStyleConfig');
$tsStyleConfig->doNotSortCategoriesBeforeMakingForm = TRUE;
$extConfTemplateArr = $tsStyleConfig->ext_initTSstyleConfig(
t3lib_div::getUrl($absPath . 'ext_conf_template.txt'),
GeneralUtility::getUrl($absPath . 'ext_conf_template.txt'),
$relPath,
$absPath,
$GLOBALS['BACK_PATH']
Expand All @@ -197,9 +206,9 @@ protected function getExtConfTemplateArr()

return $extConfTemplateArr;
}

/**
* Checks whether certain localconf whether
* Checks whether certain localconf whether
* @return string|boolean
*/
protected function getExtConfDifferences()
Expand All @@ -215,56 +224,65 @@ protected function getExtConfDifferences()
$retArray['regexp2'] = '<p><strong>regexp2 field differs from default!</strong><br><strong>local:</strong> ' . $this->extConf['regexp2']
. '<br><strong>default:</strong> ' . $this->extConfTemplateArr['regexp2']['value'] . '</p>';
}

if(empty($this->extConf['redirect_exceptions'])) {
$retArray['redirect_exceptions'] = '<p><strong>redirect_exceptions field is empty!</strong>'
. '<br><strong>default:</strong> ' . $this->extConfTemplateArr['redirect_exceptions']['value'] . '</p>';
}

return $retArray;
}

/**
* Upate the extConf
*
*
* @return boolean
*/
protected function updateExtConf($func)
{
protected function updateExtConf(
/** @noinspection PhpDocSignatureInspection */
$func
) {
// Get the value from the ext_conf_template and store it in the local conf
$this->extConf[$func] = $this->extConfTemplateArr[$func]['value'];

// Rewrite local configuration according to Typo3 version
if ($this->majorVersion >= 6) {
// Typo3 >= 6.0
$configurationManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
$configurationManager = GeneralUtility::makeInstance(
'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager'
);

$configurationManager->setLocalConfigurationValueByPath('EXT/extConf/' . $this->extKey, serialize($this->extConf));

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::removeCacheFiles();
ExtensionManagementUtility::removeCacheFiles();
} else {
// Typo3 <= 4.7
/** @noinspection PhpUndefinedClassInspection */
$install = new t3lib_install();
/** @noinspection PhpUndefinedFieldInspection */
$install->allowUpdateLocalConf = 1;
/** @noinspection PhpUndefinedFieldInspection */
$install->updateIdentity = 'cwmobileredirect Updater';

/** @noinspection PhpUndefinedMethodInspection */
$lines = $install->writeToLocalconf_control();
/** @noinspection PhpUndefinedMethodInspection */
$install->setValueInLocalconfFile(
$lines,
'$TYPO3_CONF_VARS[\'EXT\'][\'extConf\'][\'' . $this->extKey . '\']',
$lines,
'$TYPO3_CONF_VARS[\'EXT\'][\'extConf\'][\'' . $this->extKey . '\']',
serialize($this->extConf)
);

/** @noinspection PhpUndefinedMethodInspection */
$install->writeToLocalconf_control($lines);

/** @noinspection PhpUndefinedClassInspection */
t3lib_extMgm::removeCacheFiles();
}
}

return true;
}

/**
* Get the extension configuration
*
Expand All @@ -280,9 +298,3 @@ protected function getExtConf() {
return $extConf;
}
}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/cwmobileredirect/class.ext_update.php']) {
include_once ($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/cwmobileredirect/class.ext_update.php']);
}

?>
24 changes: 19 additions & 5 deletions class.tx_cwmobileredirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;

/**
* Simple user_func that uses tx_mobileredirect to determine if the
Expand Down Expand Up @@ -386,6 +388,18 @@ public function secondEntryPoint(&$params, &$ref)
*/
public function checkRedirect()
{
// if the mobile version was forced we set / update the cookie
// and do not care about redirects
if($this->isMobileForced()) {
$this->setExtensionCookie(self::MOBILEREDIRECT_COOKIE_MOBILE);
}

// if the standard version was forced we set / update the cookie
// and do not care about redirects
if($this->isStandardForced()) {
$this->setExtensionCookie(self::MOBILEREDIRECT_COOKIE_STANDARD);
}

// Don't do anything in this case
if(!$this->isMobileUrlRequested() &&
!$this->isStandardUrlRequested()
Expand All @@ -400,7 +414,7 @@ public function checkRedirect()
// don't redirect in case of configured exceptions
// e.g. (rest\/news|events)|typo3conf
if (!empty($this->_conf['redirect_exceptions']) &&
preg_match('/'.$this->_conf['redirect_exceptions'].'/', t3lib_div::getIndpEnv('REQUEST_URI')))
preg_match('/'.$this->_conf['redirect_exceptions'].'/', GeneralUtility::getIndpEnv('REQUEST_URI')))
{
return;
}
Expand Down Expand Up @@ -539,7 +553,7 @@ protected function redirectTo($url, $addParam = false)

$this->writeDebugLogArray();

t3lib_utility_Http::redirect($this->protocol . $url . $urlParam, $this->_conf['httpStatus']);
HttpUtility::redirect($this->protocol . $url . $urlParam, $this->_conf['httpStatus']);
}


Expand All @@ -553,7 +567,7 @@ protected function setHttpStatus()
{
// set default HTTP Status code, if not defined
if ('' == $this->_conf['httpStatus'] || !defined('t3lib_utility_Http::'. $this->_conf['httpStatus'])) {
$this->_conf['httpStatus'] = t3lib_utility_Http::HTTP_STATUS_303;
$this->_conf['httpStatus'] = HttpUtility::HTTP_STATUS_303;
} else {
$this->_conf['httpStatus'] = constant('t3lib_utility_Http::'. $this->_conf['httpStatus']);
}
Expand Down Expand Up @@ -700,7 +714,7 @@ protected function detectMobile($useragent = NULL)
// Thanks a lot!

if(!$useragent) {
$useragent = t3lib_div::getIndpEnv('HTTP_USER_AGENT');
$useragent = GeneralUtility::getIndpEnv('HTTP_USER_AGENT');
}

// Run detection - if true, store status
Expand Down Expand Up @@ -736,7 +750,7 @@ protected function detectMobile($useragent = NULL)
protected function detectMobileBrowser($useragent = NULL)
{
if(!$useragent) {
$useragent = t3lib_div::getIndpEnv('HTTP_USER_AGENT');
$useragent = GeneralUtility::getIndpEnv('HTTP_USER_AGENT');
}

// go through the array of known mobile browsers and check
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "intera-typo3-extension/cwmobileredirect",
"description": "Your all-in-one mobile device detection and redirection solution! Detects mobile browsers and redirects to other Typo3 sites in your setup (most likely optimized for mobiles). Allows to easily switch back to the normal version, with Cookie support to remember the users choice. The browser detection can be access via TypoScript or in your own extension.",
"type": "typo3-cms-extension",
"version": "1.4.3"
}
4 changes: 2 additions & 2 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

if (!defined ("TYPO3_MODE")) die ("Access denied.");

$TYPO3_CONF_VARS['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'][] = t3lib_extMgm::extPath($_EXTKEY) . 'class.tx_cwmobileredirect.php:&tx_cwmobileredirect->firstEntryPoint';
$TYPO3_CONF_VARS['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'][] = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'class.tx_cwmobileredirect.php:&tx_cwmobileredirect->firstEntryPoint';

$TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['configArrayPostProc']['tx_cwmobileredirect'] = t3lib_extMgm::extPath($_EXTKEY) . 'class.tx_cwmobileredirect.php:&tx_cwmobileredirect->secondEntryPoint';
$TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['configArrayPostProc']['tx_cwmobileredirect'] = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'class.tx_cwmobileredirect.php:&tx_cwmobileredirect->secondEntryPoint';