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,77 @@
<?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/>.
/**
* Admin setting for AWS regions.
*
* @package core
* @author Dmitrii Metelkin <dmitriim@catalyst-au.net>
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\aws;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/lib/adminlib.php');
/**
* Admin setting for a list of AWS regions.
*
* @package core
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_settings_aws_region extends \admin_setting_configtext {
/**
* Return part of form with setting.
*
* @param mixed $data array or string depending on setting
* @param string $query
* @return string
*/
public function output_html($data, $query='') {
global $CFG, $OUTPUT;
$default = $this->get_defaultsetting();
$options = [];
// We do require() not require_once() here, as the file returns a value and we may need to get
// this value more than once.
$all = require($CFG->dirroot . '/lib/aws-sdk/src/data/endpoints.json.php');
$ends = $all['partitions'][0]['regions'];
if ($ends) {
foreach ($ends as $key => $value) {
$options[] = [
'value' => $key,
'label' => $key . ' - ' . $value['description'],
];
}
}
$context = [
'list' => $this->get_full_name(),
'name' => $this->get_full_name(),
'id' => $this->get_id(),
'value' => $data,
'size' => $this->size,
'options' => $options,
];
$element = $OUTPUT->render_from_template('core/aws/setting_aws_region', $context);
return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $default, $query);
}
}
+100
View File
@@ -0,0 +1,100 @@
<?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/>.
/**
* AWS helper class. Contains useful functions when interacting with the SDK.
*
* @package core
* @author Peter Burnett <peterburnett@catalyst-au.net>
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\aws;
use Aws\CommandInterface;
use Aws\AwsClient;
use Psr\Http\Message\RequestInterface;
/**
* This class contains functions that help plugins to interact with the AWS SDK.
*
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class aws_helper {
/**
* This creates a proxy string suitable for use with the AWS SDK.
*
* @return string the string to use for proxy settings.
*/
public static function get_proxy_string(): string {
global $CFG;
$proxy = '';
if (empty($CFG->proxytype)) {
return $proxy;
}
if ($CFG->proxytype === 'SOCKS5') {
// If it is a SOCKS proxy, append the protocol info.
$protocol = 'socks5://';
} else {
$protocol = '';
}
if (!empty($CFG->proxyhost)) {
$proxy = $CFG->proxyhost;
if (!empty($CFG->proxyport)) {
$proxy .= ':'. $CFG->proxyport;
}
if (!empty($CFG->proxyuser) && !empty($CFG->proxypassword)) {
$proxy = $protocol . $CFG->proxyuser . ':' . $CFG->proxypassword . '@' . $proxy;
}
}
return $proxy;
}
/**
* Configure the provided AWS client to route traffic via the moodle proxy for any hosts not excluded.
*
* @param AwsClient $client
* @return AwsClient
*/
public static function configure_client_proxy(AwsClient $client): AwsClient {
$client->getHandlerList()->appendBuild(self::add_proxy_when_required(), 'proxy_bypass');
return $client;
}
/**
* Generate a middleware higher order function to wrap the handler and append proxy configuration based on target.
*
* @return callable Middleware high order callable.
*/
protected static function add_proxy_when_required(): callable {
return function (callable $fn) {
return function (CommandInterface $command, ?RequestInterface $request = null) use ($fn) {
if (isset($request)) {
$target = (string) $request->getUri();
if (!is_proxybypass($target)) {
$command['@http']['proxy'] = self::get_proxy_string();
}
}
$promise = $fn($command, $request);
return $promise;
};
};
}
}
+62
View File
@@ -0,0 +1,62 @@
<?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/>.
/**
* AWS Client factory. Retrieves a client with moodle specific HTTP configuration.
*
* @package core
* @author Peter Burnett <peterburnett@catalyst-au.net>
* @copyright 2022 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\aws;
use Aws\AwsClient;
/**
* AWS Client factory. Retrieves a client with moodle specific HTTP configuration.
*
* @copyright 2022 Catalyst IT
* @author Peter Burnett <peterburnett@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class client_factory {
/**
* Get an AWS client with moodle specific HTTP configuration.
*
* @param string $class Fully qualified AWS classname e.g. \Aws\S3\S3Client
* @param array $opts array of constructor options for AWS Client.
* @return AwsClient
*/
public static function get_client(string $class, array $opts): AwsClient {
// Modify the opts to add HTTP timeouts.
if (empty($opts['http'])) {
$opts['http'] = ['connect_timeout' => HOURSECS];
} else if (!array_key_exists('connect_timeout', $opts['http'])) {
// Try not to override existing settings.
$opts['http']['connect_timeout'] = HOURSECS;
}
// Blindly trust the call here. If it exceptions, the raw message is the most useful.
$client = new $class($opts);
if (!$client instanceof \Aws\AwsClient) {
throw new \moodle_exception('clientnotfound', 'factor_sms');
}
// Now we can configure the proxy with the routing aware middleware.
return aws_helper::configure_client_proxy($client);
}
}