store->isCollection($key)) return $driver->show403("Cannot write directly to a collection."); // TODO: Permissions in OutlineStore object? // Attempt to fetch an existing document at this key. list($orig_doc, $orig_root_node) = fetchOutlineWithSelector($key, $selector); // Ensure selected suboutline exists, if selector present. if ($selector && !$orig_doc) { return $driver->showErrorTextResponse(400, "Document not found, or suboutline selector invalid for PUT on new document."); } // Parse and sanitize new incoming data, grab the root node. list($new_doc, $new_root_node) = fetchRequestDataAsOutline(); // Without a selector, this is a request to overwrite entire doc if (!$selector) { // New document data without selector must be 'html'. if ($new_root_node->tagName != 'html') { return $driver->showErrorTextResponse(415, "Document root node must be 'html', found '".$new_root_node->tagName."'"); } // Attempt to store the sanitized document. if (FALSE !== $driver->store->put($key, $new_doc->saveXML())) { header("Location: ".$driver->config['BASE_URL'].'/outlines'.$key); return; } else { return $driver->show403('Could not save'); } } else { // On suboutline update, ensure the original and new root nodes are // the same type. if ($orig_root_node->tagName != $new_root_node->tagName) { return $driver->showErrorTextResponse(415, "Node name mismatch on suboutline update.\n" . "Original: ".$orig_root_node->tagName."\n" . " New: ".$new_root_node->tagName); return; } // Replace the original doc's node with the new one incoming. $new_import_node = $orig_doc->importNode($new_root_node, TRUE); $orig_root_node->parentNode->replaceChild($new_import_node, $orig_root_node); // Attempt to store the modified document. if (FALSE !== $driver->store->put($key, $orig_doc->saveXML())) { header("Location: ".$driver->config['BASE_URL'].'/outlines'.$key.";".$selector); return; } else { return $driver->show403(); } } ?>