aboutsummaryrefslogtreecommitdiff
path: root/vendor/edu-sharing-plugin/edu-sharing-helper.php
blob: 3c9d88413bf497e48631dcf68997acb910c7ae9c (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
46
47
<?php

class EduSharingHelper {
    /**
     * generate a new key pair (private + public) to be registered in the edu-sharing repository
     * Store the data somewhere in your application, e.g. database
     * use the public key returned to register the application in edu-sharing
     * NOTE: This function will fail on windows-based systems!
     * @throws Exception
     */
    public static function generateKeyPair(
    )
    {
        $res = openssl_pkey_new();
        if(!$res) {
            throw new Exception("No result from openssl_pkey_new. Please check your php installation");
        }
        openssl_pkey_export($res, $privatekey);
        $publickey = openssl_pkey_get_details($res);
        $publickey = $publickey["key"];
        return [
            "privatekey" => $privatekey,
            "publickey" => $publickey
        ];
    }

    /**
     * Generates an edu-sharing compatible xml file for registering the application
     * This is a very basic function and is only intended for demonstration or manual use. Data is not escaped!
     */
    public static function generateEduAppXMLData(string $appId, string $publickey, string $type = 'LMS', string $publicIP = '*') {
        return '<?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
                <properties>
                    <entry key="appid">' . $appId . '</entry>
                    <entry key="public_key">' . $publickey . '</entry>
                    <entry key="type">' . $type . '</entry>
                    <entry key="domain"></entry>
                    <!-- in case of wildcard host: Replace this, if possible, with the public ip from your service --> 
                    <entry key ="host">' . $publicIP . '</entry>
                    <!-- must be true -->
                    <entry key="trustedclient">true</entry> 
                </properties>
                ';
    }

}