Skip to content

Commit

Permalink
Fix issue where LanguageSupportPageNames was not compatible with the …
Browse files Browse the repository at this point in the history
…$config->pagefileSecure option.
  • Loading branch information
ryancramerdesign committed Jan 30, 2014
1 parent 35b5be9 commit f5a64e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
45 changes: 39 additions & 6 deletions wire/modules/LanguageSupport/LanguageSupportPageNames.module
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class LanguageSupportPageNames extends WireData implements Module, ConfigurableM
static public function getModuleInfo() {
return array(
'title' => 'Languages Support - Page Names',
'version' => 8,
'summary' => 'Required to use multi-language page names. Beta.',
'version' => 9,
'summary' => 'Required to use multi-language page names.',
'author' => 'Ryan Cramer',
'autoload' => true,
'singular' => true,
Expand Down Expand Up @@ -56,6 +56,12 @@ class LanguageSupportPageNames extends WireData implements Module, ConfigurableM
*/
protected $force404 = null;

/**
* Whether to bypass the functions provided by this module (like for a secure pagefile request)
*
*/
protected $bypass = false;

/**
* Default configuration data
*
Expand Down Expand Up @@ -114,6 +120,8 @@ class LanguageSupportPageNames extends WireData implements Module, ConfigurableM
*
*/
public function ready() {
// bypass means the request was to something in /site/assets/ that has no possibilty of language support
if($this->bypass) return;

$this->checkModuleVersion();
$this->addHookAfter('Page::path', $this, 'hookPagePath');
Expand All @@ -122,6 +130,7 @@ class LanguageSupportPageNames extends WireData implements Module, ConfigurableM
$this->addHook('Page::localName', $this, 'hookPageLocalName');
$this->addHook('Page::localUrl', $this, 'hookPageLocalUrl');
$this->addHook('Page::localPath', $this, 'hookPageLocalPath');


// verify that page path doesn't have mixed languages where it shouldn't
$redirectURL = $this->verifyPath($this->requestPath);
Expand All @@ -148,6 +157,26 @@ class LanguageSupportPageNames extends WireData implements Module, ConfigurableM
if(strlen($prefix)) $this->wire('config')->pageNumUrlPrefix = $prefix;
}

/**
* Is the given path a site assets path? (i.e. /site/)
*
* Determines whether this is a path we should attempt to perform any language processing on.
*
* @param string $path
* @return bool
*
*/
protected function isAssetPath($path) {
$config = $this->wire('config');
// determine if this is a asset request, for compatibility with pagefileSecure
$segments = explode('/', trim($config->urls->assets, '/')); // start with [subdir]/site/assets
array_pop($segments); // pop off /assets, reduce to [subdir]/site
$sitePath = '/' . implode('/', $segments) . '/'; // combine to [/subdir]/site/
$sitePath = str_replace($config->urls->root, '', $sitePath); // remove possible subdir, reduce to: site/
// if it is a request to assets, then don't attempt to modify it
return strpos($path, $sitePath) === 0;
}

/**
* Given a page path, return an updated version that lacks the language segment
*
Expand All @@ -158,8 +187,8 @@ class LanguageSupportPageNames extends WireData implements Module, ConfigurableM
if($path === '/' || !strlen($path)) return $path;
$trailingSlash = substr($path, -1) == '/';
$testPath = trim($path, '/') . '/';
$home = wire('pages')->get(1);
foreach(wire('languages') as $language) {
$home = $this->wire('pages')->get(1);
foreach($this->wire('languages') as $language) {
$name = $language->isDefault() ? $home->get("name") : $home->get("name$language");
if($name == self::HOME_NAME_DEFAULT) continue;
if(!strlen($name)) continue;
Expand Down Expand Up @@ -336,8 +365,12 @@ class LanguageSupportPageNames extends WireData implements Module, ConfigurableM
// save now, since ProcessPageView removes $_GET['it'] when it executes
$it = isset($_GET['it']) ? $_GET['it'] : '';
$this->requestPath = $it;
$it = $this->updatePath($it);
if($it != $this->requestPath) $_GET['it'] = $it;
if($this->isAssetPath($it)) {
$this->bypass = true;
} else {
$it = $this->updatePath($it);
if($it != $this->requestPath) $_GET['it'] = $it;
}
}

/**
Expand Down
9 changes: 4 additions & 5 deletions wire/modules/Process/ProcessPageView.module
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class ProcessPageView extends Process {
if(!isset($it[0]) || $it[0] != '/') $it = "/$it";
if(strpos($it, '//') !== false) return null;

if(wire('config')->pagefileSecure) {
if($this->wire('config')->pagefileSecure) {
$page = $this->checkRequestFile($it);
if(is_object($page)) return $page; // Page or NullPage
}
Expand Down Expand Up @@ -274,7 +274,7 @@ class ProcessPageView extends Process {
*
*/
protected function checkRequestFile(&$it) {
$config = wire('config');
$config = $this->wire('config');

// check for secured filename, method 1: actual file URL, minus leading "." or "-"
if(strpos(rtrim($config->urls->root, '/') . $it, $config->urls->files) === 0) {
Expand Down Expand Up @@ -447,10 +447,9 @@ class ProcessPageView extends Process {
*
* Method is hookable, for instance if you wanted to log 404s.
*
* @param Page|null $page Page that was found if applicable (like if user didn't have permission or $page's template threw the 404)
* If not applicable then NULL will be given instead.
* @param Page|null $page Page that was found if applicable (like if user didn't have permission or $page's template threw the 404). If not applicable then NULL will be given instead.
* @param string $url The URL that the request originated from (like $_SERVER['REQUEST_URI'] but already sanitized)
@ @param bool $triggerReady Whether or not the ready() hook should be triggered.
@ @param bool $triggerReady Whether or not the ready() hook should be triggered (default=false)
* @return string
* @throws WireException
*
Expand Down

0 comments on commit f5a64e3

Please sign in to comment.