1
0

JSConfigHelper.php 12 KB

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