summaryrefslogtreecommitdiff
path: root/includes/filetransfer/filetransfer.inc
blob: 2fb7699130872ea0adabcd322139e20bd8455924 (plain)
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
<?php
// $Id$

/*
 * Connection class.
 *
 * This class does file operations on directories not writeable by the
 * webserver. It connects back to the server using some backend (for example
 * FTP or SSH). To keep security the password should always be asked from the
 * user and never stored.
 */
abstract class FileTransfer {

  /**
   * The constructer for the UpdateConnection class. This method is also called
   * from the classes that extend this class and override this method.
   */
  function __construct($settings) {
    $this->username = $settings['username'];
    $this->password = $settings['password'];
    $this->hostname = isset($settings['hostname']) ? $settings['hostname'] : 'localhost';
    if (isset($settings['port'])) {
      $this->port = $settings['port'];
    }
  }

  /**
   * Implementation of the magic __get() method. If the connection isn't set to
   * anything, this will call the connect() method and set it to and return the
   * result; afterwards, the connection will be returned directly without using
   * this method.
   */
  function __get($name) {
    static $connection;
    if ($name == 'connection') {
      $this->connection = $this->connect();
      return $this->connection;
    }
  }

  /**
   * Copies a directory.
   *
   * @param $source
   *   The source path.
   * @param $destination
   *   The destination path.
   */
  protected function copyDirectory($source, $destination) {
    $this->createDirectory($destination . basename($source));
    foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) {
      $relative_path = basename($source) . substr($filename, strlen($source));
      if ($file->isDir()) {
        $this->createDirectory($destination . $relative_path);
      }
      else {
        $this->copyFile($file->getPathName(), $destination . $relative_path);
      }
    }
  }

  /**
   * Creates a directory.
   *
   * @param $directory
   *   The directory to be created.
   */
  abstract function createDirectory($directory);

  /**
   * Removes a directory.
   *
   * @param $directory
   *   The directory to be removed.
   */
  abstract function removeDirectory($directory);

  /**
   * Copies a file.
   *
   * @param $source
   *   The source file.
   * @param $destination
   *   The destination file.
   */
  abstract function copyFile($source, $destination);


  /**
   * Removes a file.
   *
   * @param $destination
   *   The destination file to be removed.
   */
  abstract function removeFile($destination);
}

/**
 * FileTransferException class.
 */
class FileTransferException extends Exception {
}