You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.php 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Björn Schießle <schiessle@owncloud.com>
  5. * @author Frank Karlitschek <frank@owncloud.org>
  6. * @author Joas Schilling <nickvergessen@owncloud.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@owncloud.com>
  9. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Philipp Kapfer <philipp.kapfer@gmx.at>
  12. * @author Robin Appelman <icewind@owncloud.com>
  13. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Vincent Petry <pvince81@owncloud.com>
  16. *
  17. * @copyright Copyright (c) 2015, ownCloud, Inc.
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. /**
  34. * Class to configure mount.json globally and for users
  35. */
  36. class OC_Mount_Config {
  37. // TODO: make this class non-static and give it a proper namespace
  38. const MOUNT_TYPE_GLOBAL = 'global';
  39. const MOUNT_TYPE_GROUP = 'group';
  40. const MOUNT_TYPE_USER = 'user';
  41. const MOUNT_TYPE_PERSONAL = 'personal';
  42. // getBackendStatus return types
  43. const STATUS_SUCCESS = 0;
  44. const STATUS_ERROR = 1;
  45. // whether to skip backend test (for unit tests, as this static class is not mockable)
  46. public static $skipTest = false;
  47. private static $backends = array();
  48. /**
  49. * @param string $class
  50. * @param array $definition
  51. * @return bool
  52. */
  53. public static function registerBackend($class, $definition) {
  54. if (!isset($definition['backend'])) {
  55. return false;
  56. }
  57. OC_Mount_Config::$backends[$class] = $definition;
  58. return true;
  59. }
  60. /**
  61. * Setup backends
  62. *
  63. * @return array of previously registered backends
  64. */
  65. public static function setUp($backends = array()) {
  66. $backup = self::$backends;
  67. self::$backends = $backends;
  68. return $backup;
  69. }
  70. /**
  71. * Get details on each of the external storage backends, used for the mount config UI
  72. * If a custom UI is needed, add the key 'custom' and a javascript file with that name will be loaded
  73. * If the configuration parameter should be secret, add a '*' to the beginning of the value
  74. * If the configuration parameter is a boolean, add a '!' to the beginning of the value
  75. * If the configuration parameter is optional, add a '&' to the beginning of the value
  76. * If the configuration parameter is hidden, add a '#' to the beginning of the value
  77. *
  78. * @return array
  79. */
  80. public static function getBackends() {
  81. $sortFunc = function ($a, $b) {
  82. return strcasecmp($a['backend'], $b['backend']);
  83. };
  84. $backEnds = array();
  85. foreach (OC_Mount_Config::$backends as $class => $backend) {
  86. if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) {
  87. if (!method_exists($class, 'checkDependencies')) {
  88. \OCP\Util::writeLog('files_external',
  89. "Backend class $class has dependencies but doesn't provide method checkDependencies()",
  90. \OCP\Util::DEBUG);
  91. continue;
  92. } elseif ($class::checkDependencies() !== true) {
  93. continue;
  94. }
  95. }
  96. $backEnds[$class] = $backend;
  97. }
  98. uasort($backEnds, $sortFunc);
  99. return $backEnds;
  100. }
  101. /**
  102. * Hook that mounts the given user's visible mount points
  103. *
  104. * @param array $data
  105. */
  106. public static function initMountPointsHook($data) {
  107. self::addStorageIdToConfig(null);
  108. if ($data['user']) {
  109. self::addStorageIdToConfig($data['user']);
  110. $user = \OC::$server->getUserManager()->get($data['user']);
  111. if (!$user) {
  112. \OC_Log::write(
  113. 'files_external',
  114. 'Cannot init external mount points for non-existant user "' . $data['user'] . '".',
  115. \OC_Log::WARN
  116. );
  117. return;
  118. }
  119. $userView = new \OC\Files\View('/' . $user->getUID() . '/files');
  120. $changePropagator = new \OC\Files\Cache\ChangePropagator($userView);
  121. $etagPropagator = new \OCA\Files_External\EtagPropagator($user, $changePropagator, \OC::$server->getConfig());
  122. $etagPropagator->propagateDirtyMountPoints();
  123. \OCP\Util::connectHook(
  124. \OC\Files\Filesystem::CLASSNAME,
  125. \OC\Files\Filesystem::signal_create_mount,
  126. $etagPropagator, 'updateHook');
  127. \OCP\Util::connectHook(
  128. \OC\Files\Filesystem::CLASSNAME,
  129. \OC\Files\Filesystem::signal_delete_mount,
  130. $etagPropagator, 'updateHook');
  131. }
  132. }
  133. /**
  134. * Returns the mount points for the given user.
  135. * The mount point is relative to the data directory.
  136. *
  137. * @param string $user user
  138. * @return array of mount point string as key, mountpoint config as value
  139. */
  140. public static function getAbsoluteMountPoints($user) {
  141. $mountPoints = array();
  142. $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
  143. $backends = self::getBackends();
  144. // Load system mount points
  145. $mountConfig = self::readData();
  146. // Global mount points (is this redundant?)
  147. if (isset($mountConfig[self::MOUNT_TYPE_GLOBAL])) {
  148. foreach ($mountConfig[self::MOUNT_TYPE_GLOBAL] as $mountPoint => $options) {
  149. $options['personal'] = false;
  150. $options['options'] = self::decryptPasswords($options['options']);
  151. if (!isset($options['priority'])) {
  152. $options['priority'] = $backends[$options['class']]['priority'];
  153. }
  154. // Override if priority greater
  155. if ((!isset($mountPoints[$mountPoint]))
  156. || ($options['priority'] >= $mountPoints[$mountPoint]['priority'])
  157. ) {
  158. $options['priority_type'] = self::MOUNT_TYPE_GLOBAL;
  159. $options['backend'] = $backends[$options['class']]['backend'];
  160. $mountPoints[$mountPoint] = $options;
  161. }
  162. }
  163. }
  164. // All user mount points
  165. if (isset($mountConfig[self::MOUNT_TYPE_USER]) && isset($mountConfig[self::MOUNT_TYPE_USER]['all'])) {
  166. $mounts = $mountConfig[self::MOUNT_TYPE_USER]['all'];
  167. foreach ($mounts as $mountPoint => $options) {
  168. $mountPoint = self::setUserVars($user, $mountPoint);
  169. foreach ($options as &$option) {
  170. $option = self::setUserVars($user, $option);
  171. }
  172. $options['personal'] = false;
  173. $options['options'] = self::decryptPasswords($options['options']);
  174. if (!isset($options['priority'])) {
  175. $options['priority'] = $backends[$options['class']]['priority'];
  176. }
  177. // Override if priority greater
  178. if ((!isset($mountPoints[$mountPoint]))
  179. || ($options['priority'] >= $mountPoints[$mountPoint]['priority'])
  180. ) {
  181. $options['priority_type'] = self::MOUNT_TYPE_GLOBAL;
  182. $options['backend'] = $backends[$options['class']]['backend'];
  183. $mountPoints[$mountPoint] = $options;
  184. }
  185. }
  186. }
  187. // Group mount points
  188. if (isset($mountConfig[self::MOUNT_TYPE_GROUP])) {
  189. foreach ($mountConfig[self::MOUNT_TYPE_GROUP] as $group => $mounts) {
  190. if (\OC_Group::inGroup($user, $group)) {
  191. foreach ($mounts as $mountPoint => $options) {
  192. $mountPoint = self::setUserVars($user, $mountPoint);
  193. foreach ($options as &$option) {
  194. $option = self::setUserVars($user, $option);
  195. }
  196. $options['personal'] = false;
  197. $options['options'] = self::decryptPasswords($options['options']);
  198. if (!isset($options['priority'])) {
  199. $options['priority'] = $backends[$options['class']]['priority'];
  200. }
  201. // Override if priority greater or if priority type different
  202. if ((!isset($mountPoints[$mountPoint]))
  203. || ($options['priority'] >= $mountPoints[$mountPoint]['priority'])
  204. || ($mountPoints[$mountPoint]['priority_type'] !== self::MOUNT_TYPE_GROUP)
  205. ) {
  206. $options['priority_type'] = self::MOUNT_TYPE_GROUP;
  207. $options['backend'] = $backends[$options['class']]['backend'];
  208. $mountPoints[$mountPoint] = $options;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. // User mount points
  215. if (isset($mountConfig[self::MOUNT_TYPE_USER])) {
  216. foreach ($mountConfig[self::MOUNT_TYPE_USER] as $mountUser => $mounts) {
  217. if (strtolower($mountUser) === strtolower($user)) {
  218. foreach ($mounts as $mountPoint => $options) {
  219. $mountPoint = self::setUserVars($user, $mountPoint);
  220. foreach ($options as &$option) {
  221. $option = self::setUserVars($user, $option);
  222. }
  223. $options['personal'] = false;
  224. $options['options'] = self::decryptPasswords($options['options']);
  225. if (!isset($options['priority'])) {
  226. $options['priority'] = $backends[$options['class']]['priority'];
  227. }
  228. // Override if priority greater or if priority type different
  229. if ((!isset($mountPoints[$mountPoint]))
  230. || ($options['priority'] >= $mountPoints[$mountPoint]['priority'])
  231. || ($mountPoints[$mountPoint]['priority_type'] !== self::MOUNT_TYPE_USER)
  232. ) {
  233. $options['priority_type'] = self::MOUNT_TYPE_USER;
  234. $options['backend'] = $backends[$options['class']]['backend'];
  235. $mountPoints[$mountPoint] = $options;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. $personalBackends = self::getPersonalBackends();
  242. // Load personal mount points
  243. $mountConfig = self::readData($user);
  244. if (isset($mountConfig[self::MOUNT_TYPE_USER][$user])) {
  245. foreach ($mountConfig[self::MOUNT_TYPE_USER][$user] as $mountPoint => $options) {
  246. if (isset($personalBackends[$options['class']])) {
  247. $options['personal'] = true;
  248. $options['options'] = self::decryptPasswords($options['options']);
  249. // Always override previous config
  250. $options['priority_type'] = self::MOUNT_TYPE_PERSONAL;
  251. $options['backend'] = $backends[$options['class']]['backend'];
  252. $mountPoints[$mountPoint] = $options;
  253. }
  254. }
  255. }
  256. return $mountPoints;
  257. }
  258. /**
  259. * fill in the correct values for $user
  260. *
  261. * @param string $user
  262. * @param string $input
  263. * @return string
  264. */
  265. private static function setUserVars($user, $input) {
  266. return str_replace('$user', $user, $input);
  267. }
  268. /**
  269. * Get details on each of the external storage backends, used for the mount config UI
  270. * Some backends are not available as a personal backend, f.e. Local and such that have
  271. * been disabled by the admin.
  272. *
  273. * If a custom UI is needed, add the key 'custom' and a javascript file with that name will be loaded
  274. * If the configuration parameter should be secret, add a '*' to the beginning of the value
  275. * If the configuration parameter is a boolean, add a '!' to the beginning of the value
  276. * If the configuration parameter is optional, add a '&' to the beginning of the value
  277. * If the configuration parameter is hidden, add a '#' to the beginning of the value
  278. *
  279. * @return array
  280. */
  281. public static function getPersonalBackends() {
  282. // Check whether the user has permissions to add personal storage backends
  283. // return an empty array if this is not the case
  284. if (OCP\Config::getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') {
  285. return array();
  286. }
  287. $backEnds = self::getBackends();
  288. // Remove local storage and other disabled storages
  289. unset($backEnds['\OC\Files\Storage\Local']);
  290. $allowedBackEnds = explode(',', OCP\Config::getAppValue('files_external', 'user_mounting_backends', ''));
  291. foreach ($backEnds as $backend => $null) {
  292. if (!in_array($backend, $allowedBackEnds)) {
  293. unset($backEnds[$backend]);
  294. }
  295. }
  296. return $backEnds;
  297. }
  298. /**
  299. * Get the system mount points
  300. * The returned array is not in the same format as getUserMountPoints()
  301. *
  302. * @return array
  303. */
  304. public static function getSystemMountPoints() {
  305. $mountPoints = self::readData();
  306. $backends = self::getBackends();
  307. $system = array();
  308. if (isset($mountPoints[self::MOUNT_TYPE_GROUP])) {
  309. foreach ($mountPoints[self::MOUNT_TYPE_GROUP] as $group => $mounts) {
  310. foreach ($mounts as $mountPoint => $mount) {
  311. // Update old classes to new namespace
  312. if (strpos($mount['class'], 'OC_Filestorage_') !== false) {
  313. $mount['class'] = '\OC\Files\Storage\\' . substr($mount['class'], 15);
  314. }
  315. $mount['options'] = self::decryptPasswords($mount['options']);
  316. if (!isset($mount['priority'])) {
  317. $mount['priority'] = $backends[$mount['class']]['priority'];
  318. }
  319. // Remove '/$user/files/' from mount point
  320. $mountPoint = substr($mountPoint, 13);
  321. $config = array(
  322. 'class' => $mount['class'],
  323. 'mountpoint' => $mountPoint,
  324. 'backend' => $backends[$mount['class']]['backend'],
  325. 'priority' => $mount['priority'],
  326. 'options' => $mount['options'],
  327. 'applicable' => array('groups' => array($group), 'users' => array())
  328. );
  329. if (isset($mount['id'])) {
  330. $config['id'] = (int)$mount['id'];
  331. }
  332. if (isset($mount['storage_id'])) {
  333. $config['storage_id'] = (int)$mount['storage_id'];
  334. }
  335. if (isset($mount['mountOptions'])) {
  336. $config['mountOptions'] = $mount['mountOptions'];
  337. }
  338. $hash = self::makeConfigHash($config);
  339. // If an existing config exists (with same class, mountpoint and options)
  340. if (isset($system[$hash])) {
  341. // add the groups into that config
  342. $system[$hash]['applicable']['groups']
  343. = array_merge($system[$hash]['applicable']['groups'], array($group));
  344. } else {
  345. $system[$hash] = $config;
  346. }
  347. }
  348. }
  349. }
  350. if (isset($mountPoints[self::MOUNT_TYPE_USER])) {
  351. foreach ($mountPoints[self::MOUNT_TYPE_USER] as $user => $mounts) {
  352. foreach ($mounts as $mountPoint => $mount) {
  353. // Update old classes to new namespace
  354. if (strpos($mount['class'], 'OC_Filestorage_') !== false) {
  355. $mount['class'] = '\OC\Files\Storage\\' . substr($mount['class'], 15);
  356. }
  357. $mount['options'] = self::decryptPasswords($mount['options']);
  358. if (!isset($mount['priority'])) {
  359. $mount['priority'] = $backends[$mount['class']]['priority'];
  360. }
  361. // Remove '/$user/files/' from mount point
  362. $mountPoint = substr($mountPoint, 13);
  363. $config = array(
  364. 'class' => $mount['class'],
  365. 'mountpoint' => $mountPoint,
  366. 'backend' => $backends[$mount['class']]['backend'],
  367. 'priority' => $mount['priority'],
  368. 'options' => $mount['options'],
  369. 'applicable' => array('groups' => array(), 'users' => array($user))
  370. );
  371. if (isset($mount['id'])) {
  372. $config['id'] = (int)$mount['id'];
  373. }
  374. if (isset($mount['storage_id'])) {
  375. $config['storage_id'] = (int)$mount['storage_id'];
  376. }
  377. if (isset($mount['mountOptions'])) {
  378. $config['mountOptions'] = $mount['mountOptions'];
  379. }
  380. $hash = self::makeConfigHash($config);
  381. // If an existing config exists (with same class, mountpoint and options)
  382. if (isset($system[$hash])) {
  383. // add the users into that config
  384. $system[$hash]['applicable']['users']
  385. = array_merge($system[$hash]['applicable']['users'], array($user));
  386. } else {
  387. $system[$hash] = $config;
  388. }
  389. }
  390. }
  391. }
  392. return array_values($system);
  393. }
  394. /**
  395. * Get the personal mount points of the current user
  396. * The returned array is not in the same format as getUserMountPoints()
  397. *
  398. * @return array
  399. */
  400. public static function getPersonalMountPoints() {
  401. $mountPoints = self::readData(OCP\User::getUser());
  402. $backEnds = self::getBackends();
  403. $uid = OCP\User::getUser();
  404. $personal = array();
  405. if (isset($mountPoints[self::MOUNT_TYPE_USER][$uid])) {
  406. foreach ($mountPoints[self::MOUNT_TYPE_USER][$uid] as $mountPoint => $mount) {
  407. // Update old classes to new namespace
  408. if (strpos($mount['class'], 'OC_Filestorage_') !== false) {
  409. $mount['class'] = '\OC\Files\Storage\\' . substr($mount['class'], 15);
  410. }
  411. $mount['options'] = self::decryptPasswords($mount['options']);
  412. $config = array(
  413. 'class' => $mount['class'],
  414. // Remove '/uid/files/' from mount point
  415. 'mountpoint' => substr($mountPoint, strlen($uid) + 8),
  416. 'backend' => $backEnds[$mount['class']]['backend'],
  417. 'options' => $mount['options']
  418. );
  419. if (isset($mount['id'])) {
  420. $config['id'] = (int)$mount['id'];
  421. }
  422. if (isset($mount['storage_id'])) {
  423. $config['storage_id'] = (int)$mount['storage_id'];
  424. }
  425. if (isset($mount['mountOptions'])) {
  426. $config['mountOptions'] = $mount['mountOptions'];
  427. }
  428. $personal[] = $config;
  429. }
  430. }
  431. return $personal;
  432. }
  433. /**
  434. * Test connecting using the given backend configuration
  435. *
  436. * @param string $class backend class name
  437. * @param array $options backend configuration options
  438. * @return int see self::STATUS_*
  439. */
  440. public static function getBackendStatus($class, $options, $isPersonal) {
  441. if (self::$skipTest) {
  442. return self::STATUS_SUCCESS;
  443. }
  444. foreach ($options as &$option) {
  445. $option = self::setUserVars(OCP\User::getUser(), $option);
  446. }
  447. if (class_exists($class)) {
  448. try {
  449. $storage = new $class($options);
  450. if ($storage->test($isPersonal)) {
  451. return self::STATUS_SUCCESS;
  452. }
  453. } catch (Exception $exception) {
  454. \OCP\Util::logException('files_external', $exception);
  455. }
  456. }
  457. return self::STATUS_ERROR;
  458. }
  459. /**
  460. * Add a mount point to the filesystem
  461. *
  462. * @param string $mountPoint Mount point
  463. * @param string $class Backend class
  464. * @param array $classOptions Backend parameters for the class
  465. * @param string $mountType MOUNT_TYPE_GROUP | MOUNT_TYPE_USER
  466. * @param string $applicable User or group to apply mount to
  467. * @param bool $isPersonal Personal or system mount point i.e. is this being called from the personal or admin page
  468. * @param int|null $priority Mount point priority, null for default
  469. * @return boolean
  470. *
  471. * @deprecated use StoragesService#addStorage() instead
  472. */
  473. public static function addMountPoint($mountPoint,
  474. $class,
  475. $classOptions,
  476. $mountType,
  477. $applicable,
  478. $isPersonal = false,
  479. $priority = null) {
  480. $backends = self::getBackends();
  481. $mountPoint = OC\Files\Filesystem::normalizePath($mountPoint);
  482. $relMountPoint = $mountPoint;
  483. if ($mountPoint === '' || $mountPoint === '/') {
  484. // can't mount at root folder
  485. return false;
  486. }
  487. if (!isset($backends[$class])) {
  488. // invalid backend
  489. return false;
  490. }
  491. if ($isPersonal) {
  492. // Verify that the mount point applies for the current user
  493. // Prevent non-admin users from mounting local storage and other disabled backends
  494. $allowed_backends = self::getPersonalBackends();
  495. if ($applicable != OCP\User::getUser() || !isset($allowed_backends[$class])) {
  496. return false;
  497. }
  498. $mountPoint = '/' . $applicable . '/files/' . ltrim($mountPoint, '/');
  499. } else {
  500. $mountPoint = '/$user/files/' . ltrim($mountPoint, '/');
  501. }
  502. $mount = array($applicable => array(
  503. $mountPoint => array(
  504. 'class' => $class,
  505. 'options' => self::encryptPasswords($classOptions))
  506. )
  507. );
  508. if (!$isPersonal && !is_null($priority)) {
  509. $mount[$applicable][$mountPoint]['priority'] = $priority;
  510. }
  511. $mountPoints = self::readData($isPersonal ? OCP\User::getUser() : null);
  512. // who else loves multi-dimensional array ?
  513. $isNew = !isset($mountPoints[$mountType]) ||
  514. !isset($mountPoints[$mountType][$applicable]) ||
  515. !isset($mountPoints[$mountType][$applicable][$mountPoint]);
  516. $mountPoints = self::mergeMountPoints($mountPoints, $mount, $mountType);
  517. // Set default priority if none set
  518. if (!isset($mountPoints[$mountType][$applicable][$mountPoint]['priority'])) {
  519. if (isset($backends[$class]['priority'])) {
  520. $mountPoints[$mountType][$applicable][$mountPoint]['priority']
  521. = $backends[$class]['priority'];
  522. } else {
  523. $mountPoints[$mountType][$applicable][$mountPoint]['priority']
  524. = 100;
  525. }
  526. }
  527. self::writeData($isPersonal ? OCP\User::getUser() : null, $mountPoints);
  528. $result = self::getBackendStatus($class, $classOptions, $isPersonal);
  529. if ($result === self::STATUS_SUCCESS && $isNew) {
  530. \OC_Hook::emit(
  531. \OC\Files\Filesystem::CLASSNAME,
  532. \OC\Files\Filesystem::signal_create_mount,
  533. array(
  534. \OC\Files\Filesystem::signal_param_path => $relMountPoint,
  535. \OC\Files\Filesystem::signal_param_mount_type => $mountType,
  536. \OC\Files\Filesystem::signal_param_users => $applicable,
  537. )
  538. );
  539. }
  540. return $result;
  541. }
  542. /**
  543. *
  544. * @param string $mountPoint Mount point
  545. * @param string $mountType MOUNT_TYPE_GROUP | MOUNT_TYPE_USER
  546. * @param string $applicable User or group to remove mount from
  547. * @param bool $isPersonal Personal or system mount point
  548. * @return bool
  549. *
  550. * @deprecated use StoragesService#removeStorage() instead
  551. */
  552. public static function removeMountPoint($mountPoint, $mountType, $applicable, $isPersonal = false) {
  553. // Verify that the mount point applies for the current user
  554. $relMountPoints = $mountPoint;
  555. if ($isPersonal) {
  556. if ($applicable != OCP\User::getUser()) {
  557. return false;
  558. }
  559. $mountPoint = '/' . $applicable . '/files/' . ltrim($mountPoint, '/');
  560. } else {
  561. $mountPoint = '/$user/files/' . ltrim($mountPoint, '/');
  562. }
  563. $mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint);
  564. $mountPoints = self::readData($isPersonal ? OCP\User::getUser() : null);
  565. // Remove mount point
  566. unset($mountPoints[$mountType][$applicable][$mountPoint]);
  567. // Unset parent arrays if empty
  568. if (empty($mountPoints[$mountType][$applicable])) {
  569. unset($mountPoints[$mountType][$applicable]);
  570. if (empty($mountPoints[$mountType])) {
  571. unset($mountPoints[$mountType]);
  572. }
  573. }
  574. self::writeData($isPersonal ? OCP\User::getUser() : null, $mountPoints);
  575. \OC_Hook::emit(
  576. \OC\Files\Filesystem::CLASSNAME,
  577. \OC\Files\Filesystem::signal_delete_mount,
  578. array(
  579. \OC\Files\Filesystem::signal_param_path => $relMountPoints,
  580. \OC\Files\Filesystem::signal_param_mount_type => $mountType,
  581. \OC\Files\Filesystem::signal_param_users => $applicable,
  582. )
  583. );
  584. return true;
  585. }
  586. /**
  587. *
  588. * @param string $mountPoint Mount point
  589. * @param string $target The new mount point
  590. * @param string $mountType MOUNT_TYPE_GROUP | MOUNT_TYPE_USER
  591. * @return bool
  592. */
  593. public static function movePersonalMountPoint($mountPoint, $target, $mountType) {
  594. $mountPoint = rtrim($mountPoint, '/');
  595. $user = OCP\User::getUser();
  596. $mountPoints = self::readData($user);
  597. if (!isset($mountPoints[$mountType][$user][$mountPoint])) {
  598. return false;
  599. }
  600. $mountPoints[$mountType][$user][$target] = $mountPoints[$mountType][$user][$mountPoint];
  601. // Remove old mount point
  602. unset($mountPoints[$mountType][$user][$mountPoint]);
  603. self::writeData($user, $mountPoints);
  604. return true;
  605. }
  606. /**
  607. * Read the mount points in the config file into an array
  608. *
  609. * @param string|null $user If not null, personal for $user, otherwise system
  610. * @return array
  611. */
  612. public static function readData($user = null) {
  613. if (isset($user)) {
  614. $jsonFile = OC_User::getHome($user) . '/mount.json';
  615. } else {
  616. $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data/');
  617. $jsonFile = \OC_Config::getValue('mount_file', $datadir . '/mount.json');
  618. }
  619. if (is_file($jsonFile)) {
  620. $mountPoints = json_decode(file_get_contents($jsonFile), true);
  621. if (is_array($mountPoints)) {
  622. return $mountPoints;
  623. }
  624. }
  625. return array();
  626. }
  627. /**
  628. * Write the mount points to the config file
  629. *
  630. * @param string|null $user If not null, personal for $user, otherwise system
  631. * @param array $data Mount points
  632. */
  633. public static function writeData($user, $data) {
  634. if (isset($user)) {
  635. $file = OC_User::getHome($user) . '/mount.json';
  636. } else {
  637. $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data/');
  638. $file = \OC_Config::getValue('mount_file', $datadir . '/mount.json');
  639. }
  640. foreach ($data as &$applicables) {
  641. foreach ($applicables as &$mountPoints) {
  642. foreach ($mountPoints as &$options) {
  643. self::addStorageId($options);
  644. }
  645. }
  646. }
  647. $content = json_encode($data, JSON_PRETTY_PRINT);
  648. @file_put_contents($file, $content);
  649. @chmod($file, 0640);
  650. }
  651. /**
  652. * check dependencies
  653. */
  654. public static function checkDependencies() {
  655. $dependencies = array();
  656. foreach (OC_Mount_Config::$backends as $class => $backend) {
  657. if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) {
  658. $result = $class::checkDependencies();
  659. if ($result !== true) {
  660. if (!is_array($result)) {
  661. $result = array($result);
  662. }
  663. foreach ($result as $key => $value) {
  664. if (is_numeric($key)) {
  665. OC_Mount_Config::addDependency($dependencies, $value, $backend['backend']);
  666. } else {
  667. OC_Mount_Config::addDependency($dependencies, $key, $backend['backend'], $value);
  668. }
  669. }
  670. }
  671. }
  672. }
  673. if (count($dependencies) > 0) {
  674. return OC_Mount_Config::generateDependencyMessage($dependencies);
  675. }
  676. return '';
  677. }
  678. private static function addDependency(&$dependencies, $module, $backend, $message = null) {
  679. if (!isset($dependencies[$module])) {
  680. $dependencies[$module] = array();
  681. }
  682. if ($message === null) {
  683. $dependencies[$module][] = $backend;
  684. } else {
  685. $dependencies[$module][] = array('backend' => $backend, 'message' => $message);
  686. }
  687. }
  688. private static function generateDependencyMessage($dependencies) {
  689. $l = new \OC_L10N('files_external');
  690. $dependencyMessage = '';
  691. foreach ($dependencies as $module => $backends) {
  692. $dependencyGroup = array();
  693. foreach ($backends as $backend) {
  694. if (is_array($backend)) {
  695. $dependencyMessage .= '<br />' . $l->t('<b>Note:</b> ') . $backend['message'];
  696. } else {
  697. $dependencyGroup[] = $backend;
  698. }
  699. }
  700. $dependencyGroupCount = count($dependencyGroup);
  701. if ($dependencyGroupCount > 0) {
  702. $backends = '';
  703. for ($i = 0; $i < $dependencyGroupCount; $i++) {
  704. if ($i > 0 && $i === $dependencyGroupCount - 1) {
  705. $backends .= ' ' . $l->t('and') . ' ';
  706. } elseif ($i > 0) {
  707. $backends .= ', ';
  708. }
  709. $backends .= '<i>' . $dependencyGroup[$i] . '</i>';
  710. }
  711. $dependencyMessage .= '<br />' . OC_Mount_Config::getSingleDependencyMessage($l, $module, $backends);
  712. }
  713. }
  714. return $dependencyMessage;
  715. }
  716. /**
  717. * Returns a dependency missing message
  718. *
  719. * @param OC_L10N $l
  720. * @param string $module
  721. * @param string $backend
  722. * @return string
  723. */
  724. private static function getSingleDependencyMessage(OC_L10N $l, $module, $backend) {
  725. switch (strtolower($module)) {
  726. case 'curl':
  727. return $l->t('<b>Note:</b> The cURL support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', $backend);
  728. case 'ftp':
  729. return $l->t('<b>Note:</b> The FTP support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', $backend);
  730. default:
  731. return $l->t('<b>Note:</b> "%s" is not installed. Mounting of %s is not possible. Please ask your system administrator to install it.', array($module, $backend));
  732. }
  733. }
  734. /**
  735. * Encrypt passwords in the given config options
  736. *
  737. * @param array $options mount options
  738. * @return array updated options
  739. */
  740. public static function encryptPasswords($options) {
  741. if (isset($options['password'])) {
  742. $options['password_encrypted'] = self::encryptPassword($options['password']);
  743. // do not unset the password, we want to keep the keys order
  744. // on load... because that's how the UI currently works
  745. $options['password'] = '';
  746. }
  747. return $options;
  748. }
  749. /**
  750. * Decrypt passwords in the given config options
  751. *
  752. * @param array $options mount options
  753. * @return array updated options
  754. */
  755. public static function decryptPasswords($options) {
  756. // note: legacy options might still have the unencrypted password in the "password" field
  757. if (isset($options['password_encrypted'])) {
  758. $options['password'] = self::decryptPassword($options['password_encrypted']);
  759. unset($options['password_encrypted']);
  760. }
  761. return $options;
  762. }
  763. /**
  764. * Encrypt a single password
  765. *
  766. * @param string $password plain text password
  767. * @return string encrypted password
  768. */
  769. private static function encryptPassword($password) {
  770. $cipher = self::getCipher();
  771. $iv = \OCP\Util::generateRandomBytes(16);
  772. $cipher->setIV($iv);
  773. return base64_encode($iv . $cipher->encrypt($password));
  774. }
  775. /**
  776. * Decrypts a single password
  777. *
  778. * @param string $encryptedPassword encrypted password
  779. * @return string plain text password
  780. */
  781. private static function decryptPassword($encryptedPassword) {
  782. $cipher = self::getCipher();
  783. $binaryPassword = base64_decode($encryptedPassword);
  784. $iv = substr($binaryPassword, 0, 16);
  785. $cipher->setIV($iv);
  786. $binaryPassword = substr($binaryPassword, 16);
  787. return $cipher->decrypt($binaryPassword);
  788. }
  789. /**
  790. * Merges mount points
  791. *
  792. * @param array $data Existing mount points
  793. * @param array $mountPoint New mount point
  794. * @param string $mountType
  795. * @return array
  796. */
  797. private static function mergeMountPoints($data, $mountPoint, $mountType) {
  798. $applicable = key($mountPoint);
  799. $mountPath = key($mountPoint[$applicable]);
  800. if (isset($data[$mountType])) {
  801. if (isset($data[$mountType][$applicable])) {
  802. // Merge priorities
  803. if (isset($data[$mountType][$applicable][$mountPath])
  804. && isset($data[$mountType][$applicable][$mountPath]['priority'])
  805. && !isset($mountPoint[$applicable][$mountPath]['priority'])
  806. ) {
  807. $mountPoint[$applicable][$mountPath]['priority']
  808. = $data[$mountType][$applicable][$mountPath]['priority'];
  809. }
  810. $data[$mountType][$applicable]
  811. = array_merge($data[$mountType][$applicable], $mountPoint[$applicable]);
  812. } else {
  813. $data[$mountType] = array_merge($data[$mountType], $mountPoint);
  814. }
  815. } else {
  816. $data[$mountType] = $mountPoint;
  817. }
  818. return $data;
  819. }
  820. /**
  821. * Returns the encryption cipher
  822. */
  823. private static function getCipher() {
  824. if (!class_exists('Crypt_AES', false)) {
  825. include('Crypt/AES.php');
  826. }
  827. $cipher = new Crypt_AES(CRYPT_AES_MODE_CBC);
  828. $cipher->setKey(\OC::$server->getConfig()->getSystemValue('passwordsalt', null));
  829. return $cipher;
  830. }
  831. /**
  832. * Computes a hash based on the given configuration.
  833. * This is mostly used to find out whether configurations
  834. * are the same.
  835. */
  836. public static function makeConfigHash($config) {
  837. $data = json_encode(
  838. array(
  839. 'c' => $config['class'],
  840. 'm' => $config['mountpoint'],
  841. 'o' => $config['options'],
  842. 'p' => isset($config['priority']) ? $config['priority'] : -1,
  843. 'mo' => isset($config['mountOptions']) ? $config['mountOptions'] : [],
  844. )
  845. );
  846. return hash('md5', $data);
  847. }
  848. /**
  849. * Add storage id to the storage configurations that did not have any.
  850. *
  851. * @param string $user user for which to process storage configs
  852. */
  853. private static function addStorageIdToConfig($user) {
  854. $config = self::readData($user);
  855. $needUpdate = false;
  856. foreach ($config as &$applicables) {
  857. foreach ($applicables as &$mountPoints) {
  858. foreach ($mountPoints as &$options) {
  859. $needUpdate |= !isset($options['storage_id']);
  860. }
  861. }
  862. }
  863. if ($needUpdate) {
  864. self::writeData($user, $config);
  865. }
  866. }
  867. /**
  868. * Get storage id from the numeric storage id and set
  869. * it into the given options argument. Only do this
  870. * if there was no storage id set yet.
  871. *
  872. * This might also fail if a storage wasn't fully configured yet
  873. * and couldn't be mounted, in which case this will simply return false.
  874. *
  875. * @param array $options storage options
  876. *
  877. * @return bool true if the storage id was added, false otherwise
  878. */
  879. private static function addStorageId(&$options) {
  880. if (isset($options['storage_id'])) {
  881. return false;
  882. }
  883. $class = $options['class'];
  884. try {
  885. /** @var \OC\Files\Storage\Storage $storage */
  886. $storage = new $class($options['options']);
  887. // TODO: introduce StorageConfigException
  888. } catch (\Exception $e) {
  889. // storage might not be fully configured yet (ex: Dropbox)
  890. // note that storage instances aren't supposed to open any connections
  891. // in the constructor, so this exception is likely to be a config exception
  892. return false;
  893. }
  894. $options['storage_id'] = $storage->getCache()->getNumericStorageId();
  895. return true;
  896. }
  897. }