diff options
Diffstat (limited to 'modules/comment/helpers')
| -rw-r--r-- | modules/comment/helpers/comment.php | 71 | ||||
| -rw-r--r-- | modules/comment/helpers/comment_block.php | 39 | ||||
| -rw-r--r-- | modules/comment/helpers/comment_event.php | 97 | ||||
| -rw-r--r-- | modules/comment/helpers/comment_installer.php | 118 | ||||
| -rw-r--r-- | modules/comment/helpers/comment_rest.php | 74 | ||||
| -rw-r--r-- | modules/comment/helpers/comment_rss.php | 92 | ||||
| -rw-r--r-- | modules/comment/helpers/comment_theme.php | 46 | ||||
| -rw-r--r-- | modules/comment/helpers/comments_rest.php | 62 | ||||
| -rw-r--r-- | modules/comment/helpers/item_comments_rest.php | 50 |
9 files changed, 649 insertions, 0 deletions
diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php new file mode 100644 index 0000000..0d922eb --- /dev/null +++ b/modules/comment/helpers/comment.php @@ -0,0 +1,71 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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. + */ +class comment_Core { + static function get_add_form($item) { + $form = new Forge("comments/create/{$item->id}", "", "post", array("id" => "g-comment-form")); + $group = $form->group("add_comment")->label(t("Add comment")); + $group->input("name") + ->label(t("Name")) + ->id("g-author") + ->error_messages("required", t("You must enter a name for yourself")); + $group->input("email") + ->label(t("Email (hidden)")) + ->id("g-email") + ->error_messages("required", t("You must enter a valid email address")) + ->error_messages("invalid", t("You must enter a valid email address")); + $group->input("url") + ->label(t("Website (hidden)")) + ->id("g-url") + ->error_messages("url", t("You must enter a valid url")); + $group->textarea("text") + ->label(t("Comment")) + ->id("g-text") + ->error_messages("required", t("You must enter a comment")); + $group->hidden("item_id")->value($item->id); + module::event("comment_add_form", $form); + module::event("captcha_protect_form", $form); + $group->submit("")->value(t("Add"))->class("ui-state-default ui-corner-all"); + + return $form; + } + + static function prefill_add_form($form) { + $active = identity::active_user(); + if (!$active->guest) { + $group = $form->add_comment; + $group->inputs["name"]->value($active->full_name)->disabled("disabled"); + $group->email->value($active->email)->disabled("disabled"); + $group->url->value($active->url)->disabled("disabled"); + } + return $form; + } + + static function can_comment() { + return !identity::active_user()->guest || + module::get_var("comment", "access_permissions") == "everybody"; + } +} + diff --git a/modules/comment/helpers/comment_block.php b/modules/comment/helpers/comment_block.php new file mode 100644 index 0000000..b602595 --- /dev/null +++ b/modules/comment/helpers/comment_block.php @@ -0,0 +1,39 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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. + */ +class comment_block_Core { + static function get_admin_list() { + return array("recent_comments" => t("Recent comments")); + } + + static function get($block_id) { + $block = new Block(); + switch ($block_id) { + case "recent_comments": + $block->css_id = "g-recent-comments"; + $block->title = t("Recent comments"); + $block->content = new View("admin_block_recent_comments.html"); + $block->content->comments = + ORM::factory("comment")->order_by("created", "DESC")->limit(5)->find_all(); + break; + } + + return $block; + } +}
\ No newline at end of file diff --git a/modules/comment/helpers/comment_event.php b/modules/comment/helpers/comment_event.php new file mode 100644 index 0000000..f73e545 --- /dev/null +++ b/modules/comment/helpers/comment_event.php @@ -0,0 +1,97 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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. + */ +class comment_event_Core { + static function item_deleted($item) { + db::build() + ->delete("comments") + ->where("item_id", "=", $item->id) + ->execute(); + } + + static function user_deleted($user) { + $guest = identity::guest(); + if (!empty($guest)) { // could be empty if there is not identity provider + db::build() + ->update("comments") + ->set("author_id", $guest->id) + ->set("guest_email", null) + ->set("guest_name", "guest") + ->set("guest_url", null) + ->where("author_id", "=", $user->id) + ->execute(); + } + } + + static function identity_provider_changed($old_provider, $new_provider) { + $guest = identity::guest(); + db::build() + ->update("comments") + ->set("author_id", $guest->id) + ->set("guest_email", null) + ->set("guest_name", "guest") + ->set("guest_url", null) + ->execute(); + } + + static function admin_menu($menu, $theme) { + $menu->get("settings_menu") + ->append(Menu::factory("link") + ->id("comment") + ->label(t("Comments")) + ->url(url::site("admin/comments"))); + + $menu->get("content_menu") + ->append(Menu::factory("link") + ->id("comments") + ->label(t("Comments")) + ->url(url::site("admin/manage_comments"))); + } + + static function photo_menu($menu, $theme) { + $menu + ->append(Menu::factory("link") + ->id("comments") + ->label(t("View comments on this item")) + ->url("#comments") + ->css_id("g-comments-link")); + } + + static function item_index_data($item, $data) { + foreach (db::build() + ->select("text") + ->from("comments") + ->where("item_id", "=", $item->id) + ->execute() as $row) { + $data[] = $row->text; + } + } + + static function show_user_profile($data) { + $view = new View("user_profile_comments.html"); + $view->comments = ORM::factory("comment") + ->order_by("created", "DESC") + ->where("state", "=", "published") + ->where("author_id", "=", $data->user->id) + ->find_all(); + if ($view->comments->count()) { + $data->content[] = (object)array("title" => t("Comments"), "view" => $view); + } + } +} diff --git a/modules/comment/helpers/comment_installer.php b/modules/comment/helpers/comment_installer.php new file mode 100644 index 0000000..136f96e --- /dev/null +++ b/modules/comment/helpers/comment_installer.php @@ -0,0 +1,118 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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. + */ +class comment_installer { + static function install() { + $db = Database::instance(); + $db->query("CREATE TABLE IF NOT EXISTS {comments} ( + `author_id` int(9) default NULL, + `created` int(9) NOT NULL, + `guest_email` varchar(128) default NULL, + `guest_name` varchar(128) default NULL, + `guest_url` varchar(255) default NULL, + `id` int(9) NOT NULL auto_increment, + `item_id` int(9) NOT NULL, + `server_http_accept_charset` varchar(64) default NULL, + `server_http_accept_encoding` varchar(64) default NULL, + `server_http_accept_language` varchar(64) default NULL, + `server_http_accept` varchar(128) default NULL, + `server_http_connection` varchar(64) default NULL, + `server_http_host` varchar(64) default NULL, + `server_http_referer` varchar(255) default NULL, + `server_http_user_agent` varchar(128) default NULL, + `server_query_string` varchar(64) default NULL, + `server_remote_addr` varchar(40) default NULL, + `server_remote_host` varchar(255) default NULL, + `server_remote_port` varchar(16) default NULL, + `state` varchar(15) default 'unpublished', + `text` text, + `updated` int(9) NOT NULL, + PRIMARY KEY (`id`)) + DEFAULT CHARSET=utf8;"); + + module::set_var("comment", "spam_caught", 0); + module::set_var("comment", "access_permissions", "everybody"); + module::set_var("comment", "rss_visible", "all"); + } + + static function upgrade($version) { + $db = Database::instance(); + if ($version == 1) { + $db->query("ALTER TABLE {comments} CHANGE `state` `state` varchar(15) default 'unpublished'"); + module::set_version("comment", $version = 2); + } + + if ($version == 2) { + module::set_var("comment", "access_permissions", "everybody"); + module::set_version("comment", $version = 3); + } + + if ($version == 3) { + // 40 bytes for server_remote_addr is enough to swallow the longest + // representation of an IPv6 addy. + // + // 255 bytes for server_remote_host is enough to swallow the longest + // legit DNS entry, with a few bytes to spare. + $db->query( + "ALTER TABLE {comments} CHANGE `server_remote_addr` `server_remote_addr` varchar(40)"); + $db->query( + "ALTER TABLE {comments} CHANGE `server_remote_host` `server_remote_host` varchar(255)"); + module::set_version("comment", $version = 4); + } + + if ($version == 4) { + module::set_var("comment", "rss_visible", "all"); + module::set_version("comment", $version = 5); + } + + // In version 5 we accidentally set the installer variable to rss_available when it should + // have been rss_visible. Migrate it over now, if necessary. + if ($version == 5) { + if (!module::get_var("comment", "rss_visible")) { + module::set_var("comment", "rss_visible", module::get_var("comment", "rss_available")); + } + module::clear_var("comment", "rss_available"); + module::set_version("comment", $version = 6); + } + + // In version 6 we accidentally left the install value of "rss_visible" to "both" when it + // should have been "all" + if ($version == 6) { + if (module::get_var("comment", "rss_visible") == "both") { + module::set_var("comment", "rss_visible", "all"); + } + module::set_version("comment", $version = 7); + } + } + + static function uninstall() { + $db = Database::instance(); + + // Notify listeners that we're deleting some data. This is probably going to be very + // inefficient for large uninstalls, and we could make it better by doing things like passing + // a SQL fragment through so that the listeners could use subselects. But by using a single, + // simple event API we lighten the load on module developers. + foreach (ORM::factory("item") + ->join("comments", "items.id", "comments.item_id") + ->find_all() as $item) { + module::event("item_related_update", $item); + } + $db->query("DROP TABLE IF EXISTS {comments};"); + } +} diff --git a/modules/comment/helpers/comment_rest.php b/modules/comment/helpers/comment_rest.php new file mode 100644 index 0000000..1971edc --- /dev/null +++ b/modules/comment/helpers/comment_rest.php @@ -0,0 +1,74 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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. + */ +class comment_rest_Core { + static function get($request) { + $comment = rest::resolve($request->url); + access::required("view", $comment->item()); + + return array( + "url" => $request->url, + "entity" => $comment->as_restful_array(), + "relationships" => rest::relationships("comment", $comment)); + } + + static function put($request) { + // Only admins can edit comments, for now + if (!identity::active_user()->admin) { + access::forbidden(); + } + + $comment = rest::resolve($request->url); + $comment = ORM::factory("comment"); + $comment->text = $request->params->text; + $comment->save(); + } + + static function delete($request) { + if (!identity::active_user()->admin) { + access::forbidden(); + } + + $comment = rest::resolve($request->url); + access::required("edit", $comment->item()); + + $comment->delete(); + } + + static function relationships($resource_type, $resource) { + switch ($resource_type) { + case "item": + return array( + "comments" => array( + "url" => rest::url("item_comments", $resource))); + } + } + + static function resolve($id) { + $comment = ORM::factory("comment", $id); + if (!access::can("view", $comment->item())) { + throw new Kohana_404_Exception(); + } + return $comment; + } + + static function url($comment) { + return url::abs_site("rest/comment/{$comment->id}"); + } +} diff --git a/modules/comment/helpers/comment_rss.php b/modules/comment/helpers/comment_rss.php new file mode 100644 index 0000000..924710f --- /dev/null +++ b/modules/comment/helpers/comment_rss.php @@ -0,0 +1,92 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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. + */ + +class comment_rss_Core { + static function feed_visible($feed_id) { + $visible = module::get_var("comment", "rss_visible"); + if (!in_array($feed_id, array("newest", "per_item"))) { + return false; + } + + return ($visible == "all" || $visible == $feed_id); + } + + static function available_feeds($item, $tag) { + $feeds = array(); + + if (comment_rss::feed_visible("newest")) { + $feeds["comment/newest"] = t("All new comments"); + } + + if ($item && comment_rss::feed_visible("per_item")) { + $feeds["comment/per_item/$item->id"] = + t("Comments on %title", array("title" => html::purify($item->title))); + } + return $feeds; + } + + static function feed($feed_id, $offset, $limit, $id) { + if (!comment_rss::feed_visible($feed_id)) { + return; + } + + $comments = ORM::factory("comment") + ->viewable() + ->where("comments.state", "=", "published") + ->order_by("comments.created", "DESC"); + + if ($feed_id == "item") { + $item = ORM::factory("item", $id); + $comments + ->where("items.left_ptr", ">=", $item->left_ptr) + ->where("items.right_ptr", "<=", $item->right_ptr); + } + + $feed = new stdClass(); + $feed->view = "comment.mrss"; + $feed->comments = array(); + foreach ($comments->find_all($limit, $offset) as $comment) { + $item = $comment->item(); + $feed->comments[] = new ArrayObject( + array("pub_date" => date("D, d M Y H:i:s O", $comment->created), + "text" => nl2br(html::purify($comment->text)), + "thumb_url" => $item->thumb_url(), + "thumb_height" => $item->thumb_height, + "thumb_width" => $item->thumb_width, + "item_uri" => url::abs_site("{$item->type}s/$item->id"), + "title" => ( + ($item->id == item::root()->id) ? + html::purify($item->title) : + t("%site_title - %item_title", + array("site_title" => item::root()->title, + "item_title" => $item->title))), + "author" => html::clean($comment->author_name())), + ArrayObject::ARRAY_AS_PROPS); + } + + $feed->max_pages = ceil($comments->count_all() / $limit); + $feed->title = html::purify(t("%site_title - Recent Comments", + array("site_title" => item::root()->title))); + $feed->uri = url::abs_site("albums/" . (empty($id) ? "1" : $id)); + $feed->description = t("Recent comments"); + + return $feed; + } +} diff --git a/modules/comment/helpers/comment_theme.php b/modules/comment/helpers/comment_theme.php new file mode 100644 index 0000000..1c2d7c5 --- /dev/null +++ b/modules/comment/helpers/comment_theme.php @@ -0,0 +1,46 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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. + */ +class comment_theme_Core { + static function head($theme) { + return $theme->css("comment.css") + . $theme->script("comment.js"); + } + + static function admin_head($theme) { + return $theme->css("comment.css"); + } + + static function photo_bottom($theme) { + $block = new Block; + $block->css_id = "g-comments"; + $block->title = t("Comments"); + $block->anchor = "comments"; + + $view = new View("comments.html"); + $view->comments = ORM::factory("comment") + ->where("item_id", "=", $theme->item()->id) + ->where("state", "=", "published") + ->order_by("created", "ASC") + ->find_all(); + + $block->content = $view; + return $block; + } +}
\ No newline at end of file diff --git a/modules/comment/helpers/comments_rest.php b/modules/comment/helpers/comments_rest.php new file mode 100644 index 0000000..6fc86ad --- /dev/null +++ b/modules/comment/helpers/comments_rest.php @@ -0,0 +1,62 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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. + */ +class comments_rest_Core { + /** + * Possible request parameters: + * start=# + * start at the Nth comment (zero based) + * + * num=# + * return up to N comments (max 100) + */ + static function get($request) { + $comments = array(); + + $p = $request->params; + $num = isset($p->num) ? min((int)$p->num, 100) : 10; + $start = isset($p->start) ? (int)$p->start : 0; + + foreach (ORM::factory("comment")->viewable()->find_all($num, $start) as $comment) { + $comments[] = rest::url("comment", $comment); + } + return array("url" => rest::url("comments"), + "members" => $comments); + } + + + static function post($request) { + $entity = $request->params->entity; + + $item = rest::resolve($entity->item); + access::required("edit", $item); + + $comment = ORM::factory("comment"); + $comment->author_id = identity::active_user()->id; + $comment->item_id = $item->id; + $comment->text = $entity->text; + $comment->save(); + + return array("url" => rest::url("comment", $comment)); + } + + static function url() { + return url::abs_site("rest/comments"); + } +} diff --git a/modules/comment/helpers/item_comments_rest.php b/modules/comment/helpers/item_comments_rest.php new file mode 100644 index 0000000..f6f8930 --- /dev/null +++ b/modules/comment/helpers/item_comments_rest.php @@ -0,0 +1,50 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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. + */ +class item_comments_rest_Core { + static function get($request) { + $item = rest::resolve($request->url); + access::required("view", $item); + + $comments = array(); + foreach (ORM::factory("comment") + ->viewable() + ->where("item_id", "=", $item->id) + ->order_by("created", "DESC") + ->find_all() as $comment) { + $comments[] = rest::url("comment", $comment); + } + + return array( + "url" => $request->url, + "members" => $comments); + } + + static function resolve($id) { + $item = ORM::factory("item", $id); + if (!access::can("view", $item)) { + throw new Kohana_404_Exception(); + } + return $item; + } + + static function url($item) { + return url::abs_site("rest/item_comments/{$item->id}"); + } +} |
