summaryrefslogtreecommitdiffstats
path: root/modules/sysconfig.inc.php
blob: f177f0f10851b3f398b32cfc1bc9f4b5982fbeba (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
<?php

class Page_SysConfig extends Page
{

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

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

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

		// Action "activate" (set sysconfig as active)
		if ($action === 'activate') {
			if (!User::hasPermission('superadmin')) {
				Message::addError('no-permission');
				Util::redirect('?do=SysConfig');
			}
			if (!isset($_REQUEST['file'])) {
				Message::addError('missing-file');
				Util::redirect('?do=SysConfig');
			}
			$file = preg_replace('/[^a-z0-9\-_\.]/', '', $_REQUEST['file']);
			$path = CONFIG_TGZ_LIST_DIR . '/' . $file;
			if (!file_exists($path)) {
				Message::addError('invalid-file', $file);
				Util::redirect('?do=SysConfig');
			}
			mkdir(CONFIG_HTTP_DIR . '/default', 0755, true);
			$linkname = CONFIG_HTTP_DIR . '/default/config.tgz';
			@unlink($linkname);
			if (file_exists($linkname)) Util::traceError('Could not delete old config.tgz link!');
			if (!symlink($path, $linkname)) Util::traceError("Could not symlink to $path at $linkname!");
			Message::addSuccess('config-activated');
			Util::redirect('?do=SysConfig');
		}
	}

	/**
	 * Render module; called by main script when this module page should render
	 * its content.
	 */
	protected function doRender()
	{
		$action = Request::any('action', 'list');
		switch ($action) {
		case 'addmodule':
			AddModule_Base::render();
			break;
		case 'list':
			$this->rr_list_configs();
			break;
		default:
			Message::addError('invalid-action', $action);
			break;
		}
	}
	
	protected function doAjax()
	{
		$action = Request::any('action', 'list');

		// Action: "addmodule" (upload new module)
		if ($action === 'addmodule') {
			$this->initAddModule();
			AddModule_Base::ajax();
		}
	}

	private function rr_list_configs()
	{
		if (!User::hasPermission('superadmin')) {
			Message::addError('no-permission');
			return;
		}
		$res = Database::simpleQuery("SELECT title FROM configtgz_module ORDER BY title ASC");
		$modules = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$modules[] = array(
				'module' => $row['title']
			);
		}
		Render::addTemplate('page-sysconfig-main', array('modules' => $modules, 'token' => Session::get('token')));
	}
	
	private function initAddModule()
	{
		$step = Request::any('step', 0);
		if ($step === 0) $step = 'AddModule_Start';
		require_once 'modules/sysconfig/addmodule.inc.php';
		foreach (glob('modules/sysconfig/addmodule_*.inc.php') as $file) {
			require_once $file;
		}
		AddModule_Base::setStep($step);
	}

}