Javascript and PHP in moodle pages

Javascript and PHP in moodle pages

by Andrew Lian -
Number of replies: 3

Hello

I understand that it is possible to include javascript in moodle pages produced as resources inside a course.

Instead of using javascript, I would like to include php code. The problem I have noticed is that any php inserted is transformed into a comment.

I know I could embed an external php page by using iframes. However, I want to be able to control access to the php program through the moodle page (something not really possible with an iframe as the external page's URL would be visible in the page's source code).

Any suggestions would be very appreciated.

Thank you.

Andrew

Average of ratings: -
In reply to Andrew Lian

Re: Javascript and PHP in moodle pages

by Weston Dunn -

I would love to see this also.  My application for this would be to include a php query to an external datase of information.

In reply to Weston Dunn

Re: Javascript and PHP in moodle pages

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

This is not possible, it is very unlikely ever to be made possible, because it would be a big security risk. Letting users type in PHP code is an 'execute arbitrary code on the server' vulnerability, which is about as bad as it gets.

If you want to add PHP code to Moodle, then you need to write some sort of Moodle plugin containing the code, and upload it to your server.

Probably the easiest sort of plugin to write to do this is a filter. http://docs.moodle.org/dev/Filters You could make a filter that looks for input like {{{MY CUSTOM REPORT OUTPUT GOES HERE|XYZZY}}} and replaces it with the result of your custom PHP code. The XYZZY is just a suggestion that you might want to add some random string that is required to be in the input, to make it harder for unauthorised users to trigger the filters.

You can also use the http://docs.moodle.org/22/en/Managing_filters system to control where in Moodle this filter is active.

The other sort of thing you could do is to write a block plugin, but that will only work if you want this custom output displayed ot the side of the page.

In reply to Tim Hunt

Re: Javascript and PHP in moodle pages

by Andrew Lian -

Thank you very much for both you answers and apologies for the delay in relpying. Interbet has been broken for several days.

Agreed about the security but there are some CMS systems that allow admins to add php code.

Anyway... the point is that this does not work for moodle. I will have to try other solutions.

Cheers

Andrew