aboutsummaryrefslogtreecommitdiff
path: root/vendor/edu-sharing-plugin/edu-sharing-helper-base.php
blob: 4c6f6da53ebe16d7cf41d49f97820269d356512b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php

class EduSharingHelperBase {
    public $baseUrl;
    public $privateKey;
    public $appId;
    public $language = 'de';
    public $http_proxy = '';

    /**
     * @param string $baseUrl
     * The base url to your repository in the format "http://<host>/edu-sharing"
     * @param string $privateKey
     * Your app's private key. This must match the public key registered in the repo
     * @param string $appId
     * Your app id name (as registered in the edu-sharing repository)
     */
    public function __construct(
        string $baseUrl,
        string $privateKey,
        string $appId,
        string $http_proxy = ''
    ) {
        if(!preg_match('/^([a-z]|[A-Z]|[0-9]|[-_])+$/', $appId)) {
            throw new Exception('The given app id contains invalid characters or symbols');
        }
        $this->baseUrl=$baseUrl;
        $this->privateKey=$privateKey;
        $this->appId=$appId;
        $this->http_proxy = $http_proxy;
    }

    public function setLanguage(string $language) {
        $this->language = $language;
    }

    function sign(string $toSign) {
        $pkeyid = openssl_get_privatekey($this->privateKey);
        openssl_sign($toSign, $signature, $pkeyid);
        $signature = base64_encode($signature);
        openssl_free_key($pkeyid);
        return $signature;
    }

}