smb_oc.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Files\Storage;
  25. use Icewind\SMB\Exception\AccessDeniedException;
  26. use Icewind\SMB\Exception\Exception;
  27. use Icewind\SMB\Server;
  28. class SMB_OC extends SMB {
  29. private $username_as_share;
  30. /**
  31. * @param array $params
  32. * @throws \Exception
  33. */
  34. public function __construct($params) {
  35. if (isset($params['host'])) {
  36. $host = $params['host'];
  37. $this->username_as_share = ($params['username_as_share'] === true);
  38. // dummy credentials, unused, to satisfy constructor
  39. $user = 'foo';
  40. $password = 'bar';
  41. if (\OC::$server->getSession()->exists('smb-credentials')) {
  42. $params_auth = json_decode(\OC::$server->getCrypto()->decrypt(\OC::$server->getSession()->get('smb-credentials')), true);
  43. $user = \OC::$server->getSession()->get('loginname');
  44. $password = $params_auth['password'];
  45. } else {
  46. // assume we are testing from the admin section
  47. }
  48. $root = isset($params['root']) ? $params['root'] : '/';
  49. $share = '';
  50. if ($this->username_as_share) {
  51. $share = '/' . $user;
  52. } elseif (isset($params['share'])) {
  53. $share = $params['share'];
  54. } else {
  55. throw new \Exception();
  56. }
  57. parent::__construct(array(
  58. "user" => $user,
  59. "password" => $password,
  60. "host" => $host,
  61. "share" => $share,
  62. "root" => $root
  63. ));
  64. } else {
  65. throw new \Exception();
  66. }
  67. }
  68. /**
  69. * Intercepts the user credentials on login and stores them
  70. * encrypted inside the session if SMB_OC storage is enabled.
  71. * @param array $params
  72. */
  73. public static function login($params) {
  74. $mountpoints = \OC_Mount_Config::getAbsoluteMountPoints($params['uid']);
  75. $mountpointClasses = array();
  76. foreach($mountpoints as $mountpoint) {
  77. $mountpointClasses[$mountpoint['class']] = true;
  78. }
  79. if(isset($mountpointClasses['\OC\Files\Storage\SMB_OC'])) {
  80. \OC::$server->getSession()->set('smb-credentials', \OC::$server->getCrypto()->encrypt(json_encode($params)));
  81. }
  82. }
  83. /**
  84. * @param string $path
  85. * @return boolean
  86. */
  87. public function isSharable($path) {
  88. return false;
  89. }
  90. /**
  91. * @param bool $isPersonal
  92. * @return bool
  93. */
  94. public function test($isPersonal = true) {
  95. if ($isPersonal) {
  96. if ($this->stat('')) {
  97. return true;
  98. }
  99. return false;
  100. } else {
  101. $server = new Server($this->server->getHost(), '', '');
  102. try {
  103. $server->listShares();
  104. return true;
  105. } catch (AccessDeniedException $e) {
  106. // expected due to anonymous login
  107. return true;
  108. } catch (Exception $e) {
  109. return false;
  110. }
  111. }
  112. }
  113. }