summaryrefslogtreecommitdiffstats
path: root/modules-available/sysconfig/page.inc.php
blob: fa04d088be069a90e2da6b08fdd07829216d37dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php

class Page_SysConfig extends Page
{

	/**
	 * Holds all the known configuration modules, with title, description, start class for their wizard, etc.
	 *
	 * @var array
	 */
	protected static $moduleTypes = array();

	/**
	 * @var int current locationid, 0 if global
	 */
	private $currentLoc;

	/**
	 * @var array Associative list of known locations
	 */
	private $locations;

	private $haveOverriddenLocations = false;

	protected function doPreprocess()
	{
		User::load();

		if (!User::isLoggedIn()) {
			Message::addError('main.no-permission');
			Util::redirect('?do=Main');
		}

		// Determine location we're editing
		if (!Module::isAvailable('locations')) {
			$this->locations = array();
			$this->currentLoc = 0;
		} else {
			$this->locations = Location::getLocationsAssoc();
			$this->currentLoc = Request::any('locationid', 0, 'int');
		}
		// Location valid?
		if ($this->currentLoc !== 0 && !isset($this->locations[$this->currentLoc])) {
			Message::addError('locations.invalid-location-id', $this->currentLoc);
			Util::redirect('?do=sysconfig');
		}

		// Action handling

		$action = Request::any('action', 'list');

		// Load all addmodule classes, as they populate the $moduleTypes array
		require_once Page::getModule()->getDir() . '/addmodule.inc.php';
		foreach (glob(Page::getModule()->getDir() . '/addmodule_*.inc.php') as $file) {
			require_once $file;
		}

		// Action: "addmodule" (upload new module)
		if ($action === 'addmodule') {
			User::assertPermission('module.edit');
			$this->initAddModule();
			AddModule_Base::preprocess();
		}

		if ($action === 'module') {
			// Action: "delmodule" (delete module)
			if (Request::post('del', 'no') !== 'no') {
				User::assertPermission('module.edit');
				$this->delModule();
			}
			if (Request::post('download', 'no') !== 'no') {
				User::assertPermission('module.download');
				$this->downloadModule();
			}
			if (Request::post('rebuild', 'no') !== 'no') {
				User::assertPermission('module.edit');
				$this->rebuildModule();
			}
		}

		// Action: "addconfig" (compose config from one or more modules)
		if ($action === 'addconfig') {
			User::assertPermission('config.edit');
			$this->initAddConfig();
			AddConfig_Base::preprocess();
		}

		if ($action === 'config') {
			// Action: "delconfig" (delete config)
			if (Request::post('del', 'no') !== 'no') {
				User::assertPermission('config.edit');
				$this->delConfig();
			}
			// Action "activate" (set sysconfig as active)
			if (Request::post('activate', 'no') !== 'no') {
				User::assertPermission('config.assign', $this->currentLoc);
				$this->activateConfig();
			}
			// Action "rebuild" (rebuild config.tgz from its modules)
			if (Request::post('rebuild', 'no') !== 'no') {
				User::assertPermission('config.edit');
				$this->rebuildConfig();
			}
		}
	}

	/**
	 * Render module; called by main script when this module page should render
	 * its content.
	 */
	protected function doRender()
	{

		Render::addTemplate('sysconfig_heading');

		$action = Request::any('action', 'list');
		switch ($action) {
		case 'addmodule':
			User::assertPermission('module.edit');
			AddModule_Base::render();
			return;
		case 'addconfig':
			User::assertPermission('config.edit');
			AddConfig_Base::render();
			return;
		case 'list':
			$pMods = User::hasPermission('module.view-list');
			$pConfs = User::hasPermission('config.view-list');
			if (!($pMods || $pConfs)) {
				User::assertPermission('config.view-list');
			}
			Render::openTag('div', array('class' => 'row'));
			if ($pConfs) {
				$this->listConfigs();
			}
			if ($this->currentLoc === 0 && $pMods) {
				$this->listModules();
			}
			Render::closeTag('div');
			if ($this->currentLoc === 0) {
				Render::addTemplate('list-legend', array('showLocationBadge' => $this->haveOverriddenLocations));
			}
			Render::addTemplate('js'); // Make this js snippet a template so i18n works
			return;
		case 'module':
			User::assertPermission('module.view-list');
			$listid = Request::post('list');
			if ($listid !== false) {
				$this->listModuleContents($listid);
				return;
			}
			break;
		case 'config':
			User::assertPermission('config.view-list');
			$listid = Request::post('list');
			if ($listid !== false) {
				$this->listConfigContents($listid);
				return;
			}
			break;
		}
		Message::addError('invalid-action', $action, 'main');
	}

	private function getLocationNames($locations, $ids)
	{
		$ret = array();
		foreach ($ids as $id) {
			settype($id, 'int');
			if (isset($locations[$id])) {
				$ret[] = $locations[$id]['locationname'];
			}
		}
		return implode(', ', $ret);
	}

	/**
	 * List all configurations and configuration modules.
	 */
	private function listConfigs()
	{
		// Configs
		$res = Database::simpleQuery("SELECT c.configid, c.title, c.filepath, c.status, c.dateline,
				GROUP_CONCAT(DISTINCT cl.locationid) AS loclist, GROUP_CONCAT(DISTINCT cxm.moduleid) AS modlist
			FROM configtgz c
			LEFT JOIN configtgz_x_module cxm USING (configid)
			LEFT JOIN configtgz_location cl ON (c.configid = cl.configid)
			GROUP BY configid
			ORDER BY title ASC");
		$configs = array();
		if ($this->currentLoc !== 0) {
			$locationName = $this->locations[$this->currentLoc]['locationname'];
		} else {
			$locationName = false;
		}
		$hasDefault = false;
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			if (is_null($row['loclist'])) {
				$locList = array();
			} else {
				$locList = explode(',', $row['loclist']);
			}
			$isDefault = in_array((string)$this->currentLoc, $locList, true);
			$hasDefault |= $isDefault;
			if ($this->currentLoc !== 0) {
				$locCount = 0;
			} else {
				$locCount = count($locList);
				if ($isDefault) {
					$locCount--;
				}
			}
			if ($locCount > 0) {
				$this->haveOverriddenLocations = true;
			}
			$configs[] = array(
				'configid' => $row['configid'],
				'config' => $row['title'],
				'modlist' => $row['modlist'],
				'current' => $isDefault,
				'loclist' => $row['loclist'],
				'readableLocList' => $this->getLocationNames($this->locations, $locList),
				'locationCount' => $locCount,
				'needrebuild' => ($row['status'] !== 'OK'),
				'dateline_s' => Util::prettyTime($row['dateline']),
			);
		}
		$data = array(
			'locationid' => $this->currentLoc,
			'locationname' => $locationName,
			'havelocations' => Module::isAvailable('locations'),
			'configs' => $configs,
			'inheritConfig' => !$hasDefault,
		);
		Permission::addGlobalTags($data['perms'], null, ['config.edit']);
		Permission::addGlobalTags($data['perms'], $this->currentLoc, ['config.assign']);
		Render::addTemplate('list-configs', $data);
	}

	private function listModules()
	{
		// Config modules
		$modules = ConfigModule::getAll();
		$types = array_map(function ($mod) { return $mod->moduleType(); }, $modules);
		$titles = array_map(function ($mod) { return $mod->title(); }, $modules);
		array_multisort($types, SORT_ASC, $titles, SORT_ASC, $modules);
		$data = array(
			'modules' => $modules,
			'havemodules' => (count($modules) > 0)
		);
		Permission::addGlobalTags($data['perms'], null, ['module.edit', 'module.download']);
		Render::addTemplate('list-modules', $data);
	}

	private function listModuleContents($moduleid)
	{
		// fetch the data
		$row = Database::queryFirst("SELECT title, filepath FROM configtgz_module WHERE moduleid = :moduleid LIMIT 1", array('moduleid' => $moduleid));
		if ($row === false) {
			Message::addError('config-invalid', $moduleid);
			Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
		}

		// find files in that archive
		$status = Taskmanager::submit('ListArchive', array(
			'file' => $row['filepath']
		));
		if (isset($status['id']))
			$status = Taskmanager::waitComplete($status, 4000);
		if (!Taskmanager::isFinished($status) || Taskmanager::isFailed($status)) {
			Taskmanager::addErrorMessage($status);
			Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
		}

		// Sort files for better display
		$dirs = array();
		foreach ($status['data']['entries'] as $file) {
			if ($file['isdir'])
				continue;
			$dirs[dirname($file['name'])][] = $file;
		}
		ksort($dirs);
		$list = array();
		foreach ($dirs as $dir => $files) {
			$list[] = array(
				'name' => $dir,
				'isdir' => true
			);
			sort($files);
			foreach ($files as $file) {
				$file['size'] = Util::readableFileSize($file['size']);
				$list[] = $file;
			}
		}

		// render the template
		Render::addDialog(Dictionary::translate('lang_contentOf') . ' ' . $row['title'], false, 'custom-filelist', array(
			'files' => $list,
		));
	}

	private function listConfigContents($configid)
	{
		// get config name
		$config = Database::queryFirst("SELECT title FROM configtgz WHERE configid = :configid LIMIT 1", array('configid' => $configid));
		if ($config === false) {
			Message::addError('config-invalid', $configid);
			Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
		}
		// fetch the data
		$res = Database::simpleQuery("SELECT module.moduleid, module.title AS moduletitle"
			. " FROM configtgz_module module"
			. " INNER JOIN configtgz_x_module USING (moduleid)"
			. " WHERE configtgz_x_module.configid = :configid"
			. " ORDER BY module.title ASC", array('configid' => $configid));

		$modules = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$modules[] = array(
				'module' => $row['moduletitle'],
				'moduleid' => $row['moduleid']
			);
		}

		// render the template
		Render::addDialog(Dictionary::translate('lang_contentOf') . ' ' . $config['title'], false, 'config-module-list', array(
			'modules' => $modules
		));
	}

	private function activateConfig()
	{
		$configid = Request::post('activate', false, 'int');
		if ($configid === false) {
			Message::addError('main.empty-field');
			Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
		}
		// Validate that either the configid is valid (in case we override for a specific location)
		// or that if the locationid is 0 (=global) that the configid exists, because it's not allowed
		// to unset the global config
		if ($this->currentLoc === 0 || $configid !== 0) {
			$row = Database::queryFirst("SELECT title, filepath FROM configtgz WHERE configid = :configid LIMIT 1", array('configid' => $configid));
			if ($row === false) {
				Message::addError('config-invalid', $configid);
				Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
			}
		}
		$locationid = $this->currentLoc;
		if ($configid === 0) {
			Database::exec("DELETE FROM configtgz_location WHERE locationid = :locationid",
				compact('locationid'));
		} else {
			Database::exec("INSERT INTO configtgz_location (locationid, configid) VALUES (:locationid, :configid)"
				. " ON DUPLICATE KEY UPDATE configid = :configid", compact('locationid', 'configid'));
		}
		Event::activeConfigChanged();
		Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
	}

	private function rebuildConfig()
	{
		$configid = Request::post('rebuild', 'MISSING');
		$config = ConfigTgz::get($configid);
		if ($config === false) {
			Message::addError('config-invalid', $configid);
			Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
		}
		$ret = $config->generate(false, 500); // TODO
		if ($ret === true)
			Message::addSuccess('module-rebuilt', $config->title());
		elseif ($ret === false)
			Message::addError('module-rebuild-failed', $config->title());
		else
			Message::addInfo('module-rebuilding', $config->title());
		Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
	}

	private function delModule()
	{
		$moduleid = Request::post('del', 'MISSING');
		$module = Database::queryFirst("SELECT title, filepath FROM configtgz_module WHERE moduleid = :moduleid LIMIT 1", array('moduleid' => $moduleid));
		if ($module === false) {
			Message::addError('config-invalid', $moduleid);
			Util::redirect('?do=sysconfig');
		}
		// Get config.tgz using this module *before* deleting it
		$existing = Database::simpleQuery("SELECT configid FROM configtgz_x_module
			WHERE moduleid = :moduleid", array('moduleid' => $moduleid));
		// Delete DB entries and file
		Database::exec("DELETE FROM configtgz_module WHERE moduleid = :moduleid LIMIT 1", array('moduleid' => $moduleid));
		$task = Taskmanager::submit('DeleteFile', array(
			'file' => $module['filepath']
		));
		if (isset($task['statusCode']) && $task['statusCode'] === Taskmanager::TASK_WAITING) {
			$task = Taskmanager::waitComplete($task['id']);
		}
		if (!isset($task['statusCode']) || $task['statusCode'] === Taskmanager::TASK_ERROR) {
			Message::addWarning('main.task-error', $task['data']['error']);
		} elseif ($task['statusCode'] === Taskmanager::TASK_FINISHED) {
			Message::addSuccess('module-deleted', $module['title']);
		}
		// Rebuild depending config.tgz
		while ($crow = $existing->fetch(PDO::FETCH_ASSOC)) {
			$config = ConfigTgz::get($crow['configid']);
			if ($config !== false) {
				$config->generate();
			}
		}
		Util::redirect('?do=sysconfig');
	}

	private function downloadModule()
	{
		$moduleid = Request::post('download', 'MISSING');
		$row = Database::queryFirst("SELECT title, filepath FROM configtgz_module WHERE moduleid = :moduleid LIMIT 1", array('moduleid' => $moduleid));
		if ($row === false) {
			Message::addError('config-invalid', $moduleid);
			Util::redirect('?do=sysconfig');
		}
		if (!Util::sendFile($row['filepath'], $row['title'] . '.tgz'))
			Util::redirect('?do=sysconfig');
		exit(0);
	}

	private function rebuildModule()
	{
		$moduleid = Request::post('rebuild', 'MISSING');
		$module = ConfigModule::get($moduleid);
		if ($module === false) {
			Message::addError('config-invalid', $moduleid);
			Util::redirect('?do=sysconfig');
		}
		$ret = $module->generate(false, 250);
		if ($ret === true)
			Message::addSuccess('module-rebuilt', $module->title());
		elseif ($ret === false)
			Message::addError('module-rebuild-failed', $module->title());
		else
			Message::addInfo('module-rebuilding', $module->title());
		Util::redirect('?do=sysconfig');
	}

	private function delConfig()
	{
		$configid = Request::post('del', 'MISSING');
		$config = ConfigTgz::get($configid);
		if ($config === false) {
			Message::addError('config-invalid', $configid);
			Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
		}
		if ($config->delete() === false) {
			Message::addError('config-delete-error', Database::lastError());
		} else {
			Message::addSuccess('config-deleted', $config->title());
		}
		Util::redirect('?do=sysconfig&locationid=' . $this->currentLoc);
	}

	private function initAddModule()
	{
		ConfigModule::loadDb();
		require_once Page::getModule()->getDir() . '/addmodule.inc.php';
		$step = Request::any('step', 'AddModule_Start', 'string');
		if (!class_exists($step) && preg_match('/^([a-zA-Z0-9]+)_/', $step, $out)) {
			require_once Page::getModule()->getDir() . '/addmodule_' . strtolower($out[1]) . '.inc.php';
		}
		AddModule_Base::setStep($step);
	}

	private function initAddConfig()
	{
		ConfigModule::loadDb();
		require_once Page::getModule()->getDir() . '/addconfig.inc.php';
		$step = Request::any('step', 0);
		if ($step === 0)
			$step = 'AddConfig_Start';
		AddConfig_Base::setStep($step);
	}

	/**
	 * If modules need updates (blue refresh buttons), we query their state
	 * via ajax, in case they are about to generate. This happens for example
	 * if you edit a module and a bunch of configs depend on it and will be
	 * rebuilt.
	 */
	protected function doAjax()
	{
		if (Request::post('action') === 'status') {
			$mods = Request::post('mods');
			$confs = Request::post('confs');
			$outMods = array();
			$outConfs = array();
			$mods = explode(',', $mods);
			$confs = explode(',', $confs);
			// Mods
			$res = Database::simpleQuery("SELECT moduleid FROM configtgz_module
					WHERE moduleid in (:mods) AND status = 'OK'", compact('mods'));
			while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
				$outMods[] = $row['moduleid'];
			}
			// Confs
			$res = Database::simpleQuery("SELECT configid FROM configtgz
					WHERE configid in (:confs) AND status = 'OK'", compact('confs'));
			while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
				$outConfs[] = $row['configid'];
			}
			Header('Content-Type: application/json');
			die(json_encode(array('mods' => $outMods, 'confs' => $outConfs)));
		}
	}

}