<?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 === null ? null : $this->edit->id(),
));
}
}
/**
* 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, 7500);
Taskmanager::release($this->taskId);
$userGroupWarn = false;
$tempfile = Session::get('mod_temp');
if (!isset($status['statusCode'])) {
unlink($tempfile);
$this->tmError();
}
if ($status['statusCode'] != Taskmanager::TASK_FINISHED) {
unlink($tempfile);
$this->taskError($status);
}
$list = SysConfig::archiveContentsFromTask($status, $userGroupWarn);
if ($this->edit !== null) {
$title = $this->edit->title();
} else if (isset($_FILES['modulefile']['name'])) {
$title = basename($_FILES['modulefile']['name']);
} else {
$title = '';
}
Render::addDialog(Dictionary::translateFile('config-module', 'custom_title'), false, 'custom-fileselect', array(
'step' => 'CustomModule_CompressModule',
'files' => $list,
'edit' => $this->edit === null ? null : $this->edit->id(),
'title' => $title,
'userGroupWarn' => $userGroupWarn,
));
}
}
class CustomModule_CompressModule extends AddModule_Base
{
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
$taskId = 'tgzmod' . mt_rand() . '-' . microtime(true);
$destFile = tempnam(sys_get_temp_dir(), 'bwlp-') . '.tgz';
Taskmanager::submit('RecompressArchive', array(
'id' => $taskId,
'inputFiles' => [$tempfile => false],
'outputFile' => $destFile,
'forceRoot' => Request::post('force-owner', 0, 'int') !== 0,
), true);
$status = Taskmanager::waitComplete($taskId, 10000);
unlink($tempfile);
if (!isset($status['statusCode'])) {
$this->tmError();
}
if ($status['statusCode'] != Taskmanager::TASK_FINISHED) {
$this->taskError($status);
}
// Seems ok, create entry
if ($this->edit === null) {
$module = ConfigModule::getInstance('CustomModule');
} else {
$module = $this->edit;
}
$module->setData('tmpFile', $destFile);
if ($this->edit !== null) {
$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 === null, NULL, 200))
Util::redirect('?do=SysConfig&action=addmodule&step=CustomModule_Start');
Session::set('mod_temp', false);
// Yay
if ($this->edit !== null) {
Message::addSuccess('module-edited');
} else {
Message::addSuccess('module-added');
AddModule_Base::setStep('AddModule_Assign', $module->id());
return;
}
Util::redirect('?do=SysConfig');
}
}