summaryrefslogblamecommitdiffstats
path: root/modules-available/sysconfig/addmodule_loginscreen.inc.php
blob: 5b4305f784c2d9507870b9719665bd5a7240ff01 (plain) (tree)













































































                                                                                                                                  
                                                                                            































































                                                                                                          
<?php

/*
 * Wizard for configuring login screen (lightdm greeter)
 */

const SCRN_SESSION_DATA = 'lgnscrndata';

const FIELDS = [
	'clock-text-style' => ['default' => 'border:none; color:#fff; font-size:14pt; qproperty-alignment:AlignRight'],
	'clock-background-style' => ['default' => 'border:none; background:#888687'],
	'clock-shadow' => ['default' => '1 1 #555 1', 'regex' => '\s*([0-9-]+(\s+[0-9-]+(\s+[#0-9a-zA-Z]+(\s+[0-9-]+)?)?)?)?\s*'],
	'username-placeholder' => ['default' => 'user id'],
	'password-placeholder' => ['default' => 'password'],
	'shib-session-button-text' => ['default' => 'Shibboleth-Login'],
	'qr-session-button-text' => ['default' => 'QR-Code Login'],
	'user-session-button-text' => ['default' => 'Campus-Login'],
	'guest-session-button-text' => ['default' => 'Gastsitzung'],
	'guest-session-start-text' => ['default' => 'Gast-Sitzungen sind möglicherweise auf einzelne' .
		' (z.B. ausschließlich interne) Webseiten beschränkt.'],
	'guest-session-start-button-text' => ['default' => 'Starten einer Gastsitzung'],
	'reset-timeout' => ['default' => 30, 'regex' => '[0-9]*'],
];

/* For translation scanner
		Dictionary::translate('ls_clock-text-style'),
		Dictionary::translate('ls_clock-background-style'),
		Dictionary::translate('ls_clock-shadow'),
		Dictionary::translate('ls_username-placeholder'),
		Dictionary::translate('ls_password-placeholder'),
		Dictionary::translate('ls_shib-session-button-text'),
		Dictionary::translate('ls_qr-session-button-text'),
		Dictionary::translate('ls_user-session-button-text'),
		Dictionary::translate('ls_guest-session-button-text'),
		Dictionary::translate('ls_guest-session-start-text'),
		Dictionary::translate('ls_guest-session-start-button-text'),
		Dictionary::translate('ls_reset-timeout'),
*/

class LoginScreen_Start extends AddModule_Base
{

	protected function renderInternal()
	{
		/* Load or initialise session data */
		if (Request::get('back', 'false', 'string') !== 'false') {
			/* If coming via the back button, load the session data */
			$session_data = Session::get(SCRN_SESSION_DATA);
			if (!is_array($session_data)) {
				$session_data = [];
			}
		} elseif ($this->edit !== null) {
			$session_data = $this->edit->getData(null);
			Session::set(SCRN_SESSION_DATA, $session_data);
		} else {
			$session_data = [];
			Session::set(SCRN_SESSION_DATA, false);
		}
		if (empty($session_data)) {
			$session_data = [];
			foreach (FIELDS as $key => $value) {
				$session_data[$key] = $value['default'];
			}
		}
		$data = [];
		foreach (FIELDS as $key => $opts) {
			$data[] = [
				'field' => $key,
				'value' => $session_data[$key] ?? '',
				'caption' => Dictionary::translate('ls_' . $key),
				'regex' => $opts['regex'] ?? null,
			];
		}
		Render::addDialog(Dictionary::translateFile('config-module', 'loginscreen_title'),
				false, 'loginscreen-start', [
				'next' => 'LoginScreen_Finish',
				'edit' => $this->edit !== null ? $this->edit->id() : 0,
				'fields' => $data,
				'title' => $this->edit !== null ? $this->edit->title() : '',
			]);
	}

}

class LoginScreen_Finish extends AddModule_Base
{

	protected function preprocessInternal()
	{
		$title = trim(Request::post('title', '', 'string'));
		if (empty($title)) {
			Util::redirect('?do=sysconfig&action=addmodule&step=LoginScreen_Start&back=true');
		}
		$session_data = Session::get(SCRN_SESSION_DATA);
		if (!is_array($session_data)) {
			$session_data = [];
		}
		$session_data['title'] = $title;

		/* Only create an instance if it's a new one */
		if ($this->edit !== null) {
			$module = $this->edit;
		} else {
			$module = ConfigModule::getInstance('LoginScreen');
		}

		$err = false;
		foreach (FIELDS as $key => $field) {
			$val = Request::post($key, '', 'string');
			if (isset($field['regex']) && !preg_match("/{$field['regex']}/", $val)) {
				Message::addError('regex-mismatch', $val, $field['regex']);
				$err = true;
			}
			$session_data[$key] = $val;
			$module->setData($key, $val);
		}
		if ($err) {
			Session::set(SCRN_SESSION_DATA, $session_data);
			Util::redirect('?do=sysconfig&action=addmodule&step=LoginScreen_Start&back=true');
		}

		/* Insert or update database entries */
		if ($this->edit !== null) {
			$module->update($session_data['title']);
		} else {
			$module->insert($session_data['title']);
		}

		$task = $module->generate($this->edit === null);

		// Yay
		Session::set(SCRN_SESSION_DATA, false);
		if ($task !== false && $this->edit !== null) {
			Message::addSuccess('module-edited');
		} elseif ($task !== false) {
			Message::addSuccess('module-added');
			AddModule_Base::setStep('AddModule_Assign', $module->id());
			return;
		}
		Util::redirect('?do=SysConfig');
	}

}