-
Notifications
You must be signed in to change notification settings - Fork 12
/
sell.php
178 lines (137 loc) · 5.21 KB
/
sell.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
set_time_limit(0);
ignore_user_abort(true);
require_once __DIR__.'/phplibs/s3er.php';
require_once __DIR__.'/phplibs/zipper.php';
require_once __DIR__.'/phplibs/db.php';
require_once __DIR__.'/phplibs/utils.php';
require_once __DIR__.'/phplibs/ga.php';
Utils::options_headers();
Utils::no_cache_headers();
function echo_json_and_close_connection($data, $exit = false) {
ob_end_clean();
header('Content-Type: text/plain; charset=utf-8');
header('Cache-control: no-cache');
header('Connection: close');
header('Content-Encoding: none');
ob_start();
echo json_encode($data);
header('Content-Length: '.ob_get_length());
ob_end_flush();
flush();
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
if ($exit) {
exit(0);
}
}
$clean_files = array();
foreach ($_FILES as $file) {
if ($file['error'] == 0) {
$clean_files[] = $file;
}
}
if (empty($clean_files) && !isset($_REQUEST['update_url'])) {
echo_json_and_close_connection(array('error' => 'Upload failed for all files', 'files' => $_FILES), true);
} else {
$metadata = array('price' => isset($_REQUEST['price']) ? (float)$_REQUEST['price'] : 0.1,
'address' => isset($_REQUEST['address']) ? trim($_REQUEST['address']) : '',
'currency' => isset($_REQUEST['currency']) ? trim($_REQUEST['currency']) : 'BTC',
);
if (strlen($metadata['currency']) == 3) {
$metadata['currency'] = strtoupper($metadata['currency']);
}
$id = false;
$updateid = false;
$download_filename = '';
$files = array();
if (isset($_REQUEST['update_url'])) {
$lookup_id = array_pop(explode('/', trim($_REQUEST['update_url'])));
$results = DB::statement('SELECT id, download_filename, price_float AS price, currency, address FROM download WHERE updateid = :updateid AND status = 0', array(':updateid' => $lookup_id));
if (empty($results)) {
echo_json_and_close_connection(array('error' => 'No such update_url exists'), true);
}
$metadata = array_pop($results);
$id = $metadata['id'];
unset($metadata['id']);
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
S3er::delete($id);
DB::statement('UPDATE download SET status = 1 WHERE id = :id', array(':id' => $id));
echo_json_and_close_connection('success', true);
}
$updateid = $lookup_id;
$download_filename = $metadata['download_filename'];
unset($metadata['download_filename']);
$results = DB::statement('SELECT filename, size FROM download_file WHERE id = :id', array(':id' => $id));
if (count($results)) {
foreach ($results as $result) {
$files[] = array('filename' => $result['filename'], 'size' => $result['size']);
}
}
foreach ($metadata as $key => $value) {
if (isset($_REQUEST[$key])) {
if ($key == 'price') {
$metadata[$key] = (float)$_REQUEST[$key];
} else {
$metadata[$key] = trim($_REQUEST[$key]);
}
}
}
}
if ($metadata['currency'] != 'BTC') {
$rates = Utils::get_rates();
if (!isset($rates[$metadata['currency']])) {
echo_json_and_close_connection(array('error' => 'Currency unsupported'), true);
} else {
// If only the currency is changed and the amount isn't updated, we correct the price
if (isset($_REQUEST['currency']) && !isset($_REQUEST['price'])) {
$metadata['price'] = $metadata['price'] * $rates[$metadata['currency']];
}
$metadata['price'] = max(0.0001 * $rates[$metadata['currency']], $metadata['price']);
}
}
if (!$id) {
DB::statement('INSERT INTO download VALUES()', array());
$id = DB::last_insert_id();
$updateid = md5(uniqid($_SERVER['REMOTE_ADDR'], true));
}
if (strlen($download_filename) == 0 && count($clean_files)) {
$rows = array();
foreach ($clean_files as $file) {
$rows[] = $id;
$rows[] = $file['name'];
$rows[] = $file['size'];
$files[] = array('filename' => $file['name'], 'size' => (int)$file['size']);
}
$placeholders = implode(',', array_fill(0, count($clean_files), '(?, ?, ?)'));
DB::statement('INSERT INTO download_file VALUES '.$placeholders, $rows);
}
$metadata = array('download_url' => 'http://' . Utils::domain() . '/' . base_convert($id, 10, 36),
'update_url' => 'http://' . Utils::domain() . '/update/' . $updateid,
'price' => (float)$metadata['price'],
'currency' => $metadata['currency'],
'address' => $metadata['address'],
'files' => $files);
echo_json_and_close_connection($metadata);
// Do the lengthy processing now that the connection is closed
DB::statement('UPDATE download SET updateid = :updateid, price_float = :price, currency = :currency, address = :address WHERE id = :id',
array(':id' => $id,
':updateid' => $updateid,
':price' => $metadata['price'],
':currency' => $metadata['currency'],
':address' => $metadata['address']));
if (strlen($download_filename) == 0 && count($clean_files)) {
if (count($clean_files) > 1) {
$file = Zipper::compress($id, $clean_files);
} else {
$file = array_pop($clean_files);
}
S3er::upload($file, $id);
$download_filename = $file['name'];
DB::statement('UPDATE download SET download_filename = :download_filename WHERE id = :id',
array(':id' => $id,
':download_filename' => $download_filename));
}
GA::event('Producer', 'Upload', $metadata['currency'], $metadata['price']);
}