2008-11-10 17:32:04 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
|
|
|
|
* Copyright (C) 2000-2008 Bharat Mediratta
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
|
* your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is the API for handling comments.
|
|
|
|
|
*
|
|
|
|
|
* Note: by design, this class does not do any permission checking.
|
|
|
|
|
*/
|
2008-11-22 06:06:02 +00:00
|
|
|
class comment_Core {
|
2008-11-12 15:43:05 +00:00
|
|
|
const SECONDS_IN_A_MINUTE = 60;
|
|
|
|
|
const SECONDS_IN_AN_HOUR = 3600;
|
|
|
|
|
const SECONDS_IN_A_DAY = 86400;
|
|
|
|
|
const SECONDS_IN_A_MONTH = 2629744;
|
|
|
|
|
const SECONDS_IN_A_YEAR = 31556926;
|
|
|
|
|
|
2008-11-10 17:32:04 +00:00
|
|
|
/**
|
2008-11-28 00:51:38 +00:00
|
|
|
* Create a new comment.
|
2008-11-10 17:32:04 +00:00
|
|
|
* @param string $author author's name
|
|
|
|
|
* @param string $email author's email
|
|
|
|
|
* @param string $text comment body
|
|
|
|
|
* @param integer $item_id id of parent item
|
2008-12-29 19:37:19 +00:00
|
|
|
* @param string $url author's url
|
2008-11-10 17:32:04 +00:00
|
|
|
* @return Comment_Model
|
|
|
|
|
*/
|
2008-12-30 21:00:47 +00:00
|
|
|
static function create($author, $email, $text, $item_id, $url=null) {
|
2008-11-10 17:32:04 +00:00
|
|
|
$comment = ORM::factory("comment");
|
|
|
|
|
$comment->author = $author;
|
|
|
|
|
$comment->email = $email;
|
|
|
|
|
$comment->text = $text;
|
|
|
|
|
$comment->item_id = $item_id;
|
2008-12-29 19:37:19 +00:00
|
|
|
$comment->url = $url;
|
2008-12-29 21:09:44 +00:00
|
|
|
$comment->ip_addr = Input::instance()->ip_address();
|
|
|
|
|
$comment->user_agent = Kohana::$user_agent;
|
2008-12-21 01:29:25 +00:00
|
|
|
$comment->created = time();
|
2008-11-10 17:32:04 +00:00
|
|
|
|
2008-12-29 19:37:19 +00:00
|
|
|
// @todo Figure out how to mock up the test of the spam_filter
|
2008-12-31 06:19:35 +00:00
|
|
|
if (module::is_installed("spam_filter") && TEST_MODE == 0) {
|
2009-01-06 14:36:20 +00:00
|
|
|
try {
|
|
|
|
|
SpamFilter::instance()->check_comment($comment);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
Kohana::log("error", print_r($e, 1));
|
|
|
|
|
$comment->state = "unpublished";
|
|
|
|
|
}
|
2008-12-29 19:37:19 +00:00
|
|
|
} else {
|
2009-01-02 18:54:37 +00:00
|
|
|
$comment->state = "published";
|
2008-12-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
2008-11-28 00:51:38 +00:00
|
|
|
$comment->save();
|
2008-11-28 19:37:01 +00:00
|
|
|
module::event("comment_created", $comment);
|
2008-11-28 00:51:38 +00:00
|
|
|
|
|
|
|
|
return $comment;
|
2008-11-10 17:32:04 +00:00
|
|
|
}
|
2008-11-11 20:54:12 +00:00
|
|
|
|
2008-12-29 19:37:19 +00:00
|
|
|
/**
|
|
|
|
|
* Update an existing comment.
|
|
|
|
|
* @param Comment_Model $comment
|
|
|
|
|
* @param string $author author's name
|
|
|
|
|
* @param string $email author's email
|
|
|
|
|
* @param string $text comment body
|
|
|
|
|
* @param string $url author's url
|
|
|
|
|
* @return Comment_Model
|
|
|
|
|
*/
|
|
|
|
|
static function update($comment, $author, $email, $text, $url) {
|
|
|
|
|
$comment->author = $author;
|
|
|
|
|
$comment->email = $email;
|
|
|
|
|
$comment->text = $text;
|
|
|
|
|
$comment->url = $url;
|
2008-12-29 21:09:44 +00:00
|
|
|
$comment->ip_addr = Input::instance()->ip_address();
|
|
|
|
|
$comment->user_agent = Kohana::$user_agent;
|
2008-12-29 19:37:19 +00:00
|
|
|
|
|
|
|
|
// @todo Figure out how to mock up the test of the spam_filter
|
2008-12-31 06:19:35 +00:00
|
|
|
if (module::is_installed("spam_filter") && TEST_MODE == 0) {
|
2009-01-02 18:31:23 +00:00
|
|
|
SpamFilter::instance()->check_comment($comment);
|
2008-12-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$comment->save();
|
|
|
|
|
if ($comment->saved) {
|
|
|
|
|
module::event("comment_updated", $comment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $comment;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-25 05:12:46 +00:00
|
|
|
static function get_add_form($item) {
|
2008-12-29 18:38:40 +00:00
|
|
|
$form = new Forge("comments", "", "post");
|
2008-12-18 22:35:16 +00:00
|
|
|
$group = $form->group("add_comment")->label(_("Add comment"));
|
2008-12-29 23:36:58 +00:00
|
|
|
$group->input("author") ->label(_("Author")) ->id("gAuthor");
|
|
|
|
|
$group->input("email") ->label(_("Email")) ->id("gEmail");
|
|
|
|
|
$group->input("url") ->label(_("Website (hidden)"))->id("gUrl");
|
|
|
|
|
$group->textarea("text") ->label(_("Text")) ->id("gText");
|
2008-12-25 05:12:46 +00:00
|
|
|
$group->hidden("item_id")->value($item->id);
|
2008-11-16 07:14:12 +00:00
|
|
|
$group->submit(_("Add"));
|
2008-11-16 07:51:42 +00:00
|
|
|
$form->add_rules_from(ORM::factory("comment"));
|
2008-11-16 07:14:12 +00:00
|
|
|
return $form;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function get_edit_form($comment) {
|
2008-12-29 18:38:40 +00:00
|
|
|
$form = new Forge("comments/{$comment->id}?_method=put", "", "post");
|
2008-12-18 22:35:16 +00:00
|
|
|
$group = $form->group("edit_comment")->label(_("Edit comment"));
|
2008-12-29 23:36:58 +00:00
|
|
|
$group->input("author") ->label(_("Author")) ->id("gAuthor")->value($comment->author);
|
|
|
|
|
$group->input("email") ->label(_("Email")) ->id("gEmail") ->value($comment->email);
|
|
|
|
|
$group->input("url") ->label(_("Website (hidden)"))->id("gUrl") ->value($comment->url);
|
|
|
|
|
$group->textarea("text")->label(_("Text")) ->id("gText") ->value($comment->text);
|
2008-11-16 07:14:12 +00:00
|
|
|
$group->submit(_("Edit"));
|
2008-11-16 07:51:42 +00:00
|
|
|
$form->add_rules_from($comment);
|
2008-11-16 07:14:12 +00:00
|
|
|
return $form;
|
|
|
|
|
}
|
2008-11-11 22:46:25 +00:00
|
|
|
}
|
|
|
|
|
|