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

Feature/urlfsmappings #14

Open
wants to merge 4 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
15 changes: 14 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,19 @@ accessible and then using symlinks or a webserver level alias directory to
make just that directory accessible to the outside. You do not want
world-writable directories exposed more than what's absolutely needed.

`SACY_URL_FS_MAPPINGS` is an optional constant that you can define to tell Sacy where to find certain files on your
webserver that are included with a static path. Assuming you want to tell Sacy that all resources included from http://static.yourdomain.com are
located inside /var/www/static on your server, set a serialized associative array to this constant this way:

define(
'SACY_URL_FS_MAPPINGS', serialize(
array(
'http://static.yourdowmain.com' => '/var/www/static',
'anotherstaticpath' => 'anotherfilesystemdir'
)
)
);


Building sacy
-------------
Expand Down Expand Up @@ -610,4 +623,4 @@ licensed under the MIT License which is reprinted in the LICENSE file
accompanying this distribution.

If you find this useful, consider dropping me a line, or,
if you want, a patch :-)
if you want, a patch :-)
22 changes: 18 additions & 4 deletions src/sacy/sacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ private function extract_attrs($attstr){
private function urlToFile($ref){
$u = parse_url($ref);
if ($u === false) return false;

// Let's check if there is a local path mapped to to the requested host.
// If so simulate a local path
if (isset($u['host']) && isset($u['scheme'])
&& array_key_exists($u['scheme'] . '://' . $u['host'], $this->_cfg->get('url_fs_mappings'))
) {
$mapping = $this->_cfg->get('url_fs_mappings');
$u['path'] = $mapping[$u['scheme'] . '://' . $u['host']] . '/' . $u['path'];
unset($u['host'], $u['scheme']);
}

if (isset($u['host']) || isset($u['scheme']))
return false;

Expand All @@ -111,6 +122,8 @@ private function urlToFile($ref){
}




private function extract_style_unit($tag, $attrdata, $content){
$attrs = $this->extract_attrs($attrdata);
$attrs['type'] = strtolower($attrs['type']);
Expand Down Expand Up @@ -215,7 +228,7 @@ public function __construct($params = null){
$this->params['query_strings'] = defined('SACY_QUERY_STRINGS') ? SACY_QUERY_STRINGS : 'ignore';
$this->params['write_headers'] = defined('SACY_WRITE_HEADERS') ? SACY_WRITE_HEADERS : true;
$this->params['debug_toggle'] = defined('SACY_DEBUG_TOGGLE') ? SACY_DEBUG_TOGGLE : '_sacy_debug';

$this->params['url_fs_mappings'] = defined('SACY_URL_FS_MAPPINGS') ? unserialize(SACY_URL_FS_MAPPINGS) : array();
if (is_array($params))
$this->setParams($params);
}
Expand All @@ -233,14 +246,15 @@ public function getDebugMode(){

public function setParams($params){
foreach($params as $key => $value){
if (!in_array($key, array('query_strings', 'write_headers', 'debug_toggle')))
if (!in_array($key, array('query_strings', 'write_headers', 'debug_toggle', 'url_fs_mappings')))
throw new Exception("Invalid option: $key");
}
if (isset($params['query_strings']) && !in_array($params['query_strings'], array('force-handle', 'ignore')))
throw new Exception("Invalid setting for query_strings: ".$params['query_strings']);
if (isset($params['write_headers']) && !in_array($params['write_headers'], array(true, false), true))
throw new Exception("Invalid setting for write_headers: ".$params['write_headers']);

if (isset($params['url_fs_mappings']) && !is_array($params['url_fs_mappings']))
throw new Exception("Invalid format for url_fs_mappings, must be an array");

$this->params = array_merge($this->params, $params);
}
Expand Down Expand Up @@ -618,4 +632,4 @@ function getOutput($work_unit){
));
}
}
}
}