public function getSource($target)
 {
     $target = $this->datadir . $target;
     if (array_key_exists($target, $this->sourcePaths)) {
         return $this->sourcePaths[$target];
     } else {
         $source = OC_Share::getSource($target);
         $this->sourcePaths[$target] = $source;
         return $source;
     }
 }
<?php

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 {
 /**
  * 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);
         }
     }
 }
<?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

$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";
    }
}
<?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']);
 /**
  * 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);
             }
         }
     }
 }