diff options
Diffstat (limited to 'modules/server_add/controllers/server_add.php')
-rw-r--r-- | modules/server_add/controllers/server_add.php | 126 |
1 files changed, 64 insertions, 62 deletions
diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 05ea5058..e2b1b01a 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -18,60 +18,94 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Server_Add_Controller extends Controller { - public function index($id) { - $paths = unserialize(module::get_var("server_add", "authorized_paths")); - + public function browse($id) { if (!user::active()->admin) { access::forbidden(); } - $item = ORM::factory("item", $id); - $view = new View("server_add_tree_dialog.html"); - $view->action = url::abs_site("__ARGS__/{$id}__TASK_ID__?csrf=" . access::csrf_token()); - $view->parents = $item->parents(); - $view->album_title = $item->title; - $tree = new View("server_add_tree.html"); - $tree->data = array(); - $tree->checked = false; - $tree->tree_id = "tree_$id"; + $paths = unserialize(module::get_var("server_add", "authorized_paths")); foreach (array_keys($paths) as $path) { - $tree->data[$path] = array("path" => $path, "is_dir" => true); + $files[$path] = basename($path); } - $view->tree = $tree->__toString(); + + $item = ORM::factory("item", $id); + $view = new View("server_add_tree_dialog.html"); + $view->item = $item; + $view->tree = new View("server_add_tree.html"); + $view->tree->files = $files; print $view; } + private function _validate_path($path) { + if (!is_readable($path) || is_link($path)) { + throw new Exception("@todo BAD_PATH"); + } + + $authorized_paths = unserialize(module::get_var("server_add", "authorized_paths")); + foreach (array_keys($authorized_paths) as $valid_path) { + if (strpos($path, $valid_path) === 0) { + return; + } + } + + throw new Exception("@todo BAD_PATH"); + } + public function children() { if (!user::active()->admin) { access::forbidden(); } - $paths = unserialize(module::get_var("server_add", "authorized_paths")); - $path_valid = false; - $path = $this->input->post("path"); - $checked = $this->input->post("checked") == "true"; + $path = $this->input->get("path"); + $this->_validate_path($path); - foreach (array_keys($paths) as $valid_path) { - if ($path_valid = strpos($path, $valid_path) === 0) { - break; + $tree = new View("server_add_tree.html"); + $tree->files = array(); + $tree->tree_id = substr(md5($path), 10); + + foreach (glob("$path/*") as $file) { + if (!is_readable($file)) { + continue; + } + + if (!is_dir($file)) { + $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); + if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4"))) { + continue; + } } + + $tree->files[$file] = basename($file); } - if (empty($path_valid)) { - throw new Exception("@todo BAD_PATH"); + print $tree; + } + + public function add() { + if (!user::active()->admin) { + access::forbidden(); } + access::verify_csrf(); - if (!is_readable($path) || is_link($path)) { - kohana::show_404(); + $authorized_paths = unserialize(module::get_var("server_add", "authorized_paths")); + + // The paths we receive are full pathnames. Convert that into a tree structure to save space + // in our task. + foreach (Input::instance()->post("path") as $path) { + if (is_dir($path)) { + $dirs[$path] = array(); + } else if (is_file($path)) { + $dir = dirname($path); + $file = basename($path); + $dirs[$dir][] = $file; + } } - $tree = new View("server_add_tree.html"); - $tree->data = $this->_get_children($path); - $tree->checked = $checked; - $tree->tree_id = "tree_" . md5($path); - print $tree; + Kohana::log("alert",print_r($dirs,1)); } + /* ================================================================================ */ + function start($id) { if (!user::active()->admin) { access::forbidden(); @@ -239,36 +273,4 @@ class Server_Add_Controller extends Controller { return $count; } - - private function _get_children($path) { - $directory_list = $file_list = array(); - $files = new DirectoryIterator($path); - foreach ($files as $file) { - if ($file->isDot() || $file->isLink()) { - continue; - } - $filename = $file->getFilename(); - if ($filename[0] != ".") { - if ($file->isDir()) { - $directory_list[$filename] = array("path" => $file->getPathname(), "is_dir" => true); - } else { - $extension = strtolower(substr(strrchr($filename, '.'), 1)); - if ($file->isReadable() && - in_array($extension, array("gif", "jpeg", "jpg", "png", "flv", "mp4"))) { - $file_list[$filename] = array("path" => $file->getPathname(), "is_dir" => false); - } - } - } - } - - ksort($directory_list); - ksort($file_list); - - // We can't use array_merge here because if a file name is numeric, it will - // get renumbered, so lets do it ourselves - foreach ($file_list as $file => $fileinfo) { - $directory_list[$file] = $fileinfo; - } - return $directory_list; - } }
\ No newline at end of file |