Subversion Repositories oidplus

Rev

Rev 1266 | Rev 1305 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1266 Rev 1293
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * OIDplus 2.0
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
6
 *
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with 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
9
 * You may obtain a copy of the License at
10
 *
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
17
 * limitations under the License.
18
 */
18
 */
19
 
19
 
20
// ATTENTION: If you change something, please make sure that the changes
20
// ATTENTION: If you change something, please make sure that the changes
21
//            are synchronous with OIDplusPageRaAutomatedAJAXCalls
21
//            are synchronous with OIDplusPageRaAutomatedAJAXCalls
22
 
22
 
23
namespace ViaThinkSoft\OIDplus;
23
namespace ViaThinkSoft\OIDplus;
24
 
24
 
25
// phpcs:disable PSR1.Files.SideEffects
25
// phpcs:disable PSR1.Files.SideEffects
26
\defined('INSIDE_OIDPLUS') or die;
26
\defined('INSIDE_OIDPLUS') or die;
27
// phpcs:enable PSR1.Files.SideEffects
27
// phpcs:enable PSR1.Files.SideEffects
28
 
28
 
29
class OIDplusPageAdminAutomatedAJAXCalls extends OIDplusPagePluginAdmin {
29
class OIDplusPageAdminAutomatedAJAXCalls extends OIDplusPagePluginAdmin {
30
 
30
 
31
        /**
31
        /**
32
         * @param string $actionID
-
 
33
         * @param array $params
32
         * @param array $params
34
         * @return array
33
         * @return array
35
         * @throws OIDplusException
34
         * @throws OIDplusException
36
         */
35
         */
37
        public function action(string $actionID, array $params): array {
36
        private function action_Blacklist(array $params): array {
38
                if ($actionID == 'blacklistJWT') {
-
 
39
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
37
                if (!OIDplus::authUtils()->isAdminLoggedIn()) {
40
                                throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), null, 401);
38
                        throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), null, 401);
41
                        }
39
                }
42
 
40
 
43
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_ADMIN', true)) {
41
                if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_ADMIN', true)) {
44
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_ADMIN'));
42
                        throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_ADMIN'));
45
                        }
43
                }
46
 
44
 
47
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
45
                $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
48
                        $sub = 'admin';
46
                $sub = 'admin';
49
 
47
 
50
                        OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub);
48
                OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub);
51
 
49
 
52
                        return array("status" => 0);
50
                return array("status" => 0);
-
 
51
        }
-
 
52
 
-
 
53
        /**
-
 
54
         * @param string $actionID
-
 
55
         * @param array $params
-
 
56
         * @return array
-
 
57
         * @throws OIDplusException
-
 
58
         */
-
 
59
        public function action(string $actionID, array $params): array {
-
 
60
                if ($actionID == 'blacklistJWT') {
-
 
61
                        return $this->action_Blacklist($params);
53
                } else {
62
                } else {
54
                        return parent::action($actionID, $params);
63
                        return parent::action($actionID, $params);
55
                }
64
                }
56
        }
65
        }
57
 
66
 
58
        /**
67
        /**
59
         * @param string $id
68
         * @param string $id
60
         * @param array $out
69
         * @param array $out
61
         * @param bool $handled
70
         * @param bool $handled
62
         * @return void
71
         * @return void
63
         * @throws OIDplusException
72
         * @throws OIDplusException
64
         */
73
         */
65
        public function gui(string $id, array &$out, bool &$handled) {
74
        public function gui(string $id, array &$out, bool &$handled) {
66
                if ($id === 'oidplus:automated_ajax_information_admin') {
75
                if ($id === 'oidplus:automated_ajax_information_admin') {
67
                        $handled = true;
76
                        $handled = true;
68
                        $out['title'] = _L('Automated AJAX calls');
77
                        $out['title'] = _L('Automated AJAX calls');
69
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
78
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
70
 
79
 
71
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
80
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
72
                                throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401);
81
                                throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401);
73
                        }
82
                        }
74
 
83
 
75
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_ADMIN', true)) {
84
                        if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_ADMIN', true)) {
76
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_ADMIN'), $out['title']);
85
                                throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_ADMIN'), $out['title']);
77
                        }
86
                        }
78
 
87
 
79
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
88
                        $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
80
                        $sub = 'admin';
89
                        $sub = 'admin';
81
 
90
 
82
                        $authSimulation = new OIDplusAuthContentStoreJWT();
91
                        $authSimulation = new OIDplusAuthContentStoreJWT();
83
                        $authSimulation->adminLogin();
92
                        $authSimulation->adminLogin();
84
                        $authSimulation->setValue('oidplus_generator', $gen);
93
                        $authSimulation->setValue('oidplus_generator', $gen);
85
                        $token = $authSimulation->getJWTToken();
94
                        $token = $authSimulation->getJWTToken();
86
 
95
 
87
                        $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>';
96
                        $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>';
88
                        $out['text'] .= '<p>'._L('The URL for the AJAX script is:').'</p>';
97
                        $out['text'] .= '<p>'._L('The URL for the AJAX script is:').'</p>';
89
                        $out['text'] .= '<p><b>'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php</b></p>';
98
                        $out['text'] .= '<p><b>'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php</b></p>';
90
                        $out['text'] .= '<p>'._L('You must at least provide following fields:').'</p>';
99
                        $out['text'] .= '<p>'._L('You must at least provide following fields:').'</p>';
91
                        $out['text'] .= '<p><pre id="oidplus_auth_jwt">';
100
                        $out['text'] .= '<p><pre id="oidplus_auth_jwt">';
92
                        $out['text'] .= htmlentities(OIDplusAuthContentStoreJWT::COOKIE_NAME).' = "'.htmlentities($token).'"'."\n";
101
                        $out['text'] .= htmlentities(OIDplusAuthContentStoreJWT::COOKIE_NAME).' = "'.htmlentities($token).'"'."\n";
93
                        $out['text'] .= '</pre></p>';
102
                        $out['text'] .= '</pre></p>';
94
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>';
103
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>';
95
                        $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
104
                        $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
96
                        $out['text'] .= '<p>'._L('The JWT-token (secret!) will automatically perform a one-time-login to fulfill the request. The other fields are the normal fields which are called during the usual operation of OIDplus.').'</p>';
105
                        $out['text'] .= '<p>'._L('The JWT-token (secret!) will automatically perform a one-time-login to fulfill the request. The other fields are the normal fields which are called during the usual operation of OIDplus.').'</p>';
97
                        $out['text'] .= '<p>'._L('Currently, there is no documentation for the AJAX calls. However, you can look at the <b>script.js</b> files of the plugins to see the field names being used. You can also enable network analysis in your web browser debugger (F12) to see the request headers sent to the server during the operation of OIDplus.').'</p>';
106
                        $out['text'] .= '<p>'._L('Currently, there is no documentation for the AJAX calls. However, you can look at the <b>script.js</b> files of the plugins to see the field names being used. You can also enable network analysis in your web browser debugger (F12) to see the request headers sent to the server during the operation of OIDplus.').'</p>';
98
 
107
 
99
                        $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>';
108
                        $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>';
100
                        $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub);
109
                        $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub);
101
                        if ($bl_time == 0) {
110
                        if ($bl_time == 0) {
102
                                $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>';
111
                                $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>';
103
                        } else {
112
                        } else {
104
                                $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>';
113
                                $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>';
105
                        }
114
                        }
106
                        $out['text'] .= '<button type="button" name="btn_blacklist_jwt" id="btn_blacklist_jwt" class="btn btn-danger btn-xs" onclick="OIDplusPageAdminAutomatedAJAXCalls.blacklistJWT()">'._L('Blacklist all previously generated tokens').'</button>';
115
                        $out['text'] .= '<button type="button" name="btn_blacklist_jwt" id="btn_blacklist_jwt" class="btn btn-danger btn-xs" onclick="OIDplusPageAdminAutomatedAJAXCalls.blacklistJWT()">'._L('Blacklist all previously generated tokens').'</button>';
107
 
116
 
108
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>';
117
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>';
109
                        $cont = file_get_contents(__DIR__.'/examples/example_js.html');
118
                        $cont = file_get_contents(__DIR__.'/examples/example_js.html');
110
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
119
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
111
                        $cont = str_replace('<token>', $token, $cont);
120
                        $cont = str_replace('<token>', $token, $cont);
112
                        $out['text'] .= '<pre id="example_js">'.htmlentities($cont).'</pre>';
121
                        $out['text'] .= '<pre id="example_js">'.htmlentities($cont).'</pre>';
113
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_js)"></p>';
122
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_js)"></p>';
114
 
123
 
115
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>';
124
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>';
116
                        $cont = file_get_contents(__DIR__.'/examples/example_php.phps');
125
                        $cont = file_get_contents(__DIR__.'/examples/example_php.phps');
117
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
126
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
118
                        $cont = str_replace('<token>', $token, $cont);
127
                        $cont = str_replace('<token>', $token, $cont);
119
                        $out['text'] .= '<pre id="example_php">'.preg_replace("@<br.*>@ismU","",highlight_string($cont,true)).'</pre>';
128
                        $out['text'] .= '<pre id="example_php">'.preg_replace("@<br.*>@ismU","",highlight_string($cont,true)).'</pre>';
120
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_php)"></p>';
129
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_php)"></p>';
121
 
130
 
122
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using Python').'</h2>';
131
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using Python').'</h2>';
123
                        $cont = file_get_contents(__DIR__.'/examples/example_python.py');
132
                        $cont = file_get_contents(__DIR__.'/examples/example_python.py');
124
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
133
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
125
                        $cont = str_replace('<token>', $token, $cont);
134
                        $cont = str_replace('<token>', $token, $cont);
126
                        $out['text'] .= '<pre id="example_python">'.htmlentities($cont).'</pre>';
135
                        $out['text'] .= '<pre id="example_python">'.htmlentities($cont).'</pre>';
127
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_python)"></p>';
136
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_python)"></p>';
128
 
137
 
129
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using VBScript').'</h2>';
138
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using VBScript').'</h2>';
130
                        $cont = file_get_contents(__DIR__.'/examples/example_vbs.vbs');
139
                        $cont = file_get_contents(__DIR__.'/examples/example_vbs.vbs');
131
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
140
                        $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
132
                        $cont = str_replace('<token>', $token, $cont);
141
                        $cont = str_replace('<token>', $token, $cont);
133
                        $out['text'] .= '<pre id="example_vbs">'.htmlentities($cont).'</pre>';
142
                        $out['text'] .= '<pre id="example_vbs">'.htmlentities($cont).'</pre>';
134
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_vbs)"></p>';
143
                        $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_vbs)"></p>';
135
                }
144
                }
136
        }
145
        }
137
 
146
 
138
        /**
147
        /**
139
         * @param array $json
148
         * @param array $json
140
         * @param string|null $ra_email
149
         * @param string|null $ra_email
141
         * @param bool $nonjs
150
         * @param bool $nonjs
142
         * @param string $req_goto
151
         * @param string $req_goto
143
         * @return bool
152
         * @return bool
144
         * @throws OIDplusException
153
         * @throws OIDplusException
145
         */
154
         */
146
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
155
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
147
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
156
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
148
 
157
 
149
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
158
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
150
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
159
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
151
                } else {
160
                } else {
152
                        $tree_icon = null; // default icon (folder)
161
                        $tree_icon = null; // default icon (folder)
153
                }
162
                }
154
 
163
 
155
                $json[] = array(
164
                $json[] = array(
156
                        'id' => 'oidplus:automated_ajax_information_admin',
165
                        'id' => 'oidplus:automated_ajax_information_admin',
157
                        'icon' => $tree_icon,
166
                        'icon' => $tree_icon,
158
                        'text' => _L('Automated AJAX calls')
167
                        'text' => _L('Automated AJAX calls')
159
                );
168
                );
160
 
169
 
161
                return true;
170
                return true;
162
        }
171
        }
163
 
172
 
164
        /**
173
        /**
165
         * @param string $request
174
         * @param string $request
166
         * @return array|false
175
         * @return array|false
167
         */
176
         */
168
        public function tree_search(string $request) {
177
        public function tree_search(string $request) {
169
                return false;
178
                return false;
170
        }
179
        }
171
}
180
}
172
 
181