/** * Updates the data * * The data argument is a readable stream resource. * * After a succesful put operation, you may choose to return an ETag. The * etag must always be surrounded by double-quotes. These quotes must * appear in the actual string you're returning. * * Clients may use the ETag from a PUT request to later on make sure that * when they update the file, the contents haven't changed in the mean * time. * * If you don't plan to store the file byte-by-byte, and you return a * different object on a subsequent GET you are strongly recommended to not * return an ETag, and just return null. * * @param resource $data * @throws Sabre_DAV_Exception_Forbidden * @return string|null */ public function put($data) { if (!\OC\Files\Filesystem::isUpdatable($this->path)) { throw new \Sabre_DAV_Exception_Forbidden(); } // mark file as partial while uploading (ignored by the scanner) $partpath = $this->path . '.part'; \OC\Files\Filesystem::file_put_contents($partpath, $data); //detect aborted upload if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { if (isset($_SERVER['CONTENT_LENGTH'])) { $expected = $_SERVER['CONTENT_LENGTH']; $actual = \OC\Files\Filesystem::filesize($partpath); if ($actual != $expected) { \OC\Files\Filesystem::unlink($partpath); throw new Sabre_DAV_Exception_BadRequest('expected filesize ' . $expected . ' got ' . $actual); } } } // rename to correct path \OC\Files\Filesystem::rename($partpath, $this->path); //allow sync clients to send the mtime along in a header $mtime = OC_Request::hasModificationTime(); if ($mtime !== false) { if (\OC\Files\Filesystem::touch($this->path, $mtime)) { header('X-OC-MTime: accepted'); } } return OC_Connector_Sabre_Node::getETagPropertyForPath($this->path); }
/** * @brief Renames the node * @param string $name The new name * @return void */ public function setName($name) { // rename is only allowed if the update privilege is granted if (!\OC\Files\Filesystem::isUpdatable($this->path)) { throw new \Sabre_DAV_Exception_Forbidden(); } list($parentPath, ) = Sabre_DAV_URLUtil::splitPath($this->path); list(, $newName) = Sabre_DAV_URLUtil::splitPath($name); $newPath = $parentPath . '/' . $newName; $oldPath = $this->path; \OC\Files\Filesystem::rename($this->path, $newPath); $this->path = $newPath; $query = OC_DB::prepare('UPDATE `*PREFIX*properties` SET `propertypath` = ?' . ' WHERE `userid` = ? AND `propertypath` = ?'); $query->execute(array($newPath, OC_User::getUser(), $oldPath)); }
/** * Determine permissions for a given file path * @param string $path * @return int */ function getPermissions($path) { // add read permissions $permissions = \OCP\PERMISSION_READ; // get directory $fileInfo = pathinfo($path); $dir = $fileInfo['dirname'] . '/'; // add update permissions if (Filesystem::isUpdatable($dir)) { $permissions |= \OCP\PERMISSION_UPDATE; } // add delete permissions if (Filesystem::isDeletable($dir)) { $permissions |= \OCP\PERMISSION_DELETE; } // add share permissions if (Filesystem::isSharable($dir)) { $permissions |= \OCP\PERMISSION_SHARE; } // return return $permissions; }
/** * Creates a new file in the directory * * Data will either be supplied as a stream resource, or in certain cases * as a string. Keep in mind that you may have to support either. * * After succesful creation of the file, you may choose to return the ETag * of the new file here. * * The returned ETag must be surrounded by double-quotes (The quotes should * be part of the actual string). * * If you cannot accurately determine the ETag, you should not return it. * If you don't store the file exactly as-is (you're transforming it * somehow) you should also not return an ETag. * * This means that if a subsequent GET to this new file does not exactly * return the same contents of what was submitted here, you are strongly * recommended to omit the ETag. * * @param string $name Name of the file * @param resource|string $data Initial payload * @throws Sabre_DAV_Exception_Forbidden * @return null|string */ public function createFile($name, $data = null) { if ($name === 'Shared' && empty($this->path)) { throw new \Sabre_DAV_Exception_Forbidden(); } // for chunked upload also updating a existing file is a "createFile" // because we create all the chunks before reasamble them to the existing file. if (isset($_SERVER['HTTP_OC_CHUNKED'])) { // exit if we can't create a new file and we don't updatable existing file $info = OC_FileChunking::decodeName($name); if (!\OC\Files\Filesystem::isCreatable($this->path) && !\OC\Files\Filesystem::isUpdatable($this->path . '/' . $info['name'])) { throw new \Sabre_DAV_Exception_Forbidden(); } } else { // For non-chunked upload it is enough to check if we can create a new file if (!\OC\Files\Filesystem::isCreatable($this->path)) { throw new \Sabre_DAV_Exception_Forbidden(); } } $path = $this->path . '/' . $name; $node = new OC_Connector_Sabre_File($path); return $node->put($data); }
public function getPermissions() { if (count($this->sharing)) { if ($this->isPublicShare()) { $permissions = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE; } else { $permissions = $this->sharing[0]['permissions']; } } else { list($owner, $path) = $this->getOwnerViewAndPath(); $permissions = 0; if (\OC\Files\Filesystem::isReadable($path)) { $permissions |= \OCP\PERMISSION_READ; } if (\OC\Files\Filesystem::isUpdatable($path)) { $permissions |= \OCP\PERMISSION_UPDATE; } } return $permissions; }
} } else { // file doesn't exist yet, so let's create it! if ($file == '') { OCP\JSON::error(array("data" => array("message" => "Empty Filename"))); exit; } \OC\Files\Filesystem::mkdir($dir); if (!\OC\Files\Filesystem::touch($dir . '/' . $file)) { OCP\JSON::error(array("data" => array("message" => "Error when creating new file!"))); OCP\Util::writeLog('files_svgedit', "Failed to create file: " . $path, OC_Log::ERROR); exit; } } // file should be existing now $writable = \OC\Files\Filesystem::isUpdatable($path); if ($writable) { if ($b64encoded) { $b64prefix = 'data:' . $b64type . ';base64,'; if (strpos($filecontents, $b64prefix) === 0) { $filecontents = base64_decode(substr($filecontents, strlen($b64prefix))); } } \OC\Files\Filesystem::file_put_contents($path, $filecontents); // Clear statcache clearstatcache(); // Get new mtime $newmtime = \OC\Files\Filesystem::filemtime($path); OCP\JSON::success(array('data' => array('mtime' => $newmtime))); } else { // Not writable!
OCP\JSON::checkLoggedIn(); OCP\JSON::callCheck(); // Get paramteres $filecontents = $_POST['filecontents']; $path = isset($_POST['path']) ? $_POST['path'] : ''; $mtime = isset($_POST['mtime']) ? $_POST['mtime'] : ''; if ($path != '' && $mtime != '') { // Get file mtime $filemtime = \OC\Files\Filesystem::filemtime($path); if ($mtime != $filemtime) { // Then the file has changed since opening OCP\JSON::error(); OCP\Util::writeLog('files_texteditor', "File: " . $path . " modified since opening.", OCP\Util::ERROR); } else { // File same as when opened, save file if (\OC\Files\Filesystem::isUpdatable($path)) { $filecontents = iconv(mb_detect_encoding($filecontents), "UTF-8", $filecontents); \OC\Files\Filesystem::file_put_contents($path, $filecontents); // Clear statcache clearstatcache(); // Get new mtime $newmtime = \OC\Files\Filesystem::filemtime($path); OCP\JSON::success(array('data' => array('mtime' => $newmtime))); } else { // Not writeable! OCP\JSON::error(array('data' => array('message' => 'Insufficient permissions'))); OCP\Util::writeLog('files_texteditor', "User does not have permission to write to file: " . $path, OCP\Util::ERROR); } } } else { if ($path == '') {
$target = OCP\Files::buildNotExistingFileName($dir . $relativePath, $files['name'][$i]); } else { $target = \OC\Files\Filesystem::normalizePath($dir . $relativePath . '/' . $files['name'][$i]); } // relative dir to return to the client if (isset($publicDirectory)) { // path relative to the public root $returnedDir = $publicDirectory . $relativePath; } else { // full path $returnedDir = $dir . $relativePath; } $returnedDir = \OC\Files\Filesystem::normalizePath($returnedDir); $exists = \OC\Files\Filesystem::file_exists($target); if ($exists) { $updatable = \OC\Files\Filesystem::isUpdatable($target); } if (!$exists || $updatable && $resolution === 'replace') { // upload and overwrite file try { if (is_uploaded_file($files['tmp_name'][$i]) and \OC\Files\Filesystem::fromTmpFile($files['tmp_name'][$i], $target)) { // updated max file size after upload $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir); $meta = \OC\Files\Filesystem::getFileInfo($target); if ($meta === false) { $error = $l->t('The target folder has been moved or deleted.'); $errorCode = 'targetnotfound'; } else { $data = \OCA\Files\Helper::formatFileInfo($meta); $data['status'] = 'success'; $data['originalname'] = $files['name'][$i];
/** * @deprecated OC_Filesystem is replaced by \OC\Files\Filesystem */ public static function isUpdatable($path) { return \OC\Files\Filesystem::isUpdatable($path); }
/** * Returns the numeric permissions for the given directory. * @param string $dir directory without trailing slash * @return numeric permissions */ public static function getDirPermissions($dir) { $permissions = \OCP\PERMISSION_READ; if (\OC\Files\Filesystem::isCreatable($dir . '/')) { $permissions |= \OCP\PERMISSION_CREATE; } if (\OC\Files\Filesystem::isUpdatable($dir . '/')) { $permissions |= \OCP\PERMISSION_UPDATE; } if (\OC\Files\Filesystem::isDeletable($dir . '/')) { $permissions |= \OCP\PERMISSION_DELETE; } if (\OC\Files\Filesystem::isSharable($dir . '/')) { $permissions |= \OCP\PERMISSION_SHARE; } return $permissions; }
} } // make breadcrumb und filelist markup $list = new OCP\Template('files', 'part.list', ''); $list->assign('files', $files); $list->assign('baseURL', OCP\Util::linkTo('files', 'index.php') . '?dir='); $list->assign('downloadURL', OCP\Util::linkToRoute('download', array('file' => '/'))); $list->assign('disableSharing', false); $breadcrumbNav = new OCP\Template('files', 'part.breadcrumb', ''); $breadcrumbNav->assign('breadcrumb', $breadcrumb); $breadcrumbNav->assign('baseURL', OCP\Util::linkTo('files', 'index.php') . '?dir='); $permissions = OCP\PERMISSION_READ; if (\OC\Files\Filesystem::isCreatable($dir . '/')) { $permissions |= OCP\PERMISSION_CREATE; } if (\OC\Files\Filesystem::isUpdatable($dir . '/')) { $permissions |= OCP\PERMISSION_UPDATE; } if (\OC\Files\Filesystem::isDeletable($dir . '/')) { $permissions |= OCP\PERMISSION_DELETE; } if (\OC\Files\Filesystem::isSharable($dir . '/')) { $permissions |= OCP\PERMISSION_SHARE; } if ($needUpgrade) { OCP\Util::addscript('files', 'upgrade'); $tmpl = new OCP\Template('files', 'upgrade', 'user'); $tmpl->printPage(); } else { // information about storage capacities $storageInfo = OC_Helper::getStorageInfo();