PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/web/message/output/jabber/message_output_jabber.php

https://github.com/dhamma-dev/SEA
PHP | 156 lines | 56 code | 22 blank | 78 comment | 10 complexity | a3c2478baca7aaf8e8db01395d0482ad MD5 | raw file
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.com //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
  10. // //
  11. // This program is free software; you can redistribute it and/or modify //
  12. // it under the terms of the GNU General Public License as published by //
  13. // the Free Software Foundation; either version 2 of the License, or //
  14. // (at your option) any later version. //
  15. // //
  16. // This program is distributed in the hope that it will be useful, //
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  19. // GNU General Public License for more details: //
  20. // //
  21. // http://www.gnu.org/copyleft/gpl.html //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. /**
  25. * Jabber message processor - send a given message by jabber
  26. *
  27. * @author Luis Rodrigues
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  29. * @package
  30. */
  31. require_once($CFG->dirroot.'/message/output/lib.php');
  32. require_once($CFG->libdir.'/jabber/XMPP/XMPP.php');
  33. class message_output_jabber extends message_output {
  34. /**
  35. * Processes the message (sends using jabber).
  36. * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
  37. * @return true if ok, false if error
  38. */
  39. function send_message($eventdata){
  40. global $CFG;
  41. if (message_output_jabber::_jabber_configured()) {
  42. if (!empty($CFG->noemailever)) {
  43. // hidden setting for development sites, set in config.php if needed
  44. debugging('$CFG->noemailever active, no jabber message sent.', DEBUG_MINIMAL);
  45. return true;
  46. }
  47. //hold onto jabber id preference because /admin/cron.php sends a lot of messages at once
  48. static $jabberaddresses = array();
  49. if (!array_key_exists($eventdata->userto->id, $jabberaddresses)) {
  50. $jabberaddresses[$eventdata->userto->id] = get_user_preferences('message_processor_jabber_jabberid', $eventdata->userto->email, $eventdata->userto->id);
  51. }
  52. $jabberaddress = $jabberaddresses[$eventdata->userto->id];
  53. $jabbermessage = fullname($eventdata->userfrom).': '.$eventdata->smallmessage;
  54. if (!empty($eventdata->contexturl)) {
  55. $jabbermessage .= "\n".get_string('view').': '.$eventdata->contexturl;
  56. }
  57. $jabbermessage .= "\n(".get_string('noreply','message').')';
  58. $conn = new XMPPHP_XMPP($CFG->jabberhost,$CFG->jabberport,$CFG->jabberusername,$CFG->jabberpassword,'moodle',$CFG->jabberserver);
  59. try {
  60. //$conn->useEncryption(false);
  61. $conn->connect();
  62. $conn->processUntil('session_start');
  63. $conn->presence();
  64. $conn->message($jabberaddress, $jabbermessage);
  65. $conn->disconnect();
  66. } catch(XMPPHP_Exception $e) {
  67. debugging($e->getMessage());
  68. return false;
  69. }
  70. }
  71. //note that we're reporting success if message was sent or if Jabber simply isnt configured
  72. return true;
  73. }
  74. /**
  75. * Creates necessary fields in the messaging config form.
  76. * @param object $mform preferences form class
  77. */
  78. function config_form($preferences){
  79. global $CFG;
  80. if (!message_output_jabber::_jabber_configured()) {
  81. return get_string('notconfigured','message_jabber');
  82. } else {
  83. return get_string('jabberid', 'message_jabber').': <input size="30" name="jabber_jabberid" value="'.$preferences->jabber_jabberid.'" />';
  84. }
  85. }
  86. /**
  87. * Parses the form submitted data and saves it into preferences array.
  88. * @param object $mform preferences form class
  89. * @param array $preferences preferences array
  90. */
  91. function process_form($form, &$preferences){
  92. if (isset($form->jabber_jabberid)) {
  93. $preferences['message_processor_jabber_jabberid'] = $form->jabber_jabberid;
  94. }
  95. }
  96. /**
  97. * Loads the config data from database to put on the form (initial load)
  98. * @param array $preferences preferences array
  99. * @param int $userid the user id
  100. */
  101. function load_data(&$preferences, $userid){
  102. $preferences->jabber_jabberid = get_user_preferences( 'message_processor_jabber_jabberid', '', $userid);
  103. }
  104. /**
  105. * Tests whether the Jabber settings have been configured
  106. * @return boolean true if Jabber is configured
  107. */
  108. private function _jabber_configured() {
  109. global $CFG;
  110. return (!empty($CFG->jabberhost) && !empty($CFG->jabberport) && !empty($CFG->jabberusername) && !empty($CFG->jabberpassword));
  111. }
  112. }
  113. /*
  114. *
  115. * $f = fopen('/tmp/event_jabberx', 'a+');
  116. fwrite($f, date('l dS \of F Y h:i:s A')."\n");
  117. fwrite($f, "from: $message->userfromid\n");
  118. fwrite($f, "userto: $message->usertoid\n");
  119. fwrite($f, "subject: $message->subject\n");
  120. fclose($f);
  121. $savemessage = new stdClass();
  122. $savemessage->useridfrom = 3;
  123. $savemessage->useridto = 2;
  124. $savemessage->subject = 'IM';
  125. $savemessage->fullmessage = 'full';
  126. $savemessage->timecreated = time();
  127. $a = new message_output_jabber();
  128. $a->send_message($savemessage);
  129. * */