Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/image blocking #926

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 82 additions & 39 deletions app/Helpers/Helpers.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: run composer lint to stop these weird style changes

Copy link
Collaborator

@itinerare itinerare May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably I'm just going to switch the relevant workflow to running on push for all branches again so neither reviewers nor contributors need to think about it, since my clever idea wrt running on PRs only was stymied (fairly reasonably) by repo perms... It was worth a shot |D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

It was worth a shot |D

can't work out all the time!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did that while I was thinking about it-- so it should now be as easy as just merging in latest develop. Hopefully.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
|
| Miscellaneous helper functions, primarily used for formatting.
|
*/
*/

/**
* Returns class name if the current URL corresponds to the given path.
Expand All @@ -17,7 +17,8 @@
*
* @return string
*/
function set_active($path, $class = 'active') {
function set_active($path, $class = 'active')
{
return call_user_func_array('Request::is', (array) $path) ? $class : '';
}

Expand All @@ -28,8 +29,9 @@ function set_active($path, $class = 'active') {
*
* @return string
*/
function add_help($text) {
return '<i class="fas fa-question-circle help-icon" data-toggle="tooltip" title="'.$text.'"></i>';
function add_help($text)
{
return '<i class="fas fa-question-circle help-icon" data-toggle="tooltip" title="' . $text . '"></i>';
}

/**
Expand All @@ -39,10 +41,11 @@ function add_help($text) {
*
* @return string
*/
function breadcrumbs($links) {
function breadcrumbs($links)
{
$ret = '<nav><ol class="breadcrumb">';
$count = 0;
$ret .= '<li class="breadcrumb-item"><a href="'.url('/').'">'.config('lorekeeper.settings.site_name', 'Lorekeeper').'</a></li>';
$ret .= '<li class="breadcrumb-item"><a href="' . url('/') . '">' . config('lorekeeper.settings.site_name', 'Lorekeeper') . '</a></li>';
foreach ($links as $key => $link) {
$isLast = ($count == count($links) - 1);

Expand All @@ -53,7 +56,7 @@ function breadcrumbs($links) {
$ret .= '">';

if (!$isLast) {
$ret .= '<a href="'.url($link).'">';
$ret .= '<a href="' . url($link) . '">';
}
$ret .= $key;
if (!$isLast) {
Expand All @@ -77,12 +80,14 @@ function breadcrumbs($links) {
*
* @return string
*/
function format_date($timestamp, $showTime = true) {
return $timestamp->format('j F Y'.($showTime ? ', H:i:s' : '')).($showTime ? ' <abbr data-toggle="tooltip" title="UTC'.$timestamp->timezone->toOffsetName().'">'.strtoupper($timestamp->timezone->getAbbreviatedName($timestamp->isDST())).'</abbr>' : '');
function format_date($timestamp, $showTime = true)
{
return $timestamp->format('j F Y' . ($showTime ? ', H:i:s' : '')) . ($showTime ? ' <abbr data-toggle="tooltip" title="UTC' . $timestamp->timezone->toOffsetName() . '">' . strtoupper($timestamp->timezone->getAbbreviatedName($timestamp->isDST())) . '</abbr>' : '');
}

function pretty_date($timestamp, $showTime = true) {
return '<abbr data-toggle="tooltip" title="'.$timestamp->format('F j Y'.($showTime ? ', H:i:s' : '')).' '.strtoupper($timestamp->timezone->getAbbreviatedName($timestamp->isDST())).'">'.$timestamp->diffForHumans().'</abbr>';
function pretty_date($timestamp, $showTime = true)
{
return '<abbr data-toggle="tooltip" title="' . $timestamp->format('F j Y' . ($showTime ? ', H:i:s' : '')) . ' ' . strtoupper($timestamp->timezone->getAbbreviatedName($timestamp->isDST())) . '">' . $timestamp->diffForHumans() . '</abbr>';
}

/**
Expand All @@ -94,8 +99,9 @@ function pretty_date($timestamp, $showTime = true) {
*
* @return string
*/
function format_masterlist_number($number, $digits) {
return sprintf('%0'.$digits.'d', $number);
function format_masterlist_number($number, $digits)
{
return sprintf('%0' . $digits . 'd', $number);
}

/**
Expand All @@ -106,12 +112,13 @@ function format_masterlist_number($number, $digits) {
*
* @return string
*/
function parse($text, &$pings = null) {
function parse($text, &$pings = null)
{
if (!$text) {
return null;
}

require_once base_path().'/vendor/ezyang/htmlpurifier/library/HTMLPurifier.auto.php';
require_once base_path() . '/vendor/ezyang/htmlpurifier/library/HTMLPurifier.auto.php';

$config = HTMLPurifier_Config::createDefault();
$config->set('Attr.EnableID', true);
Expand Down Expand Up @@ -153,7 +160,8 @@ function parse($text, &$pings = null) {
*
* @return string
*/
function parseUsers($text, &$users) {
function parseUsers($text, &$users)
{
$matches = null;
$users = [];
$count = preg_match_all('/\B@([A-Za-z0-9_-]+)/', $text, $matches);
Expand All @@ -163,7 +171,7 @@ function parseUsers($text, &$users) {
$user = App\Models\User\User::where('name', $match)->first();
if ($user) {
$users[] = $user;
$text = preg_replace('/\B@'.$match.'/', $user->displayName, $text);
$text = preg_replace('/\B@' . $match . '/', $user->displayName, $text);
}
}
}
Expand All @@ -180,7 +188,8 @@ function parseUsers($text, &$users) {
*
* @return string
*/
function parseUsersAndAvatars($text, &$users) {
function parseUsersAndAvatars($text, &$users)
{
$matches = null;
$users = [];
$count = preg_match_all('/\B%([A-Za-z0-9_-]+)/', $text, $matches);
Expand All @@ -190,7 +199,7 @@ function parseUsersAndAvatars($text, &$users) {
$user = App\Models\User\User::where('name', $match)->first();
if ($user) {
$users[] = $user;
$text = preg_replace('/\B%'.$match.'/', '<a href="'.$user->url.'"><img src="'.$user->avatarUrl.'" style="width:70px; height:70px; border-radius:50%; " alt="'.$user->name.'\'s Avatar"></a>'.$user->displayName, $text);
$text = preg_replace('/\B%' . $match . '/', '<a href="' . $user->url . '"><img src="' . $user->avatarUrl . '" style="width:70px; height:70px; border-radius:50%; " alt="' . $user->name . '\'s Avatar"></a>' . $user->displayName, $text);
}
}
}
Expand All @@ -207,7 +216,8 @@ function parseUsersAndAvatars($text, &$users) {
*
* @return string
*/
function parseUserIDs($text, &$users) {
function parseUserIDs($text, &$users)
{
$matches = null;
$users = [];
$count = preg_match_all('/\[user=([^\[\]&<>?"\']+)\]/', $text, $matches);
Expand All @@ -217,7 +227,7 @@ function parseUserIDs($text, &$users) {
$user = App\Models\User\User::where('id', $match)->first();
if ($user) {
$users[] = $user;
$text = preg_replace('/\[user='.$match.'\]/', $user->displayName, $text);
$text = preg_replace('/\[user=' . $match . '\]/', $user->displayName, $text);
}
}
}
Expand All @@ -234,7 +244,8 @@ function parseUserIDs($text, &$users) {
*
* @return string
*/
function parseUserIDsForAvatars($text, &$users) {
function parseUserIDsForAvatars($text, &$users)
{
$matches = null;
$users = [];
$count = preg_match_all('/\[userav=([^\[\]&<>?"\']+)\]/', $text, $matches);
Expand All @@ -244,7 +255,7 @@ function parseUserIDsForAvatars($text, &$users) {
$user = App\Models\User\User::where('id', $match)->first();
if ($user) {
$users[] = $user;
$text = preg_replace('/\[userav='.$match.'\]/', '<a href="'.$user->url.'"><img src="'.$user->avatarUrl.'" style="width:70px; height:70px; border-radius:50%; " alt="'.$user->name.'\'s Avatar"></a>', $text);
$text = preg_replace('/\[userav=' . $match . '\]/', '<a href="' . $user->url . '"><img src="' . $user->avatarUrl . '" style="width:70px; height:70px; border-radius:50%; " alt="' . $user->name . '\'s Avatar"></a>', $text);
}
}
}
Expand All @@ -261,7 +272,8 @@ function parseUserIDsForAvatars($text, &$users) {
*
* @return string
*/
function parseCharacters($text, &$characters) {
function parseCharacters($text, &$characters)
{
$matches = null;
$characters = [];
$count = preg_match_all('/\[character=([^\[\]&<>?"\']+)\]/', $text, $matches);
Expand All @@ -271,7 +283,7 @@ function parseCharacters($text, &$characters) {
$character = App\Models\Character\Character::where('slug', $match)->first();
if ($character) {
$characters[] = $character;
$text = preg_replace('/\[character='.$match.'\]/', $character->displayName, $text);
$text = preg_replace('/\[character=' . $match . '\]/', $character->displayName, $text);
}
}
}
Expand All @@ -288,7 +300,8 @@ function parseCharacters($text, &$characters) {
*
* @return string
*/
function parseCharacterThumbs($text, &$characters) {
function parseCharacterThumbs($text, &$characters)
{
$matches = null;
$characters = [];
$count = preg_match_all('/\[charthumb=([^\[\]&<>?"\']+)\]/', $text, $matches);
Expand All @@ -298,7 +311,7 @@ function parseCharacterThumbs($text, &$characters) {
$character = App\Models\Character\Character::where('slug', $match)->first();
if ($character) {
$characters[] = $character;
$text = preg_replace('/\[charthumb='.$match.'\]/', '<a href="'.$character->url.'"><img class="img-thumbnail" alt="Thumbnail of '.$character->fullName.'" data-toggle="tooltip" title="'.$character->fullName.'" src="'.$character->image->thumbnailUrl.'"></a>', $text);
$text = preg_replace('/\[charthumb=' . $match . '\]/', '<a href="' . $character->url . '"><img class="img-thumbnail" alt="Thumbnail of ' . $character->fullName . '" data-toggle="tooltip" title="' . $character->fullName . '" src="' . $character->image->thumbnailUrl . '"></a>', $text);
}
}
}
Expand All @@ -315,7 +328,8 @@ function parseCharacterThumbs($text, &$characters) {
*
* @return string
*/
function parseGalleryThumbs($text, &$submissions) {
function parseGalleryThumbs($text, &$submissions)
{
$matches = null;
$submissions = [];
$count = preg_match_all('/\[thumb=([^\[\]&<>?"\']+)\]/', $text, $matches);
Expand All @@ -325,7 +339,7 @@ function parseGalleryThumbs($text, &$submissions) {
$submission = App\Models\Gallery\GallerySubmission::where('id', $match)->first();
if ($submission) {
$submissions[] = $submission;
$text = preg_replace('/\[thumb='.$match.'\]/', '<a href="'.$submission->url.'" data-toggle="tooltip" title="'.$submission->displayTitle.' by '.nl2br(htmlentities($submission->creditsPlain)).(isset($submission->content_warning) ? '<br/><strong>Content Warning:</strong> '.nl2br(htmlentities($submission->content_warning)) : '').'">'.view('widgets._gallery_thumb', ['submission' => $submission]).'</a>', $text);
$text = preg_replace('/\[thumb=' . $match . '\]/', '<a href="' . $submission->url . '" data-toggle="tooltip" title="' . $submission->displayTitle . ' by ' . nl2br(htmlentities($submission->creditsPlain)) . (isset($submission->content_warning) ? '<br/><strong>Content Warning:</strong> ' . nl2br(htmlentities($submission->content_warning)) : '') . '">' . view('widgets._gallery_thumb', ['submission' => $submission]) . '</a>', $text);
}
}
}
Expand All @@ -340,7 +354,8 @@ function parseGalleryThumbs($text, &$submissions) {
*
* @return string
*/
function randomString($characters) {
function randomString($characters)
{
$src = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$code = '';
for ($i = 0; $i < $characters; $i++) {
Expand All @@ -359,12 +374,13 @@ function randomString($characters) {
*
* @return App\Models\User\User|string
*/
function checkAlias($url, $failOnError = true) {
function checkAlias($url, $failOnError = true)
{
if ($url) {
$recipient = null;
$matches = [];
// Check to see if url is 1. from a site used for auth
foreach (config('lorekeeper.sites') as $key=> $site) {
foreach (config('lorekeeper.sites') as $key => $site) {
if (isset($site['auth']) && $site['auth']) {
preg_match_all($site['regex'], $url, $matches, PREG_SET_ORDER, 0);
if ($matches != []) {
Expand Down Expand Up @@ -403,10 +419,11 @@ function checkAlias($url, $failOnError = true) {
*
* @return string
*/
function prettyProfileLink($url) {
function prettyProfileLink($url)
{
$matches = [];
// Check different sites and return site if a match is made, plus username (retreived from the URL)
foreach (config('lorekeeper.sites') as $siteName=> $siteInfo) {
foreach (config('lorekeeper.sites') as $siteName => $siteInfo) {
if (preg_match_all($siteInfo['regex'], $url, $matches)) {
$site = $siteName;
$name = $matches[1][0];
Expand All @@ -418,9 +435,9 @@ function prettyProfileLink($url) {

// Return formatted link if possible; failing that, an unformatted link
if (isset($name) && isset($site) && isset($link)) {
return '<a href="https://'.$link.'"><i class="'.$icon.' mr-1" style="opacity: 50%;"></i>'.$name.'@'.(config('lorekeeper.sites.'.$site.'.display_name') != null ? config('lorekeeper.sites.'.$site.'.display_name') : $site).'</a>';
return '<a href="https://' . $link . '"><i class="' . $icon . ' mr-1" style="opacity: 50%;"></i>' . $name . '@' . (config('lorekeeper.sites.' . $site . '.display_name') != null ? config('lorekeeper.sites.' . $site . '.display_name') : $site) . '</a>';
} else {
return '<a href="'.$url.'"><i class="fas fa-globe mr-1" style="opacity: 50%;"></i>'.$url.'</a>';
return '<a href="' . $url . '"><i class="fas fa-globe mr-1" style="opacity: 50%;"></i>' . $url . '</a>';
}
}

Expand All @@ -431,10 +448,11 @@ function prettyProfileLink($url) {
*
* @return string
*/
function prettyProfileName($url) {
function prettyProfileName($url)
{
$matches = [];
// Check different sites and return site if a match is made, plus username (retreived from the URL)
foreach (config('lorekeeper.sites') as $siteName=> $siteInfo) {
foreach (config('lorekeeper.sites') as $siteName => $siteInfo) {
if (preg_match_all($siteInfo['regex'], $url, $matches)) {
$site = $siteName;
$name = $matches[1][0];
Expand All @@ -444,8 +462,33 @@ function prettyProfileName($url) {

// Return formatted name if possible; failing that, an unformatted url
if (isset($name) && isset($site)) {
return $name.'@'.(config('lorekeeper.sites.'.$site.'.display_name') != null ? config('lorekeeper.sites.'.$site.'.display_name') : $site);
return $name . '@' . (config('lorekeeper.sites.' . $site . '.display_name') != null ? config('lorekeeper.sites.' . $site . '.display_name') : $site);
} else {
return $url;
}
}

/**
* Check if user blocked an item
*
* @param string $url
*
* @return string
*/
function checkItemBlock($item, $user)
{

$model = get_class($item);

$block = App\Models\ImageBlock::where([
['item_id', '=', $item->id],
['item_type', '=', $model],
['user_id', '=', $user->id],
])->first();

if ($block) {
return true;
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking, with returns, you don't need to encase the second in the if/else-- nothing beyond a return is executed to begin with, so you can simply have the first in the if, then the second freestanding.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, can even just
return $block ? true : false

Copy link
Contributor

@ScuffedNewt ScuffedNewt May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually follow up on this i'd do

function checkImageBlock($image, $user)
{
    return App\Models\ImageBlock::where([
        ['image_id', '=', $image->id],
        ['image_type', '=', get_class($image)],
        ['user_id', '=', $user->id],
    ])->exists();
}

return false;
}
}
Loading
Loading