Proper way to reference assets from CSS files in plugins.

General discussion of Mantis.

Moderators: Developer, Contributor

Post Reply
bkraul
Posts: 4
Joined: 09 Oct 2008, 13:55

Proper way to reference assets from CSS files in plugins.

Post by bkraul »

With the advent of mantis 1.3 and increased security, it is a little bit more involved to have access to client assets such as images, declared in CSS files included in plugins. The plugin file handling functions offer plugin_file() a function that takes files from the plugin directory and streams them securely through the mantis layer. However this is not available in client-only files such as CSS. I know that the result of plugin_file() is a path to the following php page: plugin_file.php?file=PluginName/path_to_file.

My question is: Is it OK to directly reference plugin_file.php? For example:

PluginName/files/mystyle.css

Code: Select all

.myclass {
background-image: url("plugin_file.php?file=PluginName/mybackground.png");
}
Or is the only way to have to create a php file with a text/css header in order to call the actual function:

PluginName/pages/mystyle_css.php

Code: Select all

<?php header("Content-type: text/css", true); ?>
.myclass {
background-image: url("<?php echo plugin_file('mybackground.png') ?>");
}
My concern lies on whether plugin_file.php is OK to call directly.

Thanks for your help.
cproensa
Posts: 8
Joined: 24 Aug 2015, 11:37

Re: Proper way to reference assets from CSS files in plugins

Post by cproensa »

bkraul wrote: My question is: Is it OK to directly reference plugin_file.php? For example:

PluginName/files/mystyle.css

Code: Select all

.myclass {
background-image: url("plugin_file.php?file=PluginName/mybackground.png");
}
I'd say that it's ok, it gets the work done.
One example of that is in the jQueryUI plugin.
bkraul wrote: Or is the only way to have to create a php file with a text/css header in order to call the actual function:

PluginName/pages/mystyle_css.php

Code: Select all

<?php header("Content-type: text/css", true); ?>
.myclass {
background-image: url("<?php echo plugin_file('mybackground.png') ?>");
}
With that approach, you are linking to a plugin page as if it was the actual css file:

Code: Select all

<link rel="stylesheet" type="text/css" href="http://.../plugin_page.php?page=xxxxxx" />
plugin file(...) is not valid there, because it returns a full filesystem path (which is local to the server). Eg: "/var/www/html/mantis/plugins/basename/files/xxxx.png"
you could creat a function "plugin_url" which creates a proper static url path: "plugins/basename/files/xxxx.png" (with, or without the http://domain part)

Actually, that "plugin_url" probably should be part of the plugin API, as linking to satic content, through static path, makes sense.
Maybe plugin_file serves as an abstraction layer, independent of plugin paths, etc, but a static url, for inmediately used content, has its benefits too.
Post Reply