-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_contact_form.php
208 lines (180 loc) · 9.41 KB
/
user_contact_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php // $Id: user_contact_form.php,v 1.1 2011/02/08 18:47:29 arborrow Exp $
require_once('../../config.php');
require_once($CFG->libdir.'/formslib.php');
class block_user_contact_form extends moodleform {
function definition() {
global $CFG, $USER, $PAGE, $cid, $bid, $rcp, $allhiddenrecipients, $allstandardrecipients;
$mform =& $this->_form;
//From: http://docs.moodle.org/en/Development:Coding
// Use 'sesskey' mechanism to protect form handling routines from attack.
/* Basic example of use: when form is generated, include <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />. */
// When you process the form check with if (!confirm_sesskey()) { print_error('confirmsesskeybad');}
$PAGE->requires->js('/lib/nla/ua-parser.min.js');
$onloadJS = 'var parser = new UAParser();
var b = parser.getBrowser();
var e = parser.getEngine();
var o = parser.getOS();
var d = parser.getDevice();
var device_string =
(d.model === undefined)
? "Personal Computer"
: d.model + " | " + d.type + " | " + d.vendor;
document.getElementById("ua_browser").innerHTML =
document.getElementsByName("cf_browser")[0].value = "<strong>" + b.name + "</strong> " + b.major + " (" + b.version + ")";
document.getElementById("ua_engine").innerHTML =
document.getElementsByName("cf_engine")[0].value = "<strong>" + e.name + "</strong> (" + e.version + ")";
document.getElementById("ua_os").innerHTML =
document.getElementsByName("cf_os")[0].value = "<strong>" + o.name + "</strong> " + o.version;
document.getElementById("ua_device").innerHTML =
document.getElementsByName("cf_device")[0].value = device_string;
';
$PAGE->requires->js_init_code($onloadJS, true);
$debug = false;
if ($debug) {
echo '****** Written from line '.__LINE__.' of '.__FILE__.' ********<br />';
echo '$cid = '.$cid.'<br />';
echo '$bid = '.$bid.'<br />';
echo '$rcp = '.$rcp.'<br />';
if ($allhiddenrecipients) {
$countallhiddenrecipients = count($allhiddenrecipients);
} else {
$countallhiddenrecipients = 0;
}
echo 'count($allhiddenrecipients) = '.$countallhiddenrecipients.'<br />';
if ($allstandardrecipients) {
$countallstandardrecipients = count($allstandardrecipients);
} else {
$countallstandardrecipients = 0;
}
echo 'count($allstandardrecipients) = '.$countallstandardrecipients;
}
// what am I supposed to do if a guy is hiddenrecipient and standardrecipient too?
// no fieldset needed
$mform->addElement('hidden', 'sesskey', sesskey());
$mform->addElement('hidden', 'cid', $cid);
$mform->addElement('hidden', 'bid', $bid);
$mform->addElement('hidden', 'rcp', $rcp);
// fieldset sender
$mform->addElement('header', 'sender', get_string('sender', 'block_user_contact'));
if (isloggedin() && (!isguestuser($USER))) {
// static_sendername
$mform->addElement('static', 'static_sendername', get_string('name'),fullname($USER));
$mform->addElement('hidden', 'cf_sendername', fullname($USER));
// static_senderemail
$mform->addElement('static', 'static_senderemail', get_string('email'),$USER->email);
$mform->addElement('hidden', 'cf_senderemail', $USER->email);
$mform->addElement('hidden', 'cf_sendermailformat', $USER->mailformat);
} else {
// cf_opensendername
$mform->addElement('text', 'cf_sendername', get_string('name'));
$mform->addRule('cf_sendername', get_string('missingremoteusername','block_user_contact'), 'required', null, 'client');
// cf_opensenderemail
$mform->addElement('text', 'cf_senderemail', get_string('email'));
$mform->addRule('cf_senderemail', get_string('missingremoteuseremail','block_user_contact'), 'required', null, 'client');
$mform->addElement('hidden', 'cf_sendermailformat', '1');
}
// no fieldset needed
if ($allhiddenrecipients) {
foreach ($allhiddenrecipients as $recipient) {
$mform->addElement('hidden', 'cf_teacher'.$recipient->id, '1');
$mform->addElement('hidden', 'cf_emailteacher'.$recipient->id, $recipient->email);
}
}
// fieldset recipients
if ($allstandardrecipients) {
// [NLA] commenting out line
//$mform->addElement('header', 'recipients', get_string('recipients', 'block_user_contact'));
foreach ($allstandardrecipients as $recipient) {
// [NLA] original line:
//$mform->addElement('checkbox', 'cf_teacher'.$recipient->id, fullname($recipient));
$mform->addElement('hidden', 'cf_teacher'.$recipient->id, fullname($recipient));
$mform->addElement('hidden', 'cf_emailteacher'.$recipient->id, $recipient->email);
}
if (count($allstandardrecipients) == 1) {
$mform->setDefault('cf_teacher'.$recipient->id, '1');
}
}
$mform->addElement('header', 'client', "Client Info");
$mform->addElement('static', 'browser', 'Browser', "<span id='ua_browser'></span>");
$mform->addElement('hidden', 'cf_browser', '');
$mform->addElement('static', 'engine', 'Engine', "<span id='ua_engine'></span>");
$mform->addElement('hidden', 'cf_engine', '');
$mform->addElement('static', 'os', 'OS Type', "<span id='ua_os'></span>");
$mform->addElement('hidden', 'cf_os', '');
$mform->addElement('static', 'device', 'Device', "<span id='ua_device'></span>");
$mform->addElement('hidden', 'cf_device', '');
// fieldset email
$mform->addElement('header', 'email', get_string('email', 'block_user_contact'));
// cf_mailsubject
$mform->addElement('text', 'cf_mailsubject', get_string('mailsubject','block_user_contact'));
$mform->addRule('cf_mailsubject', get_string('missingmailsubject','block_user_contact'), 'required', null, 'client');
// cf_mailbody
$textfieldoptions = array('trusttext'=>false, 'subdirs'=>true, 'maxfiles'=>3, 'maxbytes'=>1024);
$mform->addElement('editor', 'cf_mailbody', get_string('mailbody','block_user_contact'), null, $textfieldoptions);
$mform->setType('cf_mailbody', PARAM_RAW);
$mform->addRule('cf_mailbody', get_string('missingmailbody','block_user_contact'), 'required', null, 'client');
// $mform->addHelpButton('cf_mailbody', 'userdescription');
$mform->addHelpButton('cf_mailbody', 'emailbody', 'block_user_contact');
// recaptcha
if (user_contact_recaptchaenabled()) {
$mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
$mform->setHelpButton('recaptcha_element', array('recaptcha', get_string('recaptcha', 'auth')));
$mform->addRule('recaptcha_element', get_string('missingrecaptcha','block_user_contact'), 'required', null, 'client');
}
//-------------------------------------------------------------------------------
// buttons
$this->add_action_buttons(true, get_string('sendemail','block_user_contact'));
}
function definition_after_data() {
}
function validation($data) {
global $allhiddenrecipients, $allstandardrecipients;
//print_object($data);
$errors = array();
if (!confirm_sesskey()) {
print_error('confirmsesskeybad');
}
if (!isloggedin()) {
if (! validate_email($data['cf_senderemail'])) {
$errors['cf_senderemail'] = get_string('invalidemail');
}
}
if ($allhiddenrecipients) {
$offset = count($allhiddenrecipients);
} else {
$offset = 0;
}
// if there are not $allstandardrecipients (alias !isset($allstandardrecipients)) there is nothing to check
// so all is right ($allright = true)
$allright = empty($allstandardrecipients);
if (!$allright) {
$i = $offset;
foreach ($allstandardrecipients as $recipient) {
if ( isset($data['cf_teacher'.$recipient->id]) ) {
$allright = true;
continue;
}
}
}
if (!$allright) {
foreach ($allstandardrecipients as $recipient) {
$errors['cf_teacher'.$recipient->id] = get_string('missingrecipient','block_user_contact');
}
}
// controllare il recaptcha
if (user_contact_recaptchaenabled()) {
$recaptcha_element = $this->_form->getElement('recaptcha_element');
if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
$challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
$response_field = $this->_form->_submitValues['recaptcha_response_field'];
if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
$errors['recaptcha_element'] = $result;
}
} else {
$errors['recaptcha_element'] = get_string('missingrecaptchachallengefield');
}
}
return $errors;
}
}
?>