Beispiel #1
1
 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");
 }
Beispiel #2
0
 /**
  * Task that rebuilds all dirty images.
  * @param Task_Model the task
  */
 static function rebuild_dirty_images($task)
 {
     $errors = array();
     try {
         $result = graphics::find_dirty_images_query()->select("id")->execute();
         $total_count = $task->get("total_count", $result->count());
         $mode = $task->get("mode", "init");
         if ($mode == "init") {
             $task->set("total_count", $total_count);
             $task->set("mode", "process");
             batch::start();
         }
         $completed = $task->get("completed", 0);
         $ignored = $task->get("ignored", array());
         $i = 0;
         foreach ($result as $row) {
             if (array_key_exists($row->id, $ignored)) {
                 continue;
             }
             $item = ORM::factory("item", $row->id);
             if ($item->loaded()) {
                 try {
                     graphics::generate($item);
                     $completed++;
                     $errors[] = t("Successfully rebuilt images for '%title'", array("title" => html::purify($item->title)));
                 } catch (Exception $e) {
                     $errors[] = t("Unable to rebuild images for '%title'", array("title" => html::purify($item->title)));
                     $errors[] = (string) $e;
                     $ignored[$item->id] = 1;
                 }
             }
             if (++$i == 2) {
                 break;
             }
         }
         $task->status = t2("Updated: 1 image. Total: %total_count.", "Updated: %count images. Total: %total_count.", $completed, array("total_count" => $total_count));
         if ($completed < $total_count) {
             $task->percent_complete = (int) (100 * ($completed + count($ignored)) / $total_count);
         } else {
             $task->percent_complete = 100;
         }
         $task->set("completed", $completed);
         $task->set("ignored", $ignored);
         if ($task->percent_complete == 100) {
             $task->done = true;
             $task->state = "success";
             batch::stop();
             site_status::clear("graphics_dirty");
         }
     } catch (Exception $e) {
         Kohana_Log::add("error", (string) $e);
         $task->done = true;
         $task->state = "error";
         $task->status = $e->getMessage();
         $errors[] = (string) $e;
     }
     if ($errors) {
         $task->log($errors);
     }
 }
Beispiel #3
0
 /**
  * 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")));
     }
 }
 static function sync($task)
 {
     aws_s3::log("Amazon S3 Re-sync task started..");
     batch::start();
     $items = ORM::factory("item")->find_all();
     $task->set("total_count", count($items));
     $task->set("completed", 0);
     if (!module::get_var("aws_s3", "synced", false)) {
         aws_s3::log("Emptying contents of bucket");
         $task->status = "Emptying contents of bucket";
         $task->save();
         require_once MODPATH . "aws_s3/lib/s3.php";
         $s3 = new S3(module::get_var("aws_s3", "access_key"), module::get_var("aws_s3", "secret_key"));
         $bucket = module::get_var("aws_s3", "bucket_name");
         $resource = aws_s3::get_resource_url("");
         $stuff = array_reverse(S3::getBucket($bucket, $resource));
         $i = 0;
         foreach ($stuff as $uri => $item) {
             $i++;
             aws_s3::log("Removing " . $uri . " from S3");
             S3::deleteObject($bucket, $uri);
             $task->percent_complete = round(20 * ($i / count($stuff)));
             $task->save();
         }
     }
     $task->percent_complete = 20;
     aws_s3::log("Commencing upload tasks");
     $task->state = "Commencing upload...";
     $task->save();
     $completed = $task->get("completed", 0);
     $items = ORM::factory("item")->find_all();
     foreach ($items as $item) {
         try {
             if ($item->id > 1) {
                 aws_s3::upload_item($item, aws_s3::get_upload_flags());
             }
         } catch (Exception $err) {
         }
         $completed++;
         $task->set("completed", $completed);
         $task->percent_complete = round(80 * ($completed / $task->get("total_count"))) + 20;
         $task->status = $completed . " of " . $task->get("total_count") . " uploaded.";
         $task->save();
     }
     $task->percent_complete = 100;
     $task->state = "success";
     $task->done = true;
     aws_s3::log("Sync task completed successfully");
     $task->status = "Sync task completed successfully";
     module::set_var("aws_s3", "synced", true);
     site_status::clear("aws_s3_not_synced");
     batch::stop();
     $task->save();
 }
Beispiel #5
0
 public function delete($id)
 {
     access::verify_csrf();
     $item = model_cache::get("item", $id);
     access::required("view", $item);
     access::required("edit", $item);
     if ($item->is_album()) {
         $msg = t("Deleted album <b>%title</b>", array("title" => html::purify($item->title)));
     } else {
         $msg = t("Deleted photo <b>%title</b>", array("title" => html::purify($item->title)));
     }
     $parent = $item->parent();
     if ($item->is_album()) {
         // Album delete will trigger deletes for all children.  Do this in a batch so that we can be
         // smart about notifications, album cover updates, etc.
         batch::start();
         $item->delete();
         batch::stop();
     } else {
         $item->delete();
     }
     message::success($msg);
     $from_id = Input::instance()->get("from_id");
     if (Input::instance()->get("page_type") == "collection" && $from_id != $id) {
         json::reply(array("result" => "success", "reload" => 1));
     } else {
         json::reply(array("result" => "success", "location" => $parent->url()));
     }
 }
 static function sync($task)
 {
     require_once MODPATH . "aws_s3/lib/s3.php";
     $s3 = new S3(module::get_var("aws_s3", "access_key"), module::get_var("aws_s3", "secret_key"));
     $mode = $task->get("mode", "init");
     switch ($mode) {
         case "init":
             aws_s3::log("re-sync task started..");
             batch::start();
             $items = ORM::factory("item")->find_all();
             aws_s3::log("items to sync: " . count($items));
             $task->set("total_count", count($items));
             $task->set("completed", 0);
             $task->set("mode", "empty");
             $task->status = "Emptying contents of bucket";
             break;
         case "empty":
             // 0 - 10%
             aws_s3::log("emptying bucket contents (any files that may already exist in the bucket/prefix path)");
             $bucket = module::get_var("aws_s3", "bucket_name");
             $resource = aws_s3::get_resource_url("");
             $stuff = array_reverse(S3::getBucket($bucket, $resource));
             foreach ($stuff as $uri => $item) {
                 aws_s3::log("removing: " . $uri);
                 S3::deleteObject($bucket, $uri);
             }
             $task->percent_complete = 10;
             $task->set("mode", "upload");
             $task->state = "Commencing upload...";
             break;
         case "upload":
             // 10 - 100%
             $completed = $task->get("completed", 0);
             $items = ORM::factory("item")->find_all(1, $completed);
             foreach ($items as $item) {
                 if ($item->id > 1) {
                     aws_s3::log("uploading item " . $item->id . " (" . ($completed + 1) . "/" . $task->get("total_count") . ")");
                     if ($item->is_album()) {
                         aws_s3::upload_album_cover($item);
                     } else {
                         aws_s3::upload_item($item);
                     }
                 }
                 $completed++;
             }
             $task->set("completed", $completed);
             $task->percent_complete = round(90 * ($completed / $task->get("total_count"))) + 10;
             $task->status = $completed . " of " . $task->get("total_count") . " uploaded.";
             if ($completed == $task->get("total_count")) {
                 $task->set("mode", "finish");
             }
             break;
         case "finish":
             aws_s3::log("completing upload task..");
             $task->percent_complete = 100;
             $task->state = "success";
             $task->done = true;
             $task->status = "Sync task completed successfully";
             batch::stop();
             module::set_var("aws_s3", "synced", true);
             site_status::clear("aws_s3_not_synced");
             break;
     }
 }
Beispiel #7
0
 /**
  * This is the task code that adds photos and albums.  It first examines all the target files
  * and creates a set of Server_Add_Entry_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->set("dirs_scanned", 0);
             $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.
             $paths = unserialize(module::get_var("server_add", "authorized_paths"));
             $dirs_scanned = $task->get("dirs_scanned");
             while (microtime(true) - $start < 0.5) {
                 // Process every directory that doesn't yet have a parent id, these are the
                 // paths that we're importing.
                 $entry = ORM::factory("server_add_entry")->where("task_id", "=", $task->id)->where("is_directory", "=", 1)->where("checked", "=", 0)->order_by("id", "ASC")->find();
                 if ($entry->loaded()) {
                     $child_paths = glob(preg_quote($entry->path) . "/*");
                     if (!$child_paths) {
                         $child_paths = glob("{$entry->path}/*");
                     }
                     foreach ($child_paths as $child_path) {
                         if (!is_dir($child_path)) {
                             $ext = strtolower(pathinfo($child_path, PATHINFO_EXTENSION));
                             if (!legal_file::get_extensions($ext) || !filesize($child_path)) {
                                 // Not importable, skip it.
                                 continue;
                             }
                         }
                         $child_entry = ORM::factory("server_add_entry");
                         $child_entry->task_id = $task->id;
                         $child_entry->path = $child_path;
                         $child_entry->parent_id = $entry->id;
                         // null if the parent was a staging dir
                         $child_entry->is_directory = is_dir($child_path);
                         $child_entry->save();
                     }
                     // We've processed this entry, mark it as done.
                     $entry->checked = 1;
                     $entry->save();
                     $dirs_scanned++;
                 }
             }
             // 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("dirs_scanned", $dirs_scanned);
             $task->percent_complete = min($task->percent_complete + 0.1, 10);
             $task->status = t2("Scanned one directory", "Scanned %count directories", $dirs_scanned);
             if (!$entry->loaded()) {
                 $task->set("mode", "add-files");
                 $task->set("total_files", ORM::factory("server_add_entry")->where("task_id", "=", $task->id)->count_all());
                 $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_entry")->where("task_id", "=", $task->id)->where("item_id", "IS", null)->order_by("id", "ASC")->limit(10)->find_all();
             if ($entries->count() == 0) {
                 // Out of entries, we're done.
                 $task->set("mode", "done");
             }
             $owner_id = identity::active_user()->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_entry", $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->path);
                 $title = item::convert_filename_to_title($name);
                 if ($entry->is_directory) {
                     $album = ORM::factory("item");
                     $album->type = "album";
                     $album->parent_id = $parent->id;
                     $album->name = $name;
                     $album->title = $title;
                     $album->owner_id = $owner_id;
                     $album->sort_order = $parent->sort_order;
                     $album->sort_column = $parent->sort_column;
                     $album->save();
                     $entry->item_id = $album->id;
                 } else {
                     try {
                         $extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
                         if (legal_file::get_photo_extensions($extension)) {
                             $photo = ORM::factory("item");
                             $photo->type = "photo";
                             $photo->parent_id = $parent->id;
                             $photo->set_data_file($entry->path);
                             $photo->name = $name;
                             $photo->title = $title;
                             $photo->owner_id = $owner_id;
                             $photo->save();
                             $entry->item_id = $photo->id;
                         } else {
                             if (legal_file::get_movie_extensions($extension)) {
                                 $movie = ORM::factory("item");
                                 $movie->type = "movie";
                                 $movie->parent_id = $parent->id;
                                 $movie->set_data_file($entry->path);
                                 $movie->name = $name;
                                 $movie->title = $title;
                                 $movie->owner_id = $owner_id;
                                 $movie->save();
                                 $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->path}");
                             }
                         }
                     } catch (Exception $e) {
                         // This can happen if a photo file is invalid, like a BMP masquerading as a .jpg
                         $entry->item_id = 0;
                         $task->log("Skipping invalid file: {$entry->path}");
                     }
                 }
                 $completed_files++;
                 $entry->save();
             }
             $task->set("completed_files", $completed_files);
             $task->status = t("Adding photos / albums (%completed of %total)", array("completed" => $completed_files, "total" => $total_files));
             $task->percent_complete = $total_files ? 10 + 100 * ($completed_files / $total_files) : 100;
             break;
         case "done":
             batch::stop();
             $task->done = true;
             $task->state = "success";
             $task->percent_complete = 100;
             ORM::factory("server_add_entry")->where("task_id", "=", $task->id)->delete_all();
             message::info(t2("Successfully added one photo / album", "Successfully added %count photos / albums", $task->get("completed_files")));
     }
 }
Beispiel #8
0
 public function pause($id, $task_id)
 {
     access::verify_csrf();
     $task = ORM::factory("task", $task_id);
     message::warning(t("Add from server was cancelled prior to completion"));
     batch::stop();
     print json_encode(array("result" => "success"));
 }
Beispiel #9
0
 public function finish()
 {
     batch::stop();
     print json_encode(array("result" => "success"));
 }
Beispiel #10
0
 /**
  * Task that rebuilds all dirty images.
  * @param Task_Model the task
  */
 static function rebuild_dirty_images($task)
 {
     $errors = array();
     try {
         // Choose the dirty images in a random order so that if we run this task multiple times
         // concurrently each task is rebuilding different images simultaneously.
         $result = graphics::find_dirty_images_query()->select("id")->select(db::expr("RAND() as r"))->order_by("r", "ASC")->execute();
         $total_count = $task->get("total_count", $result->count());
         $mode = $task->get("mode", "init");
         if ($mode == "init") {
             $task->set("total_count", $total_count);
             $task->set("mode", "process");
             batch::start();
         }
         $completed = $task->get("completed", 0);
         $ignored = $task->get("ignored", array());
         $i = 0;
         // If there's no work left to do, skip to the end.  This can happen if we resume a task long
         // after the work got done in some other task.
         if (!$result->count()) {
             $completed = $total_count;
         }
         foreach ($result as $row) {
             if (array_key_exists($row->id, $ignored)) {
                 continue;
             }
             $item = ORM::factory("item", $row->id);
             if ($item->loaded()) {
                 try {
                     graphics::generate($item);
                     $completed++;
                     $errors[] = t("Successfully rebuilt images for '%title'", array("title" => html::purify($item->title)));
                 } catch (Exception $e) {
                     $errors[] = t("Unable to rebuild images for '%title'", array("title" => html::purify($item->title)));
                     $errors[] = (string) $e;
                     $ignored[$item->id] = 1;
                 }
             }
             if (++$i == 2) {
                 break;
             }
         }
         $task->status = t2("Updated: 1 image. Total: %total_count.", "Updated: %count images. Total: %total_count.", $completed, array("total_count" => $total_count));
         if ($completed < $total_count) {
             $task->percent_complete = (int) (100 * ($completed + count($ignored)) / $total_count);
         } else {
             $task->percent_complete = 100;
         }
         $task->set("completed", $completed);
         $task->set("ignored", $ignored);
         if ($task->percent_complete == 100) {
             $task->done = true;
             $task->state = "success";
             batch::stop();
             site_status::clear("graphics_dirty");
         }
     } catch (Exception $e) {
         Kohana_Log::add("error", (string) $e);
         $task->done = true;
         $task->state = "error";
         $task->status = $e->getMessage();
         $errors[] = (string) $e;
     }
     if ($errors) {
         $task->log($errors);
     }
 }
 public function create($id)
 {
     $album = ORM::factory("item", $id);
     access::required("view", $album);
     access::required("add", $album);
     access::verify_csrf();
     $form = embed_videos::get_add_form($album);
     $temp_filename = "";
     // TODO: Add admin page options for these.
     $maxwidth = 650;
     $maxheight = 480;
     // Yes, this is a mess.
     $youtubeUrlPattern = "youtube";
     $youtubeThumbnailUrl = "http://img.youtube.com/vi/";
     $vimeoUrlPattern = "vimeo.com";
     // End mess
     batch::start();
     try {
         $valid = $form->validate();
         if ($form->add_embedded_video->inputs['video_url']->value != "") {
             $title = $form->add_embedded_video->inputs['title']->value;
             $description = $form->add_embedded_video->inputs['description']->value;
             $valid_url = false;
             $embedded_video = ORM::factory("embedded_video");
             $item = ORM::factory("item");
             $item->type = "photo";
             $url = $form->add_embedded_video->inputs['video_url']->value;
             if (preg_match("/{$youtubeUrlPattern}/", $url)) {
                 $video_id = 0;
                 if (preg_match("/watch\\?v=(.*?)(&\\S+=\\S+)/", $url, $matches)) {
                     $video_id = $matches[1];
                 } else {
                     if (preg_match("/watch\\?v=(.*)/", $url, $matches)) {
                         $video_id = $matches[1];
                     } else {
                         if (preg_match("/v\\/(.*)/", $url, $matches)) {
                             $video_id = $matches[1];
                         }
                     }
                 }
                 if ($video_id) {
                     $video_id = $matches[1];
                     $embedded_video->embed_code = '<iframe class="youtube-player" type="text/html" width="' . $maxwidth . '" height="' . $maxheight . '" src="http://www.youtube.com/embed/' . $video_id . '" frameborder="0"></iframe>';
                     $embedded_video->source = "YouTube";
                     $content = file_get_contents("http://img.youtube.com/vi/" . $video_id . "/0.jpg");
                     $itemname = "youtube_" . $video_id . ".jpg";
                     $temp_filename = VARPATH . "tmp/{$itemname}";
                     if ($content) {
                         $valid_url = true;
                         $sxml = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/{$video_id}");
                         if ($sxml) {
                             if ($title == '') {
                                 $title = (string) $sxml->title;
                             }
                             if ($description == '') {
                                 $description = (string) $sxml->content;
                             }
                         }
                     }
                 }
             } else {
                 if (preg_match("/{$vimeoUrlPattern}/", $url)) {
                     if (preg_match("/{$vimeoUrlPattern}\\/(.*)/", $url, $matches)) {
                         $video_id = $matches[1];
                         if ($video_id) {
                             $sxml = simplexml_load_file("http://vimeo.com/api/v2/video/{$video_id}.xml");
                             if ($sxml) {
                                 if ($title == '') {
                                     $title = (string) $sxml->video->title;
                                 }
                                 if ($description == '') {
                                     $description = strip_tags((string) $sxml->video->description);
                                 }
                                 $embedded_video->source = "Vimeo";
                                 $content = file_get_contents((string) $sxml->video->thumbnail_large);
                                 $itemname = "vimeo_" . $video_id . ".jpg";
                                 $temp_filename = VARPATH . "tmp/{$itemname}";
                                 $width = min((int) $sxml->video->width, $maxwidth);
                                 $height = min((int) $sxml->video->height, $maxheight);
                                 $embedded_video->embed_code = '<iframe src="http://player.vimeo.com/video/' . $video_id . '" width="' . (string) $width . '" height="' . (string) $height . '" frameborder="0"></iframe>';
                                 $valid_url = true;
                             }
                         }
                     }
                 }
             }
             //$item->validate();
             //$content = file_get_contents("http://img.youtube.com/vi/" . $form->add_embedded_video->inputs['name']->value . "/0.jpg");
             if ($valid_url) {
                 $file = fopen($temp_filename, "wb");
                 fwrite($file, $content);
                 fclose($file);
                 gallery_graphics::composite($temp_filename, $temp_filename, array("file" => "modules/embed_videos/images/embed_video_icon.png", "position" => "center", "transparency" => 95));
                 $item->set_data_file($temp_filename);
                 $item->name = basename($itemname);
                 $item->title = $title;
                 $item->parent_id = $album->id;
                 $item->description = $description;
                 $item->slug = $form->add_embedded_video->inputs['slug']->value;
                 $path_info = @pathinfo($temp_filename);
                 $item->save();
                 $embedded_video->item_id = $item->id;
                 $embedded_video->validate();
                 $embedded_video->save();
                 log::success("content", t("Added a embedded video"), html::anchor("embeds/{$item->id}", t("view video")));
                 module::event("add_event_form_completed", $item, $form);
             } else {
                 $form->add_embedded_video->inputs['video_url']->add_error('invalid_id', 1);
                 $valid = false;
             }
         } else {
             $form->add_embedded_video->inputs['video_url']->add_error('invalid_id', 1);
             $valid = false;
         }
     } catch (Exception $e) {
         // Lame error handling for now. Just record the exception and move on
         Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString());
         // Ugh. I hate to use instanceof, But this beats catching the exception separately since
         // we mostly want to treat it the same way as all other exceptions
         if ($e instanceof ORM_Validation_Exception) {
             Kohana_Log::add("error", "Validation errors: " . print_r($e->validation->errors(), 1));
             foreach ($e->validation->errors() as $key => $error) {
                 $form->add_embed->inputs[$key]->add_error($error, 1);
             }
             $valid = false;
         }
         if (file_exists($temp_filename)) {
             unlink($temp_filename);
         }
     }
     if (file_exists($temp_filename)) {
         unlink($temp_filename);
     }
     batch::stop();
     if ($valid) {
         //print json_encode(array("result" => "success"));
         json::reply(array("result" => "success", "location" => $item->url()));
     } else {
         //json::reply(array("result" => "error", "form" => (string)$form));
         print $form;
     }
 }
Beispiel #12
0
 public function delete($id)
 {
     access::verify_csrf();
     $item = model_cache::get("item", $id);
     access::required("view", $item);
     access::required("edit", $item);
     if ($item->is_album()) {
         $msg = t("Deleted album <b>%title</b>", array("title" => html::purify($item->title)));
     } else {
         $msg = t("Deleted photo <b>%title</b>", array("title" => html::purify($item->title)));
     }
     $redirect = $item->parent();
     // redirect to this item, if current item was deleted
     if ($item->is_album()) {
         // Album delete will trigger deletes for all children.  Do this in a batch so that we can be
         // smart about notifications, album cover updates, etc.
         batch::start();
         $item->delete();
         batch::stop();
     } else {
         $where = array(array("type", "!=", "album"));
         // evaluate redirect item before delete of current item
         $position = item::get_position($item, $where);
         if ($position > 1) {
             list($previous_item, $ignore, $next_item) = $item->parent()->viewable()->children(3, $position - 2, $where);
         } else {
             $previous_item = null;
             list($next_item) = $item->parent()->viewable()->children(1, $position, $where);
         }
         if ($next_item) {
             $redirect = $next_item;
         } else {
             if ($previous_item) {
                 $redirect = $previous_item;
             }
         }
         $item->delete();
     }
     message::success($msg);
     $from_id = Input::instance()->get("from_id");
     if (Input::instance()->get("page_type") == "collection" && $from_id != $id) {
         json::reply(array("result" => "success", "reload" => 1));
     } else {
         json::reply(array("result" => "success", "location" => $redirect->url()));
     }
 }
Beispiel #13
0
 public function add($id)
 {
     $album = ORM::factory("item", $id);
     access::required("view", $album);
     access::required("add", $album);
     access::verify_csrf();
     $form = $this->_get_add_form($album);
     if ($form->validate()) {
         batch::start();
         $count = 0;
         $added_a_movie = false;
         $added_a_photo = false;
         foreach (array("file1", "file2", "file3") as $key) {
             if ($form->add_photos->{$key}->value == "") {
                 continue;
             }
             try {
                 $temp_filename = $form->add_photos->{$key}->value;
                 $item = ORM::factory("item");
                 $item->name = basename($temp_filename);
                 $item->title = item::convert_filename_to_title($item->name);
                 $item->parent_id = $album->id;
                 $item->set_data_file($temp_filename);
                 $path_info = @pathinfo($temp_filename);
                 if (array_key_exists("extension", $path_info) && in_array(strtolower($path_info["extension"]), array("flv", "mp4", "m4v"))) {
                     $item->type = "movie";
                     $item->save();
                     $added_a_movie = true;
                     log::success("content", t("Added a movie"), html::anchor("movies/{$item->id}", t("view movie")));
                 } else {
                     $item->type = "photo";
                     $item->save();
                     $added_a_photo = true;
                     log::success("content", t("Added a photo"), html::anchor("photos/{$item->id}", t("view photo")));
                 }
                 $count++;
                 module::event("add_photos_form_completed", $item, $form);
             } catch (Exception $e) {
                 // Lame error handling for now.  Just record the exception and move on
                 Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString());
                 // Ugh.  I hate to use instanceof, But this beats catching the exception separately since
                 // we mostly want to treat it the same way as all other exceptions
                 if ($e instanceof ORM_Validation_Exception) {
                     Kohana_Log::add("error", "Validation errors: " . print_r($e->validation->errors(), 1));
                 }
             }
             if (file_exists($temp_filename)) {
                 unlink($temp_filename);
             }
         }
         batch::stop();
         if ($count) {
             if ($added_a_photo && $added_a_movie) {
                 message::success(t("Added %count photos and movies", array("count" => $count)));
             } else {
                 if ($added_a_photo) {
                     message::success(t2("Added one photo", "Added %count photos", $count));
                 } else {
                     message::success(t2("Added one movie", "Added %count movies", $count));
                 }
             }
         }
         json::reply(array("result" => "success"));
     } else {
         json::reply(array("result" => "error", "html" => (string) $form));
     }
     // Override the application/json mime type.  The dialog based HTML uploader uses an iframe to
     // buffer the reply, and on some browsers (Firefox 3.6) it does not know what to do with the
     // JSON that it gets back so it puts up a dialog asking the user what to do with it.  So force
     // the encoding type back to HTML for the iframe.
     // See: http://jquery.malsup.com/form/#file-upload
     header("Content-Type: text/html; charset=" . Kohana::CHARSET);
 }
Beispiel #14
0
 public function finish()
 {
     access::verify_csrf();
     batch::stop();
     json::reply(array("result" => "success"));
 }
Beispiel #15
0
 public function finish()
 {
     access::verify_csrf();
     batch::stop();
     print json_encode(array("result" => "success"));
 }
 public function run_task($task_id)
 {
     access::verify_csrf();
     try {
         $task = task::run($task_id);
     } catch (Exception $e) {
         $error_msg = $e->getMessage();
         $task->done = true;
     }
     if ($task->done) {
         batch::stop();
         $context = unserialize($task->context);
         switch ($task->state) {
             case "success":
                 message::success($context["success_msg"]);
                 break;
             case "error":
                 message::success(empty($error_msg) ? $context["error_msg"] : $error_msg);
                 break;
         }
         print json_encode(array("result" => "success", "task" => $task->as_array()));
     } else {
         print json_encode(array("result" => "in_progress", "task" => $task->as_array()));
     }
 }
Beispiel #17
0
 public function pause($id, $task_id)
 {
     if (!user::active()->admin) {
         access::forbidden();
     }
     access::verify_csrf();
     $task = ORM::factory("task", $task_id);
     if (!$task->loaded || $task->owner_id != user::active()->id) {
         access::forbidden();
     }
     message::warning(t("Add from server was cancelled prior to completion"));
     batch::stop();
     print json_encode(array("result" => "success"));
 }
Beispiel #18
0
 function cancelTask($task_id)
 {
     access::verify_csrf();
     $task = ORM::factory("task", $task_id);
     if (!$task->loaded || $task->owner_id != user::active()->id) {
         access::forbidden();
     }
     if (!$task->done) {
         $task->done = 1;
         $task->state = "cancelled";
         $type = $task->get("type");
         switch ($type) {
             case "move":
                 $task->status = t("Move to album was cancelled prior to completion");
                 break;
             case "rearrange":
                 $task->status = t("Rearrange album was cancelled prior to completion");
             case "rotateCcw":
             case "rotateCw":
                 $task->status = t("Rotation was cancelled prior to completion");
                 break;
         }
         $task->save();
     }
     batch::stop();
     print json_encode(array("result" => "success", "task" => array("id" => $task->id, "percent_complete" => $task->percent_complete, "status" => $task->status, "state" => $task->state, "done" => $task->done)));
 }
Beispiel #19
0
 /**
  * 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");
             $paths = unserialize(module::get_var("videos", "authorized_paths"));
             while ($queue && microtime(true) - $start < 0.5) {
                 list($file, $parent_entry_id) = array_shift($queue);
                 // Ignore the staging directories as directories to be imported.
                 if (empty($paths[$file])) {
                     $entry = ORM::factory("videos_file");
                     $entry->task_id = $task->id;
                     $entry->file = $file;
                     $entry->parent_id = $parent_entry_id;
                     $entry->save();
                     $entry_id = $entry->id;
                 } else {
                     $entry_id = null;
                 }
                 $file = preg_quote($file);
                 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", "m4v")) &&
                         if (in_array($ext, unserialize(module::get_var("videos", "allowed_extensions"))) && filesize($child) > 0) {
                             $child_entry = ORM::factory("videos_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", ORM::factory("videos_file")->where("task_id", "=", $task->id)->count_all());
             if (!$queue) {
                 $task->set("mode", "add-files");
                 $task->set("total_files", ORM::factory("videos_file")->where("task_id", "=", $task->id)->count_all());
                 $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("videos_file")->where("task_id", "=", $task->id)->where("item_id", "IS", null)->order_by("id", "ASC")->limit(10)->find_all();
             if ($entries->count() == 0) {
                 // Out of entries, we're done.
                 $task->set("mode", "done");
             }
             $owner_id = identity::active_user()->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("videos_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 = ORM::factory("item");
                     $album->type = "album";
                     $album->parent_id = $parent->id;
                     $album->name = $name;
                     $album->title = $title;
                     $album->owner_id = $owner_id;
                     $album->save();
                     $entry->item_id = $album->id;
                 } else {
                     try {
                         $extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
                         if (in_array($extension, unserialize(module::get_var("videos", "allowed_extensions")))) {
                             $movie = ORM::factory("item");
                             $movie->type = "movie";
                             $movie->parent_id = $parent->id;
                             $movie->set_data_file($entry->file);
                             $movie->name = $name;
                             $movie->title = $title;
                             $movie->owner_id = $owner_id;
                             $movie->save();
                             $entry->item_id = $movie->id;
                             $items_video = ORM::factory("items_video");
                             $items_video->item_id = $movie->id;
                             $items_video->save();
                             if (file_exists($entry->file . ".flv")) {
                                 copy($entry->file . ".flv", $movie->resize_path() . ".flv");
                                 list($vid_width, $vid_height, $mime_type) = movie::get_file_metadata($entry->file . ".flv");
                                 $movie->height = $vid_height;
                                 $movie->width = $vid_width;
                                 $movie->save();
                             }
                         } 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}");
                         }
                     } catch (Exception $e) {
                         // This can happen if a photo file is invalid, like a BMP masquerading as a .jpg
                         $entry->item_id = 0;
                         $task->log("Skipping invalid file: {$entry->file}");
                     }
                 }
                 $completed_files++;
                 $entry->save();
             }
             $task->set("completed_files", $completed_files);
             $task->status = t("Adding photos / albums (%completed of %total)", array("completed" => $completed_files, "total" => $total_files));
             $task->percent_complete = $total_files ? 10 + 100 * ($completed_files / $total_files) : 100;
             break;
         case "done":
             batch::stop();
             $task->done = true;
             $task->state = "success";
             $task->percent_complete = 100;
             db::build()->delete("videos_files")->where("task_id", "=", $task->id)->execute();
             message::info(t2("Successfully added one file", "Successfully added %count files", $task->get("completed_files")));
     }
 }