w0bm.com v1.5z FULL.RETARD.BUILD.BUT.STILL.WORKS

This commit is contained in:
noxy
2019-08-26 16:58:26 +00:00
commit da71b95aa2
517 changed files with 143236 additions and 0 deletions

40
app/Models/Banner.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
// Maybe this will in a n to m relation with video someday
class Banner extends Model
{
protected $casts = [
'sfw' => 'boolean'
];
protected $dates = [
'created_at',
'updated_at',
'until'
];
// If this would be in relation with video the $sfw could be
// figured out dynamically
public static function getRandom($sfw = true) {
$q = static::active();
if($sfw) $q->sfw();
$id = $q->count() - 1;
if ($id < 0) return null;
$id = mt_rand(0, $id);
$q = static::active();
if($sfw) $q->sfw();
return $q->skip($id)->first();
}
public function scopeSfw($query) {
return $query->where('sfw', true);
}
public function scopeActive($query) {
return $query->where('until', '>=', Carbon::now());
}
}

49
app/Models/Category.php Normal file
View File

@@ -0,0 +1,49 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* App\Models\Category
*
* @property integer $id
* @property string $name
* @property string $shortname
* @property string $description
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $deleted_at
* @property-read \Illuminate\Database\Eloquent\Collection|Video[] $videos
* @method static \Illuminate\Database\Query\Builder|\App\Models\Category whereId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Category whereName($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Category whereShortname($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Category whereDescription($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Category whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Category whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Category whereDeletedAt($value)
* @property-read \Illuminate\Database\Eloquent\Collection|User[] $users
*/
class Category extends Model
{
use SoftDeletes;
protected $table = 'categories';
public function videos() {
return $this->hasMany(Video::class);
}
public function users() {
return $this->belongsToMany(User::class);
}
public function baseurl() {
return $this->shortname;
}
public function displayName() {
return e($this->name);
}
}

90
app/Models/Comment.php Normal file
View File

@@ -0,0 +1,90 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Services\Markdown;
/**
* App\Models\Comment
*
* @property integer $id
* @property string $content
* @property integer $user_id
* @property integer $video_id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $deleted_at
* @property-read User $user
* @property-read Video $video
* @method static \Illuminate\Database\Query\Builder|\App\Models\Comment whereId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Comment whereContent($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Comment whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Comment whereVideoId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Comment whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Comment whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Comment whereDeletedAt($value)
*/
class Comment extends Model
{
use SoftDeletes;
protected $appends = ['rendered_view'];
public function user() {
return $this->belongsTo(User::class);
}
public function video() {
return $this->belongsTo(Video::class);
}
public static function simplemd($text) {
$m = app()->make(Markdown::class);
$text = $m->text($text);
return $text;
}
public function getRenderedViewAttribute() {
return static::simplemd($this->content);
}
public function getMentioned() {
$text = $this->content;
$nameMatcher = '/\B@([\wÄÖÜäöü]+)/i';
$ret = [];
if(preg_match_all($nameMatcher, $text, $users) > 0) {
foreach ($users[1] as $user) {
if(User::whereUsername($user)->count() > 0) {
$ret[] = User::whereUsername($user)->first();
}
}
}
return array_unique($ret);
}
public function answered() {
$text = $this->content;
$regex = '/^[!%*]*(\^+)/m';
$answers = [];
if(preg_match_all($regex, $text, $answered) > 0) {
foreach($answered[1] as $a) {
$answers[] = mb_strlen($a);
}
}
$answers = array_unique($answers);
$comments = $this->video->comments;
$total = $comments->count();
$ret = [];
foreach($answers as $c) {
$up = $total - $c - 1;
if($up >= 0) {
$ret[] = $comments->get($up)->user;
}
}
return $ret;
}
}

20
app/Models/Donation.php Normal file
View File

@@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Donation extends Model
{
public $timestamps = false;
public static $needed = 150;
public static function getPercentage() {
return (static::getFunds() / static::$needed) * 100;
}
public static function getFunds() {
return static::sum('amount') ?? 0;
}
}

35
app/Models/Icon.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Icon extends Model
{
public $timestamps = false;
public function roles() {
return $this->hasMany(Role::class);
}
public function users() {
return $this->hasMany(User::class);
}
public function toJson($options = 0) {
return parent::toJson($options);
}
public function __toString() {
switch ($this->icon_type) {
case 'fa':
return '<i class="fa fa-' . $this->icon . '"></i>';
case 'img':
case 'image':
return '<img class="icon" src="https://s.w0bm.com/' . ltrim($this->icon, '/') . '" alt="' . $this->icon . '">';
default:
return '';
}
}
}

72
app/Models/Message.php Normal file
View File

@@ -0,0 +1,72 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* App\Models\Message
*
* @property integer $id
* @property integer $from
* @property integer $to
* @property string $content
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $deleted_at
* @property string $read
* @property string $subject
* @property-read User $fromUser
* @property-read User $toUser
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message whereId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message whereFrom($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message whereTo($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message whereContent($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message whereDeletedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message whereRead($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message whereSubject($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Message unread()
*/
class Message extends Model {
use SoftDeletes;
public function fromUser() {
return $this->belongsTo(User::class, 'from');
}
public function toUser() {
return $this->belongsTo(User::class, 'to');
}
public static function send($from, $to, $subject, $content) {
if(empty($subject)) return 'Subject must not be empty';
if(empty($content)) return 'Content must not be empty';
try {
if(!is_object($from))
$from = User::findOrFail($from);
if(!is_object($to))
$to = User::findOrFail($to);
} catch (ModelNotFoundException $e) {
return false;
}
$message = new static();
$message->from = $from->id;
$message->to = $to->id;
$message->subject = $subject;
$message->content = $content;
$message->save();
return $message;
}
public function scopeUnread($query) {
return $query->whereNull('read');
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\ModeratorLog
*
* @property integer $id
* @property integer $user_id
* @property string $type
* @property string $target_type
* @property integer $target_id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property-read User $user
* @method static \Illuminate\Database\Query\Builder|\App\Models\ModeratorLog whereId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\ModeratorLog whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\ModeratorLog whereType($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\ModeratorLog whereTargetType($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\ModeratorLog whereTargetId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\ModeratorLog whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\ModeratorLog whereUpdatedAt($value)
*/
class ModeratorLog extends Model
{
public function user() {
return $this->belongsTo(User::class);
}
public function getTarget() {
$target_type = $this->target_type;
switch ($target_type) {
case 'user': return User::find($this->target_id);
case 'comment': return Comment::find($this->target_id);
case 'video': return Video::find($this->target_id);
default: return null;
}
}
}

12
app/Models/Role.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
namespace App\Models;
use Toddish\Verify\Models\Role as VerifyRole;
class Role extends VerifyRole
{
public function icon() {
return $this->belongsTo(Icon::class, 'icon_id');
}
}

135
app/Models/User.php Normal file
View File

@@ -0,0 +1,135 @@
<?php
namespace App\Models;
use Toddish\Verify\Models\User as VerifyUser;
use Carbon\Carbon;
/**
* App\Models\User
*
* @property integer $id
* @property string $username
* @property string $password
* @property string $salt
* @property string $email
* @property string $remember_token
* @property boolean $verified
* @property boolean $disabled
* @property array $categories
* @property \Carbon\Carbon $deleted_at
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $activation_token
* @property-read \Illuminate\Database\Eloquent\Collection|Video[] $videos
* @property-read \Illuminate\Database\Eloquent\Collection|Comment[] $comments
* @property-read \Illuminate\Database\Eloquent\Collection|\config('verify.models.role')[] $roles
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUsername($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User wherePassword($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereSalt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereEmail($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereRememberToken($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereVerified($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereDisabled($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereDeletedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereActivationToken($value)
* @method static \Illuminate\Database\Query\Builder|\Toddish\Verify\Models\User verified()
* @method static \Illuminate\Database\Query\Builder|\Toddish\Verify\Models\User unverified()
* @method static \Illuminate\Database\Query\Builder|\Toddish\Verify\Models\User disabled()
* @method static \Illuminate\Database\Query\Builder|\Toddish\Verify\Models\User enabled()
* @property-read \Illuminate\Database\Eloquent\Collection|ModeratorLog[] $moderator_log
* @property-read \Illuminate\Database\Eloquent\Collection|Message[] $messagesSent
* @property-read \Illuminate\Database\Eloquent\Collection|Message[] $messagesRecv
* @property-read \Illuminate\Database\Eloquent\Collection|Video[] $favs
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereBackground($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCategories($value)
*/
class User extends VerifyUser
{
protected $casts = [
// TODO: rename db column to tag filters
'categories' => 'array'
];
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
'banend'
];
public function uploads() {
return $this->hasMany(Video::class);
}
public function videos() {
return $this->uploads();
}
public function comments() {
return $this->hasMany(Comment::class);
}
public function moderator_log() {
return $this->hasMany(ModeratorLog::class);
}
public function messagesSent() {
return $this->hasMany(Message::class, 'from');
}
public function messagesRecv() {
return $this->hasMany(Message::class, 'to');
}
public function favs() {
return $this->belongsToMany(Video::class, 'favorites');
}
public function hasFaved($id) {
return ! $this->favs->filter(function($vid) use ($id) {
return $vid->id == $id;
})->isEmpty();
}
public function icon() {
return $this->belongsTo(Icon::class, 'icon_id');
}
public function activeIcon() {
$icon = $this->icon;
if($icon === null) {
$roles = $this->roles;
$roles = $roles->sortByDesc('level');
foreach($roles as $role) {
if($role !== null) $icon = $role->icon;
}
}
return $icon;
}
public function isBanned() {
if($this->disabled == 1) {
return $this->banend->eq(Carbon::createFromTimeStampUTC(1)) || $this->banend->gt(Carbon::now());
}
return false;
}
public function getForeignKey() {
return 'user_id';
}
public function baseurl() {
return 'user/' . $this->username . '/uploads';
}
public function displayName() {
return e($this->username) . ($this->activeIcon() ? " " . $this->activeIcon() : "");
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Models;
class UserFavorite extends User {
protected $table = 'users';
// Instead of uploaded Videos get favs
public function videos() {
return $this->favs();
}
public function baseurl() {
return 'user/' . $this->username . '/favs';
}
public function displayName() {
return 'Favorites (' . parent::displayName() . ')';
}
}

189
app/Models/Video.php Normal file
View File

@@ -0,0 +1,189 @@
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* App\Models\Video
*
* @property integer $id
* @property string $file
* @property string $interpret
* @property string $songtitle
* @property string $imgsource
* @property integer $category_id
* @property integer $user_id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $deleted_at
* @property string $hash
* @property-read User $user
* @property-read Category $category
* @property-read \Illuminate\Database\Eloquent\Collection|Comment[] $comments
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereFile($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereInterpret($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereSongtitle($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereImgsource($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereCategoryId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereDeletedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video whereHash($value)
* @property-read \Illuminate\Database\Eloquent\Collection|User[] $faved
* @property-read \Illuminate\Database\Eloquent\Collection|Tag[] $tags
* @property-read mixed $tag_list
* @property-read mixed $tag_list_normalized
* @property-read mixed $tag_array
* @property-read mixed $tag_array_normalized
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video newlyups()
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video withAllTags($tags)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video withAnyTags($tags = array())
* @method static \Illuminate\Database\Query\Builder|\App\Models\Video withoutTags()
*/
class Video extends Model
{
use SoftDeletes;
use \Cviebrock\EloquentTaggable\Taggable;
public function user() {
return $this->belongsTo(User::class);
}
public function category() {
return $this->belongsTo(Category::class);
}
public function comments() {
return $this->hasMany(Comment::class);
}
public function faved() {
return $this->belongsToMany(User::class, 'favorites', 'video_id', 'user_id');
}
public static function getFirstId($related = null) {
if ($related) {
return $related->videos()->filtered()->orderBy('id', 'ASC')->first()->id;
}
return static::filtered()->orderBy('id', 'ASC')->first()->id;
}
public static function getLastId($related = null) {
if ($related) {
return $related->videos()->filtered()->orderBy('id', 'DESC')->first()->id;
}
return static::select('id')->filtered()->orderBy('id', 'DESC')->first()->id;
}
public function getNext($related = null) {
if ($related) {
return $related->videos()->filtered()->where('id', '>', $this->id)->orderBy('id', 'ASC')->first();
} else {
return static::filtered()->where('id', '>', $this->id)->orderBy('id', 'ASC')->first();
}
}
public function getPrev($related = null) {
if ($related) {
return $related->videos()->filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first();
} else {
return static::filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first();
}
}
public function scopeNewlyups($query) {
return $query->where('created_at', '>=', Carbon::now()->subHours(12));
}
public function scopeFiltered($query) {
if(auth()->check()) {
// TODO rename to filtered
$filter = auth()->user()->categories;
if(empty($filter))
return $query;
return $query->withoutAnyTags($filter);
} else {
// TODO: filter if post has sfw & nsfw tags
//return $query->withAllTags('sfw');
return $query->withoutAnyTags('nsfw');
}
}
public function checkFileEncoding() {
$dat = $this->file;
$in = public_path() . "/b";
$tmpdir = str_replace("public", "app/Http/Controllers/tmp", public_path());
for($i = 0; $i < 2; $i++) {
$ret = shell_exec("ffmpeg -y -ss 0 -i {$in}/{$dat} -vframes 1 {$tmpdir}/test.png 2>&1");
if(strpos($ret, "nothing was encoded") !== false) {
shell_exec("ffmpeg -i {$in}/{$dat} -map 0:0 -map 0:1 -c:v copy {$tmpdir}/{$dat}");
unlink($in . "/" . $dat);
rename($tmpdir . "/" . $dat, $in . "/" . $dat);
}
else return true;
}
return false;
}
/**
* Creates a .gif thumbnail to a given video file
*
* @param string $dat File of the video
*/
public function createThumbnail() {
$dat = $this->file;
$in = public_path() . "/b"; // webm-input
$out = public_path() . "/thumbs"; //thumb-output
$tmpdir = str_replace("public", "app/Http/Controllers/tmp", public_path());
$name = explode(".", $dat);
array_pop($name);
$name = join(".", $name);
if(!file_exists("{$out}/{$name}.gif")) {
$length = round(shell_exec("ffprobe -i {$in}/{$dat} -show_format -v quiet | sed -n 's/duration=//p'"));
for ($i = 1; $i < 10; $i++) {
$act = ($i * 10) * ($length / 100);
$ffmpeg = shell_exec("ffmpeg -ss {$act} -i {$in}/{$dat} -vf \"scale='if(gt(a,4/3),206,-1)':'if(gt(a,4/3),-1,116)'\" -vframes 1 {$tmpdir}/{$name}_{$i}.png 2>&1");
}
$tmp = shell_exec("convert -delay 27 -loop 0 {$tmpdir}/{$name}_*.png {$out}/{$name}.gif 2>&1");
if(@filesize("{$out}/{$name}.gif") < 2000)
@unlink("{$out}/{$name}.gif");
array_map('unlink', glob("{$tmpdir}/{$name}*.png"));
}
}
public static function getRandom($related = null) {
if ($related) {
$id = $related->videos()->filtered()->countScoped()->count() - 1;
if ($id < 0) {
return redirect()->back()->with('error', 'no videos found');
}
$id = mt_rand(0, $id);
return $related->videos()->filtered()->skip($id);
}
$id = static::filtered()->countScoped()->count() - 1;
if ($id < 0) {
return redirect()->back()->with('error', 'no videos found');
}
$id = mt_rand(0, $id);
return static::filtered()->skip($id);
}
public function isSfw() {
return $this->tags->contains(function ($key, $tag) {
$tag->normalized === 'sfw';
});
}
public function filesize() {
return filesize(getcwd() . "/b/" . $this->file);
}
}