public function postAction() { $db = $this->_helper->database->getAdapter(); // We use POST to fork buckets. Rather non-standard, perhaps move into its // own Controller? // We use the posted bucket rather than what might be in the database, so any // unsaved changes are retained. $bucket = $this->gatherBucketFromRequest(); // We always fork into the current token's namespace, not the one the bucket // is coming from. That way we can load another user's bucket and fork it // into our own namespace. $token = $this->getRequest()->getParam("token"); $namespace = isset($token) ? BucketUtils::getNamespaceForToken($db, $token) : 'public'; // if (!isset($token)) { // $namespace = 'public'; // } else { // $namespace = BucketUtils::getNamespaceForToken($db, $token); // } // Come up with a new id for the bucket $id = BucketUtils::generateId($db, $namespace); $version = 0; self::$logger->info("forking incoming bucket " . $bucket['namespace'] . "/" . $bucket['id'] . "/" . $bucket['contents']['version'] . " to {$namespace}/{$id}/{$version}"); $db->insert('bucket', array('namespace' => $namespace, 'id' => $id, 'name' => $bucket['name'], 'description' => $bucket['description'], 'latest_version' => $version)); // Merge arrays so our new info overwrites the old $db->insert('bucket_version', array_merge($bucket['contents'], array('bucket_namespace' => $namespace, 'bucket_id' => $id, 'version' => $version))); /* TODO fork files and resources table rows too? */ $response = array("success" => true, "namespace" => $namespace, "id" => $id, "version" => $version); echo Zend_Json::encode($response); }
public function postAction() { $this->_helper->viewRenderer->setNoRender(true); $db = $this->_helper->database->getAdapter(); // retrieve the guts of the bucket $name = $this->getRequest()->getParam("name"); $description = $this->getRequest()->getParam("description"); $dojo_version = $this->getRequest()->getParam("dojo_version"); $dj_config = $this->getRequest()->getParam("dj_config"); $html = $this->getRequest()->getParam("html"); $javascript = $this->getRequest()->getParam("javascript"); $css = $this->getRequest()->getParam("css"); $layers = $this->getRequest()->getParam("layers"); // prepare the guts of the bucket, used in each kind of insert/update later $bucket_contents = array('dojo_version' => $dojo_version, 'content_html' => $html, 'content_css' => $css, 'content_js' => $javascript, 'dj_config' => $dj_config, 'layers' => $layers); $response = array(); try { // We may not have been passed any identifying info $token = $this->getRequest()->getParam("token"); $namespace = $this->getRequest()->getParam("namespace"); $id = $this->getRequest()->getParam("id"); $version = $this->getRequest()->getParam("version"); self::$logger->info("postAction namespace ({$namespace}) id ({$id}) version ({$version})"); if (isset($_REQUEST['save'])) { // sanitize $namespace - may be empty for new buckets if (!isset($namespace) || $namespace == '' || $_REQUEST['save'] === 'fork') { self::$logger->debug("namespace not set or forking. Check token ({$token})"); if (isset($token) && $token != '') { $token_namespace = BucketUtils::getNamespaceForToken($db, $token); if ($token_namespace) { $namespace = $token_namespace; } } else { self::$logger->info("namespace not provided, default to 'public'"); $namespace = 'public'; } } SecurityUtils::verifyToken($db, $token, $namespace); if (!isset($id) || $id == '' || $_REQUEST['save'] === 'fork') { self::$logger->info("id not provided or forking, generating new random id"); $id = BucketUtils::generateId($db, $namespace); } if (!isset($version) || $_REQUEST['save'] === 'fork') { self::$logger->info("version not provided or forking, default to 0"); $version = 0; } if ($_REQUEST['save'] == 'new_version') { $version++; } // The bucket may not yet exist self::$logger->info("Running bucket count for namespace ({$namespace}) id ({$id})..."); $select = $db->select()->from('bucket', "COUNT(*) as cc")->where('namespace = ?', $namespace)->where('id = ?', $id); if ($db->fetchRow($select)->cc == 0) { // sandbox does not yet exist, create it self::$logger->info('bucket does not yet exist'); // Create the bucket $db->insert('bucket', array('namespace' => $namespace, 'id' => $id, 'name' => $name, 'description' => $description, 'latest_version' => $version)); } self::$logger->info("Running bucket_version count for namespace ({$namespace}) id ({$id}) version ({$version})..."); $select = $db->select()->from('bucket_version', "COUNT(*) as cc")->where('bucket_namespace = ?', $namespace)->where('bucket_id = ?', $id)->where('version = ?', $version); if ($db->fetchRow($select)->cc == 0) { // Create the initial version self::$logger->info("insert bucket_version"); $db->insert('bucket_version', array_merge(array('bucket_namespace' => $namespace, 'bucket_id' => $id, 'version' => $version), $bucket_contents)); } else { self::$logger->info("update bucket_version ({$version})"); $db->update('bucket_version', $bucket_contents, array('bucket_namespace = ?' => $namespace, 'bucket_id = ?' => $id, 'version = ?' => $version)); } // inform the caller of the new identity $response = array("success" => true, "namespace" => $namespace, "id" => $id, "version" => $version); } else { if (isset($_REQUEST['save'])) { // save_as_new was false, so overwrite existing version. $db->update('bucket_version', array_merge(array('bucket_namespace' => $namespace, 'bucket_id' => $id, 'version' => $version), $bucket_contents)); $response = array("success" => true, "namespace" => $namespace, "id" => $id, "version" => $version); } } $session = new Zend_Session_Namespace("runIframe"); $session->name = $name; $session->content_html = $bucket_contents['content_html']; $session->content_js = $bucket_contents['content_js']; $session->content_css = $bucket_contents['content_css']; $session->dojo_version = $bucket_contents['dojo_version']; $session->dj_config = $bucket_contents['dj_config']; $session->layers = $bucket_contents['layers']; $session->session_id = rand(1, 1000000); $response['session_id'] = $session->session_id; } catch (SecurityException $e) { $response['success'] = false; $response['exception'] = 'SecurityException'; $response['message'] = $e->getMessage(); } echo Zend_Json::encode($response); }