Пример #1
0
 public function testUser_prop_getlastmodified()
 {
     // We touch the file (updating the 'last modified' timestamp) and reload
     // the resource. The difference between the getlastmodified property and
     // the current time should not be greater than 1 second. Else either the
     // property is not loaded correctly or it takes too long to load the
     // resource. This last case is actually not an error in
     // user_prop_getlastmodified, but a problem nevertheless
     touch(\BeeHub::localPath('/foo/file.txt'));
     $file = new \BeeHub_MongoResource('/foo/file.txt');
     $this->assertLessThan(1, $file->user_prop_getlastmodified() - time());
 }
Пример #2
0
 public function __construct($path)
 {
     if (is_array($path)) {
         $document = $path;
         $path = '/' . $document['path'] . (isset($document['collection']) && $document['collection'] ? '/' : '');
     } else {
         $document = null;
     }
     parent::__construct($path);
     $this->localPath = BeeHub::localPath($path);
     if (file_exists($this->localPath)) {
         $this->stat = stat($this->localPath);
     }
     if (is_array($document)) {
         $this->init_props($document);
     }
 }
Пример #3
0
 /**
  * @param string $path
  */
 public function resource($path)
 {
     if (is_array($path)) {
         $document = $path;
         $path = '/' . $document['path'];
     } else {
         $document = null;
     }
     $path = DAV::unslashify($path);
     $systemPath = DAV::unslashify(BeeHub::SYSTEM_PATH);
     $usersPath = DAV::unslashify(BeeHub::USERS_PATH);
     $groupsPath = DAV::unslashify(BeeHub::GROUPS_PATH);
     $sponsorsPath = DAV::unslashify(BeeHub::SPONSORS_PATH);
     if (isset($this->resourceCache[$path])) {
         return $this->resourceCache[$path];
     }
     $localPath = BeeHub::localPath($path);
     $retval = null;
     if ($path === '/') {
         $retval = new BeeHub_Directory($path);
     } elseif ($path === $systemPath) {
         $retval = new BeeHub_System_Collection($path);
     } elseif (substr($path, 0, strlen($usersPath)) === $usersPath) {
         if ($path === $usersPath) {
             $retval = new BeeHub_Users($path);
         } else {
             try {
                 $retval = new BeeHub_User($path);
             } catch (Exception $e) {
             }
         }
     } elseif (substr($path, 0, strlen($groupsPath)) === $groupsPath) {
         if ($path === $groupsPath) {
             $retval = new BeeHub_Groups($path);
         } else {
             try {
                 $retval = new BeeHub_Group($path);
             } catch (Exception $e) {
             }
         }
     } elseif (substr($path, 0, strlen($sponsorsPath)) === $sponsorsPath) {
         if ($path === $sponsorsPath) {
             $retval = new BeeHub_Sponsors($path);
         } else {
             try {
                 $retval = new BeeHub_Sponsor($path);
             } catch (Exception $e) {
             }
         }
     } else {
         $unslashifiedPath = $path;
         if (substr($unslashifiedPath, 0, 1) === '/') {
             $unslashifiedPath = substr($unslashifiedPath, 1);
         }
         $collection = BeeHub::getNoSQL()->files;
         if (!is_array($document)) {
             $document = $collection->findOne(array('path' => $unslashifiedPath));
         }
         if (!is_null($document)) {
             if (isset($document['collection']) && $document['collection']) {
                 $retval = new BeeHub_Directory($document);
             } else {
                 $retval = new BeeHub_File($document);
             }
         } else {
             return null;
         }
     }
     return $this->resourceCache[$path] = $retval;
 }
Пример #4
0
 public function testLocalPath()
 {
     $config = \BeeHub::config();
     $this->assertSame($config['environment']['datadir'] . 'some/path', \BeeHub::localPath('/some/path'), 'BeeHub::localPath() should prepend the path of the data dir to the path provided');
 }
Пример #5
0
 public function method_MOVE($member, $destination)
 {
     $this->assert(DAVACL::PRIV_UNBIND);
     // Get the ACL of the source (including inherited ACE's)
     $sourceAcl = DAV::$REGISTRY->resource($this->path . $member)->user_prop_acl();
     // Determine if moving is allowed and if so, move the object
     DAV::$REGISTRY->resource($this->path . $member)->assert(DAVACL::PRIV_WRITE_CONTENT);
     DAV::$REGISTRY->resource($this->path . $member)->assert(BeeHub::PRIV_READ_CONTENT);
     DAV::$REGISTRY->resource($this->path . $member)->assert(DAVACL::PRIV_READ_ACL);
     $destinationResource = DAV::$REGISTRY->resource($destination);
     if ($destinationResource instanceof DAVACL_Resource) {
         $destinationResource->assert(DAVACL::PRIV_WRITE_CONTENT);
         $destinationResource->assert(DAVACL::PRIV_WRITE_ACL);
         $destinationResource->delete_recursively();
     } else {
         DAV::$REGISTRY->resource(dirname($destination))->assert(DAVACL::PRIV_WRITE_CONTENT);
     }
     $localDest = BeeHub::localPath($destination);
     rename(BeeHub::localPath($this->path . $member), $localDest);
     // Then move all properties to the new location
     $filesCollection = BeeHub::getNoSQL()->selectCollection('files');
     $path = DAV::unslashify($this->path . $member);
     if (substr($path, 0, 1) === '/') {
         $path = substr($path, 1);
     }
     $newPath = DAV::unslashify($destination);
     if (substr($newPath, 0, 1) === '/') {
         $newPath = substr($newPath, 1);
     }
     // We look up all paths that begin with the path of the resource we have to
     // move. If it is a collection, this means we will also find all child
     // resources and thus change all their locations in the database too.
     $mongoResults = $filesCollection->find(array('path' => array('$regex' => '^' . preg_quote($path) . '(/.*|$)')));
     foreach ($mongoResults as $mongoDocument) {
         $mongoDocument['path'] = $newPath . substr($mongoDocument['path'], strlen($path));
         $mongoDocument['depth'] = substr_count($mongoDocument['path'], '/') + 1;
         $filesCollection->save($mongoDocument);
     }
     $locksCollection = BeeHub::getNoSQL()->selectCollection('locks');
     $mongoResults = $locksCollection->find(array('path' => array('$regex' => '^' . preg_quote($path) . '/.*')));
     foreach ($mongoResults as $mongoDocument) {
         $mongoDocument['path'] = $newPath . substr($mongoDocument['path'], strlen($path));
         $locksCollection->save($mongoDocument);
     }
     $locksCollection->remove(array('path' => $path));
     // We need to make sure that the effective ACL at the destination is the same as at the resource
     $destinationAcl = array();
     $inheritedAcl = array();
     $copyInherited = true;
     foreach ($sourceAcl as $ace) {
         if ($ace->protected) {
             // Protected ACE's don't require copying; at this moment all resources have the same protected resources
             continue;
         }
         if ($ace->inherited) {
             // Inherited ACE's don't always need to be copied, so let's store them seperately for now
             $ace->inherited = null;
             $inheritedAcl[] = $ace;
         } else {
             // If there is already a 'deny all to everybody' ACE in the ACL, then no need to copy any inherited ACL's
             if ($ace->principal === DAVACL::PRINCIPAL_ALL && !$ace->invert && in_array(DAVACL::PRIV_ALL, $ace->privileges) && $ace->deny) {
                 $copyInherited = false;
             }
             $destinationAcl[] = $ace;
         }
     }
     $destinationResource = DAV::$REGISTRY->resource($destination);
     // If the inherited ACE's at the destination are the same as at the source, then no need to copy them (for example when moving within the same directory). The effective ACL will still be the same
     if ($copyInherited) {
         $oldDestinationAcl = $destinationResource->user_prop_acl();
         $copyInherited = false;
         foreach ($oldDestinationAcl as $ace) {
             if (!$ace->inherited) {
                 continue;
             }
             if (count($inheritedAcl) > 0 && $ace->principal === $inheritedAcl[0]->principal && $ace->invert === $inheritedAcl[0]->invert && $ace->deny === $inheritedAcl[0]->deny && $ace->privileges === $inheritedAcl[0]->privileges) {
                 array_shift($inheritedAcl);
             } else {
                 $copyInherited = true;
                 break;
             }
         }
     }
     // If needed; copy the inherited ACE's so we have the complete ACL of the source. And end it with a 'deny all to everybody' ACE so inherited ACE's at the destination don't change the effective ACL
     if ($copyInherited) {
         $destinationAcl = array_merge($destinationAcl, $inheritedAcl);
         $destinationAcl[] = new DAVACL_Element_ace(DAVACL::PRINCIPAL_ALL, false, array(DAVACL::PRIV_ALL), true, false, null);
     }
     // And store the ACL at the destination
     $destinationResource->user_set(DAV::PROP_ACL, $destinationAcl ? DAVACL_Element_ace::aces2json($destinationAcl) : null);
     $destinationResource->storeProperties();
 }
Пример #6
0
 public function method_COPY($path)
 {
     $this->assert(BeeHub::PRIV_READ_CONTENT);
     $this->assert(DAVACL::PRIV_READ_ACL);
     $destinationResource = DAV::$REGISTRY->resource($path);
     $parent = DAV::$REGISTRY->resource(dirname($path));
     if (!$parent) {
         throw new DAV_Status(DAV::HTTP_CONFLICT, 'Unable to COPY to unexisting collection');
     }
     if (!$parent instanceof BeeHub_Directory) {
         throw new DAV_Status(DAV::HTTP_FORBIDDEN);
     }
     if ($destinationResource instanceof DAVACL_Resource) {
         $destinationResource->assert(DAVACL::PRIV_WRITE_CONTENT);
         $destinationResource->assert(DAVACL::PRIV_WRITE_ACL);
         $parent->method_DELETE(basename($path));
     } else {
         $parent->assert(DAVACL::PRIV_WRITE_CONTENT);
     }
     // Determine the sponsor
     $user = BeeHub::getAuth()->current_user();
     $user_sponsors = $user->user_prop_sponsor_membership();
     if (count($user_sponsors) === 0) {
         // If the user doesn't have any sponsors, he/she can't create files and directories
         throw DAV::forbidden();
     }
     $localPath = BeeHub::localPath($path);
     exec('cp ' . BeeHub::escapeshellarg($this->localPath) . ' ' . BeeHub::escapeshellarg($localPath));
     // And copy the attributes
     $new_resource = new BeeHub_File($path);
     foreach ($this->stored_props as $prop => $value) {
         if (!in_array($prop, array(DAV::PROP_OWNER, BeeHub::PROP_SPONSOR, DAV::PROP_ACL, DAV::PROP_GETETAG, DAV::PROP_LOCKDISCOVERY))) {
             $new_resource->user_set($prop, $value);
         }
     }
     $sponsor = $parent->user_prop_sponsor();
     // The default is the directory sponsor
     if (!in_array($sponsor, $user_sponsors)) {
         //But a user can only create files sponsored by his own sponsors
         $sponsor = $user->user_prop(BeeHub::PROP_SPONSOR);
     }
     // And set the new properties
     $new_resource->user_set(DAV::PROP_OWNER, $this->user_prop_current_user_principal());
     $new_resource->user_set(BeeHub::PROP_SPONSOR, $sponsor);
     $new_resource->user_set(DAV::PROP_GETETAG, BeeHub::ETag());
     $new_resource->storeProperties();
 }