Custom rating support
This commit is contained in:
parent
74965c383b
commit
b522d68736
@ -128,6 +128,10 @@ new UserClass("base", null, [
|
|||||||
"view_hellbanned" => false,
|
"view_hellbanned" => false,
|
||||||
|
|
||||||
"protected" => false, # only admins can modify protected users (stops a moderator changing an admin's password)
|
"protected" => false, # only admins can modify protected users (stops a moderator changing an admin's password)
|
||||||
|
|
||||||
|
"edit_image_rating" => false,
|
||||||
|
"bulk_edit_image_rating" => false,
|
||||||
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
new UserClass("anonymous", "base", [
|
new UserClass("anonymous", "base", [
|
||||||
@ -140,6 +144,7 @@ new UserClass("user", "base", [
|
|||||||
"edit_image_tag" => true,
|
"edit_image_tag" => true,
|
||||||
"edit_image_source" => true,
|
"edit_image_source" => true,
|
||||||
"create_image_report" => true,
|
"create_image_report" => true,
|
||||||
|
"edit_image_rating" => true,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
new UserClass("admin", "base", [
|
new UserClass("admin", "base", [
|
||||||
@ -184,6 +189,8 @@ new UserClass("admin", "base", [
|
|||||||
"view_sysinfo" => true,
|
"view_sysinfo" => true,
|
||||||
"view_hellbanned" => true,
|
"view_hellbanned" => true,
|
||||||
"protected" => true,
|
"protected" => true,
|
||||||
|
"edit_image_rating" => true,
|
||||||
|
"bulk_edit_image_rating" => true,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
new UserClass("hellbanned", "user", [
|
new UserClass("hellbanned", "user", [
|
||||||
|
@ -19,6 +19,62 @@
|
|||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @global Rating[] $_shm_ratings
|
||||||
|
*/
|
||||||
|
global $_shm_ratings;
|
||||||
|
$_shm_ratings = [];
|
||||||
|
|
||||||
|
class Rating {
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $name = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $code = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $search_term = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
public $order = 0;
|
||||||
|
|
||||||
|
public function __construct( string $code, string $name, string $search_term, int $order)
|
||||||
|
{
|
||||||
|
global $_shm_ratings;
|
||||||
|
|
||||||
|
if(strlen($code)!=1) {
|
||||||
|
throw new Exception("Rating code must be exactly one character");
|
||||||
|
}
|
||||||
|
if($search_term[0]!=$code) {
|
||||||
|
throw new Exception("Code must be the same as the first letter of search_term");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->name = $name;
|
||||||
|
$this->code = $code;
|
||||||
|
$this->search_term = $search_term;
|
||||||
|
$this->order = $order;
|
||||||
|
|
||||||
|
if($code=="u"&&array_key_exists("u",$_shm_ratings)) {
|
||||||
|
throw new Exception("u is a reserved rating code that cnanot be overridden");
|
||||||
|
}
|
||||||
|
$_shm_ratings[$code] = $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
new Rating("s", "Safe", "safe", 0);
|
||||||
|
new Rating("q", "Questionable", "questionable", 500);
|
||||||
|
new Rating("e", "Explicit", "explicit", 1000);
|
||||||
|
new Rating("u", "Unrated", "unrated", 99999);
|
||||||
|
@include_once "data/config/ratings.conf.php";
|
||||||
|
|
||||||
class RatingSetEvent extends Event
|
class RatingSetEvent extends Event
|
||||||
{
|
{
|
||||||
/** @var Image */
|
/** @var Image */
|
||||||
@ -28,7 +84,9 @@ class RatingSetEvent extends Event
|
|||||||
|
|
||||||
public function __construct(Image $image, string $rating)
|
public function __construct(Image $image, string $rating)
|
||||||
{
|
{
|
||||||
assert(in_array($rating, ["s", "q", "e", "u"]));
|
global $_shm_ratings;
|
||||||
|
|
||||||
|
assert(in_array($rating, array_keys($_shm_ratings)));
|
||||||
|
|
||||||
$this->image = $image;
|
$this->image = $image;
|
||||||
$this->rating = $rating;
|
$this->rating = $rating;
|
||||||
@ -37,7 +95,25 @@ class RatingSetEvent extends Event
|
|||||||
|
|
||||||
class Ratings extends Extension
|
class Ratings extends Extension
|
||||||
{
|
{
|
||||||
protected $db_support = ['mysql','pgsql']; // ?
|
|
||||||
|
protected $db_support = ['mysql','pgsql'];
|
||||||
|
|
||||||
|
private $search_regexp;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
global $_shm_ratings;
|
||||||
|
|
||||||
|
$codes = implode("",array_keys($_shm_ratings));
|
||||||
|
$search_terms = [];
|
||||||
|
foreach($_shm_ratings as $key=>$rating) {
|
||||||
|
array_push($search_terms, $rating->search_term);
|
||||||
|
}
|
||||||
|
$this->search_regexp = "/^rating[=|:](?:([".$codes."]+)|(".
|
||||||
|
implode("|", $search_terms)."|unknown))$/D";
|
||||||
|
}
|
||||||
|
|
||||||
public function get_priority(): int
|
public function get_priority(): int
|
||||||
{
|
{
|
||||||
@ -46,30 +122,43 @@ class Ratings extends Extension
|
|||||||
|
|
||||||
public function onInitExt(InitExtEvent $event)
|
public function onInitExt(InitExtEvent $event)
|
||||||
{
|
{
|
||||||
global $config;
|
global $config, $_shm_user_classes, $_shm_ratings;
|
||||||
|
|
||||||
if ($config->get_int("ext_ratings2_version") < 2) {
|
if ($config->get_int("ext_ratings2_version") < 4) {
|
||||||
$this->install();
|
$this->install();
|
||||||
}
|
}
|
||||||
|
|
||||||
$config->set_default_string("ext_rating_anon_privs", 'squ');
|
foreach(array_keys($_shm_user_classes) as $key){
|
||||||
$config->set_default_string("ext_rating_user_privs", 'sqeu');
|
if($key=="base"||$key=="hellbanned") {
|
||||||
$config->set_default_string("ext_rating_admin_privs", 'sqeu');
|
continue;
|
||||||
|
}
|
||||||
|
$config->set_default_array("ext_rating_".$key."_privs", array_keys($_shm_ratings));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onSetupBuilding(SetupBuildingEvent $event)
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
||||||
{
|
{
|
||||||
$privs = [];
|
global $config, $_shm_user_classes, $_shm_ratings;
|
||||||
$privs['Safe Only'] = 's';
|
|
||||||
$privs['Safe and Unknown'] = 'su';
|
$ratings = array_values($_shm_ratings);
|
||||||
$privs['Safe and Questionable'] = 'sq';
|
usort($ratings, function($a, $b) {
|
||||||
$privs['Safe, Questionable, Unknown'] = 'squ';
|
return $a->order <=> $b->order;
|
||||||
$privs['All'] = 'sqeu';
|
});
|
||||||
|
$options = [];
|
||||||
|
foreach($ratings as $key => $rating) {
|
||||||
|
$options[$rating->name] = $rating->code;
|
||||||
|
}
|
||||||
|
|
||||||
$sb = new SetupBlock("Image Ratings");
|
$sb = new SetupBlock("Image Ratings");
|
||||||
$sb->add_choice_option("ext_rating_anon_privs", $privs, "Anonymous: ");
|
foreach(array_keys($_shm_user_classes) as $key){
|
||||||
$sb->add_choice_option("ext_rating_user_privs", $privs, "<br>Users: ");
|
if($key=="base"||$key=="hellbanned") {
|
||||||
$sb->add_choice_option("ext_rating_admin_privs", $privs, "<br>Admins: ");
|
continue;
|
||||||
|
}
|
||||||
|
$sb->add_multichoice_option("ext_rating_".$key."_privs", $options, "<br/>".$key.": ");
|
||||||
|
}
|
||||||
|
|
||||||
$event->panel->add_block($sb);
|
$event->panel->add_block($sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,7 +178,6 @@ class Ratings extends Extension
|
|||||||
* Deny images upon insufficient permissions.
|
* Deny images upon insufficient permissions.
|
||||||
**/
|
**/
|
||||||
$user_view_level = Ratings::get_user_privs($user);
|
$user_view_level = Ratings::get_user_privs($user);
|
||||||
$user_view_level = preg_split('//', $user_view_level, -1);
|
|
||||||
if (!in_array($event->image->rating, $user_view_level)) {
|
if (!in_array($event->image->rating, $user_view_level)) {
|
||||||
$page->set_mode("redirect");
|
$page->set_mode("redirect");
|
||||||
$page->set_redirect(make_link("post/list"));
|
$page->set_redirect(make_link("post/list"));
|
||||||
@ -128,16 +216,20 @@ class Ratings extends Extension
|
|||||||
|
|
||||||
public function onSearchTermParse(SearchTermParseEvent $event)
|
public function onSearchTermParse(SearchTermParseEvent $event)
|
||||||
{
|
{
|
||||||
global $user;
|
global $user, $_shm_ratings;
|
||||||
|
|
||||||
$matches = [];
|
$matches = [];
|
||||||
if (is_null($event->term) && $this->no_rating_query($event->context)) {
|
if (is_null($event->term) && $this->no_rating_query($event->context)) {
|
||||||
$set = Ratings::privs_to_sql(Ratings::get_user_privs($user));
|
$set = Ratings::privs_to_sql(Ratings::get_user_privs($user));
|
||||||
$event->add_querylet(new Querylet("rating IN ($set)"));
|
$event->add_querylet(new Querylet("rating IN ($set)"));
|
||||||
}
|
}
|
||||||
if (preg_match("/^rating[=|:](?:([sqeu]+)|(safe|questionable|explicit|unknown))$/D", strtolower($event->term), $matches)) {
|
|
||||||
|
|
||||||
|
if (preg_match($this->search_regexp, strtolower($event->term), $matches)) {
|
||||||
$ratings = $matches[1] ? $matches[1] : $matches[2][0];
|
$ratings = $matches[1] ? $matches[1] : $matches[2][0];
|
||||||
$ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user)));
|
|
||||||
|
$ratings = array_intersect(str_split($ratings), Ratings::get_user_privs($user));
|
||||||
|
|
||||||
$set = "'" . join("', '", $ratings) . "'";
|
$set = "'" . join("', '", $ratings) . "'";
|
||||||
$event->add_querylet(new Querylet("rating IN ($set)"));
|
$event->add_querylet(new Querylet("rating IN ($set)"));
|
||||||
}
|
}
|
||||||
@ -147,9 +239,9 @@ class Ratings extends Extension
|
|||||||
{
|
{
|
||||||
$matches = [];
|
$matches = [];
|
||||||
|
|
||||||
if (preg_match("/^rating[=|:](?:([sqeu]+)|(safe|questionable|explicit|unknown))$/D", strtolower($event->term), $matches) && $event->parse) {
|
if (preg_match($this->search_regexp, strtolower($event->term), $matches) && $event->parse) {
|
||||||
$ratings = $matches[1] ? $matches[1] : $matches[2][0];
|
$ratings = $matches[1] ? $matches[1] : $matches[2][0];
|
||||||
$ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user)));
|
$ratings = array_intersect(str_split($ratings), Ratings::get_user_privs($user));
|
||||||
|
|
||||||
$rating = $ratings[0];
|
$rating = $ratings[0];
|
||||||
|
|
||||||
@ -169,7 +261,7 @@ class Ratings extends Extension
|
|||||||
{
|
{
|
||||||
global $user;
|
global $user;
|
||||||
|
|
||||||
if ($user->is_admin()) {
|
if ($user->can("bulk_edit_image_rating")) {
|
||||||
$event->add_action("bulk_rate","Set Rating","",$this->theme->get_selection_rater_html("bulk_rating"));
|
$event->add_action("bulk_rate","Set Rating","",$this->theme->get_selection_rater_html("bulk_rating"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -183,7 +275,7 @@ class Ratings extends Extension
|
|||||||
if (!isset($_POST['bulk_rating'])) {
|
if (!isset($_POST['bulk_rating'])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($user->is_admin()) {
|
if ($user->can("bulk_edit_image_rating")) {
|
||||||
$rating = $_POST['bulk_rating'];
|
$rating = $_POST['bulk_rating'];
|
||||||
$total = 0;
|
$total = 0;
|
||||||
foreach ($event->items as $id) {
|
foreach ($event->items as $id) {
|
||||||
@ -206,7 +298,7 @@ class Ratings extends Extension
|
|||||||
global $user, $page;
|
global $user, $page;
|
||||||
|
|
||||||
if ($event->page_matches("admin/bulk_rate")) {
|
if ($event->page_matches("admin/bulk_rate")) {
|
||||||
if (!$user->is_admin()) {
|
if (!$user->can("bulk_edit_image_rating")) {
|
||||||
throw new PermissionDeniedException();
|
throw new PermissionDeniedException();
|
||||||
} else {
|
} else {
|
||||||
$n = 0;
|
$n = 0;
|
||||||
@ -234,26 +326,21 @@ class Ratings extends Extension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get_user_privs(User $user): string
|
public static function get_user_privs(User $user): array
|
||||||
{
|
{
|
||||||
global $config;
|
global $config, $_shm_ratings;
|
||||||
|
|
||||||
if ($user->is_anonymous()) {
|
return $config->get_array("ext_rating_".$user->class->name."_privs");
|
||||||
$sqes = $config->get_string("ext_rating_anon_privs");
|
|
||||||
} elseif ($user->is_admin()) {
|
|
||||||
$sqes = $config->get_string("ext_rating_admin_privs");
|
|
||||||
} else {
|
|
||||||
$sqes = $config->get_string("ext_rating_user_privs");
|
|
||||||
}
|
|
||||||
return $sqes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function privs_to_sql(string $sqes): string
|
public static function privs_to_sql(array $sqes): string
|
||||||
{
|
{
|
||||||
$arr = [];
|
$arr = [];
|
||||||
$length = strlen($sqes);
|
foreach($sqes as $i) {
|
||||||
for ($i=0; $i<$length; $i++) {
|
$arr[] = "'" . $i . "'";
|
||||||
$arr[] = "'" . $sqes[$i] . "'";
|
}
|
||||||
|
if(sizeof($arr)==0) {
|
||||||
|
return "' '";
|
||||||
}
|
}
|
||||||
$set = join(', ', $arr);
|
$set = join(', ', $arr);
|
||||||
return $set;
|
return $set;
|
||||||
@ -261,25 +348,19 @@ class Ratings extends Extension
|
|||||||
|
|
||||||
public static function rating_to_human(string $rating): string
|
public static function rating_to_human(string $rating): string
|
||||||
{
|
{
|
||||||
switch ($rating) {
|
global $_shm_ratings;
|
||||||
case "s": return "Safe";
|
|
||||||
case "q": return "Questionable";
|
if(array_key_exists($rating, $_shm_ratings)) {
|
||||||
case "e": return "Explicit";
|
return $_shm_ratings[$rating]->name;
|
||||||
default: return "Unknown";
|
|
||||||
}
|
}
|
||||||
|
return "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function rating_is_valid(string $rating): bool
|
public static function rating_is_valid(string $rating): bool
|
||||||
{
|
{
|
||||||
switch ($rating) {
|
global $_shm_ratings;
|
||||||
case "s":
|
|
||||||
case "q":
|
return in_array($rating, array_keys($_shm_ratings));
|
||||||
case "e":
|
|
||||||
case "u":
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -288,13 +369,7 @@ class Ratings extends Extension
|
|||||||
private function can_rate(): bool
|
private function can_rate(): bool
|
||||||
{
|
{
|
||||||
global $config, $user;
|
global $config, $user;
|
||||||
if ($user->is_anonymous() && $config->get_string("ext_rating_anon_privs") == "sqeu") {
|
if ($user->can("edit_image_rating")) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ($user->is_admin()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!$user->is_anonymous() && $config->get_string("ext_rating_user_privs") == "sqeu") {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -341,6 +416,16 @@ class Ratings extends Extension
|
|||||||
}
|
}
|
||||||
$config->set_int("ext_ratings2_version", 3);
|
$config->set_int("ext_ratings2_version", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($config->get_int("ext_ratings2_version") < 4) {
|
||||||
|
$value = $config->get_string("ext_rating_anon_privs");
|
||||||
|
$config->set_array("ext_rating_anonymous_privs", str_split($value));
|
||||||
|
$value = $config->get_string("ext_rating_user_privs");
|
||||||
|
$config->set_array("ext_rating_user_privs", str_split($value));
|
||||||
|
$value = $config->get_string("ext_rating_admin_privs");
|
||||||
|
$config->set_array("ext_rating_admin_privs", str_split($value));
|
||||||
|
$config->set_int("ext_ratings2_version", 4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function set_rating(int $image_id, string $rating, string $old_rating)
|
private function set_rating(int $image_id, string $rating, string $old_rating)
|
||||||
|
@ -4,9 +4,6 @@ class RatingsTheme extends Themelet
|
|||||||
{
|
{
|
||||||
public function get_rater_html(int $image_id, string $rating, bool $can_rate): string
|
public function get_rater_html(int $image_id, string $rating, bool $can_rate): string
|
||||||
{
|
{
|
||||||
$s_checked = $rating == 's' ? " checked" : "";
|
|
||||||
$q_checked = $rating == 'q' ? " checked" : "";
|
|
||||||
$e_checked = $rating == 'e' ? " checked" : "";
|
|
||||||
$human_rating = Ratings::rating_to_human($rating);
|
$human_rating = Ratings::rating_to_human($rating);
|
||||||
$html = "
|
$html = "
|
||||||
<tr>
|
<tr>
|
||||||
@ -15,9 +12,7 @@ class RatingsTheme extends Themelet
|
|||||||
".($can_rate ? "
|
".($can_rate ? "
|
||||||
<span class='view'>$human_rating</span>
|
<span class='view'>$human_rating</span>
|
||||||
<span class='edit'>
|
<span class='edit'>
|
||||||
<input type='radio' name='rating' value='s' id='s'$s_checked><label for='s'>Safe</label>
|
".$this->get_selection_rater_html($rating)."
|
||||||
<input type='radio' name='rating' value='q' id='q'$q_checked><label for='q'>Questionable</label>
|
|
||||||
<input type='radio' name='rating' value='e' id='e'$e_checked><label for='e'>Explicit</label>
|
|
||||||
</span>
|
</span>
|
||||||
" : "
|
" : "
|
||||||
$human_rating
|
$human_rating
|
||||||
@ -28,31 +23,36 @@ class RatingsTheme extends Themelet
|
|||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display_bulk_rater(string $terms)
|
// public function display_bulk_rater(string $terms)
|
||||||
{
|
// {
|
||||||
global $page;
|
// global $page;
|
||||||
$html = "
|
// $html = "
|
||||||
".make_form(make_link("admin/bulk_rate"))."
|
// ".make_form(make_link("admin/bulk_rate"))."
|
||||||
<input type='hidden' name='query' value='".html_escape($terms)."'>
|
// <input type='hidden' name='query' value='".html_escape($terms)."'>
|
||||||
<select name='rating'>
|
// <select name='rating'>
|
||||||
<option value='s'>Safe</option>
|
// <option value='s'>Safe</option>
|
||||||
<option value='q'>Questionable</option>
|
// <option value='q'>Questionable</option>
|
||||||
<option value='e'>Explicit</option>
|
// <option value='e'>Explicit</option>
|
||||||
<option value='u'>Unrated</option>
|
// <option value='u'>Unrated</option>
|
||||||
</select>
|
// </select>
|
||||||
<input type='submit' value='Go'>
|
// <input type='submit' value='Go'>
|
||||||
</form>
|
// </form>
|
||||||
";
|
// ";
|
||||||
$page->add_block(new Block("List Controls", $html, "left"));
|
// $page->add_block(new Block("List Controls", $html, "left"));
|
||||||
}
|
// }
|
||||||
|
|
||||||
public function get_selection_rater_html(String $id = "select_rating")
|
public function get_selection_rater_html(String $selected_option, String $id = "rating") {
|
||||||
{
|
global $_shm_ratings;
|
||||||
return "<select name='".$id."'>
|
|
||||||
<option value='s'>Safe</option>
|
$output = "<select name='".$id."'>";
|
||||||
<option value='q'>Questionable</option>
|
$options = array_values($_shm_ratings);
|
||||||
<option value='e'>Explicit</option>
|
usort($options, function($a, $b) {
|
||||||
<option value='u'>Unrated</option>
|
return $a->order <=> $b->order;
|
||||||
</select>";
|
});
|
||||||
|
|
||||||
|
foreach($options as $option) {
|
||||||
|
$output .= "<option value='".$option->code."' ".($selected_option==$option->code ? "selected='selected'": "").">".$option->name."</option>";
|
||||||
|
}
|
||||||
|
return $output."</select>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user