-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudipDownloader.php
146 lines (134 loc) · 5.65 KB
/
StudipDownloader.php
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
<?php
class StudipDownloader extends StudipPlugin implements SystemPlugin
{
//private $svnurl = 'svn://develop.studip.de/studip';
//private $svncmd = 'svn --no-auth-cache --username studip --password studip ';
private $svnurl = 'file:///home/svn/studip';
private $svncmd = '/usr/bin/svn';
public function __construct()
{
parent::__construct();
$nav = new Navigation(_("Downloads"), PluginEngine::getURL($this, array(), "downloadarea/overview"));
$nav->setImage(Icon::create("download", "navigation"));
Navigation::addItem("/downloader", $nav);
}
function perform($unconsumed_path) {
try {
parent::perform($unconsumed_path);
} catch (Exception $exception) {
if ($exception instanceOf Trails_Exception) {
$status = $exception->getCode();
} else {
$status = 500;
}
header('HTTP/1.1 ' . $status . ' ' . $exception->getMessage());
$this->render_json(array('status' => (int)$status, 'message' => $exception->getMessage()));
}
}
function releases_action()
{
$releases = $this->get_svn_list('/tags');
if ($releases) {
$this->render_json(array_values($releases->orderBy('name desc')->toArray()));
} else {
throw new Trails_Exception(500);
}
}
function branches_action()
{
$releases = $this->get_svn_list('/branches');
if ($releases) {
$this->render_json(array_values($releases->orderBy('name desc')->toArray()));
} else {
throw new Trails_Exception(500);
}
}
function download_action($what = 'stable')
{
if ($what == 'stable') {
$releases = $this->get_svn_list('/tags');
$releases->orderBy('name desc');
$download = '/tags/' . $releases->val('name');
$filename = 'studip-release-' . $releases->val('name');
} elseif ($what == 'latest-stable') {
$releases = $this->get_svn_list('/tags');
$releases->orderBy('name desc');
$download = '/branches/' . $releases->val('name');
$branches = $this->get_svn_list('/branches');
$filename = 'studip-' . $releases->val('name') . '-r' . $branches->findOneBy('name', $releases->val('name'))->rev;
} elseif ($what == 'beta') {
$branches = $this->get_svn_list('/branches');
$branches->orderBy('name desc');
$download = '/branches/' . $branches->val('name');
$filename = 'studip-' . $branches->val('name') . '-r' . $branches->val('rev');
} elseif (strpos($what, 'stable') === 0) {
list(,$release) = explode('-', $what);
$releases = $this->get_svn_list('/tags');
$stable = $releases->findBy('name', $release, '^=')->orderBy('name desc')->val('name');
if ($stable) {
$download = '/tags/' . $stable;
$filename = 'studip-release-' . $stable;
}
} elseif (strpos($what, 'latest') === 0) {
list(,$branch) = explode('-', $what);
$branches = $this->get_svn_list('/branches');
if ($branches->findOneBy('name', $branch)) {
$download = '/branches/' . $branch;
$filename = 'studip-' . $branch . '-r' . $branches->findOneBy('name', $branch)->rev;
}
} else {
$releases = $this->get_svn_list('/tags');
if ($releases->findOneBy('name', $what)) {
$download = '/tags/' . $what;
$filename = 'studip-release-' . $what;
}
}
if ($download) {
$id = $this->prepare_download($download, $filename . '.zip');
header("Expires: Mon, 12 Dec 2001 08:00:00 GMT");
header("Last-Modified: " . gmdate ("D, d M Y H:i:s") . " GMT");
header("Pragma: public");
header("Cache-Control: private");
header("Content-Type: application/zip");
header("Content-Length: " . filesize($GLOBALS['TMP_PATH'] . '/' . $id . '.zip'));
header("Content-Disposition: attachment; filename=\"$filename.zip\"");
readfile($GLOBALS['TMP_PATH'] . '/' . $id . '.zip');
} else {
throw new Trails_Exception(400);
}
}
function prepare_download($path, $filename)
{
$id = md5($filename);
if (!file_exists($GLOBALS['TMP_PATH'] . '/' . $id . '.zip')) {
@rmdirr($GLOBALS['TMP_PATH'] . '/' . $id);
exec($this->svncmd . ' --force export ' . $this->svnurl . $path . ' ' . $GLOBALS['TMP_PATH'] . '/' . $id, $out, $ret);
create_zip_from_directory($GLOBALS['TMP_PATH'] . '/' . $id, $GLOBALS['TMP_PATH'] . '/' . $id . '.zip');
@rmdirr($GLOBALS['TMP_PATH'] . '/' . $id);
return $id;
} else {
return $id;
}
}
function get_svn_list($path)
{
$out = array();
exec($this->svncmd . ' list --xml ' . $this->svnurl . $path, $out, $ret);
$list = new SimpleCollection();
if ($ret === 0) {
$taglist = simplexml_load_string(join("", $out));
foreach ($taglist->xpath('//entry') as $entry) {
$name = (string)$entry->name;
$date = (string)$entry->commit->date;
$rev = (string)$entry->commit['revision'];
$list[] = compact('name', 'date', 'rev');
}
return $list;
}
}
function render_json($data)
{
header('Content-Type: application/json;charset=utf-8');
echo json_encode(studip_utf8encode($data));
}
}