diff options
Diffstat (limited to 'modules/gallery')
22 files changed, 2028 insertions, 2 deletions
diff --git a/modules/gallery/config/identity.php b/modules/gallery/config/identity.php new file mode 100644 index 00000000..72d1b589 --- /dev/null +++ b/modules/gallery/config/identity.php @@ -0,0 +1,37 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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. + */ +/* + * @package Identity + * + * User settings, defined as arrays, or "groups". If no group name is + * used when loading the cache library, the group named "default" will be used. + * + * Each group can be used independently, and multiple groups can be used at once. + * + * Group Options: + * driver - User backend driver. Gallery comes with Gallery user driver. + * + * params - Driver parameters, specific to each driver. + */ +$config["default"] = array ( + "driver" => "gallery", + "allow_updates" => false, + "params" => array(), +); diff --git a/modules/gallery/controllers/admin_users.php b/modules/gallery/controllers/admin_users.php new file mode 100644 index 00000000..6c72440a --- /dev/null +++ b/modules/gallery/controllers/admin_users.php @@ -0,0 +1,290 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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 Admin_Users_Controller extends Admin_Controller { + public function index() { + $view = new Admin_View("admin.html"); + $view->content = new View("admin_users.html"); + $view->content->users = user::users(array("orderby" => array("name" => "ASC"))); + $view->content->groups = group::groups(array("orderby" => array("name" => "ASC"))); + print $view; + } + + public function add_user() { + access::verify_csrf(); + + $form = user::get_add_form_admin(); + $valid = $form->validate(); + $name = $form->add_user->inputs["name"]->value; + if ($user = user::lookup_by_name($name)) { + $form->add_user->inputs["name"]->add_error("in_use", 1); + $valid = false; + } + + if ($valid) { + $user = user::create( + $name, $form->add_user->full_name->value, $form->add_user->password->value); + $user->email = $form->add_user->email->value; + $user->admin = $form->add_user->admin->checked; + + if ($form->add_user->locale) { + $desired_locale = $form->add_user->locale->value; + $user->locale = $desired_locale == "none" ? null : $desired_locale; + } + $user->save(); + module::event("user_add_form_admin_completed", $user, $form); + + message::success(t("Created user %user_name", array("user_name" => $user->name))); + print json_encode(array("result" => "success")); + } else { + print json_encode(array("result" => "error", + "form" => $form->__toString())); + } + } + + public function add_user_form() { + print user::get_add_form_admin(); + } + + public function delete_user($id) { + access::verify_csrf(); + + if ($id == user::active()->id || $id == user::guest()->id) { + access::forbidden(); + } + + $user = user::lookup($id); + if (empty($user)) { + kohana::show_404(); + } + + $form = user::get_delete_form_admin($user); + if($form->validate()) { + $name = $user->name; + $user->delete(); + } else { + print json_encode(array("result" => "error", + "form" => $form->__toString())); + } + + $message = t("Deleted user %user_name", array("user_name" => $name)); + log::success("user", $message); + message::success($message); + print json_encode(array("result" => "success")); + } + + public function delete_user_form($id) { + $user = user::lookup($id); + if (empty($user)) { + kohana::show_404(); + } + print user::get_delete_form_admin($user); + } + + public function edit_user($id) { + access::verify_csrf(); + + $user = user::lookup($id); + if (empty($user)) { + kohana::show_404(); + } + + $form = user::get_edit_form_admin($user); + $valid = $form->validate(); + if ($valid) { + $new_name = $form->edit_user->inputs["name"]->value; + $temp_user = user::lookup_by_name($new_name); + if ($new_name != $user->name && + ($temp_user && $temp_user->id != $user->id)) { + $form->edit_user->inputs["name"]->add_error("in_use", 1); + $valid = false; + } else { + $user->name = $new_name; + } + } + + if ($valid) { + $user->full_name = $form->edit_user->full_name->value; + if ($form->edit_user->password->value) { + $user->password = $form->edit_user->password->value; + } + $user->email = $form->edit_user->email->value; + $user->url = $form->edit_user->url->value; + if ($form->edit_user->locale) { + $desired_locale = $form->edit_user->locale->value; + $user->locale = $desired_locale == "none" ? null : $desired_locale; + } + + // An admin can change the admin status for any user but themselves + if ($user->id != user::active()->id) { + $user->admin = $form->edit_user->admin->checked; + } + $user->save(); + module::event("user_edit_form_admin_completed", $user, $form); + + message::success(t("Changed user %user_name", array("user_name" => $user->name))); + print json_encode(array("result" => "success")); + } else { + print json_encode(array("result" => "error", + "form" => $form->__toString())); + } + } + + public function edit_user_form($id) { + $user = user::lookup($id); + if (empty($user)) { + kohana::show_404(); + } + + $form = user::get_edit_form_admin($user); + // Don't allow the user to control their own admin bit, else you can lock yourself out + if ($user->id == user::active()->id) { + $form->edit_user->admin->disabled(1); + } + print $form; + } + + public function add_user_to_group($user_id, $group_id) { + access::verify_csrf(); + $group = group::lookup($group_id); + $user = user::lookup($user_id); + $group->add($user); + $group->save(); + } + + public function remove_user_from_group($user_id, $group_id) { + access::verify_csrf(); + $group = group::lookup($group_id); + $user = user::lookup($user_id); + $group->remove($user); + $group->save(); + } + + public function group($group_id) { + $view = new View("admin_users_group.html"); + $view->group = group::lookup($group_id); + print $view; + } + + public function add_group() { + access::verify_csrf(); + + $form = group::get_add_form_admin(); + $valid = $form->validate(); + if ($valid) { + $new_name = $form->add_group->inputs["name"]->value; + $group = group::lookup_by_name($new_name); + if (!empty($group)) { + $form->add_group->inputs["name"]->add_error("in_use", 1); + $valid = false; + } + } + + if ($valid) { + $group = group::create($new_name); + $group->save(); + message::success( + t("Created group %group_name", array("group_name" => $group->name))); + print json_encode(array("result" => "success")); + } else { + print json_encode(array("result" => "error", + "form" => $form->__toString())); + } + } + + public function add_group_form() { + print group::get_add_form_admin(); + } + + public function delete_group($id) { + access::verify_csrf(); + + $group = group::lookup($id); + if (empty($group)) { + kohana::show_404(); + } + + $form = group::get_delete_form_admin($group); + if ($form->validate()) { + $name = $group->name; + $group->delete(); + } else { + print json_encode(array("result" => "error", + "form" => $form->__toString())); + } + + $message = t("Deleted group %group_name", array("group_name" => $name)); + log::success("group", $message); + message::success($message); + print json_encode(array("result" => "success")); + } + + public function delete_group_form($id) { + $group = group::lookup($id); + if (empty($group)) { + kohana::show_404(); + } + + print group::get_delete_form_admin($group); + } + + public function edit_group($id) { + access::verify_csrf(); + + $group = group::lookup($id); + if (empty($group)) { + kohana::show_404(); + } + + $form = group::get_edit_form_admin($group); + $valid = $form->validate(); + + if ($valid) { + $new_name = $form->edit_group->inputs["name"]->value; + $group = group::lookup_by_name($name); + if ($group->loaded) { + $form->edit_group->inputs["name"]->add_error("in_use", 1); + $valid = false; + } + } + + if ($valid) { + $group->name = $form->edit_group->inputs["name"]->value; + $group->save(); + message::success( + t("Changed group %group_name", array("group_name" => $group->name))); + print json_encode(array("result" => "success")); + } else { + message::error( + t("Failed to change group %group_name", array("group_name" => $group->name))); + print json_encode(array("result" => "error", + "form" => $form->__toString())); + } + } + + public function edit_group_form($id) { + $group = group::lookup($id); + if (empty($group)) { + kohana::show_404(); + } + + print group::get_edit_form_admin($group); + } + +} diff --git a/modules/gallery/controllers/login.php b/modules/gallery/controllers/login.php new file mode 100644 index 00000000..2c4bd557 --- /dev/null +++ b/modules/gallery/controllers/login.php @@ -0,0 +1,81 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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 Login_Controller extends Controller { + + public function ajax() { + $view = new View("login_ajax.html"); + $view->form = user::get_login_form("login/auth_ajax"); + print $view; + } + + public function auth_ajax() { + access::verify_csrf(); + + list ($valid, $form) = $this->_auth("login/auth_ajax"); + if ($valid) { + print json_encode( + array("result" => "success")); + } else { + print json_encode( + array("result" => "error", + "form" => $form->__toString())); + } + } + + public function html() { + print user::get_login_form("login/auth_html"); + } + + public function auth_html() { + access::verify_csrf(); + + list ($valid, $form) = $this->_auth("login/auth_html"); + if ($valid) { + url::redirect(item::root()->abs_url()); + } else { + print $form; + } + } + private function _auth($url) { + $form = user::get_login_form($url); + $valid = $form->validate(); + if ($valid) { + $user = user::lookup_by_name($form->login->inputs["name"]->value); + if (empty($user) || !user::is_correct_password($user, $form->login->password->value)) { + log::warning( + "user", + t("Failed login for %name", + array("name" => $form->login->inputs["name"]->value))); + $form->login->inputs["name"]->add_error("invalid_login", 1); + $valid = false; + } + } + + if ($valid) { + user::login($user); + log::info("user", t("User %name logged in", array("name" => $user->name))); + } + + // Either way, regenerate the session id to avoid session trapping + Session::instance()->regenerate(); + + return array($valid, $form); + } +}
\ No newline at end of file diff --git a/modules/gallery/controllers/logout.php b/modules/gallery/controllers/logout.php new file mode 100644 index 00000000..45d397ad --- /dev/null +++ b/modules/gallery/controllers/logout.php @@ -0,0 +1,38 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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 Logout_Controller extends Controller { + public function index() { + //access::verify_csrf(); + + $user = user::active(); + user::logout(); + log::info("user", t("User %name logged out", array("name" => $user->name)), + html::anchor("user/$user->id", html::clean($user->name))); + if ($continue_url = $this->input->get("continue")) { + $item = url::get_item_from_uri($continue_url); + if (access::can("view", $item)) { + // Don't use url::redirect() because it'll call url::site() and munge the continue url. + header("Location: $continue_url"); + } else { + url::redirect(item::root()->abs_url()); + } + } + } +}
\ No newline at end of file diff --git a/modules/gallery/controllers/password.php b/modules/gallery/controllers/password.php new file mode 100644 index 00000000..817ff01c --- /dev/null +++ b/modules/gallery/controllers/password.php @@ -0,0 +1,133 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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 Password_Controller extends Controller { + public function reset() { + if (request::method() == "post") { + // @todo separate the post from get parts of this function + access::verify_csrf(); + $this->_send_reset(); + } else { + print $this->_reset_form(); + } + } + + public function do_reset() { + if (request::method() == "post") { + $this->_change_password(); + } else { + $user = user::lookyp_by_hash(Input::instance()->get("key")); + if (!empty($user)) { + print $this->_new_password_form($user->hash); + } else { + throw new Exception("@todo FORBIDDEN", 503); + } + } + } + + private function _send_reset() { + $form = $this->_reset_form(); + + $valid = $form->validate(); + if ($valid) { + $user = user::lockup_by_name($form->reset->inputs["name"]->value); + if (!$user->loaded || empty($user->email)) { + $form->reset->inputs["name"]->add_error("no_email", 1); + $valid = false; + } + } + + if ($valid) { + $user->hash = md5(rand()); + $user->save(); + $message = new View("reset_password.html"); + $message->confirm_url = url::abs_site("password/do_reset?key=$user->hash"); + $message->user = $user; + + Sendmail::factory() + ->to($user->email) + ->subject(t("Password Reset Request")) + ->header("Mime-Version", "1.0") + ->header("Content-type", "text/html; charset=iso-8859-1") + ->message($message->render()) + ->send(); + + log::success( + "user", + t("Password reset email sent for user %name", array("name" => $user->name))); + } else { + // Don't include the username here until you're sure that it's XSS safe + log::warning( + "user", "Password reset email requested for bogus user"); + } + + message::success(t("Password reset email sent")); + print json_encode( + array("result" => "success")); + } + + private function _reset_form() { + $form = new Forge(url::current(true), "", "post", array("id" => "g-reset-form")); + $group = $form->group("reset")->label(t("Reset Password")); + $group->input("name")->label(t("Username"))->id("g-name")->class(null)->rules("required"); + $group->inputs["name"]->error_messages("no_email", t("No email, unable to reset password")); + $group->submit("")->value(t("Reset")); + + return $form; + } + + private function _new_password_form($hash=null) { + $template = new Theme_View("page.html", "reset"); + + $form = new Forge("password/do_reset", "", "post", array("id" => "g-change-password-form")); + $group = $form->group("reset")->label(t("Change Password")); + $hidden = $group->hidden("hash"); + if (!empty($hash)) { + $hidden->value($hash); + } + $group->password("password")->label(t("Password"))->id("g-password") + ->rules("required|length[1,40]"); + $group->password("password2")->label(t("Confirm Password"))->id("g-password2") + ->matches($group->password); + $group->inputs["password2"]->error_messages( + "mistyped", t("The password and the confirm password must match")); + $group->submit("")->value(t("Update")); + + $template->content = $form; + return $template; + } + + private function _change_password() { + $view = $this->_new_password_form(); + if ($view->content->validate()) { + $user = user::lookyp_by_hash(Input::instance()->get("key")); + if (empty($user)) { + throw new Exception("@todo FORBIDDEN", 503); + } + + $user->password = $view->content->reset->password->value; + $user->hash = null; + $user->save(); + message::success(t("Password reset successfully")); + url::redirect(item::root()->abs_url()); + } else { + print $view; + } + } +}
\ No newline at end of file diff --git a/modules/gallery/controllers/users.php b/modules/gallery/controllers/users.php new file mode 100644 index 00000000..07c5a457 --- /dev/null +++ b/modules/gallery/controllers/users.php @@ -0,0 +1,68 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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 Users_Controller extends Controller { + public function update($id) { + $user = user::lookup($id); + + if ($user->guest || $user->id != user::active()->id) { + access::forbidden(); + } + + $form = user::get_edit_form($user); + $valid = $form->validate(); + if ($valid) { + $user->full_name = $form->edit_user->full_name->value; + if ($form->edit_user->password->value) { + $user->password = $form->edit_user->password->value; + } + $user->email = $form->edit_user->email->value; + $user->url = $form->edit_user->url->value; + if ($form->edit_user->locale) { + $desired_locale = $form->edit_user->locale->value; + $new_locale = $desired_locale == "none" ? null : $desired_locale; + if ($new_locale != $user->locale) { + // Delete the session based locale preference + setcookie("g_locale", "", time() - 24 * 3600, "/"); + } + $user->locale = $new_locale; + } + $user->save(); + module::event("user_edit_form_completed", $user, $form); + + message::success(t("User information updated.")); + print json_encode( + array("result" => "success", + "resource" => url::site("users/{$user->id}"))); + } else { + print json_encode( + array("result" => "error", + "form" => $form->__toString())); + } + } + + public function form_edit($id) { + $user = user::lookup($id); + if ($user->guest || $user->id != user::active()->id) { + access::forbidden(); + } + + print user::get_edit_form($user); + } +} diff --git a/modules/gallery/helpers/gallery_block.php b/modules/gallery/helpers/gallery_block.php index 5d49a9de..f43d82c9 100644 --- a/modules/gallery/helpers/gallery_block.php +++ b/modules/gallery/helpers/gallery_block.php @@ -28,6 +28,10 @@ class gallery_block_Core { "project_news" => t("Gallery Project News")); } + static function get_site_list() { + return array("language" => t("Language Preference")); + } + static function get($block_id) { $block = new Block(); switch($block_id) { @@ -85,6 +89,25 @@ class gallery_block_Core { $block->css_id = "g-block-adder"; $block->title = t("Dashboard Content"); $block->content = self::get_add_block_form(); + break; + + case "language": + $locales = locales::installed(); + if (count($locales)) { + foreach ($locales as $locale => $display_name) { + $locales[$locale] = SafeString::of_safe_html($display_name); + } + $block = new Block(); + $block->css_id = "g-user-language-block"; + $block->title = t("Language Preference"); + $block->content = new View("user_languages_block.html"); + $block->content->installed_locales = + array_merge(array("" => t("« none »")), $locales); + $block->content->selected = (string) user::cookie_locale(); + } else { + $block = ""; + } + break; } return $block; diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index 290d7d12..e0de2152 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -19,6 +19,14 @@ */ class gallery_event_Core { + /** + * Initialization. + */ + static function gallery_ready() { + user::load_user(); + locales::set_request_locale(); + } + static function group_created($group) { access::add_group($group); } @@ -179,6 +187,10 @@ class gallery_event_Core { ->id("sidebar") ->label(t("Manage Sidebar")) ->url(url::site("admin/sidebar")))) + ->append(Menu::factory("link") + ->id("users_groups") + ->label(t("Users/Groups")) + ->url(url::site("admin/users"))) ->append(Menu::factory("submenu") ->id("statistics_menu") ->label(t("Statistics"))) diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index 20dfeb04..54b35fb7 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -37,6 +37,11 @@ class gallery_theme_Core { } } + if (count(locales::installed())) { + // Needed by the languages block + $theme->script("jquery.cookie.js"); + } + if ($session->get("l10n_mode", false)) { $theme->css("l10n_client.css"); $theme->script("jquery.cookie.js"); @@ -46,6 +51,15 @@ class gallery_theme_Core { return $buf; } + static function header_top($theme) { + if ($theme->page_type != "login") { + $view = new View("login.html"); + $view->user = user::active(); + $view->writable = user::is_writable(); + return $view->render(); + } + } + static function admin_head($theme) { $theme->script("gallery.panel.js"); $session = Session::instance(); diff --git a/modules/gallery/helpers/group.php b/modules/gallery/helpers/group.php new file mode 100644 index 00000000..074a7b83 --- /dev/null +++ b/modules/gallery/helpers/group.php @@ -0,0 +1,123 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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 groups. + * + * Note: by design, this class does not do any permission checking. + */ +class group_Core { + static function get_edit_form_admin($group) { + $form = new Forge("admin/users/edit_group/$group->id", "", "post", array("id" => "g-edit-group-form")); + $form_group = $form->group("edit_group")->label(t("Edit Group")); + $form_group->input("name")->label(t("Name"))->id("g-name")->value($group->name); + $form_group->inputs["name"]->error_messages( + "in_use", t("There is already a group with that name")); + $form_group->submit("")->value(t("Save")); + $form->add_rules_from(self::get_edit_rules()); + return $form; + } + + static function get_add_form_admin() { + $form = new Forge("admin/users/add_group", "", "post", array("id" => "g-add-group-form")); + $form->set_attr('class', "g-narrow"); + $form_group = $form->group("add_group")->label(t("Add Group")); + $form_group->input("name")->label(t("Name"))->id("g-name"); + $form_group->inputs["name"]->error_messages( + "in_use", t("There is already a group with that name")); + $form_group->submit("")->value(t("Add Group")); + $group = ORM::factory("group"); + $form->add_rules_from(self::get_edit_rules()); + return $form; + } + + static function get_delete_form_admin($group) { + $form = new Forge("admin/users/delete_group/$group->id", "", "post", + array("id" => "g-delete-group-form")); + $form_group = $form->group("delete_group")->label( + t("Are you sure you want to delete group %group_name?", array("group_name" => $group->name))); + $form_group->submit("")->value(t("Delete")); + return $form; + } + + /** + * Create a new group. + * + * @param string $name + * @return Group_Core + */ + static function create($name) { + return Identity::instance()->create_group($name); + } + + /** + * The group of all possible visitors. This includes the guest user. + * + * @return Group_Core + */ + static function everybody() { + return Identity::instance()->everybody(); + } + + /** + * The group of all logged-in visitors. This does not include guest users. + * + * @return Group_Core + */ + static function registered_users() { + return Identity::instance()->everybody(); + } + + /** + * Look up a group by id. + * @param integer $id the user id + * @return Group_Model the group object, or null if the id was invalid. + */ + static function lookup($id) { + return Identity::instance()->lookup_group($id); + } + + /** + * Look up a group by name. + * @param integer $id the group name + * @return Group_Core the group object, or null if the name was invalid. + */ + static function lookup_by_name($name) { + return Identity::instance()->lookup_group_by_name($name); + } + + /** + * List the groups + * @param mixed options to apply to the selection of the user + * @return array the group list. + */ + static function groups($filter=array()) { + return Identity::instance()->list_groups($filter); + } + + /** + * Return the edit rules associated with an group. + * + * @return stdClass containing the rules + */ + static function get_edit_rules() { + return Identity::instance()->get_edit_rules("group"); + } +} diff --git a/modules/gallery/helpers/locales.php b/modules/gallery/helpers/locales.php index ab7f7526..faec7816 100644 --- a/modules/gallery/helpers/locales.php +++ b/modules/gallery/helpers/locales.php @@ -136,6 +136,23 @@ class locales_Core { return in_array($language, array("he", "fa", "ar")); } + static function set_request_locale() { + // 1. Check the session specific preference (cookie) + $locale = user::cookie_locale(); + // 2. Check the user's preference + if (!$locale) { + $locale = user::active()->locale; + } + // 3. Check the browser's / OS' preference + if (!$locale) { + $locale = self::locale_from_http_request(); + } + // If we have any preference, override the site's default locale + if ($locale) { + I18n::instance()->locale($locale); + } + } + /** * Returns the best match comparing the HTTP accept-language header * with the installed locales. diff --git a/modules/gallery/helpers/user.php b/modules/gallery/helpers/user.php new file mode 100644 index 00000000..e3494fdf --- /dev/null +++ b/modules/gallery/helpers/user.php @@ -0,0 +1,351 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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 users. + * + * Note: by design, this class does not do any permission checking. + */ +class user_Core { + static function get_edit_form($user) { + $writable = self::is_writable(); + $form = new Forge("users/update/$user->id", "", "post", array("id" => "g-edit-user-form")); + $form->set_attr("class", "g-narrow"); + $group = $form->group("edit_user")->label(t("Edit User: %name", array("name" => $user->name))); + $group->input("full_name")->label(t("Full Name"))->id("g-fullname")->value($user->full_name); + self::_add_locale_dropdown($group, $user); + if ($writable) { + $group->password("password")->label(t("Password"))->id("g-password"); + $group->password("password2")->label(t("Confirm Password"))->id("g-password2") + ->matches($group->password); + } + $group->input("email")->label(t("Email"))->id("g-email")->value($user->email); + $group->input("url")->label(t("URL"))->id("g-url")->value($user->url); + $form->add_rules_from(self::get_edit_rules()); + + module::event("user_edit_form", $user, $form); + $group->submit("")->value(t("Save")); + + if (!$writable) { + foreach ($group->inputs as $input) { + $input->disabled("disabled"); + } + } + return $form; + } + + static function get_edit_form_admin($user) { + $writable = self::is_writable(); + $form = new Forge( + "admin/users/edit_user/$user->id", "", "post", array("id" => "g-edit-user-form")); + $group = $form->group("edit_user")->label(t("Edit User")); + $group->input("name")->label(t("Username"))->id("g-username")->value($user->name); + $group->inputs["name"]->error_messages( + "in_use", t("There is already a user with that username")); + $group->input("full_name")->label(t("Full Name"))->id("g-fullname")->value($user->full_name); + self::_add_locale_dropdown($group, $user); + if ($writable) { + $group->password("password")->label(t("Password"))->id("g-password"); + $group->password("password2")->label(t("Confirm Password"))->id("g-password2") + ->matches($group->password); + } + $group->input("email")->label(t("Email"))->id("g-email")->value($user->email); + $group->input("url")->label(t("URL"))->id("g-url")->value($user->url); + $group->checkbox("admin")->label(t("Admin"))->id("g-admin")->checked($user->admin); + $form->add_rules_from(self::get_edit_rules()); + + module::event("user_edit_form_admin", $user, $form); + $group->submit("")->value(t("Modify User")); + if (!$writable) { + foreach ($group->inputs as $input) { + $input->disabled("disabled"); + } + } + return $form; + } + + static function get_add_form_admin() { + $form = new Forge("admin/users/add_user", "", "post", array("id" => "g-add-user-form")); + $form->set_attr('class', "g-narrow"); + $group = $form->group("add_user")->label(t("Add User")); + $group->input("name")->label(t("Username"))->id("g-username") + ->error_messages("in_use", t("There is already a user with that username")); + $group->input("full_name")->label(t("Full Name"))->id("g-fullname"); + $group->password("password")->label(t("Password"))->id("g-password"); + $group->password("password2")->label(t("Confirm Password"))->id("g-password2") + ->matches($group->password); + $group->input("email")->label(t("Email"))->id("g-email"); + $group->input("url")->label(t("URL"))->id("g-url"); + self::_add_locale_dropdown($group); + $group->checkbox("admin")->label(t("Admin"))->id("g-admin"); + $form->add_rules_from(self::get_edit_rules()); + + module::event("user_add_form_admin", $user, $form); + $group->submit("")->value(t("Add User")); + return $form; + } + + private static function _add_locale_dropdown(&$form, $user=null) { + $locales = locales::installed(); + foreach ($locales as $locale => $display_name) { + $locales[$locale] = SafeString::of_safe_html($display_name); + } + if (count($locales) > 1) { + // Put "none" at the first position in the array + $locales = array_merge(array("" => t("« none »")), $locales); + $selected_locale = ($user && $user->locale) ? $user->locale : ""; + $form->dropdown("locale") + ->label(t("Language Preference")) + ->options($locales) + ->selected($selected_locale); + } + } + + static function get_delete_form_admin($user) { + $form = new Forge("admin/users/delete_user/$user->id", "", "post", + array("id" => "g-delete-user-form")); + $group = $form->group("delete_user")->label( + t("Are you sure you want to delete user %name?", array("name" => $user->name))); + $group->submit("")->value(t("Delete user %name", array("name" => $user->name))); + return $form; + } + + static function get_login_form($url) { + $form = new Forge($url, "", "post", array("id" => "g-login-form")); + $form->set_attr('class', "g-narrow"); + $group = $form->group("login")->label(t("Login")); + $group->input("name")->label(t("Username"))->id("g-username")->class(null); + $group->password("password")->label(t("Password"))->id("g-password")->class(null); + $group->inputs["name"]->error_messages("invalid_login", t("Invalid name or password")); + $group->submit("")->value(t("Login")); + return $form; + } + + /** + * Return the active user. If there's no active user, return the guest user. + * + * @return User_Model + */ + static function active() { + // @todo (maybe) cache this object so we're not always doing session lookups. + $user = Session::instance()->get("user", null); + if (!isset($user)) { + // Don't do this as a fallback in the Session::get() call because it can trigger unnecessary + // work. + $user = self::guest(); + } + return $user; + } + + /** + * Change the active user. + * + * @return User_Model + */ + static function set_active($user) { + $session = Session::instance(); + $session->set("user", $user); + $session->delete("group_ids"); + self::load_user(); + } + + /** + * Return the array of group ids this user belongs to + * + * @return array + */ + static function group_ids() { + return Session::instance()->get("group_ids", array(1)); + } + + + static function cookie_locale() { + $cookie_data = Input::instance()->cookie("g_locale"); + $locale = null; + if ($cookie_data) { + if (preg_match("/^([a-z]{2,3}(?:_[A-Z]{2})?)$/", trim($cookie_data), $matches)) { + $requested_locale = $matches[1]; + $installed_locales = locales::installed(); + if (isset($installed_locales[$requested_locale])) { + $locale = $requested_locale; + } + } + } + return $locale; + } + + /** + * Make sure that we have a session and group_ids cached in the session. This is one + * of the first calls to reference the user so call the Identity::instance to load the + * driver classes. + */ + static function load_user() { + Identity::instance(); + $session = Session::instance(); + if (!($user = $session->get("user"))) { + $session->set("user", $user = self::guest()); + } + + // The installer cannot set a user into the session, so it just sets an id which we should + // upconvert into a user. + // @todo what is user id===2 + if ($user === 2) { + $user = model_cache::get("user", 2); + self::login($user); + $session->set("user", $user); + } + + if (!$session->get("group_ids")) { + $ids = array(); + foreach ($user->groups as $group) { + $ids[] = $group->id; + } + $session->set("group_ids", $ids); + } + } + + /** + * Log in as a given user. + * @param object $user the user object. + */ + static function login($user) { + // @todo make this an interface call + $user->login_count += 1; + $user->last_login = time(); + $user->save(); + + self::set_active($user); + module::event("user_login", $user); + } + + /** + * Log out the active user and destroy the session. + * @param object $user the user object. + */ + static function logout() { + $user = self::active(); + if (!$user->guest) { + try { + Session::instance()->destroy(); + } catch (Exception $e) { + Kohana::log("error", $e); + } + module::event("user_logout", $user); + } + } + + /** + * Determine if a feature is supported by the driver. + * + * @param string $feature the name of the feature to check + * @return boolean true if supported + */ + static function is_writable() { + return Identity::instance()->is_writable(); + } + + /** + * Return the guest user. + * + * @todo consider caching + * + * @return User_Model + */ + static function guest() { + return Identity::instance()->guest(); + } + + /** + * Create a new user. + * + * @param string $name + * @param string $full_name + * @param string $password + * @return User_Model + */ + static function create($name, $full_name, $password) { + return Identity::instance()->create_user($name, $full_name, $password); + } + + /** + * Is the password provided correct? + * + * @param user User Model + * @param string $password a plaintext password + * @return boolean true if the password is correct + */ + static function is_correct_password($user, $password) { + return Identity::instance()->is_correct_password($user, $password); + } + + /** + * Create the hashed passwords. + * @param string $password a plaintext password + * @return string hashed password + */ + static function hash_password($password) { + return Identity::instance()->hash_password($password); + } + + /** + * Look up a user by id. + * @param integer $id the user id + * @return User_Model the user object, or null if the id was invalid. + */ + static function lookup($id) { + return Identity::instance()->lookup_user($id); + } + + /** + * Look up a user by name. + * @param integer $id the user name + * @return User_Model the user object, or null if the name was invalid. + */ + static function lookup_by_name($name) { + return Identity::instance()->lookup_user_by_field("name", $name); + } + + /** + * Look up a user by hash. + * @param string $name the user name + * @return User_Model the user object, or null if the name was invalid. + */ + static function lookup_by_hash($hash) { + return Identity::instance()->lookup_user_by_field("hash", $hash); + } + + /** + * List the users + * @param mixed options to apply to the selection of the user(optional) + * @return array the group list. + */ + static function users($filter=array()) { + return Identity::instance()->list_users($filter); + } + + /** + * Return the edit rules associated with an user. + * + * @return stdClass containing the rules + */ + static function get_edit_rules() { + return Identity::instance()->get_edit_rules("user"); + } +}
\ No newline at end of file diff --git a/modules/gallery/libraries/Identity.php b/modules/gallery/libraries/Identity.php new file mode 100644 index 00000000..fec822db --- /dev/null +++ b/modules/gallery/libraries/Identity.php @@ -0,0 +1,241 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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. + */ + +/** + * Provides a driver-based interface for managing users and groups. + */ +class Identity_Core { + protected static $instances = array(); + + // Configuration + protected $config; + + // Driver object + protected $driver; + + /** + * Returns a singleton instance of Identity. + * + * @param string configuration + * @return Identity_Core + */ + static function & instance($config="default") { + if (!isset(Identity::$instances[$config])) { + // Create a new instance + Identity::$instances[$config] = new Identity($config); + } + + return Identity::$instances[$config]; + } + + /** + * Loads the configured driver and validates it. + * + * @param array|string custom configuration or config group name + * @return void + */ + public function __construct($config="default") { + if (is_string($config)) { + $name = $config; + + // Test the config group name + if (($config = Kohana::config("identity.".$config)) === NULL) { + throw new Exception("@todo NO USER LIBRARY CONFIGURATION FOR: $name"); + } + + if (is_array($config)) { + // Append the default configuration options + $config += Kohana::config("identity.default"); + } else { + // Load the default group + $config = Kohana::config("identity.default"); + } + + // Cache the config in the object + $this->config = $config; + + // Set driver name + $driver = "Identity_".ucfirst($this->config["driver"])."_Driver"; + + // Load the driver + if ( ! Kohana::auto_load($driver)) { + throw new Kohana_Exception("core.driver_not_found", $this->config["driver"], + get_class($this)); + } + + // Initialize the driver + $this->driver = new $driver($this->config["params"]); + + // Validate the driver + if ( !($this->driver instanceof Identity_Driver)) + throw new Kohana_Exception("core.driver_implements", $this->config["driver"], + get_class($this), "Identity_Driver"); + + Kohana::log("debug", "Identity Library initialized"); + } + } + + /** + * Determine if a feature is supported by the driver. + * + * @param string $feature the name of the feature to check + * @return boolean true if supported + */ + public function is_writable() { + return !empty($this->config["allow_updates"]); + } + + + /** + * Return the guest user. + * + * @todo consider caching + * + * @return Identity_Model + */ + public function guest() { + return $this->driver->guest(); + } + + /** + * Create a new user. + * + * @param string $name + * @param string $full_name + * @param string $password + * @return Identity_Model + */ + public function create_user($name, $full_name, $password) { + return $this->driver->create_user($name, $full_name, $password); + } + + /** + * Is the password provided correct? + * + * @param user Identity Model + * @param string $password a plaintext password + * @return boolean true if the password is correct + */ + public function is_correct_password($user, $password) { + return $this->driver->is_correct_password($user, $password); + } + + /** + * Create the hashed passwords. + * @param string $password a plaintext password + * @return string hashed password + */ + public function hash_password($password) { + return $this->driver->hash_password($password); + } + + /** + * Look up a user by id. + * @param integer $id the user id + * @return Identity_Model the user object, or null if the id was invalid. + */ + public function lookup_user($id) { + return $this->driver->lookup_user($id); + } + + /** + * Look up a user by field value. + * @param string search field + * @param string search value + * @return Identity_Model the user object, or null if the name was invalid. + */ + public function lookup_user_by_field($field_name, $value) { + return $this->driver->lookup_user_by_field($field_name, $value); + } + + /** + * Create a new group. + * + * @param string $name + * @return Group_Model + */ + public function create_group($name) { + return $this->driver->create_group($name); + } + + /** + * The group of all possible visitors. This includes the guest user. + * + * @return Group_Model + */ + public function everybody() { + return $this->driver->everybody(); + } + + /** + * The group of all logged-in visitors. This does not include guest users. + * + * @return Group_Model + */ + public function registered_users() { + return $this->driver->everybody(); + } + + /** + * Look up a group by id. + * @param integer $id the user id + * @return Group_Model the group object, or null if the id was invalid. + */ + public function lookup_group($id) { + return $this->driver->lookup_group($id); + } + + /** + * Look up a group by name. + * @param integer $id the group name + * @return Group_Model the group object, or null if the name was invalid. + */ + public function lookup_group_by_name($name) { + return $this->driver->lookup_group_by_name($name); + } + + /** + * List the users + * @param mixed options to apply to the selection of the user + * @return array the group list. + */ + public function list_users($filter=array()) { + return $this->driver->list_users($filter); + } + + /** + * List the groups + * @param mixed options to apply to the selection of the user + * @return array the group list. + */ + public function list_groups($filter=array()) { + return $this->driver->list_groups($filter); + } + + /** + * Return the edit rules associated with an group. + * + * @param string $object_type to return rules for ("user"|"group") + * @return stdClass containing the rules + */ + public function get_edit_rules($object_type) { + return $this->driver->get_edit_rules($object_type); + } +} // End Identity diff --git a/modules/gallery/libraries/drivers/Identity.php b/modules/gallery/libraries/drivers/Identity.php new file mode 100644 index 00000000..a92958c7 --- /dev/null +++ b/modules/gallery/libraries/drivers/Identity.php @@ -0,0 +1,306 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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. + */ +interface Identity_Driver { + /** + * Return the guest user. + * + * @todo consider caching + * + * @return User_Model + */ + public function guest(); + + /** + * Create a new user. + * + * @param string $name + * @param string $full_name + * @param string $password + * @return User_Core + */ + public function create_user($name, $full_name, $password); + + /** + * Is the password provided correct? + * + * @param user User Model + * @param string $password a plaintext password + * @return boolean true if the password is correct + */ + public function is_correct_password($user, $password); + + /** + * Create the hashed passwords. + * @param string $password a plaintext password + * @return string hashed password + */ + public function hash_password($password); + /** + * Look up a user by id. + * @param integer $id the user id + * @return User_Core the user object, or null if the id was invalid. + */ + public function lookup_user($id); + + /** + * Look up a user by name. + * @param string search field + * @param string search value + * @return User_Core the user object, or null if the name was invalid. + */ + public function lookup_user_by_field($field, $value); + + /** + * Create a new group. + * + * @param string $name + * @return Group_Model + */ + public function create_group($name); + + /** + * The group of all possible visitors. This includes the guest user. + * + * @return Group_Model + */ + public function everybody(); + + /** + * The group of all logged-in visitors. This does not include guest users. + * + * @return Group_Model + */ + public function registered_users(); + + /** + * Look up a group by id. + * @param integer $id the user id + * @return Group_Model the group object, or null if the id was invalid. + */ + public function lookup_group($id); + + /** + * Look up a group by name. + * @param integer $id the group name + * @return Group_Model the group object, or null if the name was invalid. + */ + public function lookup_group_by_name($name); + + /** + * List the users + * @param mixed options to apply to the selection of the user + * @todo Do a longer write up on format of filters (@see Database.php) + * @return array the group list. + */ + public function list_users($filter=array()); + + /** + * List the groups + * @param mixed options to apply to the selection of the user + * @todo Do a longer write up on format of filters (@see Database.php) + * @return array the group list. + */ + public function list_groups($filter=array()); + + /** + * Return the edit rules associated with an group. + * + * @param string $object_type to return rules for ("user"|"group") + * @return stdClass containing the rules + */ + public function get_edit_rules($object_type); +} // End Identity Driver Definition + +/** + * User Data wrapper + */ +abstract class User_Definition { + protected $user; + public function __get($column) { + switch ($column) { + case "id": + case "name": + case "full_name": + case "password": + case "login_count": + case "last_login": + case "email": + case "admin": + case "guest": + case "hash": + case "url": + case "locale": + case "groups": + return $this->user->$column; + case "hashed_password": + throw new Exception("@todo WRITE ONLY FIELD: $column"); + break; + default: + throw new Exception("@todo UNSUPPORTED FIELD: $column"); + break; + } + } + + public function __set($column, $value) { + switch ($column) { + case "id": + case "groups": + throw new Exception("@todo READ ONLY FIELD: $column"); + break; + case "name": + case "full_name": + case "hashed_password": + case "password": + case "login_count": + case "last_login": + case "email": + case "admin": + case "guest": + case "hash": + case "url": + case "locale": + $this->user->$column = $value; + break; + default: + throw new Exception("@todo UNSUPPORTED FIELD: $column"); + break; + } + } + + public function __isset($column) { + return isset($this->user->$column); + } + + public function __unset($column) { + switch ($column) { + case "id": + case "groups": + throw new Exception("@todo READ ONLY FIELD: $column"); + break; + case "name": + case "full_name": + case "password": + case "login_count": + case "last_login": + case "email": + case "admin": + case "guest": + case "hash": + case "url": + case "locale": + unset($this->user->$column); + break; + case "hashed_password": + throw new Exception("@todo WRITE ONLY FIELD: $column"); + default: + throw new Exception("@todo UNSUPPORTED FIELD: $column"); + break; + } + } + + /** + * Return a url to the user's avatar image. + * @param integer $size the target size of the image (default 80px) + * @return string a url + */ + public function avatar_url($size=80, $default=null) { + return sprintf("http://www.gravatar.com/avatar/%s.jpg?s=%d&r=pg%s", + md5($this->user->email), $size, $default ? "&d=" . urlencode($default) : ""); + } + + /** + * Return the best version of the user's name. Either their specified full name, or fall back + * to the user name. + * @return string + */ + public function display_name() { + return empty($this->user->full_name) ? $this->user->name : $this->user->full_name; + } + + public function uncloaked() { + return $this->user; + } + + abstract public function save(); + abstract public function delete(); +} + +/** + * Group Data wrapper + */ +abstract class Group_Definition { + protected $group; + + public function __get($column) { + switch ($column) { + case "id": + case "name": + case "special": + case "users": + return $this->group->$column; + default: + throw new Exception("@todo UNSUPPORTED FIELD: $column"); + break; + } + } + + public function __set($column, $value) { + switch ($column) { + case "id": + case "users": + throw new Exception("@todo READ ONLY FIELD: $column"); + break; + case "name": + case "special": + $this->group->$column = $value; + default: + throw new Exception("@todo UNSUPPORTED FIELD: $column"); + break; + } + } + + public function __isset($column) { + return isset($this->group->$column); + } + + public function __unset($column) { + switch ($column) { + case "id": + case "users": + throw new Exception("@todo READ ONLY FIELD: $column"); + break; + case "name": + case "special": + unset($this->group->$column); + default: + throw new Exception("@todo UNSUPPORTED FIELD: $column"); + break; + } + } + + public function uncloaked() { + return $this->group; + } + + abstract public function save(); + abstract public function delete(); + abstract public function add($user); + abstract public function remove($user); +} diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 246d5fcd..5d356841 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -332,7 +332,7 @@ class Item_Model extends ORM_MPTT { // This relationship depends on an outside module, which may not be present so handle // failures gracefully. try { - return model_cache::get("user", $this->owner_id); + return user::lookup($this->owner_id); } catch (Exception $e) { return null; } diff --git a/modules/gallery/models/log.php b/modules/gallery/models/log.php index 6734afb8..d143d7bd 100644 --- a/modules/gallery/models/log.php +++ b/modules/gallery/models/log.php @@ -18,5 +18,20 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Log_Model extends ORM { - protected $has_one = array("user"); + /** + * @see ORM::__get() + */ + public function __get($column) { + if ($column == "user") { + // This relationship depends on an outside module, which may not be present so handle + // failures gracefully. + try { + return user::lookup($this->user_id); + } catch (Exception $e) { + return null; + } + } else { + return parent::__get($column); + } + } } diff --git a/modules/gallery/views/admin_users.html.php b/modules/gallery/views/admin_users.html.php new file mode 100644 index 00000000..82d0926c --- /dev/null +++ b/modules/gallery/views/admin_users.html.php @@ -0,0 +1,138 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<script type="text/javascript"> + var add_user_to_group_url = "<?= url::site("admin/users/add_user_to_group/__USERID__/__GROUPID__?csrf=$csrf") ?>"; + $(document).ready(function(){ + $("#g-user-admin-list .g-draggable").draggable({ + helper: "clone" + }); + $("#g-group-admin .g-group").droppable({ + accept: ".core-info", + hoverClass: "g-selected", + drop: function(ev, ui) { + var user_id = $(ui.draggable).attr("id").replace("user-", ""); + var group_id = $(this).attr("id").replace("group-", ""); + $.get(add_user_to_group_url.replace("__USERID__", user_id).replace("__GROUPID__", group_id), + {}, + function() { + reload_group(group_id); + }); + } + }); + $("#group-1").droppable("destroy"); + $("#group-2").droppable("destroy"); + $(".g-group-disable").droppable("destroy"); + }); + + var reload_group = function(group_id) { + var reload_group_url = "<?= url::site("admin/users/group/__GROUPID__") ?>"; + $.get(reload_group_url.replace("__GROUPID__", group_id), + {}, + function(data) { + $("#group-" + group_id).html(data); + $("#group-" + group_id + " .g-dialog-link").gallery_dialog(); + }); + } + + var remove_user = function(user_id, group_id) { + var remove_user_url = "<?= url::site("admin/users/remove_user_from_group/__USERID__/__GROUPID__?csrf=$csrf") ?>"; + $.get(remove_user_url.replace("__USERID__", user_id).replace("__GROUPID__", group_id), + {}, + function() { + reload_group(group_id); + }); + } +</script> +<div class="g-block"> + <? if (!empty($writable)): ?> + <a href="<?= url::site("admin/users/add_user_form") ?>" + class="g-dialog-link g-button g-right ui-icon-left ui-state-default ui-corner-all" + title="<?= t("Create a new user")->for_html_attr() ?>"> + <span class="ui-icon ui-icon-circle-plus"></span> + <?= t("Add a new user") ?> + </a> + <? endif ?> + + <h2> + <?= t("User Admin") ?> + </h2> + + <div class="g-block-content"> + <table id="g-user-admin-list"> + <tr> + <th><?= t("Username") ?></th> + <th><?= t("Full name") ?></th> + <th><?= t("Email") ?></th> + <th><?= t("Last login") ?></th> + <th><?= t("Actions") ?></th> + </tr> + + <? foreach ($users as $i => $user): ?> + <tr id="g-user-<?= $user->id ?>" class="<?= text::alternate("g-odd", "g-even") ?> user <?= $user->admin ? "admin" : "" ?>"> + <td id="user-<?= $user->id ?>" class="core-info <?= !empty($writable) ? "g-draggable" : "" ?> "> + <img src="<?= $user->avatar_url(20, $theme->url("images/avatar.jpg", true)) ?>" + title="<?= t("Drag user onto group below to add as a new member")->for_html_attr() ?>" + alt="<?= html::clean_attribute($user->name) ?>" + width="20" + height="20" /> + <?= html::clean($user->name) ?> + </td> + <td> + <?= html::clean($user->full_name) ?> + </td> + <td> + <?= html::clean($user->email) ?> + </td> + <td> + <?= ($user->last_login == 0) ? "" : gallery::date($user->last_login) ?> + </td> + <td class="g-actions"> + <a href="<?= url::site("admin/users/edit_user_form/$user->id") ?>" + open_text="<?= t("close") ?>" + class="g-panel-link g-button ui-state-default ui-corner-all ui-icon-left"> + <span class="ui-icon ui-icon-pencil"></span><span class="g-button-text"> + <?= (!empty($writable)) ? t("edit") : t("display") ?> + </span></a> + <? if (!empty($writable)): ?> + <? if (user::active()->id != $user->id && !$user->guest): ?> + <a href="<?= url::site("admin/users/delete_user_form/$user->id") ?>" + class="g-dialog-link g-button ui-state-default ui-corner-all ui-icon-left"> + <span class="ui-icon ui-icon-trash"></span><?= t("delete") ?></a> + <? else: ?> + <span title="<?= t("This user cannot be deleted")->for_html_attr() ?>" + class="g-button ui-state-disabled ui-corner-all ui-icon-left"> + <span class="ui-icon ui-icon-trash"></span><?= t("delete") ?></span> + <? endif ?> + <? endif ?> + </td> + </tr> + <? endforeach ?> + </table> + </div> +</div> + +<div id="g-group-admin" class="g-block g-clearfix"> + <? if (!empty($writable)): ?> + <a href="<?= url::site("admin/users/add_group_form") ?>" + class="g-dialog-link g-button g-right ui-icon-left ui-state-default ui-corner-all" + title="<?= t("Create a new group")->for_html_attr() ?>"> + <span class="ui-icon ui-icon-circle-plus"></span> + <?= t("Add a new group") ?> + </a> + <? endif ?> + + <h2> + <?= t("Group Admin") ?> + </h2> + + <div class="g-block-content"> + <ul> + <? foreach ($groups as $i => $group): ?> + <? $class = !empty($writable) ? "" : "g-group-disable" ?> + <li id="group-<?= $group->id ?>" class="g-group <?= $class ?> <?= ($group->special ? "g-default-group" : "") ?>" /> + <? $v = new View("admin_users_group.html"); $v->group = $group; $v->writable = !empty($writable) ?> + <?= $v ?> + </li> + <? endforeach ?> + </ul> + </div> +</div> diff --git a/modules/gallery/views/admin_users_group.html.php b/modules/gallery/views/admin_users_group.html.php new file mode 100644 index 00000000..539f69b7 --- /dev/null +++ b/modules/gallery/views/admin_users_group.html.php @@ -0,0 +1,38 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<h4> + <?= html::clean($group->name) ?> + <? if (!$group->special): ?> + <a href="<?= url::site("admin/users/delete_group_form/$group->id") ?>" + title="<?= t("Delete the %name group", array("name" => $group->name))->for_html_attr() ?>" + class="g-dialog-link g-button ui-state-default ui-corner-all <?= !empty($writable) ? "" : "ui-state-disabled" ?>"> + <span class="ui-icon ui-icon-trash"><?= t("delete") ?></span></a> + <? else: ?> + <a title="<?= t("This default group cannot be deleted")->for_html_attr() ?>" + class="g-dialog-link g-button ui-state-disabled ui-corner-all ui-icon-left"> + <span class="ui-icon ui-icon-trash"><?= t("delete") ?></span></a> + <? endif ?> +</h4> + +<? if ($group->users->count() > 0): ?> +<ul> + <? foreach ($group->users as $i => $user): ?> + <li class="g-user"> + <?= html::clean($user->name) ?> + <? if (!$group->special): ?> + <a href="javascript:remove_user(<?= $user->id ?>, <?= $group->id ?>)" + class="g-button ui-state-default ui-corner-all ui-icon-left <?= !empty($writable) ? "" : "ui-state-disabled" ?>" + title="<?= t("Remove %user from %group group", + array("user" => $user->name, "group" => $group->name))->for_html_attr() ?>"> + <span class="ui-icon ui-icon-closethick"><?= t("remove") ?></span> + </a> + <? endif ?> + </li> + <? endforeach ?> +</ul> +<? else: ?> +<div> + <p> + <?= t("Drag & drop users from the User Admin above into this group box to add group members.") ?> + </p> +</div> +<? endif ?> diff --git a/modules/gallery/views/login.html.php b/modules/gallery/views/login.html.php new file mode 100644 index 00000000..fa6308ae --- /dev/null +++ b/modules/gallery/views/login.html.php @@ -0,0 +1,22 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<ul id="g-login-menu"> + <? if ($user->guest): ?> + <li class="first"> + <a href="<?= url::site("login/ajax") ?>" + title="<?= t("Login to Gallery")->for_html_attr() ?>" + id="g-login-link"><?= t("Login") ?></a> + </li> + <? else: ?> + <li class="first"> + <?= t('Logged in as %name', array('name' => html::mark_clean( + '<a href="' . url::site("form/edit/users/{$user->id}") . + '" title="' . ($writable ? t("Edit Your Profile")->for_html_attr() : t("Display Your Profile")->for_html_attr()) . + '" id="g-user-profile-link" class="g-dialog-link">' . + html::clean($user->display_name()) . '</a>'))) ?> + </li> + <li> + <a href="<?= url::site("logout?csrf=$csrf&continue=" . urlencode(url::current(true))) ?>" + id="g-logout-link"><?= t("Logout") ?></a> + </li> + <? endif ?> +</ul> diff --git a/modules/gallery/views/login_ajax.html.php b/modules/gallery/views/login_ajax.html.php new file mode 100644 index 00000000..d3364b46 --- /dev/null +++ b/modules/gallery/views/login_ajax.html.php @@ -0,0 +1,43 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<script type="text/javascript"> + $("#g-login-form").ready(function() { + $("#g-password-reset").click(function() { + $.ajax({ + url: "<?= url::site("password/reset") ?>", + success: function(data) { + $("#g-login").html(data); + $("#ui-dialog-title-g-dialog").html(<?= t("Reset Password")->for_js() ?>); + $(".submit").addClass("g-button ui-state-default ui-corner-all"); + $(".submit").gallery_hover_init(); + ajaxify_login_reset_form(); + } + }); + }); + }); + + function ajaxify_login_reset_form() { + $("#g-login form").ajaxForm({ + dataType: "json", + success: function(data) { + if (data.form) { + $("#g-login form").replaceWith(data.form); + ajaxify_login_reset_form(); + } + if (data.result == "success") { + $("#g-dialog").dialog("close"); + window.location.reload(); + } + } + }); + }; +</script> +<div id="g-login"> + <ul> + <li id="g-login-form"> + <?= $form ?> + </li> + <li> + <a href="#" id="g-password-reset" class="g-right g-txt-small"><?= t("Forgot Your Password?") ?></a> + </li> + </ul> +</div> diff --git a/modules/gallery/views/reset_password.html.php b/modules/gallery/views/reset_password.html.php new file mode 100644 index 00000000..92ca4917 --- /dev/null +++ b/modules/gallery/views/reset_password.html.php @@ -0,0 +1,17 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<html> + <head> + <title><?= t("Password Reset Request") ?> </title> + </head> + <body> + <h2><?= t("Password Reset Request") ?> </h2> + <p> + <?= t("Hello, %name,", array("name" => $user->full_name ? $user->full_name : $user->name)) ?> + </p> + <p> + <?= t("We received a request to reset your password for <a href=\"%site_url\">%site_url</a>. If you made this request, you can confirm it by <a href=\"%confirm_url\">clicking this link</a>. If you didn't request this password reset, it's ok to ignore this mail.", + array("site_url" => html::mark_clean(url::base(false, "http")), + "confirm_url" => $confirm_url)) ?> + </p> + </body> +</html> diff --git a/modules/gallery/views/user_languages_block.html.php b/modules/gallery/views/user_languages_block.html.php new file mode 100644 index 00000000..89185967 --- /dev/null +++ b/modules/gallery/views/user_languages_block.html.php @@ -0,0 +1,19 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<?= form::dropdown("g-select-session-locale", $installed_locales, $selected) ?> +<script type="text/javascript"> + $("#g-select-session-locale").change(function() { + var old_locale_preference = <?= html::js_string($selected) ?>; + var locale = $(this).val(); + if (old_locale_preference == locale) { + return; + } + + var expires = -1; + if (locale) { + expires = 365; + } + $.cookie("g_locale", locale, {"expires": expires, "path": "/"}); + window.location.reload(true); + }); +</script> + |