forked from catalyst/moodle-auth_saml2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
regenerate.php
96 lines (78 loc) · 3.3 KB
/
regenerate.php
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Regenerate the Private Key and Certificate files
*
* @package auth_saml2
* @copyright Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require('setup.php');
require_login();
require_capability('moodle/site:config', context_system::instance());
$heading = get_string('regenerateheading', 'auth_saml2');
$here = "$CFG->wwwroot/auth/saml2/regenerate.php";
auth_saml2_admin_nav($heading, $here);
$mform = new \auth_saml2\form\regenerate();
if ($mform->is_cancelled()) {
redirect("$CFG->wwwroot/admin/settings.php?section=authsettingsaml2");
}
$path = $saml2auth->certcrt;
$error = '';
$success = false;
if ($fromform = $mform->get_data()) {
try {
auth_saml2_process_regenerate_form($fromform);
redirect(new moodle_url('/auth/saml2/cert.php'), get_string('success'), null, \core\output\notification::NOTIFY_SUCCESS);
} catch (saml2_exception $exception) {
$error = $exception->getMessage() . $exception->getTraceAsString();
}
}
echo $OUTPUT->header();
echo $OUTPUT->heading($heading);
echo "<p>Path: $path</p>";
// Load data from the current certificate.
$data = openssl_x509_parse(file_get_contents($path));
// Calculate date expirey interval.
$date1 = date("Y-m-d\TH:i:s\Z", str_replace ('Z', '', $data['validFrom_time_t']));
$date2 = date("Y-m-d\TH:i:s\Z", str_replace ('Z', '', $data['validTo_time_t']));
$datetime1 = new DateTime($date1);
$datetime2 = new DateTime($date2);
$interval = $datetime1->diff($datetime2);
$expirydays = $interval->format('%a');
$toform = array (
"email" => $data['subject']['emailAddress'],
"expirydays" => $expirydays,
"commonname" => substr($data['subject']['CN'], 0, 64),
"countryname" => $data['subject']['C'],
"localityname" => $data['subject']['L'],
"organizationname" => $data['subject']['O'],
"stateorprovincename" => $data['subject']['ST'],
"organizationalunitname" => $data['subject']['OU'],
);
$mform->set_data($toform); // Load current data into form.
if ($success) {
echo $OUTPUT->notification(get_string('regeneratesuccess', 'auth_saml2'), \core\output\notification::NOTIFY_SUCCESS);
} else if ($error) {
echo $OUTPUT->notification($error, \core\output\notification::NOTIFY_ERROR);
} else {
echo $OUTPUT->notification(get_string('regeneratewarning', 'auth_saml2'), \core\output\notification::NOTIFY_WARNING);
}
echo html_writer::tag('h1', get_string('regenerateheader', 'auth_saml2'));
echo html_writer::tag('p', get_string('regeneratepath', 'auth_saml2', $path));
$mform->display();
echo $OUTPUT->footer();