Монтирование через Samba

Скрипт монтирования удаленной машины через Samba в указанный каталог

samba-mount.php
<?php
 
class SabmaMount {
  private $sambaPath, $sambaCreds, $isVerbose, $mountDir, $mountPoint, $credFileName;
  public function __construct($sambaPath, array $sambaCreds, $isVerbose = false) {
    $this->sambaPath = $sambaPath;
    $this->sambaCreds = $sambaCreds;
    $this->isVerbose = $isVerbose;
  }
 
  public function __destruct() {
    $this->unmount();
  }
 
  public function mount($mountDir) {
    $this->mountDir = rtrim($mountDir, '/');
    $hash = md5($this->sambaPath);
    $this->mountPoint = $this->mountDir.'/'.$hash;
    $this->credFileName = $this->mountDir.'/'.$hash.'.cred';
 
    if($this->isMounted()) {
      $message = "Already mounted in ".$this->mountPoint;
      $this->event($message);
      throw new Exception($message, 1);
    }
    $this->mountDir();
 
    $message = "Mounted ".$this->mountPoint;
    $this->event($message);
 
    return $this;
  }
 
  public function unmount() {
    if($this->isMounted()) {
      exec(sprintf('umount "%s"', $this->mountPoint));
      rmdir($this->mountPoint);
      unlink($this->credFileName);
      $message = "Unmounted ".$this->mountPoint;
      $this->event($message);
    }
    return $this;
  }
 
  public function getMountPoint() {
    return $this->mountPoint;
  }
 
  private function isMounted() {
    return is_dir($this->mountPoint);
  }
 
  private function mountDir() {
    if(!file_put_contents($this->credFileName, implode("\n", $this->sambaCreds))) throw new Exception("Failed to create credentials: ".$this->credFileName, 1);
    mkdir($this->mountPoint, 0777);
    $res = exec(sprintf('/sbin/mount.cifs "%s" "%s" -o ro,credentials="%s" && echo Mounted', $this->sambaPath, $this->mountPoint, $this->credFileName));
    if($res != 'Mounted') throw new Exception("Failed to mount: ".$this->sambaPath, 1);
  }
 
  private function event($text) {
    if($this->isVerbose) echo $text.PHP_EOL;
  }
 
}
 
// удаленная машина
$SabmaMount = new SabmaMount('//remote-machine/path/', array('username=username', 'password=password', 'domain=domain'), true);
 
// точка монтирования
$mountPoint = $SabmaMount->mount(__DIR__)->getMountPoint();
 
$SabmaMount->unmount(); // OR unset($SabmaMount); OR __destruct()
Печать/экспорт