Permalink
Browse files

Merge branch 'master' of git://github.com/gallery/gallery3-contrib

  • Loading branch information...
2 parents a541f95 + 442716a commit ae8d635d267df94ca7b883c51041b9f9a8031c4a @ckieffer ckieffer committed Feb 16, 2011
Showing 571 changed files with 906 additions and 270 deletions.
@@ -49,9 +49,12 @@ public function remove($id) {
access::required("view", $item);
access::required("edit", $item);
- // Check for and delete the password.
- $existing_password = ORM::factory("items_albumpassword")->where("album_id", "=", $id)->find();
- if ($existing_password->loaded()) {
+ // Check for and delete the password and any cached ids assigned to it.
+ $existing_password = ORM::factory("items_albumpassword")->where("album_id", "=", $id)->find_all();
+ if (count($existing_password) > 0) {
+ foreach ($existing_password as $one_password) {
+ db::build()->delete("albumpassword_idcaches")->where("password_id", "=", $one_password->id)->execute();
+ }
db::build()->delete("items_albumpasswords")->where("album_id", "=", $id)->execute();
message::success(t("Password Removed."));
}
@@ -70,9 +73,12 @@ public function savepassword() {
$album_id = Input::instance()->post("item_id");
$album_password = Input::instance()->post("assignpassword_password");
- // Check for, and remove, any existing passwords.
- $existing_password = ORM::factory("items_albumpassword")->where("album_id", "=", $album_id)->find();
- if ($existing_password->loaded()) {
+ // Check for, and remove, any existing passwords and cached ids.
+ $existing_password = ORM::factory("items_albumpassword")->where("album_id", "=", $album_id)->find_all();
+ if (count($existing_password) > 0) {
+ foreach ($existing_password as $one_password) {
+ db::build()->delete("albumpassword_idcaches")->where("password_id", "=", $one_password->id)->execute();
+ }
db::build()->delete("items_albumpasswords")->where("album_id", "=", $album_id)->execute();
}
@@ -82,6 +88,25 @@ public function savepassword() {
$new_password->password = $album_password;
$new_password->save();
+ // Add the album to the id cache.
+ $cached_album = ORM::factory("albumpassword_idcache");
+ $cached_album->password_id = $new_password->id;
+ $cached_album->item_id = $album_id;
+ $cached_album->save();
+
+ // Check for any sub-items within the album, add all of them to the id cache.
+ $items = ORM::factory("item", $album_id)
+ ->viewable()
+ ->descendants();
+ if (count($items) > 0) {
+ foreach ($items as $one_item) {
+ $cached_item = ORM::factory("albumpassword_idcache");
+ $cached_item->password_id = $new_password->id;
+ $cached_item->item_id = $one_item->id;
+ $cached_item->save();
+ }
+ }
+
// Display a success message and close the dialog.
message::success(t("Password saved."));
print "<html>\n<body>\n<script type=\"text/javascript\">\n$(\"#g-dialog\").dialog(\"close\");\nwindow.location.reload();\n</script>\n</body>\n</html>\n";
@@ -21,38 +21,29 @@
class access extends access_Core {
static function required($perm_name, $item) {
// Original code from the required function in modules/gallery/helpers/access.php.
- if (!self::can($perm_name, $item)) {
+ if (!access::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();
+ access::forbidden();
}
// Begin rWatcher modifications.
// Throw a 404 error when a user attempts to access a protected item,
- // unless the password has been provided, or the user is the item's owner.
+ // unless the password has been provided, or the user is the item's owner.
} elseif (module::get_var("albumpassword", "hideonly") == false) {
- $album_item = "";
- do {
- if ($album_item == "") {
- if ($item->is_album()) {
- $album_item = $item;
- } else {
- $album_item = $item->parent();
- }
- } else {
- $album_item = $album_item->parent();
- }
-
- $existing_password = ORM::factory("items_albumpassword")->where("album_id", "=", $album_item->id)->find();
+ $item_protected = ORM::factory("albumpassword_idcache")->where("item_id", "=", $item->id)->order_by("cache_id")->find_all();
+ if (count($item_protected) > 0) {
+ $existing_password = ORM::factory("items_albumpassword")->where("id", "=", $item_protected[0]->password_id)->find();
if ($existing_password->loaded()) {
if ((cookie::get("g3_albumpassword") != $existing_password->password) &&
- (identity::active_user()->id != $album_item->owner_id)) {
+ (identity::active_user()->id != $item->owner_id) &&
+ (!identity::active_user()->admin)) {
throw new Kohana_404_Exception();
}
}
- } while ($album_item->parent_id > 0);
+ }
}
}
}
@@ -29,10 +29,21 @@ static function viewable($model) {
// If not, hide whatever is restricted by an album password
// that the current user is not the owner of.
if (!identity::active_user()->admin) {
- $model->and_open()->join("items_albumpasswords", "items.id", "items_albumpasswords.album_id", "LEFT OUTER")
- ->and_where("items_albumpasswords.album_id", "IS", NULL)
- ->or_where("items_albumpasswords.password", "=", cookie::get("g3_albumpassword"))
- ->or_where("items.owner_id", "=", identity::active_user()->id)->close();
+
+ // Display items that are not in idcaches.
+ $model->and_open()->join("albumpassword_idcaches", "items.id", "albumpassword_idcaches.item_id", "LEFT OUTER")
+ ->and_where("albumpassword_idcaches.item_id", "IS", NULL);
+
+ // ... Unless their password id corresponds with a valid password.
+ $existing_password = ORM::factory("items_albumpassword")->where("password", "=", cookie::get("g3_albumpassword"))->find_all();
+ if (count($existing_password) > 0) {
+ foreach ($existing_password as $one_password) {
+ $model->or_where("albumpassword_idcaches.password_id", "=", $one_password->id);
+ }
+ }
+
+ // Or the current user is the owner of the item.
+ $model->or_where("items.owner_id", "=", identity::active_user()->id)->close();
}
return $model;
@@ -81,27 +81,64 @@ static function site_menu($menu, $theme) {
->css_id("g-album-password-remove")
->url(url::site("albumpassword/remove/" . $item->id)));
} elseif ($item->id != 1) {
- $menu->get("options_menu")
- ->append(Menu::factory("dialog")
- ->id("albumpassword_assign")
- ->label(t("Assign password"))
- ->css_id("g-album-password-assign")
- ->url(url::site("albumpassword/assign/" . $item->id)));
+ $passworded_subitems = ORM::factory("item", $item->id)
+ ->and_open()->join("albumpassword_idcaches", "items.id", "albumpassword_idcaches.item_id", "LEFT OUTER")
+ ->where("albumpassword_idcaches.item_id", "IS NOT", NULL)->close()
+ ->descendants();
+
+ $existing_cacheditem = ORM::factory("albumpassword_idcache")->where("item_id", "=", $item->id)->order_by("cache_id")->find_all();
+ if ((count($existing_cacheditem) == 0) && count($passworded_subitems) == 0) {
+ $menu->get("options_menu")
+ ->append(Menu::factory("dialog")
+ ->id("albumpassword_assign")
+ ->label(t("Assign password"))
+ ->css_id("g-album-password-assign")
+ ->url(url::site("albumpassword/assign/" . $item->id)));
+ }
}
}
}
}
static function item_deleted($item) {
- // If an album is deleted, remove any associated passwords.
- $existingPasswords = ORM::factory("items_albumpassword")
- ->where("album_id", "=", $item->id)
- ->find_all();
- if (count($existingPasswords) > 0) {
- db::build()->delete("items_albumpassword")->where("album_id", "=", $item->id)->execute();
+ // Check for and delete the password and any cached ids assigned to it.
+ $existing_password = ORM::factory("items_albumpassword")->where("album_id", "=", $item->id)->find_all();
+ if (count($existing_password) > 0) {
+ foreach ($existing_password as $one_password) {
+ db::build()->delete("albumpassword_idcaches")->where("password_id", "=", $one_password->id)->execute();
+ }
+ db::build()->delete("items_albumpasswords")->where("album_id", "=", $item->id)->execute();
+ message::success(t("Password Removed."));
+ } else {
+ db::build()->delete("albumpassword_idcaches")->where("item_id", "=", $item->id)->execute();
+ }
+ }
+
+ static function item_created($item) {
+ // Check for any already existing password on parent album(s), if found, generate cache data for the new item.
+ $existing_password = ORM::factory("albumpassword_idcache")->where("item_id", "=", $item->parent_id)->order_by("cache_id")->find_all();
+ if (count($existing_password) > 0) {
+ $new_cachedid = ORM::factory("albumpassword_idcache");
+ $new_cachedid->password_id = $existing_password[0]->password_id;
+ $new_cachedid->item_id = $item->id;
+ $new_cachedid->save();
}
}
+
+ static function item_moved($item, $old_parent) {
+ // Delete any existing cache data.
+ db::build()->delete("albumpassword_idcaches")->where("item_id", "=", $item->id)->execute();
+ // Check for a password on the new parent, generate cache data if necessary.
+ $existing_password = ORM::factory("albumpassword_idcache")->where("item_id", "=", $item->parent_id)->order_by("cache_id")->find_all();
+ if (count($existing_password) > 0) {
+ $new_cachedid = ORM::factory("albumpassword_idcache");
+ $new_cachedid->password_id = $existing_password[0]->password_id;
+ $new_cachedid->item_id = $item->id;
+ $new_cachedid->save();
+ }
+ }
+
static function admin_menu($menu, $theme) {
// Add a link to the Album Password admin page to the Content menu.
$menu->get("settings_menu")
@@ -28,25 +28,45 @@ static function install() {
PRIMARY KEY (`id`))
DEFAULT CHARSET=utf8;");
+ // Create a table to store a list of all protected items in.
+ $db->query("CREATE TABLE IF NOT EXISTS {albumpassword_idcaches} (
+ `cache_id` int(9) NOT NULL auto_increment,
+ `password_id` int(9) NOT NULL,
+ `item_id` int(9) NOT NULL,
+ PRIMARY KEY (`cache_id`))
+ DEFAULT CHARSET=utf8;");
+
// Set the default value for this module's behavior.
module::set_var("albumpassword", "hideonly", true);
// Set the module's version number.
- module::set_version("albumpassword", 2);
+ module::set_version("albumpassword", 3);
}
static function upgrade($version) {
- // Set the default value for this module's behavior.
- module::set_var("albumpassword", "hideonly", true);
-
- // Set the module's version number.
- module::set_version("albumpassword", 2);
+ $db = Database::instance();
+ if ($version == 1) {
+ // Set the default value for this module's behavior.
+ module::set_var("albumpassword", "hideonly", true);
+ module::set_version("albumpassword", $version = 2);
+ }
+ if ($version == 2) {
+ // Create a table to store a list of all protected items in.
+ $db->query("CREATE TABLE IF NOT EXISTS {albumpassword_idcaches} (
+ `cache_id` int(9) NOT NULL auto_increment,
+ `password_id` int(9) NOT NULL,
+ `item_id` int(9) NOT NULL,
+ PRIMARY KEY (`cache_id`))
+ DEFAULT CHARSET=utf8;");
+ module::set_version("albumpassword", $version = 3);
+ }
}
static function uninstall() {
// Delete the password table before uninstalling.
$db = Database::instance();
- $db->query("DROP TABLE IF EXISTS {items_albumpassword};");
+ $db->query("DROP TABLE IF EXISTS {items_albumpasswords};");
+ $db->query("DROP TABLE IF EXISTS {albumpassword_idcaches};");
module::delete("albumpassword");
}
}
Oops, something went wrong.

0 comments on commit ae8d635

Please sign in to comment.