mirror of
https://github.com/flarum/core.git
synced 2025-08-13 11:54:32 +02:00
Initial commit
This commit is contained in:
23
extensions/akismet/src/Extension.php
Normal file
23
extensions/akismet/src/Extension.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Akismet;
|
||||
|
||||
use Flarum\Support\Extension as BaseExtension;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
|
||||
class Extension extends BaseExtension
|
||||
{
|
||||
public function listen(Dispatcher $events)
|
||||
{
|
||||
$events->subscribe('Flarum\Akismet\Listeners\AddClientAssets');
|
||||
$events->subscribe('Flarum\Akismet\Listeners\ValidatePost');
|
||||
}
|
||||
}
|
52
extensions/akismet/src/Listeners/AddClientAssets.php
Normal file
52
extensions/akismet/src/Listeners/AddClientAssets.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Akismet\Listeners;
|
||||
|
||||
use Flarum\Events\RegisterLocales;
|
||||
use Flarum\Events\BuildClientView;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddClientAssets
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(RegisterLocales::class, [$this, 'addLocale']);
|
||||
$events->listen(BuildClientView::class, [$this, 'addAssets']);
|
||||
}
|
||||
|
||||
public function addLocale(RegisterLocales $event)
|
||||
{
|
||||
$event->addTranslations('en', __DIR__.'/../../locale/en.yml');
|
||||
}
|
||||
|
||||
public function addAssets(BuildClientView $event)
|
||||
{
|
||||
$event->forumAssets([
|
||||
__DIR__.'/../../js/forum/dist/extension.js'
|
||||
]);
|
||||
|
||||
$event->forumBootstrapper('akismet/main');
|
||||
|
||||
$event->forumTranslations([
|
||||
// 'akismet.hello_world'
|
||||
]);
|
||||
|
||||
$event->adminAssets([
|
||||
__DIR__.'/../../js/admin/dist/extension.js'
|
||||
]);
|
||||
|
||||
$event->adminBootstrapper('akismet/main');
|
||||
|
||||
$event->adminTranslations([
|
||||
// 'akismet.hello_world'
|
||||
]);
|
||||
}
|
||||
}
|
78
extensions/akismet/src/Listeners/ValidatePost.php
Normal file
78
extensions/akismet/src/Listeners/ValidatePost.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Akismet\Listeners;
|
||||
|
||||
use Flarum\Events\PostWillBeSaved;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use TijsVerkoyen\Akismet\Akismet;
|
||||
use Flarum\Core;
|
||||
use Flarum\Core\Posts\CommentPost;
|
||||
use Flarum\Core\Settings\SettingsRepository;
|
||||
use Flarum\Report\Report;
|
||||
|
||||
class ValidatePost
|
||||
{
|
||||
protected $settings;
|
||||
|
||||
private $savingPost;
|
||||
|
||||
public function __construct(SettingsRepository $settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(PostWillBeSaved::class, [$this, 'validatePost']);
|
||||
}
|
||||
|
||||
public function validatePost(PostWillBeSaved $event)
|
||||
{
|
||||
$post = $event->post;
|
||||
|
||||
if ($post->exists || $post->user->groups()->count()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$akismet = new Akismet($this->settings->get('akismet.api_key'), Core::url());
|
||||
|
||||
$isSpam = $akismet->isSpam(
|
||||
$post->content,
|
||||
$post->user->username,
|
||||
$post->user->email,
|
||||
null,
|
||||
'comment'
|
||||
);
|
||||
|
||||
if ($isSpam) {
|
||||
$post->hide();
|
||||
|
||||
$this->savingPost = $post;
|
||||
|
||||
CommentPost::saved(function (CommentPost $post) {
|
||||
if ($post !== $this->savingPost) {
|
||||
return;
|
||||
}
|
||||
|
||||
$report = new Report;
|
||||
|
||||
$report->post_id = $post->id;
|
||||
$report->reporter = 'Akismet';
|
||||
$report->reason = 'spam';
|
||||
$report->time = time();
|
||||
|
||||
$report->save();
|
||||
|
||||
$this->savingPost = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user