-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.php
More file actions
92 lines (81 loc) · 2.59 KB
/
base.php
File metadata and controls
92 lines (81 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* @package Akismet
* @author Alan Hardman <alan@phpizza.com>
* @version 1.0.0
*/
namespace Plugin\Akismet;
use Model\Issue;
use Model\Issue\Comment;
class Base extends \Plugin
{
/**
* Initialize the plugin, adding authentication hooks and routes
*/
public function _load()
{
\Base::instance();
$this->_hook("model/issue.before_save", $this->issueBeforeSave(...));
$this->_hook("model/issue/comment.before_save", $this->commentBeforeSave(...));
}
/**
* Check if plugin is installed
* @return bool
*/
public function _installed()
{
return (bool) \Base::instance()->get("site.plugins.akismet.api_key");
}
/**
* Generate page for admin panel
*/
public function _admin()
{
$f3 = \Base::instance();
if ($f3->exists("POST.api_key")) {
if ($f3->get("POST.api_key")) {
$helper = Helper::instance();
if ($helper->isKeyValid($f3->get("POST.api_key"))) {
\Model\Config::setVal("site.plugins.akismet.api_key", $f3->get("POST.api_key"));
} else {
$f3->set("error", "Invalid API key.");
$f3->set("POST.api_key", null);
}
} else {
\Model\Config::setVal("site.plugins.akismet.api_key", null);
$f3->set("success", "Key removed, spam filter disabled.");
}
}
echo \Helper\View::instance()->render("akismet/view/admin.html");
}
/**
* Check for spam before saving issues
*/
public function issueBeforeSave(Issue $issue): Issue
{
if ($issue->changed("description")) {
$helper = Helper::instance();
if ($helper->isSpam($issue->description, "issue")) {
$f3 = \Base::instance();
$log = new \Log("akismet.log");
$log->write("Issue blocked: " . $f3->get("IP") . " - " . $f3->get("AGENT"));
throw new \Exception("Spam detected, not saving.");
}
}
return $issue;
}
/**
* Check for spam before saving issues
*/
public function commentBeforeSave(Comment $comment): Comment
{
$helper = Helper::instance();
if ($helper->isSpam($comment->text, "issue")) {
$f3 = \Base::instance();
$log = new \Log("akismet.log");
$log->write("Comment blocked: " . $f3->get("IP") . " - " . $f3->get("AGENT"));
throw new \Exception("Spam detected, not saving.");
}
return $comment;
}
}