--- /dev/null
+<?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 $curlSettings;\r
+ \r
+ public function __construct(array $settings) {\r
+ //set default curl settings\r
+ $this->curlSettings = array(\r
+ CURLOPT_RETURNTRANSFER => true,\r
+ // Return headers as part of the response\r
+ CURLOPT_HEADER => true,\r
+ // Automatically follow redirects\r
+ CURLOPT_FOLLOWLOCATION => true,\r
+ CURLOPT_MAXREDIRS => 5,\r
+ CURLOPT_SSL_VERIFYPEER => true,\r
+ //CURLOPT_SSL_VERIFYPEER => false,\r
+ );\r
+ parent::__construct($settings);\r
+ }\r
+ \r
+ public function setCurlSettings($settings) {\r
+ if (is_array($settings)) {\r
+ foreach ($settings as $k => $v) {\r
+ $this->curlSettings[$k] = $v;\r
+ }\r
+ }\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
+ $this->curlSettings[CURLOPT_POSTFIELDS] = $body; \r
+ $url = $this->getAbsoluteUrl($url);\r
+ \r
+ switch ($method) {\r
+ case 'PUT':\r
+ $this->curlSettings[CURLOPT_PUT] = true;\r
+ break;\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
+ $this->curlSettings[CURLOPT_NOBODY] = true;\r
+ $this->curlSettings[CURLOPT_CUSTOMREQUEST] = 'HEAD';\r
+ break;\r
+ \r
+ default:\r
+ $this->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
+ $this->curlSettings[CURLOPT_HTTPHEADER] = $nHeaders;\r
+ \r
+ if ($this->proxy) {\r
+ $this->curlSettings[CURLOPT_PROXY] = $this->proxy;\r
+ }\r
+ \r
+ if ($this->userName) {\r
+ $this->curlSettings[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC | CURLAUTH_DIGEST;\r
+ $this->curlSettings[CURLOPT_USERPWD] = $this->userName . ':' . $this->password;\r
+ }\r
+ \r
+ list(\r
+ $response,\r
+ $curlInfo,\r
+ $curlErrNo,\r
+ $curlError\r
+ ) = $this->curlRequest($url, $this->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
+ \r
+}
\ No newline at end of file