1
0

Helper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Miguel Prokop <miguel.prokop@vtu.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program 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 License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Share;
  27. class Helper extends \OC\Share\Constants {
  28. /**
  29. * get default expire settings defined by the admin
  30. * @return array contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
  31. */
  32. public static function getDefaultExpireSetting() {
  33. $config = \OC::$server->getConfig();
  34. $defaultExpireSettings = ['defaultExpireDateSet' => false];
  35. // get default expire settings
  36. $defaultExpireDate = $config->getAppValue('core', 'shareapi_default_expire_date', 'no');
  37. if ($defaultExpireDate === 'yes') {
  38. $enforceExpireDate = $config->getAppValue('core', 'shareapi_enforce_expire_date', 'no');
  39. $defaultExpireSettings['defaultExpireDateSet'] = true;
  40. $defaultExpireSettings['expireAfterDays'] = (int)$config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
  41. $defaultExpireSettings['enforceExpireDate'] = $enforceExpireDate === 'yes';
  42. }
  43. return $defaultExpireSettings;
  44. }
  45. public static function calcExpireDate() {
  46. $expireAfter = \OC\Share\Share::getExpireInterval() * 24 * 60 * 60;
  47. $expireAt = time() + $expireAfter;
  48. $date = new \DateTime();
  49. $date->setTimestamp($expireAt);
  50. $date->setTime(0, 0, 0);
  51. //$dateString = $date->format('Y-m-d') . ' 00:00:00';
  52. return $date;
  53. }
  54. /**
  55. * calculate expire date
  56. * @param array $defaultExpireSettings contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
  57. * @param int $creationTime timestamp when the share was created
  58. * @param int $userExpireDate expire timestamp set by the user
  59. * @return mixed integer timestamp or False
  60. */
  61. public static function calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate = null) {
  62. $expires = false;
  63. $defaultExpires = null;
  64. if (!empty($defaultExpireSettings['defaultExpireDateSet'])) {
  65. $defaultExpires = $creationTime + $defaultExpireSettings['expireAfterDays'] * 86400;
  66. }
  67. if (isset($userExpireDate)) {
  68. // if the admin decided to enforce the default expire date then we only take
  69. // the user defined expire date of it is before the default expire date
  70. if ($defaultExpires && !empty($defaultExpireSettings['enforceExpireDate'])) {
  71. $expires = min($userExpireDate, $defaultExpires);
  72. } else {
  73. $expires = $userExpireDate;
  74. }
  75. } elseif ($defaultExpires && !empty($defaultExpireSettings['enforceExpireDate'])) {
  76. $expires = $defaultExpires;
  77. }
  78. return $expires;
  79. }
  80. /**
  81. * Strips away a potential file names and trailing slashes:
  82. * - http://localhost
  83. * - http://localhost/
  84. * - http://localhost/index.php
  85. * - http://localhost/index.php/s/{shareToken}
  86. *
  87. * all return: http://localhost
  88. *
  89. * @param string $remote
  90. * @return string
  91. */
  92. protected static function fixRemoteURL($remote) {
  93. $remote = str_replace('\\', '/', $remote);
  94. if ($fileNamePosition = strpos($remote, '/index.php')) {
  95. $remote = substr($remote, 0, $fileNamePosition);
  96. }
  97. $remote = rtrim($remote, '/');
  98. return $remote;
  99. }
  100. /**
  101. * check if two federated cloud IDs refer to the same user
  102. *
  103. * @param string $user1
  104. * @param string $server1
  105. * @param string $user2
  106. * @param string $server2
  107. * @return bool true if both users and servers are the same
  108. */
  109. public static function isSameUserOnSameServer($user1, $server1, $user2, $server2) {
  110. $normalizedServer1 = strtolower(\OC\Share\Share::removeProtocolFromUrl($server1));
  111. $normalizedServer2 = strtolower(\OC\Share\Share::removeProtocolFromUrl($server2));
  112. if (rtrim($normalizedServer1, '/') === rtrim($normalizedServer2, '/')) {
  113. // FIXME this should be a method in the user management instead
  114. \OCP\Util::emitHook(
  115. '\OCA\Files_Sharing\API\Server2Server',
  116. 'preLoginNameUsedAsUserName',
  117. ['uid' => &$user1]
  118. );
  119. \OCP\Util::emitHook(
  120. '\OCA\Files_Sharing\API\Server2Server',
  121. 'preLoginNameUsedAsUserName',
  122. ['uid' => &$user2]
  123. );
  124. if ($user1 === $user2) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. }