예제 #1
0
 /**
  * Share an item, adds an entry into the database
  * @param $source The source location of the item
  * @param $uid_shared_with The user or group to share the item with
  * @param $permissions The permissions, use the constants WRITE and DELETE
  */
 public function __construct($source, $uid_shared_with, $permissions)
 {
     $uid_owner = OCP\USER::getUser();
     $query = OCP\DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
     // Check if this is a reshare and use the original source
     if ($result = OC_Share::getSource($source)) {
         $source = $result;
     }
     if ($uid_shared_with == self::PUBLICLINK) {
         $token = sha1("{$uid_shared_with}-{$source}");
         $query->execute(array($uid_owner, self::PUBLICLINK, $source, $token, $permissions));
         $this->token = $token;
     } else {
         if (OC_Group::groupExists($uid_shared_with)) {
             $gid = $uid_shared_with;
             $uid_shared_with = OC_Group::usersInGroup($gid);
             // Remove the owner from the list of users in the group
             $uid_shared_with = array_diff($uid_shared_with, array($uid_owner));
         } else {
             if (OCP\User::userExists($uid_shared_with)) {
                 $userGroups = OC_Group::getUserGroups($uid_owner);
                 // Check if the user is in one of the owner's groups
                 foreach ($userGroups as $group) {
                     if ($inGroup = OC_Group::inGroup($uid_shared_with, $group)) {
                         $gid = null;
                         $uid_shared_with = array($uid_shared_with);
                         break;
                     }
                 }
                 if (!$inGroup) {
                     throw new Exception("You can't share with " . $uid_shared_with);
                 }
             } else {
                 throw new Exception($uid_shared_with . " is not a user");
             }
         }
         foreach ($uid_shared_with as $uid) {
             // Check if this item is already shared with the user
             $checkSource = OCP\DB::prepare("SELECT source FROM *PREFIX*sharing WHERE source = ? AND uid_shared_with " . self::getUsersAndGroups($uid, false));
             $resultCheckSource = $checkSource->execute(array($source))->fetchAll();
             // TODO Check if the source is inside a folder
             if (count($resultCheckSource) > 0) {
                 if (!isset($gid)) {
                     throw new Exception("This item is already shared with " . $uid);
                 } else {
                     // Skip this user if sharing with a group
                     continue;
                 }
             }
             // Check if the target already exists for the user, if it does append a number to the name
             $sharedFolder = '/' . $uid . '/files/Shared';
             $target = $sharedFolder . "/" . basename($source);
             $checkTarget = OCP\DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with " . self::getUsersAndGroups($uid, false) . " LIMIT 1");
             $result = $checkTarget->execute(array($target))->fetchAll();
             if (count($result) > 0) {
                 if ($pos = strrpos($target, ".")) {
                     $name = substr($target, 0, $pos);
                     $ext = substr($target, $pos);
                 } else {
                     $name = $target;
                     $ext = "";
                 }
                 $counter = 1;
                 while (count($result) > 0) {
                     $target = $name . "_" . $counter . $ext;
                     $result = $checkTarget->execute(array($target))->fetchAll();
                     $counter++;
                 }
             }
             if (isset($gid)) {
                 $uid = $uid . "@" . $gid;
             }
             $query->execute(array($uid_owner, $uid, $source, $target, $permissions));
             // Update mtime of shared folder to invoke a file cache rescan
             $rootView = new OC_FilesystemView('/');
             if (!$rootView->is_dir($sharedFolder)) {
                 if (!$rootView->is_dir('/' . $uid . '/files')) {
                     OC_Util::tearDownFS();
                     OC_Util::setupFS($uid);
                     OC_Util::tearDownFS();
                 }
                 $rootView->mkdir($sharedFolder);
             }
             $rootView->touch($sharedFolder);
         }
     }
 }
예제 #2
0
require_once OC::$APPSROOT . '/apps/files_sharing/lib_share.php';
OCP\JSON::checkAppEnabled('files_sharing');
OCP\JSON::checkLoggedIn();
$userDirectory = '/' . OCP\USER::getUser() . '/files';
$sources = explode(';', $_POST['sources']);
$uid_shared_with = $_POST['uid_shared_with'];
$permissions = $_POST['permissions'];
foreach ($sources as $source) {
    $file = OC_FileCache::get($source);
    $path = ltrim($source, '/');
    $source = $userDirectory . $source;
    // Check if the file exists or if the file is being reshared
    if ($source && $file['encrypted'] == false && (OC_FILESYSTEM::file_exists($path) && OC_FILESYSTEM::is_readable($path) || OC_Share::getSource($source))) {
        try {
            $shared = new OC_Share($source, $uid_shared_with, $permissions);
            // If this is a private link, return the token
            if ($uid_shared_with == OC_Share::PUBLICLINK) {
                OCP\JSON::success(array('data' => $shared->getToken()));
            } else {
                OCP\JSON::success();
            }
        } catch (Exception $exception) {
            OCP\Util::writeLog('files_sharing', 'Unexpected Error : ' . $exception->getMessage(), OCP\Util::ERROR);
            OCP\JSON::error(array('data' => array('message' => $exception->getMessage())));
        }
    } else {
        if ($file['encrypted'] == true) {
            OCP\JSON::error(array('data' => array('message' => 'Encrypted files cannot be shared')));
        } else {
            OCP\Util::writeLog('files_sharing', 'File does not exist or is not readable :' . $source, OCP\Util::ERROR);
require_once '../../../lib/base.php';
OC_JSON::checkAppEnabled('files_sharing');
require_once '../lib_share.php';
$userDirectory = "/" . OC_User::getUser() . "/files";
$source = $userDirectory . $_GET['source'];
$path = $source;
if ($users = OC_Share::getMySharedItem($source)) {
    for ($i = 0; $i < count($users); $i++) {
        if ($users[$i]['uid_shared_with'] == OC_Share::PUBLICLINK) {
            $users[$i]['token'] = OC_Share::getTokenFromSource($source);
        }
    }
}
$source = dirname($source);
while ($source != "" && $source != "/" && $source != "." && $source != $userDirectory) {
    if ($values = OC_Share::getMySharedItem($source)) {
        $values = array_values($values);
        $parentUsers = array();
        for ($i = 0; $i < count($values); $i++) {
            if ($values[$i]['uid_shared_with'] == OC_Share::PUBLICLINK) {
                $values[$i]['token'] = OC_Share::getTokenFromSource($source) . "&path=" . substr($path, strlen($source));
            }
            $parentUsers[basename($source) . "-" . $i] = $values[$i];
        }
        $users = array_merge($users, $parentUsers);
    }
    $source = dirname($source);
}
if (!empty($users)) {
    OC_JSON::encodedPrint($users);
}
예제 #4
0
<?php

require_once OC::$APPSROOT . '/apps/files_sharing/lib_share.php';
OCP\JSON::checkAppEnabled('files_sharing');
OCP\JSON::checkLoggedIn();
$source = '/' . OCP\USER::getUser() . '/files' . $_POST['source'];
$uid_shared_with = $_POST['uid_shared_with'];
OC_Share::unshare($source, $uid_shared_with);
OCP\JSON::success();
<?php

$RUNTIME_NOAPPS = true;
//no need to load the apps
$RUNTIME_NOSETUPFS = true;
//don't setup the fs yet
require_once '../../lib/base.php';
OC_JSON::checkAppEnabled('files_sharing');
require_once 'lib_share.php';
//get the path of the shared file
$token = $_GET['token'];
$source = OC_Share::getSource($token);
if ($source !== false) {
    // TODO Manipulating the string may not be the best choice. Is there an alternative?
    $user = substr($source, 1, strpos($source, "/", 1) - 1);
    OC_Util::setupFS($user);
    $source = substr($source, strlen("/" . $user . "/files"));
    $subPath = isset($_GET['path']) ? $_GET['path'] : '';
    $root = $source;
    $source .= $subPath;
    if (!OC_Filesystem::file_exists($source)) {
        header("HTTP/1.0 404 Not Found");
        $tmpl = new OC_Template("", "404", "guest");
        $tmpl->assign("file", $subPath);
        $tmpl->printPage();
        exit;
    }
    if (OC_Filesystem::is_dir($source)) {
        $files = array();
        $rootLength = strlen($root);
        foreach (OC_Files::getdirectorycontent($source) as $i) {
<?php

/**
 * ownCloud
 *
 * @author Michael Gapczynski
 * @copyright 2011 Michael Gapczynski GapczynskiM@gmail.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
require_once '../../lib/base.php';
require_once 'lib_share.php';
OC_Util::checkLoggedIn();
OC_Util::checkAppEnabled('files_sharing');
OC_App::setActiveNavigationEntry("files_sharing_list");
OC_Util::addScript("files_sharing", "list");
$tmpl = new OC_Template("files_sharing", "list", "user");
$tmpl->assign("shared_items", OC_Share::getMySharedItems());
$tmpl->printPage();
예제 #7
0
<?php

$RUNTIME_NOSETUPFS = true;
//don't setup the fs yet
// only need authentication apps
$RUNTIME_APPTYPES = array('authentication');
OC_App::loadApps($RUNTIME_APPTYPES);
OCP\JSON::checkAppEnabled('files_sharing');
require_once 'lib_share.php';
//get the path of the shared file
if (isset($_GET['token']) && ($source = OC_Share::getSource($_GET['token']))) {
    $token = $_GET['token'];
    // TODO Manipulating the string may not be the best choice. Is there an alternative?
    $user = substr($source, 1, strpos($source, "/", 1) - 1);
    OC_Util::setupFS($user);
    $source = substr($source, strlen("/" . $user . "/files"));
    $subPath = isset($_GET['path']) ? $_GET['path'] : '';
    $root = $source;
    $source .= $subPath;
    if (!OC_Filesystem::file_exists($source)) {
        header("HTTP/1.0 404 Not Found");
        $tmpl = new OCP\Template("", "404", "guest");
        $tmpl->assign("file", $subPath);
        $tmpl->printPage();
        exit;
    }
    if (OC_Filesystem::is_dir($source)) {
        $files = array();
        $rootLength = strlen($root);
        foreach (OC_Files::getdirectorycontent($source) as $i) {
            $i['date'] = OCP\Util::formatDate($i['mtime']);
<?php

$RUNTIME_NOAPPS = true;
require_once '../../../lib/base.php';
OC_JSON::checkAppEnabled('files_sharing');
require_once '../lib_share.php';
$userDirectory = "/" . OC_User::getUser() . "/files";
$sources = explode(";", $_POST['sources']);
$uid_shared_with = $_POST['uid_shared_with'];
$permissions = $_POST['permissions'];
foreach ($sources as $source) {
    // Make sure file exists and can be shared
    if ($source && OC_FILESYSTEM::file_exists($source) && OC_FILESYSTEM::is_readable($source)) {
        $source = $userDirectory . $source;
        // If the file doesn't exist, it may be shared with the current user
    } else {
        if (!($source = OC_Share::getSource($userDirectory . $source))) {
            echo "false";
        }
    }
    try {
        $shared = new OC_Share($source, $uid_shared_with, $permissions);
        if ($uid_shared_with == OC_Share::PUBLICLINK) {
            echo $shared->getToken();
        }
    } catch (Exception $exception) {
        echo "false";
    }
}
예제 #9
0
 /**
  * expire old versions of a file.
  */
 public static function expire($filename)
 {
     if (\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED) == 'true') {
         if (\OCP\App::isEnabled('files_sharing') && ($source = \OC_Share::getSource('/' . \OCP\User::getUser() . '/files' . $filename))) {
             $pos = strpos($source, '/files', 1);
             $uid = substr($source, 1, $pos - 1);
             $filename = substr($source, $pos + 6);
         } else {
             $uid = \OCP\User::getUser();
         }
         $versionsfoldername = \OCP\Config::getSystemValue('datadirectory') . '/' . $uid . '/' . \OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER);
         // check for old versions
         $matches = glob($versionsfoldername . '/' . $filename . '.v*');
         if (count($matches) > \OCP\Config::getSystemValue('files_versionmaxversions', Storage::DEFAULTMAXVERSIONS)) {
             $numbertodelete = count($matches - \OCP\Config::getSystemValue('files_versionmaxversions', Storage::DEFAULTMAXVERSIONS));
             // delete old versions of a file
             $deleteitems = array_slice($matches, 0, $numbertodelete);
             foreach ($deleteitems as $de) {
                 unlink($versionsfoldername . '/' . $filename . '.v' . $de);
             }
         }
     }
 }
 public function rename($path1, $path2)
 {
     $oldTarget = $this->datadir . $path1;
     $newTarget = $this->datadir . $path2;
     // Check if the item is inside a shared folder
     if ($folders = OC_Share::getParentFolders($oldTarget)) {
         $root1 = substr($path1, 0, strpos($path1, "/"));
         $root2 = substr($path1, 0, strpos($path2, "/"));
         // Prevent items from being moved into different shared folders until versioning (cut and paste) and prevent items going into 'Shared'
         if ($root1 !== $root2) {
             return false;
             // Check if both paths have write permission
         } else {
             if ($this->is_writeable($path1) && $this->is_writeable($path2)) {
                 $oldSource = $this->getSource($path1);
                 $newSource = $folders['source'] . substr($newTarget, strlen($folders['target']));
                 if ($oldSource) {
                     $storage = OC_Filesystem::getStorage($oldSource);
                     return $storage->rename($this->getInternalPath($oldSource), $this->getInternalPath($newSource));
                 }
                 // If the user doesn't have write permission, items can only be renamed and not moved
             } else {
                 if (dirname($path1) !== dirname($path2)) {
                     return false;
                     // The item will be renamed in the database, but won't be touched on the owner's filesystem
                 } else {
                     OC_Share::pullOutOfFolder($oldTarget, $newTarget);
                     // If this is a folder being renamed, call setTarget in case there are any database entries inside the folder
                     if (self::is_dir($path1)) {
                         OC_Share::setTarget($oldTarget, $newTarget);
                     }
                 }
             }
         }
     } else {
         OC_Share::setTarget($oldTarget, $newTarget);
     }
     $this->clearFolderSizeCache($this->getInternalPath($oldTarget));
     $this->clearFolderSizeCache($this->getInternalPath($newTarget));
     return true;
 }
예제 #11
0
<?php

require_once OC::$APPSROOT . '/apps/files_sharing/lib_share.php';
OCP\JSON::checkAppEnabled('files_sharing');
OCP\JSON::checkLoggedIn();
$items = array();
$userDirectory = '/' . OCP\USER::getUser() . '/files';
$dirLength = strlen($userDirectory);
if ($rows = OC_Share::getMySharedItems()) {
    for ($i = 0; $i < count($rows); $i++) {
        $source = $rows[$i]['source'];
        // Strip out user directory
        $item = substr($source, $dirLength);
        if ($rows[$i]['uid_shared_with'] == OC_Share::PUBLICLINK) {
            $items[$item] = true;
        } else {
            if (!isset($items[$item])) {
                $items[$item] = false;
            }
        }
    }
}
OCP\JSON::success(array('data' => $items));
예제 #12
0
<?php

require_once OC::$APPSROOT . '/apps/files_sharing/lib_share.php';
OCP\JSON::checkAppEnabled('files_sharing');
OCP\JSON::checkLoggedIn();
OCP\JSON::callCheck();
$source = '/' . OCP\USER::getUser() . '/files' . $_POST['source'];
$uid_shared_with = $_POST['uid_shared_with'];
$permissions = $_POST['permissions'];
OC_Share::setPermissions($source, $uid_shared_with, $permissions);
OCP\JSON::success();
예제 #13
0
<?php

require_once OC::$APPSROOT . '/apps/files_sharing/lib_share.php';
OCP\JSON::checkAppEnabled('files_sharing');
OCP\JSON::checkLoggedIn();
$item = array();
$userDirectory = '/' . OCP\USER::getUser() . '/files';
$source = $userDirectory . $_GET['item'];
$path = $source;
// Search for item and shared parent folders
while ($path != $userDirectory) {
    if ($rows = OC_Share::getMySharedItem($path)) {
        for ($i = 0; $i < count($rows); $i++) {
            $uid_shared_with = $rows[$i]['uid_shared_with'];
            if ($uid_shared_with == OC_Share::PUBLICLINK && !isset($item['privateLink'])) {
                $token = OC_Share::getTokenFromSource($path);
                if ($path == $source) {
                    $item['privateLink'] = $token;
                } else {
                    // If in parent folder, include a path parameter to get direct access to file
                    $item['privateLink'] = $token . '&path=' . str_replace('%2F', '/', str_replace('+', '%20', urlencode(substr($source, strlen($path)))));
                }
            } else {
                // Check if uid_shared_with is a group
                if (($pos = strpos($uid_shared_with, '@')) !== false) {
                    $gid = substr($uid_shared_with, $pos + 1);
                    // Include users in the group so the users can be removed from the list of people to share with
                    if ($path == $source) {
                        $group = array(array('gid' => $gid, 'permissions' => $rows[$i]['permissions'], 'users' => OC_Group::usersInGroup($gid), 'parentFolder' => false));
                    } else {
                        $group = array(array('gid' => $gid, 'permissions' => $rows[$i]['permissions'], 'users' => OC_Group::usersInGroup($gid), 'parentFolder' => basename($path)));