Skip to content

Commit

Permalink
able to import non-latest data from cstimer
Browse files Browse the repository at this point in the history
  • Loading branch information
cs0x7f committed Nov 10, 2019
1 parent 80bacbd commit 8280fda
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
26 changes: 22 additions & 4 deletions dist/userdata.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
echo '{"retcode":400,"reason":"invalid uid"}';
exit(0);
}
if (!isset($_POST['offset']) || empty($_POST['offset'])) {
$offset = '0';
} else if (!preg_match("/^[0-9]+$/", $_POST['offset']) || strlen($_POST['offset']) > 5) {
echo '{"retcode":400,"reason":"invalid offset"}';
exit(0);
} else {
$offset = $_POST['offset'];
}
header("Access-Control-Allow-Origin: *");
$uid = $_POST['id'];

Expand All @@ -24,17 +32,27 @@
exit(0);
}
$data = $_POST['data'];
error_log("[" . date("Y-m-d H:i:sO") . "] SET " . $uid . " " . strlen($data) . "\n", 3, CSTIMER_USERDATA_LOGFILE);
error_log("[" . date("Y-m-d H:i:sO") . "] SET $uid " . strlen($data) . "\n", 3, CSTIMER_USERDATA_LOGFILE);
$data_md5 = md5($data);
$sql = 'INSERT INTO `export_data` (`uid`, `value_hash`, `value`) VALUES ("' . $uid . '", "' . $data_md5 . '", "' . $data . '")';
$sql = "INSERT INTO `export_data` (`uid`, `value_hash`, `value`) VALUES ('$uid', '$data_md5', '$data')";
$ret = $db->query($sql);
if ($ret === true) {
echo '{"retcode":0}';
} else {
echo '{"retcode":500,"reason":"db insert error"}';
}
} else if (isset($_POST['cnt'])) {
$sql = "SELECT COUNT(*) AS cnt FROM `export_data` WHERE `uid` = '$uid'";
$ret = $db->query($sql);
if ($ret === false) {
echo '{"retcode":500,"reason":"db select error"}';
exit(0);
}
$ret = $ret->fetch_assoc()['cnt'];
error_log("[" . date("Y-m-d H:i:sO") . "] CNT $uid " . $ret . "\n", 3, CSTIMER_USERDATA_LOGFILE);
echo '{"retcode":0,"data":"' . $ret . '"}';
} else {//GET
$sql = 'SELECT `value` FROM `export_data` WHERE `uid` = "' . $uid . '" ORDER BY `upload_time` DESC LIMIT 1;';
$sql = "SELECT `value` FROM `export_data` WHERE `uid` = '$uid' ORDER BY `upload_time` DESC LIMIT $offset,1;";
$ret = $db->query($sql);
if ($ret === false) {
echo '{"retcode":500,"reason":"db select error"}';
Expand All @@ -45,7 +63,7 @@
exit(0);
}
$ret = $ret->fetch_assoc()['value'];
error_log("[" . date("Y-m-d H:i:sO") . "] GET " . $uid . " " . strlen($ret) . "\n", 3, CSTIMER_USERDATA_LOGFILE);
error_log("[" . date("Y-m-d H:i:sO") . "] GET $uid " . strlen($ret) . " $offset\n", 3, CSTIMER_USERDATA_LOGFILE);
echo '{"retcode":0,"data":"' . $ret . '"}';
}
?>
72 changes: 51 additions & 21 deletions src/js/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,37 @@ var exportFunc = execMain(function() {
}
var target = $(e.target);
var rawText = target.html();
target.html('...');
$.post('https://cstimer.net/userdata.php', {
'id': id
}, function(val) {
target.html('......');
target.html('Check File List...');

var onerr = function() {
alert(EXPORT_ERROR);
};

var revert = function() {
target.html(rawText);
};

var cntCallback = function(val) {
var cnt = ~~val['data'];
if (cnt == 0) {
alert('No Data Found');
return revert();
}
var idx = 1;
if (kernel.getProp('expp')) {
idx = ~~prompt('You have %d file(s), load (1 - lastest one, 2 - lastest but one, etc) ?'.replace('%d', cnt), '1');
if (idx <= 0 || idx > cnt) {
return revert();
}
}
target.html('Import Data...');
$.post('https://cstimer.net/userdata.php', {
'id': id,
'offset': idx - 1
}, dataCallback, 'json').error(onerr).always(revert);
}

var dataCallback = function(val) {
var retcode = val['retcode'];
if (retcode == 0) {
try {
Expand All @@ -177,12 +203,17 @@ var exportFunc = execMain(function() {
} else {
alert(EXPORT_ERROR);
}
target.html(rawText);
}, 'json').error(function() {
alert(EXPORT_ERROR);
}).always(function() {
target.html(rawText);
});
revert();
}

if (kernel.getProp('expp')) {
$.post('https://cstimer.net/userdata.php', {
'id': id,
'cnt': 1
}, cntCallback, 'json').error(onerr).always(revert);
} else {
cntCallback({'data':1});
}
}

function downloadDataGGL() {
Expand All @@ -197,15 +228,14 @@ var exportFunc = execMain(function() {
var files = data['files'];
if (files.length == 0) {
alert('No Data Found');
updateUserInfoFromGGL();
return;
return updateUserInfoFromGGL();
}
var idx = prompt('You have %d file(s), load (1 - lastest one, 2 - lastest but one, etc) ?'.replace('%d', files.length), '1');
if (idx == null || ~~idx > files.length) {
updateUserInfoFromGGL();
return;
} else if (~~idx <= 0) {
idx = 1;
var idx = 1;
if (kernel.getProp('expp')) {
idx = ~~prompt('You have %d file(s), load (1 - lastest one, 2 - lastest but one, etc) ?'.replace('%d', files.length), '1');
if (idx <= 0 || idx > cnt) {
return updateUserInfoFromGGL();
}
}
inServGGL.html('Import Data...');
var fileId = files[idx - 1]['id'];
Expand All @@ -214,8 +244,7 @@ var exportFunc = execMain(function() {
data = JSON.parse(LZString.decompressFromEncodedURIComponent(data));
} catch (e) {
alert('No Valid Data Found');
updateUserInfoFromGGL();
return;
return updateUserInfoFromGGL();
}
loadData(data);
}).error(function() {
Expand Down Expand Up @@ -491,6 +520,7 @@ var exportFunc = execMain(function() {
kernel.regListener('export', 'export', procSignal, /^account$/);
kernel.regProp('kernel', 'atexpa', 1, 'Auto Export (per 100 solves)', ['n', ['n', 'f', 'id', 'wca'], ['Never', 'To File', 'With csTimer ID', 'With WCA Account']]);
kernel.regProp('kernel', 'atexpi', ~1, 'Auto Export Interval (Solves)', [100, [50, 100, 200, 500], ['50', '100', '200', '500']]);
kernel.regProp('kernel', 'expp', 0, 'Export non-latest data', [false]);

kernel.addButton('export', BUTTON_EXPORT, showExportDiv, 2);
exportDiv.append('<br>',
Expand Down

0 comments on commit 8280fda

Please sign in to comment.