smb.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Storage;
  9. require_once 'smb4php/smb.php';
  10. class SMB extends \OC\Files\Storage\StreamWrapper{
  11. private $password;
  12. private $user;
  13. private $host;
  14. private $root;
  15. private $share;
  16. public function __construct($params) {
  17. if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) {
  18. $this->host=$params['host'];
  19. $this->user=$params['user'];
  20. $this->password=$params['password'];
  21. $this->share=$params['share'];
  22. $this->root=isset($params['root'])?$params['root']:'/';
  23. if ( ! $this->root || $this->root[0]!='/') {
  24. $this->root='/'.$this->root;
  25. }
  26. if (substr($this->root, -1, 1)!='/') {
  27. $this->root.='/';
  28. }
  29. if ( ! $this->share || $this->share[0]!='/') {
  30. $this->share='/'.$this->share;
  31. }
  32. if (substr($this->share, -1, 1)=='/') {
  33. $this->share = substr($this->share, 0, -1);
  34. }
  35. } else {
  36. throw new \Exception();
  37. }
  38. }
  39. public function getId(){
  40. return 'smb::' . $this->user . '@' . $this->host . '/' . $this->share . '/' . $this->root;
  41. }
  42. public function constructUrl($path) {
  43. if (substr($path, -1)=='/') {
  44. $path=substr($path, 0, -1);
  45. }
  46. $path = urlencode($path);
  47. $user = urlencode($this->user);
  48. $pass = urlencode($this->password);
  49. return 'smb://'.$user.':'.$pass.'@'.$this->host.$this->share.$this->root.$path;
  50. }
  51. public function stat($path) {
  52. if ( ! $path and $this->root=='/') {//mtime doesn't work for shares
  53. $stat=stat($this->constructUrl($path));
  54. if (empty($stat)) {
  55. return false;
  56. }
  57. $mtime=$this->shareMTime();
  58. $stat['mtime']=$mtime;
  59. return $stat;
  60. } else {
  61. $stat = stat($this->constructUrl($path));
  62. // smb4php can return an empty array if the connection could not be established
  63. if (empty($stat)) {
  64. return false;
  65. }
  66. return $stat;
  67. }
  68. }
  69. /**
  70. * check if a file or folder has been updated since $time
  71. * @param string $path
  72. * @param int $time
  73. * @return bool
  74. */
  75. public function hasUpdated($path,$time) {
  76. if(!$path and $this->root=='/') {
  77. // mtime doesn't work for shares, but giving the nature of the backend,
  78. // doing a full update is still just fast enough
  79. return true;
  80. } else {
  81. $actualTime=$this->filemtime($path);
  82. return $actualTime>$time;
  83. }
  84. }
  85. /**
  86. * get the best guess for the modification time of the share
  87. */
  88. private function shareMTime() {
  89. $dh=$this->opendir('');
  90. $lastCtime=0;
  91. while($file=readdir($dh)) {
  92. if ($file!='.' and $file!='..') {
  93. $ctime=$this->filemtime($file);
  94. if ($ctime>$lastCtime) {
  95. $lastCtime=$ctime;
  96. }
  97. }
  98. }
  99. return $lastCtime;
  100. }
  101. }