Helper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Share;
  8. class Helper extends \OC\Share\Constants {
  9. /**
  10. * get default expire settings defined by the admin
  11. * @return array contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
  12. */
  13. public static function getDefaultExpireSetting() {
  14. $config = \OC::$server->getConfig();
  15. $defaultExpireSettings = ['defaultExpireDateSet' => false];
  16. // get default expire settings
  17. $defaultExpireDate = $config->getAppValue('core', 'shareapi_default_expire_date', 'no');
  18. if ($defaultExpireDate === 'yes') {
  19. $enforceExpireDate = $config->getAppValue('core', 'shareapi_enforce_expire_date', 'no');
  20. $defaultExpireSettings['defaultExpireDateSet'] = true;
  21. $defaultExpireSettings['expireAfterDays'] = (int)$config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
  22. $defaultExpireSettings['enforceExpireDate'] = $enforceExpireDate === 'yes';
  23. }
  24. return $defaultExpireSettings;
  25. }
  26. public static function calcExpireDate() {
  27. $expireAfter = \OC\Share\Share::getExpireInterval() * 24 * 60 * 60;
  28. $expireAt = time() + $expireAfter;
  29. $date = new \DateTime();
  30. $date->setTimestamp($expireAt);
  31. $date->setTime(0, 0, 0);
  32. //$dateString = $date->format('Y-m-d') . ' 00:00:00';
  33. return $date;
  34. }
  35. /**
  36. * calculate expire date
  37. * @param array $defaultExpireSettings contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
  38. * @param int $creationTime timestamp when the share was created
  39. * @param int $userExpireDate expire timestamp set by the user
  40. * @return mixed integer timestamp or False
  41. */
  42. public static function calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate = null) {
  43. $expires = false;
  44. $defaultExpires = null;
  45. if (!empty($defaultExpireSettings['defaultExpireDateSet'])) {
  46. $defaultExpires = $creationTime + $defaultExpireSettings['expireAfterDays'] * 86400;
  47. }
  48. if (isset($userExpireDate)) {
  49. // if the admin decided to enforce the default expire date then we only take
  50. // the user defined expire date of it is before the default expire date
  51. if ($defaultExpires && !empty($defaultExpireSettings['enforceExpireDate'])) {
  52. $expires = min($userExpireDate, $defaultExpires);
  53. } else {
  54. $expires = $userExpireDate;
  55. }
  56. } elseif ($defaultExpires && !empty($defaultExpireSettings['enforceExpireDate'])) {
  57. $expires = $defaultExpires;
  58. }
  59. return $expires;
  60. }
  61. /**
  62. * Strips away a potential file names and trailing slashes:
  63. * - http://localhost
  64. * - http://localhost/
  65. * - http://localhost/index.php
  66. * - http://localhost/index.php/s/{shareToken}
  67. *
  68. * all return: http://localhost
  69. *
  70. * @param string $remote
  71. * @return string
  72. */
  73. protected static function fixRemoteURL($remote) {
  74. $remote = str_replace('\\', '/', $remote);
  75. if ($fileNamePosition = strpos($remote, '/index.php')) {
  76. $remote = substr($remote, 0, $fileNamePosition);
  77. }
  78. $remote = rtrim($remote, '/');
  79. return $remote;
  80. }
  81. /**
  82. * check if two federated cloud IDs refer to the same user
  83. *
  84. * @param string $user1
  85. * @param string $server1
  86. * @param string $user2
  87. * @param string $server2
  88. * @return bool true if both users and servers are the same
  89. */
  90. public static function isSameUserOnSameServer($user1, $server1, $user2, $server2) {
  91. $normalizedServer1 = strtolower(\OC\Share\Share::removeProtocolFromUrl($server1));
  92. $normalizedServer2 = strtolower(\OC\Share\Share::removeProtocolFromUrl($server2));
  93. if (rtrim($normalizedServer1, '/') === rtrim($normalizedServer2, '/')) {
  94. // FIXME this should be a method in the user management instead
  95. \OCP\Util::emitHook(
  96. '\OCA\Files_Sharing\API\Server2Server',
  97. 'preLoginNameUsedAsUserName',
  98. ['uid' => &$user1]
  99. );
  100. \OCP\Util::emitHook(
  101. '\OCA\Files_Sharing\API\Server2Server',
  102. 'preLoginNameUsedAsUserName',
  103. ['uid' => &$user2]
  104. );
  105. if ($user1 === $user2) {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. }