PHP include in a theme

PHP include in a theme

by Richard Oelmann -
Number of replies: 11
Picture of Core developers Picture of Plugin developers Picture of Testers

I am trying to achieve something similar to what Mary was looking for in this previous discussion http://moodle.org/mod/forum/discuss.php?d=159348 but in Moodle2.

I am working on a way to allow admins with little/no server access to save a file including settings and to incorporate that into the theme. I have my idea working with the file in the theme folder and using a php include ("myfile.php") there, and also from setting the file name through the settings page (so long as I'm just giving it the file name and it is already stored in the theme directory) but I want to be able to "include" a file saved by the admin for instance in the main menu block on the front page.

Can anyone help with this with the way Moodle2 references these files?

Richard

Edit: If I try to reference the file that I uploaded to the main menu block I get the error below

Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /var/www/moodle/theme/flexi3/lib.php

while trying to use $CFG->wwwroot gives the same error, and $CFG->dataroot says no such file exists so I'm not sure I'm referencing the file properly.

Average of ratings: -
In reply to Richard Oelmann

Re: PHP include in a theme

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

That example you gave, of what I was trying to do and succeeded in doing, was to allow an Admin to be able to edit a text file in a Moodledate folder which in turn was included into the theme's header.html file, and consequently displayed in the box slider on the frontpage. This is still working as far as I know! But that was in the days that you could add images and files to your Moodledata folder via Moodle.

That same example, if you recall, you helped me solve again by adding the text and related images via a settings page in the Moodle 2.0 version of that theme. And this is also working well - Ya! soacoatoa smile

So can this 'new idea', you are trying to solve, not also work as a setting in it's own right?

I bet it could with a bit of PHP and a bit of Moodle Magic.

For instance, Admin could upload a file as a resource. Find out the url of this resouce by selecting it in the page they created for it.  COPY & PASTE the URL into a Custom URL textarea called FileURL (fileurl) in the theme's Custom settings page. This then would be added into the waiting <?php include ('$fileurl') ?> where $fileurl = $PAGE->theme->settings->fileurl; Would that not work just as well?

Or is what you are tying to do more complex?

Or am I living in cloud cuckoo land? LOL

Cheers

Mary

In reply to Mary Evans

Re: PHP include in a theme

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Mary,

Yes, its the work we did on the box slider and then soa that first gave me the idea for this. And yes, what you've outlined is exactly what I've tried to implement, with the admin uploading a file then pasting that url into a setting and using that to access the php include. The idea is that I have set the defaults for the lib.php that processes the css settings, and that the admin can upload and reference a file which can override those default css settings which then gets read into lib.php - but is then in turn overridden by any settings added to the sttings page. As I said, I've got it working the way I want using a file stored in the theme folder, but I'd like to be able to reference one that an admin can store on the site without server access as a way of simplifying the settings process and allowing transfer of settings from one setup to another :D

The problem I'm having is that it doesn't work. It keeps telling me that http wrapper is disallowed for include on the server and if I try using $CFG to point at the file it tells me the file doesn't exist - I think because of the way Moodle2 references the files in the moodledata filestore. I've tried using ini_set to override the php settings and allow the http reference but that doesn't work either. So I'm left with two possibilities - either my server is unusual in blocking the http wrapper (although it is a straight apache, php, mysql local host build on Linux - using the individual components as per a production server rather than XAMPP) and I can reset the php_ini config settings - but I don't want to rely on that for a theme if the settings are standard as I assumed they were. Or I need to find out how to reference a file correctly in the moodledata folder as I can't seem to do it properly using $CFG->dataroot.

The URL for my file is http://localhost/moodle/mod/resource/view.php?id=2 which resolves to http://localhost/moodle/pluginfile.php/21/mod_resource/content/5/userdefaultsettings.php when the file is displayed.

Richard

In reply to Richard Oelmann

Re: PHP include in a theme

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

The URL I got was similar to the one you have here...

http://localhost/moodle/pluginfile.php/21/mod_resource/content/5/userdefaultsettings.php

Is this the URl you are trying to use while testing?



In reply to Mary Evans

Re: PHP include in a theme

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Yes, I've tried it with that and with the view.php?id=2 one as well.

I'm kind of hoping its just my server setup blocking it and that it works normally smile

Rich

In reply to Richard Oelmann

Re: PHP include in a theme

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

In that case you could try...

$fileurl = new moodle_url('/pluginfile.php/21/mod_resource/content/5/userdefaultsettings.php');

requires('$fileurl');

In reply to Mary Evans

Re: PHP include in a theme

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Thanks Mary, but that comes up with a require($fileurl): failed to open stream: No such file or directory error thoughtful

In reply to Richard Oelmann

Re: PHP include in a theme

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

A couple of changes to the code (require instead of requires and removing the ' ' around the $fileurl in the require - it still doesn't work, but now I'm back to the other error

        $fileurl = new moodle_url('/pluginfile.php/21/mod_resource/content/5/userdefaultsettings.php');
        require($fileurl);

 

http:// wrapper is disabled in the server configuration by allow_url_include=0

So I'm thinking that maybe I need to research how to use the ini_set option in php perhaps - I did try it and it didn't work, but I may not be using it properly, so some more research needed smile

Thanks for your help Mary.

Richard

In reply to Richard Oelmann

Re: PHP include in a theme

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Rich

I am moving this to the developer forum where you may get a better answer.

Cheers

Mary

In reply to Mary Evans

Re: PHP include in a theme

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Thanks Mary - Probably the best place for it as although my purpose is theme related, the actual issue I'm having doesn't seem to be smile

Rich

In reply to Richard Oelmann

Re: PHP include in a theme

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

A bit more research and reading about the php include option I was trying to use shows that maybe its not such a good idea - although it would work the way I wanted it to (if I could get it working) the reason it's secured down on most sites is to prevent the injection of malicious code, so I'm guessing I really don't want to do that.

But I'm still looking for an alternative.

Basically what I was looking for is to have a section at the beginning of my theme lib.php where I set the defaults for the settings, I want to be able to read in a set of user defined defaults to override the theme ones, where the site admin (with no server access - only moodle front end) can upload a document containing a list of settings and values to override the defaults, which can in its turn be overridden by the actual settings page. The goal being to make those settings transferable between moodle installations.

Richard

In reply to Richard Oelmann

Re: PHP include in a theme

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

OK - One problem solved: I've used file_get_contents and a switch/case statement group to set the variables. However, this seems to open up another issue where the lib.php in the theme appears to be processed multiple times.

I'm going to repost in the Themes forum as I think I'm back to a technical theming issue with this, but if any of the general developers are able to assist I would appreciate it (I'll return and post the link as soon as I've posted the other discussion)

Thanks for any assistance

Richard

Edit: Discussion in Themes forum here