-<?php\r
-/**\r
-* ownCloud\r
-*\r
-* @author Michael Gapczynski\r
-* @copyright 2012 Michael Gapczynski mtgap@owncloud.com\r
-*\r
-* This library is free software; you can redistribute it and/or\r
-* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE\r
-* License as published by the Free Software Foundation; either\r
-* version 3 of the License, or any later version.\r
-*\r
-* This library is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-* GNU AFFERO GENERAL PUBLIC LICENSE for more details.\r
-*\r
-* You should have received a copy of the GNU Affero General Public\r
-* License along with this library. If not, see <http://www.gnu.org/licenses/>.\r
-*/\r
-\r
-/**\r
-* Class to configure the config/mount.php and data/$user/mount.php files\r
-*/\r
-class OC_Mount_Config {\r
-\r
- const MOUNT_TYPE_GLOBAL = 'global';\r
- const MOUNT_TYPE_GROUP = 'group';\r
- const MOUNT_TYPE_USER = 'user';\r
-\r
- /**\r
- * Get details on each of the external storage backends, used for the mount config UI\r
- * If a custom UI is needed, add the key 'custom' and a javascript file with that name will be loaded\r
- * If the configuration parameter should be secret, add a '*' to the beginning of the value\r
- * If the configuration parameter is a boolean, add a '!' to the beginning of the value\r
- * If the configuration parameter is optional, add a '&' to the beginning of the value\r
- * If the configuration parameter is hidden, add a '#' to the begining of the value\r
- * @return array\r
- */\r
- public static function getBackends() {\r
- return array(\r
- 'OC_Filestorage_Local' => array('backend' => 'Local', 'configuration' => array('datadir' => 'Location')),\r
- 'OC_Filestorage_AmazonS3' => array('backend' => 'Amazon S3', 'configuration' => array('key' => 'Key', 'secret' => '*Secret', 'bucket' => 'Bucket')),\r
- 'OC_Filestorage_Dropbox' => array('backend' => 'Dropbox', 'configuration' => array('configured' => '#configured','app_key' => 'App key', 'app_secret' => 'App secret', 'token' => '#token', 'token_secret' => '#token_secret'), 'custom' => 'dropbox'),\r
- 'OC_Filestorage_FTP' => array('backend' => 'FTP', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'root' => '&Root', 'secure' => '!Secure ftps://')),\r
- 'OC_Filestorage_Google' => array('backend' => 'Google Drive', 'configuration' => array('configured' => '#configured', 'token' => '#token', 'token_secret' => '#token secret'), 'custom' => 'google'),\r
- 'OC_Filestorage_SWIFT' => array('backend' => 'OpenStack Swift', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'token' => '*Token', 'root' => '&Root', 'secure' => '!Secure ftps://')),\r
- 'OC_Filestorage_SMB' => array('backend' => 'SMB', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'share' => 'Share', 'root' => '&Root')),\r
- 'OC_Filestorage_DAV' => array('backend' => 'WebDAV', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'root' => '&Root', 'secure' => '!Secure https://'))\r
- );\r
- }\r
-\r
- /**\r
- * Get the system mount points\r
- * The returned array is not in the same format as getUserMountPoints()\r
- * @return array\r
- */\r
- public static function getSystemMountPoints() {\r
- $mountPoints = self::readData(false);\r
- $backends = self::getBackends();\r
- $system = array();\r
- if (isset($mountPoints[self::MOUNT_TYPE_GROUP])) {\r
- foreach ($mountPoints[self::MOUNT_TYPE_GROUP] as $group => $mounts) {\r
- foreach ($mounts as $mountPoint => $mount) {\r
- // Remove '/$user/files/' from mount point\r
- $mountPoint = substr($mountPoint, 13);\r
- // Merge the mount point into the current mount points\r
- if (isset($system[$mountPoint]) && $system[$mountPoint]['configuration'] == $mount['options']) {\r
- $system[$mountPoint]['applicable']['groups'] = array_merge($system[$mountPoint]['applicable']['groups'], array($group));\r
- } else {\r
- $system[$mountPoint] = array('class' => $mount['class'], 'backend' => $backends[$mount['class']]['backend'], 'configuration' => $mount['options'], 'applicable' => array('groups' => array($group), 'users' => array()));\r
- }\r
- }\r
- }\r
- }\r
- if (isset($mountPoints[self::MOUNT_TYPE_USER])) {\r
- foreach ($mountPoints[self::MOUNT_TYPE_USER] as $user => $mounts) {\r
- foreach ($mounts as $mountPoint => $mount) {\r
- // Remove '/$user/files/' from mount point\r
- $mountPoint = substr($mountPoint, 13);\r
- // Merge the mount point into the current mount points\r
- if (isset($system[$mountPoint]) && $system[$mountPoint]['configuration'] == $mount['options']) {\r
- $system[$mountPoint]['applicable']['users'] = array_merge($system[$mountPoint]['applicable']['users'], array($user));\r
- } else {\r
- $system[$mountPoint] = array('class' => $mount['class'], 'backend' => $backends[$mount['class']]['backend'], 'configuration' => $mount['options'], 'applicable' => array('groups' => array(), 'users' => array($user)));\r
- }\r
- }\r
- }\r
- }\r
- return $system;\r
- }\r
-\r
- /**\r
- * Get the personal mount points of the current user\r
- * The returned array is not in the same format as getUserMountPoints()\r
- * @return array\r
- */\r
- public static function getPersonalMountPoints() {\r
- $mountPoints = self::readData(true);\r
- $backends = self::getBackends();\r
- $uid = OCP\User::getUser();\r
- $personal = array();\r
- if (isset($mountPoints[self::MOUNT_TYPE_USER][$uid])) {\r
- foreach ($mountPoints[self::MOUNT_TYPE_USER][$uid] as $mountPoint => $mount) {\r
- // Remove '/uid/files/' from mount point\r
- $personal[substr($mountPoint, strlen($uid) + 8)] = array('class' => $mount['class'], 'backend' => $backends[$mount['class']]['backend'], 'configuration' => $mount['options']);\r
- }\r
- }\r
- return $personal;\r
- }\r
-\r
- \r
- /**\r
- * Add a mount point to the filesystem\r
- * @param string Mount point\r
- * @param string Backend class\r
- * @param array Backend parameters for the class\r
- * @param string MOUNT_TYPE_GROUP | MOUNT_TYPE_USER\r
- * @param string User or group to apply mount to\r
- * @param bool Personal or system mount point i.e. is this being called from the personal or admin page\r
- * @return bool\r
- */\r
- public static function addMountPoint($mountPoint, $class, $classOptions, $mountType, $applicable, $isPersonal = false) {\r
- if ($isPersonal) {\r
- // Verify that the mount point applies for the current user\r
- // Prevent non-admin users from mounting local storage\r
- if ($applicable != OCP\User::getUser() || $class == 'OC_Filestorage_Local') {\r
- return false;\r
- }\r
- $mountPoint = '/'.$applicable.'/files/'.ltrim($mountPoint, '/');\r
- } else {\r
- $mountPoint = '/$user/files/'.ltrim($mountPoint, '/');\r
- }\r
- $mount = array($applicable => array($mountPoint => array('class' => $class, 'options' => $classOptions)));\r
- $mountPoints = self::readData($isPersonal);\r
- // Merge the new mount point into the current mount points\r
- if (isset($mountPoints[$mountType])) {\r
- if (isset($mountPoints[$mountType][$applicable])) {\r
- $mountPoints[$mountType][$applicable] = array_merge($mountPoints[$mountType][$applicable], $mount[$applicable]);\r
- } else {\r
- $mountPoints[$mountType] = array_merge($mountPoints[$mountType], $mount);\r
- }\r
- } else {\r
- $mountPoints[$mountType] = $mount;\r
- }\r
- self::writeData($isPersonal, $mountPoints);\r
- return true;\r
- }\r
-\r
- /**\r
- *\r
- * @param string Mount point\r
- * @param string MOUNT_TYPE_GROUP | MOUNT_TYPE_USER\r
- * @param string User or group to remove mount from\r
- * @param bool Personal or system mount point\r
- * @return bool\r
- */\r
- public static function removeMountPoint($mountPoint, $mountType, $applicable, $isPersonal = false) {\r
- // Verify that the mount point applies for the current user\r
- if ($isPersonal) {\r
- if ($applicable != OCP\User::getUser()) {\r
- return false;\r
- }\r
- $mountPoint = '/'.$applicable.'/files/'.ltrim($mountPoint, '/');\r
- } else {\r
- $mountPoint = '/$user/files/'.ltrim($mountPoint, '/');\r
- }\r
- $mountPoints = self::readData($isPersonal);\r
- // Remove mount point\r
- unset($mountPoints[$mountType][$applicable][$mountPoint]);\r
- // Unset parent arrays if empty\r
- if (empty($mountPoints[$mountType][$applicable])) {\r
- unset($mountPoints[$mountType][$applicable]);\r
- if (empty($mountPoints[$mountType])) {\r
- unset($mountPoints[$mountType]);\r
- }\r
- }\r
- self::writeData($isPersonal, $mountPoints);\r
- return true;\r
- }\r
-\r
- /**\r
- * Read the mount points in the config file into an array\r
- * @param bool Personal or system config file\r
- * @return array\r
- */\r
- private static function readData($isPersonal) {\r
- if ($isPersonal) {\r
- $file = OC_User::getHome(OCP\User::getUser()).'/mount.php';\r
- } else {\r
- $file = OC::$SERVERROOT.'/config/mount.php';\r
- }\r
- if (is_file($file)) {\r
- $mountPoints = include($file);\r
- if (is_array($mountPoints)) {\r
- return $mountPoints;\r
- }\r
- }\r
- return array();\r
- }\r
-\r
- /**\r
- * Write the mount points to the config file\r
- * @param bool Personal or system config file\r
- * @param array Mount points\r
- */\r
- private static function writeData($isPersonal, $data) {\r
- if ($isPersonal) {\r
- $file = OC_User::getHome(OCP\User::getUser()).'/mount.php';\r
- } else {\r
- $file = OC::$SERVERROOT.'/config/mount.php';\r
- }\r
- $content = "<?php return array (\n";\r
- if (isset($data[self::MOUNT_TYPE_GROUP])) {\r
- $content .= "\t'group' => array (\n";\r
- foreach ($data[self::MOUNT_TYPE_GROUP] as $group => $mounts) {\r
- $content .= "\t\t'".$group."' => array (\n";\r
- foreach ($mounts as $mountPoint => $mount) {\r
- $content .= "\t\t\t'".$mountPoint."' => ".str_replace("\n", '', var_export($mount, true)).",\n";\r
- \r
- }\r
- $content .= "\t\t),\n";\r
- }\r
- $content .= "\t),\n";\r
- }\r
- if (isset($data[self::MOUNT_TYPE_USER])) {\r
- $content .= "\t'user' => array (\n";\r
- foreach ($data[self::MOUNT_TYPE_USER] as $user => $mounts) {\r
- $content .= "\t\t'".$user."' => array (\n";\r
- foreach ($mounts as $mountPoint => $mount) {\r
- $content .= "\t\t\t'".$mountPoint."' => ".str_replace("\n", '', var_export($mount, true)).",\n";\r
- }\r
- $content .= "\t\t),\n";\r
- }\r
- $content .= "\t),\n";\r
- }\r
- $content .= ");\n?>";\r
- @file_put_contents($file, $content);\r
- }\r
- \r
- /**\r
- * Returns all user uploaded ssl root certificates\r
- * @return array\r
- */\r
- public static function getCertificates() {\r
- $view = \OCP\Files::getStorage('files_external');\r
- $path=\OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath("").'uploads/';\r
- if (!is_dir($path)) mkdir($path);\r
- $result = array();\r
- $handle = opendir($path);\r
- while (false !== ($file = readdir($handle))) {\r
- if($file != '.' && $file != '..') $result[] = $file;\r
- }\r
- return $result;\r
- }\r
- \r
- /**\r
- * creates certificate bundle\r
- */\r
- public static function createCertificateBundle() {\r
- $view = \OCP\Files::getStorage("files_external");\r
- $path = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath("");\r
- \r
- $certs = OC_Mount_Config::getCertificates();\r
- $fh_certs = fopen($path."/rootcerts.crt", 'w');\r
- foreach ($certs as $cert) {\r
- $file=$path.'/uploads/'.$cert;\r
- $fh = fopen($file, "r");\r
- $data = fread($fh, filesize($file));\r
- fclose($fh);\r
- if (strpos($data, 'BEGIN CERTIFICATE')) {\r
- fwrite($fh_certs, $data);\r
- }\r
- }\r
- \r
- fclose($fh_certs);\r
- \r
- return true;\r
- } \r
-\r
-}\r
-\r
-?>
\ No newline at end of file
+<?php
+/**
+* ownCloud
+*
+* @author Michael Gapczynski
+* @copyright 2012 Michael Gapczynski mtgap@owncloud.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+* Class to configure the config/mount.php and data/$user/mount.php files
+*/
+class OC_Mount_Config {
+
+ const MOUNT_TYPE_GLOBAL = 'global';
+ const MOUNT_TYPE_GROUP = 'group';
+ const MOUNT_TYPE_USER = 'user';
+
+ /**
+ * Get details on each of the external storage backends, used for the mount config UI
+ * If a custom UI is needed, add the key 'custom' and a javascript file with that name will be loaded
+ * If the configuration parameter should be secret, add a '*' to the beginning of the value
+ * If the configuration parameter is a boolean, add a '!' to the beginning of the value
+ * If the configuration parameter is optional, add a '&' to the beginning of the value
+ * If the configuration parameter is hidden, add a '#' to the begining of the value
+ * @return array
+ */
+ public static function getBackends() {
+ return array(
+ 'OC_Filestorage_Local' => array('backend' => 'Local', 'configuration' => array('datadir' => 'Location')),
+ 'OC_Filestorage_AmazonS3' => array('backend' => 'Amazon S3', 'configuration' => array('key' => 'Key', 'secret' => '*Secret', 'bucket' => 'Bucket')),
+ 'OC_Filestorage_Dropbox' => array('backend' => 'Dropbox', 'configuration' => array('configured' => '#configured','app_key' => 'App key', 'app_secret' => 'App secret', 'token' => '#token', 'token_secret' => '#token_secret'), 'custom' => 'dropbox'),
+ 'OC_Filestorage_FTP' => array('backend' => 'FTP', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'root' => '&Root', 'secure' => '!Secure ftps://')),
+ 'OC_Filestorage_Google' => array('backend' => 'Google Drive', 'configuration' => array('configured' => '#configured', 'token' => '#token', 'token_secret' => '#token secret'), 'custom' => 'google'),
+ 'OC_Filestorage_SWIFT' => array('backend' => 'OpenStack Swift', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'token' => '*Token', 'root' => '&Root', 'secure' => '!Secure ftps://')),
+ 'OC_Filestorage_SMB' => array('backend' => 'SMB', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'share' => 'Share', 'root' => '&Root')),
+ 'OC_Filestorage_DAV' => array('backend' => 'WebDAV', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'root' => '&Root', 'secure' => '!Secure https://'))
+ );
+ }
+
+ /**
+ * Get the system mount points
+ * The returned array is not in the same format as getUserMountPoints()
+ * @return array
+ */
+ public static function getSystemMountPoints() {
+ $mountPoints = self::readData(false);
+ $backends = self::getBackends();
+ $system = array();
+ if (isset($mountPoints[self::MOUNT_TYPE_GROUP])) {
+ foreach ($mountPoints[self::MOUNT_TYPE_GROUP] as $group => $mounts) {
+ foreach ($mounts as $mountPoint => $mount) {
+ // Remove '/$user/files/' from mount point
+ $mountPoint = substr($mountPoint, 13);
+ // Merge the mount point into the current mount points
+ if (isset($system[$mountPoint]) && $system[$mountPoint]['configuration'] == $mount['options']) {
+ $system[$mountPoint]['applicable']['groups'] = array_merge($system[$mountPoint]['applicable']['groups'], array($group));
+ } else {
+ $system[$mountPoint] = array('class' => $mount['class'], 'backend' => $backends[$mount['class']]['backend'], 'configuration' => $mount['options'], 'applicable' => array('groups' => array($group), 'users' => array()));
+ }
+ }
+ }
+ }
+ if (isset($mountPoints[self::MOUNT_TYPE_USER])) {
+ foreach ($mountPoints[self::MOUNT_TYPE_USER] as $user => $mounts) {
+ foreach ($mounts as $mountPoint => $mount) {
+ // Remove '/$user/files/' from mount point
+ $mountPoint = substr($mountPoint, 13);
+ // Merge the mount point into the current mount points
+ if (isset($system[$mountPoint]) && $system[$mountPoint]['configuration'] == $mount['options']) {
+ $system[$mountPoint]['applicable']['users'] = array_merge($system[$mountPoint]['applicable']['users'], array($user));
+ } else {
+ $system[$mountPoint] = array('class' => $mount['class'], 'backend' => $backends[$mount['class']]['backend'], 'configuration' => $mount['options'], 'applicable' => array('groups' => array(), 'users' => array($user)));
+ }
+ }
+ }
+ }
+ return $system;
+ }
+
+ /**
+ * Get the personal mount points of the current user
+ * The returned array is not in the same format as getUserMountPoints()
+ * @return array
+ */
+ public static function getPersonalMountPoints() {
+ $mountPoints = self::readData(true);
+ $backends = self::getBackends();
+ $uid = OCP\User::getUser();
+ $personal = array();
+ if (isset($mountPoints[self::MOUNT_TYPE_USER][$uid])) {
+ foreach ($mountPoints[self::MOUNT_TYPE_USER][$uid] as $mountPoint => $mount) {
+ // Remove '/uid/files/' from mount point
+ $personal[substr($mountPoint, strlen($uid) + 8)] = array('class' => $mount['class'], 'backend' => $backends[$mount['class']]['backend'], 'configuration' => $mount['options']);
+ }
+ }
+ return $personal;
+ }
+
+
+ /**
+ * Add a mount point to the filesystem
+ * @param string Mount point
+ * @param string Backend class
+ * @param array Backend parameters for the class
+ * @param string MOUNT_TYPE_GROUP | MOUNT_TYPE_USER
+ * @param string User or group to apply mount to
+ * @param bool Personal or system mount point i.e. is this being called from the personal or admin page
+ * @return bool
+ */
+ public static function addMountPoint($mountPoint, $class, $classOptions, $mountType, $applicable, $isPersonal = false) {
+ if ($isPersonal) {
+ // Verify that the mount point applies for the current user
+ // Prevent non-admin users from mounting local storage
+ if ($applicable != OCP\User::getUser() || $class == 'OC_Filestorage_Local') {
+ return false;
+ }
+ $mountPoint = '/'.$applicable.'/files/'.ltrim($mountPoint, '/');
+ } else {
+ $mountPoint = '/$user/files/'.ltrim($mountPoint, '/');
+ }
+ $mount = array($applicable => array($mountPoint => array('class' => $class, 'options' => $classOptions)));
+ $mountPoints = self::readData($isPersonal);
+ // Merge the new mount point into the current mount points
+ if (isset($mountPoints[$mountType])) {
+ if (isset($mountPoints[$mountType][$applicable])) {
+ $mountPoints[$mountType][$applicable] = array_merge($mountPoints[$mountType][$applicable], $mount[$applicable]);
+ } else {
+ $mountPoints[$mountType] = array_merge($mountPoints[$mountType], $mount);
+ }
+ } else {
+ $mountPoints[$mountType] = $mount;
+ }
+ self::writeData($isPersonal, $mountPoints);
+ return true;
+ }
+
+ /**
+ *
+ * @param string Mount point
+ * @param string MOUNT_TYPE_GROUP | MOUNT_TYPE_USER
+ * @param string User or group to remove mount from
+ * @param bool Personal or system mount point
+ * @return bool
+ */
+ public static function removeMountPoint($mountPoint, $mountType, $applicable, $isPersonal = false) {
+ // Verify that the mount point applies for the current user
+ if ($isPersonal) {
+ if ($applicable != OCP\User::getUser()) {
+ return false;
+ }
+ $mountPoint = '/'.$applicable.'/files/'.ltrim($mountPoint, '/');
+ } else {
+ $mountPoint = '/$user/files/'.ltrim($mountPoint, '/');
+ }
+ $mountPoints = self::readData($isPersonal);
+ // Remove mount point
+ unset($mountPoints[$mountType][$applicable][$mountPoint]);
+ // Unset parent arrays if empty
+ if (empty($mountPoints[$mountType][$applicable])) {
+ unset($mountPoints[$mountType][$applicable]);
+ if (empty($mountPoints[$mountType])) {
+ unset($mountPoints[$mountType]);
+ }
+ }
+ self::writeData($isPersonal, $mountPoints);
+ return true;
+ }
+
+ /**
+ * Read the mount points in the config file into an array
+ * @param bool Personal or system config file
+ * @return array
+ */
+ private static function readData($isPersonal) {
+ if ($isPersonal) {
+ $file = OC_User::getHome(OCP\User::getUser()).'/mount.php';
+ } else {
+ $file = OC::$SERVERROOT.'/config/mount.php';
+ }
+ if (is_file($file)) {
+ $mountPoints = include($file);
+ if (is_array($mountPoints)) {
+ return $mountPoints;
+ }
+ }
+ return array();
+ }
+
+ /**
+ * Write the mount points to the config file
+ * @param bool Personal or system config file
+ * @param array Mount points
+ */
+ private static function writeData($isPersonal, $data) {
+ if ($isPersonal) {
+ $file = OC_User::getHome(OCP\User::getUser()).'/mount.php';
+ } else {
+ $file = OC::$SERVERROOT.'/config/mount.php';
+ }
+ $content = "<?php return array (\n";
+ if (isset($data[self::MOUNT_TYPE_GROUP])) {
+ $content .= "\t'group' => array (\n";
+ foreach ($data[self::MOUNT_TYPE_GROUP] as $group => $mounts) {
+ $content .= "\t\t'".$group."' => array (\n";
+ foreach ($mounts as $mountPoint => $mount) {
+ $content .= "\t\t\t'".$mountPoint."' => ".str_replace("\n", '', var_export($mount, true)).",\n";
+
+ }
+ $content .= "\t\t),\n";
+ }
+ $content .= "\t),\n";
+ }
+ if (isset($data[self::MOUNT_TYPE_USER])) {
+ $content .= "\t'user' => array (\n";
+ foreach ($data[self::MOUNT_TYPE_USER] as $user => $mounts) {
+ $content .= "\t\t'".$user."' => array (\n";
+ foreach ($mounts as $mountPoint => $mount) {
+ $content .= "\t\t\t'".$mountPoint."' => ".str_replace("\n", '', var_export($mount, true)).",\n";
+ }
+ $content .= "\t\t),\n";
+ }
+ $content .= "\t),\n";
+ }
+ $content .= ");\n?>";
+ @file_put_contents($file, $content);
+ }
+
+ /**
+ * Returns all user uploaded ssl root certificates
+ * @return array
+ */
+ public static function getCertificates() {
+ $view = \OCP\Files::getStorage('files_external');
+ $path=\OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath("").'uploads/';
+ if (!is_dir($path)) mkdir($path);
+ $result = array();
+ $handle = opendir($path);
+ while (false !== ($file = readdir($handle))) {
+ if($file != '.' && $file != '..') $result[] = $file;
+ }
+ return $result;
+ }
+
+ /**
+ * creates certificate bundle
+ */
+ public static function createCertificateBundle() {
+ $view = \OCP\Files::getStorage("files_external");
+ $path = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath("");
+
+ $certs = OC_Mount_Config::getCertificates();
+ $fh_certs = fopen($path."/rootcerts.crt", 'w');
+ foreach ($certs as $cert) {
+ $file=$path.'/uploads/'.$cert;
+ $fh = fopen($file, "r");
+ $data = fread($fh, filesize($file));
+ fclose($fh);
+ if (strpos($data, 'BEGIN CERTIFICATE')) {
+ fwrite($fh_certs, $data);
+ }
+ }
+
+ fclose($fh_certs);
+
+ return true;
+ }
+
+}
-<?php\r
-/**\r
- * Copyright (c) 2012 Frank Karlitschek <frank@owncloud.org>\r
- * This file is licensed under the Affero General Public License version 3 or\r
- * later.\r
- * See the COPYING-README file.\r
- */\r
-\r
-/**\r
- * Versions\r
- *\r
- * A class to handle the versioning of files.\r
- */\r
-\r
-namespace OCA_Versions;\r
-\r
-class Storage {\r
-\r
-\r
- // config.php configuration:\r
- // - files_versions\r
- // - files_versionsfolder\r
- // - files_versionsblacklist\r
- // - files_versionsmaxfilesize\r
- // - files_versionsinterval\r
- // - files_versionmaxversions\r
- //\r
- // todo:\r
- // - finish porting to OC_FilesystemView to enable network transparency\r
- // - add transparent compression. first test if it´s worth it.\r
-\r
- const DEFAULTENABLED=true;\r
- const DEFAULTBLACKLIST='avi mp3 mpg mp4 ctmp';\r
- const DEFAULTMAXFILESIZE=1048576; // 10MB\r
- const DEFAULTMININTERVAL=60; // 1 min\r
- const DEFAULTMAXVERSIONS=50;\r
-\r
- private $view;\r
-\r
- function __construct() {\r
-\r
- $this->view = \OCP\Files::getStorage('files_versions');\r
-\r
- }\r
-\r
- /**\r
- * listen to write event.\r
- */\r
- public static function write_hook($params) {\r
- if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {\r
- $path = $params[\OC_Filesystem::signal_param_path];\r
- if($path<>'') $this->store($path);\r
- }\r
- }\r
-\r
-\r
-\r
- /**\r
- * store a new version of a file.\r
- */\r
- public function store($filename) {\r
- if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {\r
-\r
- $files_view = \OCP\Files::getStorage("files");\r
- $users_view = \OCP\Files::getStorage("files_versions");\r
- $users_view->chroot(\OCP\User::getUser().'/');\r
- \r
- //FIXME OC_Share no longer exists\r
- //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {\r
- // $pos = strpos($source, '/files', 1);\r
- // $uid = substr($source, 1, $pos - 1);\r
- // $filename = substr($source, $pos + 6);\r
- //} else {\r
- $uid = \OCP\User::getUser();\r
- //}\r
- \r
- $versionsFolderName=\OCP\Config::getSystemValue('datadirectory') . $this->view->getAbsolutePath('');\r
- \r
- //check if source file already exist as version to avoid recursions.\r
- if ($users_view->file_exists($filename)) {\r
- return false;\r
- }\r
- \r
- // check if filename is a directory\r
- if($files_view->is_dir($filename)){\r
- return false;\r
- }\r
-\r
- // check filetype blacklist\r
- $blacklist=explode(' ',\OCP\Config::getSystemValue('files_versionsblacklist', Storage::DEFAULTBLACKLIST));\r
- foreach($blacklist as $bl) {\r
- $parts=explode('.', $filename);\r
- $ext=end($parts);\r
- if(strtolower($ext)==$bl) {\r
- return false;\r
- }\r
- }\r
- \r
- // check filesize\r
- if($files_view->filesize($filename)>\OCP\Config::getSystemValue('files_versionsmaxfilesize', Storage::DEFAULTMAXFILESIZE)){\r
- return false;\r
- }\r
-\r
-\r
- // check mininterval if the file is being modified by the owner (all shared files should be versioned despite mininterval)\r
- if ($uid == \OCP\User::getUser()) {\r
- $matches=glob($versionsFolderName.'/'.$filename.'.v*');\r
- sort($matches);\r
- $parts=explode('.v',end($matches));\r
- if((end($parts)+Storage::DEFAULTMININTERVAL)>time()){\r
- return false;\r
- }\r
- }\r
-\r
-\r
- // create all parent folders\r
- $info=pathinfo($filename);\r
- if(!file_exists($versionsFolderName.'/'.$info['dirname'])) mkdir($versionsFolderName.'/'.$info['dirname'],0700,true);\r
-\r
- // store a new version of a file\r
- @$users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.time());\r
-\r
- // expire old revisions if necessary\r
- Storage::expire($filename);\r
- }\r
- }\r
-\r
-\r
- /**\r
- * rollback to an old version of a file.\r
- */\r
- public static function rollback($filename,$revision) {\r
-\r
- if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {\r
- $users_view = \OCP\Files::getStorage("files_versions");\r
- $users_view->chroot(\OCP\User::getUser().'/');\r
- \r
- //FIXME OC_Share no longer exists\r
- //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {\r
- // $pos = strpos($source, '/files', 1);\r
- // $uid = substr($source, 1, $pos - 1);\r
- // $filename = substr($source, $pos + 6);\r
- //} else {\r
- $uid = \OCP\User::getUser();\r
- //}\r
- \r
- // rollback\r
- if( @$users_view->copy('files_versions'.$filename.'.v'.$revision, 'files'.$filename) ) {\r
- \r
- return true;\r
-\r
- }else{\r
- \r
- return false;\r
-\r
- }\r
- \r
- }\r
-\r
- }\r
-\r
- /**\r
- * check if old versions of a file exist.\r
- */\r
- public static function isversioned($filename) {\r
- if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {\r
- $versions_fileview = \OCP\Files::getStorage("files_versions");\r
- //FIXME OC_Share no longer exists\r
- //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {\r
- // $pos = strpos($source, '/files', 1);\r
- // $filename = substr($source, $pos + 6);\r
- //}\r
-\r
- $versionsFolderName=\OCP\Config::getSystemValue('datadirectory'). $versions_fileview->getAbsolutePath('');\r
- \r
- // check for old versions\r
- $matches=glob($versionsFolderName.$filename.'.v*');\r
- if(count($matches)>0){\r
- return true;\r
- }else{\r
- return false;\r
- }\r
- }else{\r
- return(false);\r
- }\r
- }\r
-\r
-\r
-\r
- /**\r
- * @brief get a list of all available versions of a file in descending chronological order\r
- * @param $filename file to find versions of, relative to the user files dir\r
- * @param $count number of versions to return\r
- * @returns array\r
- */\r
- public static function getVersions( $filename, $count = 0 ) {\r
-\r
- if( \OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true' ) {\r
-\r
- //FIXME OC_Share no longer exists\r
- //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {\r
- // $pos = strpos($source, '/files', 1);\r
- // $uid = substr($source, 1, $pos - 1);\r
- // $filename = substr($source, $pos + 6);\r
- //} else {\r
- $uid = \OCP\User::getUser();\r
- //}\r
- $versions_fileview = \OCP\Files::getStorage('files_versions');\r
- $versionsFolderName = \OCP\Config::getSystemValue('datadirectory'). $versions_fileview->getAbsolutePath('');\r
- $versions = array();\r
- \r
- // fetch for old versions\r
- $matches = glob( $versionsFolderName.'/'.$filename.'.v*' );\r
- \r
- sort( $matches );\r
- \r
- $i = 0;\r
- \r
- $files_view = \OCP\Files::getStorage('files');\r
- $local_file = $files_view->getLocalFile($filename);\r
- foreach( $matches as $ma ) {\r
-\r
- $i++;\r
- $versions[$i]['cur'] = 0;\r
- $parts = explode( '.v', $ma );\r
- $versions[$i]['version'] = ( end( $parts ) );\r
-\r
- // if file with modified date exists, flag it in array as currently enabled version\r
- ( \md5_file( $ma ) == \md5_file( $local_file ) ? $versions[$i]['fileMatch'] = 1 : $versions[$i]['fileMatch'] = 0 );\r
-\r
- }\r
- \r
- $versions = array_reverse( $versions );\r
- \r
- foreach( $versions as $key => $value ) {\r
-\r
- // flag the first matched file in array (which will have latest modification date) as current version\r
- if ( $value['fileMatch'] ) {\r
-\r
- $value['cur'] = 1;\r
- break;\r
- \r
- }\r
- \r
- }\r
- \r
- $versions = array_reverse( $versions );\r
- \r
- // only show the newest commits\r
- if( $count != 0 and ( count( $versions )>$count ) ) {\r
- \r
- $versions = array_slice( $versions, count( $versions ) - $count );\r
-\r
- }\r
-\r
- return( $versions );\r
-\r
-\r
- } else {\r
-\r
- // if versioning isn't enabled then return an empty array\r
- return( array() );\r
-\r
- }\r
-\r
- }\r
-\r
- /**\r
- * @brief Erase a file's versions which exceed the set quota\r
- */\r
- public static function expire($filename) {\r
- if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {\r
-\r
- //FIXME OC_Share no longer exists\r
- //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {\r
- // $pos = strpos($source, '/files', 1);\r
- // $uid = substr($source, 1, $pos - 1);\r
- // $filename = substr($source, $pos + 6);\r
- //} else {\r
- $uid = \OCP\User::getUser();\r
- //}\r
- $versions_fileview = \OCP\Files::getStorage("files_versions");\r
- $versionsFolderName=\OCP\Config::getSystemValue('datadirectory'). $versions_fileview->getAbsolutePath('');\r
-\r
- // check for old versions\r
- $matches = glob( $versionsFolderName.'/'.$filename.'.v*' );\r
- \r
- if( count( $matches ) > \OCP\Config::getSystemValue( 'files_versionmaxversions', Storage::DEFAULTMAXVERSIONS ) ) {\r
- \r
- $numberToDelete = count( $matches-\OCP\Config::getSystemValue( 'files_versionmaxversions', Storage::DEFAULTMAXVERSIONS ) );\r
-\r
- // delete old versions of a file\r
- $deleteItems = array_slice( $matches, 0, $numberToDelete );\r
-\r
- foreach( $deleteItems as $de ) {\r
-\r
- unlink( $versionsFolderName.'/'.$filename.'.v'.$de );\r
- \r
- }\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * @brief Erase all old versions of all user files\r
- * @return true/false\r
- */\r
- public function expireAll() {\r
- return $this->view->deleteAll('', true);\r
- }\r
-}\r
+<?php
+/**
+ * Copyright (c) 2012 Frank Karlitschek <frank@owncloud.org>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+/**
+ * Versions
+ *
+ * A class to handle the versioning of files.
+ */
+
+namespace OCA_Versions;
+
+class Storage {
+
+
+ // config.php configuration:
+ // - files_versions
+ // - files_versionsfolder
+ // - files_versionsblacklist
+ // - files_versionsmaxfilesize
+ // - files_versionsinterval
+ // - files_versionmaxversions
+ //
+ // todo:
+ // - finish porting to OC_FilesystemView to enable network transparency
+ // - add transparent compression. first test if it´s worth it.
+
+ const DEFAULTENABLED=true;
+ const DEFAULTBLACKLIST='avi mp3 mpg mp4 ctmp';
+ const DEFAULTMAXFILESIZE=1048576; // 10MB
+ const DEFAULTMININTERVAL=60; // 1 min
+ const DEFAULTMAXVERSIONS=50;
+
+ private $view;
+
+ function __construct() {
+
+ $this->view = \OCP\Files::getStorage('files_versions');
+
+ }
+
+ /**
+ * listen to write event.
+ */
+ public static function write_hook($params) {
+ if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
+ $path = $params[\OC_Filesystem::signal_param_path];
+ if($path<>'') $this->store($path);
+ }
+ }
+
+
+
+ /**
+ * store a new version of a file.
+ */
+ public function store($filename) {
+ if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
+
+ $files_view = \OCP\Files::getStorage("files");
+ $users_view = \OCP\Files::getStorage("files_versions");
+ $users_view->chroot(\OCP\User::getUser().'/');
+
+ //FIXME OC_Share no longer exists
+ //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {
+ // $pos = strpos($source, '/files', 1);
+ // $uid = substr($source, 1, $pos - 1);
+ // $filename = substr($source, $pos + 6);
+ //} else {
+ $uid = \OCP\User::getUser();
+ //}
+
+ $versionsFolderName=\OCP\Config::getSystemValue('datadirectory') . $this->view->getAbsolutePath('');
+
+ //check if source file already exist as version to avoid recursions.
+ if ($users_view->file_exists($filename)) {
+ return false;
+ }
+
+ // check if filename is a directory
+ if($files_view->is_dir($filename)){
+ return false;
+ }
+
+ // check filetype blacklist
+ $blacklist=explode(' ',\OCP\Config::getSystemValue('files_versionsblacklist', Storage::DEFAULTBLACKLIST));
+ foreach($blacklist as $bl) {
+ $parts=explode('.', $filename);
+ $ext=end($parts);
+ if(strtolower($ext)==$bl) {
+ return false;
+ }
+ }
+
+ // check filesize
+ if($files_view->filesize($filename)>\OCP\Config::getSystemValue('files_versionsmaxfilesize', Storage::DEFAULTMAXFILESIZE)){
+ return false;
+ }
+
+
+ // check mininterval if the file is being modified by the owner (all shared files should be versioned despite mininterval)
+ if ($uid == \OCP\User::getUser()) {
+ $matches=glob($versionsFolderName.'/'.$filename.'.v*');
+ sort($matches);
+ $parts=explode('.v',end($matches));
+ if((end($parts)+Storage::DEFAULTMININTERVAL)>time()){
+ return false;
+ }
+ }
+
+
+ // create all parent folders
+ $info=pathinfo($filename);
+ if(!file_exists($versionsFolderName.'/'.$info['dirname'])) mkdir($versionsFolderName.'/'.$info['dirname'],0700,true);
+
+ // store a new version of a file
+ @$users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.time());
+
+ // expire old revisions if necessary
+ Storage::expire($filename);
+ }
+ }
+
+
+ /**
+ * rollback to an old version of a file.
+ */
+ public static function rollback($filename,$revision) {
+
+ if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
+ $users_view = \OCP\Files::getStorage("files_versions");
+ $users_view->chroot(\OCP\User::getUser().'/');
+
+ //FIXME OC_Share no longer exists
+ //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {
+ // $pos = strpos($source, '/files', 1);
+ // $uid = substr($source, 1, $pos - 1);
+ // $filename = substr($source, $pos + 6);
+ //} else {
+ $uid = \OCP\User::getUser();
+ //}
+
+ // rollback
+ if( @$users_view->copy('files_versions'.$filename.'.v'.$revision, 'files'.$filename) ) {
+
+ return true;
+
+ }else{
+
+ return false;
+
+ }
+
+ }
+
+ }
+
+ /**
+ * check if old versions of a file exist.
+ */
+ public static function isversioned($filename) {
+ if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
+ $versions_fileview = \OCP\Files::getStorage("files_versions");
+ //FIXME OC_Share no longer exists
+ //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {
+ // $pos = strpos($source, '/files', 1);
+ // $filename = substr($source, $pos + 6);
+ //}
+
+ $versionsFolderName=\OCP\Config::getSystemValue('datadirectory'). $versions_fileview->getAbsolutePath('');
+
+ // check for old versions
+ $matches=glob($versionsFolderName.$filename.'.v*');
+ if(count($matches)>0){
+ return true;
+ }else{
+ return false;
+ }
+ }else{
+ return(false);
+ }
+ }
+
+
+
+ /**
+ * @brief get a list of all available versions of a file in descending chronological order
+ * @param $filename file to find versions of, relative to the user files dir
+ * @param $count number of versions to return
+ * @returns array
+ */
+ public static function getVersions( $filename, $count = 0 ) {
+
+ if( \OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true' ) {
+
+ //FIXME OC_Share no longer exists
+ //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {
+ // $pos = strpos($source, '/files', 1);
+ // $uid = substr($source, 1, $pos - 1);
+ // $filename = substr($source, $pos + 6);
+ //} else {
+ $uid = \OCP\User::getUser();
+ //}
+ $versions_fileview = \OCP\Files::getStorage('files_versions');
+ $versionsFolderName = \OCP\Config::getSystemValue('datadirectory'). $versions_fileview->getAbsolutePath('');
+ $versions = array();
+
+ // fetch for old versions
+ $matches = glob( $versionsFolderName.'/'.$filename.'.v*' );
+
+ sort( $matches );
+
+ $i = 0;
+
+ $files_view = \OCP\Files::getStorage('files');
+ $local_file = $files_view->getLocalFile($filename);
+ foreach( $matches as $ma ) {
+
+ $i++;
+ $versions[$i]['cur'] = 0;
+ $parts = explode( '.v', $ma );
+ $versions[$i]['version'] = ( end( $parts ) );
+
+ // if file with modified date exists, flag it in array as currently enabled version
+ ( \md5_file( $ma ) == \md5_file( $local_file ) ? $versions[$i]['fileMatch'] = 1 : $versions[$i]['fileMatch'] = 0 );
+
+ }
+
+ $versions = array_reverse( $versions );
+
+ foreach( $versions as $key => $value ) {
+
+ // flag the first matched file in array (which will have latest modification date) as current version
+ if ( $value['fileMatch'] ) {
+
+ $value['cur'] = 1;
+ break;
+
+ }
+
+ }
+
+ $versions = array_reverse( $versions );
+
+ // only show the newest commits
+ if( $count != 0 and ( count( $versions )>$count ) ) {
+
+ $versions = array_slice( $versions, count( $versions ) - $count );
+
+ }
+
+ return( $versions );
+
+
+ } else {
+
+ // if versioning isn't enabled then return an empty array
+ return( array() );
+
+ }
+
+ }
+
+ /**
+ * @brief Erase a file's versions which exceed the set quota
+ */
+ public static function expire($filename) {
+ if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
+
+ //FIXME OC_Share no longer exists
+ //if (\OCP\App::isEnabled('files_sharing') && $source = \OC_Share::getSource('/'.\OCP\User::getUser().'/files'.$filename)) {
+ // $pos = strpos($source, '/files', 1);
+ // $uid = substr($source, 1, $pos - 1);
+ // $filename = substr($source, $pos + 6);
+ //} else {
+ $uid = \OCP\User::getUser();
+ //}
+ $versions_fileview = \OCP\Files::getStorage("files_versions");
+ $versionsFolderName=\OCP\Config::getSystemValue('datadirectory'). $versions_fileview->getAbsolutePath('');
+
+ // check for old versions
+ $matches = glob( $versionsFolderName.'/'.$filename.'.v*' );
+
+ if( count( $matches ) > \OCP\Config::getSystemValue( 'files_versionmaxversions', Storage::DEFAULTMAXVERSIONS ) ) {
+
+ $numberToDelete = count( $matches-\OCP\Config::getSystemValue( 'files_versionmaxversions', Storage::DEFAULTMAXVERSIONS ) );
+
+ // delete old versions of a file
+ $deleteItems = array_slice( $matches, 0, $numberToDelete );
+
+ foreach( $deleteItems as $de ) {
+
+ unlink( $versionsFolderName.'/'.$filename.'.v'.$de );
+
+ }
+ }
+ }
+ }
+
+ /**
+ * @brief Erase all old versions of all user files
+ * @return true/false
+ */
+ public function expireAll() {
+ return $this->view->deleteAll('', true);
+ }
+}
-<?php\r
-\r
-/**\r
- * ownCloud\r
- *\r
- * @author Bjoern Schiessle\r
- * @copyright 2012 Bjoern Schiessle <schiessle@owncloud.com>\r
- *\r
- * This library is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE\r
- * License as published by the Free Software Foundation; either\r
- * version 3 of the License, or any later version.\r
- *\r
- * This library is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.\r
- *\r
- * You should have received a copy of the GNU Affero General Public\r
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.\r
- *\r
- */\r
-\r
-class OC_Connector_Sabre_Client extends Sabre_DAV_Client {\r
- \r
- protected $trustedCertificates;\r
-\r
- /**\r
- * Add trusted root certificates to the webdav client.\r
- *\r
- * The parameter certificates should be a absulute path to a file which contains\r
- * all trusted certificates\r
- *\r
- * @param string $certificates\r
- */\r
- public function addTrustedCertificates($certificates) {\r
- $this->trustedCertificates = $certificates;\r
- }\r
- \r
- /**\r
- * Copied from SabreDAV with some modification to use user defined curlSettings\r
- * Performs an actual HTTP request, and returns the result.\r
- *\r
- * If the specified url is relative, it will be expanded based on the base\r
- * url.\r
- *\r
- * The returned array contains 3 keys:\r
- * * body - the response body\r
- * * httpCode - a HTTP code (200, 404, etc)\r
- * * headers - a list of response http headers. The header names have\r
- * been lowercased.\r
- *\r
- * @param string $method\r
- * @param string $url\r
- * @param string $body\r
- * @param array $headers\r
- * @return array\r
- */\r
- public function request($method, $url = '', $body = null, $headers = array()) {\r
- \r
- $url = $this->getAbsoluteUrl($url);\r
- \r
- $curlSettings = array(\r
- CURLOPT_RETURNTRANSFER => true,\r
- // Return headers as part of the response\r
- CURLOPT_HEADER => true,\r
- CURLOPT_POSTFIELDS => $body,\r
- // Automatically follow redirects\r
- CURLOPT_FOLLOWLOCATION => true,\r
- CURLOPT_MAXREDIRS => 5,\r
- );\r
- \r
- if($this->trustedCertificates) {\r
- $curlSettings[CURLOPT_CAINFO] = $this->trustedCertificates;\r
- }\r
- \r
- switch ($method) {\r
- case 'HEAD' :\r
- \r
- // do not read body with HEAD requests (this is neccessary because cURL does not ignore the body with HEAD\r
- // requests when the Content-Length header is given - which in turn is perfectly valid according to HTTP\r
- // specs...) cURL does unfortunately return an error in this case ("transfer closed transfer closed with\r
- // ... bytes remaining to read") this can be circumvented by explicitly telling cURL to ignore the\r
- // response body\r
- $curlSettings[CURLOPT_NOBODY] = true;\r
- $curlSettings[CURLOPT_CUSTOMREQUEST] = 'HEAD';\r
- break;\r
- \r
- default:\r
- $curlSettings[CURLOPT_CUSTOMREQUEST] = $method;\r
- break;\r
- \r
- }\r
- \r
- // Adding HTTP headers\r
- $nHeaders = array();\r
- foreach($headers as $key=>$value) {\r
- \r
- $nHeaders[] = $key . ': ' . $value;\r
- \r
- }\r
- $curlSettings[CURLOPT_HTTPHEADER] = $nHeaders;\r
- \r
- if ($this->proxy) {\r
- $curlSettings[CURLOPT_PROXY] = $this->proxy;\r
- }\r
- \r
- if ($this->userName && $this->authType) {\r
- $curlType = 0;\r
- if ($this->authType & self::AUTH_BASIC) {\r
- $curlType |= CURLAUTH_BASIC;\r
- }\r
- if ($this->authType & self::AUTH_DIGEST) {\r
- $curlType |= CURLAUTH_DIGEST;\r
- }\r
- $curlSettings[CURLOPT_HTTPAUTH] = $curlType;\r
- $curlSettings[CURLOPT_USERPWD] = $this->userName . ':' . $this->password;\r
- }\r
- \r
- list(\r
- $response,\r
- $curlInfo,\r
- $curlErrNo,\r
- $curlError\r
- ) = $this->curlRequest($url, $curlSettings);\r
- \r
- $headerBlob = substr($response, 0, $curlInfo['header_size']);\r
- $response = substr($response, $curlInfo['header_size']);\r
- \r
- // In the case of 100 Continue, or redirects we'll have multiple lists\r
- // of headers for each separate HTTP response. We can easily split this\r
- // because they are separated by \r\n\r\n\r
- $headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n"));\r
- \r
- // We only care about the last set of headers\r
- $headerBlob = $headerBlob[count($headerBlob)-1];\r
- \r
- // Splitting headers\r
- $headerBlob = explode("\r\n", $headerBlob);\r
- \r
- $headers = array();\r
- foreach($headerBlob as $header) {\r
- $parts = explode(':', $header, 2);\r
- if (count($parts)==2) {\r
- $headers[strtolower(trim($parts[0]))] = trim($parts[1]);\r
- }\r
- }\r
- \r
- $response = array(\r
- 'body' => $response,\r
- 'statusCode' => $curlInfo['http_code'],\r
- 'headers' => $headers\r
- );\r
- \r
- if ($curlErrNo) {\r
- throw new Sabre_DAV_Exception('[CURL] Error while making request: ' . $curlError . ' (error code: ' . $curlErrNo . ')');\r
- }\r
- \r
- if ($response['statusCode']>=400) {\r
- switch ($response['statusCode']) {\r
- case 404:\r
- throw new Sabre_DAV_Exception_NotFound('Resource ' . $url . ' not found.');\r
- break;\r
- \r
- default:\r
- throw new Sabre_DAV_Exception('HTTP error response. (errorcode ' . $response['statusCode'] . ')');\r
- }\r
- }\r
- \r
- return $response;\r
- \r
- } \r
+<?php
+
+/**
+ * ownCloud
+ *
+ * @author Bjoern Schiessle
+ * @copyright 2012 Bjoern Schiessle <schiessle@owncloud.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+class OC_Connector_Sabre_Client extends Sabre_DAV_Client {
+
+ protected $trustedCertificates;
+
+ /**
+ * Add trusted root certificates to the webdav client.
+ *
+ * The parameter certificates should be a absulute path to a file which contains
+ * all trusted certificates
+ *
+ * @param string $certificates
+ */
+ public function addTrustedCertificates($certificates) {
+ $this->trustedCertificates = $certificates;
+ }
+
+ /**
+ * Copied from SabreDAV with some modification to use user defined curlSettings
+ * Performs an actual HTTP request, and returns the result.
+ *
+ * If the specified url is relative, it will be expanded based on the base
+ * url.
+ *
+ * The returned array contains 3 keys:
+ * * body - the response body
+ * * httpCode - a HTTP code (200, 404, etc)
+ * * headers - a list of response http headers. The header names have
+ * been lowercased.
+ *
+ * @param string $method
+ * @param string $url
+ * @param string $body
+ * @param array $headers
+ * @return array
+ */
+ public function request($method, $url = '', $body = null, $headers = array()) {
+
+ $url = $this->getAbsoluteUrl($url);
+
+ $curlSettings = array(
+ CURLOPT_RETURNTRANSFER => true,
+ // Return headers as part of the response
+ CURLOPT_HEADER => true,
+ CURLOPT_POSTFIELDS => $body,
+ // Automatically follow redirects
+ CURLOPT_FOLLOWLOCATION => true,
+ CURLOPT_MAXREDIRS => 5,
+ );
+
+ if($this->trustedCertificates) {
+ $curlSettings[CURLOPT_CAINFO] = $this->trustedCertificates;
+ }
+
+ switch ($method) {
+ case 'HEAD' :
+
+ // do not read body with HEAD requests (this is neccessary because cURL does not ignore the body with HEAD
+ // requests when the Content-Length header is given - which in turn is perfectly valid according to HTTP
+ // specs...) cURL does unfortunately return an error in this case ("transfer closed transfer closed with
+ // ... bytes remaining to read") this can be circumvented by explicitly telling cURL to ignore the
+ // response body
+ $curlSettings[CURLOPT_NOBODY] = true;
+ $curlSettings[CURLOPT_CUSTOMREQUEST] = 'HEAD';
+ break;
+
+ default:
+ $curlSettings[CURLOPT_CUSTOMREQUEST] = $method;
+ break;
+
+ }
+
+ // Adding HTTP headers
+ $nHeaders = array();
+ foreach($headers as $key=>$value) {
+
+ $nHeaders[] = $key . ': ' . $value;
+
+ }
+ $curlSettings[CURLOPT_HTTPHEADER] = $nHeaders;
+
+ if ($this->proxy) {
+ $curlSettings[CURLOPT_PROXY] = $this->proxy;
+ }
+
+ if ($this->userName && $this->authType) {
+ $curlType = 0;
+ if ($this->authType & self::AUTH_BASIC) {
+ $curlType |= CURLAUTH_BASIC;
+ }
+ if ($this->authType & self::AUTH_DIGEST) {
+ $curlType |= CURLAUTH_DIGEST;
+ }
+ $curlSettings[CURLOPT_HTTPAUTH] = $curlType;
+ $curlSettings[CURLOPT_USERPWD] = $this->userName . ':' . $this->password;
+ }
+
+ list(
+ $response,
+ $curlInfo,
+ $curlErrNo,
+ $curlError
+ ) = $this->curlRequest($url, $curlSettings);
+
+ $headerBlob = substr($response, 0, $curlInfo['header_size']);
+ $response = substr($response, $curlInfo['header_size']);
+
+ // In the case of 100 Continue, or redirects we'll have multiple lists
+ // of headers for each separate HTTP response. We can easily split this
+ // because they are separated by \r\n\r\n
+ $headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n"));
+
+ // We only care about the last set of headers
+ $headerBlob = $headerBlob[count($headerBlob)-1];
+
+ // Splitting headers
+ $headerBlob = explode("\r\n", $headerBlob);
+
+ $headers = array();
+ foreach($headerBlob as $header) {
+ $parts = explode(':', $header, 2);
+ if (count($parts)==2) {
+ $headers[strtolower(trim($parts[0]))] = trim($parts[1]);
+ }
+ }
+
+ $response = array(
+ 'body' => $response,
+ 'statusCode' => $curlInfo['http_code'],
+ 'headers' => $headers
+ );
+
+ if ($curlErrNo) {
+ throw new Sabre_DAV_Exception('[CURL] Error while making request: ' . $curlError . ' (error code: ' . $curlErrNo . ')');
+ }
+
+ if ($response['statusCode']>=400) {
+ switch ($response['statusCode']) {
+ case 404:
+ throw new Sabre_DAV_Exception_NotFound('Resource ' . $url . ' not found.');
+ break;
+
+ default:
+ throw new Sabre_DAV_Exception('HTTP error response. (errorcode ' . $response['statusCode'] . ')');
+ }
+ }
+
+ return $response;
+
+ }
}
\ No newline at end of file