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

/*
 * Wizard for adding a custom module. A custom module is a plain archive that gets
 * included into a config.tgz the way itz is. No handling, sanity checks or anything
 * fancy is happening.
 */

class CustomModule_Start extends AddModule_Base
{

	protected function renderInternal()
	{
		Session::set('mod_temp', false);
		Render::addDialog(Dictionary::translateFile('config-module', 'custom_title'), false, 'custom-upload', array(
			'step' => 'CustomModule_ProcessUpload',
			'edit' => $this->edit ? $this->edit->id() : false
			));
	}

}

/**
 * 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 CustomModule_ProcessUpload extends AddModule_Base
{
	
	private $taskId = false;

	protected function preprocessInternal()
	{
		if (!isset($_FILES['modulefile'])) {
			Message::addError('missing-file');
			Util::redirect('?do=SysConfig');
		}
		if ($_FILES['modulefile']['error'] != UPLOAD_ERR_OK) {
			Message::addError('upload-failed', Util::uploadErrorString($_FILES['modulefile']['error']));
			Util::redirect('?do=SysConfig');
		}
		$tempfile = '/tmp/bwlp-' . mt_rand(1, 100000) . '-' . crc32($_SERVER['REMOTE_ADDR']) . '.tmp';
		if (!move_uploaded_file($_FILES['modulefile']['tmp_name'], $tempfile)) {
			Message::addError('main.error-write', $tempfile);
			Util::redirect('?do=SysConfig');
		}
		$this->taskId = 'tgzmod' . mt_rand() . '-' . microtime(true);
		Taskmanager::submit('ListArchive', array(
			'id' => $this->taskId,
			'file' => $tempfile
		), true);
		Session::set('mod_temp', $tempfile);
	}
	
	protected function renderInternal()
	{
		$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;
			}
		}
		if ($this->edit !== false)
			$title = $this->edit->title();
		elseif (isset($_FILES['modulefile']['name']))
			$title = basename($_FILES['modulefile']['name']);
		else
			$title = '';
		Render::addDialog(Dictionary::translate('config-module', 'custom_title'), false, 'custom-fileselect', array(
			'step' => 'CustomModule_CompressModule',
			'files' => $list,
			'edit' => $this->edit ? $this->edit->id() : false,
			'title' => $title
		));
		Session::save();
	}

}

class CustomModule_CompressModule extends AddModule_Base
{
	
	private $taskId = false;
	
	protected function preprocessInternal()
	{
		$title = Request::post('title');
		$tempfile = Session::get('mod_temp');
		if (empty($title) || empty($tempfile) || !file_exists($tempfile)) {
			Message::addError('main.empty-field');
			Util::redirect('?do=SysConfig&action=addmodule&step=CustomModule_Start');
		}
		// Recompress using task manager
		$this->taskId = 'tgzmod' . mt_rand() . '-' . microtime(true);
		$destFile = tempnam(sys_get_temp_dir(), 'bwlp-') . '.tgz';
		Taskmanager::submit('RecompressArchive', array(
			'id' => $this->taskId,
			'inputFiles' => array($tempfile),
			'outputFile' => $destFile
		), true);
		$status = Taskmanager::waitComplete($this->taskId, 5000);
		unlink($tempfile);
		if (!isset($status['statusCode'])) {
			$this->tmError();
		}
		if ($status['statusCode'] != TASK_FINISHED) {
			$this->taskError($status);
		}
		// Seems ok, create entry
		if ($this->edit === false)
			$module = ConfigModule::getInstance('CustomModule');
		else
			$module = $this->edit;
		if ($module === false) {
			Message::addError('main.error-read', 'custommodule.inc.php');
			Util::redirect('?do=SysConfig&action=addmodule&step=CustomModule_Start');
		}
		$module->setData('tmpFile', $destFile);
		if ($this->edit !== false)
			$ret = $module->update($title);
		else
			$ret = $module->insert($title);
		if (!$ret)
			Util::redirect('?do=SysConfig&action=addmodule&step=CustomModule_Start');
		elseif (!$module->generate($this->edit === false, NULL, 200))
			Util::redirect('?do=SysConfig&action=addmodule&step=CustomModule_Start');
		Session::set('mod_temp', false);
		Session::save();
		// Yay
		if ($this->edit !== false)
			Message::addSuccess('module-edited');
		else
			Message::addSuccess('module-added');
		Util::redirect('?do=SysConfig');
	}
	
}