/**
  * Update some internal configs from httpVars
  * @param $httpVars
  * @throws Exception
  */
 public function parseHttpVars($httpVars)
 {
     $data =& $this->internal;
     $data["DOWNLOAD_DISABLED"] = isset($httpVars["simple_right_download"]) ? false : true;
     $data["AJXP_APPLICATION_BASE"] = AJXP_Utils::detectServerURL(true);
     if (isset($httpVars["minisite_layout"])) {
         $data["AJXP_TEMPLATE_NAME"] = $httpVars["minisite_layout"];
     }
     if (isset($httpVars["expiration"])) {
         if (intval($httpVars["expiration"]) > 0) {
             $data["EXPIRE_TIME"] = time() + intval($httpVars["expiration"]) * 86400;
         } else {
             if (isset($data["EXPIRE_TIME"])) {
                 unset($data["EXPIRE_TIME"]);
             }
         }
     }
     if (isset($httpVars["downloadlimit"])) {
         if (intval($httpVars["downloadlimit"]) > 0) {
             $data["DOWNLOAD_LIMIT"] = intval($httpVars["downloadlimit"]);
         } else {
             if (isset($data["DOWNLOAD_LIMIT"])) {
                 unset($data["DOWNLOAD_LIMIT"]);
             }
         }
     }
     if (isset($httpVars["custom_handle"]) && !empty($httpVars["custom_handle"]) && (!isset($this->hash) || $httpVars["custom_handle"] != $this->hash)) {
         // Existing already
         $value = AJXP_Utils::sanitize($httpVars["custom_handle"], AJXP_SANITIZE_ALPHANUM);
         $value = strtolower($value);
         if (strlen($value) < $this->store->hashMinLength) {
             $mess = ConfService::getMessages();
             throw new Exception(str_replace("%s", $this->store->hashMinLength, $mess["share_center.223"]));
         }
         $test = $this->store->loadShare($value);
         $mess = ConfService::getMessages();
         if (!empty($test)) {
             throw new Exception($mess["share_center.172"]);
         }
         if (!isset($this->hash)) {
             $this->hash = $value;
         } else {
             $this->newHash = $value;
         }
     }
 }
 /**
  * @param ShareCenter $shareCenter
  * @param ShareStore $shareStore
  * @param ShareRightsManager $shareRightManager
  */
 public static function migrateLegacyMeta($shareCenter, $shareStore, $shareRightManager, $dryRun = true)
 {
     $metaStoreDir = AJXP_DATA_PATH . "/plugins/metastore.serial";
     $publicFolder = ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER");
     $metastores = glob($metaStoreDir . "/ajxp_meta_0");
     if ($dryRun) {
         print "RUNNING A DRY RUN FOR META MIGRATION";
     }
     foreach ($metastores as $store) {
         if (strpos($store, ".bak") !== false) {
             continue;
         }
         // Backup store
         if (!$dryRun) {
             copy($store, $store . ".bak");
         }
         $data = unserialize(file_get_contents($store));
         foreach ($data as $filePath => &$metadata) {
             foreach ($metadata as $userName => &$meta) {
                 if (!AuthService::userExists($userName)) {
                     continue;
                 }
                 $userObject = ConfService::getConfStorageImpl()->createUserObject($userName);
                 if (isset($meta["ajxp_shared"]) && isset($meta["ajxp_shared"]["element"])) {
                     print "\n\nItem {$filePath} requires upgrade :";
                     $share = $meta["ajxp_shared"];
                     $element = $meta["ajxp_shared"]["element"];
                     if (is_array($element)) {
                         $element = array_shift(array_keys($element));
                     }
                     // Take the first one only
                     $legacyLinkFile = $publicFolder . "/" . $element . ".php";
                     if (file_exists($legacyLinkFile)) {
                         // Load file, move it to DB and move the meta
                         $publiclet = $shareStore->loadShare($element);
                         rename($legacyLinkFile, $legacyLinkFile . ".migrated");
                         if (isset($share["minisite"])) {
                             print "\n--Migrate legacy minisite to new minisite?";
                             try {
                                 $sharedRepoId = $publiclet["REPOSITORY"];
                                 $sharedRepo = ConfService::getRepositoryById($sharedRepoId);
                                 if ($sharedRepo == null) {
                                     print "\n--ERROR: Cannot find repository with id " . $sharedRepoId;
                                     continue;
                                 }
                                 $shareLink = new ShareLink($shareStore, $publiclet);
                                 $user = $shareLink->getUniqueUser();
                                 if (AuthService::userExists($user)) {
                                     $userObject = ConfService::getConfStorageImpl()->createUserObject($user);
                                     $userObject->setHidden(true);
                                     print "\n--Should set existing user {$user} as hidden";
                                     if (!$dryRun) {
                                         $userObject->save();
                                     }
                                 }
                                 $shareLink->parseHttpVars(["custom_handle" => $element]);
                                 $shareLink->setParentRepositoryId($sharedRepo->getParentId());
                                 print "\n--Creating the following share object";
                                 print_r($shareLink->getJsonData($shareCenter->getPublicAccessManager(), ConfService::getMessages()));
                                 if (!$dryRun) {
                                     $shareLink->save();
                                 }
                                 $meta["ajxp_shared"] = ["shares" => [$element => ["type" => "minisite"], $sharedRepoId => ["type" => "repository"]]];
                             } catch (Exception $e) {
                                 print "\n-- Error " . $e->getMessage();
                             }
                         } else {
                             print "\n--Should migrate legacy link to new minisite with ContentFilter";
                             try {
                                 $link = new ShareLink($shareStore);
                                 $link->setOwnerId($userName);
                                 $parameters = array("custom_handle" => $element, "simple_right_download" => true);
                                 if (isset($publiclet["EXPIRE_TIME"])) {
                                     $parameters["expiration"] = $publiclet["EXPIRE_TIME"];
                                 }
                                 if (isset($publiclet["DOWNLOAD_LIMIT"])) {
                                     $parameters["downloadlimit"] = $publiclet["DOWNLOAD_LIMIT"];
                                 }
                                 $link->parseHttpVars($parameters);
                                 $parentRepositoryObject = $publiclet["REPOSITORY"];
                                 $driverInstance = AJXP_PluginsService::findPlugin("access", $parentRepositoryObject->getAccessType());
                                 if (empty($driverInstance)) {
                                     print "\n-- ERROR: Cannot find driver instance!";
                                     continue;
                                 }
                                 $options = $driverInstance->makeSharedRepositoryOptions(["file" => "/"], $parentRepositoryObject);
                                 $options["SHARE_ACCESS"] = "private";
                                 $newRepo = $parentRepositoryObject->createSharedChild(basename($filePath), $options, $parentRepositoryObject->getId(), $userObject->getId(), null);
                                 $gPath = $userObject->getGroupPath();
                                 if (!empty($gPath) && !ConfService::getCoreConf("CROSSUSERS_ALLGROUPS", "conf")) {
                                     $newRepo->setGroupPath($gPath);
                                 }
                                 $newRepo->setDescription("");
                                 // Smells like dirty hack!
                                 $newRepo->options["PATH"] = SystemTextEncoding::fromStorageEncoding($newRepo->options["PATH"]);
                                 $newRepo->setContentFilter(new ContentFilter([new AJXP_Node("pydio://" . $parentRepositoryObject->getId() . $filePath)]));
                                 if (!$dryRun) {
                                     ConfService::addRepository($newRepo);
                                 }
                                 $hiddenUserEntry = $shareRightManager->prepareSharedUserEntry(["simple_right_read" => true, "simple_right_download" => true], $link, false, null);
                                 $selection = new UserSelection($parentRepositoryObject, []);
                                 $selection->addFile($filePath);
                                 if (!$dryRun) {
                                     $shareRightManager->assignSharedRepositoryPermissions($parentRepositoryObject, $newRepo, false, [$hiddenUserEntry["ID"] => $hiddenUserEntry], [], $selection);
                                 }
                                 $link->setParentRepositoryId($parentRepositoryObject->getId());
                                 $link->attachToRepository($newRepo->getId());
                                 print "\n-- Should save following LINK: ";
                                 print_r($link->getJsonData($shareCenter->getPublicAccessManager(), ConfService::getMessages()));
                                 if (!$dryRun) {
                                     $hash = $link->save();
                                 }
                                 // UPDATE METADATA
                                 $meta["ajxp_shared"] = ["shares" => [$element => array("type" => "minisite")]];
                             } catch (Exception $e) {
                                 print "\n-- ERROR: " . $e->getMessage();
                             }
                         }
                         if ($dryRun) {
                             rename($legacyLinkFile . ".migrated", $legacyLinkFile);
                         }
                         continue;
                     } else {
                         //
                         // File does not exists, remove meta
                         //
                         unset($meta["ajxp_shared"]);
                     }
                     $repo = ConfService::getRepositoryById($element);
                     if ($repo !== null) {
                         print "\n--Shared repository: just metadata";
                         // Shared repo, migrating the meta should be enough
                         $meta["ajxp_shared"] = array("shares" => [$element => array("type" => "repository")]);
                     }
                 }
             }
         }
         print "\n\n SHOULD NOW UPDATE METADATA WITH FOLLOWING :";
         print_r($data);
         if (!$dryRun) {
             file_put_contents($store, serialize($data));
         }
     }
 }
 /**
  * Authenticates the user based on the current request.
  *
  * If authentication is successful, true must be returned.
  * If authentication fails, an exception must be thrown.
  *
  * @param DAV\Server $server
  * @param string $realm
  * @throws DAV\Exception\NotAuthenticated
  * @return bool
  */
 public function authenticate(DAV\Server $server, $realm)
 {
     $auth = new BasicAuthNoPass();
     $auth->setHTTPRequest($server->httpRequest);
     $auth->setHTTPResponse($server->httpResponse);
     $auth->setRealm($realm);
     $userpass = $auth->getUserPass();
     if (!$userpass) {
         $auth->requireLogin();
         throw new DAV\Exception\NotAuthenticated('No basic authentication headers were found');
     }
     // Authenticates the user
     $token = $userpass[0];
     $shareStore = new \ShareStore(\ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER"));
     $shareData = $shareStore->loadShare($token);
     if (is_array($shareData)) {
         $this->shareData = $shareData;
     } else {
         $auth->requireLogin();
         throw new DAV\Exception\NotAuthenticated('Username or password does not match');
     }
     if (!$this->validateUserPass($userpass[0], $userpass[1])) {
         $auth->requireLogin();
         throw new DAV\Exception\NotAuthenticated('Username or password does not match');
     }
     $repositoryId = $this->shareData["REPOSITORY"];
     $repository = \ConfService::getRepositoryById($repositoryId);
     if ($repository == null) {
         $repository = \ConfService::getRepositoryByAlias($repositoryId);
     }
     if ($repository == null) {
         throw new DAV\Exception\NotAuthenticated('Username cannot access any repository');
     } else {
         $this->rootCollection->updateRepository($repository);
     }
     $this->currentUser = $userpass[0];
     return true;
 }
Beispiel #4
0
 /**
  * Not used for the moment
  * This will expose folder as /dav/FolderName and file as /dav/FileName.txt
  *
  * @param $baseUri
  * @return \AJXP_Sabre_Collection|SharingCollection
  * @throws \Exception
  */
 protected function initCollectionForFileOrFolderAsUniqueItem(&$baseUri)
 {
     try {
         $testBackend = new BasicAuthNoPass();
         $userPass = $testBackend->getUserPass();
         if (isset($userPass[0])) {
             $shareStore = new \ShareStore(\ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER"));
             $shareData = $shareStore->loadShare($userPass[0]);
             if (isset($shareData) && isset($shareData["REPOSITORY"])) {
                 $repo = \ConfService::getRepositoryById($shareData["REPOSITORY"]);
                 if (!empty($repo) && !$repo->hasContentFilter()) {
                     $baseDir = basename($repo->getOption("PATH"));
                 }
             }
         }
     } catch (\Exception $e) {
     }
     $rootCollection = new \AJXP_Sabre_Collection("/", null, null);
     if (isset($baseDir)) {
         $currentPath = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
         if ($currentPath == $baseUri || $currentPath == $baseUri . "/") {
             $rootCollection = new SharingCollection("/", null, null);
         } else {
             $baseUri .= "/{$baseDir}";
         }
     }
     return $rootCollection;
 }