Advertisement
ccheveaux

SharedMount.php

Sep 23rd, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.54 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author Björn Schießle <bjoern@schiessle.org>
  4.  * @author Joas Schilling <nickvergessen@owncloud.com>
  5.  * @author Morris Jobke <hey@morrisjobke.de>
  6.  * @author Robin Appelman <icewind@owncloud.com>
  7.  * @author Roeland Jago Douma <rullzer@owncloud.com>
  8.  *
  9.  * @copyright Copyright (c) 2016, ownCloud, Inc.
  10.  * @license AGPL-3.0
  11.  *
  12.  * This code is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU Affero General Public License, version 3,
  14.  * as published by the Free Software Foundation.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19.  * GNU Affero General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU Affero General Public License, version 3,
  22.  * along with this program.  If not, see <http://www.gnu.org/licenses/>
  23.  *
  24.  */
  25.  
  26. namespace OCA\Files_Sharing;
  27.  
  28. use OC\Files\Filesystem;
  29. use OC\Files\Mount\MountPoint;
  30. use OC\Files\Mount\MoveableMount;
  31. use OC\Files\View;
  32.  
  33. /**
  34.  * Shared mount points can be moved by the user
  35.  */
  36. class SharedMount extends MountPoint implements MoveableMount {
  37.     /**
  38.      * @var \OC\Files\Storage\Shared $storage
  39.      */
  40.     protected $storage = null;
  41.  
  42.     /**
  43.      * @var \OC\Files\View
  44.      */
  45.     private $recipientView;
  46.  
  47.     /**
  48.      * @var string
  49.      */
  50.     private $user;
  51.  
  52.     /** @var \OCP\Share\IShare */
  53.     private $superShare;
  54.  
  55.     /** @var \OCP\Share\IShare[] */
  56.     private $groupedShares;
  57.  
  58.     /**
  59.      * @param string $storage
  60.      * @param SharedMount[] $mountpoints
  61.      * @param array|null $arguments
  62.      * @param \OCP\Files\Storage\IStorageFactory $loader
  63.      */
  64.     public function __construct($storage, array $mountpoints, $arguments = null, $loader = null) {
  65.         $this->user = $arguments['user'];
  66.         $this->recipientView = new View('/' . $this->user . '/files');
  67.  
  68.         $this->superShare = $arguments['superShare'];
  69.         $this->groupedShares = $arguments['groupedShares'];
  70.  
  71.         $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints);
  72.         $absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
  73.         $arguments['ownerView'] = new View('/' . $this->superShare->getShareOwner() . '/files');
  74.         parent::__construct($storage, $absMountPoint, $arguments, $loader);
  75.     }
  76.  
  77.     /**
  78.      * check if the parent folder exists otherwise move the mount point up
  79.      *
  80.      * @param \OCP\Share\IShare $share
  81.      * @param SharedMount[] $mountpoints
  82.      * @return string
  83.      */
  84.     private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints) {
  85.  
  86.         $mountPoint = basename($share->getTarget());
  87.         $parent = dirname($share->getTarget());
  88.  
  89.         if (!$this->recipientView->is_dir($parent)) {
  90.             $parent = Helper::getShareFolder($this->recipientView);
  91.         }
  92.  
  93.         $newMountPoint = $this->generateUniqueTarget(
  94.             \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
  95.             $this->recipientView,
  96.             $mountpoints
  97.         );
  98.  
  99.         if ($newMountPoint !== $share->getTarget()) {
  100.             $this->updateFileTarget($newMountPoint, $share);
  101.         }
  102.  
  103.         return $newMountPoint;
  104.     }
  105.  
  106.     /**
  107.      * update fileTarget in the database if the mount point changed
  108.      *
  109.      * @param string $newPath
  110.      * @param \OCP\Share\IShare $share
  111.      * @return bool
  112.      */
  113.     private function updateFileTarget($newPath, &$share) {
  114.         $share->setTarget($newPath);
  115.  
  116.         foreach ($this->groupedShares as $share) {
  117.             $share->setTarget($newPath);
  118.             \OC::$server->getShareManager()->moveShare($share, $this->user);
  119.         }
  120.     }
  121.  
  122.  
  123.     /**
  124.      * @param string $path
  125.      * @param View $view
  126.      * @param SharedMount[] $mountpoints
  127.      * @return mixed
  128.      */
  129.     private function generateUniqueTarget($path, $view, array $mountpoints) {
  130.         $pathinfo = pathinfo($path);
  131.         $ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : '';
  132.         $name = $pathinfo['filename'];
  133.         $dir = $pathinfo['dirname'];
  134.  
  135.  
  136. // INFO SUR LE BUG DES 2
  137. $targets = array_map(function($mountPoint) {
  138. return $mountPoint->getShare()->getTarget();
  139. }, $mountpoints);
  140. // INFO SUR LE BUG DES 2
  141.  
  142.  
  143.         // Helper function to find existing mount points
  144.         $mountpointExists = function($path) use ($mountpoints) {
  145.             foreach ($mountpoints as $mountpoint) {
  146. //DEBUG 2 ?
  147.                 if ($mountpoint->getShare()->getTarget() === $path) {
  148. //DEBUG 2 ?
  149. //              if ($mountpoint->getShare()->getTarget() === $path && $mountpoint->getShare()->getId() !== $this->superShare->getId()) {
  150. //DEBUG 2 ?
  151.                     return true;
  152.                 }
  153.             }
  154.             return false;
  155.         };
  156.  
  157. //DEBUG 2 ?
  158. //return $path;
  159. //DEBUG 2 ?
  160.  
  161.  
  162.         $i = 2;
  163.         while ($view->file_exists($path) || $mountpointExists($path)) {
  164.  
  165.  
  166.  
  167. // INFO SUR LE BUG DES 2
  168. /*$debugInfo = [
  169. 'user' => $this->user,
  170. 'path' => $path,
  171. 'file_exists' => $view->file_exists($path),
  172. 'mountpointExists' => $mountpointExists($path),
  173. 'targets' => $targets,
  174. ];
  175. \OC::$server->getLogger()->debug('SharedMount:: ' . json_encode($debugInfo), array('app' => 'DEBUG'));*/
  176. // INFO SUR LE BUG DES 2
  177.  
  178.  
  179.  
  180.             $path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
  181.             $i++;
  182.         }
  183.  
  184.         return $path;
  185.     }
  186.  
  187.     /**
  188.      * Format a path to be relative to the /user/files/ directory
  189.      *
  190.      * @param string $path the absolute path
  191.      * @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
  192.      * @throws \OCA\Files_Sharing\Exceptions\BrokenPath
  193.      */
  194.     protected function stripUserFilesPath($path) {
  195.         $trimmed = ltrim($path, '/');
  196.         $split = explode('/', $trimmed);
  197.  
  198.         // it is not a file relative to data/user/files
  199.         if (count($split) < 3 || $split[1] !== 'files') {
  200.             \OCP\Util::writeLog('file sharing',
  201.                 'Can not strip userid and "files/" from path: ' . $path,
  202.                 \OCP\Util::ERROR);
  203.             throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
  204.         }
  205.  
  206.         // skip 'user' and 'files'
  207.         $sliced = array_slice($split, 2);
  208.         $relPath = implode('/', $sliced);
  209.  
  210.         return '/' . $relPath;
  211.     }
  212.  
  213.     /**
  214.      * Move the mount point to $target
  215.      *
  216.      * @param string $target the target mount point
  217.      * @return bool
  218.      */
  219.     public function moveMount($target) {
  220.  
  221.         $relTargetPath = $this->stripUserFilesPath($target);
  222.         $share = $this->storage->getShare();
  223.  
  224.         $result = true;
  225.  
  226.         try {
  227.             $this->updateFileTarget($relTargetPath, $share);
  228.             $this->setMountPoint($target);
  229.             $this->storage->setMountPoint($relTargetPath);
  230.         } catch (\Exception $e) {
  231.             \OCP\Util::writeLog('file sharing',
  232.                 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
  233.                 \OCP\Util::ERROR);
  234.         }
  235.  
  236.         return $result;
  237.     }
  238.  
  239.     /**
  240.      * Remove the mount points
  241.      *
  242.      * @return bool
  243.      */
  244.     public function removeMount() {
  245.         $mountManager = \OC\Files\Filesystem::getMountManager();
  246.         /** @var $storage \OC\Files\Storage\Shared */
  247.         $storage = $this->getStorage();
  248.         $result = $storage->unshareStorage();
  249.         $mountManager->removeMount($this->mountPoint);
  250.  
  251.         return $result;
  252.     }
  253.  
  254.     /**
  255.      * @return \OCP\Share\IShare
  256.      */
  257.     public function getShare() {
  258.         return $this->superShare;
  259.     }
  260.  
  261.     /**
  262.      * Get the file id of the root of the storage
  263.      *
  264.      * @return int
  265.      */
  266.     public function getStorageRootId() {
  267.         return $this->getShare()->getNodeId();
  268.     }
  269.  
  270.     public function getStorageNumericId() {
  271.         $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  272.         $query->select('storage')
  273.             ->from('filecache')
  274.             ->where($query->expr()->eq('fileid', $query->createNamedParameter($this->getStorageRootId())));
  275.  
  276.         $result = $query->execute();
  277.         $row = $result->fetch();
  278.         $result->closeCursor();
  279.         if ($row) {
  280.             return $row['storage'];
  281.         }
  282.         return -1;
  283.     }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement