index.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /* GNUkebox -- a free software server for recording your listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once('../../database.php');
  16. require_once('../../scrobble-utils.php');
  17. if (!isset($_POST['s']) || !isset($_POST['a']) || !isset($_POST['t']) || !isset($_POST['i'])) {
  18. die("FAILED Required POST parameters are not set\n");
  19. }
  20. if (empty($_POST['s']) || empty($_POST['a']) || empty($_POST['t']) || empty($_POST['i'])) {
  21. die("FAILED Required POST parameters are empty\n");
  22. }
  23. if (!is_array($_POST['a']) || !is_array($_POST['t']) || !is_array($_POST['i'])) {
  24. die("FAILED Track parameters must be arrays\n");
  25. }
  26. $session_id = $_POST['s'];
  27. $userid = useridFromSID($session_id);
  28. $rowvalues = array();
  29. $forwardvalues = array();
  30. $actualcount = 0;
  31. $timeisstupid = 0;
  32. for ($i = 0; $i < count($_POST['a']); $i++) {
  33. if (!isset($_POST['t'][$i]) || !isset($_POST['a'][$i]) || !isset($_POST['i'][$i])) {
  34. $f = isset($_POST['t'][$i]) ? "T({$_POST['t'][$i]})" : 't';
  35. $f .= isset($_POST['a'][$i]) ? "A({$_POST['a'][$i]})" : 'a';
  36. $f .= isset($_POST['i'][$i]) ? "I({$_POST['i'][$i]})" : 'i';
  37. //Add error message to db and skip to next scrobble
  38. reportError("FAILED Track $i was submitted with empty mandatory field(s)",
  39. "artist:{$_POST['a'][$i]}, album:{$_POST['b'][$i]}, track:{$_POST['t'][$i]}, time:{$_POST['i'][$i]}");
  40. continue;
  41. }
  42. $artist = trim($_POST['a'][$i]);
  43. $artist = noSpamTracks($artist);
  44. if (empty($artist)) {
  45. //Add error message to db and skip to next scrobble
  46. reportError("FAILED Track $i was submitted with empty artist field",
  47. "artist:{$_POST['a'][$i]}, album:{$_POST['b'][$i]}, track:{$_POST['t'][$i]}, time:{$_POST['i'][$i]}");
  48. continue;
  49. } else {
  50. switch (mb_detect_encoding($artist)) {
  51. case 'ASCII':
  52. case 'UTF-8':
  53. $artist = $adodb->qstr(trim(mb_strcut($artist, 0, 255, 'UTF-8')));
  54. break;
  55. default:
  56. die("FAILED Bad encoding in artist submission $i\n");
  57. }
  58. }
  59. $track = trim($_POST['t'][$i]);
  60. $track = noSpamTracks($track);
  61. if (empty($track)) {
  62. //Add error message to db and skip to next scrobble
  63. reportError("FAILED Track $i was submitted with empty track field",
  64. "artist:{$_POST['a'][$i]}, album:{$_POST['b'][$i]}, track:{$_POST['t'][$i]}, time:{$_POST['i'][$i]}");
  65. continue;
  66. } else {
  67. switch (mb_detect_encoding($track)) {
  68. case 'ASCII':
  69. case 'UTF-8':
  70. $track = $adodb->qstr(trim(mb_strcut($track, 0, 255, 'UTF-8')));
  71. break;
  72. default:
  73. die("FAILED Bad encoding in title submission $i\n");
  74. }
  75. }
  76. if (is_numeric($_POST['i'][$i])) {
  77. $time = (int) $_POST['i'][$i];
  78. } else {
  79. // 1.1 time format
  80. date_default_timezone_set('UTC');
  81. $time = strtotime($_POST['i'][$i]);
  82. }
  83. $album = trim($_POST['b'][$i]);
  84. $album = noSpamTracks($album);
  85. if (!empty($album)) {
  86. switch (mb_detect_encoding($album)) {
  87. case 'ASCII':
  88. case 'UTF-8':
  89. $album = $adodb->qstr(trim(mb_strcut($album, 0, 255, 'UTF-8')));
  90. break;
  91. default:
  92. die("FAILED Bad encoding in album submission $i\n");
  93. }
  94. } else {
  95. $album = 'NULL';
  96. }
  97. $mb = validateMBID($_POST['m'][$i]);
  98. if ($mb) {
  99. $mbid = $adodb->qstr($mb);
  100. } else {
  101. $mbid = 'NULL';
  102. }
  103. if (isset($_POST['o'][$i])) {
  104. $source = $adodb->qstr($_POST['o'][$i]);
  105. } else {
  106. $source = 'NULL';
  107. }
  108. if (!empty($_POST['r'][$i])) {
  109. $rating = $adodb->qstr($_POST['r'][$i]);
  110. } else {
  111. $rating = $adodb->qstr('0'); // use the fake rating code 0 for now
  112. }
  113. if (isset($_POST['l'][$i])) {
  114. $length = (int)($_POST['l'][$i]);
  115. } else {
  116. $length = 'NULL';
  117. }
  118. if (($time - time()) > 300) {
  119. die("FAILED Submitted track has timestamp in the future\n"); // let's try a 5-minute tolerance
  120. }
  121. if ($time <= 1009000000) {
  122. $timeisstupid = 1;
  123. }
  124. $failed = false;
  125. try {
  126. $exists = scrobbleExists($userid, $artist, $track, $time);
  127. if (!$exists) {
  128. $stid = getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid);
  129. }
  130. } catch (Exception $ex) {
  131. $failed = true;
  132. reportError($ex->getMessage(), '');
  133. }
  134. if (!$exists && $rating != 'S' && !$failed) {
  135. $rowvalues[$actualcount] = '('
  136. . $userid . ', '
  137. . $artist . ', '
  138. . $album . ', '
  139. . $track . ', '
  140. . $time . ', '
  141. . $mbid . ', '
  142. . $source . ','
  143. . $rating . ','
  144. . $length . ','
  145. . $stid . ')';
  146. // Create array with tracks to be forwarded
  147. if (isset($lastfm_key)) {
  148. $forwardvalues[$actualcount] = array(
  149. $userid,
  150. $_POST['a'][$i],
  151. $_POST['b'][$i],
  152. $_POST['t'][$i],
  153. $time,
  154. $_POST['m'][$i],
  155. $_POST['o'][$i],
  156. $_POST['r'][$i],
  157. $_POST['l'][$i]
  158. );
  159. }
  160. $actualcount++;
  161. }
  162. if (($i + 1) == count($_POST['a'])) {
  163. if ($actualcount > 0) {
  164. $adodb->StartTrans();
  165. for ($j = 0; $j < $actualcount; $j++) {
  166. // Scrobble!
  167. $sql = 'INSERT INTO Scrobbles (userid, artist, album, track, time, mbid, source, rating, length, stid) VALUES ' . $rowvalues[$j];
  168. try {
  169. $res =& $adodb->Execute($sql);
  170. if (isset($lastfm_key)) {
  171. call_user_func_array("forwardScrobble", $forwardvalues[$j]);
  172. }
  173. } catch (Exception $e) {
  174. $msg = $e->getMessage();
  175. $adodb->FailTrans();
  176. $adodb->CompleteTrans();
  177. reportError($msg, $sql);
  178. }
  179. }
  180. try {
  181. $adodb->CompleteTrans();
  182. } catch (Exception $e) {
  183. die('FAILED ' . $e->getMessage() . "\n");
  184. }
  185. } else {
  186. if ($timeisstupid == 1) {
  187. die("FAILED Too many submitted tracks with invalid timestamps\n");
  188. }
  189. }
  190. }
  191. }
  192. die("OK\n");