google.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @author Adam Williamson <awilliam@redhat.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Lukas Reschke <lukas@owncloud.com>
  7. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  8. * @author Robin Appelman <icewind@owncloud.com>
  9. * @author Volkan Gezer <volkangezer@gmail.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. set_include_path(get_include_path().PATH_SEPARATOR.
  28. \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src');
  29. require_once 'Google/Client.php';
  30. OCP\JSON::checkAppEnabled('files_external');
  31. OCP\JSON::checkLoggedIn();
  32. OCP\JSON::callCheck();
  33. $l = \OC::$server->getL10N('files_external');
  34. if (isset($_POST['client_id']) && isset($_POST['client_secret']) && isset($_POST['redirect'])) {
  35. $client = new Google_Client();
  36. $client->setClientId((string)$_POST['client_id']);
  37. $client->setClientSecret((string)$_POST['client_secret']);
  38. $client->setRedirectUri((string)$_POST['redirect']);
  39. $client->setScopes(array('https://www.googleapis.com/auth/drive'));
  40. $client->setAccessType('offline');
  41. if (isset($_POST['step'])) {
  42. $step = $_POST['step'];
  43. if ($step == 1) {
  44. try {
  45. $authUrl = $client->createAuthUrl();
  46. OCP\JSON::success(array('data' => array(
  47. 'url' => $authUrl
  48. )));
  49. } catch (Exception $exception) {
  50. OCP\JSON::error(array('data' => array(
  51. 'message' => $l->t('Step 1 failed. Exception: %s', array($exception->getMessage()))
  52. )));
  53. }
  54. } else if ($step == 2 && isset($_POST['code'])) {
  55. try {
  56. $token = $client->authenticate((string)$_POST['code']);
  57. OCP\JSON::success(array('data' => array(
  58. 'token' => $token
  59. )));
  60. } catch (Exception $exception) {
  61. OCP\JSON::error(array('data' => array(
  62. 'message' => $l->t('Step 2 failed. Exception: %s', array($exception->getMessage()))
  63. )));
  64. }
  65. }
  66. }
  67. }