1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
|
<?php
/**
* Stateless VFS implementation for a SMB server, based on smbclient.
*
* Required values for $params:
* <pre>
* 'username' - The username with which to connect to the SMB server.
* 'password' - The password with which to connect to the SMB server.
* 'hostspec' - The SMB server to connect to.
* 'port' - The SMB port number to connect to.
* 'share' - The share to access on the SMB server.
* 'smbclient' - The path to the 'smbclient' executable.
* </pre>
*
* Optional values for $params:
* <pre>
* 'ipaddress' - The address of the server to connect to.
* </pre>
*
* Functions not implemented:
* - changePermissions(): The SMB permission style does not fit with the
* module.
*
* $Horde: framework/VFS/lib/VFS/smb.php,v 1.1.2.5 2009/02/06 18:24:20 slusarz Exp $
*
* Codebase copyright 2002 Paul Gareau <paul@xhawk.net>. Adapted with
* permission by Patrice Levesque <wayne@ptaff.ca> from phpsmb-0.8 code, and
* converted to the LGPL. Please do not taunt original author, contact
* Patrice Levesque or dev@lists.horde.org.
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Paul Gareau <paul@xhawk.net>
* @author Patrice Levesque <wayne@ptaff.ca>
* @since Horde 3.1
* @package VFS
*/
class VFS_smb extends VFS {
/**
* List of additional credentials required for this VFS backend.
*
* @var array
*/
var $_credentials = array('username', 'password');
/**
* List of permissions and if they can be changed in this VFS backend.
*
* @var array
*/
var $_permissions = array(
'owner' => array('read' => false, 'write' => false, 'execute' => false),
'group' => array('read' => false, 'write' => false, 'execute' => false),
'all' => array('read' => false, 'write' => false, 'execute' => false));
/**
* Authenticates a user on the SMB server and share.
*
* @access private
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function _connect()
{
$cmd = array('quit');
$err = $this->_command('', $cmd);
if (is_a($err, 'PEAR_Error')) {
return PEAR::raiseError(_("Authentication to the SMB server failed."));
}
return true;
}
/**
* Retrieves a file from the VFS.
*
* @param string $path The pathname to the file.
* @param string $name The filename to retrieve.
*
* @return string The file data.
*/
function read($path, $name)
{
$file = $this->readFile($path, $name);
if (is_a($file, 'PEAR_Error')) {
return $file;
}
$size = filesize($file);
if ($size === 0) {
return '';
}
return file_get_contents($file);
}
/**
* Retrieves a file from the VFS as an on-disk local file.
*
* This function provides a file on local disk with the data of a VFS file
* in it. This file <em>cannot</em> be modified! The behavior if you do
* modify it is undefined. It will be removed at the end of the request.
*
* @param string $path The pathname to the file.
* @param string $name The filename to retrieve.
*
* @return string A local filename.
*/
function readFile($path, $name)
{
// Create a temporary file and register it for deletion at the
// end of this request.
$localFile = $this->_getTempFile();
if (!$localFile) {
return PEAR::raiseError(_("Unable to create temporary file."));
}
register_shutdown_function(create_function('', 'unlink(\'' . addslashes($localFile) . '\');'));
list($path, $name) = $this->_escapeShellCommand($path, $name);
$cmd = array('get \"' . $name . '\" ' . $localFile);
$result = $this->_command($path, $cmd);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
if (!file_exists($localFile)) {
return PEAR::raiseError(sprintf(_("Unable to open VFS file \"%s\"."), $this->_getPath($path, $name)));
}
return $localFile;
}
/**
* Open a stream to a file in the VFS.
*
* @param string $path The pathname to the file.
* @param string $name The filename to retrieve.
*
* @return resource The stream.
*/
function readStream($path, $name)
{
$file = $this->readFile($path, $name);
if (is_a($file, 'PEAR_Error')) {
return $file;
}
$mode = OS_WINDOWS ? 'rb' : 'r';
return fopen($file, $mode);
}
/**
* Stores a file in the VFS.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $tmpFile The temporary file containing the data to be
* stored.
* @param boolean $autocreate Automatically create directories?
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function write($path, $name, $tmpFile, $autocreate = false)
{
// Double quotes not allowed in SMB filename.
$name = str_replace('"', "'", $name);
list($path, $name) = $this->_escapeShellCommand($path, $name);
$cmd = array('put \"' . $tmpFile . '\" \"' . $name . '\"');
// do we need to first autocreate the directory?
if ($autocreate) {
$result = $this->autocreatePath($path);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
}
$err = $this->_command($path, $cmd);
if (is_a($err, 'PEAR_Error')) {
return $err;
}
return true;
}
/**
* Stores a file in the VFS from raw data.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $data The file data.
* @param boolean $autocreate Automatically create directories?
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function writeData($path, $name, $data, $autocreate = false)
{
$tmpFile = $this->_getTempFile();
$fp = fopen($tmpFile, 'wb');
fwrite($fp, $data);
fclose($fp);
$result = $this->write($path, $name, $tmpFile, $autocreate);
unlink($tmpFile);
return $result;
}
/**
* Deletes a file from the VFS.
*
* @param string $path The path to delete the file from.
* @param string $name The filename to use.
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function deleteFile($path, $name)
{
// In some samba versions after samba-3.0.25-pre2, $path must
// end in a trailing slash.
if (substr($path, -1) != '/') {
$path .= '/';
}
list($path, $name) = $this->_escapeShellCommand($path, $name);
$cmd = array('del \"' . $name . '\"');
$err = $this->_command($path, $cmd);
if (is_a($err, 'PEAR_Error')) {
return $err;
}
return true;
}
/**
* Checks if a given pathname is a folder.
*
* @param string $path The path to the folder.
* @param string $name The file or folder name.
*
* @return boolean True if it is a folder, false otherwise.
*/
function isFolder($path, $name)
{
list($path, $name) = $this->_escapeShellCommand($path, $name);
$cmd = array('quit');
$err = $this->_command($this->_getPath($path, $name), $cmd);
if (is_a($err, 'PEAR_Error')) {
return false;
}
return true;
}
/**
* Deletes a folder from the VFS.
*
* @param string $path The path to delete the folder from.
* @param string $name The name of the folder to delete.
* @param boolean $recursive Force a recursive delete?
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function deleteFolder($path, $name, $recursive = false)
{
// In some samba versions after samba-3.0.25-pre2, $path must
// end in a trailing slash.
if (substr($path, -1) != '/') {
$path .= '/';
}
if (!$this->isFolder($path, $name)) {
return PEAR::raiseError(sprintf(_("\"%s\" is not a directory."), $path . '/' . $name));
}
$file_list = $this->listFolder($this->_getPath($path, $name));
if (is_a($file_list, 'PEAR_Error')) {
return $file_list;
}
if ($file_list && !$recursive) {
return PEAR::raiseError(sprintf(_("Unable to delete \"%s\", the directory is not empty."),
$this->_getPath($path, $name)));
}
foreach ($file_list as $file) {
if ($file['type'] == '**dir') {
$result = $this->deleteFolder($this->_getPath($path, $name), $file['name'], $recursive);
} else {
$result = $this->deleteFile($this->_getPath($path, $name), $file['name']);
}
if (is_a($result, 'PEAR_Error')) {
return $result;
}
}
// Really delete the folder.
list($path, $name) = $this->_escapeShellCommand($path, $name);
$cmd = array('rmdir \"' . $name . '\"');
$err = $this->_command($path, $cmd);
if (is_a($err, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Unable to delete VFS folder \"%s\"."), $this->_getPath($path, $name)));
} else {
return true;
}
}
/**
* Renames a file in the VFS.
*
* @param string $oldpath The old path to the file.
* @param string $oldname The old filename.
* @param string $newpath The new path of the file.
* @param string $newname The new filename.
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function rename($oldpath, $oldname, $newpath, $newname)
{
if (is_a($result = $this->autocreatePath($newpath), 'PEAR_Error')) {
return $result;
}
// Double quotes not allowed in SMB filename. The '/' character should
// also be removed from the beginning/end of the names.
$oldname = str_replace('"', "'", trim($oldname, '/'));
$newname = str_replace('"', "'", trim($newname, '/'));
if (empty($oldname)) {
return PEAR::raiseError(_("Unable to rename VFS file to same name."));
}
/* If the path was not empty (i.e. the path is not the root path),
* then add the trailing '/' character to path. */
if (!empty($oldpath)) {
$oldpath .= '/';
}
if (!empty($newpath)) {
$newpath .= '/';
}
list($file, $name) = $this->_escapeShellCommand($oldname, $newname);
$cmd = array('rename \"' . str_replace('/', '\\\\', $oldpath) . $file . '\" \"' .
str_replace('/', '\\\\', $newpath) . $name . '\"');
if (is_a($err = $this->_command('', $cmd), 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Unable to rename VFS file \"%s\"."), $this->_getPath($path, $name)));
}
return true;
}
/**
* Creates a folder on the VFS.
*
* @param string $path The path of directory to create folder.
* @param string $name The name of the new folder.
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function createFolder($path, $name)
{
// In some samba versions after samba-3.0.25-pre2, $path must
// end in a trailing slash.
if (substr($path, -1) != '/') {
$path .= '/';
}
// Double quotes not allowed in SMB filename.
$name = str_replace('"', "'", $name);
list($dir, $mkdir) = $this->_escapeShellCommand($path, $name);
$cmd = array('mkdir \"' . $mkdir . '\"');
$err = $this->_command($dir, $cmd);
if (is_a($err, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Unable to create VFS folder \"%s\"."), $this->_getPath($path, $name)));
}
return true;
}
/**
* Returns an unsorted file list.
*
* @param string $path The path of the directory to get the file list
* for.
* @param mixed $filter Hash of items to filter based on filename.
* @param boolean $dotfiles Show dotfiles? This is irrelevant with
* smbclient.
* @param boolean $dironly Show directories only?
*
* @return boolean|PEAR_Error File list on success or a PEAR_Error on
* failure.
*/
function listFolder($path = '', $filter = null, $dotfiles = true, $dironly = false)
{
list($path) = $this->_escapeShellCommand($path);
$cmd = array('ls');
$res = $this->_command($path, $cmd);
if (is_a($res, 'PEAR_Error')) {
return $res;
}
$num_lines = count($res);
$files = array();
for ($r = 0; $r < $num_lines; $r++) {
// Match file listing.
if (preg_match('/^(\s\s.+\s{6,})/', $res[$r])) {
// Split into columns at every six spaces
$split1 = preg_split('/\s{6,}/', trim($res[$r]));
// If the file name isn't . or ..
if ($split1[0] != '.' && $split1[0] != '..') {
if (isset($split1[2])) {
// If there is a small file size, inf could be split
// into 3 cols.
$split1[1] .= ' ' . $split1[2];
}
// Split file inf at every one or more spaces.
$split2 = preg_split('/\s+/', $split1[1]);
if (is_numeric($split2[0])) {
// If there is no file attr, shift cols over.
array_unshift($split2, '');
}
$my_name = $split1[0];
// Filter out dotfiles if they aren't wanted.
if (!$dotfiles && substr($my_name, 0, 1) == '.') {
continue;
}
$my_size = $split2[1];
$ext_name = explode('.', $my_name);
if ((strpos($split2[0], 'D') !== false)) {
$my_type = '**dir';
$my_size = -1;
} else {
$my_type = VFS::strtolower($ext_name[count($ext_name) - 1]);
}
$my_date = strtotime($split2[4] . ' ' . $split2[3] . ' ' .
$split2[6] . ' ' . $split2[5]);
$filedata = array('owner' => '',
'group' => '',
'perms' => '',
'name' => $my_name,
'type' => $my_type,
'date' => $my_date,
'size' => $my_size);
// watch for filters and dironly
if ($this->_filterMatch($filter, $my_name)) {
unset($file);
continue;
}
if ($dironly && $my_type !== '**dir') {
unset($file);
continue;
}
$files[$filedata['name']] = $filedata;
}
}
}
return $files;
}
/**
* Returns a sorted list of folders in specified directory.
*
* @param string $path The path of the directory to get the
* directory list for.
* @param mixed $filter Hash of items to filter based on folderlist.
* @param boolean $dotfolders Include dotfolders? Irrelevant for SMB.
*
* @return boolean|PEAR_Error Folder list on success or a PEAR_Error on
* failure.
*/
function listFolders($path = '', $filter = null, $dotfolders = true)
{
$folders = array();
$folder = array();
$folderList = $this->listFolder($path, null, $dotfolders, true);
if (is_a($folderList, 'PEAR_Error')) {
return $folderList;
}
// dirname will strip last component from path, even on a directory
$folder['val'] = dirname($path);
$folder['abbrev'] = '..';
$folder['label'] = '..';
$folders[$folder['val']] = $folder;
foreach ($folderList as $files) {
$folder['val'] = $this->_getPath($path, $files['name']);
$folder['abbrev'] = $files['name'];
$folder['label'] = $folder['val'];
$folders[$folder['val']] = $folder;
}
ksort($folders);
return $folders;
}
/**
* Copies a file through the backend.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $dest The destination of the file.
* @param boolean $autocreate Automatically create directories?
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function copy($path, $name, $dest, $autocreate = false)
{
$orig = $this->_getPath($path, $name);
if (preg_match('|^' . preg_quote($orig) . '/?$|', $dest)) {
return PEAR::raiseError(_("Cannot copy file(s) - source and destination are the same."));
}
if ($autocreate) {
$result = $this->autocreatePath($dest);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
}
$fileCheck = $this->listFolder($dest, null, true);
if (is_a($fileCheck, 'PEAR_Error')) {
return $fileCheck;
}
foreach ($fileCheck as $file) {
if ($file['name'] == $name) {
return PEAR::raiseError(sprintf(_("%s already exists."),
$this->_getPath($dest, $name)));
}
}
if ($this->isFolder($path, $name)) {
if (is_a($result = $this->_copyRecursive($path, $name, $dest), 'PEAR_Error')) {
return $result;
}
} else {
$tmpFile = $this->readFile($path, $name);
if (is_a($tmpFile, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Failed to retrieve: %s"), $orig));
}
$result = $this->write($dest, $name, $tmpFile);
if (is_a($result, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Copy failed: %s"),
$this->_getPath($dest, $name)));
}
}
return true;
}
/**
* Moves a file through the backend.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $dest The destination of the file.
* @param boolean $autocreate Automatically create directories?
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function move($path, $name, $dest, $autocreate = false)
{
$orig = $this->_getPath($path, $name);
if (preg_match('|^' . preg_quote($orig) . '/?$|', $dest)) {
return PEAR::raiseError(_("Cannot move file(s) - destination is within source."));
}
if ($autocreate) {
$result = $this->autocreatePath($dest);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
}
$fileCheck = $this->listFolder($dest, null, true);
if (is_a($fileCheck, 'PEAR_Error')) {
return $fileCheck;
}
foreach ($fileCheck as $file) {
if ($file['name'] == $name) {
return PEAR::raiseError(sprintf(_("%s already exists."),
$this->_getPath($dest, $name)));
}
}
$err = $this->rename($path, $name, $dest, $name);
if (is_a($err, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Failed to move to \"%s\"."),
$this->_getPath($dest, $name)));
}
return true;
}
/**
* Replacement for escapeshellcmd(), variable length args, as we only want
* certain characters escaped.
*
* @access private
*
* @param array $array Strings to escape.
*
* @return array
*/
function _escapeShellCommand()
{
$ret = array();
$args = func_get_args();
foreach ($args as $arg) {
$ret[] = str_replace(array(';', '\\'), array('\;', '\\\\'), $arg);
}
return $ret;
}
/**
* Executes a command and returns output lines in array.
*
* @access private
*
* @param string $cmd Command to be executed
*
* @return mixed Array on success, false on failure.
*/
function _execute($cmd)
{
$cmd = str_replace('"-U%"', '-N', $cmd);
exec($cmd, $out, $ret);
// In some cases, (like trying to delete a nonexistant file),
// smbclient will return success (at least on 2.2.7 version I'm
// testing on). So try to match error strings, even after success.
if ($ret != 0) {
$err = '';
foreach ($out as $line) {
if (strpos($line, 'Usage:') === 0) {
$err = 'Command syntax incorrect';
break;
}
if (strpos($line, 'ERRSRV') !== false ||
strpos($line, 'ERRDOS') !== false) {
$err = preg_replace('/.*\((.+)\).*/', '\\1', $line);
if (!$err) {
$err = $line;
}
break;
}
}
if (!$err) {
$err = $out ? $out[count($out) - 1] : $ret;
}
return PEAR::raiseError($err);
}
// Check for errors even on success.
$err = '';
foreach ($out as $line) {
if (strpos($line, 'NT_STATUS_NO_SUCH_FILE') !== false ||
strpos($line, 'NT_STATUS_OBJECT_NAME_NOT_FOUND') !== false) {
$err = _("No such file");
break;
} elseif (strpos($line, 'NT_STATUS_ACCESS_DENIED') !== false) {
$err = _("Permission Denied");
break;
}
}
if ($err) {
return PEAR::raiseError($err);
}
return $out;
}
/**
* Executes SMB commands - without authentication - and returns output
* lines in array.
*
* @access private
*
* @param array $path Base path for command.
* @param array $cmd Commands to be executed.
*
* @return mixed Array on success, false on failure.
*/
function _command($path, $cmd)
{
list($share) = $this->_escapeShellCommand($this->_params['share']);
putenv('PASSWD=' . $this->_params['password']);
$ipoption = (isset($this->_params['ipaddress'])) ? (' -I ' . $this->_params['ipaddress']) : null;
$fullcmd = $this->_params['smbclient'] .
' "//' . $this->_params['hostspec'] . '/' . $share . '"' .
' "-p' . $this->_params['port'] . '"' .
' "-U' . $this->_params['username'] . '"' .
' -D "' . $path . '" ' .
$ipoption .
' -c "';
foreach ($cmd as $c) {
$fullcmd .= $c . ";";
}
$fullcmd .= '"';
return $this->_execute($fullcmd);
}
}
|