Example #1
0
 public function switchAction($action, $httpVars, $fileVars)
 {
     if (strpos($action, "sharelist") === false && !isset($this->accessDriver)) {
         throw new Exception("Cannot find access driver!");
     }
     if (strpos($action, "sharelist") === false && $this->accessDriver->getId() == "access.demo") {
         $errorMessage = "This is a demo, all 'write' actions are disabled!";
         if ($httpVars["sub_action"] == "delegate_repo") {
             return AJXP_XMLWriter::sendMessage(null, $errorMessage, false);
         } else {
             print $errorMessage;
         }
         return null;
     }
     switch ($action) {
         //------------------------------------
         // SHARING FILE OR FOLDER
         //------------------------------------
         case "share":
             $subAction = isset($httpVars["sub_action"]) ? $httpVars["sub_action"] : "";
             if (empty($subAction) && isset($httpVars["simple_share_type"])) {
                 $subAction = "create_minisite";
                 if (!isset($httpVars["simple_right_read"]) && !isset($httpVars["simple_right_download"])) {
                     $httpVars["simple_right_read"] = $httpVars["simple_right_download"] = "true";
                 }
             }
             $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
             $ajxpNode = new AJXP_Node($this->urlBase . $file);
             if (!file_exists($ajxpNode->getUrl())) {
                 throw new Exception("Cannot share a non-existing file: " . $ajxpNode->getUrl());
             }
             //$metadata = null;
             $newMeta = null;
             $maxdownload = abs(intval($this->getFilteredOption("FILE_MAX_DOWNLOAD", $this->repository->getId())));
             $download = isset($httpVars["downloadlimit"]) ? abs(intval($httpVars["downloadlimit"])) : 0;
             if ($maxdownload == 0) {
                 $httpVars["downloadlimit"] = $download;
             } elseif ($maxdownload > 0 && $download == 0) {
                 $httpVars["downloadlimit"] = $maxdownload;
             } else {
                 $httpVars["downloadlimit"] = min($download, $maxdownload);
             }
             $maxexpiration = abs(intval($this->getFilteredOption("FILE_MAX_EXPIRATION", $this->repository->getId())));
             $expiration = isset($httpVars["expiration"]) ? abs(intval($httpVars["expiration"])) : 0;
             if ($maxexpiration == 0) {
                 $httpVars["expiration"] = $expiration;
             } elseif ($maxexpiration > 0 && $expiration == 0) {
                 $httpVars["expiration"] = $maxexpiration;
             } else {
                 $httpVars["expiration"] = min($expiration, $maxexpiration);
             }
             $forcePassword = $this->getFilteredOption("SHARE_FORCE_PASSWORD", $this->repository->getId());
             $httpHash = null;
             $originalHash = null;
             if ($subAction == "delegate_repo") {
                 header("Content-type:text/plain");
                 $auth = $this->getAuthorization("folder", "workspace");
                 if (!$auth) {
                     print 103;
                     break;
                 }
                 $result = $this->createSharedRepository($httpVars, $this->repository, $this->accessDriver);
                 if (is_a($result, "Repository")) {
                     $newMeta = array("id" => $result->getUniqueId(), "type" => "repository");
                     $numResult = 200;
                 } else {
                     $numResult = $result;
                 }
                 print $numResult;
             } else {
                 if ($subAction == "create_minisite") {
                     header("Content-type:text/plain");
                     if (isset($httpVars["hash"]) && !empty($httpVars["hash"])) {
                         $httpHash = $httpVars["hash"];
                     }
                     if (isset($httpVars["simple_share_type"])) {
                         $httpVars["create_guest_user"] = "******";
                         if ($httpVars["simple_share_type"] == "private" && !isset($httpVars["guest_user_pass"])) {
                             throw new Exception("Please provide a guest_user_pass for private link");
                         }
                     }
                     if ($forcePassword && (isset($httpVars["create_guest_user"]) && $httpVars["create_guest_user"] == "true" && empty($httpVars["guest_user_pass"]) || isset($httpVars["guest_user_id"]) && isset($httpVars["guest_user_pass"]) && $httpVars["guest_user_pass"] == "")) {
                         $mess = ConfService::getMessages();
                         throw new Exception($mess["share_center.175"]);
                     }
                     $res = $this->createSharedMinisite($httpVars, $this->repository, $this->accessDriver);
                     if (!is_array($res)) {
                         $url = $res;
                     } else {
                         list($hash, $url) = $res;
                         $newMeta = array("id" => $hash, "type" => "minisite");
                         if ($httpHash != null && $hash != $httpHash) {
                             $originalHash = $httpHash;
                         }
                     }
                     print $url;
                 } else {
                     $data = $this->accessDriver->makePublicletOptions($file, $httpVars["password"], $httpVars["expiration"], $httpVars["downloadlimit"], $this->repository);
                     $customData = array();
                     foreach ($httpVars as $key => $value) {
                         if (substr($key, 0, strlen("PLUGINS_DATA_")) == "PLUGINS_DATA_") {
                             $customData[substr($key, strlen("PLUGINS_DATA_"))] = $value;
                         }
                     }
                     if (count($customData)) {
                         $data["PLUGINS_DATA"] = $customData;
                     }
                     list($hash, $url) = $this->writePubliclet($data, $this->accessDriver, $this->repository);
                     $newMeta = array("id" => $hash, "type" => "file");
                     if (isset($httpVars["format"]) && $httpVars["format"] == "json") {
                         header("Content-type:application/json");
                         echo json_encode(array("element_id" => $hash, "publiclet_link" => $url));
                     } else {
                         header("Content-type:text/plain");
                         echo $url;
                     }
                     flush();
                 }
             }
             if ($newMeta != null && $ajxpNode->hasMetaStore() && !$ajxpNode->isRoot()) {
                 $this->addShareInMeta($ajxpNode, $newMeta["type"], $newMeta["id"], $originalHash);
             }
             AJXP_Controller::applyHook("msg.instant", array("<reload_shared_elements/>", ConfService::getRepository()->getId()));
             // as the result can be quite small (e.g error code), make sure it's output in case of OB active.
             flush();
             break;
         case "toggle_link_watch":
             $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
             $watchValue = $httpVars["set_watch"] == "true" ? true : false;
             $folder = false;
             $shNode = new AJXP_Node($this->urlBase . $file);
             if (isset($httpVars["element_type"]) && $httpVars["element_type"] == "folder") {
                 $folder = true;
                 $node = new AJXP_Node($this->baseProtocol . "://" . $httpVars["repository_id"] . "/");
             } else {
                 $node = new AJXP_Node($this->urlBase . $file);
             }
             $this->getSharesFromMeta($shNode, $shares, false);
             if (!count($shares)) {
                 break;
             }
             if (isset($httpVars["element_id"]) && isset($shares[$httpVars["element_id"]])) {
                 $elementId = $httpVars["element_id"];
             } else {
                 $sKeys = array_keys($shares);
                 $elementId = $sKeys[0];
             }
             if ($this->watcher !== false) {
                 if (!$folder) {
                     if ($watchValue) {
                         $this->watcher->setWatchOnFolder($node, AuthService::getLoggedUser()->getId(), MetaWatchRegister::$META_WATCH_USERS_READ, array($elementId));
                     } else {
                         $this->watcher->removeWatchFromFolder($node, AuthService::getLoggedUser()->getId(), true, $elementId);
                     }
                 } else {
                     if ($watchValue) {
                         $this->watcher->setWatchOnFolder($node, AuthService::getLoggedUser()->getId(), MetaWatchRegister::$META_WATCH_BOTH);
                     } else {
                         $this->watcher->removeWatchFromFolder($node, AuthService::getLoggedUser()->getId());
                     }
                 }
             }
             $mess = ConfService::getMessages();
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::sendMessage($mess["share_center.47"], null);
             AJXP_XMLWriter::close();
             break;
         case "load_shared_element_data":
             $node = null;
             if (isset($httpVars["hash"])) {
                 $t = "minisite";
                 if (isset($httpVars["element_type"]) && $httpVars["element_type"] == "file") {
                     $t = "file";
                 }
                 $parsedMeta = array($httpVars["hash"] => array("type" => $t));
             } else {
                 $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
                 $node = new AJXP_Node($this->urlBase . $file);
                 $this->getSharesFromMeta($node, $parsedMeta, true);
             }
             $flattenJson = false;
             $jsonData = array();
             foreach ($parsedMeta as $shareId => $shareMeta) {
                 $jsonData[] = $this->shareToJson($shareId, $shareMeta, $node);
                 if ($shareMeta["type"] != "file") {
                     $flattenJson = true;
                 }
             }
             header("Content-type:application/json");
             if ($flattenJson && count($jsonData)) {
                 $jsonData = $jsonData[0];
             }
             echo json_encode($jsonData);
             break;
         case "unshare":
             if (isset($httpVars["hash"])) {
                 $res = $this->getShareStore()->deleteShare($httpVars["element_type"], $httpVars["hash"]);
                 if ($res !== false) {
                     AJXP_XMLWriter::header();
                     AJXP_XMLWriter::sendMessage("Successfully unshared element", null);
                     AJXP_XMLWriter::close();
                 }
             } else {
                 $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
                 $ajxpNode = new AJXP_Node($this->urlBase . $file);
                 $this->getSharesFromMeta($ajxpNode, $shares, false);
                 if (count($shares)) {
                     if (isset($httpVars["element_id"]) && isset($shares[$httpVars["element_id"]])) {
                         $elementId = $httpVars["element_id"];
                     } else {
                         $sKeys = array_keys($shares);
                         $elementId = $sKeys[0];
                     }
                     if (isset($shares[$elementId]) && isset($shares[$elementId]["type"])) {
                         $t = $shares[$elementId]["type"];
                     } else {
                         $t = "file";
                     }
                     $this->getShareStore()->deleteShare($t, $elementId);
                     $this->removeShareFromMeta($ajxpNode, $elementId);
                     AJXP_Controller::applyHook("msg.instant", array("<reload_shared_elements/>", ConfService::getRepository()->getId()));
                 }
             }
             break;
         case "reset_counter":
             if (isset($httpVars["hash"])) {
                 $userId = AuthService::getLoggedUser()->getId();
                 if (isset($httpVars["owner_id"]) && $httpVars["owner_id"] != $userId) {
                     if (!AuthService::getLoggedUser()->isAdmin()) {
                         throw new Exception("You are not allowed to access this resource");
                     }
                     $userId = $httpVars["owner_id"];
                 }
                 $this->getShareStore()->resetDownloadCounter($httpVars["hash"], $userId);
             } else {
                 $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
                 $ajxpNode = new AJXP_Node($this->urlBase . $file);
                 $metadata = $ajxpNode->retrieveMetadata("ajxp_shared", true, AJXP_METADATA_SCOPE_REPOSITORY);
                 if (!isset($metadata["shares"]) || !is_array($metadata["shares"])) {
                     return null;
                 }
                 if (isset($httpVars["element_id"]) && isset($metadata["shares"][$httpVars["element_id"]])) {
                     $this->getShareStore()->resetDownloadCounter($httpVars["element_id"], $httpVars["owner_id"]);
                 } else {
                     $keys = array_keys($metadata["shares"]);
                     foreach ($keys as $key) {
                         $this->getShareStore()->resetDownloadCounter($key, null);
                     }
                 }
             }
             break;
         case "update_shared_element_data":
             if (!in_array($httpVars["p_name"], array("counter", "tags"))) {
                 return null;
             }
             $hash = AJXP_Utils::decodeSecureMagic($httpVars["element_id"]);
             $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
             if ($this->getShareStore()->shareIsLegacy($hash)) {
                 // Store in metadata
                 $ajxpNode = new AJXP_Node($this->urlBase . $file);
                 $metadata = $ajxpNode->retrieveMetadata("ajxp_shared", true, AJXP_METADATA_SCOPE_REPOSITORY);
                 if (isset($metadata["shares"][$httpVars["element_id"]])) {
                     if (!is_array($metadata["shares"][$httpVars["element_id"]])) {
                         $metadata["shares"][$httpVars["element_id"]] = array();
                     }
                     $metadata["shares"][$httpVars["element_id"]][$httpVars["p_name"]] = $httpVars["p_value"];
                     $ajxpNode->setMetadata("ajxp_shared", $metadata, true, AJXP_METADATA_SCOPE_REPOSITORY);
                 }
             } else {
                 $this->getShareStore()->updateShareProperty($hash, $httpVars["p_name"], $httpVars["p_value"]);
             }
             break;
         case "sharelist-load":
             $parentRepoId = isset($httpVars["parent_repository_id"]) ? $httpVars["parent_repository_id"] : "";
             $userContext = $httpVars["user_context"];
             $currentUser = true;
             if ($userContext == "global" && AuthService::getLoggedUser()->isAdmin()) {
                 $currentUser = false;
             }
             $nodes = $this->listSharesAsNodes("/data/repositories/{$parentRepoId}/shares", $currentUser, $parentRepoId);
             AJXP_XMLWriter::header();
             if ($userContext == "current") {
                 AJXP_XMLWriter::sendFilesListComponentConfig('<columns template_name="ajxp_user.shares">
                 <column messageId="ajxp_conf.8" attributeName="ajxp_label" sortType="String"/>
                 <column messageId="share_center.132" attributeName="shared_element_parent_repository_label" sortType="String"/>
                 <column messageId="3" attributeName="share_type_readable" sortType="String"/>
                 </columns>');
             } else {
                 AJXP_XMLWriter::sendFilesListComponentConfig('<columns switchDisplayMode="list" switchGridMode="filelist" template_name="ajxp_conf.repositories">
                 <column messageId="ajxp_conf.8" attributeName="ajxp_label" sortType="String"/>
                 <column messageId="share_center.159" attributeName="owner" sortType="String"/>
                 <column messageId="3" attributeName="share_type_readable" sortType="String"/>
                 <column messageId="share_center.52" attributeName="share_data" sortType="String"/>
                 </columns>');
             }
             foreach ($nodes as $node) {
                 AJXP_XMLWriter::renderAjxpNode($node);
             }
             AJXP_XMLWriter::close();
             break;
         case "sharelist-clearExpired":
             $currentUser = ConfService::getRepository()->getAccessType() != "ajxp_conf";
             $count = $this->clearExpiredFiles($currentUser);
             AJXP_XMLWriter::header();
             if ($count) {
                 AJXP_XMLWriter::sendMessage("Removed " . count($count) . " expired links", null);
             } else {
                 AJXP_XMLWriter::sendMessage("Nothing to do", null);
             }
             AJXP_XMLWriter::close();
             break;
         default:
             break;
     }
     return null;
 }