Plugin Directory

Changeset 371860


Ignore:
Timestamp:
04/12/2011 10:24:19 AM (15 years ago)
Author:
ostlund
Message:

uUpdated dropbox class to latest version
-This line, and those below, will be ignored--
pbo

M trunk/inc/dropbox.php
M trunk/readme.txt

Location:
dropbox-upload-form/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • dropbox-upload-form/trunk/inc/dropbox.php

    r314433 r371860  
    99 * If you report a bug, make sure you give me enough information (include your code).
    1010 *
     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 *
    1117 * Changelog since 1.0.0
    1218 * - fixed some issues with generation off the basestring
    1319 *
    1420 * License
    15  * Copyright (c) 2010, Tijs Verkoyen. All rights reserved.
     21 * Copyright (c), Tijs Verkoyen. All rights reserved.
    1622 *
    1723 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     
    2430 *
    2531 * @author      Tijs Verkoyen <[email protected]>
    26  * @version     1.0.1
    27  *
    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.
    2935 * @license     BSD License
    3036 */
     
    4248
    4349    // current version
    44     const VERSION = '1.0.1';
     50    const VERSION = '1.0.3';
    4551
    4652
     
    207213        }
    208214
    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
    210228        $base = $method .'&';
    211         $base .= urlencode($url);
     229        $base .= implode('%2F', $urlChunks);
    212230        $base .= (substr_count($url, '?')) ? '%26' : '&';
    213231        $base .= implode('%26', $chunks);
    214         $base = str_replace('%3F', '&', $base);
     232        $base = str_replace(array('%3F', '%20'), array('&', '%2520'), $base);
    215233
    216234        // return
     
    258276     * @param   array[optional] $parameters
    259277     */
    260     private function doOAuthCall($url, array $parameters = null)
     278    private function doOAuthCall($url, array $parameters = null, $method = 'POST', $expectJSON = true)
    261279    {
    262280        // redefine
     
    278296        // calculate header
    279297        $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        }
    280314
    281315        // set options
     
    289323        $options[CURLOPT_SSL_VERIFYHOST] = false;
    290324        $options[CURLOPT_HTTPHEADER] = array('Expect:');
    291         $options[CURLOPT_POST] = true;
    292         $options[CURLOPT_POSTFIELDS] = $this->buildQuery($parameters);
    293325
    294326        // init
     
    310342
    311343        // return
    312         return json_decode($response, true);
     344        if($expectJSON) return json_decode($response, true);
     345
     346        // fallback
     347        return $response;
    313348    }
    314349
     
    349384        // set data
    350385        $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        }
    352394
    353395        if($filePath != null)
     
    402444        else
    403445        {
     446            // reset post
     447            $options[CURLOPT_POST] = 0;
     448            unset($options[CURLOPT_POSTFIELDS]);
     449
    404450            // add the parameters into the querystring
    405451            if(!empty($parameters)) $url .= '?'. $this->buildQuery($parameters);
     
    688734            $replace = array('%20', '%20', '~', '%25');
    689735
    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;
    692833    }
    693834
     
    776917        $url = '0/files/';
    777918        $url .= ($sandbox) ? 'sandbox/' : 'dropbox/';
    778         $url .= (string) $path;
     919        $url .= trim((string) $path, '/');
    779920
    780921        // make the call
     
    796937        $url = '0/files/';
    797938        $url .= ($sandbox) ? 'sandbox/' : 'dropbox/';
    798         $url .= (string) $path;
     939        $url .= trim((string) $path, '/');
    799940
    800941        // make the call
     
    819960        $url = '0/metadata/';
    820961        $url .= ($sandbox) ? 'sandbox/' : 'dropbox/';
    821         $url .= (string) $path;
     962        $url .= trim((string) $path, '/');
    822963
    823964        // build parameters
     
    843984        // build url
    844985        $url = '0/thumbnails/dropbox/';
    845         $url .= (string) $path;
     986        $url .= trim((string) $path, '/');
    846987
    847988        // build parameters
     
    8901031
    8911032        // build parameters
    892         $parameters['path'] = (string) $path;
     1033        $parameters['path'] = trim((string) $path, '/');
    8931034        $parameters['root'] = ($sandbox) ? 'sandbox' : 'dropbox';
    8941035
     
    9111052
    9121053        // build parameters
    913         $parameters['path'] = (string) $path;
     1054        $parameters['path'] = trim((string) $path, '/');
    9141055        $parameters['root'] = ($sandbox) ? 'sandbox' : 'dropbox';
    9151056
  • dropbox-upload-form/trunk/readme.txt

    r303875 r371860  
    3737
    3838= 0.1.2 =
    39 *
     39* Updated dropbox class to latest version
    4040
    4141= 0.1.1 =
Note: See TracChangeset for help on using the changeset viewer.