Source for file sl_send_object_linker.php

Documentation is available at sl_send_object_linker.php

  1. <?php
  2.     /**
  3.     * Sloodle object distribution linker script.
  4.     *
  5.     * Can be called by scripts to request in-world distribution of objects.
  6.     *
  7.     * @package sloodledistrib
  8.     * @copyright Copyright (c) 2008 Sloodle (various contributors)
  9.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10.     *
  11.     * @contributor Edmund Edgar
  12.     * @contributor Peter R. Bloomfield
  13.     *
  14.     */
  15.         
  16.     // This script should only be accessed by a browser (a script linker version can be found at "sl_send_object_linker.php")
  17.     // There are two modes: query and sendobject.
  18.     // The following parameters are required:
  19.     //
  20.     //   sloodlepwd = prim password for authenticating the request
  21.     //   sloodlecmd = indicates what action should be carried out (can be 'query' or 'sendobject')
  22.     //
  23.     // The following parameters are required if 'sloodlecmd' is set to 'sendobject':
  24.     //    
  25.     //   sloodleuuid = UUID of avatar to whom to send an object (NOTE: avatar does NOT need to be in the Sloodle database to use this!)
  26.     //   sloodleavname = name of avatar to whom to send an object (NOTE: avatar DOES need to be in the Sloodle database to use this!)
  27.     //   sloodleobject = name of the object to send
  28.     //
  29.     
  30.     // In 'query' mode, this script will return a list of the names of all objects available for distribution.
  31.     // Success status code is 1. Each object name will be on its own line following the status line, e.g.:
  32.     //
  33.     //   1|OK
  34.     //   Sloodle Set v0.701
  35.     //   Sloodle Toolbar v1.2 (customizable)
  36.     //   Sloodle Toolbar v1.2 (sloodle.org)
  37.     //
  38.     // NOTE: if not objects are available, then success code 1 is still returned, but no objects are listed in the data lines.
  39.     //
  40.     //
  41.     // In 'sendobject' mode, this script will return status code 1 if successful.
  42.     // There will be a single data line containing the name of the sent object.
  43.     // The status line will contain the UUID of the avatar to whom the object was sent.
  44.     //
  45.     //
  46.     // If the requested object is not listed in the database, then -812 is returned.
  47.     // If the distribution channel cannot be found, then status code -103 is returned.
  48.     // If the object distribution fails (e.g. the XML-RPC could not connect) then -105 is returned.
  49.     // If the 'sloodlecmd' value is not recognised, then -811 is returned.
  50.     //    
  51.     
  52.     
  53.     require_once('../config.php');
  54.     require_once(SLOODLE_DIRROOT.'/sl_debug.php');    
  55.     require_once(SLOODLE_DIRROOT.'/lib/sl_lsllib.php');
  56.     
  57.     // Construct the LSL handler, and process the basic request data
  58.     sloodle_debug_output('Constructing LSL handler...<br/>');
  59.     $lsl new SloodleLSLHandler();
  60.     sloodle_debug_output('Processing basic request data...<br/>');
  61.     $lsl->request->process_request_data();
  62.     
  63.     // Ensure the request is authenticated
  64.     sloodle_debug_output('Authenticating request...<br/>');
  65.     $lsl->request->authenticate_request();
  66.     
  67.     // Fetch other additional parameters
  68.     sloodle_debug_output('Fetching additional parameters...<br/>');
  69.     $sloodlecmd $lsl->request->required_param('sloodlecmd'PARAM_RAW);
  70.     $sloodleobject optional_param('sloodleobject'NULLPARAM_RAW);
  71.     
  72.     
  73.     // Make sure the command is recognised
  74.     sloodle_debug_output('Checking mode...<br/>');
  75.     $sloodlecmd strtolower($sloodlecmd);
  76.     if ($sloodlecmd != 'query' && $sloodlecmd != 'sendobject'{
  77.         $lsl->response->quick_output(-811"DISTRIB""Parameter 'sloodlecmd' gave unrecognised script mode: '$sloodlecmd'"FALSE);
  78.         exit();
  79.     }    
  80.         
  81.     // Fetch the key for the XMLRPC channel
  82.     sloodle_debug_output('Getting the distribution channel...<br/>');
  83.     $distribchannel sloodle_get_config('sloodle_distrib_channel');
  84.     if (($distribchannel == NULL|| ($distribchannel == ''|| ($distribchannel == FALSE)) {
  85.         $lsl->response->quick_output(-103"DISTRIB""XML-RPC distribution channel not found in database."FALSE);
  86.         exit();
  87.     }
  88.     
  89.     // Fetch the list of objects
  90.     sloodle_debug_output('Fetching list of distribution objects...<br/>');
  91.     $distribobjects sloodle_get_distribution_list();
  92.     if (!is_array($distribobjects|| count($distribobjects== 0{
  93.         // No objects available - check which mode we're in to determine our response
  94.         if ($sloodlecmd == 'query'{
  95.             $lsl->response->quick_output(1"DISTRIB"""FALSE);
  96.         else {
  97.             $lsl->response->quick_output(-812"DISTRIB""No objects available for distribution."FALSE);
  98.         }
  99.         exit();
  100.     }
  101.     sort($distribobjectsSORT_STRING);
  102.     
  103.     
  104.     // What command was specified?
  105.     sloodle_debug_output('Checking mode type...<br/>');
  106.     switch ($sloodlecmd{
  107.     case 'query':
  108.         sloodle_debug_output('***** QUERY MODE *****<br/>');
  109.         // Prepare the response
  110.         sloodle_debug_output('Preparing response...<br/>');
  111.         $lsl->response->set_status_code(1);
  112.         $lsl->response->set_status_descriptor("DISTRIB");
  113.         // Go through each distribution object, and add it as a data line
  114.         sloodle_debug_output('Iterating through each available object...<br/>');
  115.         foreach($distribobjects as $obj{
  116.             $lsl->response->add_data_line($obj);
  117.         }
  118.        
  119.         break;
  120.         
  121.      case 'sendobject':
  122.         sloodle_debug_output('***** SENDOBJECT MODE *****<br/>');
  123.         // Send an object to the specified user
  124.         
  125.         // Was the UUID  ommitted?
  126.         sloodle_debug_output('Checking for UUID...<br/>');
  127.         if ($lsl->request->get_avatar_uuid(== NULL{
  128.             // Yes - make sure the avatar name was specified
  129.             sloodle_debug_output('-&gt; UUID not specified.<br/>');
  130.             if ($lsl->request->get_avatar_name(== NULL{
  131.                 $lsl->response->quick_output(-311"USER_AUTH""Neither the avatar name or UUID were specified."FALSE);
  132.                 exit();
  133.             }
  134.             
  135.             // The request object would have found the matching UUID when asked to process data. The user is obviously not in the database.
  136.             $lsl->response->quick_output(-321"USER_AUTH""Failed to find named avatar in Moodle database."FALSE);
  137.             exit();
  138.         }
  139.         
  140.         // Make sure the object was specified
  141.         $lsl->request->required_param('sloodleobject'PARAM_RAW);
  142.         // Make sure it was found in the object list
  143.         sloodle_debug_output('Ensuring object is available...<br/>');
  144.         if (array_search($sloodleobject$distribobjects=== FALSE{
  145.             $lsl->response->quick_output(-812"DISTRIB""Requested object not available for distribution."FALSE);
  146.             exit();
  147.         }
  148.         
  149.         // Construct our XML-RPC data message
  150.         sloodle_debug_output('Preparing XMLRPC message...<br/>');
  151.         $response new SloodleLSLResponse();
  152.         $response->set_status_code(1);
  153.         $response->set_status_descriptor('OK');
  154.         $response->add_data_line(array('SENDOBJECT'$lsl->request->get_avatar_uuid()$sloodleobject));
  155.         // Render it to a string so we can send it
  156.         $str '';
  157.         $response->render_to_string($str);
  158.         $str str_replace("\n""\\n"$str)// Ugly hack to stop the newline character disappearing before it reaches SL
  159.         
  160.         // Send the XMLRPC
  161.         sloodle_debug_output('Sending XMLRPC message...<br/>');
  162.         $ok sloodle_send_xmlrpc_message($distribchannel0$str);
  163.         
  164.         // What was the result?
  165.         sloodle_debug_output('Checking response...<br/>');
  166.         if ($ok{
  167.             sloodle_debug_output('-&gt; OK<br/>');
  168.             $lsl->response->set_status_code(1);
  169.             $lsl->response->set_status_descriptor("DISTRIB");
  170.             $lsl->response->add_data_line($sloodleobject);
  171.         else {
  172.             sloodle_debug_output('-&gt; Failed.<br/>');
  173.             $lsl->response->set_status_code(-105);
  174.             $lsl->response->set_status_descriptor("DISTRIB");
  175.             $lsl->response->add_data_line("XML-RPC distribution failed.");
  176.         }
  177.         break;        
  178.     }
  179.     
  180.     // Output the response
  181.     sloodle_debug_output('Outputting response...<br/>');
  182.     sloodle_debug_output("<pre>");
  183.     $lsl->response->render_to_output();
  184.     sloodle_debug_output("</pre>");
  185.     
  186.     exit();
  187.  
  188. ?>

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