blog_linker.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org) and is released under the GNU GPL v3.
  3. /**
  4. * Sloodle blog linker.
  5. *
  6. * Allows the Sloodle Toolbar HUD object in Second Life to write to a user's Moodle blog.
  7. *
  8. * @todo Implement ability to read blog entries too.
  9. *
  10. * @package sloodle
  11. * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  12. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  13. *
  14. * @contributor (various)
  15. * @contributor Peter R. Bloomfield
  16. *
  17. */
  18. // This script is expected to be accessed from in-world.
  19. // The following parameters are required:
  20. //
  21. // sloodlepwd = the password used to authenticate the request (user-centric authentication)
  22. // sloodleuuid = the SL UUID of the agent making the blog entry
  23. // sloodleavname = the name of the avatar making the blog entry
  24. //
  25. // If called with only the above parameters, then the system will simply check the user's
  26. // object authorisation and ability to write blogs.
  27. // The following parameters are needed to write a blog entry:
  28. //
  29. // sloodleblogsubject = the subject line of the blog entry
  30. // sloodleblogbody = the main body of the blog entry
  31. //
  32. // The followign parameter is optional:
  33. //
  34. // sloodleblogvisibility = the visibility of the post: public, site, private (or draft)
  35. //
  36. //
  37. // Returns status code 1 if successful in simply checking authorisation and blogging ability.
  38. // Returns status code 103 is successful in adding blog entry.
  39. // Can also return various blog-specific error codes.
  40. /** Lets Sloodle know we are in a linker script. */
  41. define('SLOODLE_LINKER_SCRIPT', true);
  42. /** Grab the Sloodle/Moodle configuration. */
  43. require_once('../init.php');
  44. /** Include the Sloodle PHP API. */
  45. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  46. // Authenticate the request as user-specific, and load a blog module
  47. $sloodle = new SloodleSession();
  48. $sloodle->authenticate_user_request();
  49. $sloodle->load_module('blog', false); // No database data required
  50. // Attempt to validate the avatar
  51. $sloodle->validate_avatar();
  52. $sloodle->user->login();
  53. // Is blogging enabled?
  54. if (!$sloodle->module->is_enabled()) {
  55. $sloodle->response->quick_output(-10401, 'BLOG', 'Blogging is disabled on this site.', false);
  56. exit();
  57. }
  58. // Check if the user has permission to create blog entries
  59. if (!$sloodle->module->user_can_write()) {
  60. $sloodle->response->quick_output(-10402, 'BLOG', 'User lacks permission to write blog entries.', false);
  61. exit();
  62. }
  63. // Check for our additional parameters
  64. $sloodleblogsubject = $sloodle->request->optional_param('sloodleblogsubject', '');
  65. $sloodleblogbody = $sloodle->request->optional_param('sloodleblogbody', '');
  66. $sloodleblogvisibility = $sloodle->request->optional_param('sloodleblogvisibility', 'site');
  67. // If subject or body parameters were given, then both are required
  68. if (!empty($sloodleblogsubject)) $sloodle->request->required_param('sloodleblogbody');
  69. else if (!empty($sloodleblogbody)) $sloodle->request->required_param('sloodleblogsubject');
  70. else {
  71. // Neither parameter was specified
  72. $sloodle->response->quick_output(1, 'OK', '', false);
  73. exit();
  74. }
  75. // We need to know if all header data was retrieved
  76. $use_slurl = (isset($_SERVER['HTTP_X_SECONDLIFE_REGION']) && isset($_SERVER['HTTP_X_SECONDLIFE_LOCAL_POSITION']));
  77. // Use the HTTP headers added by SL to get the region and position data, and construct a SLurl from them
  78. if ($use_slurl) {
  79. $region = $_SERVER['HTTP_X_SECONDLIFE_REGION'];
  80. $region = substr ( $region,0, strpos($region, '(' ) - 1 );
  81. $position = $_SERVER['HTTP_X_SECONDLIFE_LOCAL_POSITION'];
  82. sloodle_debug('Constructing SLurl...<br/>');
  83. sscanf($position, "(%f, %f, %f)", $x, $y, $z);
  84. $slurl = "http://slurl.com/secondlife/" .$region ."/" .$x ."/" .$y ."/" .$z;
  85. $slurl = '<a href="' .$slurl .'">' .$region .'</a>';
  86. } else {
  87. $slurl = '['.get_string('unknown','sloodle').']';
  88. }
  89. // Construct the final blog body
  90. $sloodleblogbody = get_string('postedfromsl','sloodle').': '.$slurl ."\n\n" .$sloodleblogbody;
  91. // Make all string data safe
  92. $sloodleblogsubject = addslashes(clean_text(stripslashes($sloodleblogsubject), FORMAT_PLAIN));
  93. $sloodleblogbody = addslashes(clean_text(stripslashes($sloodleblogbody), FORMAT_MOODLE));
  94. $sloodleblogvisibility = addslashes(clean_text(stripslashes($sloodleblogvisibility), FORMAT_PLAIN));
  95. // Write the entry to the database
  96. if ($sloodle->module->add_entry($sloodleblogsubject, $sloodleblogbody, $sloodleblogvisibility)) {
  97. $sloodle->response->set_status_code(103);
  98. $sloodle->response->set_status_descriptor('OK');
  99. } else {
  100. $sloodle->response->set_status_code(-1);
  101. $sloodle->response->set_status_descriptor('ERROR');
  102. $sloodle->response->add_data_line('Failed to insert blog entry into database.');
  103. }
  104. // Output the response
  105. $sloodle->response->render_to_output();
  106. exit();
  107. ?>