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