httpin_config_linker.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /* Released under the GNU GPL 3.0
  3. * This script can be used in your scripts, but you must include this copyright header as per the GPL Licence
  4. * For more information about GPL 3.0 - see: http://www.gnu.org/copyleft/gpl.html
  5. * This script is part of the SLOODLE Project see http://sloodle.org
  6. *
  7. * sloodle/classroom/httpin_config_linker.php
  8. *
  9. * Copyright:
  10. * @contributers Paul G. Preibisch (Fire Centaur in SL)
  11. * @contributers Edmund Edgar
  12. *
  13. * The purpose of this file is to receive a message from a rezzer which is passing an httpinurl for a child object requesting its config.
  14. * this file will search for the child object in active_object table, save its httpin which was sent, and then pass it its config using curl
  15. *
  16. */
  17. /** Lets Sloodle know we are in a linker script. */
  18. define('SLOODLE_LINKER_SCRIPT', true);
  19. /** Grab the Sloodle/Moodle configuration. */
  20. require_once('../init.php');
  21. /** Include the Sloodle PHP API. */
  22. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  23. require_once(SLOODLE_LIBROOT.'/general.php');
  24. // Attempt to authenticate the request
  25. // (only require authentication if controller ID and/or password is set)
  26. $authrequired = (isset($_REQUEST['sloodlecontrollerid']) || isset($_REQUEST['sloodlepwd']));
  27. $sloodle = new SloodleSession();
  28. $request_auth = $sloodle->authenticate_request($authrequired);
  29. // Get the extra parameters
  30. $rezzeruuid = $sloodle->request->required_param('sloodleobjuuid');
  31. $httpinurl = $sloodle->request->required_param('httpinurl');
  32. $childobjectuuid= $sloodle->request->required_param('childobjectuuid');
  33. $controllerid = $sloodle->request->required_param('sloodlecontrollerid');
  34. $objectname = $sloodle->request->optional_param('sloodleobjname');
  35. $extraParams = ($rezzeruuid == $childobjectuuid) ? array() : array( 'sloodlerezzeruuid' => $rezzeruuid );
  36. // An object authorizing itself.
  37. if ($childobjectuuid == $rezzeruuid) {
  38. // configs come from a notecard
  39. $configs = array();
  40. $configs['sloodlecontrollerid'] = $controllerid;
  41. foreach($_POST as $n=>$v) {
  42. if (preg_match('/^set\:(.*)$/', $n, $matches) ) {
  43. $configs[ $matches[1] ] = $v;
  44. }
  45. }
  46. $objecttype = isset($configs['sloodleobjtype']) ? $configs['sloodleobjtype'] : 'unknown';
  47. // This is an object trying to register itself with a notecard.
  48. // Go ahead an do it - it has a legitimate prim password or it would have been stopped already.
  49. $controller = new SloodleController();
  50. if (!$controller->load( $controllerid )) {
  51. $sloodle->response->set_status_code(-217);//Could not save HTTP In URL for rezzed object
  52. $sloodle->response->set_status_descriptor('OBJECT_AUTH');
  53. $sloodle->response->add_data_line('Could not save HTTP In URL for rezzed object');
  54. }
  55. // TODO: Refactor and move this stuff into activeobject
  56. // first time we way it
  57. $ao = new SloodleActiveObject();
  58. if(!$ao->loadByUUID( $childobjectuuid )) {
  59. $primpassword = sloodle_random_prim_password();
  60. $httpinpassword = sloodle_random_prim_password();
  61. if ( !$authid = $controller->register_object($childobjectuuid, $objectname, $sloodle->user, $primpassword, $httpinpassword, $objecttype) ) {
  62. $sloodle->response->set_status_code(-217);//Could not save HTTP In URL for rezzed object
  63. $sloodle->response->set_status_descriptor('OBJECT_AUTH');
  64. $sloodle->response->add_data_line('Could not save HTTP In URL for rezzed object');
  65. }
  66. } else {
  67. $authid = $ao->id;
  68. // already got it - clean out old configs
  69. sloodle_delete_records('sloodle_object_config', 'object', $authid);
  70. }
  71. if (count($configs) > 0) {
  72. foreach($configs as $n => $v) {
  73. $config->id = null;
  74. $config->object = $authid;
  75. $config->name = $n;
  76. $config->value = $v;
  77. if (!sloodle_insert_record('sloodle_object_config',$config)) {
  78. $ok = false;
  79. }
  80. }
  81. }
  82. }
  83. //search for active_object table
  84. //create new active object for found record
  85. $active_object= new SloodleActiveObject();
  86. //loadByUUid will search and find the active object
  87. $sloodle->response->add_data_line($childobjectuuid);
  88. if(!$active_object->loadByUUID( $childobjectuuid )){
  89. // Not a full registration - just the httpinurl and the uuid
  90. $active_object->httpinurl=$httpinurl;
  91. $active_object->uuid=$childobjectuuid;
  92. $active_object->save();
  93. /*
  94. $active_object->response = new SloodleResponse();
  95. $active_object->response->set_status_code(-201);
  96. $active_object->response->set_status_descriptor('OBJECT_AUTH');
  97. $active_object->response->add_data_line('Failed to register new active object.');
  98. */
  99. $renderStr="";
  100. //create message
  101. //send message to httpin
  102. $sloodle->response->set_status_code(1);
  103. $sloodle->response->set_status_descriptor('OBJECT_AUTH');
  104. $sloodle->response->render_to_output();
  105. } else{
  106. // If an object we've never seen before wants to tell us its http-in url
  107. // ...we'll record it for now, but not do the rest of the registration.
  108. // This means the object will get configured even if the request from the object gets to the server
  109. // ...before the object that rezzed it has told us about it.
  110. $active_object->httpinurl=$httpinurl;
  111. if (!$active_object->save()) {
  112. $sloodle->response->set_status_code(-217);//Could not save HTTP In URL for rezzed object
  113. $sloodle->response->set_status_descriptor('OBJECT_AUTH');
  114. $sloodle->response->add_data_line('Could not save HTTP In URL for rezzed object');
  115. }
  116. $async_config = ( defined('SLOODLE_ASYNC_SEND_CONFIG') && SLOODLE_ASYNC_SEND_CONFIG);
  117. //active object is loaded, send config to the object, and also send the rezzeruuid to the object so
  118. //our object also knows its rezzer uuid
  119. $result = $active_object->sendConfig($extraParams, $async_config, $async_config);
  120. //result is an array(curlinfo,curlresult)
  121. //lsl script returns an OK or FAIL
  122. if ( in_array( $result["result"], array('OK','QUEUED') ) ) {
  123. $sloodle->response->set_status_code(1);
  124. $sloodle->response->set_status_descriptor('OK');//httpin config sent properly
  125. }else{
  126. $sloodle->response->set_status_code(-219);//Sending configuration to object via HTTP-in URL failed
  127. $sloodle->response->set_status_descriptor('OBJECT_AUTH');
  128. $sloodle->response->add_data_line('Sending configuration to object via HTTP-in URL failed');
  129. }
  130. $sloodle->response->render_to_output();
  131. }//end else
  132. ?>