Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php declare(strict_types=1);
2
 
3
namespace EduSharingApiClient;
4
 
5
use Exception;
6
 
7
/**
8
 * Class EduSharingHelperBase
9
 *
10
 * @author Torsten Simon  <simon@edu-sharing.net>
11
 */
12
class EduSharingHelperBase
13
{
14
    public string      $baseUrl;
15
    public string      $privateKey;
16
    public string      $appId;
17
    public string      $language = 'de';
18
    public CurlHandler $curlHandler;
19
 
20
    /**
21
     * @param string $baseUrl
22
     * The base url to your repository in the format "http://<host>/edu-sharing"
23
     * @param string $privateKey
24
     * Your app's private key. This must match the public key registered in the repo
25
     * @param string $appId
26
     * Your app id name (as registered in the edu-sharing repository)
27
     * @throws Exception
28
     */
29
    public function __construct(string $baseUrl, string $privateKey, string $appId) {
30
        if (!preg_match('/^([a-z]|[A-Z]|[0-9]|[-_]|[.])+$/', $appId)) {
31
            throw new InvalidAppIdException('The given app id contains invalid characters or symbols');
32
        }
33
        $baseUrl           = rtrim($baseUrl, '/');
34
        $this->baseUrl     = $baseUrl;
35
        $this->privateKey  = $privateKey;
36
        $this->appId       = $appId;
37
        $this->curlHandler = new DefaultCurlHandler();
38
    }
39
 
40
    /**
41
     * Function registerCurlHandler
42
     *
43
     * @param CurlHandler $handler
44
     * @return void
45
     */
46
    public function registerCurlHandler(CurlHandler $handler): void {
47
        $this->curlHandler = $handler;
48
    }
49
 
50
    /**
51
     * Function handleCurlRequest
52
     *
53
     * @param string $url
54
     * @param array $curlOptions
55
     * @return CurlResult
56
     */
57
    public function handleCurlRequest(string $url, array $curlOptions): CurlResult {
58
        return $this->curlHandler->handleCurlRequest($url, $curlOptions);
59
    }
60
 
61
    /**
62
     * Function setLanguage
63
     *
64
     * @param string $language
65
     * @return void
66
     */
67
    public function setLanguage(string $language): void {
68
        $this->language = $language;
69
    }
70
 
71
    /**
72
     * Function sign
73
     *
74
     * @param string $toSign
75
     * @return string
76
     * @throws Exception
77
     */
78
    public function sign(string $toSign): string {
79
        $privateKeyId = openssl_get_privatekey($this->privateKey);
80
        $success      = false;
81
        if ($privateKeyId !== false) {
82
            $success = openssl_sign($toSign, $signature, $privateKeyId);
83
        }
84
        if (!$success || !isset($signature)) {
85
            throw new Exception("Private key invalid or empty.");
86
        }
87
        return base64_encode($signature);
88
    }
89
 
90
    /**
91
     * will throw an exception if the given edu-sharing api is not compatible with this library version
92
     * i.e. you could call this in your configuration / setup
93
     *
94
     * @throws Exception
95
     */
96
    public function verifyCompatibility(): void {
97
        $minVersion = '8.0';
98
        $request    = $this->handleCurlRequest($this->baseUrl . '/rest/_about', [
99
            CURLOPT_HTTPHEADER     => [
100
                'Accept: application/json',
101
                'Content-Type: application/json',
102
            ],
103
            CURLOPT_FAILONERROR    => false,
104
            CURLOPT_RETURNTRANSFER => 1
105
        ]);
106
        if ((int)$request->info["http_code"] === 200) {
107
            $result = json_decode($request->content, true, 512, JSON_THROW_ON_ERROR);
108
            if (version_compare($result["version"]["repository"], $minVersion) < 0) {
109
                throw new Exception("The edu-sharing version of the target repository is too low. Minimum required is " . $minVersion . "\n" . print_r($result['version'] ?? $result, true));
110
            }
111
        } else {
112
            throw new Exception("The edu-sharing version could not be retrieved\n" . print_r($request->info, true));
113
        }
114
    }
115
}