PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/message/refresh.php

https://github.com/nadavkav/MoodleTAO
PHP | 115 lines | 93 code | 19 blank | 3 comment | 8 complexity | f5b22c083515c54c7d094f462464e3db MD5 | raw file
  1. <?php // $Id$
  2. require('../config.php');
  3. define('MESSAGE_DEFAULT_REFRESH', 5);
  4. require_login();
  5. if (isguest()) {
  6. redirect($CFG->wwwroot);
  7. }
  8. if (empty($CFG->messaging)) {
  9. error("Messaging is disabled on this site");
  10. }
  11. /// Script parameters
  12. $userid = required_param('id', PARAM_INT);
  13. $userfullname = strip_tags(required_param('name', PARAM_RAW));
  14. $wait = optional_param('wait', MESSAGE_DEFAULT_REFRESH, PARAM_INT);
  15. $stylesheetshtml = '';
  16. foreach ($CFG->stylesheets as $stylesheet) {
  17. $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
  18. }
  19. header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
  20. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  21. header('Cache-Control: no-cache, must-revalidate');
  22. header('Pragma: no-cache');
  23. header('Content-Type: text/html; charset=utf-8');
  24. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n";
  25. echo '<html><head><title> </title>';
  26. echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
  27. echo '<script type="text/javascript">'."\n";
  28. echo '<!--'."\n";
  29. echo 'if (parent.messages.document.getElementById("messagestarted") == null) {'."\n";
  30. echo ' parent.messages.document.close();'."\n";
  31. echo ' parent.messages.document.open("text/html","replace");'."\n";
  32. echo ' parent.messages.document.write("<html><head><title> <\/title>");'."\n";
  33. echo ' parent.messages.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");'."\n";
  34. echo ' parent.messages.document.write("<base target=\"_blank\" />");'."\n";
  35. echo ' parent.messages.document.write("'.addslashes_js($stylesheetshtml).'");'."\n";
  36. echo ' parent.messages.document.write("<\/head><body class=\"message course-1\" id=\"message-messages\"><div style=\"display: none\" id=\"messagestarted\">&nbsp;<\/div>");'."\n";
  37. echo '}'."\n";
  38. @ob_implicit_flush(true);
  39. @ob_end_flush();
  40. if ($messages = get_records_select('message', "useridto = '$USER->id' AND useridfrom = '$userid'",
  41. 'timecreated')) {
  42. foreach ($messages as $message) {
  43. $time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
  44. $options = new object();
  45. $options->para = false;
  46. $options->newlines = true;
  47. $printmessage = format_text($message->message, $message->format, $options);
  48. $printmessage = '<div class="message other"><span class="author">'.s($userfullname).'</span> '.
  49. '<span class="time">['.$time.']</span>: '.
  50. '<span class="content">'.$printmessage.'</span></div>';
  51. $printmessage = addslashes_js($printmessage); // So Javascript can write it
  52. echo "parent.messages.document.write('".$printmessage."');\n";
  53. /// Move the entry to the other table
  54. $message->timeread = time();
  55. $message = addslashes_object($message);
  56. $messageid = $message->id;
  57. unset($message->id);
  58. if (insert_record('message_read', $message)) {
  59. delete_records('message', 'id', $messageid);
  60. }
  61. }
  62. if (get_user_preferences('message_beepnewmessage', 0)) {
  63. $playbeep = true;
  64. }
  65. echo 'parent.messages.scroll(1,5000000);'."\n";
  66. echo 'parent.send.focus();'."\n";
  67. $wait = MESSAGE_DEFAULT_REFRESH;
  68. } else {
  69. if ($wait < 300) { // Until the wait is five minutes
  70. $wait = ceil(1.2 * (float)$wait); // Exponential growth
  71. }
  72. }
  73. echo '-->'."\n";
  74. echo '</script>'."\n";
  75. echo '</head>'."\n";
  76. echo '<body>'."\n";
  77. if (!empty($playbeep)) {
  78. echo '<embed src="bell.wav" autostart="true" hidden="true" name="bell" />';
  79. echo '<script type="text/javascript">'."\n";
  80. echo '<!--'."\n";
  81. echo 'parent.send.focus();'."\n";
  82. echo '-->'."\n";
  83. echo '</script>'."\n";
  84. }
  85. // Javascript for Mozilla to cope with the redirect bug from editor being on in this page
  86. ?>
  87. <script type="text/javascript">
  88. <!--
  89. function redirect() {
  90. document.location.replace('refresh.php?id=<?php echo $userid ?>&name=<?php echo urlencode($userfullname) ?>&wait=<?php echo $wait ?>');
  91. }
  92. setTimeout("redirect()", <?php echo ($wait*1000) ?>);
  93. -->
  94. </script>
  95. </body>
  96. </html>