Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of the Contact Form plugin for Moodle - https://moodle.org/
3
//
4
// Contact Form 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
// Contact Form 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 Contact Form.  If not, see <https://www.gnu.org/licenses/>.
16
 
17
/**
18
 * This plugin for Moodle is used to send emails through a web form.
19
 *
20
 * @package    local_contact
21
 * @copyright  2016-2024 TNG Consulting Inc. - www.tngconsulting.ca
22
 * @author     Michael Milette
23
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
require_once('../../config.php');
27
require_once($CFG->dirroot . '/local/contact/classes/local_contact.php');
28
 
29
if (empty(get_local_referer(false))) {
30
    $PAGE->set_url('/local/contact/index.php');
31
} else {
32
    $PAGE->set_url(get_local_referer(false));
33
}
34
 
35
// If we require user to be logged in.
36
if (!empty(get_config('local_contact', 'loginrequired'))) {
37
    // Log them in and then redirect them back to the form.
38
    if (!isloggedin() || isguestuser()) {
39
        // Set message that session has timed out.
40
        $SESSION->has_timed_out = 1;
41
        require_login();
42
    }
43
}
44
 
45
$context = context_system::instance();
46
$PAGE->set_context($context);
47
$PAGE->set_heading(format_text($SITE->fullname, FORMAT_HTML, ['context' => $context, 'escape' => false]));
48
$PAGE->set_pagelayout('standard');
49
$PAGE->set_title(get_string('confirmationpage', 'local_contact'));
50
$PAGE->navbar->add('');
51
 
52
$contact = new local_contact();
53
if ($contact->isspambot) {
54
    header('HTTP/1.0 403 Forbidden');
55
    if ($CFG->debugdisplay == 1 || is_siteadmin()) {
56
        die(get_string('forbidden', 'local_contact') . '. ' . $contact->errmsg);
57
    } else {
58
        die(get_string('forbidden', 'local_contact')) . '.';
59
    }
60
}
61
 
62
// Display page header.
63
echo $OUTPUT->header();
64
 
65
// Determine the recipient's name and email address.
66
 
67
// The default recipient is the Moodle site's support contact. This will
68
// be used if no recipient was specified or if the recipient is unknown.
69
$name = trim($CFG->supportname ?? '');
70
$email = trim($CFG->supportemail ?? '');
71
 
72
// Handle recipient alias.
73
// If the form includes a recipient's alias, search the plugin's recipient list settings for a name and email address.
74
$recipient = trim(optional_param('recipient', '', PARAM_TEXT));
75
if (!empty($recipient)) {
76
    $lines = explode("\n", get_config('local_contact', 'recipient_list'));
77
    foreach ($lines as $linenumbe => $line) {
78
        $line = trim($line ?? '');
79
        if (empty($line)) { // Blank line.
80
            continue;
81
        }
82
        $thisrecipient = explode('|', $line);
83
        // 0 = alias, 1 = email address, 2 = name.
84
        if (count($thisrecipient) == 3) {
85
            // Trim leading and trailing spaces from each of the 3 parameters.
86
            $thisrecipient = array_map('trim', $thisrecipient);
87
            // See if this alias matches the one we are looking for.
88
            if ($thisrecipient[0] == $recipient && !empty($thisrecipient[1]) && !empty($thisrecipient[2])) {
89
                $email = $thisrecipient[1];
90
                $name = $thisrecipient[2];
91
                break;
92
            }
93
        }
94
    }
95
}
96
 
97
// Test for ReCAPTCHA.
98
 
99
// ReCAPTCHA is never required for logged-in non-guest users.
100
if (!isloggedin() || isguestuser()) {
101
    // Is ReCAPTCHA configured in Moodle?
102
    if (
103
        !empty($CFG->recaptchaprivatekey)
104
        && !empty($CFG->recaptchapublickey)
105
        && empty(get_config('local_contact', 'norecaptcha'))
106
    ) {
107
        // If so, ensure that it was filled correctly and submitted with the form.
108
        if (file_exists($CFG->libdir . '/recaptchalib_v2.php')) {
109
            // For reCAPTCHA 2.0.
110
            require_once($CFG->libdir . '/recaptchalib_v2.php');
111
            $response = recaptcha_check_response(
112
                RECAPTCHA_VERIFY_URL,
113
                $CFG->recaptchaprivatekey,
114
                getremoteaddr(),
115
                optional_param('g-recaptcha-response', '', PARAM_TEXT)
116
            );
117
            $resp = new stdClass();
118
            $resp->is_valid = $response['isvalid'];
119
            if (!$resp->is_valid) {
120
                $resp->error = $response['error'];
121
            }
122
        } else {
123
            // For reCAPTCHA 1.0.
124
            $resp = recaptcha_check_answer(
125
                $CFG->recaptchaprivatekey,
126
                $_SERVER["REMOTE_ADDR"],
127
                optional_param('recaptcha_challenge_field', '', PARAM_TEXT),
128
                optional_param('recaptcha_response_field', '', PARAM_TEXT)
129
            );
130
        }
131
 
132
        if (!$resp->is_valid) {
133
            // Display error message if CAPTCHA was entered incorrectly.
134
            echo '<h3>' . get_string('missingrecaptchachallengefield') . '</h3>';
135
            echo '<p>' . get_string('recaptcha_help', 'auth')
136
                . ($CFG->debugdisplay == 1 ? ' (' .  $resp->error . ')' : '') . '</p>';
137
            echo '<button type="button" onclick="history.back();">' . get_string('incorrectpleasetryagain', 'auth') . '</a>';
138
            // Display page footer.
139
            echo $OUTPUT->footer();
140
            die;
141
        }
142
    }
143
}
144
 
145
// Send the message.
146
 
147
if ($contact->sendmessage($email, $name)) {
148
    // Share a gratitude and Say Thank You! Your user will love to know their message was sent.
149
    echo '<h3>' . get_string('eventmessagesent', 'message') . '</h3>';
150
    echo '<p>' . get_string('confirmationmessage', 'local_contact') . '</p>';
151
} else {
152
    // Oh no! What are the chances. Looks like we failed to meet user expectations (message not sent).
153
    echo '<h3>' . get_string('errorsendingtitle', 'local_contact') . '</h3>';
154
    echo '<p>' . get_string('errorsending', 'local_contact') . '</p>';
155
}
156
 
157
// Determine the URL of the Continue button after the form has been submitted.
158
// If a 'referrer' parameter is provided and is a valid URL on the same site, use it.
159
// If not, if the course ID is the same as the site ID, set the button to the home page.
160
// Otherwise, it tries to return to the page where the form was submitted from.
161
// If it can't find such a page, it redirects to the course page.
162
// Finally, it outputs a continue button with the determined URL.
163
$continueurl = optional_param('referrer', '', PARAM_URL);
164
// Only allow continue to relative or absolute URLs on this site.
165
if (!(stripos($continueurl, $CFG->wwwroot) === 0 || substr($continueurl, 0, 1) != '/' || substr($continueurl, 0, 1) != '.')) {
166
    $continueurl = '';
167
}
168
if (empty($continueurl)) {
169
    if ($PAGE->course->id == SITEID) {
170
        // If coming from a site page, continue to the home page.
171
        $continueurl = $CFG->wwwroot;
172
    } else { // If coming from a course page.
173
        // See if we can return to the page from where the form was submitted.
174
        $continueurl = get_local_referer(false);
175
        if (empty($continueurl)) {
176
            // If the referrer was not available, go to the course page.
177
            $continueurl = new moodle_url('/course/view.php', ['id' => $PAGE->course->id]);
178
        }
179
    }
180
}
181
echo $OUTPUT->continue_button($continueurl);
182
 
183
// Display page footer.
184
echo $OUTPUT->footer();