rez_object.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // Simulates an ajax object-rezzing request
  3. require_once '../../../lib/json/json_encoding.inc.php';
  4. /** Grab the Sloodle/Moodle configuration. */
  5. require_once('../../../init.php');
  6. /** Include the Sloodle PHP API. */
  7. /** Sloodle core library functionality */
  8. require_once(SLOODLE_DIRROOT.'/lib.php');
  9. /** General Sloodle functions. */
  10. require_once(SLOODLE_LIBROOT.'/io.php');
  11. /** Sloodle course data. */
  12. require_once(SLOODLE_LIBROOT.'/course.php');
  13. require_once(SLOODLE_LIBROOT.'/layout_profile.php');
  14. require_once(SLOODLE_LIBROOT.'/active_object.php');
  15. require_once(SLOODLE_LIBROOT.'/user.php');
  16. //ini_set('display_errors', 1);
  17. //error_reporting(E_ALL);
  18. require_once(SLOODLE_LIBROOT.'/object_configs.php');
  19. $object_configs = SloodleObjectConfig::AllAvailableAsArray();
  20. if (!$USER->id) {
  21. output( 'User not logged in' );
  22. exit;
  23. }
  24. $rezzer = new SloodleActiveObject();
  25. $sloodleuser = new SloodleUser();
  26. $sloodleuser->user_data = $USER;
  27. if (!$layoutentryid = optional_param('layoutentryid', 0, PARAM_INT)) {
  28. error_output( 'Layout ID missing' );
  29. }
  30. if (!$controllerid = optional_param('controllerid', 0, PARAM_INT)) {
  31. error_output( 'Controller ID missing' );
  32. }
  33. $layoutentry = new SloodleLayoutEntry();
  34. if ( !$layoutentry->load( $layoutentryid ) ) {
  35. error_output( 'Layout Entry ID missing' );
  36. }
  37. $controller_context = get_context_instance( CONTEXT_MODULE, $controllerid);
  38. if (!has_capability('mod/sloodle:uselayouts', $controller_context)) {
  39. error_output( 'Access denied');
  40. }
  41. // TODO: Get actual object name via layoutentryid
  42. $objectname = $layoutentry->name;
  43. if ( !$objectname ) {
  44. error_output( 'Layout entry did not have a name');
  45. }
  46. $config = SloodleObjectConfig::ForObjectName( $objectname );
  47. $possibleobjectnames = $config->possibleObjectNames();
  48. $primpassword = sloodle_random_prim_password();
  49. $controller = new SloodleController();
  50. if (!$controller->load( $controllerid )) {
  51. error_output('Could not load controller');
  52. }
  53. if ( !$rezzer->loadByUUID($_REQUEST['rezzeruuid']) ) {
  54. error_output('Could not load rezzer');
  55. }
  56. $rez_http_in_password = sloodle_random_prim_password();
  57. //build response string
  58. $response = new SloodleResponse();
  59. $response->set_status_code(1);
  60. $response->set_status_descriptor('SYSTEM');
  61. $response->set_request_descriptor('REZ_OBJECT');
  62. $response->set_http_in_password($rezzer->httpinpassword);
  63. $response->add_data_line(join('|', $possibleobjectnames)); // object names - rezzer will use the first one it has in its inventory
  64. $response->add_data_line('<0,-1,0>'); // position
  65. $response->add_data_line('<0,0,0>'); // rotation
  66. $response->add_data_line($rez_http_in_password); // This is set as the start parameter. As of 2011-08-08, this is ignored by the object at this point. When we do object persistance, it should be used to prevent people hijacking the object.
  67. // The following are sent to the rezzer just in case the rezzer needs to tell us what it rezzed.
  68. // In the normal case where we rez synchronously we already know it
  69. // ...but if we want to rez asynchronously, it may be useful.
  70. $response->add_data_line($primpassword);
  71. $response->add_data_line($layoutentryid);
  72. //create message - NB for some reason render_to_string changes the string by reference instead of just returning it.
  73. $renderStr="";
  74. $response->render_to_string($renderStr);
  75. //send message to httpinurl
  76. $async = ( defined('SLOODLE_ASYNC_REZZING') && SLOODLE_ASYNC_REZZING );
  77. $reply = $rezzer->sendMessage($renderStr, $async, false, 'rez');
  78. if ($async) {
  79. $result = 'queued';
  80. $error = '';
  81. $content = array(
  82. 'result' => $result,
  83. 'error' => $error,
  84. );
  85. print json_encode($content);
  86. exit;
  87. }
  88. if (!( $reply['info']['http_code'] == 200 ) ) {
  89. error_output('Rezzing failed');
  90. }
  91. $lines = explode("\n",$reply['result']);
  92. $rezzed_object_uuid = $lines[0];
  93. if ( !$authid = $controller->register_object($rezzed_object_uuid, $objectname, $sloodleuser, $primpassword, $rez_http_in_password, $config->type()) ) {
  94. error_output('Object registration failed');
  95. }
  96. if (!$controller->configure_object_from_layout_entry($authid, $layoutentryid, $rezzer->uuid)) {
  97. error_output('Configuration from layout entry failed');
  98. }
  99. // The object may have registered itself and its URL before we got here.
  100. // In that case, send it its config.
  101. // TODO: This uses more db hits than it should - it would be better if register_object returned the object, not just its ID.
  102. $ao = new SloodleActiveObject();
  103. if ($ao->loadByUUID($rezzed_object_uuid)) {
  104. $async_config = ( defined('SLOODLE_ASYNC_SEND_CONFIG') && SLOODLE_ASYNC_SEND_CONFIG);
  105. if ($ao->httpinurl) {
  106. $extraParams = array('sloodlerezzeruuid' => $rezzer->uuid);
  107. $ao->sendConfig( $extraParams, $async_config, $async_config );
  108. }
  109. }
  110. $result = 'rezzed';
  111. $error = '';
  112. $content = array(
  113. 'result' => $result,
  114. 'error' => $error,
  115. );
  116. print json_encode($content);
  117. function error_output($error) {
  118. $content = array(
  119. 'result' => 'failed',
  120. 'error' => $error,
  121. );
  122. print json_encode($content);
  123. exit;
  124. }
  125. ?>