Subversion Repositories oidplus

Rev

Rev 104 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
74 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
class OIDplusRegistrationWizard extends OIDplusPagePlugin {
21
        public function type() {
22
                return 'system';
23
        }
24
 
25
        public function priority() {
26
                return 000;
27
        }
28
 
29
        public function action(&$handled) {
30
                // Nothing
31
        }
32
 
33
        public function cfgSetValue($name, $value) {
34
                if ($name == 'reg_enabled') {
35
                        if (($value != '0') && ($value != '1')) {
36
                                throw new Exception("Please enter either 0 or 1.");
37
                        }
38
                }
39
        }
40
 
41
        public function gui($id, &$out, &$handled) {
42
                // nothing
43
        }
44
 
106 daniel-mar 45
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
104 daniel-mar 46
                return false;
74 daniel-mar 47
        }
48
 
49
        public function init($html=true) {
75 daniel-mar 50
                OIDplus::config()->prepareConfigKey('registration_done', 'Registration wizard done once?', '0', 1, 0);
51
                OIDplus::config()->prepareConfigKey('reg_enabled', 'Register your system to the ViaThinkSoft directory?', '0', 0, 1);
52
                OIDplus::config()->prepareConfigKey('reg_ping_interval', 'Registration ping interval', '3600', 0, 0);
53
                OIDplus::config()->prepareConfigKey('reg_last_ping', 'Last ping to ViaThinkSoft directory services', '0', 1, 0);
54
 
74 daniel-mar 55
                if (function_exists('openssl_sign')) {
56
                        // This is what we answer to the ViaThinkSoft server
57
 
58
                        if (isset($_REQUEST['vts_regqry'])) {
59
                                $payload = array(
60
                                        "version" => 1,
61
                                        "vts_directory_listing" => OIDplus::config()->getValue('reg_enabled') ? true : false,
62
                                        "oidinfo_xml_unlocked" => OIDplus::config()->exists('oidinfo_export_protected') && !OIDplus::config()->getValue('oidinfo_export_protected') ? true : false,
63
                                        "oidinfo_xml_location" => 'plugins/adminPages/400_oidinfo_export/oidinfo_export.php?online=1'
64
                                );
65
 
66
                                $signature = '';
67
                                openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'));
68
 
69
                                $data = array(
70
                                        "payload" => $payload,
71
                                "signature" => base64_encode($signature)
72
                                );
73
 
74
                                header_remove('Content-Type');
75
                                header('Content-Type: application/json');
76
                                die(json_encode($data));
77
                        }
78
 
79
                        // Show registration wizard once
80
 
81
                        if ($html && (OIDplus::config()->getValue('registration_done') != '1')) {
82
                                if (basename($_SERVER['SCRIPT_NAME']) != 'registration.php') {
83
                                        header('Location:plugins/system/'.basename(__DIR__).'/registration.php');
84
                                        die();
85
                                }
86
                        }
87
 
88
                        // Is it time to register / renew directory entry?
89
 
76 daniel-mar 90
                        if ((OIDplus::config()->getValue('reg_enabled')) &&
91
                           (time()-OIDplus::config()->getValue('reg_last_ping') >= OIDplus::config()->getValue('reg_ping_interval'))) {
74 daniel-mar 92
                                if ($system_url = OIDplus::system_url()) {
93
                                        $payload = array(
94
                                                "system_id" => OIDplus::system_id(false),
95
                                                "public_key" => OIDplus::config()->getValue('oidplus_public_key'),
96
                                                "system_url" => $system_url,
97
                                                "hide_system_url" => 0,
76 daniel-mar 98
                                                "hide_public_key" => 0,
99
                                                "admin_email" => OIDplus::config()->getValue('admin_email'),
100
                                                "system_title" => OIDplus::config()->systemTitle()
74 daniel-mar 101
                                        );
102
 
103
                                        $signature = '';
104
                                        openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'));
105
 
106
                                        $data = array(
107
                                                "payload" => $payload,
108
                                                "signature" => base64_encode($signature)
109
                                        );
110
 
111
                                        $res = file_get_contents('https://oidplus.viathinksoft.com/reg/register.php?data='.base64_encode(json_encode($data)));
112
                                        // die("RES: $res\n");
113
                                        // if ($res == 'OK') ...
114
 
115
                                        OIDplus::config()->setValue('reg_last_ping', time());
116
                                }
117
                        }
118
                }
119
        }
120
}
121
 
122
OIDplus::registerPagePlugin(new OIDplusRegistrationWizard());