Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 39 and 311]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Jabber message processor to send messages by jabber
  19   *
  20   * @package    message_jabber
  21   * @copyright  2008 Luis Rodrigues
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  23   */
  24  
  25  require_once($CFG->dirroot.'/message/output/lib.php');
  26  /**
  27   * The jabber message processor
  28   *
  29   * @package   message_jabber
  30   * @copyright 2008 Luis Rodrigues
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class message_output_jabber extends message_output {
  34  
  35      /**
  36       * Processes the message and sends a notification via jabber
  37       *
  38       * @param stdClass $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
  39       * @return true if ok, false if error
  40       */
  41      function send_message($eventdata){
  42          global $CFG;
  43  
  44          // Skip any messaging of suspended and deleted users.
  45          if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) {
  46              return true;
  47          }
  48  
  49          if (!empty($CFG->noemailever)) {
  50              // hidden setting for development sites, set in config.php if needed
  51              debugging('$CFG->noemailever is active, no jabber message sent.', DEBUG_MINIMAL);
  52              return true;
  53          }
  54  
  55          if (PHPUNIT_TEST) {
  56              // No connection to external servers allowed in phpunit tests.
  57              return true;
  58          }
  59  
  60          //hold onto jabber id preference because /admin/cron.php sends a lot of messages at once
  61          static $jabberaddresses = array();
  62  
  63          if (!array_key_exists($eventdata->userto->id, $jabberaddresses)) {
  64              $jabberaddresses[$eventdata->userto->id] = get_user_preferences('message_processor_jabber_jabberid', null, $eventdata->userto->id);
  65          }
  66          $jabberaddress = $jabberaddresses[$eventdata->userto->id];
  67  
  68          //calling s() on smallmessage causes Jabber to display things like &lt; Jabber != a browser
  69          $jabbermessage = fullname($eventdata->userfrom).': '.$eventdata->smallmessage;
  70  
  71          if (!empty($eventdata->contexturl)) {
  72              $jabbermessage .= "\n".get_string('view').': '.$eventdata->contexturl;
  73          }
  74  
  75          $jabbermessage .= "\n(".get_string('noreply','message').')';
  76  
  77          $conn = new \BirknerAlex\XMPPHP\XMPP($CFG->jabberhost,$CFG->jabberport,$CFG->jabberusername,$CFG->jabberpassword,'moodle',$CFG->jabberserver);
  78  
  79          // No need to track the presence during the sending message process.
  80          $conn->track_presence = false;
  81  
  82          try {
  83              //$conn->useEncryption(false);
  84              $conn->connect();
  85              $conn->processUntil('session_start');
  86              $conn->presence();
  87              $conn->message($jabberaddress, $jabbermessage);
  88              $conn->disconnect();
  89          } catch(\BirknerAlex\XMPPHP\Exception $e) {
  90              debugging($e->getMessage());
  91              return false;
  92          }
  93          return true;
  94      }
  95  
  96      /**
  97       * Creates necessary fields in the messaging config form.
  98       *
  99       * @param array $preferences An array of user preferences
 100       */
 101      function config_form($preferences){
 102          global $CFG;
 103  
 104          if (!$this->is_system_configured()) {
 105              return get_string('notconfigured','message_jabber');
 106          } else {
 107              return get_string('jabberid', 'message_jabber').': <input size="30" name="jabber_jabberid" value="'.s($preferences->jabber_jabberid).'" />';
 108          }
 109      }
 110  
 111      /**
 112       * Parses the submitted form data and saves it into preferences array.
 113       *
 114       * @param stdClass $form preferences form class
 115       * @param array $preferences preferences array
 116       */
 117      function process_form($form, &$preferences){
 118          if (isset($form->jabber_jabberid) && !empty($form->jabber_jabberid)) {
 119              $preferences['message_processor_jabber_jabberid'] = $form->jabber_jabberid;
 120          }
 121      }
 122  
 123      /**
 124       * Loads the config data from database to put on the form during initial form display
 125       *
 126       * @param array $preferences preferences array
 127       * @param int $userid the user id
 128       */
 129      function load_data(&$preferences, $userid){
 130          $preferences->jabber_jabberid = get_user_preferences( 'message_processor_jabber_jabberid', '', $userid);
 131      }
 132  
 133      /**
 134       * Tests whether the Jabber settings have been configured
 135       * @return boolean true if Jabber is configured
 136       */
 137      function is_system_configured() {
 138          global $CFG;
 139          return (!empty($CFG->jabberhost) && !empty($CFG->jabberport) && !empty($CFG->jabberusername) && !empty($CFG->jabberpassword));
 140      }
 141  
 142      /**
 143       * Tests whether the Jabber settings have been configured on user level
 144       * @param  object $user the user object, defaults to $USER.
 145       * @return bool has the user made all the necessary settings
 146       * in their profile to allow this plugin to be used.
 147       */
 148      function is_user_configured($user = null) {
 149          global $USER;
 150  
 151          if (is_null($user)) {
 152              $user = $USER;
 153          }
 154          return (bool)get_user_preferences('message_processor_jabber_jabberid', null, $user->id);
 155      }
 156  }
 157  
 158  /*
 159   *
 160   *         $f = fopen('/tmp/event_jabberx', 'a+');
 161          fwrite($f, date('l dS \of F Y h:i:s A')."\n");
 162          fwrite($f, "from: $message->userfromid\n");
 163          fwrite($f, "userto: $message->usertoid\n");
 164          fwrite($f, "subject: $message->subject\n");
 165          fclose($f);
 166  
 167  
 168  $savemessage = new stdClass();
 169      $savemessage->useridfrom        = 3;
 170      $savemessage->useridto          = 2;
 171      $savemessage->subject           = 'IM';
 172      $savemessage->fullmessage       = 'full';
 173      $savemessage->timecreated       = time();
 174  
 175  
 176  $a = new message_output_jabber();
 177  
 178  $a->send_message($savemessage);
 179  * */
 180