Changeset 371860
- Timestamp:
- 04/12/2011 10:24:19 AM (15 years ago)
- Location:
- dropbox-upload-form/trunk
- Files:
-
- 2 edited
-
inc/dropbox.php (modified) (17 diffs)
-
readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
dropbox-upload-form/trunk/inc/dropbox.php
r314433 r371860 9 9 * If you report a bug, make sure you give me enough information (include your code). 10 10 * 11 * Changelog since 1.0.2 12 * - Added methods to enable oauth-usage. 13 * 14 * Changelog since 1.0.1 15 * - Bugfix: when doing multiple calles where GET and POST is mixed, the postfields should be reset (thx to Daniel Hüsken) 16 * 11 17 * Changelog since 1.0.0 12 18 * - fixed some issues with generation off the basestring 13 19 * 14 20 * License 15 * Copyright (c) 2010, Tijs Verkoyen. All rights reserved.21 * Copyright (c), Tijs Verkoyen. All rights reserved. 16 22 * 17 23 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: … … 24 30 * 25 31 * @author Tijs Verkoyen <[email protected]> 26 * @version 1.0. 127 * 28 * @copyright Copyright (c) 2010, Tijs Verkoyen. All rights reserved.32 * @version 1.0.3 33 * 34 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved. 29 35 * @license BSD License 30 36 */ … … 42 48 43 49 // current version 44 const VERSION = '1.0. 1';50 const VERSION = '1.0.3'; 45 51 46 52 … … 207 213 } 208 214 209 // buils base 215 $urlChunks = explode('/', $url); 216 $i = 0; 217 218 foreach($urlChunks as &$chunk) 219 { 220 if($i > 4) $chunk = self::urlencode_rfc3986($chunk); 221 else $chunk = urlencode($chunk); 222 223 $i++; 224 225 } 226 227 // build base 210 228 $base = $method .'&'; 211 $base .= urlencode($url);229 $base .= implode('%2F', $urlChunks); 212 230 $base .= (substr_count($url, '?')) ? '%26' : '&'; 213 231 $base .= implode('%26', $chunks); 214 $base = str_replace( '%3F', '&', $base);232 $base = str_replace(array('%3F', '%20'), array('&', '%2520'), $base); 215 233 216 234 // return … … 258 276 * @param array[optional] $parameters 259 277 */ 260 private function doOAuthCall($url, array $parameters = null )278 private function doOAuthCall($url, array $parameters = null, $method = 'POST', $expectJSON = true) 261 279 { 262 280 // redefine … … 278 296 // calculate header 279 297 $header = $this->calculateHeader($parameters, self::API_URL .'/'. $url); 298 299 if($method == 'POST') 300 { 301 $options[CURLOPT_POST] = true; 302 $options[CURLOPT_POSTFIELDS] = $this->buildQuery($parameters); 303 } 304 305 else 306 { 307 // reset post 308 $options[CURLOPT_POST] = 0; 309 unset($options[CURLOPT_POSTFIELDS]); 310 311 // add the parameters into the querystring 312 if(!empty($parameters)) $url .= '?'. $this->buildQuery($parameters); 313 } 280 314 281 315 // set options … … 289 323 $options[CURLOPT_SSL_VERIFYHOST] = false; 290 324 $options[CURLOPT_HTTPHEADER] = array('Expect:'); 291 $options[CURLOPT_POST] = true;292 $options[CURLOPT_POSTFIELDS] = $this->buildQuery($parameters);293 325 294 326 // init … … 310 342 311 343 // return 312 return json_decode($response, true); 344 if($expectJSON) return json_decode($response, true); 345 346 // fallback 347 return $response; 313 348 } 314 349 … … 349 384 // set data 350 385 $data = $oauth; 351 if(!empty($parameters)) $data = array_merge($data, $parameters); 386 if(!empty($parameters)) 387 { 388 // convert to UTF-8 389 foreach($parameters as &$value) $value = utf8_encode($value); 390 391 // merge 392 $data = array_merge($data, $parameters); 393 } 352 394 353 395 if($filePath != null) … … 402 444 else 403 445 { 446 // reset post 447 $options[CURLOPT_POST] = 0; 448 unset($options[CURLOPT_POSTFIELDS]); 449 404 450 // add the parameters into the querystring 405 451 if(!empty($parameters)) $url .= '?'. $this->buildQuery($parameters); … … 688 734 $replace = array('%20', '%20', '~', '%25'); 689 735 690 return str_replace($search, $replace, urlencode($value)); 691 } 736 return str_replace($search, $replace, rawurlencode($value)); 737 } 738 } 739 740 741 // oauth resources 742 /** 743 * Call for obtaining an OAuth request token. 744 * Returns a request token and the corresponding request token secret. This token and secret cannot be used to sign requests for the /metadata and /file content API calls. 745 * Their only purpose is for signing a request to oauth/access_token once the user has gone through the application authorization steps provided by oauth/authorize. 746 * 747 * @return array 748 */ 749 public function oAuthRequestToken() 750 { 751 // make the call 752 $response = $this->doOAuthCall('0/oauth/request_token', null, 'POST', false); 753 754 // process response 755 $response = (array) explode('&', $response); 756 $return = array(); 757 758 // loop chunks 759 foreach($response as $chunk) 760 { 761 // split again 762 $chunks = explode('=', $chunk, 2); 763 764 // store return 765 if(count($chunks) == 2) $return[$chunks[0]] = $chunks[1]; 766 } 767 768 // return 769 return $return; 770 } 771 772 773 /** 774 * Redirect the user to the oauth/authorize location so that Dropbox can authenticate the user and ask whether or not the user wants to authorize the application to access 775 * file metadata and content on its behalf. oauth/authorize is not an API call per se, because it does not have a return value, but rather directs the user to a page on 776 * api.dropbox.com where they are provided means to log in to Dropbox and grant authority to the application requesting it. 777 * The page served by oauth/authorize should be presented to the user through their web browser. 778 * Please note, without directing the user to a Dropbox-provided page via oauth/authorize, it is impossible for your application to use the request token it received 779 * via oauth/request_token to obtain an access token from oauth/access_token. 780 * 781 * @return void 782 * @param string $oauthToken The request token of the application requesting authority from a user. 783 * @param string[optional] $oauthCallback After the user authorizes an application, the user is redirected to the application-served URL provided by this parameter. 784 */ 785 public function oAuthAuthorize($oauthToken, $oauthCallback = null) 786 { 787 // build parameters 788 $parameters = array(); 789 $parameters['oauth_token'] = (string) $oauthToken; 790 if($oauthCallback !== null) $parameters['oauth_callback'] = (string) $oauthCallback; 791 792 // build url 793 $url = self::API_URL .'/0/oauth/authorize?'. http_build_query($parameters); 794 795 // redirect 796 header('Location: '. $url); 797 exit; 798 } 799 800 801 /** 802 * This call returns a access token and the corresponding access token secret. 803 * Upon return, the authorization process is now complete and the access token and corresponding secret are used to sign requests for the metadata and file content API calls. 804 * 805 * @return array 806 * @param string $oauthToken The token returned after authorizing 807 */ 808 public function oAuthAccessToken($oauthToken) 809 { 810 // build parameters 811 $parameters = array(); 812 $parameters['oauth_token'] = (string) $oauthToken; 813 814 // make the call 815 $response = $this->doOAuthCall('0/oauth/access_token', $parameters, 'POST', false); 816 817 // process response 818 $response = (array) explode('&', $response); 819 $return = array(); 820 821 // loop chunks 822 foreach($response as $chunk) 823 { 824 // split again 825 $chunks = explode('=', $chunk, 2); 826 827 // store return 828 if(count($chunks) == 2) $return[$chunks[0]] = $chunks[1]; 829 } 830 831 // return 832 return $return; 692 833 } 693 834 … … 776 917 $url = '0/files/'; 777 918 $url .= ($sandbox) ? 'sandbox/' : 'dropbox/'; 778 $url .= (string) $path;919 $url .= trim((string) $path, '/'); 779 920 780 921 // make the call … … 796 937 $url = '0/files/'; 797 938 $url .= ($sandbox) ? 'sandbox/' : 'dropbox/'; 798 $url .= (string) $path;939 $url .= trim((string) $path, '/'); 799 940 800 941 // make the call … … 819 960 $url = '0/metadata/'; 820 961 $url .= ($sandbox) ? 'sandbox/' : 'dropbox/'; 821 $url .= (string) $path;962 $url .= trim((string) $path, '/'); 822 963 823 964 // build parameters … … 843 984 // build url 844 985 $url = '0/thumbnails/dropbox/'; 845 $url .= (string) $path;986 $url .= trim((string) $path, '/'); 846 987 847 988 // build parameters … … 890 1031 891 1032 // build parameters 892 $parameters['path'] = (string) $path;1033 $parameters['path'] = trim((string) $path, '/'); 893 1034 $parameters['root'] = ($sandbox) ? 'sandbox' : 'dropbox'; 894 1035 … … 911 1052 912 1053 // build parameters 913 $parameters['path'] = (string) $path;1054 $parameters['path'] = trim((string) $path, '/'); 914 1055 $parameters['root'] = ($sandbox) ? 'sandbox' : 'dropbox'; 915 1056 -
dropbox-upload-form/trunk/readme.txt
r303875 r371860 37 37 38 38 = 0.1.2 = 39 * 39 * Updated dropbox class to latest version 40 40 41 41 = 0.1.1 =
Note: See TracChangeset
for help on using the changeset viewer.