Page 1 of 1

Question about how to make a comments plugin

Posted: Tue Nov 08, 2005 4:58 am
by Emil
Hi!

I working on a blog system that is only open to registered users (thanks for the HTTP-AUTH plugin!!)

And since all users are registered i wish to spare them the trouble of filling their name, email and such when postin comments. So I've been working on a plugin that will automatically do this. (by patching together other pulgins :-)

But I'm kind of new to this, and I don't really understand the way comments are saved in the s9y system. I've been looking at the code in "include/functions_comments.inc.php":

Code: Select all

function serendipity_saveComment($id, $commentInfo, $type = 'NORMAL', $source = 'internal') {
    global $serendipity;

    $query = "SELECT id, allow_comments, moderate_comments, last_modified, timestamp FROM {$serendipity['dbPrefix']}entries WHERE id = '". (int)$id ."'";
    $ca    = serendipity_db_query($query, true);

    $commentInfo['type'] = $type;
    $commentInfo['source'] = $source;
    serendipity_plugin_api::hook_event('frontend_saveComment', $ca, $commentInfo);
    if (!is_array($ca) || serendipity_db_bool($ca['allow_comments'])) {
        $title         = serendipity_db_escape_string(isset($commentInfo['title']) ? $commentInfo['title'] : '');
        $comments      = $commentInfo['comment'];
        $ip            = serendipity_db_escape_string(isset($commentInfo['ip']) ? $commentInfo['ip'] : $_SERVER['REMOTE_ADDR']);
        $commentsFixed = serendipity_db_escape_string($commentInfo['comment']);
        $name          = serendipity_db_escape_string($commentInfo['name']);
        $url           = serendipity_db_escape_string($commentInfo['url']);
        $email         = serendipity_db_escape_string($commentInfo['email']);
        $parentid      = (isset($commentInfo['parent_id']) && is_numeric($commentInfo['parent_id'])) ? $commentInfo['parent_id'] : 0;
        $status        = serendipity_db_escape_string(isset($commentInfo['status']) ? $commentInfo['status'] : (serendipity_db_bool($ca['moderate_comments']) ? 'pending' : 'approved'));
        $t             = serendipity_db_escape_string(isset($commentInfo['time']) ? $commentInfo['time'] : time());
        $referer       = (isset($_SESSION['HTTP_REFERER']) ? serendipity_db_escape_string($_SESSION['HTTP_REFERER']) : '');

        $query = "SELECT a.email, e.title, a.mail_comments, a.mail_trackbacks
                 FROM {$serendipity['dbPrefix']}entries e, {$serendipity['dbPrefix']}authors a
                 WHERE e.id  = '". (int)$id ."'
                 AND e.authorid = a.authorid";
        $row = serendipity_db_query($query, true); // Get info on author/entry
        if (!is_array($row) || empty($id)) {
            // No associated entry found.
            return false;
        }

        if (isset($commentInfo['subscribe'])) {
            $subscribe = 'true';
        } else {
            $subscribe = 'false';
        }

        $query  = "INSERT INTO {$serendipity['dbPrefix']}comments (entry_id, parent_id, ip, author, email, url, body, type, timestamp, title, subscribed, status, referer)";
        $query .= " VALUES ('". (int)$id ."', '$parentid', '$ip', '$name', '$email', '$url', '$commentsFixed', '$type', '$t', '$title', '$subscribe', '$status', '$referer')";

        serendipity_db_query($query);
        $cid = serendipity_db_insert_id('comments', 'id');

        // Send mail to the author if he chose to receive these mails, or if the comment is awaiting moderation
        if (serendipity_db_bool($ca['moderate_comments'])
            || ($type == 'NORMAL' && serendipity_db_bool($row['mail_comments']))
            || ($type == 'TRACKBACK' && serendipity_db_bool($row['mail_trackbacks']))) {
            serendipity_sendComment($cid, $row['email'], $name, $email, $url, $id, $row['title'], $comments, $type, serendipity_db_bool($ca['moderate_comments']));
        }

        // Approve with force, if moderation is disabled
        if (empty($ca['moderate_comments']) || serendipity_db_bool($ca['moderate_comments']) == false) {
            serendipity_approveComment($cid, $id, true);
        }
        serendipity_purgeEntry($id, $t);
        return true;
    } else {
        return false;
    }
}
And my thought is to use the 'frontend_saveComment' hook to modify $commentInfo['name'] etc.

So far my code looks like this:

Code: Select all

    function event_hook($event, &$bag, &$eventData) {
        global $serendipity, $commentInfo;
		
        $hooks = &$bag->get('event_hooks');

        if (isset($hooks[$event])) {
                $this->debug('Tjekker hooks.');
            switch($event) {
				case 'frontend_saveComment':
					$this->debug('Fundet hooks frontend_saveComment');
                    if (1 == 1) {
					                $this->debug('If saetning er sand');
									$this->debug($commentInfo['name']);
                        $commentInfo['name'] = "Blog Bruger";
				  $this->debug($commentInfo['name']);
                    }
                    return true;
                    break;

                default:
                  return false;
            }
        } else {
            return false;
        }
    }
My debuggin says that the code gets executed, but the desired result is nowhere to be seen.

So if anyone could give me some hints on how to modify the data submited in a comment form before saving it to the database I would be grateful.

Notes: setting name to "Blog Bruger" will of course later be replaced by a variable containing the Author name, and e-mail website and maybe other stuff will be replaced in the code as well.

-Thanks in advance for any help!

Re: Question about how to make a comments plugin

Posted: Tue Nov 08, 2005 11:33 am
by garvinhicking
The plugin serendipity_event_adduser already contains code that enabled comments only available for registered users, and fills in the values automagically. You can look at that plugin to borrow this code snippet :)

You already spotted the right hook, I think :)

If you need more help, just report back :)

Regards,
Garvin

Thanks

Posted: Tue Nov 08, 2005 1:58 pm
by Emil
Hey Garvin,

Thanks for the tip! - By the way, love your work.

-Emil