28 Commits

Author SHA1 Message Date
187a7cd02f GenericTable: prevent passing NULL to strtotime 2022-07-14 16:45:32 +02:00
8414843bbf Prevent current page from being 0 if no items are present 2022-07-14 16:45:17 +02:00
474c387786 Add double-density thumbnails to albums and photo pages 2022-07-08 23:53:28 +02:00
12407d797d Address deprecation notices for certain function signatures 2022-07-08 23:52:03 +02:00
64d7433a56 Thumbnails: crop from original size if 2x is unavailable 2022-07-08 23:49:29 +02:00
58b7204fbf Do not delete thumbnail queue when replacing an asset
Thumbnails are normally created on demand, e.g. when processing the format codes in a post's body text.
Normally, the temporary URL is only used once to generate thumbnails ad-hoc. However, when cache is
enabled, a reference to the asset may be used in a cached version of a formatted body text, skipping
the normal thumbnail generation routine.

When an asset is replaced, currently, all thumbnails are removed and references to them are removed
from the database. In case the asset is still referenced in a cached formatted body text, this could lead
to an error when requesting the thumbnail, as the thumbnail request is no longer present in the system.

As we do not know what posts use particular assets at this point in the code, it is best to work around this
issue by unsetting the thumbnail filenames rather than deleting the entries outright. This effectively
generates them again on the next request.

In the future, we should aim to keep track of what posts make use of assets, so cache may be invalidated
in a more targeted way.
2022-07-08 23:49:20 +02:00
36a2779381 Don't try to generate double-density thumbs for small images 2022-07-08 23:49:13 +02:00
44bb501d13 Write new thumbnail filenames to parent Image object as well 2022-07-08 23:48:53 +02:00
9010123d18 Thumbnail class: minor refactor of generate method 2022-07-08 23:48:45 +02:00
e3b67c4022 Thumbnail class: refactor getUrl method 2022-07-08 23:48:38 +02:00
2bcdc5fe6e Split Image::getImageUrls from Image::getInlineImage 2022-07-08 23:48:30 +02:00
edfad992cc Rewrite Image::getInlineImage to support double density displays 2022-07-08 23:48:19 +02:00
357d95f6ff Add Image::getInlineImage method 2022-07-08 23:47:55 +02:00
0ec0de4414 Replace deprecated strftime calls 2022-05-07 13:25:19 +02:00
69417c36ed Merge pull request 'EXIF: prefer DateTimeOriginal over DateTimeDigitized' (#27) from exif-date-time-original into master
Reviewed-on: Public/pics#27
2022-02-16 21:56:04 +01:00
f2d8a32e67 EXIF: prefer DateTimeOriginal over DateTimeDigitized 2022-02-16 21:43:55 +01:00
4863561129 Merge pull request 'Refactor generic tables and page index classes' (#26) from refactor-tables into master
Reviewed-on: Public/pics#26
2021-05-17 20:19:18 +02:00
8474d3b2b2 Merge pull request 'Modernise autosuggest code' (#25) from autosuggest into master
Reviewed-on: Public/pics#25
2021-05-17 20:19:05 +02:00
3bf69fd21f Prevent XSS in error log viewer. 2021-03-10 17:40:06 +01:00
237f4005bd Apply htmlspecialchars to basic values. 2021-02-17 22:44:26 +01:00
4bf4641428 Minor refactor of generateColumnHeaders function 2021-02-17 20:45:58 +01:00
ff808ba18d Refactor processing of raw rows and their cell data. 2021-02-17 20:45:22 +01:00
6c662481bc Remove a few obscure, unused features.
* Removed support for row classification. Use of CSS is preferred.
* Removed support for disabling/enabling columns via a property. Unset as needed.
* Removed support for passing and inheriting a cell width by column. Header width suffices.
2021-02-17 20:45:22 +01:00
af73f00701 Restrict access to GenericTable, Pagination class members. 2021-02-17 20:45:21 +01:00
681af07985 Pass builder function for custom pagination link generation.
This makes pagination for GenericTables work properly again.
2021-02-17 20:43:30 +01:00
cba42a9129 Refactor GenericTable to use PageIndex rather than inherit from it
This has on my todo list for years... I'm glad to finally get around to it.
2021-02-17 20:43:30 +01:00
96937b6952 Use null-coalescing operator where appropriate 2021-02-17 20:43:29 +01:00
5c55e45c3c Use splat operator instead of call_user_func_array 2021-02-17 20:43:29 +01:00
11 changed files with 279 additions and 211 deletions

View File

@@ -47,9 +47,13 @@ class ManageErrors extends HTMLController
'parse' => [
'type' => 'function',
'data' => function($row) {
return $row['message'] . '<br><div><a onclick="this.parentNode.childNodes[1].style.display=\'block\';this.style.display=\'none\';">Show debug info</a>' .
'<pre style="display: none">' . $row['debug_info'] . '</pre></div>' .
'<small><a href="' . BASEURL . $row['request_uri'] . '">' . $row['request_uri'] . '</a></small>';
return $row['message'] . '<br>' .
'<div><a onclick="this.parentNode.childNodes[1].style.display=\'block\';this.style.display=\'none\';">Show debug info</a>' .
'<pre style="display: none">' . htmlspecialchars($row['debug_info']) .
'</pre></div>' .
'<small><a href="' . BASEURL .
htmlspecialchars($row['request_uri']) . '">' .
htmlspecialchars($row['request_uri']) . '</a></small>';
}
],
'header' => 'Message / URL',
@@ -85,7 +89,7 @@ class ManageErrors extends HTMLController
'header' => 'UID',
'is_sortable' => true,
'parse' => [
'link' => BASEURL . '/member/?id={ID_USER}',
'link' => BASEURL . '/edituser/?id={ID_USER}',
'data' => 'id_user',
],
],

View File

@@ -488,7 +488,7 @@ class Database
/**
* This function can be used to insert data into the database in a secure way.
*/
public function insert($method = 'replace', $table, $columns, $data)
public function insert($method, $table, $columns, $data)
{
// With nothing to insert, simply return.
if (empty($data))

View File

@@ -96,7 +96,9 @@ class EXIF
elseif (!empty($exif['Make']))
$meta['camera'] = trim($exif['Make']);
if (!empty($exif['DateTimeDigitized']))
if (!empty($exif['DateTimeOriginal']))
$meta['created_timestamp'] = self::toUnixTime($exif['DateTimeOriginal']);
elseif (!empty($exif['DateTimeDigitized']))
$meta['created_timestamp'] = self::toUnixTime($exif['DateTimeDigitized']);
return new self($meta);

View File

@@ -6,19 +6,16 @@
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class GenericTable extends PageIndex
class GenericTable
{
protected $header = [];
protected $body = [];
protected $page_index = [];
private $header = [];
private $body = [];
private $pageIndex = null;
private $currentPage = 1;
protected $title;
protected $title_class;
protected $tableIsSortable = false;
protected $recordCount;
protected $needsPageIndex = false;
protected $current_page;
protected $num_pages;
private $title;
private $title_class;
private $tableIsSortable = false;
public $form_above;
public $form_below;
@@ -36,34 +33,32 @@ class GenericTable extends PageIndex
// Make sure we know whether we can actually sort on something.
$this->tableIsSortable = !empty($options['base_url']);
// How much stuff do we have?
$this->recordCount = call_user_func_array($options['get_count'], !empty($options['get_count_params']) ? $options['get_count_params'] : []);
// How much data do we have?
$this->recordCount = $options['get_count'](...(!empty($options['get_count_params']) ? $options['get_count_params'] : []));
// Should we create a page index?
// How much data do we need to retrieve?
$this->items_per_page = !empty($options['items_per_page']) ? $options['items_per_page'] : 30;
$this->needsPageIndex = !empty($this->items_per_page) && $this->recordCount > $this->items_per_page;
$this->index_class = isset($options['index_class']) ? $options['index_class'] : '';
// Figure out where to start.
$this->start = empty($options['start']) || !is_numeric($options['start']) || $options['start'] < 0 || $options['start'] > $this->recordCount ? 0 : $options['start'];
// Figure out where we are on the whole, too.
$numPages = max(1, ceil($this->recordCount / $this->items_per_page));
$this->currentPage = min(ceil($this->start / $this->items_per_page) + 1, $numPages);
// Let's bear a few things in mind...
$this->base_url = $options['base_url'];
// This should be set at all times, too.
if (empty($options['no_items_label']))
$options['no_items_label'] = '';
// Gather parameters for the data gather function first.
$parameters = [$this->start, $this->items_per_page, $options['sort_order'], $options['sort_direction']];
if (!empty($options['get_data_params']) && is_array($options['get_data_params']))
$parameters = array_merge($parameters, $options['get_data_params']);
// Okay, let's fetch the data!
$data = call_user_func_array($options['get_data'], $parameters);
$data = $options['get_data'](...$parameters);
// Clean up a bit.
$rows = $data['rows'];
// Extract data into local variables.
$rawRowData = $data['rows'];
$this->sort_order = $data['order'];
$this->sort_direction = $data['direction'];
unset($data);
@@ -71,24 +66,24 @@ class GenericTable extends PageIndex
// Okay, now for the column headers...
$this->generateColumnHeaders($options);
// Generate a pagination if requested
if ($this->needsPageIndex)
$this->generatePageIndex();
// Should we create a page index?
$needsPageIndex = !empty($this->items_per_page) && $this->recordCount > $this->items_per_page;
if ($needsPageIndex)
$this->generatePageIndex($options);
// Not a single row in sight?
if (empty($rows))
$this->body = $options['no_items_label'];
// Otherwise, parse it all!
// Process the data to be shown into rows.
if (!empty($rawRowData))
$this->processAllRows($rawRowData, $options);
else
$this->parseAllRows($rows, $options);
$this->body = $options['no_items_label'] ?? '';
// Got a title?
$this->title = isset($options['title']) ? htmlentities($options['title']) : '';
$this->title_class = isset($options['title_class']) ? $options['title_class'] : '';
$this->title = $options['title'] ?? '';
$this->title_class = $options['title_class'] ?? '';
// Maybe even a form or two?
$this->form_above = isset($options['form_above']) ? $options['form_above'] : (isset($options['form']) ? $options['form'] : null);
$this->form_below = isset($options['form_below']) ? $options['form_below'] : (isset($options['form']) ? $options['form'] : null);
$this->form_above = $options['form_above'] ?? $options['form'] ?? null;
$this->form_below = $options['form_below'] ?? $options['form'] ?? null;
}
private function generateColumnHeaders($options)
@@ -98,10 +93,13 @@ class GenericTable extends PageIndex
if (empty($column['header']))
continue;
$isSortable = $this->tableIsSortable && !empty($column['is_sortable']);
$sortDirection = $key == $this->sort_order && $this->sort_direction === 'up' ? 'down' : 'up';
$header = [
'class' => isset($column['class']) ? $column['class'] : '',
'colspan' => !empty($column['header_colspan']) ? $column['header_colspan'] : 1,
'href' => !$this->tableIsSortable || empty($column['is_sortable']) ? '' : $this->getLink($this->start, $key, $key == $this->sort_order && $this->sort_direction == 'up' ? 'down' : 'up'),
'href' => $isSortable ? $this->getLink($this->start, $key, $sortDirection) : null,
'label' => $column['header'],
'scope' => 'col',
'sort_mode' => $key == $this->sort_order ? $this->sort_direction : null,
@@ -112,99 +110,18 @@ class GenericTable extends PageIndex
}
}
private function parseAllRows($rows, $options)
private function generatePageIndex($options)
{
foreach ($rows as $i => $row)
{
$className = $i & 1 ? 'even' : 'odd';
if (isset($options['row_classifier']))
$className .= $options['row_classifier']($row);
$newRow = [
'class' => $className,
'cells' => [],
];
foreach ($options['columns'] as $column)
{
if (isset($column['enabled']) && $column['enabled'] == false)
continue;
// The hard way?
if (isset($column['parse']))
{
if (!isset($column['parse']['type']))
$column['parse']['type'] = 'value';
// Parse the basic value first.
switch ($column['parse']['type'])
{
// value: easy as pie.
default:
case 'value':
$value = $row[$column['parse']['data']];
break;
// sprintf: filling the gaps!
case 'sprintf':
$parameters = [$column['parse']['data']['pattern']];
foreach ($column['parse']['data']['arguments'] as $identifier)
$parameters[] = $row[$identifier];
$value = call_user_func_array('sprintf', $parameters);
break;
// timestamps: let's make them readable!
case 'timestamp':
if (empty($column['parse']['data']['pattern']) || $column['parse']['data']['pattern'] === 'long')
$pattern = '%F %H:%M';
elseif ($column['parse']['data']['pattern'] === 'short')
$pattern = '%F';
else
$pattern = $column['parse']['data']['pattern'];
if (!is_numeric($row[$column['parse']['data']['timestamp']]))
$timestamp = strtotime($row[$column['parse']['data']['timestamp']]);
else
$timestamp = (int) $row[$column['parse']['data']['timestamp']];
if (isset($column['parse']['data']['if_null']) && $timestamp == 0)
$value = $column['parse']['data']['if_null'];
else
$value = strftime($pattern, $timestamp);
break;
// function: the flexible way!
case 'function':
$value = $column['parse']['data']($row);
break;
}
// Generate a link, if requested.
if (!empty($column['parse']['link']))
{
// First, generate the replacement variables.
$keys = array_keys($row);
$values = array_values($row);
foreach ($keys as $keyKey => $keyValue)
$keys[$keyKey] = '{' . strtoupper($keyValue) . '}';
$value = '<a href="' . str_replace($keys, $values, $column['parse']['link']) . '">' . $value . '</a>';
}
}
// The easy way!
else
$value = $row[$column['value']];
// Append the cell to the row.
$newRow['cells'][] = [
'width' => !empty($column['cell_width']) && is_int($column['cell_width']) ? $column['cell_width'] : null,
'value' => $value,
];
}
// Append the new row in the body.
$this->body[] = $newRow;
}
$this->pageIndex = new PageIndex([
'base_url' => $this->base_url,
'index_class' => $options['index_class'] ?? '',
'items_per_page' => $this->items_per_page,
'linkBuilder' => [$this, 'getLink'],
'recordCount' => $this->recordCount,
'sort_direction' => $this->sort_direction,
'sort_order' => $this->sort_order,
'start' => $this->start,
]);
}
public function getLink($start = null, $order = null, $dir = null)
@@ -224,12 +141,6 @@ class GenericTable extends PageIndex
return $this->start;
}
public function getArray()
{
// Makes no sense to call it for a table, but inherits from PageIndex due to poor design, sorry.
throw new Exception('Function call is ambiguous.');
}
public function getHeader()
{
return $this->header;
@@ -240,6 +151,16 @@ class GenericTable extends PageIndex
return $this->body;
}
public function getCurrentPage()
{
return $this->currentPage;
}
public function getPageIndex()
{
return $this->pageIndex;
}
public function getTitle()
{
return $this->title;
@@ -249,4 +170,96 @@ class GenericTable extends PageIndex
{
return $this->title_class;
}
private function processAllRows($rows, $options)
{
foreach ($rows as $i => $row)
{
$newRow = [
'cells' => [],
];
foreach ($options['columns'] as $column)
{
// Process data for this particular cell.
if (isset($column['parse']))
$value = self::processCell($column['parse'], $row);
else
$value = $row[$column['value']];
// Append the cell to the row.
$newRow['cells'][] = [
'value' => $value,
];
}
// Append the new row in the body.
$this->body[] = $newRow;
}
}
private function processCell($options, $rowData)
{
if (!isset($options['type']))
$options['type'] = 'value';
// Parse the basic value first.
switch ($options['type'])
{
// Basic option: simply take a use a particular data property.
case 'value':
$value = htmlspecialchars($rowData[$options['data']]);
break;
// Processing via a lambda function.
case 'function':
$value = $options['data']($rowData);
break;
// Using sprintf to fill out a particular pattern.
case 'sprintf':
$parameters = [$options['data']['pattern']];
foreach ($options['data']['arguments'] as $identifier)
$parameters[] = $rowData[$identifier];
$value = sprintf(...$parameters);
break;
// Timestamps get custom treatment.
case 'timestamp':
if (empty($options['data']['pattern']) || $options['data']['pattern'] === 'long')
$pattern = 'Y-m-d H:i';
elseif ($options['data']['pattern'] === 'short')
$pattern = 'Y-m-d';
else
$pattern = $options['data']['pattern'];
if (!isset($rowData[$options['data']['timestamp']]))
$timestamp = 0;
elseif (!is_numeric($rowData[$options['data']['timestamp']]))
$timestamp = strtotime($rowData[$options['data']['timestamp']]);
else
$timestamp = (int) $rowData[$options['data']['timestamp']];
if (isset($options['data']['if_null']) && $timestamp == 0)
$value = $options['data']['if_null'];
else
$value = date($pattern, $timestamp);
break;
}
// Generate a link, if requested.
if (!empty($options['link']))
{
// First, generate the replacement variables.
$keys = array_keys($rowData);
$values = array_values($rowData);
foreach ($keys as $keyKey => $keyValue)
$keys[$keyKey] = '{' . strtoupper($keyValue) . '}';
$value = '<a href="' . str_replace($keys, $values, $options['link']) . '">' . $value . '</a>';
}
return $value;
}
}

View File

@@ -67,14 +67,33 @@ class Image extends Asset
return EXIF::fromFile($this->getPath());
}
public function getPath()
public function getImageUrls($width = null, $height = null)
{
return ASSETSDIR . '/' . $this->subdir . '/' . $this->filename;
$image_urls = [];
if (isset($width) || isset($height))
{
$thumbnail = new Thumbnail($this);
$image_urls[1] = $this->getThumbnailUrl($width, $height, false);
// Can we afford to generate double-density thumbnails as well?
if ((!isset($width) || $this->image_width >= $width * 2) &&
(!isset($height) || $this->image_height >= $height * 2))
$image_urls[2] = $this->getThumbnailUrl($width * 2, $height * 2, false);
else
$image_urls[2] = $this->getThumbnailUrl($this->image_width, $this->image_height, true);
}
else
$image_urls[1] = $this->getUrl();
return $image_urls;
}
public function getUrl()
public function getInlineImage($width = null, $height = null, $className = 'inline-image')
{
return ASSETSURL . '/' . $this->subdir . '/' . $this->filename;
$image_urls = $this->getImageUrls($width, $height);
return '<img class="' . $className . '" src="' . $image_urls[1] . '" alt=""' .
(isset($image_urls[2]) ? ' srcset="' . $image_urls[2] . ' 2x"' : '') . '>';
}
/**
@@ -141,7 +160,8 @@ class Image extends Asset
}
return Registry::get('db')->query('
DELETE FROM assets_thumbs
UPDATE assets_thumbs
SET filename = NULL
WHERE id_asset = {int:id_asset}',
['id_asset' => $this->id_asset]);
}

View File

@@ -8,26 +8,47 @@
class PageIndex
{
protected $page_index = [];
protected $current_page = 1;
protected $items_per_page = 0;
protected $needsPageIndex = false;
protected $num_pages = 1;
protected $recordCount = 0;
protected $start = 0;
protected $sort_order = null;
protected $sort_direction = null;
protected $base_url;
protected $index_class = 'pagination';
protected $page_slug = '%AMP%page=%PAGE%';
private $base_url;
private $current_page = 1;
private $index_class = 'pagination';
private $items_per_page = 0;
private $linkBuilder;
private $needsPageIndex = false;
private $num_pages = 1;
private $page_index = [];
private $page_slug = '%AMP%page=%PAGE%';
private $recordCount = 0;
private $sort_direction = null;
private $sort_order = null;
private $start = 0;
public function __construct($options)
{
foreach ($options as $key => $value)
$this->$key = $value;
static $neededKeys = ['base_url', 'items_per_page', 'recordCount'];
foreach ($neededKeys as $key)
{
if (!isset($options[$key]))
throw new Exception('PageIndex: argument ' . $key . ' missing in options');
$this->$key = $options[$key];
}
static $optionalKeys = ['index_class', 'linkBuilder', 'page_slug', 'sort_direction', 'sort_order', 'start'];
foreach ($optionalKeys as $key)
if (isset($options[$key]))
$this->$key = $options[$key];
$this->generatePageIndex();
}
private function buildLink($start = null, $order = null, $dir = null)
{
if (isset($this->linkBuilder))
return call_user_func($this->linkBuilder, $start, $order, $dir);
else
return $this->getLink($start, $order, $dir);
}
protected function generatePageIndex()
{
/*
@@ -42,9 +63,9 @@ class PageIndex
lower current/cont. pgs. center upper
*/
$this->num_pages = ceil($this->recordCount / $this->items_per_page);
$this->num_pages = max(1, ceil($this->recordCount / $this->items_per_page));
$this->current_page = min(ceil($this->start / $this->items_per_page) + 1, $this->num_pages);
if ($this->num_pages == 0)
if ($this->num_pages <= 1)
{
$this->needsPageIndex = false;
return;
@@ -68,7 +89,7 @@ class PageIndex
$this->page_index[$p] = [
'index' => $p,
'is_selected' => $this->current_page == $p,
'href'=> $this->getLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
'href'=> $this->buildLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
];
// The center of the page index.
@@ -81,7 +102,7 @@ class PageIndex
$this->page_index[$center] = [
'index' => $center,
'is_selected' => $this->current_page == $center,
'href'=> $this->getLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
'href'=> $this->buildLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
];
}
@@ -94,7 +115,7 @@ class PageIndex
$this->page_index[$p] = [
'index' => $p,
'is_selected' => $this->current_page == $p,
'href'=> $this->getLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
'href'=> $this->buildLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
];
// The center of the page index.
@@ -107,7 +128,7 @@ class PageIndex
$this->page_index[$center] = [
'index' => $center,
'is_selected' => $this->current_page == $center,
'href'=> $this->getLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
'href'=> $this->buildLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
];
}
@@ -120,7 +141,7 @@ class PageIndex
$this->page_index[$p] = [
'index' => $p,
'is_selected' => $this->current_page == $p,
'href'=> $this->getLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
'href'=> $this->buildLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
];
// Previous page?
@@ -157,11 +178,6 @@ class PageIndex
return $url;
}
public function getArray()
{
return $this->page_index;
}
public function getPageIndex()
{
return $this->page_index;

View File

@@ -9,6 +9,7 @@
class Thumbnail
{
private $image;
private $image_meta;
private $thumbnails;
private $properly_initialised;
@@ -23,7 +24,7 @@ class Thumbnail
const CROP_MODE_SLICE_CENTRE = 4;
const CROP_MODE_SLICE_BOTTOM = 5;
public function __construct($image)
public function __construct(Image $image)
{
$this->image = $image;
$this->image_meta = $image->getMeta();
@@ -45,51 +46,45 @@ class Thumbnail
$thumb_selector = $this->width . 'x' . $this->height . $this->filename_suffix;
if (!empty($this->thumbnails[$thumb_selector]))
{
$thumb_path = '/' . $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector];
if (file_exists(THUMBSDIR . $thumb_path))
return THUMBSURL . $thumb_path;
$thumb_filename = $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector];
if (file_exists(THUMBSDIR . '/' . $thumb_filename))
return THUMBSURL . '/' . $thumb_filename;
}
// Do we have a custom thumbnail on file?
$custom_selector = 'custom_' . $this->width . 'x' . $this->height;
if (isset($this->image_meta[$custom_selector]))
{
$custom_thumb_path = '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector];
if (file_exists(ASSETSDIR . $custom_thumb_path))
$custom_filename = $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector];
if (file_exists(ASSETSDIR . '/' . $custom_filename))
{
// Ensure destination thumbnail directory exists.
if (!file_exists($this->image->getSubdir()))
@mkdir(THUMBSDIR . '/' . $this->image->getSubdir(), 0755, true);
// Copy the custom thumbail to the general thumbnail directory.
copy(ASSETSDIR . $custom_thumb_path, THUMBSDIR . $custom_thumb_path);
copy(ASSETSDIR . '/' . $custom_filename, THUMBSDIR . '/' . $custom_filename);
// Let's remember this for future reference.
$this->markAsGenerated($this->image_meta[$custom_selector]);
return THUMBSURL . $custom_thumb_path;
return THUMBSURL . '/' . $custom_filename;
}
else
throw new UnexpectedValueException('Custom thumbnail expected, but missing in file system!');
}
// Is this the right moment to generate a thumbnail, then?
if ($generate && array_key_exists($thumb_selector, $this->thumbnails))
if ($generate)
{
if (array_key_exists($thumb_selector, $this->thumbnails))
return $this->generate();
else
throw new Exception("Trying to generate a thumbnail not previously queued by the system\n" .
print_r(func_get_args(), true));
}
// If not, queue it for generation at another time, and return a URL to generate it with.
elseif (!$generate)
{
$this->markAsQueued();
return BASEURL . '/thumbnail/' . $this->image->getId() . '/' . $this->width . 'x' . $this->height . $this->filename_suffix . '/';
}
// Still here..? What are you up to? ..Sneaking?
else
{
throw new Exception("Trying to generate a thumbnail for selector " . $thumb_selector . ", which does not appear to have been requested by the system.\n" . print_r(func_get_args(), true));
$this->markAsQueued();
return BASEURL . '/thumbnail/' . $this->image->getId() . '/' . $thumb_selector . '/';
}
}
@@ -260,14 +255,18 @@ class Thumbnail
'_' . $this->width . 'x' . $this->height . $this->filename_suffix . '.' . $ext;
// Ensure the thumbnail subdirectory exists.
if (!is_dir(THUMBSDIR . '/' . $this->image->getSubdir()))
mkdir(THUMBSDIR . '/' . $this->image->getSubdir(), 0755, true);
$target_dir = THUMBSDIR . '/' . $this->image->getSubdir();
if (!is_dir($target_dir))
mkdir($target_dir, 0755, true);
if (!is_writable($target_dir))
throw new Exception('Thumbnail directory is not writable!');
// No need to preserve every detail.
$thumb->setImageCompressionQuality(80);
// Save it in a public spot.
$thumb->writeImage(THUMBSDIR . '/' . $this->image->getSubdir() . '/' . $thumb_filename);
$thumb->writeImage($target_dir . '/' . $thumb_filename);
// Let's remember this for future reference...
$this->markAsGenerated($thumb_filename);
@@ -278,7 +277,6 @@ class Thumbnail
// Finally, return the URL for the generated thumbnail image.
return THUMBSURL . '/' . $this->image->getSubdir() . '/' . $thumb_filename;
}
// Blast! Curse your sudden but inevitable betrayal!
catch (ImagickException $e)
{
throw new Exception('ImageMagick error occurred while generating thumbnail. Output: ' . $e->getMessage());
@@ -336,11 +334,17 @@ class Thumbnail
if ($success)
{
$thumb_selector = $this->width . 'x' . $this->height . $this->filename_suffix;
$this->thumbnails[$thumb_selector] = $filename !== 'NULL' ? $filename : '';
}
$this->thumbnails[$thumb_selector] = $filename !== 'NULL' ? $filename : null;
// For consistency, write new thumbnail filename to parent Image object.
// TODO: there could still be an inconsistency if multiple objects exists for the same image asset.
$this->image->getThumbnails()[$thumb_selector] = $this->thumbnails[$thumb_selector];
return $success;
}
else
throw new UnexpectedValueException('Thumbnail queuing query failed');
}
private function markAsQueued()
{

View File

@@ -25,7 +25,7 @@ class FormView extends SubTemplate
if (!empty($this->title))
echo '
<div class="admin_box">
<h2>', $this->title, '</h2>';
<h2>', htmlspecialchars($this->title), '</h2>';
foreach ($this->_subtemplates as $template)
$template->html_main();

View File

@@ -65,11 +65,9 @@ class PhotoPage extends SubTemplate
<a href="', $this->photo->getUrl(), '">';
if ($this->photo->isPortrait())
echo '
<img src="', $this->photo->getThumbnailUrl(null, 960), '" alt="">';
echo $this->photo->getInlineImage(null, 960);
else
echo '
<img src="', $this->photo->getThumbnailUrl(1280, null), '" alt="">';
echo $this->photo->getInlineImage(1280, null);
echo '
</a>

View File

@@ -90,8 +90,14 @@ class PhotosIndex extends SubTemplate
<a class="edit" href="', BASEURL, '/editasset/?id=', $image->getId(), '">Edit</a>';
echo '
<a href="', $image->getPageUrl(), $this->url_suffix, '">
<img src="', $image->getThumbnailUrl($width, $height, $crop, $fit), '" alt="" title="', $image->getTitle(), '">';
<a href="', $image->getPageUrl(), $this->url_suffix, '#photo_frame">
<img src="', $image->getThumbnailUrl($width, $height, $crop, $fit), '"';
// Can we offer double-density thumbs?
if ($image->width() >= $width * 2 && $image->height() >= $height * 2)
echo ' srcset="', $image->getThumbnailUrl($width * 2, $height * 2, $crop, $fit), ' 2x"';
echo ' alt="" title="', $image->getTitle(), '">';
if ($this->show_labels)
echo '

View File

@@ -6,12 +6,15 @@
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class TabularData extends Pagination
class TabularData extends SubTemplate
{
public function __construct(GenericTable $table)
{
$this->_t = $table;
parent::__construct($table);
$pageIndex = $table->getPageIndex();
if ($pageIndex)
$this->pager = new Pagination($pageIndex);
}
protected function html_content()
@@ -25,7 +28,8 @@ class TabularData extends Pagination
<h2>', $title, '</h2>';
// Showing a page index?
parent::html_content();
if (isset($this->pager))
$this->pager->html_content();
// Maybe even a small form?
if (isset($this->_t->form_above))
@@ -87,7 +91,8 @@ class TabularData extends Pagination
$this->showForm($this->_t->form_below);
// Showing a page index?
parent::html_content();
if (isset($this->pager))
$this->pager->html_content();
echo '
</div>';