use of $theme->setting_file_url in formal_white

use of $theme->setting_file_url in formal_white

by Daniele Cordella -
Number of replies: 1
Picture of Core developers Picture of Plugin developers

I am looking for a solution in the frame of formal_white (FW) development and I need some opinion from developer sight.

As you may know, in FW, the header logo does not come from a css backround style rule but is an html img tag.
I choosed this way to operate for the following reason: I want the height of the header ALWAYS follow the height of the logo the user may choose.
-> By adding a custom logo via css I have to also provide a rule like "height: the_height_of_the_uploaded_logo px" in order to follow the height of the logo. And, at first sight, I do not have it.
-> By adding a custom logo via html, I do not care the height of the logo because html will resize the image container tag (<div id="headerlogo">) to the optimum size.
This is why in FW I added logo via html and not via css.

Said that, I aimed to add the new file selector:
$setting = new admin_setting_configstoredfile($name, $title, $description, 'logo');
to FW.
I arrived to use
$logourl = $theme->setting_file_url('logo', 'logo');
and I found that this method returns an URL like:
//localhost/HEAD/pluginfile.php/1/theme_formal_white/logo/-1/small.jpg
that is enought for the css but not for the html tag img.
In html img tag I need the complete URL!

So, in my local installation, I changed the method setting_file_url in HEAD/lib/outputlib.php from:

/**
 * Returns URL to the stored file via pluginfile.php.
 *
 * Note the theme must also implement pluginfile.php handler,
 * theme revision is used instead of the itemid.
 *
 * @param string $setting
 * @param string $filearea
 * @return string protocol relative URL or null if not present
 */
public function setting_file_url($setting, $filearea) {
    global $CFG;

    if (empty($this->settings->$setting)) {
        return null;
    }

    $component = 'theme_'.$this->name;
    $itemid = theme_get_revision();
    $filepath = $this->settings->$setting;
    $syscontext = context_system::instance();

    $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php", "/$syscontext->id/$component/$filearea/$itemid".$filepath);

    // Now this is tricky because the we can not hardcode http or https here, lets use the relative link.
    // Note: unfortunately moodle_url does not support //urls yet.
$url = preg_replace('|^https?://|i', '//', $url->out(false)); return $url; }

to

/**
 * Returns URL to the stored file via pluginfile.php.
 *
 * Note the theme must also implement pluginfile.php handler,
 * theme revision is used instead of the itemid.
 *
 * @param string $setting
 * @param string $filearea
 * @return string protocol relative URL or null if not present
 */
public function setting_file_url($setting, $filearea, $useinhtml=false) {
    global $CFG;

    if (empty($this->settings->$setting)) {
        return null;
    }

    $component = 'theme_'.$this->name;
    $itemid = theme_get_revision();
    $filepath = $this->settings->$setting;
    $syscontext = context_system::instance();

    $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php", "/$syscontext->id/$component/$filearea/$itemid".$filepath);

    // Now this is tricky because the we can not hardcode http or https here, lets use the relative link.
    // Note: unfortunately moodle_url does not support //urls yet.

    if (!$useinhtml) {
        $url = preg_replace('|^https?://|i', '//', $url->out(false));
    }

    return $url;
}

Now, of course,
$logourl = $PAGE->theme->setting_file_url('logo', 'logo', true);
returns the complete url I need.

Is this a viable solution?
Is this solution opening a security issue?
If so, how can I manage this problem?

 

Thanks in advance.

Average of ratings: -