Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vampy committed May 16, 2014
1 parent 9edd892 commit 29fcc1d
Show file tree
Hide file tree
Showing 14 changed files with 693 additions and 409 deletions.
75 changes: 49 additions & 26 deletions download.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,36 @@
* along with stkaddons. If not, see <http://www.gnu.org/licenses/>.
*/

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

$dir = $_GET['type'];
$file = $_GET['file'];
// Make sure directory is not unsafe
if (!preg_match('/^[a-z]+$/i',$dir))
if (!preg_match('/^[a-z]+$/i', $dir))
{
// Directory is unsafe - throw a 404 error
header("HTTP/1.0 404 Not Found");
exit;
}

// Make sure file name is not unsafe
if (!preg_match('/^[\w\-\ ]+\.[a-z0-9]+$/i',$file))
if (!preg_match('/^[\w\-\ ]+\.[a-z0-9]+$/i', $file))
{
// File is unsafe - throw a 404 error
header("HTTP/1.0 404 Not Found");
exit;
}

if ($dir != 'assets')
$assetpath = $dir.'/'.$file;
{
$assetpath = $dir . '/' . $file;
}
else
{
$assetpath = $file;
}

// 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,50 +57,68 @@
// Check user-agent
$uagent = $_SERVER['HTTP_USER_AGENT'];
$matches = array();
if (preg_match('#^(SuperTuxKart/[a-z0-9\\.\\-_]+)( \\(.*\\))?$#',$uagent,&$matches)) {
try {
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) {
'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;
}

// Increase daily count for this user-agent
try {
try
{
DBConnection::get()->query(
'INSERT INTO `'.DB_PREFIX.'stats`
'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) {
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
try {
DBConnection::get()->query('CALL `'.DB_PREFIX.'increment_download` (:path)',
DBConnection::NOTHING, array(':path' => $assetpath));
} catch (DBException $e) {
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') {
header('Location: http://stkaddons.net/xml/'.$file);
} else {
header('Location: http://downloads.tuxfamily.org/stkaddons/assets/'.$assetpath);
if ($dir == 'xml')
{
header('Location: http://stkaddons.net/xml/' . $file);
}
else
{
header('Location: http://downloads.tuxfamily.org/stkaddons/assets/' . $assetpath);
}
exit;
31 changes: 19 additions & 12 deletions error.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* copyright 2012-2014 Stephen Just <[email protected]>
*
Expand All @@ -18,10 +17,11 @@
* You should have received a copy of the GNU General Public License
* along with stkaddons. If not, see <http://www.gnu.org/licenses/>.
*/
$error_code = (empty($_GET['e'])) ? NULL : $_GET['e'];
$error_code = (empty($_GET['e'])) ? null : $_GET['e'];

// Send appropriate error header
switch ($error_code) {
switch ($error_code)
{
default:
break;
case '403':
Expand All @@ -33,11 +33,12 @@
}

define('ROOT', './');
require_once(ROOT.'config.php');
require_once(INCLUDE_DIR.'StkTemplate.class.php');
require_once(ROOT . 'config.php');
require_once(INCLUDE_DIR . 'StkTemplate.class.php');

$tpl = new StkTemplate('error-page.tpl');
switch ($error_code) {
switch ($error_code)
{
default:
// I18N: Heading for general error page
$error_head = htmlspecialchars(_('An Error Occurred'));
Expand All @@ -48,18 +49,24 @@
// I18N: Heading for HTTP 403 Forbidden error page
$error_head = htmlspecialchars(_('403 - Forbidden'));
// I18N: Error message for HTTP 403 Forbidden error page
$error_text = htmlspecialchars(_('You\'re not supposed to be here. Click one of the links in the menu above to find some better content.'));
$error_text = htmlspecialchars(
_('You\'re not supposed to be here. Click one of the links in the menu above to find some better content.')
);
break;
case '404':
// I18N: Heading for HTTP 404 Not Found error page
$error_head = htmlspecialchars(_('404 - Not Found'));
// I18N: Error message for HTTP 404 Not Found error page
$error_text = htmlspecialchars(_('We can\'t find what you are looking for. The link you followed may be broken.'));
$error_text =
htmlspecialchars(_('We can\'t find what you are looking for. The link you followed may be broken.'));
break;
}
$tpl->assign('error', array(
'title' => $error_head,
'message' => $error_text
));
$tpl->assign(
'error',
array(
'title' => $error_head,
'message' => $error_text
)
);

echo $tpl;
6 changes: 3 additions & 3 deletions generate_xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
* along with stkaddons. If not, see <http://www.gnu.org/licenses/>.
*/

define('ROOT','./');
define('ROOT', './');
include_once('include.php');

$xml = writeNewsXML();
echo 'News xml written: '.$xml.'<br />';
echo 'News xml written: ' . $xml . '<br />';

$xml = writeAssetXML();
echo 'Asset xml written: '.$xml.'<br />';
echo 'Asset xml written: ' . $xml . '<br />';
2 changes: 1 addition & 1 deletion image.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
You should have received a copy of the GNU General Public License along with
stkaddons. If not, see <http://www.gnu.org/licenses/>. */

define('ROOT','./');
define('ROOT', './');
include('include.php');
resizeImage($_GET['pic']);
1 change: 0 additions & 1 deletion include.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@
require_once(INCLUDE_DIR . 'User.class.php');
require_once(INCLUDE_DIR . 'locale.php');
require_once(INCLUDE_DIR . 'Ratings.class.php');
require_once(INCLUDE_DIR . 'image.php');
require_once(INCLUDE_DIR . 'xmlWrite.php');
Loading

0 comments on commit 29fcc1d

Please sign in to comment.