Source for file sl_loginzone_entry.php

Documentation is available at sl_loginzone_entry.php

  1. <?php    
  2.     
  3.     /**
  4.     * Sloodle LoginZone entry-point interface script.
  5.     *
  6.     * Provides an entry-point for Moodle users who want to create a new Sloodle loginzone to authenticate their avatar
  7.     *
  8.     * @package sloodlelogin
  9.     * @copyright Copyright (c) 2007 Sloodle (various contributors)
  10.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11.     *
  12.     * @contributor Edmund Edgar
  13.     * @contributor Peter R. Bloomfield
  14.     *
  15.     */
  16.     
  17.     // This script is expected to be accessed manually via a web-browser.
  18.     // The user will be required to login before using it.
  19.     // On loading, if no Sloodle account is already linked to the Moodle account, the script will allocate
  20.     //  and link a new Sloodle user for the logged-in Moodle account.
  21.     // A SLurl will then be provided which the user may use to enter an in-world loginzone.
  22.     // Their position when they login will be relayed back to the server, providing avatar authentication.
  23.     
  24.     require_once('../config.php');
  25.     
  26.     // Make sure the user is logged-in to Moodle
  27.     require_login();
  28.     
  29.     // Construct breadcrumb navigation links
  30.     $navigation "";
  31.     if ($COURSE && $COURSE->id 1$navigation .= "<a href=\"{$CFG->wwwroot}/course/view.php?id={$COURSE->id}\">{$COURSE->shortname}</a> -> ";
  32.     $navigation .= get_string('loginzone:entry''sloodle');
  33.     
  34.     // Display the Moodle page headers
  35.     print_header(get_string('loginzone:entry''sloodle')get_string('loginzone:entry','sloodle')$navigation""""false);
  36.     
  37.     // Make sure it's not a guest who is logged in
  38.     if (isguest()) {
  39.         ?>
  40.         <div style="text-align:center;">
  41.          <h3><?php print_string('error''sloodle')?></h3>
  42.          <p><?php print_string('noguestaccess''sloodle')?></p>
  43.         </div>
  44.         <?php
  45.         print_footer();
  46.         exit();
  47.     }
  48.     
  49.     // Include our other Sloodle libraries
  50.     require_once(SLOODLE_DIRROOT.'/sl_debug.php');
  51.     require_once(SLOODLE_DIRROOT.'/lib/sl_lsllib.php');
  52.     
  53.     // We need to use the Sloodle user functionality
  54.     sloodle_debug_output("Instantiating SloodleUser object...<br/>");
  55.     $user new SloodleUser();
  56.     $user->set_moodle_user_id($USER->id);
  57.     
  58.     //This string will contain the body of our response
  59.     $body "";
  60.     
  61.     // Determine whether or not the user already has a Sloodle account
  62.     sloodle_debug_output("Attempting to find Sloodle user linked with Moodle account...<br/>");
  63.     $has_sloodle_account $user->find_linked_sloodle_user();
  64.     // Determine if the account is authenticated (i.e. has both the avatar name and UUID)
  65.     $sloodle_is_auth FALSE;
  66.     if ($has_sloodle_account{
  67.         $sloodle_is_auth (!(empty($user->sloodle_user_cache->avname)) && !(empty($user->sloodle_user_cache->uuid)));
  68.     }
  69.     
  70.     // Did an error occur?
  71.     if (is_string($has_sloodle_account)) {
  72.         // An error occurred
  73.         sloodle_debug_output("-&gt;An error occurred.<br/>");
  74.         $body .= "<h3>".get_string('error','sloodle')."</h3>\n";
  75.         $body .= "<p>".get_string('errorlinkedsloodleuser''sloodle')."</p>";
  76.         if (SLOODLE_DEBUG && is_string($has_sloodle_account)) {
  77.             $body .= "<p>(DEBUG): $has_sloodle_account</p>";
  78.         }
  79.         
  80.     else if ($has_sloodle_account && $sloodle_is_auth{
  81.         // The user has a Sloodle account and is fully authenticated
  82.         sloodle_debug_output("-&gt;Found Sloodle user - fully authenticated.<br/>");
  83.         
  84.         // Generate an appropriate teleport link outside the loginzone, since the user is already authenticated
  85.         $coord sloodle_finished_login_coordinates();
  86.         $region sloodle_get_loginzone_region();
  87.         if ($region === FALSE || $coord === FALSE{
  88.             $body .= "<p>".get_string('loginzone:datamissing''sloodle')."<br/>\n";
  89.             $body .= get_string('loginzone:mayneedrerez''sloodle')."</p>\n";
  90.         else {
  91.             $link "secondlife://$region/{$coord['x']}/{$coord['y']}/{$coord['z']}";
  92.             $body .= "<p>".get_string('alreadyauthenticated''sloodle')."<br/>\n";
  93.             $body .= get_string('avatarname''sloodle').": ".$user->sloodle_user_cache->avname."<br/>\n";
  94.             $body .= "<a href=\"$link\">".get_string('clicktoteleportanyway''sloodle')."</a></p>\n";
  95.         }
  96.         
  97.     else {
  98.         // We want to know if the registration process is successful
  99.         $result TRUE;
  100.         if ($has_sloodle_account{
  101.             sloodle_debug_output("-&gt;User already has Sloodle account.<br/>");
  102.         else {
  103.             // User has no Sloodle account at all
  104.             sloodle_debug_output("-&gt;No linked Sloodle user.<br/>");
  105.             // Create an empty Sloodle user, linked to the Moodle account
  106.             sloodle_debug_output("Created Sloodle user for Moodle account...<br/>");
  107.             if ($user->create_sloodle_user(''''$user->get_moodle_user_id()) !== TRUE{
  108.                 // Something went wrong
  109.                 sloodle_debug_output("-&gt;Failed to add user to database.<br/>");
  110.                 $body "<p>".get_string('failedcreatesloodleuser''sloode')."</p>\n";
  111.                 $result FALSE;
  112.             else {
  113.                 // Success
  114.                 sloodle_debug_output("-&gt;Success.<br/>");
  115.             }
  116.         }
  117.         
  118.         // This will store our login position
  119.         $loginpos array();
  120.         // Calculate a login position expiry time, 15 minutes from now
  121.         $expires time(900;
  122.         
  123.         // Has the use never been given a login position, or has it expired?
  124.         if ($result && (empty($user->sloodle_user_cache->loginposition|| (int)$user->sloodle_user_cache->loginpositionexpires time())) {
  125.             // Generate a new login zone position
  126.             sloodle_debug_output("Generating new login position.<br/>");
  127.             $loginpos $user->generate_login_position($expires);
  128.             if (is_array($loginpos)) {
  129.                 // Successful allocation
  130.                 sloodle_debug_output("-&gt;Success.<br/>");
  131.             else if ($loginpos === FALSE{
  132.                 // No positions left to allocate
  133.                 sloodle_debug_output("-&gt;Failed.<br/>");
  134.                 $body .= "<p>".get_string('loginzone:allocationfailed','sloodle')."</p>";
  135.                 $result FALSE;
  136.             else {
  137.                 // An error occurred
  138.                 if (is_string($result)) {
  139.                     sloodle_debug_output("-&gt;ERROR: $result.<br/>");
  140.                 else {
  141.                     sloodle_debug_output("-&gt;ERROR: error not specified.<br/>");
  142.                 }
  143.                 $result FALSE;
  144.                 $body .= "<p>".get_string('loginzone:allocationerror','sloodle')."</p>";
  145.             }
  146.             
  147.         else {
  148.             // Renew the expiry time (give it another 15 minutes from now) and use the existing login position
  149.             sloodle_debug_output("Renewing current login-position.<br/>");
  150.             $user->sloodle_user_cache->loginpositionexpires $expires;
  151.             $user->update_sloodle_user_cache_to_db();
  152.             $loginpos sloodle_vector_to_array($user->sloodle_user_cache->loginposition);
  153.         }
  154.         
  155.         // Attempt to get the region for the loginzone
  156.         $region sloodle_get_loginzone_region();
  157.         if ($region === FALSE{
  158.             $body .= "<p>".get_string('loginzone:datamissing''sloodle')."<br/>\n";
  159.             $body .= get_string('loginzone:mayneedrerez''sloodle')."</p>\n";
  160.             $result FALSE;
  161.         }
  162.  
  163.         // Did everything go OK?
  164.         if ($result{
  165.             // Construct the teleport link
  166.             $link "secondlife://$region/{$loginpos['x']}/{$loginpos['y']}/{$loginpos['z']}";
  167.             // Add instructions and the teleport link to the body
  168.             $body .= "<p>".get_string('loginzone:useteleportlink','sloodle')."</p>\n";
  169.             $body .= "<p style=\"font-size:150%;\"><a href=\"$link\">".get_string('loginzone:teleport','sloodle')."</a></p>";
  170.             $body .= "<p>".get_string('loginzone:expirynote''sloodle')."</p>\n";
  171.         }
  172.     }
  173.     
  174.     
  175.     // Output the response
  176.     echo '<div style="text-align:center;">';    
  177.     echo $body;
  178.     echo '</div>';
  179.     
  180.     print_footer();
  181.     exit();
  182. ?>

Documentation generated on Tue, 04 Mar 2008 15:08:47 +0000 by phpDocumentor 1.4.0