Ejemplo n.º 1
0
 /**
  * Create a merged list of all photo and movie filename filters,
  * (e.g. "*.gif"), based on allowed extensions.
  */
 static function get_filters()
 {
     $filters = array();
     foreach (legal_file::get_extensions() as $extension) {
         array_push($filters, "*." . $extension, "*." . strtoupper($extension));
     }
     return $filters;
 }
Ejemplo n.º 2
0
 public function add_photo($id)
 {
     $album = ORM::factory("item", $id);
     access::required("view", $album);
     access::required("add", $album);
     access::verify_csrf();
     // The Flash uploader not call /start directly, so simulate it here for now.
     if (!batch::in_progress()) {
         batch::start();
     }
     $form = $this->_get_add_form($album);
     // Uploadify adds its own field to the form, so validate that separately.
     $file_validation = new Validation($_FILES);
     $file_validation->add_rules("Filedata", "upload::valid", "upload::required", "upload::type[" . implode(",", legal_file::get_extensions()) . "]");
     if ($form->validate() && $file_validation->validate()) {
         $temp_filename = upload::save("Filedata");
         Event::add("system.shutdown", create_function("", "unlink(\"{$temp_filename}\");"));
         try {
             $item = ORM::factory("item");
             $item->name = substr(basename($temp_filename), 10);
             // Skip unique identifier Kohana adds
             $item->title = item::convert_filename_to_title($item->name);
             $item->parent_id = $album->id;
             $item->set_data_file($temp_filename);
             // Remove double extensions from the filename - they'll be disallowed in the model but if
             // we don't do it here then it'll result in a failed upload.
             $item->name = legal_file::smash_extensions($item->name);
             $path_info = @pathinfo($temp_filename);
             if (array_key_exists("extension", $path_info) && in_array(strtolower($path_info["extension"]), legal_file::get_movie_extensions())) {
                 $item->type = "movie";
                 $item->save();
                 log::success("content", t("Added a movie"), html::anchor("movies/{$item->id}", t("view movie")));
             } else {
                 $item->type = "photo";
                 $item->save();
                 log::success("content", t("Added a photo"), html::anchor("photos/{$item->id}", t("view photo")));
             }
             module::event("add_photos_form_completed", $item, $form);
         } catch (Exception $e) {
             // The Flash uploader has no good way of reporting complex errors, so just keep it simple.
             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));
             }
             header("HTTP/1.1 500 Internal Server Error");
             print "ERROR: " . $e->getMessage();
             return;
         }
         print "FILEID: {$item->id}";
     } else {
         header("HTTP/1.1 400 Bad Request");
         print "ERROR: " . t("Invalid upload");
     }
 }
Ejemplo n.º 3
0
 static function cron()
 {
     $owner_id = 2;
     $debug = !empty($_SERVER['argv']) && isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == "debug";
     // Login as Admin
     $debug and print "Starting user session\n";
     $session = Session::instance();
     $session->delete("user");
     auth::login(IdentityProvider::instance()->admin_user());
     // check if some folders are still unprocessed from previous run
     $entry = ORM::factory("folder_sync_entry")->where("is_directory", "=", 1)->where("checked", "=", 0)->order_by("id", "ASC")->find();
     if (!$entry->loaded()) {
         $debug and print "Adding default folders\n";
         $paths = unserialize(module::get_var("folder_sync", "authorized_paths"));
         foreach (array_keys($paths) as $path) {
             if (folder_sync::is_valid_path($path)) {
                 $path = rtrim($path, "/");
                 $debug and print " * {$path}\n";
                 $entry = ORM::factory("folder_sync_entry")->where("is_directory", "=", 1)->where("path", "=", $path)->find();
                 if ($entry && $entry->loaded()) {
                     $entry->checked = 0;
                     $entry->save();
                 } else {
                     $entry = ORM::factory("folder_sync_entry");
                     $entry->path = $path;
                     $entry->is_directory = 1;
                     $entry->parent_id = null;
                     $entry->item_id = module::get_var("folder_sync", "destination_album_id", 1);
                     $entry->md5 = '';
                     $entry->save();
                 }
             }
         }
     }
     // Scan and add files
     $debug and print "Starting the loop\n";
     $done = false;
     $limit = 500;
     while (!$done && $limit > 0) {
         $debug and print "Loop started: Limit = {$limit}\n";
         $entry = ORM::factory("folder_sync_entry")->where("is_directory", "=", 1)->where("checked", "=", 0)->order_by("id", "ASC")->find();
         if ($entry->loaded()) {
             // get the parrent
             $parent = ORM::factory("item", $entry->item_id);
             if (!$parent->loaded()) {
                 $debug and print "Deleting entry #{$entry->id} pointing to missing item #{$entry->item_id}\n";
                 //$entry->delete();
                 //continue;
             }
             $debug and print "Scanning folder: {$entry->path}\n";
             $child_paths = glob(preg_quote($entry->path) . "/*");
             if (!$child_paths) {
                 $child_paths = glob("{$entry->path}/*");
             }
             foreach ($child_paths as $child_path) {
                 $name = basename($child_path);
                 $title = item::convert_filename_to_title($name);
                 $debug and print "Found {$child_path}...";
                 if (is_dir($child_path)) {
                     $debug and print "folder\n";
                     $entry_exists = ORM::factory("folder_sync_entry")->where("is_directory", "=", 1)->where("path", "=", $child_path)->find();
                     if ($entry_exists && $entry_exists->loaded()) {
                         $debug and print "Folder is already imported, marked to re-sync.\n";
                         $entry_exists->checked = 0;
                         $entry_exists->save();
                     } else {
                         $debug and print "Adding new folder.\n";
                         $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();
                         $child_entry = ORM::factory("folder_sync_entry");
                         $child_entry->path = $child_path;
                         $child_entry->parent_id = $entry->id;
                         $child_entry->item_id = $album->id;
                         $child_entry->is_directory = 1;
                         $child_entry->md5 = "";
                         $child_entry->save();
                     }
                 } else {
                     $debug and print "file\n";
                     $ext = strtolower(pathinfo($child_path, PATHINFO_EXTENSION));
                     if (!in_array($ext, legal_file::get_extensions()) || !filesize($child_path)) {
                         // Not importable, skip it.
                         $debug and print "File is incompatible. Skipping.\n";
                         continue;
                     }
                     // check if file was already imported
                     $entry_exists = ORM::factory("folder_sync_entry")->where("is_directory", "=", 0)->where("path", "=", $child_path)->find();
                     if ($entry_exists && $entry_exists->loaded()) {
                         $debug and print "Image is already imported...";
                         if (empty($entry_exists->added) || empty($entry_exists->md5) || $entry_exists->added != filemtime($child_path) || $entry_exists->md5 != md5_file($child_path)) {
                             $item = ORM::factory("item", $entry_exists->item_id);
                             if ($item->loaded()) {
                                 $item->set_data_file($child_path);
                                 $debug and print "updating.\n";
                                 try {
                                     $item->save();
                                 } catch (ORM_Validation_Exception $e) {
                                     print "Error saving the image (ID = {$item->id}) with the new data file.\n";
                                     exit;
                                 }
                             } else {
                                 $debug and print "deleting.\n";
                                 $entry_exists->delete();
                             }
                         } else {
                             $debug and print "skipping.\n";
                         }
                         // since it's an update, don't count too much towards the limit
                         $limit -= 0.25;
                     } else {
                         if (in_array($ext, legal_file::get_photo_extensions())) {
                             $debug and print "Adding new photo.\n";
                             $item = ORM::factory("item");
                             $item->type = "photo";
                             $item->parent_id = $parent->id;
                             $item->set_data_file($child_path);
                             $item->name = $name;
                             $item->title = $title;
                             $item->owner_id = $owner_id;
                             $item->save();
                         } else {
                             if (in_array($ext, legal_file::get_movie_extensions())) {
                                 $debug and print "Adding new video.\n";
                                 $item = ORM::factory("item");
                                 $item->type = "movie";
                                 $item->parent_id = $parent->id;
                                 $item->set_data_file($child_path);
                                 $item->name = $name;
                                 $item->title = $title;
                                 $item->owner_id = $owner_id;
                                 $item->save();
                             }
                         }
                         $entry_exists = ORM::factory("folder_sync_entry");
                         $entry_exists->path = $child_path;
                         $entry_exists->parent_id = $entry->id;
                         // null if the parent was a staging dir
                         $entry_exists->is_directory = 0;
                         $entry_exists->md5 = md5_file($child_path);
                         $entry_exists->added = filemtime($child_path);
                         $entry_exists->item_id = $item->id;
                         $entry_exists->save();
                         $limit--;
                     }
                 }
                 // Did we hit the limit?
                 if ($limit <= 0) {
                     $debug and print "Reached the limit. Exiting.\n";
                     exit;
                 }
             }
             // We've processed this entry unless we reached a limit.
             if ($limit > 0) {
                 $entry->checked = 1;
                 $entry->save();
             }
         } else {
             $done = true;
             $debug and print "All folders are processed. Exiting.\n";
         }
     }
     // process deletes
     if (module::get_var("folder_sync", "process_deletes", false)) {
         $entries = ORM::factory("folder_sync_entry")->order_by("id", "ASC")->find_all();
         foreach ($entries as $entry) {
             if (!file_exists($entry->path) && $entry->item_id > 1) {
                 $item = ORM::factory("item", $entry->item_id);
                 if ($item->loaded()) {
                     $item->delete();
                 }
             }
         }
     }
     exit;
 }
Ejemplo n.º 4
0
 /**
  * Sanitize a filename into something presentable as an item title
  * @param string $filename
  * @return string title
  */
 static function convert_filename_to_title($filename)
 {
     $title = strtr($filename, "_", " ");
     $exts = legal_file::get_extensions();
     foreach ($exts as $ext) {
         $title = preg_replace("/\\." . $ext . "\$/", "", $title);
     }
     $title = preg_replace("/ +/", " ", $title);
     return $title;
 }
Ejemplo n.º 5
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")));
     }
 }
Ejemplo n.º 6
0
 public function get_extensions_test()
 {
     $this->assert_equal(true, legal_file::get_extensions("jpg"));
     // photo
     $this->assert_equal(true, legal_file::get_extensions("FLV"));
     // movie
     $this->assert_equal(false, legal_file::get_extensions("php"));
     // invalid
     $this->assert_equal(false, legal_file::get_extensions("php.jpg"));
     // invalid w/ .
     // No extension returns full array
     $this->assert_equal(9, count(legal_file::get_extensions()));
 }