1<?php
2namespace LAM\CONFIG;
3use htmlButton;
4use htmlGroup;
5use htmlInputFileUpload;
6use htmlLink;
7use htmlOutputText;
8use htmlResponsiveInputCheckbox;
9use htmlResponsiveInputField;
10use htmlResponsiveRow;
11use htmlStatusMessage;
12use htmlSubTitle;
13use LAM\PERSISTENCE\ConfigDataExporter;
14use LAM\PERSISTENCE\ConfigDataImporter;
15use LAMCfgMain;
16use LAMException;
17
18/*
19
20  This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
21  Copyright (C) 2020  Roland Gruber
22
23  This program is free software; you can redistribute it and/or modify
24  it under the terms of the GNU General Public License as published by
25  the Free Software Foundation; either version 2 of the License, or
26  (at your option) any later version.
27
28  This program is distributed in the hope that it will be useful,
29  but WITHOUT ANY WARRANTY; without even the implied warranty of
30  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  GNU General Public License for more details.
32
33  You should have received a copy of the GNU General Public License
34  along with this program; if not, write to the Free Software
35  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
36
37*/
38
39
40/**
41* Import and export functions for LAM configuration.
42*
43* @package configuration
44* @author Roland Gruber
45*/
46
47
48/** Access to persistence functions */
49include_once('../../lib/persistence.inc');
50
51// start session
52if (strtolower(session_module_name()) == 'files') {
53	session_save_path("../../sess");
54}
55lam_start_session();
56
57setlanguage();
58
59if (!isset($_SESSION['cfgMain'])) {
60	$cfg = new LAMCfgMain();
61	$_SESSION['cfgMain'] = $cfg;
62}
63$cfg = &$_SESSION['cfgMain'];
64
65// export
66if (isset($_POST['exportConfig']) && $cfg->checkPassword($_SESSION["mainconf_password"])) {
67	$exporter = new ConfigDataExporter();
68	if (!headers_sent()) {
69		header('Content-Type: application/json; charset=utf-8');
70		header('Content-disposition: attachment; filename=lam-config.json');
71	}
72	try {
73		echo $exporter->exportAsJson();
74    }
75	catch (LAMException $e) {
76	    logNewMessage('ERROR', $e->getTitle() . ' ' . $e->getMessage());
77    }
78	exit;
79}
80
81echo $_SESSION['header'];
82printHeaderContents(_("Import and export configuration"), '../..');
83
84?>
85	</head>
86	<body class="admin">
87    <?php
88        // include all JavaScript files
89        printJsIncludes('../..');
90    ?>
91    <table class="lamTop ui-corner-all">
92        <tr>
93            <td align="left">
94                <a class="lamLogo" href="http://www.ldap-account-manager.org/" target="new_window">
95				    <?php echo getLAMVersionText(); ?>
96                </a>
97            </td>
98        </tr>
99    </table>
100    <form action="confImportExport.php" method="post" autocomplete="off" enctype="multipart/form-data">
101    <br><br>
102    <?php
103
104    // check if user is logged in
105    if (!isset($_POST['submitLogin']) && !isset($_SESSION["mainconf_password"])) {
106        showLoginDialog();
107        exit();
108    }
109
110    // check login
111    if (isset($_POST['submitLogin']) && !checkLogin($cfg)) {
112	    exit();
113    }
114
115    displayImportExport();
116
117    /**
118     * Shows the login dialog for the configuration master password.
119     *
120     * @param htmlStatusMessage $message message to show if any error occured
121     */
122	function showLoginDialog($message = null) {
123    	$tabindex = 0;
124		$content = new htmlResponsiveRow();
125		$loginContent = new htmlResponsiveRow();
126		$loginContent->setCSSClasses(array('maxrow fullwidth roundedShadowBox spacing5'));
127		if ($message !== null) {
128		    $loginContent->add($message, 12);
129        }
130		$pwdInput = new htmlResponsiveInputField(_("Master password"), 'password', '', '236');
131		$pwdInput->setIsPassword(true);
132		$pwdInput->setCSSClasses(array('lam-initial-focus'));
133		$loginContent->add($pwdInput, 12);
134		$loginContent->addLabel(new htmlOutputText('&nbsp;', false));
135		$loginContent->addField(new htmlButton('submitLogin', _("Ok")));
136
137		$content->add($loginContent, 12);
138
139		parseHtml(null, $content, array(), false, $tabindex, null);
140		renderBackLink();
141	}
142
143    /**
144     * Renders the link back to login page.
145     */
146	function renderBackLink() {
147		$tabindex = 0;
148		$content = new htmlResponsiveRow();
149        $content->addVerticalSpacer('2rem');
150        $content->add(new htmlLink(_('Back to login'), '../login.php', '../../graphics/undo.png'), 12);
151		$content->addVerticalSpacer('1rem');
152		parseHtml(null, $content, array(), false, $tabindex, null);
153    }
154
155    /**
156     * Checks the login password.
157     *
158     * @param LAMCfgMain $cfg main config
159     * @return bool login ok
160     */
161	function checkLogin($cfg) {
162        $password = $_POST['password'];
163        if ($cfg->checkPassword($password)) {
164	        $_SESSION["mainconf_password"] = $password;
165            return true;
166        }
167        showLoginDialog(new htmlStatusMessage('ERROR', _('The password is invalid! Please try again.')));
168        return false;
169    }
170
171    /**
172     * Displays the import/export functions.
173     */
174    function displayImportExport() {
175	    $tabindex = 0;
176	    $content = new htmlResponsiveRow();
177
178	    $content->add(new htmlSubTitle(_('Export')), 12);
179	    $content->add(new htmlButton('exportConfig', _('Export')), 12);
180
181	    $content->add(new htmlSubTitle(_('Import')), 12);
182	    renderImportPart($content);
183
184	    parseHtml(null, $content, array(), false, $tabindex, null);
185	    renderBackLink();
186    }
187
188    /**
189     * Renders the import area.
190     *
191     * @param htmlResponsiveRow $content content where to add import part
192     */
193    function renderImportPart($content) {
194        $validUpload = false;
195        $importSteps = array();
196        if (isset($_POST['importConfig'])) {
197	        try {
198	            if (empty($_FILES['import-file']['tmp_name'])) {
199	                throw new LAMException('The file you uploaded is too large. Please check php.ini, upload_max_size setting');
200                }
201		        $handle = fopen($_FILES['import-file']['tmp_name'], "r");
202		        $data = fread($handle, 100000000);
203		        fclose($handle);
204	            $importer = new ConfigDataImporter();
205		        $importSteps = $importer->getPossibleImportSteps($data);
206		        $tmpFile = __DIR__ . '/../../tmp/internal/import_' . getRandomNumber() . '.tmp';
207		        $file = @fopen($tmpFile, "w");
208		        if ($file) {
209			        fputs($file, $data);
210			        fclose($file);
211			        chmod($tmpFile, 0600);
212		        }
213		        $_SESSION['configImportFile'] = $tmpFile;
214	            $validUpload = true;
215            }
216            catch (LAMException $e) {
217                $content->add(new htmlStatusMessage('ERROR', htmlspecialchars($e->getTitle()), htmlspecialchars($e->getMessage())), 12);
218            }
219        }
220        if (!isset($_POST['importConfigConfirm']) && !$validUpload) {
221	        $content->add(new htmlInputFileUpload('import-file'), 12);
222	        $content->add(new htmlButton('importConfig', _('Submit')), 12);
223        }
224        elseif (isset($_POST['importConfig'])) {
225            $content->add(new htmlOutputText(_('Import steps')), 12);
226            foreach ($importSteps as $importStep) {
227                $stepKey = 'step_' . $importStep->getKey();
228                $stepCheckbox = new htmlResponsiveInputCheckbox($stepKey, true, $importStep->getLabel());
229                $stepCheckbox->setLabelAfterCheckbox();
230                $stepCheckbox->setCSSClasses(array('bold'));
231                $subStepIds = array();
232                $content->add($stepCheckbox, 12);
233	            $content->addVerticalSpacer('0.3rem');
234                foreach ($importStep->getSubSteps() as $subStep) {
235                    $subStepKey = 'step_' . $subStep->getKey();
236                    $subStepIds[] = $subStepKey;
237	                $subStepCheckbox = new htmlResponsiveInputCheckbox($subStepKey, true, $subStep->getLabel());
238	                $subStepCheckbox->setLabelAfterCheckbox();
239	                $content->add($subStepCheckbox, 12);
240                }
241                $stepCheckbox->setTableRowsToShow($subStepIds);
242                $content->addVerticalSpacer('1rem');
243            }
244            $buttonGroup = new htmlGroup();
245	        $buttonGroup->addElement(new htmlButton('importConfigConfirm', _('Import')));
246	        $buttonGroup->addElement(new htmlButton('importCancel', _('Cancel')));
247	        $content->add($buttonGroup, 12);
248        }
249        elseif (isset($_POST['importConfigConfirm'])) {
250			$handle = fopen($_SESSION['configImportFile'], "r");
251	        $data = fread($handle, 100000000);
252	        fclose($handle);
253	        try {
254		        $importer = new ConfigDataImporter();
255		        $importSteps = $importer->getPossibleImportSteps($data);
256		        foreach ($importSteps as $importStep) {
257			        $importStep->setActive(isset($_POST['step_' . $importStep->getKey()]));
258			        foreach ($importStep->getSubSteps() as $subStep) {
259				        $subStep->setActive(isset($_POST['step_' . $subStep->getKey()]));
260                    }
261		        }
262		        $importer->runImport($importSteps);
263		        unlink($_SESSION['configImportFile']);
264		        $content->add(new htmlStatusMessage('INFO', _('Configuration import ended successful.')), 12);
265		        $content->add(new htmlButton('importNew', _('New import')), 12);
266	        }
267	        catch (LAMException $e) {
268		        $content->add(new htmlStatusMessage('ERROR', htmlspecialchars($e->getTitle()), htmlspecialchars($e->getMessage())), 12);
269		        $content->add(new htmlButton('importCancel', _('Back')), 12);
270	        }
271        }
272    }
273
274	?>
275    </form>
276	</body>
277</html>
278