Skip to content

Commit

Permalink
Merge pull request #1 from davist11/master
Browse files Browse the repository at this point in the history
Add ability to pull in remote files
  • Loading branch information
aelvan committed Jan 14, 2015
2 parents 6ca8fdd + 399989a commit 2414816
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 100 deletions.
2 changes: 1 addition & 1 deletion InlinPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function getName()

public function getVersion()
{
return '1.0';
return '1.1';
}

public function getDeveloper()
Expand Down
36 changes: 0 additions & 36 deletions InlinService.php

This file was deleted.

20 changes: 0 additions & 20 deletions InlinVariable.php

This file was deleted.

32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
Inlin for Craft
===========

A tiny plugin for inlining files in Craft templates.
A tiny plugin for inlining files in Craft templates.


Usage
---
Use it like this:

<script src="{{ craft.inlin.er('/build/svg/my.svg') | raw }}"></script>

Why? [Sometimes](http://css-tricks.com/svg-sprites-use-better-icon-fonts/) it
[makes sense](http://www.yottaa.com/blog/bid/306224/Inlining-for-Performance-When-to-Let-the-Cache-Go),
<script src="{{ craft.inlin.er('/build/svg/my.svg') | raw }}"></script>

Why? [Sometimes](http://css-tricks.com/svg-sprites-use-better-icon-fonts/) it
[makes sense](http://www.yottaa.com/blog/bid/306224/Inlining-for-Performance-When-to-Let-the-Cache-Go),
performance or workflow wise, to inline resources instead of requesting them.

To include a remote file, pass in true as the second parameter:

{{ craft.inlin.er('http://example.com/remote/path.svg', true) | raw }}

Warning
---
Understand that inserting filedata in your templates, especially when passing it through Twig's raw filter,
is a potential security risk. And the path is relative to your document root, so the path could point to a
file anywhere on your server. **Make sure you never, ever let a third party control what is inserted.**
Understand that inserting filedata in your templates, especially when passing it through Twig's raw filter,
is a potential security risk. And the path is relative to your document root, so the path could point to a
file anywhere on your server. **Make sure you never, ever let a third party control what is inserted.**
In case you're thinking "meh", insert this into your template:

{{ craft.inlin.er('/../craft/config/db.php') | raw }}

*"With great power, comes great responsibility" -Voltaire*
*"With great power, comes great responsibility" -Voltaire*


Configuration
---
Inlin needs to know the public document root to know where your files are located. By default
Inlin will use $_SERVER['DOCUMENT_ROOT'], but on some server configurations this is not the correct
path. You can configure the path by setting the inlinPublicRoot setting in your config file
Inlin will use $_SERVER['DOCUMENT_ROOT'], but on some server configurations this is not the correct
path. You can configure the path by setting the inlinPublicRoot setting in your config file
(usually found in /craft/config/general.php)

####Example

'inlinPublicRoot' => '/path/to/website/public/',


Changelog
---
### Version 1.1
- Added ability to pull remote files

### Version 1.0
- Initial release
34 changes: 16 additions & 18 deletions services/InlinService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,32 @@

class InlinService extends BaseApplicationComponent
{
var $settings = array();

var $settings = array();

/**
* Gets a plugin setting
*
*
* @param $name String Setting name
* @return mixed Setting value
* @author André Elvan
*/
public function getSetting($name) {
$this->settings = $this->_init_settings();
return $this->settings[$name];
}

public function getSetting($name) {
$this->settings = $this->_init_settings();
return $this->settings[$name];
}

/**
* Gets Stamp settings from config
*
*
* @return array Array containing all settings
* @author André Elvan
*/
private function _init_settings() {
$settings = array();
$settings['inlinPublicRoot'] = craft()->config->get('inlinPublicRoot');

return $settings;
}


private function _init_settings() {
$settings = array();
$settings['inlinPublicRoot'] = craft()->config->get('inlinPublicRoot');

return $settings;
}

}
29 changes: 17 additions & 12 deletions variables/InlinVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@

class InlinVariable
{
public function er($fileName)
public function er($fileName, $remote = false)
{
$documentRoot = craft()->inlin->getSetting('inlinPublicRoot')!=null ? craft()->inlin->getSetting('inlinPublicRoot') : $_SERVER['DOCUMENT_ROOT'];
$filePath = $documentRoot . $fileName;

if ($fileName != '' && file_exists($filePath)) {

$content = file_get_contents($filePath);
return $content;

} else {
return '';
}
$documentRoot = craft()->inlin->getSetting('inlinPublicRoot')!=null ? craft()->inlin->getSetting('inlinPublicRoot') : $_SERVER['DOCUMENT_ROOT'];
$filePath = $documentRoot . $fileName;

if ($fileName !== '' && file_exists($filePath)) {

$content = @file_get_contents($filePath);
return $content;

} else if ($remote) {

$content = @file_get_contents($fileName);
return $content;

} else {
return '';
}
}
}

0 comments on commit 2414816

Please sign in to comment.