Initial commit.

This is to be the new HashRU website based on the Aaronweb.net/Kabuki CMS.
This commit is contained in:
2016-09-01 23:13:23 +02:00
commit ab0e4efbcb
68 changed files with 8054 additions and 0 deletions

478
models/Asset.php Normal file
View File

@@ -0,0 +1,478 @@
<?php
/*****************************************************************************
* Asset.php
* Contains key class Asset.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Asset
{
protected $id_asset;
protected $subdir;
protected $filename;
protected $title;
protected $mimetype;
protected $image_width;
protected $image_height;
protected $date_captured;
protected $priority;
protected $meta;
protected $tags;
protected function __construct(array $data)
{
foreach ($data as $attribute => $value)
$this->$attribute = $value;
if (!empty($data['date_captured']) && $data['date_captured'] !== 'NULL')
$this->date_captured = new DateTime($data['date_captured']);
}
public static function fromId($id_asset, $return_format = 'object')
{
$db = Registry::get('db');
$row = $db->queryAssoc('
SELECT *
FROM assets
WHERE id_asset = {int:id_asset}',
[
'id_asset' => $id_asset,
]);
// Asset not found?
if (empty($row))
return false;
$row['meta'] = $db->queryPair('
SELECT variable, value
FROM assets_meta
WHERE id_asset = {int:id_asset}',
[
'id_asset' => $id_asset,
]);
return $return_format == 'object' ? new Asset($row) : $row;
}
public static function fromIds(array $id_assets, $return_format = 'array')
{
if (empty($id_assets))
return [];
$db = Registry::get('db');
$res = $db->query('
SELECT *
FROM assets
WHERE id_asset IN ({array_int:id_assets})
ORDER BY id_asset',
[
'id_assets' => $id_assets,
]);
$assets = [];
while ($asset = $db->fetch_assoc($res))
{
$assets[$asset['id_asset']] = $asset;
$assets[$asset['id_asset']]['meta'] = [];
}
$metas = $db->queryRows('
SELECT id_asset, variable, value
FROM assets_meta
WHERE id_asset IN ({array_int:id_assets})
ORDER BY id_asset',
[
'id_assets' => $id_assets,
]);
foreach ($metas as $meta)
$assets[$meta[0]]['meta'][$meta[1]] = $meta[2];
if ($return_format == 'array')
return $assets;
else
{
$objects = [];
foreach ($assets as $id => $asset)
$objects[$id] = new Asset($asset);
return $objects;
}
}
public static function byPostId($id_post, $return_format = 'object')
{
$db = Registry::get('db');
// !!! TODO
}
public static function createNew(array $data, $return_format = 'object')
{
// Extract the data array.
extract($data);
// No filename? Abort!
if (!isset($filename_to_copy) || !is_file($filename_to_copy))
return false;
// No subdir? Use YYYY/MM
if (!isset($preferred_subdir))
$preferred_subdir = date('Y') . '/' . date('m');
// Does this dir exist yet? If not, create it.
if (!is_dir(ASSETSDIR . '/' . $preferred_subdir))
mkdir(ASSETSDIR . '/' . $preferred_subdir, 0755, true);
// Construct the destination filename. Make sure we don't accidentally overwrite anything.
if (!isset($preferred_filename))
$preferred_filename = basename($filename_to_copy);
$new_filename = $preferred_filename;
$destination = ASSETSDIR . '/' . $preferred_subdir . '/' . $preferred_filename;
while (file_exists($destination))
{
$filename = pathinfo($preferred_filename, PATHINFO_FILENAME) . '_' . mt_rand(10, 99);
$extension = pathinfo($preferred_filename, PATHINFO_EXTENSION);
$new_filename = $filename . '.' . $extension;
$destination = dirname($destination) . '/' . $new_filename;
}
// Can we write to the target directory? Then copy the file.
if (is_writable(ASSETSDIR . '/' . $preferred_subdir))
copy($filename_to_copy, $destination);
else
return false;
// Figure out the mime type for the file.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $destination);
finfo_close($finfo);
// Do we have a title yet? Otherwise, use the filename.
$title = isset($data['title']) ? $data['title'] : pathinfo($preferred_filename, PATHINFO_FILENAME);
// Same with the slug.
$slug = isset($data['slug']) ? $data['slug'] : $preferred_subdir . '/' . pathinfo($preferred_filename, PATHINFO_FILENAME);
// Detected an image?
if (substr($mimetype, 0, 5) == 'image')
{
$image = new Imagick($destination);
$d = $image->getImageGeometry();
// Get image dimensions, bearing orientation in mind.
switch ($image->getImageOrientation())
{
case Imagick::ORIENTATION_LEFTBOTTOM:
case Imagick::ORIENTATION_RIGHTTOP:
$image_width = $d['height'];
$image_height = $d['width'];
break;
default:
$image_width = $d['width'];
$image_height = $d['height'];
}
unset($image);
$exif = EXIF::fromFile($destination);
$date_captured = intval($exif->created_timestamp);
}
$db = Registry::get('db');
$res = $db->query('
INSERT INTO assets
(subdir, filename, title, slug, mimetype, image_width, image_height, date_captured, priority)
VALUES
({string:subdir}, {string:filename}, {string:title}, {string:slug}, {string:mimetype},
{int:image_width}, {int:image_height},
IF({int:date_captured} > 0, FROM_UNIXTIME({int:date_captured}), NULL),
{int:priority})',
[
'subdir' => $preferred_subdir,
'filename' => $new_filename,
'title' => $title,
'slug' => $slug,
'mimetype' => $mimetype,
'image_width' => isset($image_width) ? $image_width : 'NULL',
'image_height' => isset($image_height) ? $image_height : 'NULL',
'date_captured' => isset($date_captured) ? $date_captured : 'NULL',
'priority' => isset($priority) ? (int) $priority : 0,
]);
if (!$res)
{
unlink($destination);
return false;
}
$data['id_asset'] = $db->insert_id();
return $return_format == 'object' ? new self($data) : $data;
}
public function getId()
{
return $this->id_asset;
}
public function getDateCaptured()
{
return $this->date_captured;
}
public function getFilename()
{
return $this->filename;
}
public function getLinkedPosts()
{
$posts = Registry::get('db')->queryValues('
SELECT id_post
FROM posts_assets
WHERE id_asset = {int:id_asset}',
['id_asset' => $this->id_asset]);
// TODO: fix empty post iterator.
if (empty($posts))
return [];
return PostIterator::getByOptions([
'ids' => $posts,
'type' => '',
]);
}
public function getMeta()
{
return $this->meta;
}
public function getPath()
{
return $this->subdir;
}
public function getPriority()
{
return $this->priority;
}
public function getTags()
{
if (!isset($this->tags))
$this->tags = Tag::byAssetId($this->id_asset);
return $this->tags;
}
public function getTitle()
{
return $this->title;
}
public function getUrl()
{
return BASEURL . '/assets/' . $this->subdir . '/' . $this->filename;
}
public function getType()
{
return substr($this->mimetype, 0, strpos($this->mimetype, '/'));
}
public function getDimensions($as_type = 'string')
{
return $as_type === 'string' ? $this->image_width . 'x' . $this->image_height : [$this->image_width, $this->image_height];
}
public function isImage()
{
return substr($this->mimetype, 0, 5) === 'image';
}
public function getImage()
{
if (!$this->isImage())
throw new Exception('Trying to upgrade an Asset to an Image while the Asset is not an image!');
return new Image(get_object_vars($this));
}
public function replaceFile($filename)
{
// No filename? Abort!
if (!isset($filename) || !is_readable($filename))
return false;
// Can we write to the target file?
$destination = ASSETSDIR . '/' . $this->subdir . '/' . $this->filename;
if (!is_writable($destination))
return false;
copy($filename, $destination);
// Figure out the mime type for the file.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$this->mimetype = finfo_file($finfo, $destination);
finfo_close($finfo);
// Detected an image?
if (substr($this->mimetype, 0, 5) == 'image')
{
$image = new Imagick($destination);
$d = $image->getImageGeometry();
$this->image_width = $d['width'];
$this->image_height = $d['height'];
unset($image);
$exif = EXIF::fromFile($destination);
if (!empty($exif->created_timestamp))
$this->date_captured = new DateTime(date('r', $exif->created_timestamp));
else
$this->date_captured = null;
}
else
{
$this->image_width = null;
$this->image_height = null;
$this->date_captured = null;
}
return Registry::get('db')->query('
UPDATE assets
SET
mimetype = {string:mimetype},
image_width = {int:image_width},
image_height = {int:image_height},
date_captured = {datetime:date_captured},
priority = {int:priority}
WHERE id_asset = {int:id_asset}',
[
'id_asset' => $this->id_asset,
'mimetype' => $this->mimetype,
'image_width' => isset($this->image_width) ? $this->image_width : 'NULL',
'image_height' => isset($this->image_height) ? $this->image_height : 'NULL',
'date_captured' => isset($this->date_captured) ? $this->date_captured : 'NULL',
'priority' => $this->priority,
]);
}
protected function saveMetaData()
{
$this->setMetaData($this->meta);
}
public function setMetaData(array $new_meta, $mode = 'replace')
{
$db = Registry::get('db');
// If we're replacing, delete current data first.
if ($mode === 'replace')
{
$to_remove = array_diff_key($this->meta, $new_meta);
if (!empty($to_remove))
$db->query('
DELETE FROM assets_meta
WHERE id_asset = {int:id_asset} AND
variable IN({array_string:variables})',
[
'id_asset' => $this->id_asset,
'variables' => array_keys($to_remove),
]);
}
// Build rows
$to_insert = [];
foreach ($new_meta as $key => $value)
$to_insert[] = [
'id_asset' => $this->id_asset,
'variable' => $key,
'value' => $value,
];
// Do the insertion
$res = Registry::get('db')->insert('replace', 'assets_meta', [
'id_asset' => 'int',
'variable' => 'string',
'value' => 'string',
], $to_insert, ['id_asset', 'variable']);
if ($res)
$this->meta = $new_meta;
}
public function delete()
{
return Registry::get('db')->query('
DELETE FROM assets
WHERE id_asset = {int:id_asset}',
[
'id_asset' => $this->id_asset,
]);
}
public function linkTags(array $id_tags)
{
if (empty($id_tags))
return true;
$pairs = [];
foreach ($id_tags as $id_tag)
$pairs[] = ['id_asset' => $this->id_asset, 'id_tag' => $id_tag];
Registry::get('db')->insert('ignore', 'assets_tags', [
'id_asset' => 'int',
'id_tag' => 'int',
], $pairs, ['id_asset', 'id_tag']);
Tag::recount($id_tags);
}
public function unlinkTags(array $id_tags)
{
if (empty($id_tags))
return true;
Registry::get('db')->query('
DELETE FROM assets_tags
WHERE id_asset = {int:id_asset} AND id_tag IN ({array_int:id_tags})',
[
'id_asset' => $this->id_asset,
'id_tags' => $id_tags,
]);
Tag::recount($id_tags);
}
public static function getCount()
{
return $db->queryValue('
SELECT COUNT(*)
FROM assets');
}
public function setKeyData($title, DateTime $date_captured = null, $priority)
{
$params = [
'id_asset' => $this->id_asset,
'title' => $title,
'priority' => $priority,
];
if (isset($date_captured))
$params['date_captured'] = $date_captured->format('Y-m-d H:i:s');
return Registry::get('db')->query('
UPDATE assets
SET title = {string:title},' . (isset($date_captured) ? '
date_captured = {datetime:date_captured},' : '') . '
priority = {int:priority}
WHERE id_asset = {int:id_asset}',
$params);
}
}

154
models/AssetIterator.php Normal file
View File

@@ -0,0 +1,154 @@
<?php
/*****************************************************************************
* AssetIterator.php
* Contains key class AssetIterator.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class AssetIterator extends Asset
{
private $return_format;
private $res_assets;
private $res_meta;
protected function __construct($res_assets, $res_meta, $return_format)
{
$this->db = Registry::get('db');
$this->res_assets = $res_assets;
$this->res_meta = $res_meta;
$this->return_format = $return_format;
}
public function next()
{
$row = $this->db->fetch_assoc($this->res_assets);
// No more rows?
if (!$row)
return false;
// Looks up metadata.
$row['meta'] = [];
while ($meta = $this->db->fetch_assoc($this->res_meta))
{
if ($meta['id_asset'] != $row['id_asset'])
continue;
$row['meta'][$meta['variable']] = $meta['value'];
}
// Reset internal pointer for next asset.
$this->db->data_seek($this->res_meta, 0);
if ($this->return_format == 'object')
return new Asset($row);
else
return $row;
}
public function reset()
{
$this->db->data_seek($this->res_assets, 0);
$this->db->data_seek($this->res_meta, 0);
}
public function clean()
{
if (!$this->res_assets)
return;
$this->db->free_result($this->res_assets);
$this->res_assets = null;
}
public function num()
{
return $this->db->num_rows($this->res_assets);
}
public static function all()
{
return self::getByOptions();
}
public static function getByOptions(array $options = [], $return_count = false, $return_format = 'object')
{
$params = [
'limit' => isset($options['limit']) ? $options['limit'] : 0,
'order' => isset($options['order']) && in_array($options['order'], ['id_asset', 'subdir', 'filename', 'title',
'mime_type', 'image_width', 'image_height', 'date_captured']) ? $options['order'] : 'id_asset',
'direction' => isset($options['direction']) ? $options['direction'] : 'asc',
];
if (isset($options['offset']))
$params['offset'] = $options['offset'];
elseif (isset($options['page']) && $options['page'] >= 1)
$params['offset'] = $params['limit'] * ($options['page'] - 1);
else
$params['offset'] = 0;
$where = [];
if (isset($options['mime_type']))
{
$params['mime_type'] = $options['mime_type'];
if (is_array($options['mime_type']))
$where[] = 'a.mimetype IN({array_string:mime_type})';
else
$where[] = 'a.mimetype = {string:mime_type}';
}
if (isset($options['id_tag']))
{
$params['id_tag'] = $options['id_tag'];
$where[] = 'id_asset IN(
SELECT l.id_asset
FROM assets_tags AS l
WHERE l.id_tag = {int:id_tag})';
}
// Make it valid SQL.
$order = 'a.' . $params['order'] . ' ' . ($params['direction'] == 'desc' ? 'DESC' : 'ASC');
$where = empty($where) ? '1' : implode(' AND ', $where);
// And ... go!
$db = Registry::get('db');
// Get a resource object for the assets.
$res_assets = $db->query('
SELECT a.*
FROM assets AS a
WHERE ' . $where . '
ORDER BY ' . $order . (!empty($params['limit']) ? '
LIMIT {int:offset}, {int:limit}' : ''),
$params);
// Get a resource object for the asset meta.
$res_meta = $db->query('
SELECT id_asset, variable, value
FROM assets_meta
WHERE id_asset IN(
SELECT id_asset
FROM assets AS a
WHERE ' . $where . '
)
ORDER BY id_asset',
$params);
$iterator = new self($res_assets, $res_meta, $return_format);
// Returning total count, too?
if ($return_count)
{
$count = $db->queryValue('
SELECT COUNT(*)
FROM assets AS a
WHERE ' . $where,
$params);
return [$iterator, $count];
}
else
return $iterator;
}
}

114
models/Authentication.php Normal file
View File

@@ -0,0 +1,114 @@
<?php
/*****************************************************************************
* Authentication.php
* Contains key class Authentication.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
/**
* Authentication class, containing various static functions used for account verification
* and session management.
*/
class Authentication
{
/**
* Checks whether a user still exists in the database.
*/
public static function checkExists($id_user)
{
$res = Registry::get('db')->queryValue('
SELECT id_user
FROM users
WHERE id_user = {int:id}',
[
'id' => $id_user,
]);
return $res !== null;
}
/**
* Finds the user id belonging to a certain emailaddress.
*/
public static function getUserId($emailaddress)
{
$res = Registry::get('db')->queryValue('
SELECT id_user
FROM users
WHERE emailaddress = {string:emailaddress}',
[
'emailaddress' => $emailaddress,
]);
return empty($res) ? false : $res;
}
/**
* Verifies whether the user is currently logged in.
*/
public static function isLoggedIn()
{
// Check whether the active session matches the current user's environment.
if (isset($_SESSION['ip_address'], $_SESSION['user_agent']) && (
(isset($_SERVER['REMOTE_ADDR']) && $_SESSION['ip_address'] != $_SERVER['REMOTE_ADDR']) ||
(isset($_SERVER['HTTP_USER_AGENT']) && $_SESSION['user_agent'] != $_SERVER['HTTP_USER_AGENT'])))
{
session_destroy();
return false;
}
// A user is logged in if a user id exists in the session and this id is (still) in the database.
return isset($_SESSION['user_id']) && self::checkExists($_SESSION['user_id']);
}
/**
* Checks a password for a given username against the database.
*/
public static function checkPassword($emailaddress, $password)
{
// Retrieve password hash for user matching the provided emailaddress.
$password_hash = Registry::get('db')->queryValue('
SELECT password_hash
FROM users
WHERE emailaddress = {string:emailaddress}',
[
'emailaddress' => $emailaddress,
]);
// If there's no hash, the user likely does not exist.
if (!$password_hash)
return false;
return password_verify($password, $password_hash);
}
/**
* Computes a password hash.
*/
public static function computeHash($password)
{
$hash = password_hash($password, PASSWORD_DEFAULT);
if (!$hash)
throw new Exception('Hash creation failed!');
return $hash;
}
/**
* Resets a password for a certain user.
*/
public static function updatePassword($id_user, $hash)
{
return Registry::get('db')->query('
UPDATE users
SET
password_hash = {string:hash},
reset_key = {string:blank}
WHERE id_user = {int:id_user}',
[
'id_user' => $id_user,
'hash' => $hash,
'blank' => '',
]);
}
}

149
models/BestColor.php Normal file
View File

@@ -0,0 +1,149 @@
<?php
/*****************************************************************************
* BestColor.php
* Contains key class BestColor.
*
* !!! Licensing?
*****************************************************************************/
class BestColor
{
private $best;
public function __construct(Image $asset)
{
// Set fallback color.
$this->best = ['r' => 204, 'g' => 204, 'b' => 204]; // #cccccc
// We will be needing to read this...
if (!file_exists($asset->getPath()))
return;
// Try the arcane stuff again.
try
{
$image = new Imagick($asset->getPath());
$width = $image->getImageWidth();
$height = $image->getImageHeight();
// Sample six points in the image: four based on the rule of thirds, as well as the horizontal and vertical centre.
$topy = round($height / 3);
$bottomy = round(($height / 3) * 2);
$leftx = round($width / 3);
$rightx = round(($width / 3) * 2);
$centery = round($height / 2);
$centerx = round($width / 2);
// Grab their colours.
$rgb = [
$image->getImagePixelColor($leftx, $topy)->getColor(),
$image->getImagePixelColor($rightx, $topy)->getColor(),
$image->getImagePixelColor($leftx, $bottomy)->getColor(),
$image->getImagePixelColor($rightx, $bottomy)->getColor(),
$image->getImagePixelColor($centerx, $centery)->getColor(),
];
// We won't be needing this anymore, so save us some memory.
$image->clear();
$image->destroy();
}
// In case something does go wrong...
catch (ImagickException $e)
{
// Fall back to default color.
return;
}
// Process rgb values into hsv values
foreach ($rgb as $i => $color)
{
$colors[$i] = $color;
list($colors[$i]['h'], $colors[$i]['s'], $colors[$i]['v']) = self::rgb2hsv($color['r'], $color['g'], $color['b']);
}
// Figure out which color is the best saturated.
$best_saturation = $best_brightness = 0;
$the_best_s = $the_best_v = ['v' => 0];
foreach ($colors as $color)
{
if ($color['s'] > $best_saturation)
{
$best_saturation = $color['s'];
$the_best_s = $color;
}
if ($color['v'] > $best_brightness)
{
$best_brightness = $color['v'];
$the_best_v = $color;
}
}
// Is brightest the same as most saturated?
$this->best = ($the_best_s['v'] >= ($the_best_v['v'] - ($the_best_v['v'] / 2))) ? $the_best_s : $the_best_v;
}
public static function hex2rgb($hex)
{
return sscanf($hex, '%2X%2X%2X');
}
public static function rgb2hex($red, $green, $blue)
{
return sprintf('%02X%02X%02X', $red, $green, $blue);
}
public static function rgb2hsv($r, $g, $b)
{
$max = max($r, $g, $b);
$min = min($r, $g, $b);
$delta = $max - $min;
$v = round(($max / 255) * 100);
$s = ($max != 0) ? (round($delta / $max * 100)) : 0;
if ($s == 0)
{
$h = false;
}
else
{
if ($r == $max)
$h = ($g - $b) / $delta;
elseif ($g == $max)
$h = 2 + ($b - $r) / $delta;
elseif ($b == $max)
$h = 4 + ($r - $g) / $delta;
$h = round($h * 60);
if ($h > 360)
$h = 360;
if ($h < 0)
$h += 360;
}
return [$h, $s, $v];
}
/**
* Get a normal (light) background color as hexadecimal value (without hash prefix).
* @return color string
*/
public function hex()
{
$c = $this->best;
return self::rgb2hex($c['r'], $c['g'], $c['b']);
}
/**
* Get a 50% darker version of the best color as string.
* @param factor, defaults to 0.5
* @param alpha, defaults to 0.7
* @return rgba(r * factor, g * factor, b * factor, alpha)
*/
public function rgba($factor = 0.5, $alpha = 0.7)
{
$c = $this->best;
return 'rgba(' . round($c['r'] * $factor) . ', ' . round($c['g'] * $factor) . ', ' . round($c['b'] * $factor) . ', ' . $alpha . ')';
}
}

61
models/Cache.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
/*****************************************************************************
* Cache.php
* Contains key class Cache.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Cache
{
public static $hits = 0;
public static $misses = 0;
public static $puts = 0;
public static $removals = 0;
public static function put($key, $value, $ttl = 3600)
{
// If the cache is unavailable, don't bother.
if (!CACHE_ENABLED || !function_exists('apcu_store'))
return false;
// Keep track of the amount of cache puts.
self::$puts++;
// Store the data in serialized form.
return apcu_store(CACHE_KEY_PREFIX . $key, serialize($value), $ttl);
}
// Get some data from the cache.
public static function get($key)
{
// If the cache is unavailable, don't bother.
if (!CACHE_ENABLED || !function_exists('apcu_fetch'))
return false;
// Try to fetch it!
$value = apcu_fetch(CACHE_KEY_PREFIX . $key);
// Were we successful?
if (!empty($value))
{
self::$hits++;
return unserialize($value);
}
// Otherwise, it's a miss.
else
{
self::$misses++;
return null;
}
}
public static function remove($key)
{
if (!CACHE_ENABLED || !function_exists('apcu_delete'))
return false;
self::$removals++;
return apcu_delete(CACHE_KEY_PREFIX . $key);
}
}

535
models/Database.php Normal file
View File

@@ -0,0 +1,535 @@
<?php
/*****************************************************************************
* Database.php
* Contains key class Database.
*
* Adapted from SMF 2.0's DBA (C) 2011 Simple Machines
* Used under BSD 3-clause license.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
/**
* The database model used to communicate with the MySQL server.
*/
class Database
{
private $connection;
private $query_count = 0;
private $logged_queries = [];
/**
* Initialises a new database connection.
* @param server: server to connect to.
* @param user: username to use for authentication.
* @param password: password to use for authentication.
* @param name: database to select.
*/
public function __construct($server, $user, $password, $name)
{
$this->connection = @mysqli_connect($server, $user, $password, $name);
// Give up if we have a connection error.
if (mysqli_connect_error())
{
header('HTTP/1.1 503 Service Temporarily Unavailable');
echo '<h2>Database Connection Problems</h2><p>Our software could not connect to the database. We apologise for any inconvenience and ask you to check back later.</p>';
exit;
}
$this->query('SET NAMES {string:utf8}', array('utf8' => 'utf8'));
}
public function getQueryCount()
{
return $this->query_count;
}
public function getLoggedQueries()
{
return $this->logged_queries;
}
/**
* Fetches a row from a given recordset, using field names as keys.
*/
public function fetch_assoc($resource)
{
return mysqli_fetch_assoc($resource);
}
/**
* Fetches a row from a given recordset, using numeric keys.
*/
public function fetch_row($resource)
{
return mysqli_fetch_row($resource);
}
/**
* Destroys a given recordset.
*/
public function free_result($resource)
{
return mysqli_free_result($resource);
}
public function data_seek($result, $row_num)
{
return mysqli_data_seek($result, $row_num);
}
/**
* Returns the amount of rows in a given recordset.
*/
public function num_rows($resource)
{
return mysqli_num_rows($resource);
}
/**
* Returns the amount of fields in a given recordset.
*/
public function num_fields($resource)
{
return mysqli_num_fields($resource);
}
/**
* Escapes a string.
*/
public function escape_string($string)
{
return mysqli_real_escape_string($this->connection, $string);
}
/**
* Unescapes a string.
*/
public function unescape_string($string)
{
return stripslashes($string);
}
/**
* Returns the last MySQL error.
*/
public function error()
{
return mysqli_error($this->connection);
}
public function server_info()
{
return mysqli_get_server_info($this->connection);
}
/**
* Selects a database on a given connection.
*/
public function select_db($database)
{
return mysqli_select_db($database, $this->connection);
}
/**
* Returns the amount of rows affected by the previous query.
*/
public function affected_rows()
{
return mysqli_affected_rows($this->connection);
}
/**
* Returns the id of the row created by a previous query.
*/
public function insert_id()
{
return mysqli_insert_id($this->connection);
}
/**
* Do a MySQL transaction.
*/
public function transaction($operation = 'commit')
{
switch ($operation)
{
case 'begin':
case 'rollback':
case 'commit':
return @mysqli_query($this->connection, strtoupper($operation));
default:
return false;
}
}
/**
* Function used as a callback for the preg_match function that parses variables into database queries.
*/
private function replacement_callback($matches)
{
list ($values, $connection) = $this->db_callback;
if (!isset($matches[2]))
trigger_error('Invalid value inserted or no type specified.', E_USER_ERROR);
if (!isset($values[$matches[2]]))
trigger_error('The database value you\'re trying to insert does not exist: ' . htmlspecialchars($matches[2]), E_USER_ERROR);
$replacement = $values[$matches[2]];
switch ($matches[1])
{
case 'int':
if ((!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) && $replacement !== 'NULL')
trigger_error('Wrong value type sent to the database. Integer expected.', E_USER_ERROR);
return $replacement !== 'NULL' ? (string) (int) $replacement : 'NULL';
break;
case 'string':
case 'text':
return $replacement !== 'NULL' ? sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $replacement)) : 'NULL';
break;
case 'array_int':
if (is_array($replacement))
{
if (empty($replacement))
trigger_error('Database error, given array of integer values is empty.', E_USER_ERROR);
foreach ($replacement as $key => $value)
{
if (!is_numeric($value) || (string) $value !== (string) (int) $value)
trigger_error('Wrong value type sent to the database. Array of integers expected.', E_USER_ERROR);
$replacement[$key] = (string) (int) $value;
}
return implode(', ', $replacement);
}
else
trigger_error('Wrong value type sent to the database. Array of integers expected.', E_USER_ERROR);
break;
case 'array_string':
if (is_array($replacement))
{
if (empty($replacement))
trigger_error('Database error, given array of string values is empty.', E_USER_ERROR);
foreach ($replacement as $key => $value)
$replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value));
return implode(', ', $replacement);
}
else
trigger_error('Wrong value type sent to the database. Array of strings expected.', E_USER_ERROR);
break;
case 'date':
if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1)
return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
elseif ($replacement === 'NULL')
return 'NULL';
else
trigger_error('Wrong value type sent to the database. Date expected.', E_USER_ERROR);
break;
case 'datetime':
if (is_a($replacement, 'DateTime'))
return $replacement->format('\'Y-m-d H:i:s\'');
elseif (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) (\d{2}):(\d{2}):(\d{2})$~', $replacement, $date_matches) === 1)
return sprintf('\'%04d-%02d-%02d %02d:%02d:%02d\'', $date_matches[1], $date_matches[2], $date_matches[3], $date_matches[4], $date_matches[5], $date_matches[6]);
elseif ($replacement === 'NULL')
return 'NULL';
else
trigger_error('Wrong value type sent to the database. DateTime expected.', E_USER_ERROR);
break;
case 'float':
if (!is_numeric($replacement) && $replacement !== 'NULL')
trigger_error('Wrong value type sent to the database. Floating point number expected.', E_USER_ERROR);
return $replacement !== 'NULL' ? (string) (float) $replacement : 'NULL';
break;
case 'identifier':
// Backticks inside identifiers are supported as of MySQL 4.1. We don't need them here.
return '`' . strtr($replacement, array('`' => '', '.' => '')) . '`';
break;
case 'raw':
return $replacement;
break;
case 'bool':
case 'boolean':
// In mysql this is a synonym for tinyint(1)
return (bool)$replacement ? 1 : 0;
break;
default:
trigger_error('Undefined type <b>' . $matches[1] . '</b> used in the database query', E_USER_ERROR);
break;
}
}
/**
* Escapes and quotes a string using values passed, and executes the query.
*/
public function query($db_string, $db_values = array())
{
// One more query....
$this->query_count ++;
// Overriding security? This is evil!
$security_override = $db_values === 'security_override' || !empty($db_values['security_override']);
// Please, just use new style queries.
if (strpos($db_string, '\'') !== false && !$security_override)
trigger_error('Hack attempt!', 'Illegal character (\') used in query.', E_USER_ERROR);
if (!$security_override && !empty($db_values))
{
// Set some values for use in the callback function.
$this->db_callback = array($db_values, $this->connection);
// Insert the values passed to this function.
$db_string = preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~', array(&$this, 'replacement_callback'), $db_string);
// Save some memory.
$this->db_callback = [];
}
if (defined("DB_LOG_QUERIES") && DB_LOG_QUERIES)
$this->logged_queries[] = $db_string;
$return = @mysqli_query($this->connection, $db_string, empty($this->unbuffered) ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
if (!$return)
{
$clean_sql = implode("\n", array_map('trim', explode("\n", $db_string)));
trigger_error($this->error() . '<br>' . $clean_sql, E_USER_ERROR);
}
return $return;
}
/**
* Escapes and quotes a string just like db_query, but does not execute the query.
* Useful for debugging purposes.
*/
public function quote($db_string, $db_values = array())
{
// Please, just use new style queries.
if (strpos($db_string, '\'') !== false)
trigger_error('Hack attempt!', 'Illegal character (\') used in query.', E_USER_ERROR);
// Save some values for use in the callback function.
$this->db_callback = array($db_values, $this->connection);
// Insert the values passed to this function.
$db_string = preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~', array(&$this, 'replacement_callback'), $db_string);
// Save some memory.
$this->db_callback = array();
return $db_string;
}
/**
* Executes a query, returning an array of all the rows it returns.
*/
public function queryRow($db_string, $db_values = array())
{
$res = $this->query($db_string, $db_values);
if (!$res || $this->num_rows($res) == 0)
return array();
$row = $this->fetch_row($res);
$this->free_result($res);
return $row;
}
/**
* Executes a query, returning an array of all the rows it returns.
*/
public function queryRows($db_string, $db_values = array())
{
$res = $this->query($db_string, $db_values);
if (!$res || $this->num_rows($res) == 0)
return array();
$rows = array();
while ($row = $this->fetch_row($res))
$rows[] = $row;
$this->free_result($res);
return $rows;
}
/**
* Executes a query, returning an array of all the rows it returns.
*/
public function queryPair($db_string, $db_values = array())
{
$res = $this->query($db_string, $db_values);
if (!$res || $this->num_rows($res) == 0)
return array();
$rows = array();
while ($row = $this->fetch_row($res))
$rows[$row[0]] = $row[1];
$this->free_result($res);
return $rows;
}
/**
* Executes a query, returning an array of all the rows it returns.
*/
public function queryPairs($db_string, $db_values = array())
{
$res = $this->query($db_string, $db_values);
if (!$res || $this->num_rows($res) == 0)
return array();
$rows = array();
while ($row = $this->fetch_assoc($res))
{
$key_value = reset($row);
$rows[$key_value] = $row;
}
$this->free_result($res);
return $rows;
}
/**
* Executes a query, returning an associative array of all the rows it returns.
*/
public function queryAssoc($db_string, $db_values = array())
{
$res = $this->query($db_string, $db_values);
if (!$res || $this->num_rows($res) == 0)
return array();
$row = $this->fetch_assoc($res);
$this->free_result($res);
return $row;
}
/**
* Executes a query, returning an associative array of all the rows it returns.
*/
public function queryAssocs($db_string, $db_values = array(), $connection = null)
{
$res = $this->query($db_string, $db_values);
if (!$res || $this->num_rows($res) == 0)
return array();
$rows = array();
while ($row = $this->fetch_assoc($res))
$rows[] = $row;
$this->free_result($res);
return $rows;
}
/**
* Executes a query, returning the first value of the first row.
*/
public function queryValue($db_string, $db_values = array())
{
$res = $this->query($db_string, $db_values);
// If this happens, you're doing it wrong.
if (!$res || $this->num_rows($res) == 0)
return null;
list($value) = $this->fetch_row($res);
$this->free_result($res);
return $value;
}
/**
* Executes a query, returning an array of the first value of each row.
*/
public function queryValues($db_string, $db_values = array())
{
$res = $this->query($db_string, $db_values);
if (!$res || $this->num_rows($res) == 0)
return array();
$rows = array();
while ($row = $this->fetch_row($res))
$rows[] = $row[0];
$this->free_result($res);
return $rows;
}
/**
* This function can be used to insert data into the database in a secure way.
*/
public function insert($method = 'replace', $table, $columns, $data)
{
// With nothing to insert, simply return.
if (empty($data))
return;
// Inserting data as a single row can be done as a single array.
if (!is_array($data[array_rand($data)]))
$data = array($data);
// Create the mold for a single row insert.
$insertData = '(';
foreach ($columns as $columnName => $type)
{
// Are we restricting the length?
if (strpos($type, 'string-') !== false)
$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
else
$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
}
$insertData = substr($insertData, 0, -2) . ')';
// Create an array consisting of only the columns.
$indexed_columns = array_keys($columns);
// Here's where the variables are injected to the query.
$insertRows = array();
foreach ($data as $dataRow)
$insertRows[] = $this->quote($insertData, array_combine($indexed_columns, $dataRow));
// Determine the method of insertion.
$queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT');
// Do the insert.
return $this->query('
' . $queryTitle . ' INTO ' . $table . ' (`' . implode('`, `', $indexed_columns) . '`)
VALUES
' . implode(',
', $insertRows),
array(
'security_override' => true,
)
);
}
}

140
models/Dispatcher.php Normal file
View File

@@ -0,0 +1,140 @@
<?php
/*****************************************************************************
* Dispatcher.php
* Contains key class Dispatcher.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Dispatcher
{
public static function route()
{
$possibleActions = [
'albums' => 'ViewPhotoAlbums',
'editasset' => 'EditAsset',
'edituser' => 'EditUser',
'login' => 'Login',
'logout' => 'Logout',
'managecomments' => 'ManageComments',
'manageerrors' => 'ManageErrors',
'managetags' => 'ManageTags',
'manageusers' => 'ManageUsers',
'people' => 'ViewPeople',
'suggest' => 'ProvideAutoSuggest',
'timeline' => 'ViewTimeline',
'uploadmedia' => 'UploadMedia',
];
// Work around PHP's FPM not always providing PATH_INFO.
if (empty($_SERVER['PATH_INFO']) && isset($_SERVER['REQUEST_URI']))
{
if (strpos($_SERVER['REQUEST_URI'], '?') === false)
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
else
$_SERVER['PATH_INFO'] = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?'));
}
// Nothing in particular? Just show the root of the album list.
if (empty($_SERVER['PATH_INFO']) || $_SERVER['PATH_INFO'] == '/')
{
return new ViewPhotoAlbum();
}
// An album, person, or any other tag?
elseif (preg_match('~^/(?<tag>.+?)(?:/page/(?<page>\d+))?/?$~', $_SERVER['PATH_INFO'], $path) && Tag::matchSlug($path['tag']))
{
$_GET = array_merge($_GET, $path);
return new ViewPhotoAlbum();
}
// Look for an action...
elseif (preg_match('~^/(?<action>[a-z]+)(?:/page/(?<page>\d+))?/?~', $_SERVER['PATH_INFO'], $path) && isset($possibleActions[$path['action']]))
{
$_GET = array_merge($_GET, $path);
return new $possibleActions[$path['action']]();
}
else
throw new NotFoundException();
}
public static function dispatch()
{
// Let's try to find our bearings!
try
{
$page = self::route();
$page->showContent();
}
// Something wasn't found?
catch (NotFoundException $e)
{
self::trigger404();
}
// Or are they just sneaking into areas they don't belong?
catch (NotAllowedException $e)
{
if (Registry::get('user')->isGuest())
self::kickGuest();
else
self::trigger403();
}
catch (Exception $e)
{
ErrorHandler::handleError(E_USER_ERROR, 'Unspecified exception: ' . $e->getMessage(), $e->getFile(), $e->getLine());
}
catch (Error $e)
{
ErrorHandler::handleError(E_USER_ERROR, 'Fatal error: ' . $e->getMessage(), $e->getFile(), $e->getLine());
}
}
/**
* Kicks a guest to a login form, redirecting them back to this page upon login.
*/
public static function kickGuest()
{
$form = new LogInForm('Log in');
$form->setErrorMessage('Admin access required. Please log in.');
$form->setRedirectUrl($_SERVER['REQUEST_URI']);
$page = new MainTemplate('Login required');
$page->appendStylesheet(BASEURL . '/css/admin.css');
$page->adopt($form);
$page->html_main();
exit;
}
public static function trigger400()
{
header('HTTP/1.1 400 Bad Request');
$page = new MainTemplate('Bad request');
$page->adopt(new DummyBox('Bad request', '<p>The server does not understand your request.</p>'));
$page->html_main();
exit;
}
public static function trigger403()
{
header('HTTP/1.1 403 Forbidden');
$page = new MainTemplate('Access denied');
$page->adopt(new DummyBox('Forbidden', '<p>You do not have access to the page you requested.</p>'));
$page->html_main();
exit;
}
public static function trigger404()
{
header('HTTP/1.1 404 Not Found');
$page = new MainTemplate('Page not found');
if (Registry::has('user') && Registry::get('user')->isAdmin())
{
$page->appendStylesheet(BASEURL . '/css/admin.css');
$page->adopt(new AdminBar());
}
$page->adopt(new DummyBox('Well, this is a bit embarrassing!', '<p>The page you requested could not be found. Don\'t worry, it\'s probably not your fault. You\'re welcome to browse the website, though!</p>', 'errormsg'));
$page->addClass('errorpage');
$page->html_main();
exit;
}
}

135
models/EXIF.php Normal file
View File

@@ -0,0 +1,135 @@
<?php
class EXIF
{
public $aperture = 0;
public $credit = '';
public $camera = '';
public $caption = '';
public $created_timestamp = 0;
public $copyright = '';
public $focal_length = 0;
public $iso = 0;
public $shutter_speed = 0;
public $title = '';
private function __construct(array $meta)
{
foreach ($meta as $key => $value)
$this->$key = $value;
}
public static function fromFile($file, $as_array = false, $override_cache = false)
{
if (!file_exists($file))
return false;
$meta = [
'aperture' => 0,
'credit' => '',
'camera' => '',
'caption' => '',
'created_timestamp' => 0,
'copyright' => '',
'focal_length' => 0,
'iso' => 0,
'shutter_speed' => 0,
'title' => '',
];
if (!function_exists('exif_read_data'))
throw new Exception("The PHP module 'exif' appears to be disabled. Please enable it to use EXIF functions.");
list(, , $image_type) = getimagesize($file);
if (!in_array($image_type, [IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM]))
return new self($meta);
$exif = @exif_read_data($file);
if (!empty($exif['Title']))
$meta['title'] = trim($exif['Title']);
if (!empty($exif['ImageDescription']))
{
if (empty($meta['title']) && strlen($exif['ImageDescription']) < 80)
{
$meta['title'] = trim($exif['ImageDescription']);
if (!empty($exif['COMPUTED']['UserComment']) && trim($exif['COMPUTED']['UserComment']) != $meta['title'])
$meta['caption'] = trim($exif['COMPUTED']['UserComment']);
}
elseif (trim($exif['ImageDescription']) != $meta['title'])
$meta['caption'] = trim($exif['ImageDescription']);
}
elseif (!empty($exif['Comments']) && trim($exif['Comments']) != $meta['title'])
$meta['caption'] = trim($exif['Comments']);
if (!empty($exif['Artist']))
$meta['credit'] = trim($exif['Artist']);
elseif (!empty($exif['Author']))
$meta['credit'] = trim($exif['Author']);
if (!empty($exif['Copyright']))
$meta['copyright'] = trim($exif['Copyright']);
if (!empty($exif['ExposureTime']))
$meta['shutter_speed'] = (string) self::frac2dec($exif['ExposureTime']);
if (!empty($exif['FNumber']))
$meta['aperture'] = round(self::frac2dec($exif['FNumber']), 2);
if (!empty($exif['FocalLength']))
$meta['focal_length'] = (string) self::frac2dec($exif['FocalLength']);
if (!empty($exif['ISOSpeedRatings']))
{
$meta['iso'] = is_array($exif['ISOSpeedRatings']) ? reset($exif['ISOSpeedRatings']) : $exif['ISOSpeedRatings'];
$meta['iso'] = trim($meta['iso']);
}
if (!empty($exif['Model']))
{
if (!empty($exif['Make']) && strpos($exif['Model'], $exif['Make']) === false)
$meta['camera'] = trim($exif['Make']) . ' ' . trim($exif['Model']);
else
$meta['camera'] = trim($exif['Model']);
}
elseif (!empty($exif['Make']))
$meta['camera'] = trim($exif['Make']);
if (!empty($exif['DateTimeDigitized']))
$meta['created_timestamp'] = self::toUnixTime($exif['DateTimeDigitized']);
return new self($meta);
}
public function shutterSpeedFraction()
{
$speed = $this->shutter_speed;
if (empty($this->shutter_speed))
return '';
if (1 / $this->shutter_speed <= 1)
return $this->shutter_speed . ' seconds';
$speed = (float) 1 / $this->shutter_speed;
if (in_array($speed, [1.3, 1.5, 1.6, 2.5]))
return "1/" . number_format($speed, 1, '.', '') . ' second';
else
return "1/" . number_format($speed, 0, '.', '') . ' second';
}
// Convert a fraction string to a decimal.
private static function frac2dec($str)
{
@list($n, $d) = explode('/', $str);
return !empty($d) ? $n / $d : $str;
}
// Convert an EXIF formatted date to a UNIX timestamp.
private static function toUnixTime($str)
{
@list($date, $time) = explode(' ', trim($str));
@list($y, $m, $d) = explode(':', $date);
return strtotime("{$y}-{$m}-{$d} {$time}");
}
}

159
models/ErrorHandler.php Normal file
View File

@@ -0,0 +1,159 @@
<?php
/*****************************************************************************
* ErrorHandler.php
* Contains key class ErrorHandler.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class ErrorHandler
{
private static $error_count = 0;
private static $handling_error;
// Handler for standard PHP error messages.
public static function handleError($error_level, $error_message, $file, $line, $context = null)
{
// Don't handle suppressed errors (e.g. through @ operator)
if (!(error_reporting() & $error_level))
return;
// Prevent recursing if we've messed up in this code path.
if (self::$handling_error)
return;
self::$error_count++;
self::$handling_error = true;
// Basically, htmlspecialchars it, minus '&' for HTML entities.
$error_message = strtr($error_message, ['<' => '&lt;', '>' => '&gt;', '"' => '&quot;', "\t" => ' ']);
$error_message = strtr($error_message, ['&lt;br&gt;' => "<br>", '&lt;br /&gt;' => "<br>", '&lt;b&gt;' => '<strong>', '&lt;/b&gt;' => '</strong>', '&lt;pre&gt;' => '<pre>', '&lt;/pre&gt;' => '</pre>']);
// Generate a bunch of useful information to ease debugging later.
$debug_info = self::getDebugInfo(debug_backtrace());
// Log the error in the database.
self::logError($error_message, $debug_info, $file, $line);
// Are we considering this fatal? Then display and exit.
// !!! TODO: should we consider warnings fatal?
if (true) // DEBUG || (!DEBUG && $error_level === E_WARNING || $error_level === E_USER_WARNING))
self::display($file . ' (' . $line . ')<br>' . $error_message, $debug_info);
// If it wasn't a fatal error, well...
self::$handling_error = false;
}
public static function getDebugInfo(array $trace)
{
$debug_info = "Backtrace:\n";
$debug_info .= self::formatBacktrace($trace);
// Include info on the contents of superglobals.
if (!empty($_SESSION))
$debug_info .= "\nSESSION: " . print_r($_SESSION, true);
if (!empty($_POST))
$debug_info .= "\nPOST: " . print_r($_POST, true);
if (!empty($_GET))
$debug_info .= "\nGET: " . print_r($_GET, true);
return $debug_info;
}
private static function formatBacktrace(array $trace)
{
$buffer = '';
$skipping = true;
foreach ($trace as $i => $call)
{
if (isset($call['class']) && ($call['class'] === 'ErrorHandler' || $call['class'] === 'Database') ||
isset($call['function']) && $call['function'] === 'preg_replace_callback')
{
if (!$skipping)
{
$buffer .= "[...]\n";
$skipping = true;
}
continue;
}
else
$skipping = false;
$file = isset($call['file']) ? str_replace(BASEDIR, '', $call['file']) : 'Unknown';
$object = isset($call['class']) ? $call['class'] . $call['type'] : '';
$args = [];
foreach ($call['args'] as $j => $arg)
{
if (is_array($arg))
$args[$j] = print_r($arg, true);
elseif (is_object($arg))
$args[$j] = var_dump($arg);
}
$buffer .= '#' . str_pad($i, 3, ' ')
. $object . $call['function'] . '(' . implode(', ', $args) . ')'
. ' called at [' . $file . ':' . $call['line'] . "]\n";
}
return $buffer;
}
// Logs an error into the database.
private static function logError($error_message = '', $debug_info = '', $file = '', $line = 0)
{
if (!ErrorLog::log([
'message' => $error_message,
'debug_info' => $debug_info,
'file' => str_replace(BASEDIR, '', $file),
'line' => $line,
'id_user' => Registry::has('user') ? Registry::get('user')->getUserId() : 0,
'ip_address' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '',
'request_uri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '',
]))
{
header('HTTP/1.1 503 Service Temporarily Unavailable');
echo '<h2>An Error Occured</h2><p>Our software could not connect to the database. We apologise for any inconvenience and ask you to check back later.</p>';
exit;
}
return $error_message;
}
public static function display($message, $debug_info)
{
// Just show the message if we're running in a console.
if (empty($_SERVER['HTTP_HOST']))
{
echo $message;
exit;
}
// Initialise the main template to present a nice message to the user.
$page = new MainTemplate('An error occured!');
// Show the error.
$is_admin = Registry::has('user') && Registry::get('user')->isAdmin();
if (DEBUG || $is_admin)
{
$page->adopt(new DummyBox('An error occured!', '<p>' . $message . '</p><pre>' . $debug_info . '</pre>'));
// Let's provide the admin navigation despite it all!
if ($is_admin)
{
$page->appendStylesheet(BASEURL . '/css/admin.css');
$page->adopt(new AdminBar());
}
}
else
$page->adopt(new DummyBox('An error occured!', '<p>Our apologies, an error occured while we were processing your request. Please try again later, or contact us if the problem persists.</p>'));
// If we got this far, make sure we're not showing stuff twice.
ob_end_clean();
// Render the page.
$page->html_main();
exit;
}
}

36
models/ErrorLog.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
/*****************************************************************************
* ErrorLog.php
* Contains key class ErrorLog.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class ErrorLog
{
public static function log(array $data)
{
if (!Registry::has('db'))
return false;
return Registry::get('db')->query('
INSERT INTO log_errors
(id_user, message, debug_info, file, line, request_uri, time, ip_address)
VALUES
({int:id_user}, {string:message}, {string:debug_info}, {string:file}, {int:line},
{string:request_uri}, CURRENT_TIMESTAMP, {string:ip_address})',
$data);
}
public static function flush()
{
return Registry::get('db')->query('TRUNCATE log_errors');
}
public static function getCount()
{
return Registry::get('db')->queryValue('
SELECT COUNT(*)
FROM log_errors');
}
}

165
models/Form.php Normal file
View File

@@ -0,0 +1,165 @@
<?php
/*****************************************************************************
* Form.php
* Contains key class Form.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Form
{
public $request_method;
public $request_url;
public $content_above;
public $content_below;
private $fields;
private $data;
private $missing;
// NOTE: this class does not verify the completeness of form options.
public function __construct($options)
{
$this->request_method = !empty($options['request_method']) ? $options['request_method'] : 'POST';
$this->request_url = !empty($options['request_url']) ? $options['request_url'] : BASEURL;
$this->fields = !empty($options['fields']) ? $options['fields'] : [];
$this->content_below = !empty($options['content_below']) ? $options['content_below'] : null;
$this->content_above = !empty($options['content_above']) ? $options['content_above'] : null;
}
public function verify($post)
{
$this->data = [];
$this->missing = [];
foreach ($this->fields as $field_id => $field)
{
// Field disabled?
if (!empty($field['disabled']))
{
$this->data[$field_id] = '';
continue;
}
// No data present at all for this field?
if ((!isset($post[$field_id]) || $post[$field_id] == '') && empty($field['is_optional']))
{
$this->missing[] = $field_id;
$this->data[$field_id] = '';
continue;
}
// Verify data for all fields
switch ($field['type'])
{
case 'select':
case 'radio':
// Skip validation? Dangerous territory!
if (isset($field['verify_options']) && $field['verify_options'] === false)
$this->data[$field_id] = $post[$field_id];
// Check whether selected option is valid.
elseif (isset($post[$field_id]) && !isset($field['options'][$post[$field_id]]))
{
$this->missing[] = $field_id;
$this->data[$field_id] = '';
continue;
}
else
$this->data[$field_id] = $post[$field_id];
break;
case 'checkbox':
// Just give us a 'boolean' int for this one
$this->data[$field_id] = empty($post[$field_id]) ? 0 : 1;
break;
case 'color':
// Colors are stored as a string of length 3 or 6 (hex)
if (!isset($post[$field_id]) || (strlen($post[$field_id]) != 3 && strlen($post[$field_id]) != 6))
{
$this->missing[] = $field_id;
$this->data[$field_id] = '';
continue;
}
else
$this->data[$field_id] = $post[$field_id];
break;
case 'file':
// Needs to be verified elsewhere!
break;
case 'numeric':
$data = isset($post[$field_id]) ? $post[$field_id] : '';
// Do we need to check bounds?
if (isset($field['min_value']) && is_numeric($data))
{
if (is_float($field['min_value']) && (float) $data < $field['min_value'])
{
$this->missing[] = $field_id;
$this->data[$field_id] = 0.0;
}
elseif (is_int($field['min_value']) && (int) $data < $field['min_value'])
{
$this->missing[] = $field_id;
$this->data[$field_id] = 0;
}
else
$this->data[$field_id] = $data;
}
elseif (isset($field['max_value']) && is_numeric($data))
{
if (is_float($field['max_value']) && (float) $data > $field['max_value'])
{
$this->missing[] = $field_id;
$this->data[$field_id] = 0.0;
}
elseif (is_int($field['max_value']) && (int) $data > $field['max_value'])
{
$this->missing[] = $field_id;
$this->data[$field_id] = 0;
}
else
$this->data[$field_id] = $data;
}
// Does it look numeric?
elseif (is_numeric($data))
{
$this->data[$field_id] = $data;
}
// Let's consider it missing, then.
else
{
$this->missing[] = $field_id;
$this->data[$field_id] = 0;
}
break;
case 'text':
case 'textarea':
default:
$this->data[$field_id] = isset($post[$field_id]) ? $post[$field_id] : '';
}
}
}
public function setData($data)
{
$this->verify($data);
$this->missing = [];
}
public function getFields()
{
return $this->fields;
}
public function getData()
{
return $this->data;
}
public function getMissing()
{
return $this->missing;
}
}

246
models/GenericTable.php Normal file
View File

@@ -0,0 +1,246 @@
<?php
/*****************************************************************************
* GenericTable.php
* Contains key class GenericTable.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class GenericTable extends PageIndex
{
protected $header = [];
protected $body = [];
protected $page_index = [];
protected $title;
protected $title_class;
protected $tableIsSortable = false;
protected $recordCount;
protected $needsPageIndex = false;
protected $current_page;
protected $num_pages;
public $form_above;
public $form_below;
public function __construct($options)
{
// Make sure we're actually sorting on something sortable.
if (!isset($options['sort_order']) || (!empty($options['sort_order']) && empty($options['columns'][$options['sort_order']]['is_sortable'])))
$options['sort_order'] = '';
// Order in which direction?
if (!empty($options['sort_direction']) && !in_array($options['sort_direction'], array('up', 'down')))
$options['sort_direction'] = 'up';
// 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'] : array());
// Should we create a page index?
$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'];
// 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 = array($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);
// Clean up a bit.
$rows = $data['rows'];
$this->sort_order = $data['order'];
$this->sort_direction = $data['direction'];
unset($data);
// Okay, now for the column headers...
$this->generateColumnHeaders($options);
// Generate a pagination if requested
if ($this->needsPageIndex)
$this->generatePageIndex();
// Not a single row in sight?
if (empty($rows))
$this->body = $options['no_items_label'];
// Otherwise, parse it all!
else
$this->parseAllRows($rows, $options);
// Got a title?
$this->title = isset($options['title']) ? htmlentities($options['title']) : '';
$this->title_class = isset($options['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);
}
private function generateColumnHeaders($options)
{
foreach ($options['columns'] as $key => $column)
{
if (empty($column['header']))
continue;
$header = array(
'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'),
'label' => $column['header'],
'scope' => 'col',
'sort_mode' => $key == $this->sort_order ? $this->sort_direction : null,
'width' => !empty($column['header_width']) && is_int($column['header_width']) ? $column['header_width'] : null,
);
$this->header[] = $header;
}
}
private function parseAllRows($rows, $options)
{
// Parse all rows...
$i = 0;
foreach ($rows as $row)
{
$i ++;
$newRow = array(
'class' => $i %2 == 1 ? 'odd' : 'even',
'cells' => array(),
);
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 = array($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':
$pattern = !empty($column['parse']['data']['pattern']) && $column['parse']['data']['pattern'] == 'long' ? '%F %H:%M' : '%H:%M';
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'][] = array(
'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;
}
}
protected function getLink($start = null, $order = null, $dir = null)
{
if ($start === null)
$start = $this->start;
if ($order === null)
$order = $this->sort_order;
if ($dir === null)
$dir = $this->sort_direction;
return $this->base_url . (strpos($this->base_url, '?') ? '&' : '?') . 'start=' . $start . '&order=' . $order. '&dir=' . $dir;
}
public function getOffset()
{
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;
}
public function getBody()
{
return $this->body;
}
public function getTitle()
{
return $this->title;
}
public function getTitleClass()
{
return $this->title_class;
}
}

31
models/Guest.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
/*****************************************************************************
* Guest.php
* Contains key class Guest, derived from User.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
/**
* Guest model; sets typical guest settings to the common attributes.
*/
class Guest extends User
{
/**
* Constructor for the Guest class. Sets common attributes.
*/
public function __construct()
{
$this->id_user = 0;
$this->is_logged = false;
$this->is_guest = true;
$this->is_admin = false;
$this->first_name = 'Guest';
$this->last_name = '';
}
public function updateAccessTime()
{
return true;
}
}

382
models/Image.php Normal file
View File

@@ -0,0 +1,382 @@
<?php
/*****************************************************************************
* Image.php
* Contains key class Image.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Image extends Asset
{
const TYPE_PANORAMA = 1;
const TYPE_LANDSCAPE = 2;
const TYPE_PORTRAIT = 4;
protected function __construct(array $data)
{
foreach ($data as $attribute => $value)
$this->$attribute = $value;
}
public static function fromId($id_asset, $return_format = 'object')
{
$asset = parent::fromId($id_asset, 'array');
if ($asset)
return $return_format == 'object' ? new Image($asset) : $asset;
else
return false;
}
public static function fromIds(array $id_assets, $return_format = 'object')
{
if (empty($id_assets))
return [];
$assets = parent::fromIds($id_assets, 'array');
if ($return_format == 'array')
return $assets;
else
{
$objects = [];
foreach ($assets as $id => $asset)
$objects[$id] = new Image($asset);
return $objects;
}
}
public function save()
{
$data = [];
foreach ($this->meta as $key => $value)
$data[] = [
'id_asset' => $this->id_asset,
'variable' => $key,
'value' => $value,
];
return Registry::get('db')->insert('replace', 'assets_meta', [
'id_asset' => 'int',
'variable' => 'string',
'value' => 'string',
], $data);
}
public function getExif()
{
return EXIF::fromFile($this->getPath());
}
public function getPath()
{
return ASSETSDIR . '/' . $this->subdir . '/' . $this->filename;
}
public function getUrl()
{
return ASSETSURL . '/' . $this->subdir . '/' . $this->filename;
}
/**
* @param width: width of the thumbnail.
* @param height: height of the thumbnail.
* @param crop: whether and how to crop original image to fit. [false|true|'top'|'center'|'bottom']
* @param fit: whether to fit the image to given boundaries [true], or use them merely as an estimation [false].
*/
public function getThumbnailUrl($width, $height, $crop = true, $fit = true)
{
// First, assert the image's dimensions are properly known in the database.
if (!isset($this->image_height, $this->image_width))
throw new UnexpectedValueException('Image width or height is undefined -- inconsistent database?');
// Inferring width or height?
if (!$height)
$height = ceil($width / $this->image_width * $this->image_height);
elseif (!$width)
$width = ceil($height / $this->image_height * $this->image_width);
// Inferring the height from the original image's ratio?
if (!$fit)
$height = floor($width / ($this->image_width / $this->image_height));
// Assert we have both, now...
if (empty($width) || empty($height))
throw new InvalidArgumentException('Expecting at least either width or height as argument.');
// If we're cropping, verify we're in the right mode.
if ($crop)
{
// If the original image's aspect ratio is much wider, take a slice instead.
if ($this->image_width / $this->image_height > $width / $height)
$crop = 'slice';
// We won't be cropping if the thumbnail is proportional to its original.
if (abs($width / $height - $this->image_width / $this->image_height) <= 0.05)
$crop = false;
}
// Do we have an exact crop boundary for these dimensions?
$crop_selector = "crop_{$width}x{$height}";
if (isset($this->meta[$crop_selector]))
$crop = 'exact';
// Now, do we need to suffix the filename?
if ($crop)
$suffix = '_c' . (is_string($crop) && $crop !== 'center' ? substr($crop, 0, 1) : '');
else
$suffix = '';
// Check whether we already resized this earlier.
$thumb_selector = "thumb_{$width}x{$height}{$suffix}";
if (isset($this->meta[$thumb_selector]) && file_exists(THUMBSDIR . '/' . $this->subdir . '/' . $this->meta[$thumb_selector]))
return THUMBSURL . '/' . $this->subdir . '/' . $this->meta[$thumb_selector];
// Do we have a custom thumbnail on file?
$custom_selector = "custom_{$width}x{$height}";
if (isset($this->meta[$custom_selector]))
{
if (file_exists(ASSETSDIR . '/' . $this->subdir . '/' . $this->meta[$custom_selector]))
{
// Copy the custom thumbail to the general thumbnail directory.
copy(ASSETSDIR . '/' . $this->subdir . '/' . $this->meta[$custom_selector],
THUMBSDIR . '/' . $this->subdir . '/' . $this->meta[$custom_selector]);
// Let's remember this for future reference.
$this->meta[$thumb_selector] = $this->meta[$custom_selector];
$this->save();
return THUMBSURL . '/' . $this->subdir . '/' . $this->meta[$custom_selector];
}
else
throw new UnexpectedValueException('Custom thumbnail expected, but missing in file system!');
}
// Let's try some arcane stuff...
try
{
if (!class_exists('Imagick'))
throw new Exception("The PHP module 'imagick' appears to be disabled. Please enable it to use image resampling functions.");
$thumb = new Imagick(ASSETSDIR . '/' . $this->subdir . '/' . $this->filename);
// The image might have some orientation set through EXIF. Let's apply this first.
self::applyRotation($thumb);
// Just resizing? Easy peasy.
if (!$crop)
$thumb->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1);
// Cropping in the center?
elseif ($crop === true || $crop === 'center')
$thumb->cropThumbnailImage($width, $height);
// Exact cropping? We can do that.
elseif ($crop === 'exact')
{
list($crop_width, $crop_height, $crop_x_pos, $crop_y_pos) = explode(',', $this->meta[$crop_selector]);
$thumb->cropImage($crop_width, $crop_height, $crop_x_pos, $crop_y_pos);
$thumb->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1);
}
// Advanced cropping? Fun!
else
{
$size = $thumb->getImageGeometry();
// Taking a horizontal slice from the top or bottom of the original image?
if ($crop === 'top' || $crop === 'bottom')
{
$crop_width = $size['width'];
$crop_height = floor($size['width'] / $width * $height);
$target_x = 0;
$target_y = $crop === 'top' ? 0 : $size['height'] - $crop_height;
}
// Otherwise, we're taking a vertical slice from the centre.
else
{
$crop_width = floor($size['height'] / $height * $width);
$crop_height = $size['height'];
$target_x = floor(($size['width'] - $crop_width) / 2);
$target_y = 0;
}
$thumb->cropImage($crop_width, $crop_height, $target_x, $target_y);
$thumb->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1);
}
// What sort of image is this? Fall back to PNG if we must.
switch ($thumb->getImageFormat())
{
case 'JPEG':
$ext = 'jpg';
break;
case 'GIF':
$ext = 'gif';
break;
case 'PNG':
default:
$thumb->setFormat('PNG');
$ext = 'png';
break;
}
// So, how do we name this?
$thumbfilename = substr($this->filename, 0, strrpos($this->filename, '.')) . "_{$width}x{$height}{$suffix}.$ext";
// Ensure the thumbnail subdirectory exists.
if (!is_dir(THUMBSDIR . '/' . $this->subdir))
mkdir(THUMBSDIR . '/' . $this->subdir, 0755, true);
// Save it in a public spot.
$thumb->writeImage(THUMBSDIR . '/' . $this->subdir . '/' . $thumbfilename);
$thumb->clear();
$thumb->destroy();
}
// Blast! Curse your sudden but inevitable betrayal!
catch (ImagickException $e)
{
throw new Exception('ImageMagick error occurred while generating thumbnail. Output: ' . $e->getMessage());
}
// Let's remember this for future reference.
$this->meta[$thumb_selector] = $thumbfilename;
$this->save();
// Ah yes, you wanted a URL, didn't you...
return THUMBSURL . '/' . $this->subdir . '/' . $this->meta[$thumb_selector];
}
private static function applyRotation(Imagick $image)
{
switch ($image->getImageOrientation())
{
// Clockwise rotation
case Imagick::ORIENTATION_RIGHTTOP:
$image->rotateImage("#000", 90);
break;
// Counter-clockwise rotation
case Imagick::ORIENTATION_LEFTBOTTOM:
$image->rotateImage("#000", 270);
break;
// Upside down?
case Imagick::ORIENTATION_BOTTOMRIGHT:
$image->rotateImage("#000", 180);
}
// Having rotated the image, make sure the EXIF data is set properly.
$image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
}
public function bestColor()
{
// Save some computations if we can.
if (isset($this->meta['best_color']))
return $this->meta['best_color'];
// Find out what colour is most prominent.
$color = new BestColor($this);
$this->meta['best_color'] = $color->hex();
$this->save();
// There's your colour.
return $this->meta['best_color'];
}
public function bestLabelColor()
{
// Save some computations if we can.
if (isset($this->meta['best_color_label']))
return $this->meta['best_color_label'];
// Find out what colour is most prominent.
$color = new BestColor($this);
$this->meta['best_color_label'] = $color->rgba();
$this->save();
// There's your colour.
return $this->meta['best_color_label'];
}
public function width()
{
return $this->image_width;
}
public function height()
{
return $this->image_height;
}
public function isPanorama()
{
return $this->image_width / $this->image_height > 2;
}
public function isPortrait()
{
return $this->image_width / $this->image_height < 1;
}
public function isLandscape()
{
$ratio = $this->image_width / $this->image_height;
return $ratio >= 1 && $ratio <= 2;
}
public function removeAllThumbnails()
{
foreach ($this->meta as $key => $value)
{
if (substr($key, 0, 6) !== 'thumb_')
continue;
$thumb_path = THUMBSDIR . '/' . $this->subdir . '/' . $value;
if (is_file($thumb_path))
unlink($thumb_path);
unset($this->meta[$key]);
}
$this->saveMetaData();
}
public function replaceThumbnail($descriptor, $tmp_file)
{
if (!is_file($tmp_file))
return -1;
if (!isset($this->meta[$descriptor]))
return -2;
$image = new Imagick($tmp_file);
$d = $image->getImageGeometry();
unset($image);
// Check whether dimensions match.
$test_descriptor = 'thumb_' . $d['width'] . 'x' . $d['height'];
if ($descriptor !== $test_descriptor && strpos($descriptor, $test_descriptor . '_') === false)
return -3;
// Save the custom thumbnail in the assets directory.
$destination = ASSETSDIR . '/' . $this->subdir . '/' . $this->meta[$descriptor];
if (file_exists($destination) && !is_writable($destination))
return -4;
if (!copy($tmp_file, $destination))
return -5;
// Copy it to the thumbnail directory, overwriting the automatically generated one, too.
$destination = THUMBSDIR . '/' . $this->subdir . '/' . $this->meta[$descriptor];
if (file_exists($destination) && !is_writable($destination))
return -6;
if (!copy($tmp_file, $destination))
return -7;
// A little bookkeeping
$this->meta['custom_' . $d['width'] . 'x' . $d['height']] = $this->meta[$descriptor];
$this->saveMetaData();
return 0;
}
}

192
models/Member.php Normal file
View File

@@ -0,0 +1,192 @@
<?php
/*****************************************************************************
* Member.php
* Contains key class Member, derived from User.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Member extends User
{
private function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = $value;
$this->is_logged = true;
$this->is_guest = false;
$this->is_admin = $this->is_admin == 1;
}
public static function fromId($id_user)
{
$row = Registry::get('db')->queryAssoc('
SELECT *
FROM users
WHERE id_user = {int:id_user}',
[
'id_user' => $id_user,
]);
// This should never happen.
if (empty($row))
throw new NotFoundException('Cannot create Member object; user not found in db!');
return new Member($row);
}
public static function fromSlug($slug)
{
$row = Registry::get('db')->queryAssoc('
SELECT *
FROM users
WHERE slug = {string:slug}',
[
'slug' => $slug,
]);
// This shouldn't happen.
if (empty($row))
throw new NotFoundException('Cannot create Member object; user not found in db!');
return new Member($row);
}
/**
* Creates a new member from the data provided.
* @param data
*/
public static function createNew(array $data)
{
$error = false;
$new_user = [
'first_name' => !empty($data['first_name']) ? $data['first_name'] : $error |= true,
'surname' => !empty($data['surname']) ? $data['surname'] : $error |= true,
'slug' => !empty($data['slug']) ? $data['slug'] : $error |= true,
'emailaddress' => !empty($data['emailaddress']) ? $data['emailaddress'] : $error |= true,
'password_hash' => !empty($data['password']) ? Authentication::computeHash($data['password']) : $error |= true,
'creation_time' => time(),
'ip_address' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '',
'is_admin' => empty($data['is_admin']) ? 0 : 1,
];
if ($error)
return false;
$db = Registry::get('db');
$bool = $db->insert('insert', 'users', [
'first_name' => 'string-30',
'surname' => 'string-60',
'slug' => 'string-90',
'emailaddress' => 'string-255',
'password_hash' => 'string-255',
'creation_time' => 'int',
'ip_address' => 'string-15',
'is_admin' => 'int',
], $new_user, ['id_user']);
if (!$bool)
return false;
$new_user['id_user'] = $db->insert_id();
$member = new Member($new_user);
return $member;
}
/**
* Updates the member using the data provided.
* @param data
*/
public function update(array $new_data)
{
foreach ($new_data as $key => $value)
{
if (in_array($key, ['first_name', 'surname', 'slug', 'emailaddress']))
$this->$key = $value;
elseif ($key === 'password')
$this->password_hash = Authentication::computeHash($value);
elseif ($key === 'is_admin')
$this->is_admin = $value == 1 ? 1 : 0;
}
return Registry::get('db')->query('
UPDATE users
SET
first_name = {string:first_name},
surname = {string:surname},
slug = {string:slug},
emailaddress = {string:emailaddress},
password_hash = {string:password_hash},
is_admin = {int:is_admin}
WHERE id_user = {int:id_user}',
get_object_vars($this));
}
/**
* Deletes the member.
* @param data
*/
public function delete()
{
return Registry::get('db')->query('
DELETE FROM users
WHERE id_user = {int:id_user}',
['id_user' => $this->id_user]);
}
/**
* Checks whether an email address is already linked to an account.
* @param emailaddress to check
* @return false if account does not exist
* @return user id if user does exist
*/
public static function exists($emailaddress)
{
$res = Registry::get('db')->queryValue('
SELECT id_user
FROM users
WHERE emailaddress = {string:emailaddress}',
[
'emailaddress' => $emailaddress,
]);
if (empty($res))
return false;
return $res;
}
public function updateAccessTime()
{
return Registry::get('db')->query('
UPDATE users
SET
last_action_time = {int:now},
ip_address = {string:ip}
WHERE id_user = {int:id}',
[
'now' => time(),
'id' => $this->id_user,
'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '',
]);
}
public function getUrl()
{
return BASEURL . '/author/' . $this->slug . '/';
}
public static function getCount()
{
return Registry::get('db')->queryValue('
SELECT COUNT(*)
FROM users');
}
public function getProps()
{
// We should probably phase out the use of this function, or refactor the access levels of member properties...
return get_object_vars($this);
}
}

View File

@@ -0,0 +1,12 @@
<?php
/*****************************************************************************
* NotAllowedException.php
* Contains exception class NotAllowedException.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class NotAllowedException extends Exception
{
}

View File

@@ -0,0 +1,12 @@
<?php
/*****************************************************************************
* NotFoundException.php
* Contains exception class NotFoundException.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class NotFoundException extends Exception
{
}

189
models/PageIndex.php Normal file
View File

@@ -0,0 +1,189 @@
<?php
/*****************************************************************************
* PageIndex.php
* Contains key class PageIndex.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class PageIndex
{
protected $page_index = [];
protected $current_page = 0;
protected $items_per_page = 0;
protected $needsPageIndex = false;
protected $num_pages = 0;
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%';
public function __construct($options)
{
foreach ($options as $key => $value)
$this->$key = $value;
$this->generatePageIndex();
}
protected function generatePageIndex()
{
/*
Example 1:
[1] [2] [3] [...] [c-2] [c-1] [c] [c+1] [c+2] [...] [n-2] [n-1] [n]
\---------/ \-------------------------/ \-------------/
lower current/contiguous pages upper
Example 2:
[1] [2] [3] [4] [5] [c] [6] [7] [...] [n/2] [...] [n-2] [n-1] [n]
\---------/ \-----------------/ \---/ \-------------/
lower current/cont. pgs. center upper
*/
$this->num_pages = 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)
{
$this->needsPageIndex = false;
return;
}
$lowerLower = 1;
$lowerUpper = min($this->num_pages, 3);
$contigLower = max($lowerUpper + 1, max($this->current_page - 2, 1));
$contigUpper = min($this->current_page + 2, $this->num_pages);
$center = floor($this->num_pages / 2);
$upperLower = max($contigUpper + 1, max(0, $this->num_pages - 2));
$upperUpper = $this->num_pages;
$this->page_index = [];
// Lower pages
for ($p = $lowerLower; $p <= $lowerUpper; $p++)
$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),
];
// The center of the page index.
if ($center > $lowerUpper && $center < $contigLower)
{
// Gap?
if ($lowerUpper != $center)
$this->page_index[] = '...';
$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),
];
}
// Gap?
if ($contigLower != $p)
$this->page_index[] = '...';
// contig pages
for ($p = $contigLower; $p <= $contigUpper; $p++)
$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),
];
// The center of the page index.
if ($center > $contigUpper && $center < $upperLower)
{
// Gap?
if ($contigUpper != $center)
$this->page_index[] = '...';
$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),
];
}
// Gap?
if ($upperLower != $p)
$this->page_index[] = '...';
// Upper pages
for ($p = $upperLower; $p <= $upperUpper; $p++)
$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),
];
// Previous page?
if ($this->current_page > 1)
$this->page_index['previous'] = $this->page_index[$this->current_page - 1];
// Next page?
if ($this->current_page < $this->num_pages)
$this->page_index['next'] = $this->page_index[$this->current_page + 1];
}
protected function getLink($start = null, $order = null, $dir = null)
{
$url = $this->base_url;
$amp = strpos($this->base_url, '?') ? '&' : '?';
if (!empty($start))
{
$page = ($start / $this->items_per_page) + 1;
$url .= strtr($this->page_slug, ['%PAGE%' => $page, '%AMP%' => $amp]);
$amp = '&';
}
if (!empty($order))
{
$url .= $amp . 'order=' . $order;
$amp = '&';
}
if (!empty($dir))
{
$url .= $amp . 'dir=' . $dir;
$amp = '&';
}
return $url;
}
public function getArray()
{
return $this->page_index;
}
public function getPageIndex()
{
return $this->page_index;
}
public function getPageIndexClass()
{
return $this->index_class;
}
public function getCurrentPage()
{
return $this->current_page;
}
public function getNumberOfPages()
{
return $this->num_pages;
}
public function getRecordCount()
{
return $this->recordCount;
}
}

166
models/PhotoMosaic.php Normal file
View File

@@ -0,0 +1,166 @@
<?php
/*****************************************************************************
* PhotoMosaic.php
* Contains the photo mosaic model, an iterator to create tiled photo galleries.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class PhotoMosaic
{
private $queue = [];
const NUM_DAYS_CUTOFF = 7;
public function __construct(AssetIterator $iterator)
{
$this->iterator = $iterator;
}
public function __destruct()
{
$this->iterator->clean();
}
public static function getRecentPhotos()
{
return new self(AssetIterator::getByOptions([
'tag' => 'photo',
'order' => 'date_captured',
'direction' => 'desc',
'limit' => 15, // worst case: 3 rows * (portrait + 4 thumbs)
]));
}
private static function matchTypeMask(Image $image, $type_mask)
{
return ($type_mask & Image::TYPE_PANORAMA) && $image->isPanorama() ||
($type_mask & Image::TYPE_LANDSCAPE) && $image->isLandscape() ||
($type_mask & Image::TYPE_PORTRAIT) && $image->isPortrait();
}
private function fetchImage($desired_type = Image::TYPE_PORTRAIT | Image::TYPE_LANDSCAPE | Image::TYPE_PANORAMA, Image $refDateImage = null)
{
// First, check if we have what we're looking for in the queue.
foreach ($this->queue as $i => $image)
{
// Image has to match the desired type and be taken within a week of the reference image.
if (self::matchTypeMask($image, $desired_type) && !(isset($refDateImage) && abs(self::daysApart($image, $refDateImage)) > self::NUM_DAYS_CUTOFF))
{
unset($this->queue[$i]);
return $image;
}
}
// Check whatever's next up!
while (($asset = $this->iterator->next()) && ($image = $asset->getImage()))
{
// Image has to match the desired type and be taken within a week of the reference image.
if (self::matchTypeMask($image, $desired_type) && !(isset($refDateImage) && abs(self::daysApart($image, $refDateImage)) > self::NUM_DAYS_CUTOFF))
return $image;
else
$this->pushToQueue($image);
}
return false;
}
private function pushToQueue(Image $image)
{
$this->queue[] = $image;
}
private static function orderPhotos(Image $a, Image $b)
{
// Show images of highest priority first.
$priority_diff = $a->getPriority() - $b->getPriority();
if ($priority_diff !== 0)
return -$priority_diff;
// In other cases, we'll just show the newest first.
return $a->getDateCaptured() > $b->getDateCaptured() ? -1 : 1;
}
private static function daysApart(Image $a, Image $b)
{
return $a->getDateCaptured()->diff($b->getDateCaptured())->days;
}
public function getRow()
{
// Fetch the first image...
$image = $this->fetchImage();
// No image at all?
if (!$image)
return false;
// Is it a panorama? Then we've got our row!
elseif ($image->isPanorama())
return [[$image], 'panorama'];
// Alright, let's initalise a proper row, then.
$photos = [$image];
$num_portrait = $image->isPortrait() ? 1 : 0;
$num_landscape = $image->isLandscape() ? 1 : 0;
// Get an initial batch of non-panorama images to work with.
for ($i = 1; $i < 3 && ($image = $this->fetchImage(Image::TYPE_LANDSCAPE | Image::TYPE_PORTRAIT, $image)); $i++)
{
$num_portrait += $image->isPortrait() ? 1 : 0;
$num_landscape += $image->isLandscape() ? 1 : 0;
$photos[] = $image;
}
// Sort photos by priority and date captured.
usort($photos, 'self::orderPhotos');
// At least one portrait?
if ($num_portrait >= 1)
{
// Grab two more landscapes, so we can put a total of four tiles on the side.
for ($i = 0; $image && $i < 2 && ($image = $this->fetchImage(Image::TYPE_LANDSCAPE, $image)); $i++)
$photos[] = $image;
// We prefer to have the portrait on the side, so prepare to process that first.
usort($photos, function($a, $b) {
if ($a->isPortrait() && !$b->isPortrait())
return -1;
elseif ($b->isPortrait() && !$a->isPortrait())
return 1;
else
return self::orderPhotos($a, $b);
});
// We might not have a full set of photos, but only bother if we have at least three.
if (count($photos) > 3)
return [$photos, 'portrait'];
}
// One landscape at least, hopefully?
if ($num_landscape >= 1)
{
if (count($photos) === 3)
{
// We prefer to have the landscape on the side, so prepare to process that first.
usort($photos, function($a, $b) {
if ($a->isLandscape() && !$b->isLandscape())
return -1;
elseif ($b->isLandscape() && !$a->isLandscape())
return 1;
else
return self::orderPhotos($a, $b);
});
return [$photos, 'landscape'];
}
elseif (count($photos) === 2)
return [$photos, 'duo'];
else
return [$photos, 'single'];
}
// A boring set it is, then.
return [$photos, 'row'];
}
}

39
models/Registry.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
/*****************************************************************************
* Registry.php
* Allows sharing static variables between classes.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Registry
{
public static $storage = [];
public static function set($key, $value)
{
self::$storage[$key] = $value;
return true;
}
public static function has($key)
{
return isset(self::$storage[$key]);
}
public static function get($key)
{
if (!isset(self::$storage[$key]))
trigger_error('Key does not exist in Registry: ' . $key, E_USER_ERROR);
return self::$storage[$key];
}
public static function remove($key)
{
if (!isset(self::$storage[$key]))
trigger_error('Key does not exist in Registry: ' . $key, E_USER_ERROR);
unset(self::$storage[$key]);
}
}

87
models/Session.php Normal file
View File

@@ -0,0 +1,87 @@
<?php
/*****************************************************************************
* Session.php
* Contains the key class Session.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Session
{
public static function start()
{
session_start();
// Resuming an existing session? Check what we know!
if (isset($_SESSION['user_id'], $_SESSION['ip_address'], $_SESSION['user_agent']))
{
if (isset($_SERVER['REMOTE_ADDR']) && $_SESSION['ip_address'] !== $_SERVER['REMOTE_ADDR'])
{
$_SESSION = [];
throw new NotAllowedException('Your session failed to validate: your IP address has changed. Please re-login and try again.');
}
elseif (isset($_SERVER['HTTP_USER_AGENT']) && $_SESSION['user_agent'] !== $_SERVER['HTTP_USER_AGENT'])
{
$_SESSION = [];
throw new NotAllowedException('Your session failed to validate: your browser identifier has changed. Please re-login and try again.');
}
}
elseif (!isset($_SESSION['ip_address'], $_SESSION['user_agent']))
$_SESSION = [
'ip_address' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '',
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
];
return true;
}
public static function resetSessionToken()
{
$_SESSION['session_token'] = sha1(session_id() . mt_rand());
$_SESSION['session_token_key'] = substr(preg_replace('~^\d+~', '', sha1(mt_rand() . session_id() . mt_rand())), 0, rand(7, 12));
return true;
}
public static function validateSession($method = 'post')
{
// First, check whether the submitted token and key match the ones in storage.
if (($method === 'post' && (!isset($_POST[$_SESSION['session_token_key']]) || $_POST[$_SESSION['session_token_key']] !== $_SESSION['session_token'])) ||
($method === 'get' && (!isset($_GET[$_SESSION['session_token_key']]) || $_GET[$_SESSION['session_token_key']] !== $_SESSION['session_token'])))
trigger_error('Session failed to verify (' . $method . '). Please reload the page and try again.', E_USER_ERROR);
// Check the referring site, too -- should be the same site!
$referring_host = isset($_SERVER['HTTP_REFERER']) ? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) : '';
if (!empty($referring_host))
{
if (strpos($_SERVER['HTTP_HOST'], ':') !== false)
$current_host = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], ':'));
else
$current_host = $_SERVER['HTTP_HOST'];
$base_url_host = parse_url(BASEURL, PHP_URL_HOST);
// The referring_host must match either the base_url_host or the current_host.
if (strtolower($referring_host) !== strtolower($base_url_host) && strtolower($referring_host) !== strtolower($current_host))
trigger_error('Invalid referring URL. Please reload the page and try again.', E_USER_ERROR);
}
// All looks good from here! But you can only use this token once, so...
return self::resetSessionToken();
}
public static function getSessionToken()
{
if (empty($_SESSION['session_token']))
trigger_error('Call to getSessionToken without a session token being set!', E_USER_ERROR);
return $_SESSION['session_token'];
}
public static function getSessionTokenKey()
{
if (empty($_SESSION['session_token_key']))
trigger_error('Call to getSessionTokenKey without a session token key being set!', E_USER_ERROR);
return $_SESSION['session_token_key'];
}
}

80
models/Setting.php Normal file
View File

@@ -0,0 +1,80 @@
<?php
/*****************************************************************************
* Setting.php
* Contains key class Setting.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Setting
{
public static $cache = [];
public static function set($key, $value, $id_user = null)
{
$id_user = Registry::get('user')->getUserId();
if (isset(self::$cache[$id_user], self::$cache[$id_user][$key]))
unset(self::$cache[$id_user][$key]);
if (Registry::get('db')->query('
REPLACE INTO settings
(id_user, variable, value, time_set)
VALUES
({int:id_user}, {string:key}, {string:value}, CURRENT_TIMESTAMP())',
[
'id_user' => $id_user,
'key' => $key,
'value' => $value,
]))
{
if (!isset(self::$cache[$id_user]))
self::$cache[$id_user] = [];
self::$cache[$id_user][$key] = $value;
}
}
public static function get($key, $id_user = null)
{
$id_user = Registry::get('user')->getUserId();
if (isset(self::$cache[$id_user], self::$cache[$id_user][$key]))
return self::$cache[$id_user][$key];
$value = Registry::get('db')->queryValue('
SELECT value
FROM settings
WHERE id_user = {int:id_user} AND variable = {string:key}',
[
'id_user' => $id_user,
'key' => $key,
]);
if (!$value)
return false;
if (!isset(self::$cache[$id_user]))
self::$cache[$id_user] = [];
self::$cache[$id_user][$key] = $value;
return $value;
}
public static function remove($key, $id_user = null)
{
$id_user = Registry::get('user')->getUserId();
if (Registry::get('db')->query('
DELETE FROM settings
WHERE id_user = {int:id_user} AND variable = {string:key}',
[
'id_user' => $id_user,
'key' => $key,
]))
{
if (isset(self::$cache[$id_user], self::$cache[$id_user][$key]))
unset(self::$cache[$id_user][$key]);
}
}
}

337
models/Tag.php Normal file
View File

@@ -0,0 +1,337 @@
<?php
/*****************************************************************************
* Tag.php
* Contains key class Tag.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class Tag
{
public $id_tag;
public $id_parent;
public $id_asset_id;
public $tag;
public $slug;
public $description;
public $kind;
public $count;
protected function __construct(array $data)
{
foreach ($data as $attribute => $value)
$this->$attribute = $value;
}
public static function fromId($id_tag, $return_format = 'object')
{
$db = Registry::get('db');
$row = $db->queryAssoc('
SELECT *
FROM tags
WHERE id_tag = {int:id_tag}',
[
'id_tag' => $id_tag,
]);
// Tag not found?
if (empty($row))
throw new NotFoundException();
return $return_format == 'object' ? new Tag($row) : $row;
}
public static function fromSlug($slug, $return_format = 'object')
{
$db = Registry::get('db');
$row = $db->queryAssoc('
SELECT *
FROM tags
WHERE slug = {string:slug}',
[
'slug' => $slug,
]);
// Tag not found?
if (empty($row))
throw new NotFoundException();
return $return_format == 'object' ? new Tag($row) : $row;
}
public static function getAll($limit = 0, $return_format = 'array')
{
$rows = Registry::get('db')->queryAssocs('
SELECT *
FROM tags
ORDER BY ' . ($limit > 0 ? 'count
LIMIT {int:limit}' : 'tag'),
[
'limit' => $limit,
]);
// No tags found?
if (empty($rows))
return [];
// Limited? Make sure the lot is sorted alphabetically.
if (!empty($limit))
{
usort($rows, function($a, $b) {
return strcmp($a['tag'], $b['tag']);
});
}
if ($return_format == 'object')
{
$return = [];
foreach ($rows as $row)
$return[$row['id_tag']] = new Tag($row);
return $return;
}
else
return $rows;
}
public static function getAlbums($id_parent = 0, $return_format = 'array')
{
$rows = Registry::get('db')->queryAssocs('
SELECT *
FROM tags
WHERE id_parent = {int:id_parent} AND kind = {string:kind}
ORDER BY tag ASC',
[
'id_parent' => $id_parent,
'kind' => 'Album',
]);
if ($return_format == 'object')
{
$return = [];
foreach ($rows as $row)
$return[$row['id_tag']] = new Tag($row);
return $return;
}
else
return $rows;
}
public static function getPeople($id_parent = 0, $return_format = 'array')
{
$rows = Registry::get('db')->queryAssocs('
SELECT *
FROM tags
WHERE id_parent = {int:id_parent} AND kind = {string:kind}
ORDER BY tag ASC',
[
'id_parent' => $id_parent,
'kind' => 'Person',
]);
if ($return_format == 'object')
{
$return = [];
foreach ($rows as $row)
$return[$row['id_tag']] = new Tag($row);
return $return;
}
else
return $rows;
}
public static function byAssetId($id_asset, $return_format = 'object')
{
$rows = Registry::get('db')->queryAssocs('
SELECT *
FROM tags
WHERE id_tag IN(
SELECT id_tag
FROM assets_tags
WHERE id_asset = {int:id_asset}
)
ORDER BY count DESC',
[
'id_asset' => $id_asset,
]);
// No tags found?
if (empty($rows))
return [];
if ($return_format == 'object')
{
$return = [];
foreach ($rows as $row)
$return[$row['id_tag']] = new Tag($row);
return $return;
}
else
return $rows;
}
public static function byPostId($id_post, $return_format = 'object')
{
$rows = Registry::get('db')->queryAssocs('
SELECT *
FROM tags
WHERE id_tag IN(
SELECT id_tag
FROM posts_tags
WHERE id_post = {int:id_post}
)
ORDER BY count DESC',
[
'id_post' => $id_post,
]);
// No tags found?
if (empty($rows))
return [];
if ($return_format == 'object')
{
$return = [];
foreach ($rows as $row)
$return[$row['id_tag']] = new Tag($row);
return $return;
}
else
return $rows;
}
public static function recount(array $id_tags = [])
{
return Registry::get('db')->query('
UPDATE tags AS t SET count = (
SELECT COUNT(*)
FROM `assets_tags` AS at
WHERE at.id_tag = t.id_tag
)' . (!empty($id_tags) ? '
WHERE t.id_tag IN({array_int:id_tags})' : ''),
['id_tags' => $id_tags]);
}
public static function createNew(array $data, $return_format = 'object')
{
$db = Registry::get('db');
if (!isset($data['id_parent']))
$data['id_parent'] = 0;
if (!isset($data['count']))
$data['count'] = 0;
$res = $db->query('
INSERT IGNORE INTO tags
(id_parent, tag, slug, kind, description, count)
VALUES
({int:id_parent}, {string:tag}, {string:slug}, {string:kind}, {string:description}, {int:count})
ON DUPLICATE KEY UPDATE count = count + 1',
$data);
if (!$res)
trigger_error('Could not create the requested tag.', E_USER_ERROR);
$data['id_tag'] = $db->insert_id();
return $return_format == 'object' ? new Tag($data) : $data;
}
public function getUrl()
{
return BASEURL . '/tag/' . $this->slug . '/';
}
public function save()
{
return Registry::get('db')->query('
UPDATE tags
SET
tag = {string:tag},
count = {int:count}
WHERE id_tag = {int:id_tag}',
get_object_vars($this));
}
public function delete()
{
$db = Registry::get('db');
$res = $db->query('
DELETE FROM posts_tags
WHERE id_tag = {int:id_tag}',
[
'id_tag' => $this->id_tag,
]);
if (!$res)
return false;
return $db->query('
DELETE FROM tags
WHERE id_tag = {int:id_tag}',
[
'id_tag' => $this->id_tag,
]);
}
public static function match($tokens)
{
if (!is_array($tokens))
$tokens = explode(' ', $tokens);
return Registry::get('db')->queryPair('
SELECT id_tag, tag
FROM tags
WHERE LOWER(tag) LIKE {string:tokens}
ORDER BY tag ASC',
['tokens' => '%' . strtolower(implode('%', $tokens)) . '%']);
}
public static function exactMatch($tag)
{
if (!is_string($tag))
throw new InvalidArgumentException('Expecting a string!');
return Registry::get('db')->queryPair('
SELECT id_tag, tag
FROM tags
WHERE tag = {string:tag}',
['tag' => $tag]);
}
public static function matchSlug($slug)
{
if (!is_string($slug))
throw new InvalidArgumentException('Expecting a string!');
return Registry::get('db')->queryValue('
SELECT id_tag
FROM tags
WHERE slug = {string:slug}',
['slug' => $slug]);
}
public static function matchAll(array $tags)
{
return Registry::get('db')->queryPair('
SELECT tag, id_tag
FROM tags
WHERE tag IN ({array_string:tags})',
['tags' => $tags]);
}
public static function getCount($only_active = 1)
{
return $db->queryValue('
SELECT COUNT(*)
FROM tags' . ($only_active ? '
WHERE count > 0' : ''));
}
public function __toString()
{
return $this->tag;
}
}

98
models/User.php Normal file
View File

@@ -0,0 +1,98 @@
<?php
/*****************************************************************************
* User.php
* Contains key class User, as well as the Guest and Member class derived
* from it.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
/**
* User model; contains attributes and methods shared by both members and guests.
*/
abstract class User
{
protected $id_user;
protected $first_name;
protected $surname;
protected $emailaddress;
protected $creation_time;
protected $last_action_time;
protected $ip_address;
protected $is_admin;
protected $is_logged;
protected $is_guest;
/**
* Returns user id.
*/
public function getUserId()
{
return $this->id_user;
}
/**
* Returns first name.
*/
public function getFirstName()
{
return $this->first_name;
}
/**
* Returns surname.
*/
public function getSurname()
{
return $this->surname;
}
/**
* Returns full name.
*/
public function getFullName()
{
return trim($this->first_name . ' ' . $this->surname);
}
/**
* Returns email address.
*/
public function getEmailAddress()
{
return $this->emailaddress;
}
/**
* Have a guess!
*/
public function getIPAddress()
{
return $this->ip_address;
}
/**
* Returns whether user is logged in.
*/
public function isLoggedIn()
{
return $this->is_logged;
}
/**
* Returns whether user is a guest.
*/
public function isGuest()
{
return $this->is_guest;
}
/**
* Returns whether user is an administrator.
*/
public function isAdmin()
{
return $this->is_admin;
}
}