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
|
<?php
class Page_MiniLinux extends Page
{
protected function doPreprocess()
{
User::load();
if (!User::hasPermission('superadmin')) {
Message::addError('no-permission');
Util::redirect('?do=Main');
}
}
protected function doRender()
{
Render::addTemplate('page-minilinux', array(
'listurl' => '?do=MiniLinux&async=true&action=list'
));
Render::addFooter('<script> $(window).load(function (e) {
loadSystemList(0);
}); // </script>');
}
protected function doAjax()
{
$data = Property::getVersionCheckInformation();
if (!is_array($data) || !isset($data['systems'])) {
echo Render::parse('messagebox-error', array(
'message' => 'Failed to retrieve the list: ' . print_r($data, true)
));
return;
}
$action = Request::any('action');
$selectedVersion = (int)Request::any('version', 0);
switch ($action) {
case 'list':
$count = 0;
foreach ($data['systems'] as &$system) {
// Get latest version, build simple array of all version numbers
$versionNumbers = array();
$selected = false;
foreach ($system['versions'] as $version) {
if (!is_numeric($version['version']) || $version['version'] < 1)
continue;
if ($selectedVersion === 0 && ($selected === false || $selected['version'] < $version['version']))
$selected = $version;
elseif ($version['version'] == $selectedVersion)
$selected = $version;
$versionNumbers[(int)$version['version']] = array(
'version' => $version['version']
);
}
if ($selected === false) continue; // No versions for this system!?
ksort($versionNumbers);
// Mark latest version as selected
$versionNumbers[(int)$selected['version']]['selected'] = true;
// Add status information to system and its files
foreach ($selected['files'] as &$file) {
$file['uid'] = 'dlid' . $count++;
$local = CONFIG_HTTP_DIR . '/' . $system['id'] . '/' . $file['name'];
if (!file_exists($local) || filesize($local) !== $file['size'] || filemtime($local) < $file['mtime']) {
$file['fileChanged'] = true;
$system['systemChanged'] = true;
}
$taskId = Property::getDownloadTask($file['md5']);
if ($taskId !== false) {
$task = Taskmanager::status($taskId);
if (isset($task['data']['progress'])) {
$file['download'] = Render::parse('minilinux/download', array(
'task' => $taskId,
'name' => $file['name']
));
}
}
}
unset($system['versions']);
$system['files'] = $selected['files'];
$system['version'] = $selected['version'];
}
$data['versions'] = array_values($versionNumbers);
echo Render::parse('minilinux/filelist', $data);
return;
case 'download':
$id = Request::post('id');
$name = Request::post('name');
if (!$id || !$name || strpos("$id$name", '/') !== false) {
echo "Invalid download request";
return;
}
$file = false;
$gpg = 'missing';
foreach ($data['systems'] as &$system) {
if ($system['id'] !== $id) continue;
foreach ($system['versions'] as &$version) {
if ($version['version'] != $selectedVersion) continue;
foreach ($version['files'] as &$f) {
if ($f['name'] !== $name) continue;
$file = $f;
if (!empty($f['gpg'])) $gpg = $f['gpg'];
break;
}
}
}
if ($file === false) {
echo "Nonexistent system/file: $id / $name";
return;
}
$task = Taskmanager::submit('DownloadFile', array(
'url' => CONFIG_REMOTE_ML . '/' . $id . '/' . $selectedVersion . '/' . $name,
'destination' => CONFIG_HTTP_DIR . '/' . $id . '/' . $name,
'gpg' => $gpg
));
if (!isset($task['id'])) {
echo 'Error launching download task: ' . $task['statusCode'];
return;
}
Property::setDownloadTask($file['md5'], $task['id']);
echo Render::parse('minilinux/download', array(
'name' => $name,
'task' => $task['id']
));
return;
}
}
}
|