summaryrefslogtreecommitdiff
path: root/includes/filetransfer/filetransfer.inc
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2009-06-23 12:11:19 +0000
committerDries Buytaert <dries@buytaert.net>2009-06-23 12:11:19 +0000
commitd7e2be1520b45edb09da11df52fb307d0ec906ab (patch)
tree1a056f73dbd6dc56d3431997c4678a18fd90bb56 /includes/filetransfer/filetransfer.inc
parentc84c1d9c340687bc008e199fc4a9afd1208a5b37 (diff)
downloadbrdo-d7e2be1520b45edb09da11df52fb307d0ec906ab.tar.gz
brdo-d7e2be1520b45edb09da11df52fb307d0ec906ab.tar.bz2
- Patch #395472 by chx, dww, cwgordon7, JacobSingh, et al: added different file transport mechanisms to core in preparation of a plugin manager.
Diffstat (limited to 'includes/filetransfer/filetransfer.inc')
-rw-r--r--includes/filetransfer/filetransfer.inc102
1 files changed, 102 insertions, 0 deletions
diff --git a/includes/filetransfer/filetransfer.inc b/includes/filetransfer/filetransfer.inc
new file mode 100644
index 000000000..2fb769913
--- /dev/null
+++ b/includes/filetransfer/filetransfer.inc
@@ -0,0 +1,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 {
+}