Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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