Simple status box for the sidebar.
[elgg_plugins.git] / akismet / lib.php
blobb024fcbf106ee9165e268207410a78c280d4e78a
1 <?php
3 /*
4 Elgg Akismet plugin
6 Ben Werdmuller, Dec 18 2006
7 ben@curverider.co.uk
9 Released under the GPL v2
12 global $CFG;
14 // Configuration - enter your Akismet API key and blog URL (including http://) here
15 // See http://akismet.com/commercial/ for more details.
16 $CFG->akismet->apikey = "";
17 $CFG->akismet->blogurl = "";
19 // Menu items
20 function akismet_pagesetup() {
21 // TODO: add a menu item to flagged comments
24 // Initialise the database etc
25 function akismet_init() {
27 global $CFG, $function, $db, $METATABLES;
31 TODO: add flagged comments database and interface to manage false positives
32 and erase real spam
34 // Check for the akismet database
35 if (!in_array($CFG->prefix . "akismet_comments", $METATABLES) || !in_array($CFG->prefix . "akismet_comments", $METATABLES)) {
36 if (file_exists($CFG->dirroot . "mod/akismet/$CFG->dbtype.sql")) {
37 modify_database($CFG->dirroot . "mod/akismet/$CFG->dbtype.sql");
38 } else {
39 error("Error: Your database ($CFG->dbtype) is not yet fully supported by the Elgg Akismet plugin. See the mod/akismet directory.");
41 print_continue("index.php");
42 exit;
44 */
45 // Register the event: we want to know when a comment is created
47 listen_for_event("comment","create","akismet_vet");
48 listen_for_event("weblog_comment","create","akismet_vet");
52 // Vet comments for spam
53 function akismet_vet($object_type, $event, $object) {
55 global $CFG, $messages;
57 if (($object_type == "comment" || $object_type == "weblog_comment") && !empty($CFG->akismet->apikey) && !empty($CFG->akismet->blogurl)) {
59 // Load Akismet class
60 @require_once($CFG->dirroot . "mod/akismet/PHP5Akismet.0.2/Akismet.class.php");
62 // Get the parent post
63 $post = get_record('weblog_posts','ident',$object->post_id);
65 // Generate the post URL
66 if (!isset($object->comment_type)) {
67 $object->comment_type = "weblog";
69 switch($object->comment_type) {
70 case "weblog":
71 $url = $CFG->wwwroot . user_info("username", $post->weblog) . "/". $object->comment_type ."/" . $post->ident . ".html";
72 break;
75 // Initialise Akismet
76 $akismet = new Akismet($CFG->akismet->blogurl,$CFG->akismet->apikey);
77 $akismet->setCommentAuthor($object->postedname);
78 $akismet->setCommentAuthorEmail("");
79 $akismet->setCommentAuthorURL("");
80 $akismet->setCommentContent($object->body);
81 $akismet->setPermalink($url);
83 if ($akismet->isCommentSpam()) {
84 $messages[] = __gettext("Your comment could not be posted.");
85 return null;
86 } else {
87 return $object;
90 } else {
91 return $object;