message MDL-25816 added safety checks in form processing
[moodle.git] / message / output / email / message_output_email.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 * Email message processor - send a given message by email
28 *
29 * @author Luis Rodrigues
3b120e46 30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
1d422980 31 * @package
3b120e46 32 */
33require_once($CFG->dirroot.'/message/output/lib.php');
34
35class message_output_email extends message_output {
36 /**
37 * Processes the message (sends by email).
fe983847 38 * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
3b120e46 39 */
fe983847 40 function send_message($eventdata) {
38d93cb0
PS
41 global $CFG;
42
43 if (!empty($CFG->noemailever)) {
44 // hidden setting for development sites, set in config.php if needed
45 debugging('$CFG->noemailever active, no email message sent.', DEBUG_MINIMAL);
46 return true;
47 }
3b120e46 48
fe983847
AD
49 //hold onto email preference because /admin/cron.php sends a lot of messages at once
50 static $useremailaddresses = array();
6d2e6936
AD
51
52 //check user preference for where user wants email sent
fe983847
AD
53 if (!array_key_exists($eventdata->userto->id, $useremailaddresses)) {
54 $useremailaddresses[$eventdata->userto->id] = get_user_preferences('message_processor_email_email', $eventdata->userto->email, $eventdata->userto->id);
55 }
56 $usertoemailaddress = $useremailaddresses[$eventdata->userto->id];
3b120e46 57
6d2e6936
AD
58 if ( !empty($usertoemailaddress)) {
59 $userto->email = $usertoemailaddress;
3b120e46 60 }
6d2e6936 61
fe983847
AD
62 $result = email_to_user($eventdata->userto, $eventdata->userfrom,
63 $eventdata->subject, $eventdata->fullmessage, $eventdata->fullmessagehtml);
3b120e46 64
46505ee7 65 return $result;
3b120e46 66 }
67
1d422980 68 /**
3b120e46 69 * Creates necessary fields in the messaging config form.
70 * @param object $mform preferences form class
71 */
1d422980 72 function config_form($preferences){
366e4ec4 73 global $USER;
a813a748 74 $string = get_string('email','message_email').': <input size="30" name="email_email" value="'.$preferences->email_email.'" />';
0d447fe6
AD
75
76 if (empty($preferences->email_email) && !empty($preferences->userdefaultemail)) {
77 $string .= ' ('.get_string('default').': '.$preferences->userdefaultemail.')';
366e4ec4 78 }
305a014f 79 return $string;
3b120e46 80 }
81
82 /**
71666cf3 83 * Parses the form submitted data and saves it into preferences array.
3b120e46 84 * @param object $mform preferences form class
1d422980 85 * @param array $preferences preferences array
3b120e46 86 */
d18b1bbd 87 function process_form($form, &$preferences){
6c0f13a5
AD
88 if (isset($form->email_email)) {
89 $preferences['message_processor_email_email'] = $form->email_email;
90 }
3b120e46 91 }
92
93 /**
1d422980
PS
94 * Loads the config data from database to put on the form (initial load)
95 * @param array $preferences preferences array
3b120e46 96 * @param int $userid the user id
97 */
98 function load_data(&$preferences, $userid){
99 $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
100 }
101}