download.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * copyright 2011 Stephen Just <stephenjust@users.sf.net>
  4. * 2014 Daniel Butum <danibutum at gmail dot com>
  5. * This file is part of stk-addons.
  6. *
  7. * stk-addons is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * stk-addons is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with stk-addons. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('DOWNLOAD_MODE', true);
  21. require_once(__DIR__ . DIRECTORY_SEPARATOR . "config.php");
  22. $file = isset($_GET['file']) ? $_GET['file'] : null;
  23. $assets_path = filter_var($file, FILTER_SANITIZE_URL);
  24. // TODO probably the best solutions is not to redirect to the file, but instead output the file from here
  25. // Don't bother checking if the file exists - if it doesn't exist, you'll get
  26. // a 404 error anyways after redirecting. Yes, this may make the stats below
  27. // inaccurate, but the actual 404's that used to be thrown here were relatively
  28. // rare anyways.
  29. // Check user-agent
  30. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  31. $matches = [];
  32. $uri = $_SERVER['REQUEST_URI'];
  33. if (Util::str_contains($uri, 'news.xml') &&
  34. preg_match('#^(SuperTuxKart/[a-z0-9\\.\\-_]+)( \\(.*\\))?$#', $user_agent, $matches))
  35. {
  36. try
  37. {
  38. DBConnection::get()->query(
  39. 'INSERT IGNORE INTO `{DB_VERSION}_clients`
  40. (`agent_string`)
  41. VALUES
  42. (:uagent)',
  43. DBConnection::NOTHING,
  44. [':uagent' => $matches[1]]
  45. );
  46. }
  47. catch(DBException $e)
  48. {
  49. http_response_code(404);
  50. exit;
  51. }
  52. // Increase daily count for this user-agent
  53. try
  54. {
  55. DBConnection::get()->query(
  56. 'INSERT INTO `{DB_VERSION}_stats`
  57. (`type`,`date`,`value`)
  58. VALUES
  59. (:type, CURDATE(), 1)
  60. ON DUPLICATE KEY UPDATE
  61. `value` = `value` + 1',
  62. DBConnection::NOTHING,
  63. [':type' => 'uagent ' . $user_agent]
  64. );
  65. }
  66. catch(DBException $e)
  67. {
  68. http_response_code(404);
  69. exit('Failed to update statistics');
  70. }
  71. }
  72. // Update download count for addons
  73. try
  74. {
  75. File::incrementDownload($assets_path);
  76. }
  77. catch(FileException $e)
  78. {
  79. http_response_code(404);
  80. exit;
  81. }
  82. // Redirect to actual resource,
  83. header('Location: ' . ROOT_LOCATION . 'downloads/' . $assets_path);
  84. exit;