Commit c2945095 authored by P. J. McDermott's avatar P. J. McDermott

Clean up code in gnukebox/submissions/1.2/index.php.

parent 131fcad3
......@@ -21,14 +21,14 @@
require_once('../../database.php');
require_once('../../scrobble-utils.php');
if(!isset($_POST['s']) || !isset($_POST['a']) || !isset($_POST['t']) || !isset($_POST['i'])) {
if (!isset($_POST['s']) || !isset($_POST['a']) || !isset($_POST['t']) || !isset($_POST['i'])) {
die("FAILED Required POST parameters are not set\n");
}
if(empty($_POST['s']) || empty($_POST['a']) || empty($_POST['t']) || empty($_POST['i'])) {
if (empty($_POST['s']) || empty($_POST['a']) || empty($_POST['t']) || empty($_POST['i'])) {
die("FAILED Required POST parameters are empty\n");
}
if(!is_array($_POST['a']) || !is_array($_POST['t']) || !is_array($_POST['i'])) {
if (!is_array($_POST['a']) || !is_array($_POST['t']) || !is_array($_POST['i'])) {
die("FAILED Track parameters must be arrays\n");
}
......@@ -39,24 +39,24 @@ $rowvalues = array();
$actualcount = 0;
$timeisstupid = 0;
for($i = 0; $i < count($_POST['a']); $i++) {
for ($i = 0; $i < count($_POST['a']); $i++) {
switch (mb_detect_encoding($_POST['a'][$i])) {
case "ASCII":
case "UTF-8":
$artist = $adodb->qstr(trim(mb_strcut($_POST['a'][$i], 0, 255, "UTF-8")));
case 'ASCII':
case 'UTF-8':
$artist = $adodb->qstr(trim(mb_strcut($_POST['a'][$i], 0, 255, 'UTF-8')));
break;
default:
die("FAILED Bad encoding in artist submission $i\n");
}
if(isset($_POST['b'][$i]) && !empty($_POST['b'][$i])) {
if (isset($_POST['b'][$i]) && !empty($_POST['b'][$i])) {
switch (mb_detect_encoding($_POST['b'][$i])) {
case "ASCII":
case "UTF-8":
$album = $adodb->qstr(trim(mb_strcut($_POST['b'][$i], 0, 255, "UTF-8")));
break;
default:
die("FAILED Bad encoding in album submission $i\n");
case 'ASCII':
case 'UTF-8':
$album = $adodb->qstr(trim(mb_strcut($_POST['b'][$i], 0, 255, 'UTF-8')));
break;
default:
die("FAILED Bad encoding in album submission $i\n");
}
} else {
$album = 'NULL';
......@@ -71,103 +71,102 @@ for($i = 0; $i < count($_POST['a']); $i++) {
}
switch (mb_detect_encoding($_POST['t'][$i])) {
case "ASCII":
case "UTF-8":
$track = $adodb->qstr(trim(mb_strcut($_POST['t'][$i], 0, 255, "UTF-8")));
case 'ASCII':
case 'UTF-8':
$track = $adodb->qstr(trim(mb_strcut($_POST['t'][$i], 0, 255, 'UTF-8')));
break;
default:
die("FAILED Bad encoding in title submission $i\n");
}
if(is_numeric($_POST['i'][$i])) {
if (is_numeric($_POST['i'][$i])) {
$time = (int) $_POST['i'][$i];
} else {
// 1.1 time format
date_default_timezone_set("UTC");
date_default_timezone_set('UTC');
$time = strtotime($_POST['i'][$i]);
}
$mb = validateMBID($_POST['m'][$i]);
if($mb) {
if ($mb) {
$mbid = $adodb->qstr($mb);
} else {
$mbid = 'NULL';
}
if(isset($_POST['o'][$i])) {
if (isset($_POST['o'][$i])) {
$source = $adodb->qstr($_POST['o'][$i]);
} else {
$source = 'NULL';
}
if(!empty($_POST['r'][$i])) {
if (!empty($_POST['r'][$i])) {
$rating = $adodb->qstr($_POST['r'][$i]);
} else {
$rating = $adodb->qstr("0"); // use the fake rating code 0 for now
$rating = $adodb->qstr('0'); // use the fake rating code 0 for now
}
if(isset($_POST['l'][$i])) {
if (isset($_POST['l'][$i])) {
$length = (int)($_POST['l'][$i]);
} else {
$length = 'NULL';
}
if(($time - time()) > 300) {
if (($time - time()) > 300) {
die("FAILED Submitted track has timestamp in the future\n"); // let's try a 5-minute tolerance
}
if($time <= 1009000000) {
if ($time <= 1009000000) {
$timeisstupid = 1;
}
$failed = false;
try {
createArtistIfNew($artist);
if($album != 'NULL') {
if ($album != 'NULL') {
createAlbumIfNew($artist, $album);
}
$tid = getTrackCreateIfNew($artist, $album, $track, $mbid);
$stid = getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid, $tid);
$exists = scrobbleExists($userid, $artist, $track, $time);
} catch (exception $ex) {
} catch (Exception $ex) {
$failed = true;
reportError($ex->getMessage(), '');
}
if((!$exists) && $rating<>'S' && !$failed) {
$rowvalues[$actualcount] = "("
. $userid . ", "
. $artist . ", "
. $album . ", "
. $track . ", "
. $time . ", "
. $mbid . ", "
. $source . ","
. $rating . ","
. $length . ","
. $stid . ")";
if (!$exists && $rating != 'S' && !$failed) {
$rowvalues[$actualcount] = '('
. $userid . ', '
. $artist . ', '
. $album . ', '
. $track . ', '
. $time . ', '
. $mbid . ', '
. $source . ','
. $rating . ','
. $length . ','
. $stid . ')';
$actualcount++;
}
if(($i+1) == count($_POST['a'])) {
if($actualcount>0) {
if (($i + 1) == count($_POST['a'])) {
if ($actualcount > 0) {
$adodb->StartTrans();
for($j = 0; $j < $actualcount; $j++) {
for ($j = 0; $j < $actualcount; $j++) {
// Scrobble!
$sql = "INSERT INTO Scrobbles (userid, artist, album, track, time, mbid, source, rating, length, stid) VALUES " . $rowvalues[$j];
try {
$res =& $adodb->Execute($sql);
if(isset($lastfm_key)) {
if (isset($lastfm_key)) {
forwardScrobble($userid, $_POST['a'][$i], $_POST['b'][$i], $_POST['t'][$i], $time, $_POST['m'][$i], $_POST['o'][$i], $_POST['r'][$i], $_POST['l'][$i]);
}
}
catch (exception $e) {
} catch (Exception $e) {
$msg = $e->getMessage();
$adodb->FailTrans();
$adodb->CompleteTrans();
......@@ -178,13 +177,12 @@ for($i = 0; $i < count($_POST['a']); $i++) {
try {
$adodb->CompleteTrans();
}
catch (exception $e) {
die("FAILED " . $e->getMessage() . "\n");
} catch (Exception $e) {
die('FAILED ' . $e->getMessage() . "\n");
}
} else {
if($timeisstupid == 1) {
if ($timeisstupid == 1) {
die("FAILED Too many submitted tracks with invalid timestamps\n");
}
}
......@@ -193,5 +191,3 @@ for($i = 0; $i < count($_POST['a']); $i++) {
}
die("OK\n");
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment