Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle 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
// Moodle 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 Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\aws;
18
 
19
use Aws\CommandInterface;
20
use Aws\AwsClient;
21
use Psr\Http\Message\RequestInterface;
22
 
23
/**
24
 * This class contains functions that help plugins to interact with the AWS SDK.
25
 *
26
 * @copyright  2020 Catalyst IT
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 28
 * @deprecated Since Moodle 4.5
29
 * @todo       MDL-82459 Final deprecation in Moodle 5.0.
1 efrain 30
 */
31
class aws_helper {
32
 
33
    /**
34
     * This creates a proxy string suitable for use with the AWS SDK.
35
     *
36
     * @return string the string to use for proxy settings.
1441 ariadna 37
     * @deprecated Since Moodle 4.5
1 efrain 38
     */
1441 ariadna 39
    #[\core\attribute\deprecated(
40
        'aws_helper::get_proxy_string()',
41
        since: '4.5',
42
        mdl: 'MDL-80962',
43
    )]
1 efrain 44
    public static function get_proxy_string(): string {
1441 ariadna 45
        \core\deprecation::emit_deprecation(__FUNCTION__);
1 efrain 46
        global $CFG;
47
        $proxy = '';
48
        if (empty($CFG->proxytype)) {
49
            return $proxy;
50
        }
51
        if ($CFG->proxytype === 'SOCKS5') {
52
            // If it is a SOCKS proxy, append the protocol info.
53
            $protocol = 'socks5://';
54
        } else {
55
            $protocol = '';
56
        }
57
        if (!empty($CFG->proxyhost)) {
58
            $proxy = $CFG->proxyhost;
59
            if (!empty($CFG->proxyport)) {
60
                $proxy .= ':'. $CFG->proxyport;
61
            }
62
            if (!empty($CFG->proxyuser) && !empty($CFG->proxypassword)) {
63
                $proxy = $protocol . $CFG->proxyuser . ':' . $CFG->proxypassword . '@' . $proxy;
64
            }
65
        }
66
        return $proxy;
67
    }
68
 
69
    /**
70
     * Configure the provided AWS client to route traffic via the moodle proxy for any hosts not excluded.
71
     *
72
     * @param AwsClient $client
73
     * @return AwsClient
1441 ariadna 74
     * @deprecated Since Moodle 4.5
1 efrain 75
     */
1441 ariadna 76
    #[\core\attribute\deprecated(
77
        'aws_helper::configure_client_proxy()',
78
        since: '4.5',
79
        mdl: 'MDL-80962',
80
    )]
1 efrain 81
    public static function configure_client_proxy(AwsClient $client): AwsClient {
1441 ariadna 82
        \core\deprecation::emit_deprecation(__FUNCTION__);
1 efrain 83
        $client->getHandlerList()->appendBuild(self::add_proxy_when_required(), 'proxy_bypass');
84
        return $client;
85
    }
86
 
87
    /**
88
     * Generate a middleware higher order function to wrap the handler and append proxy configuration based on target.
89
     *
90
     * @return callable Middleware high order callable.
1441 ariadna 91
     * @deprecated Since Moodle 4.5
1 efrain 92
     */
1441 ariadna 93
    #[\core\attribute\deprecated(
94
        'aws_helper::add_proxy_when_required()',
95
        since: '4.5',
96
        mdl: 'MDL-80962',
97
    )]
1 efrain 98
    protected static function add_proxy_when_required(): callable {
1441 ariadna 99
        \core\deprecation::emit_deprecation(__FUNCTION__);
1 efrain 100
        return function (callable $fn) {
101
            return function (CommandInterface $command, ?RequestInterface $request = null) use ($fn) {
102
                if (isset($request)) {
103
                    $target = (string) $request->getUri();
104
                    if (!is_proxybypass($target)) {
105
                        $command['@http']['proxy'] = self::get_proxy_string();
106
                    }
107
                }
108
 
109
                $promise = $fn($command, $request);
110
                return $promise;
111
            };
112
        };
113
    }
114
}