1
0

Initial commit. There is probably many bugs, but it seems to work...

This commit is contained in:
Romain LE DISEZ 2010-11-28 07:25:26 +08:00 committed by Bharat Mediratta
parent db479a61e2
commit d84ea788b2
9 changed files with 421 additions and 0 deletions

View File

@ -0,0 +1,61 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 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 access extends access_Core {
/**
* Does the active user have this permission on this item?
*
* @param string $perm_name
* @param Item_Model $item
* @return boolean
*/
static function can($perm_name, $item) {
return self::user_can(identity::active_user(), $perm_name, $item);
}
/**
* If the active user does not have this permission, failed with an access::forbidden().
*
* @param string $perm_name
* @param Item_Model $item
* @return boolean
*/
static function required($perm_name, $item) {
if (!self::can($perm_name, $item)) {
if ($perm_name == "view") {
// Treat as if the item didn't exist, don't leak any information.
throw new Kohana_404_Exception();
} else {
self::forbidden();
}
}
}
static function user_can($user, $perm_name, $item) {
if( $user->id == identity::active_user()->id && user_chroot::album() ) {
if( $item->left_ptr < user_chroot::album()->left_ptr || user_chroot::album()->right_ptr < $item->right_ptr ) {
return false;
}
}
return parent::user_can($user, $perm_name, $item);
}
}

View File

@ -0,0 +1,34 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 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 extends item_Core {
static function viewable($model) {
$model = parent::viewable($model);
if( user_chroot::album() ) {
$model->and_open()
->and_where("items.left_ptr", ">=", user_chroot::album()->left_ptr)
->and_where("items.right_ptr", "<=", user_chroot::album()->right_ptr)
->close();
}
return $model;
}
}

View File

@ -0,0 +1,47 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 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.
*/
// /!\ Hack: There is no good way to extend gallery url
$gallery_url = file_get_contents(MODPATH . 'gallery/helpers/MY_url.php');
$gallery_url = preg_replace('#^<\?php #', '', $gallery_url);
$gallery_url = preg_replace('#class url extends url_Core#', 'class url_G3 extends url_Core', $gallery_url);
eval($gallery_url);
class url extends url_G3 {
static function parse_url() {
if( user_chroot::album() ) {
if( Router::$current_uri == '' ) {
Router::$controller = false;
}
Router::$current_uri = trim(user_chroot::album()->relative_url().'/'.Router::$current_uri, '/');
}
return parent::parse_url();
}
static function site($uri = '', $protocol = FALSE) {
if( user_chroot::album() ) {
$uri = preg_replace('#^'.user_chroot::album()->relative_url().'#', '', $uri);
}
return parent::site($uri, $protocol);
}
}

View File

@ -0,0 +1,41 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 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 user_chroot_Core {
private static $_album = null;
public static function album() {
if( is_null(self::$_album) ) {
self::$_album = false;
$user = identity::active_user();
$user_chroot = ORM::factory("user_chroot", $user->id);
if( $user_chroot->loaded() && $user_chroot->album_id != 0 ) {
$item = ORM::factory("item", $user_chroot->album_id);
if( $item->loaded() ) {
self::$_album = $item;
}
}
}
return self::$_album;
}
}

View File

@ -0,0 +1,112 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 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 user_chroot_event_Core {
/**
* Called just before a user is deleted. This will remove the user from
* the user_chroot directory.
*/
static function user_before_delete($user) {
ORM::factory("user_chroot")->delete($user->id);
}
/**
* Called when admin is adding a user
*/
static function user_add_form_admin($user, $form) {
$form->add_user->dropdown("user_chroot")
->label(t("Root Album"))
->options(self::createGalleryArray())
->selected(0);
}
/**
* Called after a user has been added
*/
static function user_add_form_admin_completed($user, $form) {
$user_chroot = ORM::factory("user_chroot")->where("id", "=", $user->id)->find();
$user_chroot->id = $user->id;
$user_chroot->album_id = $form->add_user->user_chroot->value;
$user_chroot->save();
}
/**
* Called when admin is editing a user
*/
static function user_edit_form_admin($user, $form) {
$user_chroot = ORM::factory("user_chroot")->where("id", "=", $user->id)->find();
if ($user_chroot->loaded()) {
$selected = $user_chroot->album_id;
} else {
$selected = 0;
}
$form->edit_user->dropdown("user_chroot")
->label(t("Root Album"))
->options(self::createGalleryArray())
->selected($selected);
}
/**
* Called after a user had been edited by the admin
*/
static function user_edit_form_admin_completed($user, $form) {
$user_chroot = ORM::factory("user_chroot")->where("id", "=", $user->id)->find();
if ($user_chroot->loaded()) {
$user_chroot->album_id = $form->edit_user->user_chroot->value;
} else {
$user_chroot->id = $user->id;
$user_chroot->album_id = $form->edit_user->user_chroot->value;
}
$user_chroot->save();
}
/**
* Creates an array of galleries
*/
static function createGalleryArray() {
$array[0] = "none";
$root = ORM::factory("item", 1);
self::tree($root, "", $array);
return $array;
}
/**
* recursive function to build array for drop down list
*/
static function tree($parent, $dashes, &$array) {
if ($parent->id == "1") {
$array[$parent->id] = ORM::factory("item", 1)->title;
} else {
$array[$parent->id] = "$dashes $parent->name";
}
$albums = ORM::factory("item")
->where("parent_id", "=", $parent->id)
->where("type", "=", "album")
->order_by("title", "ASC")
->find_all();
foreach ($albums as $album) {
self::tree($album, "-$dashes", $array);
}
return;
}
}

View File

@ -0,0 +1,40 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 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 user_chroot_installer {
static function install() {
$db = Database::instance();
$db->query("CREATE TABLE IF NOT EXISTS {user_chroots} (
`id` int(9) NOT NULL,
`album_id` int(9) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY(`id`))
DEFAULT CHARSET=utf8;");
module::set_version("user_chroot", 1);
}
/**
* Drops the table of user chroot when the module is uninstalled.
*/
static function uninstall() {
$db = Database::instance();
$db->query("DROP TABLE IF EXISTS {user_chroots};");
}
}

View File

@ -0,0 +1,61 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 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 ORM_MPTT extends ORM_MPTT_Core {
private $model_name = null;
function __construct($id=null) {
parent::__construct($id);
$this->model_name = inflector::singular($this->table_name);
}
/**
* Return the parent of this node
*
* @return ORM
*/
/*function parent() {
if( user_chroot::album() && user_chroot::album()->id == $this->id ) {
return null;
} else {
return parent::parent();
}
}*/
/**
* Return all the parents of this node, in order from root to this node's immediate parent.
*
* @return array ORM
*/
function parents() {
$select = $this
->where("left_ptr", "<=", $this->left_ptr)
->where("right_ptr", ">=", $this->right_ptr)
->where("id", "<>", $this->id)
->order_by("left_ptr", "ASC");
if( user_chroot::album() ) {
$select->where("left_ptr", ">=", user_chroot::album()->left_ptr);
$select->where("right_ptr", "<=", user_chroot::album()->right_ptr);
}
return $select->find_all();
}
}

View File

@ -0,0 +1,22 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 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 User_chroot_Model extends ORM {
}

View File

@ -0,0 +1,3 @@
name = "Chroot Users"
description = "Restrict access to one album."
version = 1