From 29fcc1d044b63287dd73998533a3fc873aac6c67 Mon Sep 17 00:00:00 2001 From: Daniel Butum Date: Fri, 16 May 2014 21:36:38 +0300 Subject: [PATCH] More refactoring --- download.php | 75 +++++--- error.php | 31 ++-- generate_xml.php | 6 +- image.php | 2 +- include.php | 1 - include/Music.class.php | 153 +++++++++++------ include/SImage.class.php | 310 ++++++++++++++++++++++++++++++---- include/SLocale.class.php | 106 ++++++++---- include/SMail.class.php | 215 +++++++++++++++-------- include/image.php | 152 ----------------- include/locale.php | 12 +- music.php | 29 ++-- privacy.php | 8 +- tpl/default/music-browser.tpl | 2 +- 14 files changed, 693 insertions(+), 409 deletions(-) delete mode 100644 include/image.php diff --git a/download.php b/download.php index 56373cac..fb3750b1 100644 --- a/download.php +++ b/download.php @@ -18,21 +18,22 @@ * along with stkaddons. If not, see . */ -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"); @@ -40,9 +41,13 @@ } 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 @@ -52,32 +57,41 @@ // 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; @@ -85,17 +99,26 @@ } // 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; diff --git a/error.php b/error.php index e547de79..c3cb2413 100644 --- a/error.php +++ b/error.php @@ -1,5 +1,4 @@ * @@ -18,10 +17,11 @@ * You should have received a copy of the GNU General Public License * along with stkaddons. If not, see . */ -$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': @@ -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')); @@ -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; diff --git a/generate_xml.php b/generate_xml.php index 44ce0996..028bac93 100644 --- a/generate_xml.php +++ b/generate_xml.php @@ -18,11 +18,11 @@ * along with stkaddons. If not, see . */ -define('ROOT','./'); +define('ROOT', './'); include_once('include.php'); $xml = writeNewsXML(); -echo 'News xml written: '.$xml.'
'; +echo 'News xml written: ' . $xml . '
'; $xml = writeAssetXML(); -echo 'Asset xml written: '.$xml.'
'; +echo 'Asset xml written: ' . $xml . '
'; diff --git a/image.php b/image.php index f98ef8fc..fef74e8a 100644 --- a/image.php +++ b/image.php @@ -16,6 +16,6 @@ You should have received a copy of the GNU General Public License along with stkaddons. If not, see . */ -define('ROOT','./'); +define('ROOT', './'); include('include.php'); resizeImage($_GET['pic']); diff --git a/include.php b/include.php index 75fa54e8..37ee87aa 100644 --- a/include.php +++ b/include.php @@ -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'); diff --git a/include/Music.class.php b/include/Music.class.php index 92046d24..d2b5582c 100644 --- a/include/Music.class.php +++ b/include/Music.class.php @@ -1,7 +1,7 @@ - * + * 2014 Daniel Butum * This file is part of stkaddons * * stkaddons is free software: you can redistribute it and/or modify @@ -20,150 +20,201 @@ require_once(INCLUDE_DIR . 'DBConnection.class.php'); -class Music { +/** + * Class Music + */ +class Music +{ /** * @var integer */ - private $id = NULL; + private $id; + /** * @var string */ - private $title = NULL; + private $title; + /** * @var string */ - private $artist = NULL; + private $artist; + /** * @var string */ - private $license = NULL; + private $license; + /** * @var integer */ - private $length = NULL; + private $length; + /** * @var float */ - private $gain = NULL; + private $gain; + /** * @var string */ - private $file = NULL; + private $file; + /** * @var string */ - private $file_md5 = NULL; + private $file_md5; + /** * @var string */ - private $xml_file = NULL; - + private $xml_file; + /** * @return integer */ - public function getId() { + public function getId() + { return $this->id; } + /** * @return string */ - public function getTitle() { + public function getTitle() + { return $this->title; } + /** * @return string */ - public function getArtist() { + public function getArtist() + { return $this->artist; } + /** * @return string */ - public function getLicense() { + public function getLicense() + { return $this->license; } + /** * @return string */ - public function getFile() { + public function getFile() + { return $this->file; } + /** * @return string */ - public function getXmlFile() { + public function getXmlFile() + { return $this->xml_file; } + /** * @return float */ - public function getGain() { + public function getGain() + { return $this->gain; } + /** * @return integer */ - public function getLength() { + public function getLength() + { return $this->length; } - + /** * Get a Music object by ID + * * @param integer $id - * @return \self + * + * @return Music */ - public static function get($id) { + public static function get($id) + { $instance = new self(); $instance->populateById($id); + return $instance; } - + /** * Get an array of Music objects containing all tracks, sorted by title + * * @return array */ - public static function getAllByTitle() { - try { + public static function getAllByTitle() + { + try + { $music_tracks = array(); $result = DBConnection::get()->query( - 'SELECT `id` FROM `'.DB_PREFIX.'music` - ORDER BY `title` ASC', - DBConnection::FETCH_ALL); - foreach ($result AS $music_track) { + 'SELECT `id` FROM `' . DB_PREFIX . 'music` + ORDER BY `title` ASC', + DBConnection::FETCH_ALL + ); + foreach ($result as $music_track) + { $track = Music::get($music_track['id']); - if ($track->getId() !== NULL) + if ($track->getId() !== null) + { $music_tracks[] = $track; + } } + return $music_tracks; - } catch (DBException $e) { + } + catch(DBException $e) + { return array(); } } - + /** * Populate a music object by looking up the ID in the database + * * @param integer $id - * @return void + * + * @return null */ - private function populateById($id) { - try { + private function populateById($id) + { + try + { $track_info = DBConnection::get()->query( - 'SELECT * FROM `'.DB_PREFIX.'music` - WHERE `id` = :id', - DBConnection::FETCH_ALL, - array(':id' => (int) $id)); - if (count($track_info) === 0) return; - + 'SELECT * FROM `' . DB_PREFIX . 'music` + WHERE `id` = :id', + DBConnection::FETCH_FIRST, + array(':id' => (int)$id) + ); + if (empty($track_info)) + { + return null; + } + $this->id = $id; - $this->title = $track_info[0]['title']; - $this->artist = $track_info[0]['artist']; - $this->license = $track_info[0]['license']; - $this->gain = $track_info[0]['gain']; - $this->length = $track_info[0]['length']; - $this->file = $track_info[0]['file']; - $this->file_md5 = $track_info[0]['file_md5']; - $this->xml_file = $track_info[0]['xml_filename']; - } catch (DBException $e) { + $this->title = $track_info['title']; + $this->artist = $track_info['artist']; + $this->license = $track_info['license']; + $this->gain = $track_info['gain']; + $this->length = $track_info['length']; + $this->file = $track_info['file']; + $this->file_md5 = $track_info['file_md5']; + $this->xml_file = $track_info['xml_filename']; + } + catch(DBException $e) + { return; } } diff --git a/include/SImage.class.php b/include/SImage.class.php index 0fee3cf7..c912e513 100644 --- a/include/SImage.class.php +++ b/include/SImage.class.php @@ -1,7 +1,7 @@ - * + * 2014 Daniel Butum * This file is part of stkaddons * * stkaddons is free software: you can redistribute it and/or modify @@ -18,25 +18,220 @@ * along with stkaddons. If not, see . */ +/** + * Resize an image + * + * @param string $file + * + * @return null + */ +function resizeImage($file) +{ + // Determine image size + $type = (isset($_GET['type'])) ? $_GET['type'] : null; + switch ($type) + { + default: + $size = 100; + break; + case 'small': + $size = 25; + break; + case 'medium': + $size = 75; + break; + case 'big': + $size = 300; + break; + } + $cache_name = $size . '--' . basename($file); + $local_path = UP_LOCATION . $file; + + // Check if image exists, and if it does, check its format + $orig_file = File::exists($file); + if ($orig_file === -1) + { + if (!file_exists(ROOT . $file)) + { + header('HTTP/1.1 404 Not Found'); + + return; + } + else + { + $local_path = ROOT . $file; + } + } + + // Check if a cached version is available + if (Cache::fileExists($cache_name) !== array()) + { + header('Cached-Image: true'); + header('Location: ' . CACHE_DL . '/' . $cache_name); + + return; + } + + // Start processing the original file + $image_info = @getimagesize($local_path); + switch ($image_info[2]) + { + default: + $source = imagecreatefrompng(ROOT . 'image/notfound.png'); + $format = 'png'; + break; + case IMAGETYPE_PNG: + $source = imagecreatefrompng($local_path); + $format = 'png'; + break; + case IMAGETYPE_JPEG: + $source = imagecreatefromjpeg($local_path); + $format = 'jpg'; + break; + } + + // Get length and width of original image + $width_source = imagesx($source); + $height_source = imagesy($source); + if ($width_source > $height_source) + { + $width_destination = $size; + $height_destination = $size * $height_source / $width_source; + } + if ($width_source <= $height_source) + { + $height_destination = $size; + $width_destination = $size * $width_source / $height_source; + } + + // Create new canvas + $destination = imagecreatetruecolor($width_destination, $height_destination); + + // Preserve transparency + imagealphablending($destination, false); + imagesavealpha($destination, true); + $transparent_bg = imagecolorallocatealpha($destination, 255, 255, 255, 127); + imagefilledrectangle($destination, 0, 0, $width_destination, $height_destination, $transparent_bg); + + // Resize image + imagecopyresampled( + $destination, + $source, + 0, + 0, + 0, + 0, + $width_destination, + $height_destination, + $width_source, + $height_source + ); + + // Display image and cache the result + header('Cached-Image: false'); + if ($format === 'png') + { + header('Content-Type: image/png'); + imagepng($destination, CACHE_DIR . '/' . $cache_name, 9); + imagepng($destination, null, 9); + } + else + { + header("Content-Type: image/jpeg"); + imagejpeg($destination, CACHE_DIR . '/' . $cache_name, 90); + imagejpeg($destination, null, 90); + } + + // Create a record of the cached file + $orig_file_addon = File::getAddon($orig_file); + Cache::createFile($cache_name, $orig_file_addon, sprintf('w=%d,h=%d', $width_destination, $height_destination)); +} + +/** + * @param string $text + * + * @return string + */ +function img_label($text) +{ + $write_dir = UP_LOCATION . 'temp/'; + $read_dir = DOWN_LOCATION . 'temp/'; + $text_noaccent = preg_replace('/\W/s', '_', $text); + + if (!file_exists($write_dir . 'im_' . $text_noaccent . '.png')) + { + $text_size = 11; + $text_angle = 90; + $font = ROOT . 'include/DejaVuSans.ttf'; + $bbox = imagettfbbox($text_size, $text_angle, $font, $text); + + $min_x = min(array($bbox[0], $bbox[2], $bbox[4], $bbox[6])); + $max_x = max(array($bbox[0], $bbox[2], $bbox[4], $bbox[6])); + $min_y = min(array($bbox[1], $bbox[3], $bbox[5], $bbox[7])); + $max_y = max(array($bbox[1], $bbox[3], $bbox[5], $bbox[7])); + + $width = $max_x - $min_x + 2; + $height = $max_y - $min_y + 2; + + $image = imagecreatetruecolor($width, $height); + + $bgcolor = imagecolorallocate($image, 0, 0, 0); + imagecolortransparent($image, $bgcolor); + $textcolor = imagecolorallocate($image, 2, 2, 2); + + imagettftext($image, $text_size, $text_angle, $width, $height, $textcolor, $font, $text); + + imagepng($image, $write_dir . 'im_' . $text_noaccent . '.png'); + imagedestroy($image); + } + + return '' . htmlentities($text) . ''; +} + + /** * Class to hold all image-manipulation operations * * @author Stephen */ -class SImage { +class SImage +{ + /** + * @var + */ public $path; + + /** + * @var string + */ public $format; + + /** + * @var array + */ public $info; - private $imagedata; - - public function SImage($file) { + + /** + * @var resource + */ + private $imageData; + + /** + * @param string $file + * + * @throws ImageException + */ + public function __construct($file) + { if (!file_exists($file)) + { throw new ImageException('Image file not found.'); + } $this->path = $file; - $imageinfo = getimagesize($this->path); - switch ($imageinfo[2]) + $image_info = getimagesize($this->path); + switch ($image_info[2]) { default: throw new ImageException('Unsupported image format.'); @@ -50,80 +245,131 @@ public function SImage($file) { break; } - $this->info = $imageinfo; + $this->info = $image_info; $this->imagedata = $source; $this->format = $format; } /** * Write the current image to a file - * @param string $filepath Absolute file path + * + * @param string $file_path Absolute file path */ - public function save($filepath) { - if ($this->format == 'png') - imagepng($this->imagedata, $filepath); - elseif ($this->format == 'jpg') - imagejpeg($this->imagedata, $filepath); + public function save($file_path) + { + if ($this->format === 'png') + { + imagepng($this->imageData, $file_path); + } + elseif ($this->format === 'jpg') + { + imagejpeg($this->imageData, $file_path); + } } - + /** * Scale image based on given dimensions. - * @param integer $max_x Max width, or 0 to ignore - * @param integer $max_y Max height, or 0 to ignore - * @param integer $min_x > 1 - * @param integer $min_y > 1 + * + * @param int $max_x Max width, or 0 to ignore + * @param int $max_y Max height, or 0 to ignore + * @param int $min_x > 1 + * @param int $min_y > 1 + * + * @throws ImageException */ - public function scale($max_x,$max_y,$min_x = 1,$min_y = 1) { - if (($max_x < $min_x && $max_x !== 0) - || $max_y < $min_y && $max_y !== 0) + public function scale($max_x, $max_y, $min_x = 1, $min_y = 1) + { + if (($max_x < $min_x && $max_x !== 0) || $max_y < $min_y && $max_y !== 0) + { throw new ImageException('Maximum dimension is less than minimum dimension. Cannot scale image.'); + } $old_x = $this->info[0]; $old_y = $this->info[1]; - + if ($old_y == 0 || $old_x == 0) + { throw new ImageException('Image dimensions cannot be 0. Failed to scale image.'); - - if ($max_x === 0) { + } + + if ($max_x === 0) + { // Scale based on image height if ($old_y > $max_y) + { $new_y = $max_y; + } if ($old_y < $min_y) + { $new_y = $min_y; + } $new_x = (int)(($new_y / $old_y) * $old_x); if ($new_x < $min_x) + { $new_x = $min_x; - } elseif ($max_y === 0) { + } + } + elseif ($max_y === 0) + { // Scale based on image width if ($old_x > $max_x) + { $new_x = $max_x; + } if ($old_x < $min_x) + { $new_x = $min_x; + } $new_y = (int)(($new_x / $old_x) * $old_y); if ($new_y < $min_y) + { $new_y = $min_y; - } else { + } + } + else + { // Scale image based on both max dimensions - if($old_x > $old_y) { + if ($old_x > $old_y) + { if ($old_x > $max_x) + { $new_x = $max_x; + } if ($old_x < $min_x) + { $new_x = $min_x; + } + $new_y = (int)(($new_x / $old_x) * $old_y); if ($new_y < $min_y) + { $new_y = $min_y; + } if ($new_y > $max_y) + { $new_y = $max_y; - } else { + } + } + else + { if ($old_y > $max_y) + { $new_y = $max_y; + } if ($old_y < $min_y) + { $new_y = $min_y; + } + $new_x = (int)(($new_y / $old_y) * $old_x); if ($new_x < $min_x) + { $new_x = $min_x; + } if ($new_x > $max_x) + { $new_x = $max_x; + } } } @@ -137,8 +383,8 @@ public function scale($max_x,$max_y,$min_x = 1,$min_y = 1) { imagefilledrectangle($destination, 0, 0, $new_x, $new_y, $transparent_bg); // Resize image - imagecopyresampled($destination, $this->imagedata, 0, 0, 0, 0, $new_x, $new_y, $old_x, $old_y); - - $this->imagedata = $destination; + imagecopyresampled($destination, $this->imageData, 0, 0, 0, 0, $new_x, $new_y, $old_x, $old_y); + + $this->imageData = $destination; } } diff --git a/include/SLocale.class.php b/include/SLocale.class.php index a72284e8..1a9c9426 100644 --- a/include/SLocale.class.php +++ b/include/SLocale.class.php @@ -1,7 +1,7 @@ - * + * 2014 Daniel Butum * This file is part of stkaddons * * stkaddons is free software: you can redistribute it and/or modify @@ -17,70 +17,97 @@ * You should have received a copy of the GNU General Public License * along with stkaddons. If not, see . */ - // Note that PHP has a built-in Locale class in the newest versions of PHP -class SLocale { - + +/** + * Class SLocale + */ +class SLocale +{ + /** * Array of supported languages, format is: * language code, flag image x-offset, flag image y-offset, flag label * @var array */ private static $languages = array( - array('en_US',0,0,'EN'), - array('ca_ES',-96,-99,'CA'), - array('de_DE',0,-33,'DE'), - array('es_ES',-96,-66,'ES'), + array('en_US', 0, 0, 'EN'), + array('ca_ES', -96, -99, 'CA'), + array('de_DE', 0, -33, 'DE'), + array('es_ES', -96, -66, 'ES'), array('eu_ES', -144, -66, 'EU'), - array('fr_FR',0,-66,'FR'), - array('ga_IE',0,-99,'GA'), - array('gd_GB',-144, -33,'GD'), - array('gl_ES',-48,0,'GL'), - array('id_ID',-48,-33,'ID'), - array('it_IT',-96,-33,'IT'), - array('nl_NL',-48,-66,'NL'), - array('pt_BR',-144,0,'PT'), - array('ru_RU',-48,-99,'RU'), - array('zh_TW',-96,0,'ZH (T)') - ); - + array('fr_FR', 0, -66, 'FR'), + array('ga_IE', 0, -99, 'GA'), + array('gd_GB', -144, -33, 'GD'), + array('gl_ES', -48, 0, 'GL'), + array('id_ID', -48, -33, 'ID'), + array('it_IT', -96, -33, 'IT'), + array('nl_NL', -48, -66, 'NL'), + array('pt_BR', -144, 0, 'PT'), + array('ru_RU', -48, -99, 'RU'), + array('zh_TW', -96, 0, 'ZH (T)') + ); + + /** + * @var int + */ private static $cookie_lifetime = 31536000; // One year - + /** * Create the locale object + * * @param string $locale */ - public function __construct($locale = NULL) { - if ($locale == NULL && isset($_GET['lang']) && strlen($_GET['lang']) != 0) { + public function __construct($locale = null) + { + if ($locale == null && isset($_GET['lang']) && strlen($_GET['lang']) != 0) + { $locale = $_GET['lang']; - } elseif ($locale == NULL && isset($_COOKIE['lang'])) { + } + elseif ($locale == null && isset($_COOKIE['lang'])) + { $locale = $_COOKIE['lang']; - } elseif ($locale == NULL) + } + elseif ($locale == null) + { $locale = "en_US"; + } - if (!SLocale::IsValid($locale)) + if (!SLocale::isValid($locale)) + { die("Invalid locale"); - - SLocale::SetLocale($locale); + } + + SLocale::setLocale($locale); } - + /** * Check if locale is a valid value + * * @param string $locale + * * @return boolean */ - public static function IsValid($locale) { - foreach (SLocale::$languages AS $lang) { - if ($locale == $lang[0]) return true; + public static function isValid($locale) + { + foreach (SLocale::$languages AS $lang) + { + if ($locale == $lang[0]) + { + return true; + } } + return false; } - + /** * Set page locale + * * @param string $locale Locale string - input should already be checked */ - private static function SetLocale($locale) { + private static function setLocale($locale) + { // Set cookie setcookie('lang', $locale, time() + SLocale::$cookie_lifetime); putenv("LC_ALL=$locale.UTF-8"); @@ -88,15 +115,24 @@ private static function SetLocale($locale) { $_COOKIE['lang'] = $locale; // Set translation file info - bindtextdomain('translations', ROOT.'locale'); + bindtextdomain('translations', ROOT . 'locale'); textdomain('translations'); bind_textdomain_codeset('translations', 'UTF-8'); if (!defined('LANG')) + { define('LANG', $locale); + } } } +/** + * Macro function for htmlspecialchars(_($message)) + * + * @param string $message + * + * @return string + */ function _h($message) { return htmlspecialchars(_($message)); diff --git a/include/SMail.class.php b/include/SMail.class.php index e0aeb4d4..444fbd2a 100755 --- a/include/SMail.class.php +++ b/include/SMail.class.php @@ -1,6 +1,6 @@ - + 2014 Daniel Butum This file is part of stkaddons. stkaddons is free software: you can redistribute it and/or @@ -14,97 +14,166 @@ more details. You should have received a copy of the GNU General Public License along with - stkaddons. If not, see . */ + stkaddons. If not, see . +*/ // Include PEAR::Mail require_once('Mail.php'); -class SMail { +/** + * Class SMail + */ +class SMail +{ + /** + * @var Mail + */ private $factory; + + /** + * @var array + */ private $headers; - public function __construct() { - if (MAIL_METHOD == 'smtp') { - @$this->factory = Mail::factory(MAIL_METHOD,array( - 'host' => SMTP_HOST, - 'port' => SMTP_PORT, - 'auth' => SMTP_AUTH, - 'username' => SMTP_USER, - 'password' => SMTP_PASS - )); - } else { - @$this->factory = Mail::factory(MAIL_METHOD,array( - 'sendmail_path' => SENDMAIL_PATH, - 'sendmail_args' => SENDMAIL_ARGS - )); - } - $this->headers = array( - 'From' => '"STK-Addons Administrator" <'.ConfigManager::getConfig('admin_email').'>', - 'Content-Type' => 'text/plain; charset="UTF-8"', - 'Content-Transfer-Encoding' => '8bit' - ); + /** + * The constructor + */ + public function __construct() + { + if (MAIL_METHOD == 'smtp') + { + @$this->factory = Mail::factory( + MAIL_METHOD, + array( + 'host' => SMTP_HOST, + 'port' => SMTP_PORT, + 'auth' => SMTP_AUTH, + 'username' => SMTP_USER, + 'password' => SMTP_PASS + ) + ); + } + else + { + @$this->factory = Mail::factory( + MAIL_METHOD, + array( + 'sendmail_path' => SENDMAIL_PATH, + 'sendmail_args' => SENDMAIL_ARGS + ) + ); + } + $this->headers = array( + 'From' => '"STK-Addons Administrator" <' . ConfigManager::getConfig('admin_email') . '>', + 'Content-Type' => 'text/plain; charset="UTF-8"', + 'Content-Transfer-Encoding' => '8bit' + ); } - - public function newAccountNotification($email, $userid, $username, $ver_code, $ver_page) { - $message = "Thank you for registering an account on the SuperTuxKart Add-Ons Manager.\n". - "Please go to " . SITE_ROOT . "$ver_page?action=valid&num=$ver_code&user=$userid to activate your account.\n\n". - "Username: $username"; - $subject = "New Account at ".$_SERVER["SERVER_NAME"]; - - $this->headers['To'] = $email; - $this->headers['Subject'] = $subject; - $result = @$this->factory->send($email, $this->headers, $message); - if (@PEAR::isError($result)) - throw new Exception($result->getMessage()); + + /** + * @param string $email + * @param int $userid + * @param string $username + * @param string $ver_code + * @param string $ver_page + * + * @throws Exception + */ + public function newAccountNotification($email, $userid, $username, $ver_code, $ver_page) + { + $message = "Thank you for registering an account on the SuperTuxKart Add-Ons Manager.\n" . + "Please go to " . SITE_ROOT . "$ver_page?action=valid&num=$ver_code&user=$userid to activate your account.\n\n" . + "Username: $username"; + $subject = "New Account at " . $_SERVER["SERVER_NAME"]; + + $this->headers['To'] = $email; + $this->headers['Subject'] = $subject; + $result = @$this->factory->send($email, $this->headers, $message); + if (@PEAR::isError($result)) + { + throw new Exception($result->getMessage()); + } } - - public function passwordResetNotification($email, $userid, $username, $ver_code, $ver_page) { - $message = "You have requested to reset your password on the SuperTuxKart Add-Ons Manager.\n". - "Please go to http://{$_SERVER["SERVER_NAME"]}/$ver_page?action=valid&num=$ver_code&user=$userid to reset your password.\n\n". - "Username: $username"; - $subject = "Reset Password on ".$_SERVER["SERVER_NAME"]; - - $this->headers['To'] = $email; - $this->headers['Subject'] = $subject; - $result = @$this->factory->send($email, $this->headers, $message); - if (@PEAR::isError($result)) - throw new Exception($result->getMessage()); + + /** + * @param string $email + * @param int $userid + * @param string $username + * @param string $ver_code + * @param string $ver_page + * + * @throws Exception + */ + public function passwordResetNotification($email, $userid, $username, $ver_code, $ver_page) + { + $message = "You have requested to reset your password on the SuperTuxKart Add-Ons Manager.\n" . + "Please go to http://{$_SERVER["SERVER_NAME"]}/$ver_page?action=valid&num=$ver_code&user=$userid to reset your password.\n\n" . + "Username: $username"; + $subject = "Reset Password on " . $_SERVER["SERVER_NAME"]; + + $this->headers['To'] = $email; + $this->headers['Subject'] = $subject; + $result = @$this->factory->send($email, $this->headers, $message); + if (@PEAR::isError($result)) + { + throw new Exception($result->getMessage()); + } } - - public function addonNoteNotification($email, $addon_id, $notes) { - $addon_name = Addon::getName($addon_id); - $message = "A moderator has left a note concerning your add-on, '$addon_name.' The notes saved for each revision of this add-on are shown below.\n\n"; - $message .= $notes; - $subject = "New message for add-on '$addon_name'"; - - $this->headers['To'] = $email; - $this->headers['Subject'] = $subject; - $result = @$this->factory->send($email, $this->headers, $message); - if (@PEAR::isError($result)) - throw new Exception($result->getMessage()); + + /** + * @param string $email + * @param string $addon_id + * @param string $notes + * + * @throws Exception + */ + public function addonNoteNotification($email, $addon_id, $notes) + { + $addon_name = Addon::getName($addon_id); + $message = + "A moderator has left a note concerning your add-on, '$addon_name.' The notes saved for each revision of this add-on are shown below.\n\n"; + $message .= $notes; + $subject = "New message for add-on '$addon_name'"; + + $this->headers['To'] = $email; + $this->headers['Subject'] = $subject; + $result = @$this->factory->send($email, $this->headers, $message); + if (@PEAR::isError($result)) + { + throw new Exception($result->getMessage()); + } } } +/** + * @param string $subject + * @param string $message_html + * + * @return null + */ function moderator_email($subject, $message_html) { $mail_address = ConfigManager::getConfig('list_email'); - if (strlen($mail_address) == 0) + if (empty($mail_address)) { - echo ''.htmlspecialchars(_('No moderator mailing-list email is set.')).'
'; + echo '' . htmlspecialchars( + _('No moderator mailing-list email is set.') + ) . '
'; + return null; } - $boundary = "-----=".md5(rand()); - $header = "From: \"STK-Addons Administrator\" <".ConfigManager::getConfig('admin_email').">\n" - ."Reply-to: \"STK-Addons Administrator\" <".ConfigManager::getConfig('admin_email').">\n" - ."MIME-Version: 1.0\n" - ."Content-Type: multipart/alternative;\n boundary=\"$boundary\"\n"; - $message = "\n--".$boundary."\n" - ."Content-Type: text/html; charset=\"ISO-8859-1\"\n" - ."Content-Transfer-Encoding: 8bit\n" - ."\n".$message_html."\n" - ."\n--".$boundary."--\n" - ."\n--".$boundary."--\n"; - mail($mail_address,$subject,$message,$header); + $boundary = "-----=" . md5(rand()); + $header = "From: \"STK-Addons Administrator\" <" . ConfigManager::getConfig('admin_email') . ">\n" + . "Reply-to: \"STK-Addons Administrator\" <" . ConfigManager::getConfig('admin_email') . ">\n" + . "MIME-Version: 1.0\n" + . "Content-Type: multipart/alternative;\n boundary=\"$boundary\"\n"; + $message = "\n--" . $boundary . "\n" + . "Content-Type: text/html; charset=\"ISO-8859-1\"\n" + . "Content-Transfer-Encoding: 8bit\n" + . "\n" . $message_html . "\n" + . "\n--" . $boundary . "--\n" + . "\n--" . $boundary . "--\n"; + mail($mail_address, $subject, $message, $header); } diff --git a/include/image.php b/include/image.php deleted file mode 100644 index a234e57b..00000000 --- a/include/image.php +++ /dev/null @@ -1,152 +0,0 @@ - - * - * This file is part of stkaddons - * - * stkaddons is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * stkaddons is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with stkaddons. If not, see . - */ - -function resizeImage($file) -{ - // Determine image size - $type = (isset($_GET['type'])) ? $_GET['type'] : NULL; - switch ($type) { - default: - $size = 100; - break; - case 'small': - $size = 25; - break; - case 'medium': - $size = 75; - break; - case 'big': - $size = 300; - break; - } - $cache_name = $size.'--'.basename($file); - $local_path = UP_LOCATION.$file; - - // Check if image exists, and if it does, check its format - $orig_file = File::exists($file); - if ($orig_file === -1) - { - if (!file_exists(ROOT.$file)) { - header('HTTP/1.1 404 Not Found'); - return; - } else - $local_path = ROOT.$file; - } - - // Check if a cached version is available - if (Cache::fileExists($cache_name) !== array()) { - header('Cached-Image: true'); - header('Location: '.CACHE_DL.'/'.$cache_name); - return; - } - - // Start processing the original file - $imageinfo = @getimagesize($local_path); - switch ($imageinfo[2]) - { - default: - $source = imagecreatefrompng(ROOT.'image/notfound.png'); - $format = 'png'; - break; - case IMAGETYPE_PNG: - $source = imagecreatefrompng($local_path); - $format = 'png'; - break; - case IMAGETYPE_JPEG: - $source = imagecreatefromjpeg($local_path); - $format = 'jpg'; - break; - } - - // Get length and width of original image - $width_source = imagesx($source); - $height_source = imagesy($source); - if($width_source > $height_source) - { - $width_destination = $size; - $height_destination = $size*$height_source/$width_source; - } - if($width_source <= $height_source) - { - $height_destination = $size; - $width_destination = $size*$width_source/$height_source; - } - // Create new canvas - $destination = imagecreatetruecolor($width_destination, $height_destination); - - // Preserve transparency - imagealphablending($destination, false); - imagesavealpha($destination, true); - $transparent_bg = imagecolorallocatealpha($destination, 255, 255, 255, 127); - imagefilledrectangle($destination, 0, 0, $width_destination, $height_destination, $transparent_bg); - - // Resize image - imagecopyresampled($destination, $source, 0, 0, 0, 0, $width_destination, $height_destination, $width_source, $height_source); - // Display image and cache the result - header('Cached-Image: false'); - if ($format === 'png') { - header('Content-Type: image/png'); - imagepng($destination,CACHE_DIR.'/'.$cache_name,9); - imagepng($destination,NULL,9); - } else { - header("Content-Type: image/jpeg"); - imagejpeg($destination,CACHE_DIR.'/'.$cache_name,90); - imagejpeg($destination,NULL,90); - } - - // Create a record of the cached file - $orig_file_addon = File::getAddon($orig_file); - Cache::createFile($cache_name, $orig_file_addon, sprintf('w=%d,h=%d',$width_destination,$height_destination)); -} - -function img_label($text) -{ - $write_dir = UP_LOCATION.'temp/'; - $read_dir = DOWN_LOCATION.'temp/'; - $text_noaccent = preg_replace('/\W/s','_',$text); - - if (!file_exists($write_dir.'im_'.$text_noaccent.'.png')) - { - $text_size = 11; - $text_angle = 90; - $font = ROOT.'include/DejaVuSans.ttf'; - $bbox = imagettfbbox($text_size, $text_angle, $font, $text); - - $min_x = min(array($bbox[0],$bbox[2],$bbox[4],$bbox[6])); - $max_x = max(array($bbox[0],$bbox[2],$bbox[4],$bbox[6])); - $min_y = min(array($bbox[1],$bbox[3],$bbox[5],$bbox[7])); - $max_y = max(array($bbox[1],$bbox[3],$bbox[5],$bbox[7])); - - $width = $max_x - $min_x + 2; - $height = $max_y - $min_y + 2; - - $image = imagecreatetruecolor($width, $height); - - $bgcolor = imagecolorallocate($image,0,0,0); - imagecolortransparent($image, $bgcolor); - $textcolor = imagecolorallocate($image,2,2,2); - - imagettftext($image, $text_size, $text_angle, $width, $height, $textcolor, $font, $text); - - imagepng($image,$write_dir.'im_'.$text_noaccent.'.png'); - imagedestroy($image); - } - return ''.htmlentities($text).''; -} diff --git a/include/locale.php b/include/locale.php index f2f96cf3..a9020879 100644 --- a/include/locale.php +++ b/include/locale.php @@ -21,12 +21,14 @@ // Get the current page address (without "lang" parameter) $page_url = $_SERVER['REQUEST_URI']; -if (strstr($page_url,'?') === false) - $page_url .= '?'; +if (strstr($page_url, '?') === false) +{ + $page_url .= '?'; +} // Clean up the new url -$page_url = preg_replace('/lang=[a-z_]+/i',NULL,$page_url); -$page_url = preg_replace('/[&]+/i','&',$page_url); -$page_url = preg_replace('/\?&/i','?',$page_url); +$page_url = preg_replace('/lang=[a-z_]+/i', null, $page_url); +$page_url = preg_replace('/[&]+/i', '&', $page_url); +$page_url = preg_replace('/\?&/i', '?', $page_url); // Set the locale new SLocale(); diff --git a/music.php b/music.php index 17c3ade4..e778ab83 100644 --- a/music.php +++ b/music.php @@ -25,27 +25,30 @@ require_once(INCLUDE_DIR . 'StkTemplate.class.php'); $tpl = new StkTemplate('music-browser.tpl'); -$tpl->assign('title', htmlspecialchars(_('STK Add-ons').' | '._('Browse Music'))); +$tpl->assign('title', htmlspecialchars(_('STK Add-ons') . ' | ' . _('Browse Music'))); $music_tracks = Music::getAllByTitle(); $music_data = array(); -foreach ($music_tracks AS $track) { +foreach ($music_tracks as $track) +{ $music_data[] = $track->getTitle(); $music_data[] = $track->getArtist(); $music_data[] = $track->getLicense(); - $link = ''.$track->getFile().''; + $link = '' . $track->getFile() . ''; $music_data[] = $link; } -$tpl->assign('music_browser', array( - 'heading' => htmlspecialchars(_('Browse Music')), - 'cols' => array( - htmlspecialchars(_('Track Title')), - htmlspecialchars(_('Track Artist')), - htmlspecialchars(_('License')), - htmlspecialchars(_('File')) - ), - 'data' => $music_data -)); +$tpl->assign( + 'music_browser', + array( + 'cols' => array( + htmlspecialchars(_('Track Title')), + htmlspecialchars(_('Track Artist')), + htmlspecialchars(_('License')), + htmlspecialchars(_('File')) + ), + 'data' => $music_data + ) +); echo $tpl; diff --git a/privacy.php b/privacy.php index 24c7c7ef..6d9d5f5d 100644 --- a/privacy.php +++ b/privacy.php @@ -19,11 +19,11 @@ */ 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 = StkTemplate('info-page.tpl'); -$tpl->assign('title', htmlspecialchars(_('STK Add-ons').' | '._('Privacy'))); +$tpl = new StkTemplate('info-page.tpl'); +$tpl->assign('title', htmlspecialchars(_('STK Add-ons') . ' | ' . _('Privacy'))); $page_content = <<< EOF

Privacy Policy

diff --git a/tpl/default/music-browser.tpl b/tpl/default/music-browser.tpl index 846d1b72..a8b509b0 100644 --- a/tpl/default/music-browser.tpl +++ b/tpl/default/music-browser.tpl @@ -2,7 +2,7 @@ {include file=#header#}
-

{$music_browser.heading}

+

{t}Browse Music{/t}

{html_table loop=$music_browser.data cols=$music_browser.cols table_attr='border="0"'}
{* #content *}