-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgha_queue.php
136 lines (109 loc) · 3.63 KB
/
gha_queue.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
<?php
/***
* This file allows provides a queue system for artifacts.
* Artifact names given to this system must be unique within the artifacts list, having unique Build IDs.
*
***/
require_once("config.php");
define("CI_SNAPSHOTS", true);
require_once('lib/functions.common.php');
require_once('lib/functions.http.php');
header('Content-Type: text/plain');
if ((!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) && GHA_REQUIRES_AUTH) {
ExitUnauthorized();
}
// bootstrap web-app.
require_once('lib/init.php');
// test given auth credentials.
if (GHA_REQUIRES_AUTH) {
if (!CheckUserCredentials($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) {
ExitForbidden();
}
}
if (getSnapshotDirectorySize() >= MAX_CAPACITY_BYTES && MAX_CAPACITY_DELETE_OLDEST == false) {
ExitFullStorage();
}
$artifact_name = null;
if ( isset($_REQUEST['artifact_name']) && !empty($_REQUEST['artifact_name']) ) {
if ( false == preg_match('/^[a-z0-9-_\.]+$/iu', $_REQUEST['artifact_name']) ) {
ExitClientError('Invalid artifact name');
} else {
$artifact_name = trim($_REQUEST['artifact_name']);
}
} else {
ExitClientError('Missing required parameter artifact_name');
}
$unzip = false;
if ( isset($_REQUEST['unzip']) ) {
$unzip = filter_var($_REQUEST['unzip'], FILTER_VALIDATE_BOOLEAN);
} else {
ExitClientError('Missing required parameter unzip');
}
$artifact_queue_file = $ScriptPath . DIRECTORY_SEPARATOR . TEMP_DIR . GHA_QFILE_PREFIX . $artifact_name . GHA_QFILE_EXT ;
$gha_list_url = GHA_LIST_URL . '?per_page=100&page=1';
if ( file_exists($artifact_queue_file) ) {
ExitFailedRequest('Artifact is already queued');
}
$list_req = do_curl_get($gha_list_url, array('Authorization: token ' . GHA_AUTH_TOKEN));
if ( $list_req === false ) {
ExitFailedRequest('Error checking artifact list');
}
if ( $list_req[2] !== 200 ) {
ExitFailedRequest('Failed to fetch artifact list - ' . strval($list_req[2]) );
}
$list_data = json_decode($list_req[0], true);
if ( ! isset($list_data['artifacts']) ) {
ExitFailedRequest('Failed to find artifacts');
}
$artifact_is_unique = true;
$artifact_id = null;
if ( !empty($list_data['artifacts']) ) {
// check existing artifacts for 0 or 1 instance of the given name.
// hopefully we can get away with just checking the first 100 entries.
// it seems to list the latest fisrt anyways...
$found=0;
foreach( $list_data['artifacts'] as $idx => $arti ) {
if ( $artifact_name == $arti['name'] ) {
$found = $found + 1;
$artifact_id = $arti['id'];
}
if ( $found > 1 ) {
$artifact_is_unique = false;
}
}
}
if (!$artifact_is_unique) {
ExitClientError('Artifact is not unique');
}
$maxdays = 0;
if (isset($_SERVER['HTTP_MAX_DAYS'])) {
if (preg_match('/^[0-9]+$/i', $_SERVER['HTTP_MAX_DAYS']) === 1) {
$maxdays = intval($_SERVER['HTTP_MAX_DAYS']);
}
}
$maxdls = 0;
if (isset($_SERVER['HTTP_MAX_DOWNLOADS'])) {
if (preg_match('/^[0-9]+$/i', $_SERVER['HTTP_MAX_DOWNLOADS']) === 1) {
$maxdls = intval($_SERVER['HTTP_MAX_DOWNLOADS']);
}
}
$artifact_data = array(
'name' => $artifact_name,
'unzip' => $unzip,
'qtime' => time()
);
if ($artifact_id !== null) {
$artifact_data['id'] = $artifact_id;
}
if ($maxdays != 0) {
$artifact_data['maxdays'] = $maxdays;
}
if ($maxdls != 0) {
$artifact_data['maxdls'] = $maxdls;
}
$queue_json = json_encode($artifact_data);
if ( false !== file_put_contents($artifact_queue_file, $queue_json, LOCK_EX) ) {
echo('Artifact enqueued');
} else {
ExitFailedRequest('Failed to enqueue artifact');
}