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 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
declare(strict_types=1);
18
 
19
namespace mod_edusharing;
20
 
21
use curl;
22
use EduSharingApiClient\CurlHandler;
23
use EduSharingApiClient\CurlResult;
24
 
25
/**
26
 * class MoodleCurlHandler
27
 *
28
 * @author Marian Ziegler <ziegler@edu-sharing.net>
29
 * @package mod_edusharing
30
 */
31
class MoodleCurlHandler extends CurlHandler {
32
    // phpcs:disable -- Function cannot be lowercase as it implements an interface
33
    /**
34
     * Function handleCurlRequest
35
     *
36
     * Method name does not comply with moodle code style
37
     * in order to ensure compatibility with edu-sharing api library
38
     *
39
     * @param string $url
40
     * @param array $curlOptions
41
     * @return CurlResult
42
     */
43
    public function handleCurlRequest(string $url, array $curlOptions): CurlResult {
44
        // phpcs:enable
45
        global $CFG;
46
        require_once($CFG->libdir . '/filelib.php');
47
        $curl         = new curl();
48
        $params       = [];
49
        $options      = [];
50
        $allconstants = null;
51
        // phpcs:ignore -- var name is camelCase to match interface.
52
        foreach ($curlOptions as $key => $value) {
53
            if (is_int($key)) {
54
                if ($allconstants === null) {
55
                    $allconstants = get_defined_constants(true)['curl'];
56
                }
57
                $key = array_search($key, $allconstants, true);
58
                if ($key === false) {
59
                    continue;
60
                }
61
            }
62
            if ($key === 'CURLOPT_HTTPHEADER') {
63
                $curl->header = $value;
64
            } else if ($key === 'CURLOPT_POSTFIELDS') {
65
                $params = $value;
66
            } else if ($key === 'CURLOPT_POST' && $value === 1) {
67
                $this->method = static::METHOD_POST;
68
            } else if ($key === 'CURLOPT_CUSTOMREQUEST' && $value === 'DELETE') {
69
                $this->method = static::METHOD_DELETE;
70
            } else {
71
                $options[$key] = $value;
72
            }
73
        }
74
        if ($this->method === static::METHOD_POST) {
75
            $result = $curl->post($url, $params, $options);
76
        } else if ($this->method === static::METHOD_PUT) {
77
            $result = $curl->put($url, $params, $options);
78
        } else if ($this->method === static::METHOD_DELETE) {
79
            $result = $curl->delete($url, $params, $options);
80
        } else {
81
            $result = $curl->get($url, $params, $options);
82
        }
83
        if ($curl->errno !== 0 && is_array($curl->info)) {
84
            $curl->info['message'] = $curl->error;
85
        }
86
        $this->method = self::METHOD_GET;
87
        return new CurlResult($result, $curl->errno, is_array($curl->info) ? $curl->info : ['message' => $curl->error]);
88
    }
89
}