|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00004 // // 00005 // NOTICE OF COPYRIGHT // 00006 // // 00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 00008 // http://moodle.com // 00009 // // 00010 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com // 00011 // // 00012 // This program is free software; you can redistribute it and/or modify // 00013 // it under the terms of the GNU General Public License as published by // 00014 // the Free Software Foundation; either version 2 of the License, or // 00015 // (at your option) any later version. // 00016 // // 00017 // This program is distributed in the hope that it will be useful, // 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00020 // GNU General Public License for more details: // 00021 // // 00022 // http://www.gnu.org/copyleft/gpl.html // 00023 // // 00025 00034 require_once($CFG->dirroot.'/message/output/lib.php'); 00035 require_once($CFG->libdir.'/jabber/XMPP/XMPP.php'); 00036 00037 class message_output_jabber extends message_output { 00038 00044 function send_message($eventdata){ 00045 global $CFG; 00046 00047 if (!empty($CFG->noemailever)) { 00048 // hidden setting for development sites, set in config.php if needed 00049 debugging('$CFG->noemailever active, no jabber message sent.', DEBUG_MINIMAL); 00050 return true; 00051 } 00052 00053 // skip any messaging suspended and deleted users 00054 if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) { 00055 return true; 00056 } 00057 00058 //hold onto jabber id preference because /admin/cron.php sends a lot of messages at once 00059 static $jabberaddresses = array(); 00060 00061 if (!array_key_exists($eventdata->userto->id, $jabberaddresses)) { 00062 $jabberaddresses[$eventdata->userto->id] = get_user_preferences('message_processor_jabber_jabberid', null, $eventdata->userto->id); 00063 } 00064 $jabberaddress = $jabberaddresses[$eventdata->userto->id]; 00065 00066 //calling s() on smallmessage causes Jabber to display things like < Jabber != a browser 00067 $jabbermessage = fullname($eventdata->userfrom).': '.$eventdata->smallmessage; 00068 00069 if (!empty($eventdata->contexturl)) { 00070 $jabbermessage .= "\n".get_string('view').': '.$eventdata->contexturl; 00071 } 00072 00073 $jabbermessage .= "\n(".get_string('noreply','message').')'; 00074 00075 $conn = new XMPPHP_XMPP($CFG->jabberhost,$CFG->jabberport,$CFG->jabberusername,$CFG->jabberpassword,'moodle',$CFG->jabberserver); 00076 00077 try { 00078 //$conn->useEncryption(false); 00079 $conn->connect(); 00080 $conn->processUntil('session_start'); 00081 $conn->presence(); 00082 $conn->message($jabberaddress, $jabbermessage); 00083 $conn->disconnect(); 00084 } catch(XMPPHP_Exception $e) { 00085 debugging($e->getMessage()); 00086 return false; 00087 } 00088 return true; 00089 } 00090 00095 function config_form($preferences){ 00096 global $CFG; 00097 00098 if (!$this->is_system_configured()) { 00099 return get_string('notconfigured','message_jabber'); 00100 } else { 00101 return get_string('jabberid', 'message_jabber').': <input size="30" name="jabber_jabberid" value="'.s($preferences->jabber_jabberid).'" />'; 00102 } 00103 } 00104 00110 function process_form($form, &$preferences){ 00111 if (isset($form->jabber_jabberid) && !empty($form->jabber_jabberid)) { 00112 $preferences['message_processor_jabber_jabberid'] = $form->jabber_jabberid; 00113 } 00114 } 00115 00121 function load_data(&$preferences, $userid){ 00122 $preferences->jabber_jabberid = get_user_preferences( 'message_processor_jabber_jabberid', '', $userid); 00123 } 00124 00129 function is_system_configured() { 00130 global $CFG; 00131 return (!empty($CFG->jabberhost) && !empty($CFG->jabberport) && !empty($CFG->jabberusername) && !empty($CFG->jabberpassword)); 00132 } 00133 00140 function is_user_configured($user = null) { 00141 global $USER; 00142 00143 if (is_null($user)) { 00144 $user = $USER; 00145 } 00146 return (bool)get_user_preferences('message_processor_jabber_jabberid', null, $user->id); 00147 } 00148 } 00149 00150 /* 00151 * 00152 * $f = fopen('/tmp/event_jabberx', 'a+'); 00153 fwrite($f, date('l dS \of F Y h:i:s A')."\n"); 00154 fwrite($f, "from: $message->userfromid\n"); 00155 fwrite($f, "userto: $message->usertoid\n"); 00156 fwrite($f, "subject: $message->subject\n"); 00157 fclose($f); 00158 00159 00160 $savemessage = new stdClass(); 00161 $savemessage->useridfrom = 3; 00162 $savemessage->useridto = 2; 00163 $savemessage->subject = 'IM'; 00164 $savemessage->fullmessage = 'full'; 00165 $savemessage->timecreated = time(); 00166 00167 00168 $a = new message_output_jabber(); 00169 00170 $a->send_message($savemessage); 00171 * */ 00172