Source for file lsl.php
Documentation is available at lsl.php
* Sloodle LSL handling library.
* Provides the central API functionality, automatically combining several other API elements.
* @copyright Copyright (c) 2007-8 Sloodle (various contributors)
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
* @contributor Peter R. Bloomfield
// This library expects that the Sloodle config file has already been included
// (along with the Moodle libraries)
/** Include the general Sloodle functionality. */
require_once(SLOODLE_DIRROOT.
'/lib/general.php');
/** Include the Sloodle IO library. */
require_once(SLOODLE_DIRROOT.
'/lib/io.php');
/** Include the Sloodle user management library. */
require_once(SLOODLE_DIRROOT.
'/lib/user.php');
* A helper class to automate many API features.
* Most interaction with the object-oriented parts of the Sloodle API should occur via this class.
* It brings together the other classes, and automatically handles various features.
* Instantiated to the {@link: SloodleUser} type in the class constructor.
* No accessors - should be accessed directly.
* A Sloodle request handling object.
* Instantiated to the {@link: SloodleLSLRequest} type in the class constructor.
* No accessors - should be accessed directly.
* A Sloodle response handling object.
* Instantiated to the {@link: SloodleLSLResponse} type in the class constructor.
* No accessors - should be accessed directly.
* Instantiates member objects.
// Instantiate and link our components
$this->response =
new SloodleLSLResponse();
* Performs an internal 'login' based on user identified in request.
* Checks for avatar UUID and/or name request parameters, and checks for an associated Moodle account.
* If found, the user is logged-in (details stored in Moodle's global $USER variable).
* @param bool $require If true (default), the function will terminate the script with an error message if login fails. (Error message will be LSL-friendly).
* @param bool $suppress_autoreg If true (not default), the function will always suppress auto-registration of new users. Note: this parameter will be ignored if auto-registration is disabled.
* @return bool True if login was successful. False if login failed, but parameter $require was false.
// Make sure the request is processed and authenticated
$this->request->process_request_data(TRUE); // Force re-processing to ensure accurate user data
if (!$this->request->authenticate_request($require)) return FALSE;
// Make sure the items of data were specified
if ($this->request->get_avatar_uuid() ==
NULL &&
$this->request->get_avatar_name() ==
NULL) {
$this->response->set_status_descriptor('USER_AUTH');
$this->response->add_data_line('User not identified in request.');
// Is the user already fully registered?
if ($this->user->get_sloodle_user_id() >
0 &&
$this->user->get_moodle_user_id() >
0) {
// Yes - attempt to login
if (!$this->user->login_moodle_user()) {
$this->response->set_status_descriptor('USER_AUTH');
$this->response->add_data_line('Failed to login Moodle user for unkown reason.');
// Is auto-registration disabled or suppressed?
if (sloodle_is_automatic_registration_on() ==
FALSE ||
$suppress_autoreg ==
TRUE) {
// We cannot do auto-registration
$this->response->set_status_descriptor('USER_AUTH');
$this->response->add_data_line('User not registered, and auto registration was not permitted.');
if ($suppress_autoreg) $this->response->add_data_line('Note: auto registration was explicitly suppressed.');
// Make sure we have both the avatar name and UUID
// [Note: if the user was fully registered with Sloodle and not Moodle, and only name OR UUID was provided,
// then the earlier data processing will have retrieved the missing value... cunning, eh? :-)]
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('User auto registration not possible - avatar UUID was not provided or found in the database.');
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('User auto registration not possible - avatar name was not provided or found in the database.');
// Do we need to register a Moodle account?
if ($this->user->get_moodle_user_id() <=
0) {
// Extract the first and last name parts
$avname =
$this->request->get_avatar_name();
// Expecting that all SL names are first last, with a space in between
if (preg_match('/^(.*)\s(.*?)$/', $avname, $avbits)) {
// Something wasn't quite right about the name
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('User auto registration failed - could not extract first and last names from avatar name "'.
$avname.
'".');
// Register the new Moodle account
$autoreg =
$this->user->create_moodle_user($firstname, $lastname, $this->request->get_avatar_uuid().
'@lsl.secondlife.com');
// Did something go wrong?
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('Moodle user creation failed.');
// Do we need to register a Sloodle entry?
if ($this->user->get_sloodle_user_id() <=
0) {
// Create the Sloodle entry
$newsloodle =
$this->user->create_sloodle_user(
$this->user->get_moodle_user_id()
); // Other parameters can be default
if ($newsloodle !==
TRUE) {
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('Sloodle user creation failed.');
// We already have a Sloodle user
// Just link the users together
$link =
$this->user->link_users();
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('Failed to link new Sloodle and Moodle users together.');
// User has been auto-registered, so add a side-effect code to our response
// Finally, attempt to login the new user
return $this->user->login_moodle_user();
* Authenticates the avatar identified in the request by the login security token.
* Uses the avatar UUID and/or name, and the security token request to parameters to
* check against the details in the database.
* @param bool $use_cache If true (not default) then the function will use the user data stored in Sloodle user cache of the {@link: $user} member object (instead of querying the database for new data).
* @return bool True if authentication was successful, or false if not.
// Was a login security token specified in the request? Do nothing if not
if ($this->request->get_login_security_token() ==
NULL) return FALSE;
// Are we using the cache?
// No - fetch fresh data and ensure it worked
if ($this->update_sloodle_user_cache_from_db() !==
TRUE) return FALSE;
// Make sure the login security token is specified in the cache
if (!isset
($this->user->sloodle_user_cache->loginsecuritytoken)) return FALSE;
// Compare the security tokens
return ($this->request->get_login_security_token() ===
$this->user->sloodle_user_cache->loginsecuritytoken);
* Checks that a user is enrolled in a course, based on request data.
* Fetches user and course identification from the standard request parameters.
* @param bool $require If true (default) then the function will terminate with an LSL-friendly error message if user is not enrolled, or not all data was present.
* @param bool $use_cache If true (not default) then the courses cache in the {@link: $user} member will be used, instead of querying the database for fresh data.
* @return bool True if the user is enrolled, or false otherwise.
// Make sure the course exists
$course =
$this->request->get_course_record($require);
if (!$this->user->is_user_in_course($course->id, $use_cache)) {
$this->response->set_status_descriptor('USER_ENROL');
$this->response->add_data_line('User not enroled in course, and auto-enrolment is not yet implemented.');
Documentation generated on Mon, 16 Jun 2008 15:56:41 +0100 by phpDocumentor 1.4.0