diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-07-02 11:23:40 -0700 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-07-02 11:23:40 -0700 |
commit | e5b6193b26cf0f8509a98f7913a1d87fa354da05 (patch) | |
tree | 232a2dacd29acae22795234383140550415dfa63 /modules/server_add/controllers | |
parent | 495c76f729372e09f7511967c63948e36303e3d7 (diff) |
Partial pass of server_add cleanup. It's broken at this stage since
I've redone the browsing code but I have not implemented the adding
code.
1) Rename index() to browse() since index is too generic.
2) Simplify the data that we pass to _dialog and _tree
3) Change _tree to return list items only, so that the outer dialog
can be a <ul> for consistency.
4) Simplify the data structures so that we're not tracking checked vs.
unchecked status in the PHP code, it's all done in jquery where we
can do it with just a line or two of JS
5) use glob() which pretty much entirely replaces _get_children
Diffstat (limited to 'modules/server_add/controllers')
-rw-r--r-- | modules/server_add/controllers/server_add.php | 103 |
1 files changed, 42 insertions, 61 deletions
diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 4cd3cec7..45ec0a2e 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -18,26 +18,38 @@ * 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(); } + + $paths = unserialize(module::get_var("server_add", "authorized_paths")); + foreach (array_keys($paths) as $path) { + $files[$path] = basename($path); + } + $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; + } - $tree = new View("server_add_tree.html"); - $tree->data = array(); - $tree->checked = false; - $tree->tree_id = "tree_$id"; - foreach (array_keys($paths) as $path) { - $tree->data[$path] = array("path" => $path, "is_dir" => true); + private function _validate_path($path) { + if (!is_readable($path) || is_link($path)) { + throw new Exception("@todo BAD_PATH"); } - $view->tree = $tree->__toString(); - print $view; + + $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() { @@ -45,31 +57,32 @@ class Server_Add_Controller extends Controller { 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 (empty($path_valid)) { - throw new Exception("@todo BAD_PATH"); - } - if (!is_readable($path) || is_link($path)) { - kohana::show_404(); - } + if (!is_dir($file)) { + $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); + if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4"))) { + continue; + } + } - $tree = new View("server_add_tree.html"); - $tree->data = $this->_get_children($path); - $tree->checked = $checked; - $tree->tree_id = "tree_" . md5($path); + $tree->files[$file] = basename($file); + } print $tree; } + /* ================================================================================ */ + function start($id) { if (!user::active()->admin) { access::forbidden(); @@ -237,36 +250,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 |