Access moodlelib functions from outside moodle directory?

Access moodlelib functions from outside moodle directory?

by Deleted user -
Number of replies: 15
When creating a custom program that is outside the Moodle directory (say in my server root) that will include/call functions from say lib/moodlelib.php, will it allow me to use and access those functions directly without providing any authentication credentials?

Thanks
Average of ratings: -
In reply to Deleted user

Re: Access moodlelib functions from outside moodle directory?

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Jeff - If I understand your question correctly, you will need to include the Moodle config.php file into the custom program; otherwise, the calls to moodlelib.php will probably not work unless you make them functions in your custom program. Peace - Anthony
In reply to Anthony Borrow

Re: Access moodlelib functions from outside moodle directory?

by Deleted user -
Thanks Anthony... I understand the requirement for includes, I am just wondering if there is any other 'authorization' that might be required by Moodle... will I somehow have to submit a username & password in order to use the functions, or should they be useable (from outside the Moodle directories, but still on the same server) as long as I have included the appropriate files (like config.php, etc)?

I think the answer is that no authorization will be required.

Thanks
In reply to Deleted user

Re: Access moodlelib functions from outside moodle directory?

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Jeff - As I recall, I believe that when you include the config.php file that if the person is not authenticated it will take care of attempting to authenticate them to Moodle. So authentication will be required. I could be mistaken but give it a shot and see what happens. I'd be interested to know if my inclination is correct. Peace - Anthony
In reply to Anthony Borrow

Re: Access moodlelib functions from outside moodle directory?

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Jeff,
as Anthony says, it depends on the site set up as to whether authentication is required or not. If you have your site set up so that guests can at least view the main Moodle site page then authentication will not be required by just including config.php.

However, dotted throughout the Moodle code are tests on Moodle roles, so if you want to anything that only an administrator could do, such as view all courses (including the hidden ones), you would have to be properly logged in.

Depending on your familiarity with Moodle and your database, you could just access the Moodle database with a short PHP script. For example, at my school we have several Moodle sites, for different academic years and different departments, and the main portal page for the web server contains a script to connect to the MySQL server, get a list of Moodle databases and create a list of links for student to navigate on to the Moodle site they want.

cheers
Gordon
In reply to Gordon Bateson

Re: Access moodlelib functions from outside moodle directory?

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Thanks for the follow up Gordon, I had forgotten about guests. Peace - Anthony
In reply to Anthony Borrow

Re: Access moodlelib functions from outside moodle directory?

by Deleted user -
Thanks Anthony and Gordon...

My site does not allow guest access, so later this week I will try to access the functions and see what happens... sounds like I will need to authenticate though!!

I could easily write a custom db query (I've done that in the past), but that's what I'm trying to avoid. I would prefer to use the functions already built in to Moodle. I know there is a Moodle API on the drawing board, but it does not appear to be implemented yet, so I will start making my own, and maybe some of it could be used in the future API.

Does anybody know how to pass authentication credentials into Moodle from an external program on the same server? Is there just a variable where I need to store an admin username and password, or is it more complicated than that?

Thanks
In reply to Deleted user

Re: Access moodlelib functions from outside moodle directory?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
After require_once('config.php'); just call require_login to ensure the user is authenticated.
In reply to Tim Hunt

Re: Access moodlelib functions from outside moodle directory?

by Deleted user -
require_login actually requires that you be logged into Moodle, so this is not what I am trying to accomplish. I'm talking about a script that is running outside the Moodle directory that is not being used from within Moddle and is not being used while you are logged into Moodle and is not being used from a browser... it is a seperate program running on the same server as Moodle.

I'm looking for a way to make Moodle 'think' that an admin is logged in when in fact nobody is logged in.
In reply to Deleted user

Re: Access moodlelib functions from outside moodle directory?

by Alan Zaitchik -
Can your program send an HTTP POST of username ('admin'), password, and target URL to /login/index.php? (No doubt you should encrypt them and add some lines at the start of /login/index.php to decrypt them.) In that case index.php will (I think) authenticate the caller as admin and redirect to the target URL. If your script is prepared to handle a redirect HTTP response (and whatever other HTTP responses are generated by the target URL), wouldn't that work? Perhaps I have missed something here...
In reply to Deleted user

Re: Access moodlelib functions from outside moodle directory?

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Jeff you don't need to do anything beyond including the config.php file then ... by default all code runs with full privileges (ie like an "admin") ... most of our scripts use functions like require_login and require_capability to *restrict* access only.
In reply to Martin Dougiamas

Re: Access moodlelib functions from outside moodle directory?

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
And if the code wants to run as the real admin (for whatever reasons), this should do it, shouldn't it:


require_once ('path/to/config.php');
$USER = get_admin();
load_all_capabilities();


Saludos. Iñaki.
In reply to Iñaki Arenaza

Re: Access moodlelib functions from outside moodle directory?

by Deleted user -
Thanks for all your replies... after doing some testing, here are my test scripts to help clarify this for anybody else that is new to developing and trying to do something similar.

It appears (per Martins comment) that no authentication is required when running your script from outside the Moodle directory on the same server... just include the config.php file and start using Moodle functions. When you do this though, Moodle does NOT recognize you as an Admin. I'm not sure if that will ever be an issue or not, but Inakis comment above will make Moodle think that you ARE an Admin if that should be necessary.

TEST SCRIPTS:
I created the following script in moodle/admin/my_test.php

<?php
function my_test() {
if(isadmin()) {
$result = 'yes, is an admin';
} else {
$result = 'no, is not admin';
}
return $result;
}
?>


I then created this script in my server root /home/scripts/outside_test.php . This script is run by forwarding (piping) an email to it, and it should send an email back with the result:

<?php
require_once('/home/public_html/moodle/config.php');
require('/home/public_html/moodle/admin/my_test.php');

$USER = get_admin();
load_all_capabilities();
//With the above two lines uncommented, the expected result is "yes, is an admin"
//With the above two lines commented out, the expected result is "no, is not an admin"

$result = 'nothing';
$result = my_test();
mail('myemail@mydomain.com','test results',$result);
?>

In reply to Deleted user

Re: Access moodlelib functions from outside moodle directory?

by Deleted user -
Small correction to the second test script I supplied above... I forgot the file read part:

<?php
$file_number = fopen("php://stdin", "r");
while (!feof($file_number)) {
$read_email .= fread($file_number, 1024);
}
fclose($file_number);

require_once('/home/frtv/public_html/members/config.php');
require('/home/frtv/public_html/members/admin/custom_test.php');

//$USER = get_admin();
//load_all_capabilities();
//With the above two lines uncommented, the expected result is "yes, is an admin"
//With the above two lines commented out, the expected result is "no, is not an admin"

$result = 'nothing';
$result = my_test();
mail('webmaster@forerunnertv.com','test results',$result);
?>

In reply to Deleted user

Re: Access moodlelib functions from outside moodle directory?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Just to point out, this approach is totally insecure. It will run - as admin - for anyone who goes to the right URL.

Much better to insert a call to require_login.
In reply to Tim Hunt

Re: Access moodlelib functions from outside moodle directory?

by Deleted user -
My script is in my server root (outside the www) where moodledata is located, so there is no url for them to get at it!!