Skip to content

Commit

Permalink
Prevent inconsistencies in download statistics
Browse files Browse the repository at this point in the history
Download script also uses DBConnection now,
SQL changed to reflect new primary keys and relations
  • Loading branch information
stephenjust committed Dec 30, 2013
1 parent 3dd38c9 commit c649d4d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 55 deletions.
80 changes: 33 additions & 47 deletions download.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/

define('ROOT','./');
include_once('config.php');
include_once('include/sql.php');
require_once('config.php');
require_once(INCLUDE_DIR . 'DBConnection.class.php');

$dir = $_GET['type'];
$file = $_GET['file'];
Expand All @@ -43,7 +43,6 @@
$assetpath = $dir.'/'.$file;
else
$assetpath = $file;
$filepath = UP_LOCATION.$assetpath;

// Don't bother checking if the file exists - if it doesn't exist, you'll get
// a 404 error anyways after redirecting. Yes, this may make the stats below
Expand All @@ -52,59 +51,46 @@

// Check user-agent
$uagent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('#^(SuperTuxKart/[a-z0-9\.\-_]+)( \\(.*\\))?$#',$uagent,$matches)) {
// Check if this user-agent is already known
$checkSql = 'SELECT `agent_string`, `disabled` FROM `'.DB_PREFIX.'clients`
WHERE `agent_string` = \''.mysql_real_escape_string($matches[1]).'\'';
$checkHandle = sql_query($checkSql);
if (mysql_num_rows($checkHandle) != 1)
{
// New user-agent. Add it to the database.
$newSql = 'INSERT INTO `'.DB_PREFIX.'clients`
(`agent_string`) VALUES (\''.mysql_real_escape_string($matches[1]).'\')';
$newHandle = sql_query($newSql);
}
else
{
$checkResult = sql_next($checkHandle);
if ($checkResult['disabled'] == 1)
{
header("HTTP/1.0 404 Not Found");
exit;
}
}

// Increase daily count for this user-agent
$checkStatQuery = 'SELECT `id`
FROM `'.DB_PREFIX.'stats`
WHERE `type` = \'uagent '.mysql_real_escape_string($uagent).'\'
AND `date` = CURDATE()';
$checkStatHandle = sql_query($checkStatQuery);
if (!$checkStatHandle) {
$matches = array();
if (preg_match('#^(SuperTuxKart/[a-z0-9\\.\\-_]+)( \\(.*\\))?$#',$uagent,&$matches)) {
try {
DBConnection::get()->query(
'INSERT IGNORE INTO `'.DB_PREFIX.'clients`
(`agent_string`)
VALUES
(:uagent)',
DBConnection::NOTHING,
array(':uagent' => $matches[1]));
} catch (DBException $e) {
header("HTTP/1.0 404 Not Found");
exit;
}
if (mysql_num_rows($checkStatHandle) === 0) {
// Insert new stat record
$insertStatQuery = 'INSERT INTO `'.DB_PREFIX.'stats`
(`type`,`date`,`value`) VALUES
(\'uagent '.mysql_real_escape_string($uagent).'\',CURDATE(),1)';
} else {
$insertStatQuery = 'UPDATE `'.DB_PREFIX.'stats`
SET `value` = `value` + 1
WHERE `type` = \'uagent '.mysql_real_escape_string($uagent).'\'
AND `date` = CURDATE()';
}
$insertStatHandle = sql_query($insertStatQuery);
if (!$insertStatHandle) {

// Increase daily count for this user-agent
try {
DBConnection::get()->query(
'INSERT INTO `'.DB_PREFIX.'stats`
(`type`,`date`,`value`)
VALUES
(:type, CURDATE(), 1)
ON DUPLICATE KEY UPDATE
`value` = `value` + 1',
DBConnection::NOTHING,
array(':type' => 'uagent '.$uagent));
} catch (DBException $e) {
header("HTTP/1.0 404 Not Found");
echo 'Failed to update statistics';
exit;
}
}

// Update download count for addons
$counterQuery = 'CALL `'.DB_PREFIX.'increment_download` (\''.$assetpath.'\')';
$counterHandle = sql_query($counterQuery);
try {
DBConnection::get()->query('CALL `'.DB_PREFIX.'increment_download` (:path)',
DBConnection::NOTHING, array(':path' => $assetpath));
} catch (DBException $e) {
// Do nothing
}

// Redirect to actual resource
if ($dir == 'xml') {
Expand Down
21 changes: 13 additions & 8 deletions table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- http://www.phpmyadmin.net
--
-- Host: sql
-- Generation Time: Dec 24, 2013 at 02:11 AM
-- Generation Time: Dec 30, 2013 at 10:13 PM
-- Server version: 5.1.72
-- PHP Version: 5.3.3-7+squeeze18

Expand Down Expand Up @@ -147,12 +147,11 @@ CREATE TABLE IF NOT EXISTS `v2_cache` (
--

CREATE TABLE IF NOT EXISTS `v2_clients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`agent_string` varchar(255) NOT NULL,
`stk_version` varchar(64) NOT NULL DEFAULT 'latest',
`disabled` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
PRIMARY KEY (`agent_string`(32))
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

Expand Down Expand Up @@ -202,7 +201,8 @@ CREATE TABLE IF NOT EXISTS `v2_files` (
`downloads` int(10) unsigned NOT NULL DEFAULT '0',
`delete_date` date NOT NULL DEFAULT '0000-00-00',
PRIMARY KEY (`id`),
KEY `delete_date` (`delete_date`)
KEY `delete_date` (`delete_date`),
KEY `addon_id` (`addon_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------
Expand Down Expand Up @@ -367,13 +367,12 @@ CREATE TABLE IF NOT EXISTS `v2_server_conn` (
--

CREATE TABLE IF NOT EXISTS `v2_stats` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type` text NOT NULL,
`date` date NOT NULL,
`value` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
PRIMARY KEY (`date`,`type`(40)),
KEY `date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

Expand Down Expand Up @@ -467,6 +466,12 @@ ALTER TABLE `v2_addons`
ALTER TABLE `v2_arenas_revs`
ADD CONSTRAINT `v2_arenas_revs_ibfk_1` FOREIGN KEY (`addon_id`) REFERENCES `v2_addons` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `v2_files`
--
ALTER TABLE `v2_files`
ADD CONSTRAINT `v2_files_ibfk_1` FOREIGN KEY (`addon_id`) REFERENCES `v2_addons` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `v2_karts_revs`
--
Expand Down

0 comments on commit c649d4d

Please sign in to comment.