Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
added domain restriction, removed name format restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Jan 18, 2016
1 parent 9452a42 commit b112e97
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
6 changes: 6 additions & 0 deletions system/modules/short_urls/classes/ShortURLs.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public function checkForShortURL()
if( !$objShortURL->target )
return;

// check for domain restriction
if( $objShortURL->domain )
if( ( $objDomain = \PageModel::findById( $objShortURL->domain ) ) !== null )
if( strcasecmp( $objDomain->dns, \Environment::get('host') ) != 0 )
return;

// build redirect URL
$url = self::processTarget( $objShortURL->target );

Expand Down
96 changes: 87 additions & 9 deletions system/modules/short_urls/dca/tl_short_urls.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
// Palettes
'palettes' => array
(
'default' => '{shorturl_legend},name,target,redirect,disable'
'default' => '{shorturl_legend},name,domain,target,redirect,disable'
),

// Fields
Expand All @@ -100,9 +100,20 @@
'exclude' => true,
'search' => true,
'inputType' => 'text',
'eval' => array('rgxp'=>'alias', 'unique'=>true, 'maxlength'=>128, 'mandatory'=>true),
'eval' => array('unique'=>true, 'maxlength'=>255, 'mandatory'=>true,'tl_class'=>'w50'),
'save_callback' => array( array('tl_short_urls', 'validateName') ),
'sql' => "varchar(128) NOT NULL default ''"
'sql' => "varchar(255) NOT NULL default ''"
),
'domain' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_short_urls']['domain'],
'default' => '',
'exclude' => true,
'inputType' => 'select',
'options_callback' => array('tl_short_urls', 'getDomains'),
'eval' => array('tl_class'=>'w50','submitOnChange'=>true),
'save_callback' => array( array('tl_short_urls', 'validateDomain') ),
'sql' => "int(10) unsigned NOT NULL default '0'"
),
'redirect' => array
(
Expand Down Expand Up @@ -158,14 +169,55 @@ class tl_short_urls extends Backend
*/
public function validateName($varValue, DataContainer $dc)
{
// transform name to lower case
$varValue = strtolower( $varValue );
$this->validate( $varValue, $dc->activeRecord->domain );
return $varValue;
}

/**
* Process and validate the name of a short url
*
* @param mixed $varValue
* @param DataContainer $dc
*
* @return string
*
* @throws Exception
*/
public function validateDomain($varValue, DataContainer $dc)
{
$this->validate( $dc->activeRecord->name, $varValue );
return $varValue;
}

private function validate($name, $domain)
{
// check if a page exists
if( !\Config::get('urlSuffix') && \PageModel::findByAlias( $varValue ) !== null )
throw new Exception(sprintf($GLOBALS['TL_LANG']['tl_short_urls']['pageExists'], $varValue));
if( !\Config::get('urlSuffix') && ( $objPages = \PageModel::findByAlias( $name ) ) !== null )
{
// check if short url has a domain
if( $domain > 0 )
{
$valid = true;
while( $objPages->next() )
{
// load page details
$objPages->current()->loadDetails();

return $varValue;
// if one of the found pages is within the same domain, do not allow it
if( $objPages->rootId == $domain )
{
$valid = false;
break;
}
}

if( $valid )
return;
}

// otherwise throw exception
throw new Exception(sprintf($GLOBALS['TL_LANG']['tl_short_urls']['pageExists'], $name));
}
}


Expand Down Expand Up @@ -263,7 +315,33 @@ public function labelCallback($arrRow)
// remove current host
$targetURL = str_replace( \Environment::get('base'), '', $targetURL );

// check for domain restriction
$domain = '';
if( $arrRow['domain'] && ( $objPage = \PageModel::findById( $arrRow['domain'] ) ) !== null )
$domain = $objPage->dns . '/';

// generate list record
return '<div class="tl_content_right"><span style="color:rgb(200,200,200)">[' . ( $arrRow['redirect'] == 'permanent' ? 301 : 302 ) . ']</span></div><div class="tl_content_left">' . $arrRow['name'] . ' &raquo; ' . $targetURL . ' </div>';
return '<div class="tl_content_right"><span style="color:rgb(200,200,200)">[' . ( $arrRow['redirect'] == 'permanent' ? 301 : 302 ) . ']</span></div><div class="tl_content_left">' . $domain . $arrRow['name'] . ' &raquo; ' . $targetURL . ' </div>';
}


/**
* Retreives all available domains in this installation (plus empty selection)
*
* @return array
*/
public function getDomains()
{
// options array
$options = array( $GLOBALS['TL_LANG']['tl_short_urls']['noDomain'] );

// get the root pages and their dns settings
if( ( $objPages = \PageModel::findPublishedRootPages() ) !== null )
while( $objPages->next() )
if( $objPages->dns )
$options[$objPages->id] = $objPages->dns;

// return the options
return $options;
}
}
2 changes: 2 additions & 0 deletions system/modules/short_urls/languages/de/tl_short_urls.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
$GLOBALS['TL_LANG']['tl_short_urls']['alias'] = 'Zusätzlicher Seiten Alias';
$GLOBALS['TL_LANG']['tl_short_urls']['disable'] = array('Deaktivieren','Deaktiviert diese Short URL.');
$GLOBALS['TL_LANG']['tl_short_urls']['pageExists'] = 'Es existiert bereits eine Seite mit dem Alias "%s".';
$GLOBALS['TL_LANG']['tl_short_urls']['noDomain'] = 'Keine Einschränkung';
$GLOBALS['TL_LANG']['tl_short_urls']['domain'] = array('Domain Einschränkung', 'Die Short URL wird nur bei Aufruf über die angegebene Domain verwendet.');
3 changes: 3 additions & 0 deletions system/modules/short_urls/languages/en/tl_short_urls.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
$GLOBALS['TL_LANG']['tl_short_urls']['alias'] = 'Additional page alias';
$GLOBALS['TL_LANG']['tl_short_urls']['disable'] = array('Disable','Disables this Short URL.');
$GLOBALS['TL_LANG']['tl_short_urls']['pageExists'] = 'There is already a page with the alias "%s".';
$GLOBALS['TL_LANG']['tl_short_urls']['noDomain'] = 'No restriction';
$GLOBALS['TL_LANG']['tl_short_urls']['domain'] = array('Domain restriction', 'The short URL will only be used via the given domain.');

0 comments on commit b112e97

Please sign in to comment.