Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
329 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
// ATTENTION: If you change something, please make sure that the changes
21
//            are synchronous with OIDplusPageRaAutomatedAJAXCalls
22
 
23
class OIDplusPageAdminAutomatedAJAXCalls extends OIDplusPagePluginAdmin {
24
 
25
        private static function getUnlockKey($user) {
26
                // This key prevents that the system gets hacked with brute
27
                // force of the user passwords.
28
                return sha1('ANTI-BRUTEFORCE-AJAX/'.$user.'/'.OIDplus::baseConfig()->getValue('SERVER_SECRET',''));
29
        }
30
 
31
        private $autoLoggedIn = false;
32
 
33
        private function shutdownLogout() {
34
                if ($this->autoLoggedIn) {
35
                        OIDplus::authUtils()::adminLogout();
36
                }
37
        }
38
 
39
        public function init($html=true) {
40
                if (isset($_SERVER['SCRIPT_FILENAME']) && (basename($_SERVER['SCRIPT_FILENAME']) == 'ajax.php')) {
41
                        $input = array_merge($_POST,$_GET);
42
 
43
                        if (!isset($input['batch_login_username'])) return;
44
                        if (!isset($input['batch_login_password'])) return;
45
                        if (!isset($input['batch_ajax_unlock_key'])) return;
46
                        if ($input['batch_ajax_unlock_key'] != self::getUnlockKey($input['batch_login_username'])) return;
47
 
48
                        if (($input['batch_login_username'] == 'admin') && !OIDplus::authUtils()::isAdminLoggedIn() && OIDplus::authUtils()::adminCheckPassword($input['batch_login_password'])) {
49
                                OIDplus::authUtils()::adminLogin();
50
                                $this->autoLoggedIn = true;
51
                                register_shutdown_function(array($this,'shutdownLogout'));
52
                        }
53
                }
54
        }
55
 
56
        public function gui($id, &$out, &$handled) {
57
                if ($id === 'oidplus:automated_ajax_information_admin') {
58
                        $handled = true;
360 daniel-mar 59
                        $out['title'] = _L('Automated AJAX calls');
329 daniel-mar 60
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
61
 
62
                        if (!OIDplus::authUtils()::isAdminLoggedIn()) {
63
                                $out['icon'] = 'img/error_big.png';
360 daniel-mar 64
                                $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login')).'</p>';
329 daniel-mar 65
                                return;
66
                        }
67
 
360 daniel-mar 68
                        $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>';
69
                        $out['text'] .= '<p>'._L('The URL for the AJAX script is:').':</p>';
329 daniel-mar 70
                        $out['text'] .= '<p><b>'.OIDplus::getSystemUrl().'ajax.php</b></p>';
360 daniel-mar 71
                        $out['text'] .= '<p>'._L('You must at least provide following fields').':</p>';
329 daniel-mar 72
                        $out['text'] .= '<p><pre>';
73
                        $out['text'] .= 'batch_login_username  = "admin"'."\n";
74
                        $out['text'] .= 'batch_login_password  = "........."'."\n";
75
                        $out['text'] .= 'batch_ajax_unlock_key = "'.$this->getUnlockKey('admin').'"'."\n";
76
                        $out['text'] .= '</pre></p>';
360 daniel-mar 77
                        $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
78
                        $out['text'] .= '<p>'._L('The batch-fields 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>';
79
                        $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>';
80
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>';
329 daniel-mar 81
                        $out['text'] .= '<pre>'.htmlentities(file_get_contents(__DIR__.'/examples/example_js.html')).'</pre>';
360 daniel-mar 82
                        $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>';
329 daniel-mar 83
                        $out['text'] .= '<pre>'.preg_replace("@<br.*>@ismU","",highlight_file(__DIR__.'/examples/example_php.phps',true)).'</pre>';
84
                }
85
        }
86
 
87
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
88
                if (!OIDplus::authUtils()::isAdminLoggedIn()) return false;
89
 
90
                if (file_exists(__DIR__.'/treeicon.png')) {
91
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
92
                } else {
93
                        $tree_icon = null; // default icon (folder)
94
                }
95
 
96
                $json[] = array(
97
                        'id' => 'oidplus:automated_ajax_information_admin',
98
                        'icon' => $tree_icon,
360 daniel-mar 99
                        'text' => _L('Automated AJAX calls')
329 daniel-mar 100
                );
101
 
102
                return true;
103
        }
104
 
105
        public function tree_search($request) {
106
                return false;
107
        }
360 daniel-mar 108
}