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 dml_exception;
22
use DOMDocument;
23
use EduSharingApiClient\EduSharingHelper;
24
use Exception;
25
use SimpleXMLElement;
26
 
27
/**
28
 * Class MetadataLogic
29
 *
30
 * @author Marian Ziegler <ziegler@edu-sharing.net>
31
 * @package mod_edusharing
32
 */
33
class MetadataLogic {
34
    /**
35
     * @var bool
36
     */
37
    public bool $reloadform = false;
38
    /**
39
     * @var string|null
40
     */
41
    private ?string $hostaliases = null;
42
    /**
43
     * @var string|null
44
     */
45
    private ?string $wloguestuser = null;
46
    /**
47
     * @var string|null
48
     */
49
    private ?string $appid = null;
50
    /**
51
     * @var EduSharingService
52
     */
53
    private EduSharingService $service;
54
    /**
55
     * @var UtilityFunctions|null
56
     */
57
    private ?UtilityFunctions $utils;
58
 
59
    /**
60
     * MetadataLogic constructor
61
     *
62
     * @param EduSharingService $service
63
     * @param UtilityFunctions|null $utils
64
     */
65
    public function __construct(EduSharingService $service, ?UtilityFunctions $utils = null) {
66
        $this->service = $service;
67
        $this->utils   = $utils;
68
        global $CFG;
69
        require_once($CFG->dirroot . '/mod/edusharing/eduSharingAutoloader.php');
70
        $this->init();
71
    }
72
 
73
    /**
74
     * Function init
75
     *
76
     * @return void
77
     */
78
    private function init(): void {
79
        $this->utils === null && $this->utils = new UtilityFunctions();
80
    }
81
 
82
    /**
83
     * Function import_metadata
84
     *
85
     * @param string $metadataurl
86
     * @param string|null $host
87
     * @throws EduSharingUserException
88
     * @throws dml_exception
89
     */
90
    public function import_metadata(string $metadataurl, ?string $host = null): void {
91
        global $CFG;
92
        $xml = new DOMDocument();
93
        libxml_use_internal_errors(true);
94
        $result = $this->service->import_metadata($metadataurl);
95
        if ($result->error !== 0) {
96
            $message = $result->info['message'] ?? 'unknown';
97
            debugging('cURL Error: ' . $message);
98
            $this->reloadform = true;
99
            throw new EduSharingUserException($message, 0, null,
100
                '<p style="background: #FF8170">cURL Error: ' . $message . '<br></p>');
101
        }
102
        if (!$xml->loadXML($result->content)) {
103
            $this->reloadform = true;
104
            throw new EduSharingUserException('xml error', 0, null,
105
                '<p style="background: #FF8170">could not load ' . $metadataurl . ' please check url <br></p>');
106
        }
107
        $xml->preserveWhiteSpace = false;
108
        $xml->formatOutput       = true;
109
        $entries                 = $xml->getElementsByTagName('entry');
110
        if ($this->appid === null) {
111
            $this->appid = !empty($this->utils->get_config_entry('application_appid'))
112
                ? $this->utils->get_config_entry('application_appid') : uniqid('moodle_');
113
        }
114
        $repoid     = $this->utils->get_config_entry('repository_appid');
115
        $privatekey = $this->utils->get_config_entry('application_private_key');
116
        $publickey  = $this->utils->get_config_entry('application_public_key');
117
        foreach ($entries as $entry) {
118
            $this->utils->set_config_entry('repository_' . $entry->getAttribute('key'), $entry->nodeValue);
119
        }
120
        if (empty ($host)) {
121
            if (!empty($_SERVER['SERVER_ADDR'])) {
122
                $host = $_SERVER['SERVER_ADDR'];
123
            } else if (!empty($_SERVER['SERVER_NAME'])) {
124
                $host = gethostbyname($_SERVER['SERVER_NAME']);
125
            } else {
126
                throw new Exception('Host could not be discerned. Cancelling ES-registration process.');
127
            }
128
        }
129
        $clientprotocol = $this->utils->get_config_entry('repository_clientprotocol');
130
        $repodomain     = $this->utils->get_config_entry('repository_domain');
131
        $clientport     = $this->utils->get_config_entry('repository_clientport');
132
        $this->utils->set_config_entry('application_host', $host);
133
        $this->utils->set_config_entry('application_appid', $this->appid);
134
        $this->utils->set_config_entry('application_type', 'LMS');
135
        $this->utils->set_config_entry('application_homerepid', $repoid);
136
        $this->utils->set_config_entry(
137
            'application_cc_gui_url', $clientprotocol . '://' . $repodomain . ':' . $clientport . '/edu-sharing/'
138
        );
139
        if ($this->hostaliases !== null) {
140
            $this->utils->set_config_entry('application_host_aliases', $this->hostaliases);
141
        }
142
        if ($this->wloguestuser !== null) {
143
            $this->utils->set_config_entry('wlo_guest_option', '1');
144
            $this->utils->set_config_entry('edu_guest_guest_id', $this->wloguestuser);
145
        }
146
        if (empty($privatekey) || empty($publickey)) {
147
            $keypair = EduSharingHelper::generateKeyPair();
148
            $this->utils->set_config_entry('application_private_key', $keypair['privateKey']);
149
            $this->utils->set_config_entry('application_public_key', $keypair['publicKey']);
150
        }
151
        if (empty($this->utils->get_config_entry('application_private_key'))) {
152
            throw new EduSharingUserException('ssl keypair generation error', 0, null,
153
                '<h3 class="edu_error">Generating of SSL keys failed. Please check your configuration.</h3>');
154
        }
155
        $this->utils->set_config_entry('application_blowfishkey', 'thetestkey');
156
        $this->utils->set_config_entry('application_blowfishiv', 'initvect');
157
        $this->utils->set_config_entry('EDU_AUTH_KEY', 'username');
158
        $this->utils->set_config_entry('EDU_AUTH_PARAM_NAME_USERID', 'userid');
159
        $this->utils->set_config_entry('EDU_AUTH_PARAM_NAME_LASTNAME', 'lastname');
160
        $this->utils->set_config_entry('EDU_AUTH_PARAM_NAME_FIRSTNAME', 'firstname');
161
        $this->utils->set_config_entry('EDU_AUTH_PARAM_NAME_EMAIL', 'email');
162
        $this->utils->set_config_entry('EDU_AUTH_AFFILIATION', $CFG->siteidentifier);
163
        $this->utils->set_config_entry('EDU_AUTH_AFFILIATION_NAME', $CFG->siteidentifier);
164
    }
165
 
166
    /**
167
     * Function create_xml_metadata
168
     *
169
     * @return string
170
     */
171
    public function create_xml_metadata(): string {
172
        global $CFG;
173
        // phpcs:disable -- This is just a long string.
174
        $xml = new SimpleXMLElement(
175
            '<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties></properties>');
176
        // phpcs:enable
177
        try {
178
            $entry = $xml->addChild('entry', $this->utils->get_config_entry('application_appid'));
179
            $entry->addAttribute('key', 'appid');
180
            $entry = $xml->addChild('entry', $this->utils->get_config_entry('application_type'));
181
            $entry->addAttribute('key', 'type');
182
            $entry = $xml->addChild('entry', 'moodle');
183
            $entry->addAttribute('key', 'subtype');
184
            $entry = $xml->addChild('entry', parse_url($CFG->wwwroot, PHP_URL_HOST));
185
            $entry->addAttribute('key', 'domain');
186
            $entry = $xml->addChild('entry', $this->utils->get_config_entry('application_host'));
187
            $entry->addAttribute('key', 'host');
188
            $entry = $xml->addChild('entry', $this->utils->get_config_entry('application_host_aliases'));
189
            $entry->addAttribute('key', 'host_aliases');
190
            $entry = $xml->addChild('entry', 'true');
191
            $entry->addAttribute('key', 'trustedclient');
192
            $entry = $xml->addChild('entry', 'moodle:course/update');
193
            $entry->addAttribute('key', 'hasTeachingPermission');
194
            $entry = $xml->addChild('entry', $this->utils->get_config_entry('application_public_key'));
195
            $entry->addAttribute('key', 'public_key');
196
            $entry = $xml->addChild('entry', $this->utils->get_config_entry('EDU_AUTH_AFFILIATION_NAME'));
197
            $entry->addAttribute('key', 'appcaption');
198
            if ($this->utils->get_config_entry('wlo_guest_option')) {
199
                $entry = $xml->addChild('entry', $this->utils->get_config_entry('edu_guest_guest_id'));
200
                $entry->addAttribute('key', 'auth_by_app_user_whitelist');
201
            }
202
        } catch (dml_exception $exception) {
203
            unset($exception);
204
 
205
            return '';
206
        }
207
 
208
        return html_entity_decode($xml->asXML(), ENT_COMPAT);
209
    }
210
 
211
    /**
212
     * Function set_host_aliases
213
     *
214
     * @param string $hostaliases
215
     * @return void
216
     */
217
    public function set_host_aliases(string $hostaliases): void {
218
        $this->hostaliases = $hostaliases;
219
    }
220
 
221
    /**
222
     * Function set_wlo_guest_user
223
     *
224
     * @param string $wloguestuser
225
     * @return void
226
     */
227
    public function set_wlo_guest_user(string $wloguestuser): void {
228
        $this->wloguestuser = $wloguestuser;
229
    }
230
 
231
 
232
    /**
233
     * Function set_app_id
234
     *
235
     * @param string $appid
236
     * @return void
237
     */
238
    public function set_app_id(string $appid): void {
239
        $this->appid = $appid;
240
    }
241
}