Dropbox.php.dist 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. $oauth = new Dropbox_OAuth_PHP('XXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXX');
  3. $cache = __DIR__.'/dropbox.cache';
  4. $checkOAuth = function ($oauth) {
  5. $dropbox = new \Dropbox_API($oauth);
  6. $result = $dropbox->getAccountInfo();
  7. return isset($result['uid']);
  8. };
  9. $createTokens = true;
  10. if (file_exists($cache)) {
  11. $tokens = unserialize(file_get_contents($cache));
  12. $oauth->setToken($tokens);
  13. if ($checkOAuth($oauth)) {
  14. $createTokens = false;
  15. } else {
  16. fwrite(STDOUT, 'Need to recreate tokens.'.PHP_EOL);
  17. }
  18. }
  19. if ($createTokens) {
  20. $tokens = $oauth->getRequestToken();
  21. fwrite(STDOUT, 'Visit the following URL and accept the application:'.PHP_EOL);
  22. fwrite(STDOUT, $oauth->getAuthorizeUrl().PHP_EOL);
  23. fwrite(STDOUT, 'Once you\'re done, press a key.'.PHP_EOL);
  24. fwrite(STDOUT, 'Visit the following url in a browser, login if necessary, and click "Allow":'.PHP_EOL);
  25. fwrite(STDOUT, $oauth->getAuthorizeUrl().PHP_EOL.PHP_EOL);
  26. fwrite(STDOUT, 'Press enter when this is done...');
  27. fgets(STDIN);
  28. $tokens = $oauth->getAccessToken();
  29. $oauth->setToken($tokens);
  30. if ($checkOAuth($oauth)) {
  31. file_put_contents($cache, serialize($tokens));
  32. fwrite(STDOUT, 'OAuth token ready!'.PHP_EOL);
  33. } else {
  34. fwrite(STDOUT, 'Error with token.'.PHP_EOL);
  35. }
  36. file_put_contents($cache, serialize($tokens));
  37. }
  38. $dropbox = new Dropbox_API($oauth, Dropbox_API::ROOT_SANDBOX);
  39. $directory = $dropbox->getMetaData('/');
  40. foreach ($directory['contents'] as $file) {
  41. $dropbox->delete($file['path']);
  42. }
  43. return new Gaufrette\Adapter\Dropbox($dropbox);