0
class midcom_core_controllers_snippets
0
+ private $snippetdir = null;
0
+ private $snippet = null;
0
+ private $object_path = '';
0
public function __construct($instance)
0
$this->configuration = $_MIDCOM->configuration;
0
- private function get_snippetdir_children($snippetdir_id
, $uri_prefix)
0
+ private function get_snippetdir_children($snippetdir_id
)
0
// Load children for PROPFIND purposes
0
@@ -30,19 +34,22 @@ class midcom_core_controllers_snippets
0
$mc->add_value_property('description');
0
$snippetdirs = $mc->list_keys();
0
-
foreach ($snippetdirs as $name => $array)
0
+
if (is_array($snippetdirs))
0
+
foreach ($snippetdirs as $name => $array)
0
+ 'uri' => "{$_MIDCOM->context->prefix}__snippets{$this->object_path}/{$name}/", // FIXME: dispatcher::generate_url
0
+ 'mimetype' => 'httpd/unix-directory',
0
+ 'resource' => 'collection',
0
- 'uri' => "{$uri_prefix}{$name}/", // FIXME: dispatcher::generate_url
0
- 'mimetype' => 'httpd/unix-directory',
0
- 'resource' => 'collection',
0
@@ -50,57 +57,214 @@ class midcom_core_controllers_snippets
0
$qb->add_constraint('up', '=', $snippetdir_id);
0
$qb->add_constraint('sitegroup', '=', $_MIDGARD['sitegroup']);
0
$qb->add_constraint('name', '<>', '');
0
- $snippets = $qb->execute();
0
- foreach ($snippets as $snippet)
0
- 'uri' => "{$uri_prefix}{$snippet->name}/", // FIXME: dispatcher::generate_url
0
- 'title' => $snippet->name,
0
- 'mimetype' => 'text/plain',
0
- 'size' => $snippet->metadata->size,
0
- 'revised' => $snippet->metadata->revised,
0
+ $snippets = $qb->execute();
0
+ if (is_array($snippets))
0
+ foreach ($snippets as $snippet)
0
+ 'uri' => "{$_MIDCOM->context->prefix}__snippets{$this->object_path}/{$snippet->name}.php", // FIXME: dispatcher::generate_url
0
+ 'title' => $snippet->name,
0
+ 'mimetype' => 'text/plain',
0
+ 'size' => $snippet->metadata->size,
0
+ 'revised' => $snippet->metadata->revised,
0
- public function action_webdav($route_id, &$data, $args)
0
+ private function get_snippetdir($object_path)
0
+ $snippetdir = new midgard_snippetdir();
0
+ $snippetdir->get_by_path($object_path);
0
+ catch (midgard_error_exception $e)
0
+ $this->snippetdir = $snippetdir;
0
+ private function get_snippet($object_path)
0
+ $snippet = new midgard_snippet();
0
+ $snippet->get_by_path(str_replace('.php', '', $object_path));
0
+ catch (midgard_error_exception $e)
0
+ $this->snippet = $snippet;
0
+ private function handle_propfind($route_id, &$data)
0
if ($route_id == 'snippets_root')
0
- $data['children'] = $this->get_snippetdir_children(0, "{$_MIDCOM->context->prefix}__snippets/");
0
+ $data['children'] = $this->get_snippetdir_children(0);
0
+ if (!$this->get_snippetdir($this->object_path))
0
- // First we need to load the object
0
- $object_path = '/' . implode('/', $args['variable_arguments']);
0
+ throw new midcom_exception_notfound("Snippetdir {$this->object_path} not found");
0
+ $data['children'] = $this->get_snippetdir_children($this->snippetdir->id);
0
+ private function handle_put($route_id, &$data)
0
+ if ( $route_id == 'snippets_root'
0
+ || $this->get_snippetdir($this->object_path))
0
+ throw new midcom_exception_httperror("PUT to snippetdir not allowed", 405);
0
+ if (!$this->get_snippet($this->object_path))
0
+ $parent_path = dirname($this->object_path);
0
+ if (!$this->get_snippetdir($parent_path))
0
- $snippetdir = new midgard_snippetdir();
0
- $snippetdir->get_by_path($object_path);
0
+ throw new midcom_exception_notfound("Snippetdir {$parent_path} not found");
0
- catch (midgard_error_exception $e)
0
+ $_MIDCOM->authorization->require_do('midgard:create', $this->snippetdir);
0
+ $new_snippet = new midgard_snippet();
0
+ $new_snippet->up = $this->snippetdir->id;
0
+ $new_snippet->name = basename(str_replace('.php', '', $this->object_path));
0
+ $new_snippet->create();
0
+ $new_snippet = $this->snippet;
0
+ $new_snippet->code = file_get_contents('php://input');
0
+ $new_snippet->update();
0
+ private function handle_mkcol($route_id, &$data)
0
+ if ( $this->get_snippet
0
+ throw new midcom_exception_httperror("MKCOL not allowed", 405);
0
+ $parent_path = dirname($this->object_path);
0
+ if ( $parent_path != '/'
0
+ && !$this->get_snippetdir($parent_path))
0
+ throw new midcom_exception_notfound("Snippetdir {$parent_path} not found");
0
+ $new_snippetdir = new midgard_snippetdir();
0
+ $new_snippetdir->name = basename($this->object_path);
0
+ if ($parent_path != '/')
0
+ $_MIDCOM->authorization->require_do('midgard:create', $this->snippetdir);
0
+ $new_snippetdir->up = $this->snippetdir->id;
0
+ $new_snippetdir->create();
0
+ private function handle_move($route_id, &$data)
0
+ if (!isset($data['dest']))
0
+ throw new Exception("No destination defined");
0
+ $snippets_prefix = '__snippets';
0
+ if (substr($data['dest'], 0, strlen($snippets_prefix)) != $snippets_prefix)
0
+ throw new Exception("Invalid destination {$data['dest']}");
0
+ $destination_object_path = dirname(substr($data['dest'], strlen($snippets_prefix)));
0
+ if (!$this->get_snippetdir($destination_object_path))
0
+ throw new Exception("No snippetdir {$destination_object_path} found");
0
+ $destination_snippetdir = $this->snippetdir;
0
+ if (!$this->get_snippetdir($this->object_path))
0
+ // Possibly moving snippets instead
0
+ if (!$this->get_snippet($this->object_path))
0
+ throw new midcom_exception_notfound("Snippetdir {$this->object_path} not found");
0
+ $this->snippet->up = $destination_snippetdir->id;
0
+ $this->snippet->name = str_replace('.php', '', basename($data['dest']));
0
+ $this->snippet->update();
0
+ $this->snippetdir->up = $destination_snippetdir->id;
0
+ $this->snippetdir->name = basename($data['dest']);
0
+ $this->snippetdir->update();
0
+ public function action_webdav($route_id, &$data, $args)
0
+ if ($route_id == 'snippets')
0
+ $this->object_path = '/' . implode('/', $args['variable_arguments']);
0
+ switch ($this->dispatcher->request_method)
0
+ $this->handle_propfind($route_id, &$data);
0
+ if ($this->get_snippet($this->object_path))
0
- // This is possibly snippet instead
0
- $snippet = new midgard_snippet();
0
- $snippet->get_by_path($object_path);
0
+ $data['data'] = $this->snippet->code;
0
+ $data['mimetype'] = 'text/plain';
0
+ $data['mtime'] = strtotime($this->snippet->metadata->revised);
0
-
catch (midgard_error_exception $e)
0
+
elseif ($this->get_snippetdir($this->object_path))
0
- throw new midcom_exception_notfound("Code Snippet {$object_path} not found");
0
+ $data['mimetype'] = 'httpd/unix-directory';
0
+ $data['mtime'] = strtotime($this->snippetdir->metadata->revised);
0
- $data['children'] = $this->get_snippetdir_children($snippetdir->id, "{$_MIDCOM->context->prefix}__snippets{$object_path}/");
0
+ throw new midcom_exception_notfound("Snippetdir {$this->object_path} not found");
0
+ $this->handle_put($route_id, &$data);
0
+ $this->handle_mkcol($route_id, &$data);
0
+ $this->handle_move($route_id, &$data);
0
\ No newline at end of file
Comments
No one has commented yet.