-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.php
87 lines (79 loc) · 2.28 KB
/
base.php
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
<?php
/**
* @package Akismet
* @author Alan Hardman <[email protected]>
* @version 1.0.0
*/
namespace Plugin\Akismet;
class Base extends \Plugin {
/**
* Initialize the plugin, adding authentication hooks and routes
*/
public function _load() {
$f3 = \Base::instance();
$this->_hook("model/issue.before_save", array($this, "issueBeforeSave"));
$this->_hook("model/issue/comment.before_save", array($this, "commentBeforeSave"));
}
/**
* Check if plugin is installed
* @return bool
*/
public function _installed() {
return !!\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
*
* @param \Model\Issue $issue
* @return \Model\Issue
*/
public function issueBeforeSave(\Model\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
*
* @param \Model\Issue\Comment $comment
* @return \Model\Issue\Comment
*/
public function commentBeforeSave(\Model\Issue\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;
}
}