View file app/Models/Flood.php

File size: 1.83Kb
<?php

namespace App\Models;

/**
 * Class Flood
 *
 * @property int id
 * @property int user_id
 * @property string page
 * @property int created_at
 */
class Flood extends BaseModel
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * Определяет время антифлуда
     *
     * @return int
     */
    public static function getPeriod(): int
    {
        if (isAdmin()) {
            return 0;
        }

        $period = setting('floodstime');

        if (getUser('point') >= 100) {
            $period = round(setting('floodstime') / 2);
        }

        if (getUser('point') >= 300) {
            $period = round(setting('floodstime') / 3);
        }

        if (getUser('point') >= 500) {
            $period = round(setting('floodstime') / 6);
        }

        return $period;
    }

    /**
     * Проверяет сообщение на флуд
     *
     * @param int $period
     * @return bool
     */
    public static function isFlood($period = 0): bool
    {
        $userId = getUser('id');
        $period = $period ?: self::getPeriod();

        if (empty($period)) {
            return true;
        }

        self::query()->where('created_at', '<', SITETIME)->delete();

        $flood = self::query()
            ->where('user_id', $userId)
            ->where('page', server('PHP_SELF'))
            ->first();

        if (! $flood) {
            self::query()->create([
                'user_id'    => $userId,
                'page'       => server('PHP_SELF'),
                'created_at' => SITETIME + $period,
            ]);

            return true;
        }

        return false;
    }
}