function add_albums_and_photos($count, $desired_type = null) { srand(time()); $parents = ORM::factory("item")->where("type", "album")->find_all()->as_array(); $owner_id = user::active()->id; $test_images = glob(MODPATH . "gallery/tests/images/*.[Jj][Pp][Gg]"); batch::start(); $album_count = $photo_count = 0; for ($i = 0; $i < $count; $i++) { set_time_limit(30); $parent = $parents[array_rand($parents)]; $parent->reload(); $type = $desired_type; if (!$type) { $type = rand(0, 10) ? "photo" : "album"; } if ($type == "album") { $thumb_size = module::get_var("gallery", "thumb_size"); $parents[] = album::create($parent, "rnd_" . rand(), "Rnd {$i}", "random album {$i}", $owner_id)->save(); $album_count++; } else { $photo_index = rand(0, count($test_images) - 1); photo::create($parent, $test_images[$photo_index], basename($test_images[$photo_index]), "rnd_" . rand(), "sample thumb", $owner_id); $photo_count++; } } batch::stop(); if ($photo_count > 0) { log::success("content", "(scaffold) Added {$photo_count} photos"); } if ($album_count > 0) { log::success("content", "(scaffold) Added {$album_count} albums"); } url::redirect("scaffold"); }
public function resize_url_test() { $rand = rand(); $root = ORM::factory("item", 1); $album = album::create($root, $rand, $rand, $rand); $photo = photo::create($album, MODPATH . "gallery/tests/test.jpg", "{$rand}.jpg", $rand, $rand); $this->assert_equal("http://./var/resizes/{$rand}/{$rand}.jpg", $photo->resize_url()); }
public function create_photo_creates_reasonable_slug_test() { $rand = rand(); $root = ORM::factory("item", 1); $album = album::create($root, $rand, $rand, $rand); $photo = photo::create($album, MODPATH . "gallery/tests/test.jpg", "This (is) my file%name.jpg", $rand, $rand); $this->assert_equal("This-is-my-file-name", $photo->slug); }
public function deleting_an_item_deletes_its_comments_too_test() { $rand = rand(); $album = album::create(ORM::factory("item", 1), "test_{$rand}", "test_{$rand}"); $comment = comment::create($album, identity::guest(), "text_{$rand}", "name_{$rand}", "email_{$rand}", "url_{$rand}"); $album->delete(); $deleted_comment = ORM::factory("comment", $comment->id); $this->assert_false($deleted_comment->loaded); }
static function add_from_server($task) { $context = unserialize($task->context); try { $paths = array_keys(unserialize(module::get_var("server_add", "authorized_paths"))); $path = $paths[$context["next_path"]]; if (!empty($context["files"][$path])) { $file = $context["files"][$path][$context["position"]]; $parent = ORM::factory("item", $file["parent_id"]); access::required("server_add", $parent); access::required("add", $parent); if (!$parent->is_album()) { throw new Exception("@todo BAD_ALBUM"); } $name = $file["name"]; if ($file["type"] == "album") { $album = ORM::factory("item")->where("name", $name)->where("parent_id", $parent->id)->find(); if (!$album->loaded) { $album = album::create($parent, $name, $name, null, user::active()->id); } // Now that we have a new album. Go through the remaining files to import and change the // parent_id of any file that has the same relative path as this album's path. $album_path = "{$file['path']}/{$name}"; for ($idx = $context["position"] + 1; $idx < count($context["files"][$path]); $idx++) { if (strpos($context["files"][$path][$idx]["path"], $album_path) === 0) { $context["files"][$path][$idx]["parent_id"] = $album->id; } } } else { $extension = strtolower(substr(strrchr($name, '.'), 1)); $source_path = "{$path}{$file['path']}/{$name}"; if (in_array($extension, array("flv", "mp4"))) { $movie = movie::create($parent, $source_path, $name, $name, null, user::active()->id); } else { $photo = photo::create($parent, $source_path, $name, $name, null, user::active()->id); } } $context["counter"]++; if (++$context["position"] >= count($context["files"][$path])) { $context["next_path"]++; $context["position"] = 0; } } else { $context["next_path"]++; } } catch (Exception $e) { $context["errors"][$path] = $e->getMessage(); } $task->context = serialize($context); $task->state = "success"; $task->percent_complete = $context["counter"] / (double) $context["total"] * 100; $task->done = $context["counter"] == (double) $context["total"]; }
public function create_album_silently_trims_trailing_periods_test() { $rand = rand(); $root = ORM::factory("item", 1); try { $album = album::create($root, $rand . "..", $rand, $rand); } catch (Exception $e) { $this->assert_equal("@todo NAME_CANNOT_END_IN_PERIOD", $e->getMessage()); return; } $this->assert_true(false, "Shouldn't create an album with trailing . in the name"); }
public function viewable_test() { $root = ORM::factory("item", 1); $album = album::create($root, rand(), rand(), rand()); $item = self::_create_random_item($album); identity::set_active_user(identity::guest()); // We can see the item when permissions are granted access::allow(identity::everybody(), "view", $album); $this->assert_equal(1, ORM::factory("item")->viewable()->where("id", "=", $item->id)->count_all()); // We can't see the item when permissions are denied access::deny(identity::everybody(), "view", $album); $this->assert_equal(0, ORM::factory("item")->viewable()->where("id", "=", $item->id)->count_all()); }
public function cant_view_comments_for_unviewable_items_test() { $root = ORM::factory("item", 1); $album = album::create($root, rand(), rand(), rand()); $comment = comment::create($album, user::guest(), "text", "name", "email", "url"); user::set_active(user::guest()); // We can see the comment when permissions are granted on the album access::allow(group::everybody(), "view", $album); $this->assert_equal(1, ORM::factory("comment")->viewable()->where("comments.id", $comment->id)->count_all()); // We can't see the comment when permissions are denied on the album access::deny(group::everybody(), "view", $album); $this->assert_equal(0, ORM::factory("comment")->viewable()->where("comments.id", $comment->id)->count_all()); }
private function _create_album($album) { access::required("edit", $album); $form = album::get_add_form($album); if ($form->validate()) { $new_album = album::create($album, $this->input->post("name"), $this->input->post("title", $this->input->post("name")), $this->input->post("description"), user::active()->id); log::success("content", "Created an album", html::anchor("albums/{$new_album->id}", "view album")); message::success(t("Created album %album_title", array("album_title" => $new_album->title))); print json_encode(array("result" => "success", "location" => url::site("albums/{$new_album->id}"), "resource" => url::site("albums/{$new_album->id}"))); } else { print json_encode(array("result" => "error", "form" => $form->__toString())); } }
public function setup() { $this->_server = $_SERVER; $root = ORM::factory("item", 1); $this->_album = album::create($root, rand(), "test album"); access::deny(identity::everybody(), "view_full", $this->_album); access::deny(identity::registered_users(), "view_full", $this->_album); $rand = rand(); $this->_item = photo::create($this->_album, MODPATH . "gallery/tests/test.jpg", "{$rand}.jpg", $rand, $rand); $this->_proxy = ORM::factory("digibug_proxy"); $this->_proxy->uuid = md5(rand()); $this->_proxy->item_id = $this->_item->id; $this->_proxy->save(); }
public function change_album_no_csrf_fails_test() { $controller = new Albums_Controller(); $root = ORM::factory("item", 1); $this->_album = album::create($root, "test", "test", "test"); $_POST["name"] = "new name"; $_POST["title"] = "new title"; $_POST["description"] = "new description"; access::allow(group::everybody(), "edit", $root); try { $controller->_update($this->_album); $this->assert_true(false, "This should fail"); } catch (Exception $e) { // pass } }
public function move_to_test() { $root = ORM::factory("item", 1); $album1 = album::create($root, "move_to_test_1", "move_to_test_1"); $album1_1 = album::create($album1, "move_to_test_1_1", "move_to_test_1_1"); $album1_2 = album::create($album1, "move_to_test_1_2", "move_to_test_1_2"); $album1_1_1 = album::create($album1_1, "move_to_test_1_1_1", "move_to_test_1_1_1"); $album1_1_2 = album::create($album1_1, "move_to_test_1_1_2", "move_to_test_1_1_2"); $album1_2->reload(); $album1_1_1->reload(); $album1_1_1->move_to($album1_2); $album1_1->reload(); $album1_2->reload(); $this->assert_equal(3, $album1_1->right - $album1_1->left); $this->assert_equal(3, $album1_2->right - $album1_2->left); $this->assert_equal(array($album1_1_2->id => "move_to_test_1_1_2"), $album1_1->children()->select_list()); $this->assert_equal(array($album1_1_1->id => "move_to_test_1_1_1"), $album1_2->children()->select_list()); }
public function create_tag_test() { $rand = rand(); $root = ORM::factory("item", 1); $album = album::create($root, $rand, $rand, $rand); $tag1 = "tag1"; tag::add($album, $tag1); $tag = ORM::factory("tag")->where("name", $tag1)->find(); $this->assert_true(1, $tag->count); // Make sure adding the tag again doesn't increase the count tag::add($album, $tag1); $tag = ORM::factory("tag")->where("name", $tag1)->find(); $this->assert_true(1, $tag->count); $rand = rand(); $album = album::create($root, $rand, $rand, $rand); tag::add($album, $tag1); $tag = ORM::factory("tag")->where("name", $tag1)->find(); $this->assert_true(2, $tag->count); }
public function move_album_test() { // Create an album with a photo in it $root = ORM::factory("item", 1); $album = album::create($root, rand(), rand(), rand()); $photo = ORM::factory("item"); $photo->name = rand(); $photo->type = "photo"; $photo->add_to_parent($album); file_put_contents($photo->thumb_path(), "thumb"); file_put_contents($photo->resize_path(), "resize"); file_put_contents($photo->file_path(), "file"); $original_album_name = $album->name; $original_photo_name = $photo->name; $new_album_name = rand(); // Now rename the album $album->rename($new_album_name)->save(); $photo->reload(); // Expected: // * the album name changed. // * the album dirs are all moved // * the photo's paths are all inside the albums paths // * the photo files are all still intact and accessible $this->assert_equal($new_album_name, $album->name); $this->assert_equal($new_album_name, basename($album->file_path())); $this->assert_equal($new_album_name, basename(dirname($album->thumb_path()))); $this->assert_equal($new_album_name, basename(dirname($album->resize_path()))); $this->assert_same(0, strpos($photo->file_path(), $album->file_path())); $this->assert_same(0, strpos($photo->thumb_path(), dirname($album->thumb_path()))); $this->assert_same(0, strpos($photo->resize_path(), dirname($album->resize_path()))); $this->assert_equal("thumb", file_get_contents($photo->thumb_path())); $this->assert_equal("resize", file_get_contents($photo->resize_path())); $this->assert_equal("file", file_get_contents($photo->file_path())); }
public function create($parent_id) { access::verify_csrf(); $album = ORM::factory("item", $parent_id); access::required("view", $album); access::required("add", $album); $input = Input::instance(); $form = album::get_add_form($album); if ($form->validate()) { $new_album = album::create($album, $input->post("name"), $input->post("title", $input->post("name")), $input->post("description"), identity::active_user()->id, $input->post("slug")); log::success("content", "Created an album", html::anchor("albums/{$new_album->id}", "view album")); message::success(t("Created album %album_title", array("album_title" => html::purify($new_album->title)))); print json_encode(array("result" => "success", "location" => $new_album->url())); } else { print json_encode(array("result" => "error", "form" => $form->__toString())); } }
/** * Import a single album. */ static function import_album(&$queue) { // The queue is a set of nested associative arrays where the key is the album id and the // value is an array of similar arrays. We'll do a breadth first tree traversal using the // queue to keep our state. Doing it breadth first means that the parent will be created by // the time we get to the child. // Dequeue the current album and enqueue its children list($g2_album_id, $children) = each($queue); unset($queue[$g2_album_id]); foreach ($children as $key => $value) { $queue[$key] = $value; } if (self::map($g2_album_id)) { return t("Album with id: %id already imported, skipping", array("id" => $g2_album_id)); } try { // Load the G2 album item, and figure out its parent in G3. $g2_album = g2(GalleryCoreApi::loadEntitiesById($g2_album_id)); } catch (Exception $e) { return t("Failed to import Gallery 2 album with id: %id\n%exception", array("id" => $g2_album_id, "exception" => $e->__toString())); } if ($g2_album->getParentId() == null) { return t("Skipping Gallery 2 root album"); } $parent_album = ORM::factory("item", self::map($g2_album->getParentId())); $album = album::create($parent_album, $g2_album->getPathComponent(), self::_decode_html_special_chars($g2_album->getTitle()), self::_decode_html_special_chars(self::extract_description($g2_album)), self::map($g2_album->getOwnerId())); $album->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_album_id)); $album->created = $g2_album->getCreationTimestamp(); $order_map = array("originationTimestamp" => "captured", "creationTimestamp" => "created", "description" => "description", "modificationTimestamp" => "updated", "orderWeight" => "weight", "pathComponent" => "name", "summary" => "description", "title" => "title", "viewCount" => "view_count"); $direction_map = array(ORDER_ASCENDING => "asc", ORDER_DESCENDING => "desc"); if (array_key_exists($g2_order = $g2_album->getOrderBy(), $order_map)) { $album->sort_column = $order_map[$g2_order]; $album->sort_order = $direction_map[$g2_album->getOrderDirection()]; } $album->save(); self::import_keywords_as_tags($g2_album->getKeywords(), $album); self::set_map($g2_album_id, $album->id); // @todo import album highlights }
private static function _add_album_or_photo($desired_type = null) { srand(time()); $parents = ORM::factory("item")->where("type", "album")->find_all()->as_array(); $owner_id = user::active()->id; $test_images = glob(dirname(dirname(__FILE__)) . "/data/*.[Jj][Pp][Gg]"); $parent = $parents[array_rand($parents)]; $parent->reload(); $type = $desired_type; if (!$type) { $type = rand(0, 10) ? "photo" : "album"; } if ($type == "album") { $thumb_size = module::get_var("core", "thumb_size"); $rand = rand(); $parents[] = album::create($parent, "rnd_{$rand}", "Rnd {$rand}", "random album {$rand}", $owner_id)->save(); } else { $photo_index = rand(0, count($test_images) - 1); photo::create($parent, $test_images[$photo_index], basename($test_images[$photo_index]), "rnd_" . rand(), "sample thumb", $owner_id); } }
public function everybody_view_full_permission_maintains_htaccess_files_test() { $root = ORM::factory("item", 1); $album = album::create($root, rand(), "test album"); $this->assert_false(file_exists($album->file_path() . "/.htaccess")); $this->assert_false(file_exists($album->resize_path() . "/.htaccess")); $this->assert_false(file_exists($album->thumb_path() . "/.htaccess")); access::deny(group::everybody(), "view_full", $album); $this->assert_true(file_exists($album->file_path() . "/.htaccess")); $this->assert_false(file_exists($album->resize_path() . "/.htaccess")); $this->assert_false(file_exists($album->thumb_path() . "/.htaccess")); access::allow(group::everybody(), "view_full", $album); $this->assert_false(file_exists($album->file_path() . "/.htaccess")); $this->assert_false(file_exists($album->resize_path() . "/.htaccess")); $this->assert_false(file_exists($album->thumb_path() . "/.htaccess")); access::deny(group::everybody(), "view_full", $album); $this->assert_true(file_exists($album->file_path() . "/.htaccess")); $this->assert_false(file_exists($album->resize_path() . "/.htaccess")); $this->assert_false(file_exists($album->thumb_path() . "/.htaccess")); access::reset(group::everybody(), "view_full", $album); $this->assert_false(file_exists($album->file_path() . "/.htaccess")); $this->assert_false(file_exists($album->resize_path() . "/.htaccess")); $this->assert_false(file_exists($album->thumb_path() . "/.htaccess")); }
public function moved_items_inherit_new_permissions_test() { user::set_active(user::lookup_by_name("admin")); $root = ORM::factory("item", 1); $public_album = album::create($root, rand(), "public album"); $public_photo = photo::create($public_album, MODPATH . "gallery/images/gallery.png", "", ""); access::allow(group::everybody(), "view", $public_album); $root->reload(); // Account for MPTT changes $private_album = album::create($root, rand(), "private album"); access::deny(group::everybody(), "view", $private_album); $private_photo = photo::create($private_album, MODPATH . "gallery/images/gallery.png", "", ""); // Make sure that we now have a public photo and private photo. $this->assert_true(access::group_can(group::everybody(), "view", $public_photo)); $this->assert_false(access::group_can(group::everybody(), "view", $private_photo)); // Swap the photos item::move($public_photo, $private_album); $private_album->reload(); // Reload to get new MPTT pointers and cached perms. $public_album->reload(); $private_photo->reload(); $public_photo->reload(); item::move($private_photo, $public_album); $private_album->reload(); // Reload to get new MPTT pointers and cached perms. $public_album->reload(); $private_photo->reload(); $public_photo->reload(); // Make sure that the public_photo is now private, and the private_photo is now public. $this->assert_false(access::group_can(group::everybody(), "view", $public_photo)); $this->assert_true(access::group_can(group::everybody(), "view", $private_photo)); }
public function cant_move_parent_into_own_subtree_test() { $album1 = album::create(item::root(), "move_to_test", "move_to_test"); $album2 = album::create($album1, "move_to_test", "move_to_test"); $album3 = album::create($album2, "move_to_test", "move_to_test"); try { $album1->move_to($album3); $self->assert_true(false, "We should be unable to move an item inside its own hierarchy"); } catch (Exception $e) { // pass } }
public function move_photo_fails_invalid_target_test() { // Create an album with a photo in it $root = ORM::factory("item", 1); $photo_name = rand(); $photo1 = self::_create_random_item($root, $photo_name); $name = rand(); $album = album::create($root, $name, $name, $name); $photo2 = self::_create_random_item($album, $photo_name); try { $photo2->move_to($root); } catch (Exception $e) { // pass $this->assert_true(strpos($e->getMessage(), "INVALID_MOVE_TARGET_EXISTS") !== false, "incorrect exception."); return; } $this->assert_false(true, "Item_Model::rename should not accept / characters"); }
/** * This is the task code that adds photos and albums. It first examines all the target files * and creates a set of Server_Add_File_Models, then runs through the list of models and adds * them one at a time. */ static function add($task) { $mode = $task->get("mode", "init"); $start = microtime(true); switch ($mode) { case "init": $task->set("mode", "build-file-list"); $task->percent_complete = 0; $task->status = t("Starting up"); batch::start(); break; case "build-file-list": // 0% to 10% // We can't fit an arbitrary number of paths in a task, so store them in a separate table. // Don't use an iterator here because we can't get enough control over it when we're dealing // with a deep hierarchy and we don't want to go over our time quota. The queue is in the // form [path, parent_id] where the parent_id refers to another Server_Add_File_Model. We // have this extra level of abstraction because we don't know its Item_Model id yet. $queue = $task->get("queue"); while ($queue && microtime(true) - $start < 0.5) { list($file, $parent_entry_id) = array_shift($queue); $entry = ORM::factory("server_add_file"); $entry->task_id = $task->id; $entry->file = $file; $entry->parent_id = $parent_entry_id; $entry->save(); foreach (glob("{$file}/*") as $child) { if (is_dir($child)) { $queue[] = array($child, $entry->id); } else { $ext = strtolower(pathinfo($child, PATHINFO_EXTENSION)); if (in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4"))) { $child_entry = ORM::factory("server_add_file"); $child_entry->task_id = $task->id; $child_entry->file = $child; $child_entry->parent_id = $entry->id; $child_entry->save(); } } } } // We have no idea how long this can take because we have no idea how deep the tree // hierarchy rabbit hole goes. Leave ourselves room here for 100 iterations and don't go // over 10% in percent_complete. $task->set("queue", $queue); $task->percent_complete = min($task->percent_complete + 0.1, 10); $task->status = t2("Found one file", "Found %count files", Database::instance()->where("task_id", $task->id)->count_records("server_add_files")); if (!$queue) { $task->set("mode", "add-files"); $task->set("total_files", database::instance()->count_records("server_add_files", array("task_id" => $task->id))); $task->percent_complete = 10; } break; case "add-files": // 10% to 100% $completed_files = $task->get("completed_files", 0); $total_files = $task->get("total_files"); // Ordering by id ensures that we add them in the order that we created the entries, which // will create albums first. Ignore entries which already have an Item_Model attached, // they're done. $entries = ORM::factory("server_add_file")->where("task_id", $task->id)->where("item_id", null)->orderby("id", "ASC")->limit(10)->find_all(); if ($entries->count() == 0) { // Out of entries, we're done. $task->set("mode", "done"); } $owner_id = user::active()->id; foreach ($entries as $entry) { if (microtime(true) - $start > 0.5) { break; } // Look up the parent item for this entry. By now it should exist, but if none was // specified, then this belongs as a child of the current item. $parent_entry = ORM::factory("server_add_file", $entry->parent_id); if (!$parent_entry->loaded) { $parent = ORM::factory("item", $task->get("item_id")); } else { $parent = ORM::factory("item", $parent_entry->item_id); } $name = basename($entry->file); $title = item::convert_filename_to_title($name); if (is_dir($entry->file)) { $album = album::create($parent, $name, $title, null, $owner_id); $entry->item_id = $album->id; } else { $extension = strtolower(pathinfo($name, PATHINFO_EXTENSION)); if (in_array($extension, array("gif", "png", "jpg", "jpeg"))) { $photo = photo::create($parent, $entry->file, $name, $title, null, $owner_id); $entry->item_id = $photo->id; } else { if (in_array($extension, array("flv", "mp4"))) { $movie = movie::create($parent, $entry->file, $name, $title, null, $owner_id); $entry->item_id = $movie->id; } else { // This should never happen, because we don't add stuff to the list that we can't // process. But just in, case.. set this to a non-null value so that we skip this // entry. $entry->item_id = 0; $task->log("Skipping unknown file type: {$entry->file}"); } } } $completed_files++; $entry->save(); } $task->set("completed_files", $completed_files); $task->status = t("Adding photos and albums (%completed of %total)", array("completed" => $completed_files, "total" => $total_files)); $task->percent_complete = 10 + 100 * ($completed_files / $total_files); break; case "done": batch::stop(); $task->done = true; $task->state = "success"; $task->percent_complete = 100; ORM::factory("server_add_file")->where("task_id", $task->id)->delete_all(); message::info(t2("Successfully added one photo", "Successfully added %count photos and albums", $task->get("completed_files"))); } }