apps/files_external/3rdparty/Dropbox/OAuth/Curl.php
/**
     * Fetches a secured oauth url and returns the response body.
     *
     * @param string $uri
     * @param mixed $arguments
     * @param string $method
     * @param array $httpHeaders
     * @return string
     */
Fetches a secured oauth url and returns the response body.
Fetches a secured oauth url and returns the response body .
public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) {
		$uri=str_replace('http://', 'https://', $uri); 
		if (is_string($arguments) and strtoupper($method) == 'POST') {
			preg_match("/\?file=(.*)$/i", $uri, $matches);
			if (isset($matches[1])) {
				$uri = str_replace($matches[0], "", $uri);
				$filename =  rawurldecode(str_replace('%7E', '~', $matches[1]));
				$httpHeaders=array_merge($httpHeaders,$this->getOAuthHeader($uri, array("file" => $filename), $method));
			}
		} else {
			$httpHeaders=array_merge($httpHeaders,$this->getOAuthHeader($uri, $arguments, $method));
		}
		$ch = curl_init();
		if (strtoupper($method) == 'POST') {
			curl_setopt($ch, CURLOPT_URL, $uri);
			curl_setopt($ch, CURLOPT_POST, true);
			if (is_array($arguments)) {
				$arguments=http_build_query($arguments);
			}
			curl_setopt($ch, CURLOPT_POSTFIELDS, $arguments);
			$httpHeaders['Content-Length']=strlen($arguments);
		} else if (strtoupper($method) == 'PUT' && $this->inFile) {
			curl_setopt($ch, CURLOPT_URL, $uri.'?'.http_build_query($arguments));
			curl_setopt($ch, CURLOPT_PUT, true);
			curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
			curl_setopt($ch, CURLOPT_INFILE, $this->inFile);
			curl_setopt($ch, CURLOPT_INFILESIZE, $this->inFileSize);
			fseek($this->inFile, 0);
			$this->inFileSize = $this->inFile = null;
		} else {
			curl_setopt($ch, CURLOPT_URL, $uri.'?'.http_build_query($arguments));
			curl_setopt($ch, CURLOPT_POST, false);
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 600);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
		curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ca-bundle.pem');
		$headers = array();
		foreach ($httpHeaders as $name => $value) {
			$headers[] = "{$name}: $value";
		}
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		if (!ini_get('safe_mode') && !ini_get('open_basedir'))
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
		if (function_exists($this->ProgressFunction) and defined('CURLOPT_PROGRESSFUNCTION')) {
			curl_setopt($ch, CURLOPT_NOPROGRESS, false);
			curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, $this->ProgressFunction);
			curl_setopt($ch, CURLOPT_BUFFERSIZE, 512);
		}
		$response=curl_exec($ch);
		$errorno=curl_errno($ch);
		$error=curl_error($ch);
		$status=curl_getinfo($ch,CURLINFO_HTTP_CODE);
		curl_close($ch);
		$this->lastResponse = array(
				'httpStatus' => $status,
				'body' => $response
		);
		if (!empty($errorno))
			throw new Dropbox_Exception_NotFound('Curl error: ('.$errorno.') '.$error."\n");
		if ($status>=300) {
			$body = array();
			$body = json_decode($response, true);
			if (!is_array($body)) {
				$body = array();
			}
			$jsonErr = isset($body['error'])? $body['error'] : '';
			switch ($status) {
				case 304 :
					return array(
						'httpStatus' => 304,
						'body' => null,
					);
					break;
				case 400 :
					throw new Dropbox_Exception_Forbidden('Forbidden. Bad input parameter. Error message should indicate which one and why.');
				case 401 :
					throw new Dropbox_Exception_Forbidden('Forbidden. Bad or expired token. This can happen if the user or Dropbox revoked or expired an access token. To fix, you should re-authenticate the user.');
				case 403 :
					throw new Dropbox_Exception_Forbidden('Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location.');
				case 404 :
					throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found');
				case 405 :
					throw new Dropbox_Exception_Forbidden('Forbidden. Request method not expected (generally should be GET or POST).');
				case 500 :
					throw new Dropbox_Exception_Forbidden('Server error. ' . $jsonErr);
				case 503 :
					throw new Dropbox_Exception_Forbidden('Forbidden. Your app is making too many requests and is being rate limited. 503s can trigger on a per-app or per-user basis.');
				case 507 :
					throw new Dropbox_Exception_OverQuota('This dropbox is full');
				default:
					throw new Dropbox_Exception_RequestToken('Error: ('.$status.') ' . $jsonErr);
			}
			if (!empty($body["error"]))
				throw new Dropbox_Exception_RequestToken('Error: ('.$status.') ' . $jsonErr);
		}
		return array(
			'body' => $response,
			'httpStatus' => $status
		);
    }
public function fetch ( $ uri , $ arguments = array ( ) , $ method = 'GET' , $ httpHeaders = array ( ) ) { $ uri = str_replace ( 'http://' , 'https://' , $ uri ) ; if ( is_string ( $ arguments ) and strtoupper ( $ method ) == 'POST' ) { preg_match ( " /\?file=(.*)$/i " , $ uri , $ matches ) ; if ( isset ( $ matches [ 1 ] ) ) { $ uri = str_replace ( $ matches [ 0 ] , " " , $ uri ) ; $ filename = rawurldecode ( str_replace ( '%7E' , '~' , $ matches [ 1 ] ) ) ; $ httpHeaders = array_merge ( $ httpHeaders , $ this -> getOAuthHeader ( $ uri , array ( " file " => $ filename ) , $ method ) ) ; } } else { $ httpHeaders = array_merge ( $ httpHeaders , $ this -> getOAuthHeader ( $ uri , $ arguments , $ method ) ) ; } $ ch = curl_init ( ) ; if ( strtoupper ( $ method ) == 'POST' ) { curl_setopt ( $ ch , CURLOPT_URL , $ uri ) ; curl_setopt ( $ ch , CURLOPT_POST , true ) ; if ( is_array ( $ arguments ) ) { $ arguments = http_build_query ( $ arguments ) ; } curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ arguments ) ; $ httpHeaders [ 'Content-Length' ] = strlen ( $ arguments ) ; } else if ( strtoupper ( $ method ) == 'PUT' && $ this -> inFile ) { curl_setopt ( $ ch , CURLOPT_URL , $ uri . '?' . http_build_query ( $ arguments ) ) ; curl_setopt ( $ ch , CURLOPT_PUT , true ) ; curl_setopt ( $ ch , CURLOPT_BINARYTRANSFER , true ) ; curl_setopt ( $ ch , CURLOPT_INFILE , $ this -> inFile ) ; curl_setopt ( $ ch , CURLOPT_INFILESIZE , $ this -> inFileSize ) ; fseek ( $ this -> inFile , 0 ) ; $ this -> inFileSize = $ this -> inFile = null ; } else { curl_setopt ( $ ch , CURLOPT_URL , $ uri . '?' . http_build_query ( $ arguments ) ) ; curl_setopt ( $ ch , CURLOPT_POST , false ) ; } curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true ) ; curl_setopt ( $ ch , CURLOPT_TIMEOUT , 600 ) ; curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , true ) ; curl_setopt ( $ ch , CURLOPT_SSL_VERIFYHOST , 2 ) ; curl_setopt ( $ ch , CURLOPT_FRESH_CONNECT , true ) ; curl_setopt ( $ ch , CURLOPT_CAINFO , dirname ( __FILE__ ) . DIRECTORY_SEPARATOR . 'ca-bundle.pem' ) ; $ headers = array ( ) ; foreach ( $ httpHeaders as $ name => $ value ) { $ headers [ ] = " { $ name } :  $ value " ; } curl_setopt ( $ ch , CURLOPT_HTTPHEADER , $ headers ) ; if ( ! ini_get ( 'safe_mode' ) && ! ini_get ( 'open_basedir' ) ) curl_setopt ( $ ch , CURLOPT_FOLLOWLOCATION , true ) ; if ( function_exists ( $ this -> ProgressFunction ) and defined ( 'CURLOPT_PROGRESSFUNCTION' ) ) { curl_setopt ( $ ch , CURLOPT_NOPROGRESS , false ) ; curl_setopt ( $ ch , CURLOPT_PROGRESSFUNCTION , $ this -> ProgressFunction ) ; curl_setopt ( $ ch , CURLOPT_BUFFERSIZE , 512 ) ; } $ response = curl_exec ( $ ch ) ; $ errorno = curl_errno ( $ ch ) ; $ error = curl_error ( $ ch ) ; $ status = curl_getinfo ( $ ch , CURLINFO_HTTP_CODE ) ; curl_close ( $ ch ) ; $ this -> lastResponse = array ( 'httpStatus' => $ status , 'body' => $ response ) ; if ( ! empty ( $ errorno ) ) throw new Dropbox_Exception_NotFound ( 'Curl error: (' . $ errorno . ') ' . $ error . " \n " ) ; if ( $ status >= 300 ) { $ body = array ( ) ; $ body = json_decode ( $ response , true ) ; if ( ! is_array ( $ body ) ) { $ body = array ( ) ; } $ jsonErr = isset ( $ body [ 'error' ] ) ? $ body [ 'error' ] : '' ; switch ( $ status ) { case 304 : return array ( 'httpStatus' => 304 , 'body' => null , ) ; break ; case 400 : throw new Dropbox_Exception_Forbidden ( 'Forbidden. Bad input parameter. Error message should indicate which one and why.' ) ; case 401 : throw new Dropbox_Exception_Forbidden ( 'Forbidden. Bad or expired token. This can happen if the user or Dropbox revoked or expired an access token. To fix, you should re-authenticate the user.' ) ; case 403 : throw new Dropbox_Exception_Forbidden ( 'Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location.' ) ; case 404 : throw new Dropbox_Exception_NotFound ( 'Resource at uri: ' . $ uri . ' could not be found' ) ; case 405 : throw new Dropbox_Exception_Forbidden ( 'Forbidden. Request method not expected (generally should be GET or POST).' ) ; case 500 : throw new Dropbox_Exception_Forbidden ( 'Server error. ' . $ jsonErr ) ; case 503 : throw new Dropbox_Exception_Forbidden ( 'Forbidden. Your app is making too many requests and is being rate limited. 503s can trigger on a per-app or per-user basis.' ) ; case 507 : throw new Dropbox_Exception_OverQuota ( 'This dropbox is full' ) ; default : throw new Dropbox_Exception_RequestToken ( 'Error: (' . $ status . ') ' . $ jsonErr ) ; } if ( ! empty ( $ body [ " error " ] ) ) throw new Dropbox_Exception_RequestToken ( 'Error: (' . $ status . ') ' . $ jsonErr ) ; } return array ( 'body' => $ response , 'httpStatus' => $ status ) ; }
Fetches a secured oauth url and returns the response body.
Fetches a secured oauth url and returns the response body .
// all https, upload makes problems if not //Build header // Not modified