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

/**
 * Addmodule subpage base - makes sure
 * we have the two required methods preprocess and render
 */
abstract class AddModule_Base
{

	/**
	 * 
	 * @param type $step
	 * @return \AddModule_Base
	 */
	public static function get($step)
	{
		switch ($step) {
			case 0: // Upload form
				return new AddModule_UploadForm();
			case 1: // Handle config module uploading
				return new AddModule_ProcessUpload();
			case 2: // ?
				return new AddModule_CompressModule();
		}
		Message::addError('invalid-action', $step);
		Util::redirect('?do=sysconfig');
	}
	
	protected function tmError()
	{
		Message::addError('taskmanager-error');
		Util::redirect('?do=sysconfig');
	}
	
	protected function taskError($status)
	{
		if (isset($status['data']['error'])) {
			$error = $status['data']['error'];
		} elseif (isset($status['statusCode'])) {
			$error = $status['statusCode'];
		} else {
			$error = 'Unbekannter Taskmanager-Fehler'; // TODO: No text
		}
		Message::addError('task-error', $error);
		Util::redirect('?do=sysconfig');
	}

	/**
	 * Called before any HTML rendering happens, so you can
	 * pepare stuff, validate input, and optionally redirect
	 * early if something is wrong, or you received post
	 * data etc.
	 */
	public function preprocess()
	{
		// void
	}

	/**
	 * Do page rendering
	 */
	public function render()
	{
		// void
	}

}

class AddModule_UploadForm extends AddModule_Base
{

	public function render()
	{
		global $nextStep;
		Session::set('mod_temp', false);
		Render::addDialog('Eigenes Modul hinzufügen', false, 'sysconfig/custom-upload', array('step' => $nextStep));
	}

}

/**
 * Some file has just been uploaded. Try to store it, then try to unpack/analyze it.
 * If this succeeds, we proceed to the next step where we present the user our findings
 * and ask what to do with this.
 */
class AddModule_ProcessUpload extends AddModule_Base
{
	
	private $taskId = false;

	public function preprocess()
	{
		if (!isset($_FILES['modulefile'])) {
			Message::addError('missing-file');
			return;
		}
		if ($_FILES['modulefile']['error'] != UPLOAD_ERR_OK) {
			Message::addError('upload-failed', $_FILE['modulefile']['name']);
			return;
		}
		$tempfile = $_FILES['modulefile']['tmp_name'] . '.tmp';
		if (!move_uploaded_file($_FILES['modulefile']['tmp_name'], $tempfile)) {
			Message:addError('error-write', $tempfile);
			return;
		}
		$this->taskId = 'tgzmod' . mt_rand() . '-' . microtime(true);
		Taskmanager::submit('ListArchive', array(
			'id' => $this->taskId,
			'file' => $tempfile
		), true);
		Session::set('mod_temp', $tempfile);
	}
	
	public function render()
	{
		$status = Taskmanager::waitComplete($this->taskId);
		Taskmanager::release($this->taskId);
		$tempfile = Session::get('mod_temp');
		if (!isset($status['statusCode'])) {
			unlink($tempfile);
			$this->tmError();
		}
		if ($status['statusCode'] != TASK_FINISHED) {
			unlink($tempfile);
			$this->taskError($status);
		}
		// 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;
			}
		}
		global $nextStep;
		Render::addDialog('Eigenes Modul hinzufügen', false, 'sysconfig/custom-fileselect', array(
			'step' => $nextStep,
			'files' => $list,
		));
		Session::save();
	}

}

class AddModule_CompressModule extends AddModule_Base
{
	
	private $taskId = false;
	
	public function preprocess()
	{
		$title = Request::post('title');
		$tempfile = Session::get('mod_temp');
		if (empty($title) || empty($tempfile) || !file_exists($tempfile)) {
			Message::addError('empty-field');
			return;
		}
		// Recompress using task manager
		$this->taskId = 'tgzmod' . mt_rand() . '-' . microtime(true);
		$destFile = CONFIG_TGZ_LIST_DIR . '/modules/mod-' . preg_replace('/[^a-z0-9_\-]+/is', '_', $title) . '-' . microtime(true) . '.tgz';
		Taskmanager::submit('RecompressArchive', array(
			'id' => $this->taskId,
			'inputFiles' => array($tempfile),
			'outputFile' => $destFile
		), true);
		$status = Taskmanager::waitComplete($this->taskId);
		unlink($tempfile);
		if (!isset($status['statusCode'])) {
			$this->tmError();
		}
		if ($status['statusCode'] != TASK_FINISHED) {
			$this->taskError($status);
		}
		// Seems ok, create entry in DB
		$ret = Database::exec("INSERT INTO configtgz_module (title, moduletype, filename, contents) VALUES (:title, 'custom', :file, '')",
			array('title' => $title, 'file' => $destFile));
		if ($ret === false) {
			unlink($destFile);
			Util::traceError("Could not insert module into Database");
		}
		Message::addSuccess('module-added');
		Util::redirect('?do=sysconfig');
	}
	
}