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 JsonException;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Class PluginRegistration
|
|
|
25 |
*
|
|
|
26 |
* @author Marian Ziegler <ziegler@edu-sharing.net>
|
|
|
27 |
* @package mod_edusharing
|
|
|
28 |
*/
|
|
|
29 |
class PluginRegistration {
|
|
|
30 |
/**
|
|
|
31 |
* @var EduSharingService
|
|
|
32 |
*/
|
|
|
33 |
private EduSharingService $service;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* PluginRegistration constructor
|
|
|
37 |
*
|
|
|
38 |
* @param EduSharingService $service
|
|
|
39 |
*/
|
|
|
40 |
public function __construct(EduSharingService $service) {
|
|
|
41 |
$this->service = $service;
|
|
|
42 |
global $CFG;
|
|
|
43 |
require_once($CFG->dirroot . '/mod/edusharing/eduSharingAutoloader.php');
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Function register_plugin
|
|
|
48 |
*
|
|
|
49 |
* @param string $repourl
|
|
|
50 |
* @param string $login
|
|
|
51 |
* @param string $pwd
|
|
|
52 |
* @param string $data
|
|
|
53 |
* @return array
|
|
|
54 |
* @throws EduSharingUserException
|
|
|
55 |
* @throws JsonException
|
|
|
56 |
*/
|
|
|
57 |
public function register_plugin(string $repourl, string $login, string $pwd, string $data): array {
|
|
|
58 |
$this->validate_alfresco_session($repourl, $login . ':' . $pwd);
|
|
|
59 |
return $this->perform_registration($repourl, $data, $login . ':' . $pwd);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Function validate_alfresco_session
|
|
|
64 |
*
|
|
|
65 |
* @param string $repourl
|
|
|
66 |
* @param string $auth
|
|
|
67 |
* @throws EduSharingUserException
|
|
|
68 |
* @throws JsonException
|
|
|
69 |
*/
|
|
|
70 |
private function validate_alfresco_session(string $repourl, string $auth): void {
|
|
|
71 |
$result = $this->service->validate_session($repourl, $auth);
|
|
|
72 |
if ($result->error !== 0) {
|
|
|
73 |
throw new EduSharingUserException('API connection error');
|
|
|
74 |
}
|
|
|
75 |
$data = json_decode($result->content, true, 512, JSON_THROW_ON_ERROR);
|
|
|
76 |
if (($data['isAdmin'] ?? false) === false) {
|
|
|
77 |
throw new EduSharingUserException('Given user / password was not accepted as admin');
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Function perform_registration
|
|
|
83 |
*
|
|
|
84 |
* @param string $repourl
|
|
|
85 |
* @param string $data
|
|
|
86 |
* @param string $auth
|
|
|
87 |
* @return array
|
|
|
88 |
* @throws EduSharingUserException
|
|
|
89 |
* @throws JsonException
|
|
|
90 |
*/
|
|
|
91 |
private function perform_registration(string $repourl, string $data, string $auth): array {
|
|
|
92 |
$delimiter = '-------------' . uniqid();
|
|
|
93 |
$body = $this->get_registration_api_body($delimiter, $data);
|
|
|
94 |
$result = $this->service->register_plugin($repourl, $delimiter, $body, $auth);
|
|
|
95 |
if ($result->error !== 0) {
|
|
|
96 |
throw new EduSharingUserException('API connection error');
|
|
|
97 |
}
|
|
|
98 |
return json_decode($result->content, true, 512, JSON_THROW_ON_ERROR);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Function get_registration_api_body
|
|
|
103 |
*
|
|
|
104 |
* @param string $delimiter
|
|
|
105 |
* @param string $data
|
|
|
106 |
* @return string
|
|
|
107 |
*/
|
|
|
108 |
private function get_registration_api_body(string $delimiter, string $data): string {
|
|
|
109 |
$body = '--' . $delimiter . "\r\n";
|
|
|
110 |
$body .= 'Content-Disposition: form-data; name="' . 'xml' . '"';
|
|
|
111 |
$body .= '; filename="metadata.xml"' . "\r\n";
|
|
|
112 |
$body .= 'Content-Type: text/xml' . "\r\n\r\n";
|
|
|
113 |
$body .= $data . "\r\n";
|
|
|
114 |
$body .= "--" . $delimiter . "--\r\n";
|
|
|
115 |
|
|
|
116 |
return $body;
|
|
|
117 |
}
|
|
|
118 |
}
|