compat.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * Original:
  6. * @author Frank Karlitschek
  7. * @copyright 2010 Frank Karlitschek karlitschek@kde.org
  8. *
  9. * Adapted:
  10. * @author Michiel de Jong, 2011
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  14. * License as published by the Free Software Foundation; either
  15. * version 3 of the License, or any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public
  23. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. // Do not load FS ...
  27. $RUNTIME_NOSETUPFS = true;
  28. require_once('../../lib/base.php');
  29. OC_Util::checkAppEnabled('remoteStorage');
  30. require_once('Sabre/autoload.php');
  31. require_once('lib_remoteStorage.php');
  32. require_once('oauth_ro_auth.php');
  33. ini_set('default_charset', 'UTF-8');
  34. #ini_set('error_reporting', '');
  35. @ob_clean();
  36. //allow use as remote storage for other websites
  37. if(isset($_SERVER['HTTP_ORIGIN'])) {
  38. header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
  39. header('Access-Control-Max-Age: 3600');
  40. header('Access-Control-Allow-Methods: OPTIONS, GET, PUT, DELETE, PROPFIND');
  41. header('Access-Control-Allow-Headers: Authorization, Content-Type');
  42. } else {
  43. header('Access-Control-Allow-Origin: *');
  44. }
  45. $path = substr($_SERVER["REQUEST_URI"], strlen($_SERVER["SCRIPT_NAME"]));
  46. $pathParts = explode('/', $path);
  47. // for webdav:
  48. // 0/ 1 / 2 / 3 / 4 / 5 / 6 / 7
  49. // /$ownCloudUser/remoteStorage/webdav/$userHost/$userName/$dataScope/$key
  50. // for oauth:
  51. // 0/ 1 / 2 / 3 / 4
  52. // /$ownCloudUser/remoteStorage/oauth/auth
  53. if(count($pathParts) >= 8 && $pathParts[0] == '' && $pathParts[2] == 'remoteStorage' && $pathParts[3] == 'webdav') {
  54. list($dummy0, $ownCloudUser, $dummy2, $dummy3, $userHost, $userName, $dataScope) = $pathParts;
  55. OC_Util::setupFS($ownCloudUser);
  56. // Create ownCloud Dir
  57. $publicDir = new OC_Connector_Sabre_Directory('');
  58. $server = new Sabre_DAV_Server($publicDir);
  59. // Path to our script
  60. $server->setBaseUri(OC::$WEBROOT."/apps/remoteStorage/compat.php/$ownCloudUser");
  61. // Auth backend
  62. $authBackend = new OC_Connector_Sabre_Auth_ro_oauth(OC_remoteStorage::getValidTokens($ownCloudUser, $userName.'@'.$userHost, $dataScope));
  63. $authPlugin = new Sabre_DAV_Auth_Plugin($authBackend,'ownCloud');//should use $validTokens here
  64. $server->addPlugin($authPlugin);
  65. // Also make sure there is a 'data' directory, writable by the server. This directory is used to store information about locks
  66. $lockBackend = new OC_Connector_Sabre_Locks();
  67. $lockPlugin = new Sabre_DAV_Locks_Plugin($lockBackend);
  68. $server->addPlugin($lockPlugin);
  69. // And off we go!
  70. $server->exec();
  71. } else if(count($pathParts) >= 4 && $pathParts[0] == '' && $pathParts[2] == 'remoteStorage' && $pathParts[3] == 'oauth2' && $pathParts[4] = 'auth') {
  72. if(isset($_POST['allow'])) {
  73. //TODO: input checking. these explodes may fail to produces the desired arrays:
  74. $ownCloudUser = $pathParts[1];
  75. foreach($_GET as $k => $v) {
  76. if($k=='user_address'){
  77. $userAddress=$v;
  78. } else if($k=='redirect_uri'){
  79. $appUrl=$v;
  80. } else if($k=='scope'){
  81. $dataScope=$v;
  82. }
  83. }
  84. if(OC_User::getUser() == $ownCloudUser) {
  85. //TODO: check if this can be faked by editing the cookie in firebug!
  86. $token=OC_remoteStorage::createDataScope($appUrl, $userAddress, $dataScope);
  87. header('Location: '.$_GET['redirect_uri'].'#access_token='.$token.'&token_type=remoteStorage');
  88. } else {
  89. if((isset($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'])) {
  90. $url = "https://";
  91. } else {
  92. $url = "http://";
  93. }
  94. $url .= $_SERVER['SERVER_NAME'];
  95. $url .= substr($_SERVER['SCRIPT_NAME'], 0, -strlen('apps/remoteStorage/compat.php'));
  96. die('Please '
  97. .'<input type="submit" onclick="'
  98. ."window.open('$url','Close me!','height=600,width=300');"
  99. .'" value="log in">'
  100. .', close the pop-up, and '
  101. .'<form method="POST"><input name="allow" type="submit" value="Try again"></form>');
  102. }
  103. } else {
  104. echo '<form method="POST"><input name="allow" type="submit" value="Allow this web app to store stuff on your owncloud."></form>';
  105. }
  106. } else {
  107. die('not webdav and not oauth. dont know what to do '.var_export($pathParts, true));
  108. }