diff --git a/image.php b/image.php
index 009abe34..cf95b162 100644
--- a/image.php
+++ b/image.php
@@ -17,8 +17,8 @@
* You should have received a copy of the GNU General Public License
* along with stkaddons. If not, see .
*/
-
require_once(__DIR__ . DIRECTORY_SEPARATOR . "config.php");
-$type = (isset($_GET['type'])) ? $_GET['type'] : null;
-Util::resizeImage($_GET['pic'], $type);
+$size = empty($_GET['size']) ? null : $_GET['size'];
+$file = empty($_GET['pic']) ? null : $_GET['pic'];
+Util::resizeImage($file, (int)$size);
diff --git a/include/Addon.class.php b/include/Addon.class.php
index 1d7374eb..8b7416e6 100644
--- a/include/Addon.class.php
+++ b/include/Addon.class.php
@@ -1483,7 +1483,7 @@ public static function filterMenuTemplate($addons, $type)
// Make sure an icon file is set for kart
if ($addon->getImage(true) != 0)
{
- $im = Cache::getImage($addon->getImage(true), ['size' => 'small']);
+ $im = Cache::getImage($addon->getImage(true), SImage::SIZE_SMALL);
if ($im['exists'] && $im['approved'])
{
$icon = $im['url'];
diff --git a/include/AddonViewer.class.php b/include/AddonViewer.class.php
index eeeaaf17..acbd885b 100755
--- a/include/AddonViewer.class.php
+++ b/include/AddonViewer.class.php
@@ -100,7 +100,7 @@ public function fillTemplate($template)
}
// Get image url
- $image = Cache::getImage($this->addon->getImage(), ['size' => 'big']);
+ $image = Cache::getImage($this->addon->getImage(), SImage::SIZE_BIG);
if ($this->addon->getImage() != 0 && $image['exists'] == true && $image['approved'] == true)
{
$tpl['image_url'] = $image['url'];
@@ -181,7 +181,7 @@ public function fillTemplate($template)
$image_files_db = $this->addon->getImages();
foreach ($image_files_db as $image)
{
- $imageCache = Cache::getImage($image['id'], ['size' => 'medium']);
+ $imageCache = Cache::getImage($image['id'], SImage::SIZE_MEDIUM);
$image['thumb']['url'] = $imageCache['url'];
$image['url'] = DOWNLOAD_LOCATION . $image['file_path'];
$image['approved'] = (bool)$image['approved'];
diff --git a/include/Cache.class.php b/include/Cache.class.php
index 55e9ec0b..989548f8 100644
--- a/include/Cache.class.php
+++ b/include/Cache.class.php
@@ -27,14 +27,16 @@
class Cache
{
+
/**
* Empty the cache folder, leave certain files in place
*
+ * @param string $exclude_regex files to exclude regex
+ *
* @return bool
*/
- public static function clear()
+ public static function clear($exclude_regex = '/^(cache_graph_.*\.png)$/i')
{
- $exclude_regex = '/^(cache_graph_.*\.png)$/i';
File::deleteDir(CACHE_PATH, $exclude_regex);
mkdir(CACHE_PATH);
@@ -51,12 +53,8 @@ public static function clear()
{
continue;
}
- DBConnection::get()->query(
- 'DELETE FROM `' . DB_PREFIX . 'cache`
- WHERE `file` = :file',
- DBConnection::NOTHING,
- [':file' => $cache_item['file']]
- );
+
+ DBConnection::get()->delete("cache", "`file` = :file", [':file' => $cache_item['file']]);
}
}
catch(DBException $e)
@@ -70,7 +68,7 @@ public static function clear()
/**
* Clear the addon cache files
*
- * @param string $addon
+ * @param string $addon the addon id
*
* @return bool
*/
@@ -94,20 +92,16 @@ public static function clearAddon($addon)
foreach ($cache_list AS $cache_item)
{
unlink(CACHE_PATH . $cache_item['file']);
- DBConnection::get()->query(
- 'DELETE FROM `' . DB_PREFIX . 'cache`
- WHERE `file` = :file',
- DBConnection::NOTHING,
- [':file' => $cache_item['file']]
- );
- }
- return true;
+ DBConnection::get()->delete("cache", "`file` = :file", [':file' => $cache_item['file']]);
+ }
}
catch(DBException $e)
{
return false;
}
+
+ return true;
}
/**
@@ -177,22 +171,22 @@ public static function fileExists($path)
/**
* Get image properties for a cacheable image
*
- * @param integer $id
- * @param array $props
+ * @param int $id the id of the file
+ * @param int $size image size, see SImage::SIZE_
*
* @return array
* @throws CacheException
*/
- public static function getImage($id, $props = [])
+ public static function getImage($id, $size = null)
{
try
{
- $result = DBConnection::get()->query(
+ $file = DBConnection::get()->query(
'SELECT `file_path`, `approved`
FROM `' . DB_PREFIX . 'files`
WHERE `id` = :id
LIMIT 1',
- DBConnection::FETCH_ALL,
+ DBConnection::FETCH_FIRST,
[':id' => $id],
[':id' => DBConnection::PARAM_INT]
);
@@ -202,7 +196,8 @@ public static function getImage($id, $props = [])
throw new CacheException('Failed to look up image file.');
}
- if (empty($result))
+ // image does with tha id does not exist in the database
+ if (empty($file))
{
return [
'url' => IMG_LOCATION . 'notfound.png',
@@ -212,22 +207,21 @@ public static function getImage($id, $props = [])
}
$return = [
- 'url' => DOWNLOAD_LOCATION . $result[0]['file_path'],
- 'approved' => (bool)$result[0]['approved'],
+ 'url' => DOWNLOAD_LOCATION . $file['file_path'],
+ 'approved' => (bool)$file['approved'],
'exists' => true
];
- $cache_prefix = null;
- if (array_key_exists('size', $props))
- {
- $cache_prefix = Cache::cachePrefix($props['size']);
+ $cache_prefix = $size ? Cache::cachePrefix($size) : "";
- $return['url'] = SITE_ROOT . 'image.php?type=' . $props['size'] . '&pic=' . $result[0]['file_path'];
+ // image exists in cache
+ if (Cache::fileExists($cache_prefix . basename($file['file_path'])))
+ {
+ $return['url'] = CACHE_LOCATION . $cache_prefix . basename($file['file_path']);
}
-
- if (Cache::fileExists($cache_prefix . basename($result[0]['file_path'])))
+ else // create new cache by resizing the image
{
- $return['url'] = CACHE_LOCATION . $cache_prefix . basename($result[0]['file_path']);
+ $return['url'] = SITE_ROOT . 'image.php?size=' . $size . '&pic=' . $file['file_path'];
}
return $return;
@@ -236,27 +230,28 @@ public static function getImage($id, $props = [])
/**
* @param string $size
*
- * @return null|string
+ * @return string
*/
- private static function cachePrefix($size)
+ public static function cachePrefix($size)
{
- if (empty($size))
+ if (!$size)
{
- return null;
+ return '';
}
- if ($size === 'big')
+
+ if ($size === SImage::SIZE_BIG)
{
return '300--';
}
- if ($size === 'medium')
+ if ($size === SImage::SIZE_MEDIUM)
{
return '75--';
}
- if ($size === 'small')
+ if ($size === SImage::SIZE_SMALL)
{
return '25--';
}
- return null;
+ return '100--';
}
}
diff --git a/include/File.class.php b/include/File.class.php
index 70673e75..3ee9e68f 100644
--- a/include/File.class.php
+++ b/include/File.class.php
@@ -113,7 +113,7 @@ public static function exists($path)
return 0;
}
- if (empty($files))
+ if (empty($file))
{
return 0;
}
@@ -771,9 +771,8 @@ public static function getAllFiles()
}
$return_files[] = $db_file;
}
- // fs_files now contains only files that do not exist in the database
- // and exist only on disk
- $fs_files = array_values($fs_files);
+ // fs_files now contains only files that do not exist in the database and exist only on disk
+ $fs_files = array_values($fs_files); // reset indices
// add files that exist on the disk but not in the database
foreach ($fs_files as $file_path)
diff --git a/include/SImage.class.php b/include/SImage.class.php
index 37fd8011..1048e3fc 100644
--- a/include/SImage.class.php
+++ b/include/SImage.class.php
@@ -25,6 +25,21 @@
*/
class SImage
{
+ /**
+ * @const int
+ */
+ const SIZE_SMALL = 1;
+
+ /**
+ * @const int
+ */
+ const SIZE_MEDIUM = 2;
+
+ /**
+ * @const int
+ */
+ const SIZE_BIG = 3;
+
/**
* @var string
*/
diff --git a/include/Util.class.php b/include/Util.class.php
index 875c0245..5b16d616 100644
--- a/include/Util.class.php
+++ b/include/Util.class.php
@@ -188,7 +188,7 @@ public static function array_flatten(array $array, $preserve_keys = true)
/**
* A time is old enough if the current time is greater than the user time + the max age
*
- * @param int $time current time in seconds
+ * @param int $time current time in seconds
* @param int $max_age max time in seconds
*
* @return bool
@@ -346,7 +346,7 @@ public static function getQueryVars($query)
foreach ($hashes as $hash)
{
$hash = explode("=", $hash);
- // key => balue
+ // key => value
$vars[$hash[0]] = $hash[1];
}
@@ -609,46 +609,71 @@ public static function getRandomString(
/**
* Resize an image, and send the new resized image to the user with http headers
*
- * @param string $file
- * @param string|null the type of image
+ * @param string $file
+ * @param int $orig_size the size of the image
*
* @return null
*/
- public static function resizeImage($file, $type = null)
+ public static function resizeImage($file, $orig_size = null)
{
+ // file is invalid
+ if (!$file)
+ {
+ header('HTTP/1.1 404 Not Found');
+ if (DEBUG_MODE)
+ {
+ echo "file is empty";
+ }
+
+ return;
+ }
+
// Determine image size
- switch ($type)
+ switch ($orig_size)
{
- case 'small':
+ case SImage::SIZE_SMALL:
$size = 25;
break;
- case 'medium':
+
+ case SImage::SIZE_MEDIUM:
$size = 75;
break;
- case 'big':
+
+ case SImage::SIZE_BIG:
$size = 300;
break;
+
default:
$size = 100;
break;
}
- $cache_name = $size . '--' . basename($file);
- $local_path = UP_PATH . $file;
- // Check if image exists, and if it does, check its format
+ $cache_name = Cache::cachePrefix($orig_size) . basename($file);
+ $local_path = UP_PATH . $file; // all images should be in our upload directory
+
+ // Check if image exists in the database
$orig_file = File::exists($file);
- if ($orig_file)
+ if (!$orig_file)
{
- if (!file_exists(ROOT_PATH . $file))
+ header('HTTP/1.1 404 Not Found');
+ if (DEBUG_MODE)
{
- header('HTTP/1.1 404 Not Found');
-
- return;
+ echo sprintf("%s does not exist in the database", $file);
}
- else
+
+ return;
+ }
+
+ // file does not exist on disk
+ if (!file_exists($local_path))
+ {
+ header('HTTP/1.1 404 Not Found');
+ if (DEBUG_MODE)
{
- $local_path = ROOT_PATH . $file;
+ echo sprintf("%s does not exist on the disk", $file);
}
+
+ return;
}
// Check if a cached version is available
@@ -661,21 +686,23 @@ public static function resizeImage($file, $type = null)
}
// Start processing the original file
- $image_info = @getimagesize($local_path);
+ $image_info = getimagesize($local_path);
switch ($image_info[2])
{
- default:
- $source = imagecreatefrompng(IMG_LOCATION . '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;
+
+ default:
+ $source = imagecreatefrompng(IMG_LOCATION . 'notfound.png');
+ $format = 'png';
+ break;
}
// Get length and width of original image
diff --git a/install/table.sql b/install/table.sql
index 55f1ddf9..9ddd0a67 100644
--- a/install/table.sql
+++ b/install/table.sql
@@ -141,7 +141,7 @@ CREATE TABLE IF NOT EXISTS `v2_arenas_revs` (
--
CREATE TABLE IF NOT EXISTS `v2_cache` (
- `file` VARCHAR(30) NOT NULL,
+ `file` VARCHAR(128) NOT NULL,
`addon` VARCHAR(30) DEFAULT NULL,
`props` TEXT,
UNIQUE KEY `file` (`file`)
diff --git a/install/table_old_to_new.sql b/install/table_old_to_new.sql
index 44d715e2..6926928f 100644
--- a/install/table_old_to_new.sql
+++ b/install/table_old_to_new.sql
@@ -90,7 +90,7 @@ CREATE PROCEDURE `convert_to_utf8`()
ALTER TABLE `v2_cache` ENGINE = InnoDB;
ALTER TABLE `v2_cache` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
- ALTER TABLE `v2_cache` CHANGE `file` `file` VARCHAR( 30 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL ;
+ ALTER TABLE `v2_cache` CHANGE `file` `file` VARCHAR( 128 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL ;
ALTER TABLE `v2_cache` CHANGE `addon` `addon` VARCHAR( 30 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL ;
ALTER TABLE `v2_cache` CHANGE `props` `props` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL ;
diff --git a/manage-panel.php b/manage-panel.php
index 46c69ee1..23a00d36 100644
--- a/manage-panel.php
+++ b/manage-panel.php
@@ -75,7 +75,7 @@
{
if ($image["approved"] == 0)
{
- $unapproved[] = '';
+ $unapproved[] = '';
}
}
// add to view
diff --git a/tpl/default/addons-panel.tpl b/tpl/default/addons-panel.tpl
index 524a682b..1a157b85 100644
--- a/tpl/default/addons-panel.tpl
+++ b/tpl/default/addons-panel.tpl
@@ -70,8 +70,7 @@
{if $addon.dl}
-
- {t}Download this add-on in game!{/t}
+
{t}Download this add-on in game!{/t}
{/if}
{t}License{/t}