JSConfigHelper.php 12 KB

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