JSConfigHelper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Template;
  8. use bantu\IniGetWrapper\IniGetWrapper;
  9. use OC\Authentication\Token\IProvider;
  10. use OC\CapabilitiesManager;
  11. use OC\Files\FilenameValidator;
  12. use OC\Share\Share;
  13. use OCA\Provisioning_API\Controller\AUserData;
  14. use OCP\App\AppPathNotFoundException;
  15. use OCP\App\IAppManager;
  16. use OCP\Authentication\Exceptions\ExpiredTokenException;
  17. use OCP\Authentication\Exceptions\InvalidTokenException;
  18. use OCP\Authentication\Exceptions\WipeTokenException;
  19. use OCP\Authentication\Token\IToken;
  20. use OCP\Constants;
  21. use OCP\Defaults;
  22. use OCP\Files\FileInfo;
  23. use OCP\IConfig;
  24. use OCP\IGroupManager;
  25. use OCP\IInitialStateService;
  26. use OCP\IL10N;
  27. use OCP\ILogger;
  28. use OCP\ISession;
  29. use OCP\IURLGenerator;
  30. use OCP\IUser;
  31. use OCP\Session\Exceptions\SessionNotAvailableException;
  32. use OCP\Share\IManager as IShareManager;
  33. use OCP\User\Backend\IPasswordConfirmationBackend;
  34. use OCP\Util;
  35. class JSConfigHelper {
  36. /** @var array user back-ends excluded from password verification */
  37. private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
  38. public function __construct(
  39. protected IL10N $l,
  40. protected Defaults $defaults,
  41. protected IAppManager $appManager,
  42. protected ISession $session,
  43. protected ?IUser $currentUser,
  44. protected IConfig $config,
  45. protected IGroupManager $groupManager,
  46. protected IniGetWrapper $iniWrapper,
  47. protected IURLGenerator $urlGenerator,
  48. protected CapabilitiesManager $capabilitiesManager,
  49. protected IInitialStateService $initialStateService,
  50. protected IProvider $tokenProvider,
  51. protected FilenameValidator $filenameValidator,
  52. ) {
  53. }
  54. public function getConfig(): string {
  55. $userBackendAllowsPasswordConfirmation = true;
  56. if ($this->currentUser !== null) {
  57. $uid = $this->currentUser->getUID();
  58. $backend = $this->currentUser->getBackend();
  59. if ($backend instanceof IPasswordConfirmationBackend) {
  60. $userBackendAllowsPasswordConfirmation = $backend->canConfirmPassword($uid);
  61. } elseif (isset($this->excludedUserBackEnds[$this->currentUser->getBackendClassName()])) {
  62. $userBackendAllowsPasswordConfirmation = false;
  63. }
  64. } else {
  65. $uid = null;
  66. }
  67. // Get the config
  68. $apps_paths = [];
  69. if ($this->currentUser === null) {
  70. $apps = $this->appManager->getInstalledApps();
  71. } else {
  72. $apps = $this->appManager->getEnabledAppsForUser($this->currentUser);
  73. }
  74. foreach ($apps as $app) {
  75. try {
  76. $apps_paths[$app] = $this->appManager->getAppWebPath($app);
  77. } catch (AppPathNotFoundException $e) {
  78. $apps_paths[$app] = false;
  79. }
  80. }
  81. $enableLinkPasswordByDefault = $this->config->getAppValue('core', 'shareapi_enable_link_password_by_default', 'no');
  82. $enableLinkPasswordByDefault = $enableLinkPasswordByDefault === 'yes';
  83. $defaultExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
  84. $defaultExpireDate = $enforceDefaultExpireDate = null;
  85. if ($defaultExpireDateEnabled) {
  86. $defaultExpireDate = (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
  87. $enforceDefaultExpireDate = $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
  88. }
  89. $outgoingServer2serverShareEnabled = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
  90. $defaultInternalExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_internal_expire_date', 'no') === 'yes';
  91. $defaultInternalExpireDate = $defaultInternalExpireDateEnforced = null;
  92. if ($defaultInternalExpireDateEnabled) {
  93. $defaultInternalExpireDate = (int)$this->config->getAppValue('core', 'shareapi_internal_expire_after_n_days', '7');
  94. $defaultInternalExpireDateEnforced = $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no') === 'yes';
  95. }
  96. $defaultRemoteExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_remote_expire_date', 'no') === 'yes';
  97. $defaultRemoteExpireDate = $defaultRemoteExpireDateEnforced = null;
  98. if ($defaultRemoteExpireDateEnabled) {
  99. $defaultRemoteExpireDate = (int)$this->config->getAppValue('core', 'shareapi_remote_expire_after_n_days', '7');
  100. $defaultRemoteExpireDateEnforced = $this->config->getAppValue('core', 'shareapi_enforce_remote_expire_date', 'no') === 'yes';
  101. }
  102. $countOfDataLocation = 0;
  103. $dataLocation = str_replace(\OC::$SERVERROOT . '/', '', $this->config->getSystemValue('datadirectory', ''), $countOfDataLocation);
  104. if ($countOfDataLocation !== 1 || $uid === null || !$this->groupManager->isAdmin($uid)) {
  105. $dataLocation = false;
  106. }
  107. if ($this->currentUser instanceof IUser) {
  108. if ($this->canUserValidatePassword()) {
  109. $lastConfirmTimestamp = $this->session->get('last-password-confirm');
  110. if (!is_int($lastConfirmTimestamp)) {
  111. $lastConfirmTimestamp = 0;
  112. }
  113. } else {
  114. $lastConfirmTimestamp = PHP_INT_MAX;
  115. }
  116. } else {
  117. $lastConfirmTimestamp = 0;
  118. }
  119. $capabilities = $this->capabilitiesManager->getCapabilities(false, true);
  120. $userFirstDay = $this->config->getUserValue($uid, 'core', AUserData::USER_FIELD_FIRST_DAY_OF_WEEK, null);
  121. $firstDay = (int)($userFirstDay ?? $this->l->l('firstday', null));
  122. $config = [
  123. /** @deprecated 30.0.0 - use files capabilities instead */
  124. 'blacklist_files_regex' => FileInfo::BLACKLIST_FILES_REGEX,
  125. /** @deprecated 30.0.0 - use files capabilities instead */
  126. 'forbidden_filename_characters' => $this->filenameValidator->getForbiddenCharacters(),
  127. 'auto_logout' => $this->config->getSystemValue('auto_logout', false),
  128. 'loglevel' => $this->config->getSystemValue('loglevel_frontend',
  129. $this->config->getSystemValue('loglevel', ILogger::WARN)
  130. ),
  131. 'lost_password_link' => $this->config->getSystemValue('lost_password_link', null),
  132. 'modRewriteWorking' => $this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true',
  133. 'no_unsupported_browser_warning' => $this->config->getSystemValue('no_unsupported_browser_warning', false),
  134. 'session_keepalive' => $this->config->getSystemValue('session_keepalive', true),
  135. 'session_lifetime' => min($this->config->getSystemValue('session_lifetime', $this->iniWrapper->getNumeric('session.gc_maxlifetime')), $this->iniWrapper->getNumeric('session.gc_maxlifetime')),
  136. 'sharing.maxAutocompleteResults' => max(0, $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT)),
  137. 'sharing.minSearchStringLength' => $this->config->getSystemValueInt('sharing.minSearchStringLength', 0),
  138. 'version' => implode('.', Util::getVersion()),
  139. 'versionstring' => \OC_Util::getVersionString(),
  140. 'enable_non-accessible_features' => $this->config->getSystemValueBool('enable_non-accessible_features', true),
  141. ];
  142. $array = [
  143. '_oc_debug' => $this->config->getSystemValue('debug', false) ? 'true' : 'false',
  144. '_oc_isadmin' => $uid !== null && $this->groupManager->isAdmin($uid) ? 'true' : 'false',
  145. 'backendAllowsPasswordConfirmation' => $userBackendAllowsPasswordConfirmation ? 'true' : 'false',
  146. 'oc_dataURL' => is_string($dataLocation) ? '"' . $dataLocation . '"' : 'false',
  147. '_oc_webroot' => '"' . \OC::$WEBROOT . '"',
  148. '_oc_appswebroots' => str_replace('\\/', '/', json_encode($apps_paths)), // Ugly unescape slashes waiting for better solution
  149. 'datepickerFormatDate' => json_encode($this->l->l('jsdate', null)),
  150. 'nc_lastLogin' => $lastConfirmTimestamp,
  151. 'nc_pageLoad' => time(),
  152. 'dayNames' => json_encode([
  153. $this->l->t('Sunday'),
  154. $this->l->t('Monday'),
  155. $this->l->t('Tuesday'),
  156. $this->l->t('Wednesday'),
  157. $this->l->t('Thursday'),
  158. $this->l->t('Friday'),
  159. $this->l->t('Saturday')
  160. ]),
  161. 'dayNamesShort' => json_encode([
  162. $this->l->t('Sun.'),
  163. $this->l->t('Mon.'),
  164. $this->l->t('Tue.'),
  165. $this->l->t('Wed.'),
  166. $this->l->t('Thu.'),
  167. $this->l->t('Fri.'),
  168. $this->l->t('Sat.')
  169. ]),
  170. 'dayNamesMin' => json_encode([
  171. $this->l->t('Su'),
  172. $this->l->t('Mo'),
  173. $this->l->t('Tu'),
  174. $this->l->t('We'),
  175. $this->l->t('Th'),
  176. $this->l->t('Fr'),
  177. $this->l->t('Sa')
  178. ]),
  179. 'monthNames' => json_encode([
  180. $this->l->t('January'),
  181. $this->l->t('February'),
  182. $this->l->t('March'),
  183. $this->l->t('April'),
  184. $this->l->t('May'),
  185. $this->l->t('June'),
  186. $this->l->t('July'),
  187. $this->l->t('August'),
  188. $this->l->t('September'),
  189. $this->l->t('October'),
  190. $this->l->t('November'),
  191. $this->l->t('December')
  192. ]),
  193. 'monthNamesShort' => json_encode([
  194. $this->l->t('Jan.'),
  195. $this->l->t('Feb.'),
  196. $this->l->t('Mar.'),
  197. $this->l->t('Apr.'),
  198. $this->l->t('May.'),
  199. $this->l->t('Jun.'),
  200. $this->l->t('Jul.'),
  201. $this->l->t('Aug.'),
  202. $this->l->t('Sep.'),
  203. $this->l->t('Oct.'),
  204. $this->l->t('Nov.'),
  205. $this->l->t('Dec.')
  206. ]),
  207. 'firstDay' => json_encode($firstDay),
  208. '_oc_config' => json_encode($config),
  209. 'oc_appconfig' => json_encode([
  210. 'core' => [
  211. 'defaultExpireDateEnabled' => $defaultExpireDateEnabled,
  212. 'defaultExpireDate' => $defaultExpireDate,
  213. 'defaultExpireDateEnforced' => $enforceDefaultExpireDate,
  214. 'enforcePasswordForPublicLink' => Util::isPublicLinkPasswordRequired(),
  215. 'enableLinkPasswordByDefault' => $enableLinkPasswordByDefault,
  216. 'sharingDisabledForUser' => Util::isSharingDisabledForUser(),
  217. 'resharingAllowed' => Share::isResharingAllowed(),
  218. 'remoteShareAllowed' => $outgoingServer2serverShareEnabled,
  219. 'federatedCloudShareDoc' => $this->urlGenerator->linkToDocs('user-sharing-federated'),
  220. 'allowGroupSharing' => \OC::$server->get(IShareManager::class)->allowGroupSharing(),
  221. 'defaultInternalExpireDateEnabled' => $defaultInternalExpireDateEnabled,
  222. 'defaultInternalExpireDate' => $defaultInternalExpireDate,
  223. 'defaultInternalExpireDateEnforced' => $defaultInternalExpireDateEnforced,
  224. 'defaultRemoteExpireDateEnabled' => $defaultRemoteExpireDateEnabled,
  225. 'defaultRemoteExpireDate' => $defaultRemoteExpireDate,
  226. 'defaultRemoteExpireDateEnforced' => $defaultRemoteExpireDateEnforced,
  227. ]
  228. ]),
  229. '_theme' => json_encode([
  230. 'entity' => $this->defaults->getEntity(),
  231. 'name' => $this->defaults->getName(),
  232. 'productName' => $this->defaults->getProductName(),
  233. 'title' => $this->defaults->getTitle(),
  234. 'baseUrl' => $this->defaults->getBaseUrl(),
  235. 'syncClientUrl' => $this->defaults->getSyncClientUrl(),
  236. 'docBaseUrl' => $this->defaults->getDocBaseUrl(),
  237. 'docPlaceholderUrl' => $this->defaults->buildDocLinkToKey('PLACEHOLDER'),
  238. 'slogan' => $this->defaults->getSlogan(),
  239. 'logoClaim' => '',
  240. 'folder' => \OC_Util::getTheme(),
  241. ]),
  242. ];
  243. if ($this->currentUser !== null) {
  244. $array['oc_userconfig'] = json_encode([
  245. 'avatar' => [
  246. 'version' => (int)$this->config->getUserValue($uid, 'avatar', 'version', 0),
  247. 'generated' => $this->config->getUserValue($uid, 'avatar', 'generated', 'true') === 'true',
  248. ]
  249. ]);
  250. }
  251. $this->initialStateService->provideInitialState('core', 'projects_enabled', $this->config->getSystemValueBool('projects.enabled', false));
  252. $this->initialStateService->provideInitialState('core', 'config', $config);
  253. $this->initialStateService->provideInitialState('core', 'capabilities', $capabilities);
  254. // Allow hooks to modify the output values
  255. \OC_Hook::emit('\OCP\Config', 'js', ['array' => &$array]);
  256. $result = '';
  257. // Echo it
  258. foreach ($array as $setting => $value) {
  259. $result .= 'var '. $setting . '='. $value . ';' . PHP_EOL;
  260. }
  261. return $result;
  262. }
  263. protected function canUserValidatePassword(): bool {
  264. try {
  265. $token = $this->tokenProvider->getToken($this->session->getId());
  266. } catch (ExpiredTokenException|WipeTokenException|InvalidTokenException|SessionNotAvailableException) {
  267. // actually we do not know, so we fall back to this statement
  268. return true;
  269. }
  270. $scope = $token->getScopeAsArray();
  271. return !isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) || $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === false;
  272. }
  273. }