2013-10-15 20:22:52 +02:00
|
|
|
<?php
|
|
|
|
class Model_Post extends RedBean_SimpleModel
|
|
|
|
{
|
2013-10-17 22:57:32 +02:00
|
|
|
public static function locate($key, $disallowNumeric = false)
|
|
|
|
{
|
|
|
|
if (is_numeric($key) and !$disallowNumeric)
|
|
|
|
{
|
|
|
|
$post = R::findOne('post', 'id = ?', [$key]);
|
|
|
|
if (!$post)
|
|
|
|
throw new SimpleException('Invalid post ID "' . $key . '"');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$post = R::findOne('post', 'name = ?', [$key]);
|
|
|
|
if (!$post)
|
|
|
|
throw new SimpleException('Invalid post name "' . $key . '"');
|
|
|
|
}
|
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
|
2013-10-15 20:22:52 +02:00
|
|
|
public static function validateSafety($safety)
|
|
|
|
{
|
|
|
|
$safety = intval($safety);
|
|
|
|
|
|
|
|
if (!in_array($safety, PostSafety::getAll()))
|
|
|
|
throw new SimpleException('Invalid safety type "' . $safety . '"');
|
|
|
|
|
|
|
|
return $safety;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function validateTag($tag)
|
|
|
|
{
|
|
|
|
$tag = trim($tag);
|
|
|
|
|
2013-10-19 20:51:32 +02:00
|
|
|
$minLength = 1;
|
|
|
|
$maxLength = 64;
|
|
|
|
if (strlen($tag) < $minLength)
|
|
|
|
throw new SimpleException('Tag must have at least ' . $minLength . ' characters');
|
|
|
|
if (strlen($tag) > $maxLength)
|
|
|
|
throw new SimpleException('Tag must have at most ' . $maxLength . ' characters');
|
|
|
|
|
2013-10-15 20:22:52 +02:00
|
|
|
if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
|
|
|
|
throw new SimpleException('Invalid tag "' . $tag . '"');
|
|
|
|
|
|
|
|
return $tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function validateTags($tags)
|
|
|
|
{
|
|
|
|
$tags = trim($tags);
|
|
|
|
$tags = preg_split('/[,;\s]+/', $tags);
|
|
|
|
$tags = array_filter($tags, function($x) { return $x != ''; });
|
|
|
|
$tags = array_unique($tags);
|
|
|
|
|
|
|
|
foreach ($tags as $key => $tag)
|
|
|
|
$tags[$key] = self::validateTag($tag);
|
|
|
|
|
|
|
|
if (empty($tags))
|
|
|
|
throw new SimpleException('No tags set');
|
|
|
|
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
}
|