first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,142 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Form to edit handlers.
*
* @package tool_messageinbound
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
/**
* Form to edit handlers.
*
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_messageinbound_edit_handler_form extends moodleform {
/**
* The form definition
*/
public function definition() {
$mform = $this->_form;
$handler = $this->_customdata['handler'];
// Set up the options for formatting text for descriptions, etc.
$formatoptions = new stdClass();
$formatoptions->trusted = false;
$formatoptions->noclean = false;
$formatoptions->filter = false;
$formatoptions->para = true;
$formatoptions->newlines = false;
$formatoptions->overflowdiv = true;
// General information about the handler.
$mform->addElement('header', 'general', get_string('general'));
$mform->addElement('static', 'name', get_string('name', 'tool_messageinbound'),
$handler->name);
$mform->addElement('static', 'classname', get_string('classname', 'tool_messageinbound'));
$description = format_text($handler->description, FORMAT_MARKDOWN, $formatoptions);
$mform->addElement('static', 'description', get_string('description', 'tool_messageinbound'),
$description);
// Items which can be configured.
$mform->addElement('header', 'configuration', get_string('configuration'));
if ($handler->can_change_defaultexpiration()) {
// Show option to change expiry only if the handler supports it.
$options = array(
HOURSECS => get_string('onehour', 'tool_messageinbound'),
DAYSECS => get_string('oneday', 'tool_messageinbound'),
WEEKSECS => get_string('oneweek', 'tool_messageinbound'),
YEARSECS => get_string('oneyear', 'tool_messageinbound'),
0 => get_string('noexpiry', 'tool_messageinbound'),
);
$mform->addElement('select', 'defaultexpiration', get_string('defaultexpiration', 'tool_messageinbound'), $options);
$mform->addHelpButton('defaultexpiration', 'defaultexpiration', 'tool_messageinbound');
} else {
$text = $this->get_defaultexpiration_text($handler);
$mform->addElement('static', 'defaultexpiration_fake', get_string('defaultexpiration', 'tool_messageinbound'), $text);
$mform->addElement('hidden', 'defaultexpiration');
$mform->addHelpButton('defaultexpiration_fake', 'defaultexpiration', 'tool_messageinbound');
$mform->setType('defaultexpiration', PARAM_INT);
}
if ($handler->can_change_validateaddress()) {
$mform->addElement('checkbox', 'validateaddress', get_string('requirevalidation', 'tool_messageinbound'));
$mform->addHelpButton('validateaddress', 'validateaddress', 'tool_messageinbound');
} else {
if ($handler->validateaddress) {
$text = get_string('yes');
} else {
$text = get_string('no');
}
$mform->addElement('static', 'validateaddress_fake', get_string('requirevalidation', 'tool_messageinbound'), $text);
$mform->addElement('hidden', 'validateaddress');
$mform->addHelpButton('validateaddress_fake', 'fixedvalidateaddress', 'tool_messageinbound');
$mform->setType('validateaddress', PARAM_INT);
}
if ($handler->can_change_enabled()) {
$mform->addElement('checkbox', 'enabled', get_string('enabled', 'tool_messageinbound'));
} else {
if ($handler->enabled) {
$text = get_string('yes');
} else {
$text = get_string('no');
}
$mform->addElement('static', 'enabled_fake', get_string('enabled', 'tool_messageinbound'), $text);
$mform->addHelpButton('enabled', 'fixedenabled', 'tool_messageinbound');
$mform->addElement('hidden', 'enabled');
$mform->setType('enabled', PARAM_INT);
}
$this->add_action_buttons(true, get_string('savechanges'));
}
/**
* Return a text string representing the selected default expiration for the handler.
*
* @param \core\message\inbound\handler $handler handler instance.
*
* @return string localised text string.
*/
protected function get_defaultexpiration_text(\core\message\inbound\handler $handler) {
switch($handler->defaultexpiration) {
case HOURSECS :
return get_string('onehour', 'tool_messageinbound');
case DAYSECS :
return get_string('oneday', 'tool_messageinbound');
case WEEKSECS :
return get_string('oneweek', 'tool_messageinbound');
case YEARSECS :
return get_string('oneyear', 'tool_messageinbound');
case 0:
return get_string('noexpiry', 'tool_messageinbound');
default:
return ''; // Should never happen.
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,94 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A Handler to re-process messages which previously failed sender verification.
*
* @package tool_messageinbound
* @category message
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_messageinbound\message\inbound;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');
/**
* A Handler to re-process messages which previously failed sender verification.
*
* This may happen if the user did not use their registerd e-mail address,
* the verification hash used had expired, or if some erroneous content was
* introduced into the content hash.
*
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class invalid_recipient_handler extends \core\message\inbound\handler {
/**
* Do not allow changes to the address validation setting.
*/
public function can_change_validateaddress() {
return false;
}
/**
* Return a description for the current handler.
*
* @return string
*/
public function get_description() {
return get_string('invalid_recipient_handler', 'tool_messageinbound');
}
/**
* Return a name for the current handler.
* This appears in the admin pages as a human-readable name.
*
* @return string
*/
public function get_name() {
return get_string('invalid_recipient_handler_name', 'tool_messageinbound');
}
/**
* Process a message received and validated by the Inbound Message processor.
*
* @param \stdClass $record The Inbound Message record
* @param \stdClass $data The message data packet.
* @return bool Whether the message was successfully processed.
* @throws \core\message\inbound\processing_failed_exception when the message can not be found.
*/
public function process_message(\stdClass $record, \stdClass $data) {
global $DB;
if (!$maildata = $DB->get_record('messageinbound_messagelist', array('id' => $record->datavalue))) {
// The message requested couldn't be found. Failing here will alert the user that we failed.
throw new \core\message\inbound\processing_failed_exception('oldmessagenotfound', 'tool_messageinbound');
}
mtrace("=== Request to re-process message {$record->datavalue} from server.");
mtrace("=== Message-Id:\t{$maildata->messageid}");
mtrace("=== Recipient:\t{$maildata->address}");
$manager = new \tool_messageinbound\manager();
return $manager->process_existing_message($maildata);
}
}
@@ -0,0 +1,217 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Data provider.
*
* @package tool_messageinbound
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_messageinbound\privacy;
defined('MOODLE_INTERNAL') || die();
use context;
use context_user;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
/**
* Data provider class.
*
* @package tool_messageinbound
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\plugin\provider {
/**
* Returns metadata.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
$collection->add_database_table('messageinbound_messagelist', [
'messageid' => 'privacy:metadata:messagelist:messageid',
'userid' => 'privacy:metadata:messagelist:userid',
'address' => 'privacy:metadata:messagelist:address',
'timecreated' => 'privacy:metadata:messagelist:timecreated',
], 'privacy:metadata:messagelist');
// Arguably the keys are handled by \core\message\inbound\address_manager and thus could/should be handled by core.
$collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:coreuserkey');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return \contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): \core_privacy\local\request\contextlist {
$contextlist = new \core_privacy\local\request\contextlist();
// Always add the user context so we're sure we're not dodging user keys, besides it's not costly to do so.
$contextlist->add_user_context($userid);
return $contextlist;
}
/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
global $DB;
$context = $userlist->get_context();
if (!is_a($context, \context_user::class)) {
return;
}
// Add user if any messagelist data exists.
if ($DB->record_exists('messageinbound_messagelist', ['userid' => $context->instanceid])) {
// Only using user context, so instance ID will be the only user ID.
$userlist->add_user($context->instanceid);
}
// Add users based on userkey (since we also delete those).
\core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'messageinbound_handler');
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
if (!static::approved_contextlist_contains_my_context($contextlist)) {
// We only care about the user's user context.
return;
}
$userid = $contextlist->get_user()->id;
$context = context_user::instance($userid);
$path = [get_string('messageinbound', 'tool_messageinbound')];
// Export user keys.
\core_userkey\privacy\provider::export_userkeys($context, $path, 'messageinbound_handler');
// Export the message list.
$data = [];
$recordset = $DB->get_recordset('messageinbound_messagelist', ['userid' => $userid], 'timecreated, id');
foreach ($recordset as $record) {
$data[] = [
'received_at' => $record->address,
'timecreated' => transform::datetime($record->timecreated),
];
}
$recordset->close();
writer::with_context($context)->export_data($path, (object) ['messages_pending_validation' => $data]);
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(context $context) {
global $DB;
if ($context->contextlevel != CONTEXT_USER) {
return;
}
static::delete_user_data($context->instanceid);
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
if (!static::approved_contextlist_contains_my_context($contextlist)) {
// We only care about the user's user context.
return;
}
static::delete_user_data($contextlist->get_user()->id);
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
$context = $userlist->get_context();
$userids = $userlist->get_userids();
// Since this falls within a user context, only that user should be valid.
if ($context->contextlevel != CONTEXT_USER || count($userids) != 1 || $context->instanceid != $userids[0]) {
return;
}
static::delete_user_data($userids[0]);
}
/**
* Delete a user's data.
*
* @param int $userid The user ID.
* @return void
*/
protected static function delete_user_data($userid) {
global $DB;
$DB->delete_records_select('messageinbound_messagelist', 'userid = :userid', ['userid' => $userid]);
\core_userkey\privacy\provider::delete_userkeys('messageinbound_handler', $userid);
}
/**
* Return whether the contextlist contains our own context.
*
* @param approved_contextlist $contextlist The contextlist
* @return bool
*/
protected static function approved_contextlist_contains_my_context(approved_contextlist $contextlist) {
$userid = $contextlist->get_user()->id;
foreach ($contextlist->get_contexts() as $context) {
if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $userid) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A scheduled task to handle cleanup of old, unconfirmed e-mails.
*
* @package tool_messageinbound
* @category task
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_messageinbound\task;
defined('MOODLE_INTERNAL') || die();
/**
* A scheduled task to handle cleanup of old, unconfirmed e-mails.
*
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cleanup_task extends \core\task\scheduled_task {
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('taskcleanup', 'tool_messageinbound');
}
/**
* Execute the main Inbound Message pickup task.
*/
public function execute() {
$manager = new \tool_messageinbound\manager();
$manager->tidy_old_messages();
$manager->tidy_old_verification_failures();
}
}
@@ -0,0 +1,54 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A scheduled task to handle Inbound Message e-mail pickup.
*
* @package tool_messageinbound
* @category task
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_messageinbound\task;
defined('MOODLE_INTERNAL') || die();
/**
* A scheduled task to handle Inbound Message e-mail pickup.
*
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class pickup_task extends \core\task\scheduled_task {
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('taskpickup', 'tool_messageinbound');
}
/**
* Execute the main Inbound Message pickup task.
*/
public function execute() {
$manager = new \tool_messageinbound\manager();
return $manager->pickup_messages();
}
}
@@ -0,0 +1,56 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace tool_messageinbound;
/**
* The Mail Pickup Utils.
*
* @package tool_messageinbound
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class utils {
/** @var int Encoding type: 7 bit SMTP semantic data. */
const ENC7BIT = 0;
/** @var int Encoding type: 8 bit SMTP semantic data. */
const ENC8BIT = 1;
/** @var int Encoding type: 8 bit binary data. */
const ENCBINARY = 2;
/** @var int Encoding type: BASE64 encoded data. */
const ENCBASE64 = 3;
/** @var int Encoding type: Human-readable 8-as-7 bit data. */
const ENCQUOTEDPRINTABLE = 4;
/** @var int Encoding type: Unknown. */
const ENCOTHER = 5;
/**
* Get body content encoding.
*
* @return string[] List of body content encoding.
*/
public static function get_body_encoding(): array {
return [
self::ENC7BIT => '7BIT',
self::ENC8BIT => '8BIT',
self::ENCBINARY => 'BINARY',
self::ENCBASE64 => 'BASE64',
self::ENCQUOTEDPRINTABLE => 'QUOTED-PRINTABLE',
self::ENCOTHER => 'X-UNKNOWN',
];
}
}