Ejemplo n.º 1
0
 /**
  * put filesystem info in the cache
  * @param string $path
  * @param array data
  * @param string root (optional)
  * @note $data is an associative array in the same format as returned 
  * by get
  */
 public static function put($path, $data, $root = false)
 {
     if ($root === false) {
         $root = OC_Filesystem::getRoot();
     }
     $fullpath = OC_Filesystem::normalizePath($root . '/' . $path);
     $parent = self::getParentId($fullpath);
     $id = self::getId($fullpath, '');
     if (isset(OC_FileCache_Cached::$savedData[$fullpath])) {
         $data = array_merge(OC_FileCache_Cached::$savedData[$fullpath], $data);
         unset(OC_FileCache_Cached::$savedData[$fullpath]);
     }
     if ($id != -1) {
         self::update($id, $data);
         return;
     }
     // add parent directory to the file cache if it does not exist yet.
     if ($parent == -1 && $fullpath != $root) {
         $parentDir = dirname($path);
         self::scanFile($parentDir);
         $parent = self::getParentId($fullpath);
     }
     if (!isset($data['size']) or !isset($data['mtime'])) {
         //save incomplete data for the next time we write it
         OC_FileCache_Cached::$savedData[$fullpath] = $data;
         return;
     }
     if (!isset($data['encrypted'])) {
         $data['encrypted'] = false;
     }
     if (!isset($data['versioned'])) {
         $data['versioned'] = false;
     }
     $mimePart = dirname($data['mimetype']);
     $data['size'] = (int) $data['size'];
     $data['ctime'] = (int) $data['mtime'];
     $data['writable'] = (int) $data['writable'];
     $data['encrypted'] = (int) $data['encrypted'];
     $data['versioned'] = (int) $data['versioned'];
     $user = OC_User::getUser();
     $query = OC_DB::prepare('INSERT INTO `*PREFIX*fscache`(`parent`, `name`, `path`, `path_hash`, `size`, `mtime`, `ctime`, `mimetype`, `mimepart`,`user`,`writable`,`encrypted`,`versioned`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)');
     $result = $query->execute(array($parent, basename($fullpath), $fullpath, md5($fullpath), $data['size'], $data['mtime'], $data['ctime'], $data['mimetype'], $mimePart, $user, $data['writable'], $data['encrypted'], $data['versioned']));
     if (OC_DB::isError($result)) {
         OC_Log::write('files', 'error while writing file(' . $fullpath . ') to cache', OC_Log::ERROR);
     }
     if ($cache = OC_Cache::getUserCache(true)) {
         $cache->remove('fileid/' . $fullpath);
         //ensure we don't have -1 cached
     }
 }
Ejemplo n.º 2
0
 /**
  * get the content of a directory
  * @param dir $directory path under datadirectory
  */
 public static function getDirectoryContent($directory, $mimetype_filter = '')
 {
     $directory = OC_Filesystem::normalizePath($directory);
     if ($directory == '/') {
         $directory = '';
     }
     $files = array();
     if (($directory == '/Shared' || substr($directory, 0, 8) == '/Shared/') && OC_App::isEnabled('files_sharing')) {
         if ($directory == '/Shared') {
             $files = OCP\Share::getItemsSharedWith('file', OC_Share_Backend_File::FORMAT_FILE_APP, array('folder' => $directory, 'mimetype_filter' => $mimetype_filter));
         } else {
             $pos = strpos($directory, '/', 8);
             // Get shared folder name
             if ($pos !== false) {
                 $itemTarget = substr($directory, 7, $pos - 7);
             } else {
                 $itemTarget = substr($directory, 7);
             }
             $files = OCP\Share::getItemSharedWith('folder', $itemTarget, OC_Share_Backend_File::FORMAT_FILE_APP, array('folder' => $directory, 'mimetype_filter' => $mimetype_filter));
         }
     } else {
         $files = OC_FileCache::getFolderContent($directory, false, $mimetype_filter);
         foreach ($files as &$file) {
             $file['directory'] = $directory;
             $file['type'] = $file['mimetype'] == 'httpd/unix-directory' ? 'dir' : 'file';
             $permissions = OCP\PERMISSION_READ;
             // NOTE: Remove check when new encryption is merged
             if (!$file['encrypted']) {
                 $permissions |= OCP\PERMISSION_SHARE;
             }
             if ($file['type'] == 'dir' && $file['writable']) {
                 $permissions |= OCP\PERMISSION_CREATE;
             }
             if ($file['writable']) {
                 $permissions |= OCP\PERMISSION_UPDATE | OCP\PERMISSION_DELETE;
             }
             $file['permissions'] = $permissions;
         }
         if ($directory == '' && OC_App::isEnabled('files_sharing')) {
             // Add 'Shared' folder
             $files = array_merge($files, OCP\Share::getItemsSharedWith('file', OC_Share_Backend_File::FORMAT_FILE_APP_ROOT));
         }
     }
     usort($files, "fileCmp");
     //TODO: remove this once ajax is merged
     return $files;
 }
Ejemplo n.º 3
0
 /**
  * @param $path
  * @param $data
  * @return bool
  */
 public function preFile_put_contents($path, &$data)
 {
     if (self::shouldEncrypt($path)) {
         if (!is_resource($data)) {
             // get root view
             $view = new \OC_FilesystemView('/');
             // get relative path
             $relativePath = \OCA\Encryption\Helper::stripUserFilesPath($path);
             if (!isset($relativePath)) {
                 return true;
             }
             // create random cache folder
             $cacheFolder = rand();
             $path_slices = explode('/', \OC_Filesystem::normalizePath($path));
             $path_slices[2] = "cache/" . $cacheFolder;
             $tmpPath = implode('/', $path_slices);
             $handle = fopen('crypt://' . $tmpPath, 'w');
             if (is_resource($handle)) {
                 // write data to stream
                 fwrite($handle, $data);
                 // close stream
                 fclose($handle);
                 // disable encryption proxy to prevent recursive calls
                 $proxyStatus = \OC_FileProxy::$enabled;
                 \OC_FileProxy::$enabled = false;
                 // get encrypted content
                 $data = $view->file_get_contents($tmpPath);
                 // remove our temp file
                 $view->deleteAll('/' . \OCP\User::getUser() . '/cache/' . $cacheFolder);
                 // re-enable proxy - our work is done
                 \OC_FileProxy::$enabled = $proxyStatus;
             }
         }
     }
     return true;
 }
Ejemplo n.º 4
0
 public function file_assemble($path)
 {
     $absolutePath = OC_Filesystem::normalizePath(OC_Filesystem::getView()->getAbsolutePath($path));
     $data = '';
     // use file_put_contents as method because that best matches what this function does
     if (OC_FileProxy::runPreProxies('file_put_contents', $absolutePath, $data) && OC_Filesystem::isValidPath($path)) {
         $path = OC_Filesystem::getView()->getRelativePath($absolutePath);
         $exists = OC_Filesystem::file_exists($path);
         $run = true;
         if (!$exists) {
             OC_Hook::emit(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_create, array(OC_Filesystem::signal_param_path => $path, OC_Filesystem::signal_param_run => &$run));
         }
         OC_Hook::emit(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_write, array(OC_Filesystem::signal_param_path => $path, OC_Filesystem::signal_param_run => &$run));
         if (!$run) {
             return false;
         }
         $target = OC_Filesystem::fopen($path, 'w');
         if ($target) {
             $count = $this->assemble($target);
             fclose($target);
             if (!$exists) {
                 OC_Hook::emit(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_create, array(OC_Filesystem::signal_param_path => $path));
             }
             OC_Hook::emit(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_write, array(OC_Filesystem::signal_param_path => $path));
             OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count);
             return $count > 0;
         } else {
             return false;
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * @brief update file cache with the new unencrypted size after file was written
  * @param string $path
  * @param mixed $result
  * @return mixed
  */
 public function postFile_put_contents($path, $result)
 {
     $normalizedPath = \OC_Filesystem::normalizePath($path);
     if (isset(self::$unencryptedSizes[$normalizedPath])) {
         $view = new \OC_FilesystemView('/');
         $view->putFileInfo($normalizedPath, array('encrypted' => true, 'unencrypted_size' => self::$unencryptedSizes[$normalizedPath]));
         unset(self::$unencryptedSizes[$normalizedPath]);
     }
     return $result;
 }
Ejemplo n.º 6
0
$list->assign('downloadURL', OCP\Util::linkTo('files', 'download.php') . '?file=', false);
$breadcrumbNav = new OCP\Template('files', 'part.breadcrumb', '');
$breadcrumbNav->assign('breadcrumb', $breadcrumb, false);
$breadcrumbNav->assign('baseURL', OCP\Util::linkTo('files', 'index.php') . '&dir=', false);
$upload_max_filesize = OCP\Util::computerFileSize(ini_get('upload_max_filesize'));
$post_max_size = OCP\Util::computerFileSize(ini_get('post_max_size'));
$maxUploadFilesize = min($upload_max_filesize, $post_max_size);
$freeSpace = OC_Filesystem::free_space('/');
$freeSpace = max($freeSpace, 0);
$maxUploadFilesize = min($maxUploadFilesize, $freeSpace);
$permissions = OCP\Share::PERMISSION_READ;
if (OC_Filesystem::isUpdatable($dir . '/')) {
    $permissions |= OCP\Share::PERMISSION_UPDATE;
}
if (OC_Filesystem::isDeletable($dir . '/')) {
    $permissions |= OCP\Share::PERMISSION_DELETE;
}
if (OC_Filesystem::isSharable($dir . '/')) {
    $permissions |= OCP\Share::PERMISSION_SHARE;
}
$tmpl = new OCP\Template('files', 'index', 'user');
$tmpl->assign('fileList', $list->fetchPage(), false);
$tmpl->assign('breadcrumb', $breadcrumbNav->fetchPage(), false);
$tmpl->assign('dir', OC_Filesystem::normalizePath($dir));
$tmpl->assign('isCreatable', OC_Filesystem::isCreatable($dir . '/'));
$tmpl->assign('permissions', $permissions);
$tmpl->assign('files', $files);
$tmpl->assign('uploadMaxFilesize', $maxUploadFilesize);
$tmpl->assign('uploadMaxHumanFilesize', OCP\Util::humanFileSize($maxUploadFilesize));
$tmpl->assign('allowZipDownload', intval(OCP\Config::getSystemValue('allowZipDownload', true)));
$tmpl->printPage();
Ejemplo n.º 7
0
 /**
  * @brief make sure that the path has the correct root
  *
  * @param string $path path returned from the share API
  * @param string $folder current root folder
  * @return string the correct path
  */
 protected static function correctPath($path, $folder)
 {
     return \OC_Filesystem::normalizePath('/' . $folder . '/' . basename($path));
 }
Ejemplo n.º 8
0
 /**
  * @brief Get shared items from the database
  * @param string Item type
  * @param string Item source or target (optional)
  * @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique
  * @param string User or group the item is being shared with
  * @param string User that is the owner of shared items (optional)
  * @param int Format to convert items to with formatItems()
  * @param mixed Parameters to pass to formatItems()
  * @param int Number of items to return, -1 to return all matches (optional)
  * @param bool Include collection item types (optional)
  * @return mixed
  *
  * See public functions getItem(s)... for parameter usage
  *
  */
 private static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, $includeCollections = false, $itemShareWithBySource = false)
 {
     if (!self::isEnabled()) {
         if ($limit == 1 || isset($uidOwner) && isset($item)) {
             return false;
         } else {
             return array();
         }
     }
     $backend = self::getBackend($itemType);
     // Get filesystem root to add it to the file target and remove from the file source, match file_source with the file cache
     if ($itemType == 'file' || $itemType == 'folder') {
         $root = \OC_Filesystem::getRoot();
         $where = 'INNER JOIN `*PREFIX*fscache` ON `file_source` = `*PREFIX*fscache`.`id`';
         if (!isset($item)) {
             $where .= ' WHERE `file_target` IS NOT NULL';
         }
         $fileDependent = true;
         $queryArgs = array();
     } else {
         $fileDependent = false;
         $root = '';
         if ($includeCollections && !isset($item) && ($collectionTypes = self::getCollectionItemTypes($itemType))) {
             // If includeCollections is true, find collections of this item type, e.g. a music album contains songs
             if (!in_array($itemType, $collectionTypes)) {
                 $itemTypes = array_merge(array($itemType), $collectionTypes);
             } else {
                 $itemTypes = $collectionTypes;
             }
             $placeholders = join(',', array_fill(0, count($itemTypes), '?'));
             $where .= ' WHERE item_type IN (' . $placeholders . '))';
             $queryArgs = $itemTypes;
         } else {
             $where = ' WHERE `item_type` = ?';
             $queryArgs = array($itemType);
         }
     }
     if (isset($shareType)) {
         // Include all user and group items
         if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) {
             $where .= ' AND `share_type` IN (?,?,?)';
             $queryArgs[] = self::SHARE_TYPE_USER;
             $queryArgs[] = self::SHARE_TYPE_GROUP;
             $queryArgs[] = self::$shareTypeGroupUserUnique;
             $userAndGroups = array_merge(array($shareWith), \OC_Group::getUserGroups($shareWith));
             $placeholders = join(',', array_fill(0, count($userAndGroups), '?'));
             $where .= ' AND `share_with` IN (' . $placeholders . ')';
             $queryArgs = array_merge($queryArgs, $userAndGroups);
             // Don't include own group shares
             $where .= ' AND `uid_owner` != ?';
             $queryArgs[] = $shareWith;
         } else {
             $where .= ' AND `share_type` = ?';
             $queryArgs[] = $shareType;
             if (isset($shareWith)) {
                 $where .= ' AND `share_with` = ?';
                 $queryArgs[] = $shareWith;
             }
         }
     }
     if (isset($uidOwner)) {
         $where .= ' AND `uid_owner` = ?';
         $queryArgs[] = $uidOwner;
         if (!isset($shareType)) {
             // Prevent unique user targets for group shares from being selected
             $where .= ' AND `share_type` != ?';
             $queryArgs[] = self::$shareTypeGroupUserUnique;
         }
         if ($itemType == 'file' || $itemType == 'folder') {
             $column = 'file_source';
         } else {
             $column = 'item_source';
         }
     } else {
         if ($itemType == 'file' || $itemType == 'folder') {
             $column = 'file_target';
         } else {
             $column = 'item_target';
         }
     }
     if (isset($item)) {
         if ($includeCollections && ($collectionTypes = self::getCollectionItemTypes($itemType))) {
             $where .= ' AND (';
         } else {
             $where .= ' AND';
         }
         // If looking for own shared items, check item_source else check item_target
         if (isset($uidOwner) || $itemShareWithBySource) {
             // If item type is a file, file source needs to be checked in case the item was converted
             if ($itemType == 'file' || $itemType == 'folder') {
                 $where .= ' `file_source` = ?';
                 $column = 'file_source';
             } else {
                 $where .= ' `item_source` = ?';
                 $column = 'item_source';
             }
         } else {
             if ($itemType == 'file' || $itemType == 'folder') {
                 $where .= ' `file_target` = ?';
                 $item = \OC_Filesystem::normalizePath($item);
             } else {
                 $where .= ' `item_target` = ?';
             }
         }
         $queryArgs[] = $item;
         if ($includeCollections && $collectionTypes) {
             $placeholders = join(',', array_fill(0, count($collectionTypes), '?'));
             $where .= ' OR item_type IN (' . $placeholders . '))';
             $queryArgs = array_merge($queryArgs, $collectionTypes);
         }
     }
     if ($limit != -1 && !$includeCollections) {
         if ($shareType == self::$shareTypeUserAndGroups) {
             // Make sure the unique user target is returned if it exists, unique targets should follow the group share in the database
             // If the limit is not 1, the filtering can be done later
             $where .= ' ORDER BY `*PREFIX*share`.`id` DESC';
         }
         // The limit must be at least 3, because filtering needs to be done
         if ($limit < 3) {
             $queryLimit = 3;
         } else {
             $queryLimit = $limit;
         }
     } else {
         $queryLimit = null;
     }
     // TODO Optimize selects
     if ($format == self::FORMAT_STATUSES) {
         if ($itemType == 'file' || $itemType == 'folder') {
             $select = '`*PREFIX*share`.`id`, `item_type`, `*PREFIX*share`.`parent`, `share_type`, `file_source`, `path`, `expiration`';
         } else {
             $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `expiration`';
         }
     } else {
         if (isset($uidOwner)) {
             if ($itemType == 'file' || $itemType == 'folder') {
                 $select = '`*PREFIX*share`.`id`, `item_type`, `*PREFIX*share`.`parent`, `share_type`, `share_with`, `file_source`, `path`, `permissions`, `stime`, `expiration`';
             } else {
                 $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `permissions`, `stime`, `file_source`, `expiration`';
             }
         } else {
             if ($fileDependent) {
                 if (($itemType == 'file' || $itemType == 'folder') && $format == \OC_Share_Backend_File::FORMAT_FILE_APP || $format == \OC_Share_Backend_File::FORMAT_FILE_APP_ROOT) {
                     $select = '`*PREFIX*share`.`id`, `item_type`, `*PREFIX*share`.`parent`, `uid_owner`, `share_type`, `share_with`, `file_source`, `path`, `file_target`, `permissions`, `expiration`, `name`, `ctime`, `mtime`, `mimetype`, `size`, `encrypted`, `versioned`, `writable`';
                 } else {
                     $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`, `*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`, `file_source`, `path`, `file_target`, `permissions`, `stime`, `expiration`';
                 }
             } else {
                 $select = '*';
             }
         }
     }
     $root = strlen($root);
     $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where, $queryLimit);
     $result = $query->execute($queryArgs);
     $items = array();
     $targets = array();
     while ($row = $result->fetchRow()) {
         // Filter out duplicate group shares for users with unique targets
         if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) {
             $row['share_type'] = self::SHARE_TYPE_GROUP;
             $row['share_with'] = $items[$row['parent']]['share_with'];
             // Remove the parent group share
             unset($items[$row['parent']]);
             if ($row['permissions'] == 0) {
                 continue;
             }
         } else {
             if (!isset($uidOwner)) {
                 // Check if the same target already exists
                 if (isset($targets[$row[$column]])) {
                     // Check if the same owner shared with the user twice through a group and user share - this is allowed
                     $id = $targets[$row[$column]];
                     if ($items[$id]['uid_owner'] == $row['uid_owner']) {
                         // Switch to group share type to ensure resharing conditions aren't bypassed
                         if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) {
                             $items[$id]['share_type'] = self::SHARE_TYPE_GROUP;
                             $items[$id]['share_with'] = $row['share_with'];
                         }
                         // Switch ids if sharing permission is granted on only one share to ensure correct parent is used if resharing
                         if (~(int) $items[$id]['permissions'] & self::PERMISSION_SHARE && (int) $row['permissions'] & self::PERMISSION_SHARE) {
                             $items[$row['id']] = $items[$id];
                             unset($items[$id]);
                             $id = $row['id'];
                         }
                         // Combine the permissions for the item
                         $items[$id]['permissions'] |= (int) $row['permissions'];
                         continue;
                     }
                 } else {
                     $targets[$row[$column]] = $row['id'];
                 }
             }
         }
         // Remove root from file source paths if retrieving own shared items
         if (isset($uidOwner) && isset($row['path'])) {
             if (isset($row['parent'])) {
                 $row['path'] = '/Shared/' . basename($row['path']);
             } else {
                 $row['path'] = substr($row['path'], $root);
             }
         }
         if (isset($row['expiration'])) {
             $time = new \DateTime();
             if ($row['expiration'] < date('Y-m-d H:i', $time->format('U') - $time->getOffset())) {
                 self::delete($row['id']);
                 continue;
             }
         }
         $items[$row['id']] = $row;
     }
     if (!empty($items)) {
         $collectionItems = array();
         foreach ($items as &$row) {
             // Return only the item instead of a 2-dimensional array
             if ($limit == 1 && $row['item_type'] == $itemType && $row[$column] == $item) {
                 if ($format == self::FORMAT_NONE) {
                     return $row;
                 } else {
                     break;
                 }
             }
             // Check if this is a collection of the requested item type
             if ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) {
                 if (($collectionBackend = self::getBackend($row['item_type'])) && $collectionBackend instanceof Share_Backend_Collection) {
                     // Collections can be inside collections, check if the item is a collection
                     if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) {
                         $collectionItems[] = $row;
                     } else {
                         $collection = array();
                         $collection['item_type'] = $row['item_type'];
                         if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') {
                             $collection['path'] = basename($row['path']);
                         }
                         $row['collection'] = $collection;
                         // Fetch all of the children sources
                         $children = $collectionBackend->getChildren($row[$column]);
                         foreach ($children as $child) {
                             $childItem = $row;
                             $childItem['item_type'] = $itemType;
                             if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') {
                                 $childItem['item_source'] = $child['source'];
                                 $childItem['item_target'] = $child['target'];
                             }
                             if ($backend instanceof Share_Backend_File_Dependent) {
                                 if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') {
                                     $childItem['file_source'] = $child['source'];
                                 } else {
                                     $childItem['file_source'] = \OC_FileCache::getId($child['file_path']);
                                 }
                                 $childItem['file_target'] = \OC_Filesystem::normalizePath($child['file_path']);
                             }
                             if (isset($item)) {
                                 if ($childItem[$column] == $item) {
                                     // Return only the item instead of a 2-dimensional array
                                     if ($limit == 1) {
                                         if ($format == self::FORMAT_NONE) {
                                             return $childItem;
                                         } else {
                                             // Unset the items array and break out of both loops
                                             $items = array();
                                             $items[] = $childItem;
                                             break 2;
                                         }
                                     } else {
                                         $collectionItems[] = $childItem;
                                     }
                                 }
                             } else {
                                 $collectionItems[] = $childItem;
                             }
                         }
                     }
                 }
                 // Remove collection item
                 unset($items[$row['id']]);
             }
         }
         if (!empty($collectionItems)) {
             $items = array_merge($items, $collectionItems);
         }
         if ($format == self::FORMAT_NONE) {
             return $items;
         } else {
             if ($format == self::FORMAT_STATUSES) {
                 $statuses = array();
                 // Switch column to path for files and folders, used for determining statuses inside of folders
                 if ($itemType == 'file' || $itemType == 'folder') {
                     $column = 'path';
                 }
                 foreach ($items as $item) {
                     if ($item['share_type'] == self::SHARE_TYPE_LINK) {
                         $statuses[$item[$column]] = true;
                     } else {
                         if (!isset($statuses[$item[$column]])) {
                             $statuses[$item[$column]] = false;
                         }
                     }
                 }
                 return $statuses;
             } else {
                 return $backend->formatItems($items, $format, $parameters);
             }
         }
     } else {
         if ($limit == 1 || isset($uidOwner) && isset($item)) {
             return false;
         }
     }
     return array();
 }
Ejemplo n.º 9
0
 /**
  * @brief create recursively missing directories
  * @param string $filename $path to a file
  * @param \OC\Files\View $view view on data/user/
  */
 private static function createMissingDirectories($filename, $view)
 {
     $dirname = \OC_Filesystem::normalizePath(dirname($filename));
     $dirParts = explode('/', $dirname);
     $dir = "/files_versions";
     foreach ($dirParts as $part) {
         $dir = $dir . '/' . $part;
         if (!$view->file_exists($dir)) {
             $view->mkdir($dir);
         }
     }
 }
Ejemplo n.º 10
0
                }
            }
            break;
    }
}
if ($source) {
    if (substr($source, 0, 8) != 'https://' and substr($source, 0, 7) != 'http://') {
        OCP\JSON::error(array("data" => array("message" => "Not a valid source")));
        exit;
    }
    $ctx = stream_context_create(null, array('notification' => 'progress'));
    $sourceStream = fopen($source, 'rb', false, $ctx);
    $target = $dir . '/' . $filename;
    $result = OC_Filesystem::file_put_contents($target, $sourceStream);
    if ($result) {
        $target = OC_Filesystem::normalizePath($target);
        $meta = OC_FileCache::get($target);
        $mime = $meta['mimetype'];
        $id = OC_FileCache::getId($target);
        $eventSource->send('success', array('mime' => $mime, 'size' => OC_Filesystem::filesize($target), 'id' => $id));
    } else {
        $eventSource->send('error', "Error while downloading " . $source . ' to ' . $target);
    }
    $eventSource->close();
    exit;
} else {
    if ($content) {
        if (OC_Filesystem::file_put_contents($dir . '/' . $filename, $content)) {
            $meta = OC_FileCache::get($dir . '/' . $filename);
            $id = OC_FileCache::getId($dir . '/' . $filename);
            OCP\JSON::success(array("data" => array('content' => $content, 'id' => $id)));
Ejemplo n.º 11
0
 public function dummyHook($arguments)
 {
     $path = $arguments['path'];
     $this->assertEqual($path, OC_Filesystem::normalizePath($path));
     //the path passed to the hook should already be normalized
 }
Ejemplo n.º 12
0
 /**
  * @brief create directory recursively
  * @param string $path
  * @param \OC\Files\View $view
  */
 public static function mkdirr($path, $view)
 {
     $dirname = \OC_Filesystem::normalizePath(dirname($path));
     $dirParts = explode('/', $dirname);
     $dir = "";
     foreach ($dirParts as $part) {
         $dir = $dir . '/' . $part;
         if (!$view->file_exists($dir)) {
             $view->mkdir($dir);
         }
     }
 }
Ejemplo n.º 13
0
 /**
  * @brief get uid of the owners of the file and the path to the file
  * @param string $path Path of the file to check
  * @throws \Exception
  * @note $shareFilePath must be relative to data/UID/files. Files
  *       relative to /Shared are also acceptable
  * @return array
  */
 public function getUidAndFilename($path)
 {
     $pathinfo = pathinfo($path);
     $partfile = false;
     $parentFolder = false;
     if (array_key_exists('extension', $pathinfo) && $pathinfo['extension'] === 'part') {
         // if the real file exists we check this file
         $filePath = $this->userFilesDir . '/' . $pathinfo['dirname'] . '/' . $pathinfo['filename'];
         if ($this->view->file_exists($filePath)) {
             $pathToCheck = $pathinfo['dirname'] . '/' . $pathinfo['filename'];
         } else {
             // otherwise we look for the parent
             $pathToCheck = $pathinfo['dirname'];
             $parentFolder = true;
         }
         $partfile = true;
     } else {
         $pathToCheck = $path;
     }
     $view = new \OC\Files\View($this->userFilesDir);
     $fileOwnerUid = $view->getOwner($pathToCheck);
     // handle public access
     if ($this->isPublic) {
         $filename = $path;
         $fileOwnerUid = $this->userId;
         return array($fileOwnerUid, $filename);
     } else {
         // Check that UID is valid
         if (!\OCP\User::userExists($fileOwnerUid)) {
             throw new \Exception('Could not find owner (UID = "' . var_export($fileOwnerUid, 1) . '") of file "' . $path . '"');
         }
         // NOTE: Bah, this dependency should be elsewhere
         \OC\Files\Filesystem::initMountPoints($fileOwnerUid);
         // If the file owner is the currently logged in user
         if ($fileOwnerUid === $this->userId) {
             // Assume the path supplied is correct
             $filename = $path;
         } else {
             $info = $view->getFileInfo($pathToCheck);
             $ownerView = new \OC\Files\View('/' . $fileOwnerUid . '/files');
             // Fetch real file path from DB
             $filename = $ownerView->getPath($info['fileid']);
             if ($parentFolder) {
                 $filename = $filename . '/' . $pathinfo['filename'];
             }
             if ($partfile) {
                 $filename = $filename . '.' . $pathinfo['extension'];
             }
         }
         return array($fileOwnerUid, \OC_Filesystem::normalizePath($filename));
     }
 }
Ejemplo n.º 14
0
 /**
  * @brief abstraction layer for basic filesystem functions: wrapper for OC_Filestorage
  * @param string $operation
  * @param string #path
  * @param array (optional) hooks
  * @param mixed (optional) $extraParam
  * @return mixed
  *
  * This method takes requests for basic filesystem functions (e.g. reading & writing
  * files), processes hooks and proxies, sanitises paths, and finally passes them on to
  * OC_Filestorage for delegation to a storage backend for execution
  */
 private function basicOperation($operation, $path, $hooks = array(), $extraParam = null)
 {
     $postFix = substr($path, -1, 1) === '/' ? '/' : '';
     $absolutePath = OC_Filesystem::normalizePath($this->getAbsolutePath($path));
     if (OC_FileProxy::runPreProxies($operation, $absolutePath, $extraParam) and OC_Filesystem::isValidPath($path)) {
         $path = $this->getRelativePath($absolutePath);
         if ($path == null) {
             return false;
         }
         $internalPath = $this->getInternalPath($path . $postFix);
         $run = $this->runHooks($hooks, $path);
         if ($run and $storage = $this->getStorage($path . $postFix)) {
             if (!is_null($extraParam)) {
                 $result = $storage->{$operation}($internalPath, $extraParam);
             } else {
                 $result = $storage->{$operation}($internalPath);
             }
             $result = OC_FileProxy::runPostProxies($operation, $this->getAbsolutePath($path), $result);
             if (OC_Filesystem::$loaded and $this->fakeRoot == OC_Filesystem::getRoot()) {
                 if ($operation != 'fopen') {
                     //no post hooks for fopen, the file stream is still open
                     $this->runHooks($hooks, $path, true);
                 }
             }
             return $result;
         }
     }
     return null;
 }
Ejemplo n.º 15
0
 /**
  * @brief get uid of the owners of the file and the path to the file
  * @param string $path Path of the file to check
  * @throws \Exception
  * @note $shareFilePath must be relative to data/UID/files. Files
  *       relative to /Shared are also acceptable
  * @return array
  */
 public function getUidAndFilename($path)
 {
     $view = new \OC\Files\View($this->userFilesDir);
     $fileOwnerUid = $view->getOwner($path);
     // handle public access
     if ($this->isPublic) {
         $filename = $path;
         $fileOwnerUid = $GLOBALS['fileOwner'];
         return array($fileOwnerUid, $filename);
     } else {
         // Check that UID is valid
         if (!\OCP\User::userExists($fileOwnerUid)) {
             throw new \Exception('Could not find owner (UID = "' . var_export($fileOwnerUid, 1) . '") of file "' . $path . '"');
         }
         // NOTE: Bah, this dependency should be elsewhere
         \OC\Files\Filesystem::initMountPoints($fileOwnerUid);
         // If the file owner is the currently logged in user
         if ($fileOwnerUid === $this->userId) {
             // Assume the path supplied is correct
             $filename = $path;
         } else {
             $info = $view->getFileInfo($path);
             $ownerView = new \OC\Files\View('/' . $fileOwnerUid . '/files');
             // Fetch real file path from DB
             $filename = $ownerView->getPath($info['fileid']);
             // TODO: Check that this returns a path without including the user data dir
         }
         return array($fileOwnerUid, \OC_Filesystem::normalizePath($filename));
     }
 }