JSConfigHelper.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Abijeet <abijeetpatro@gmail.com>
  7. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  8. * @author Bjoern Schiessle <bjoern@schiessle.org>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Vincent Petry <vincent@nextcloud.com>
  16. *
  17. * @license GNU AGPL version 3 or any later version
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License as
  21. * published by the Free Software Foundation, either version 3 of the
  22. * License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. *
  32. */
  33. namespace OC\Template;
  34. use bantu\IniGetWrapper\IniGetWrapper;
  35. use OC\Authentication\Token\IProvider;
  36. use OC\CapabilitiesManager;
  37. use OC\Share\Share;
  38. use OCP\App\AppPathNotFoundException;
  39. use OCP\App\IAppManager;
  40. use OCP\Authentication\Exceptions\ExpiredTokenException;
  41. use OCP\Authentication\Exceptions\InvalidTokenException;
  42. use OCP\Authentication\Exceptions\WipeTokenException;
  43. use OCP\Constants;
  44. use OCP\Defaults;
  45. use OCP\Files\FileInfo;
  46. use OCP\IConfig;
  47. use OCP\IGroupManager;
  48. use OCP\IInitialStateService;
  49. use OCP\IL10N;
  50. use OCP\ILogger;
  51. use OCP\ISession;
  52. use OCP\IURLGenerator;
  53. use OCP\IUser;
  54. use OCP\Session\Exceptions\SessionNotAvailableException;
  55. use OCP\User\Backend\IPasswordConfirmationBackend;
  56. use OCP\Util;
  57. class JSConfigHelper {
  58. /** @var array user back-ends excluded from password verification */
  59. private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
  60. public function __construct(
  61. protected IL10N $l,
  62. protected Defaults $defaults,
  63. protected IAppManager $appManager,
  64. protected ISession $session,
  65. protected ?IUser $currentUser,
  66. protected IConfig $config,
  67. protected IGroupManager $groupManager,
  68. protected IniGetWrapper $iniWrapper,
  69. protected IURLGenerator $urlGenerator,
  70. protected CapabilitiesManager $capabilitiesManager,
  71. protected IInitialStateService $initialStateService,
  72. protected IProvider $tokenProvider,
  73. ) {
  74. }
  75. public function getConfig(): string {
  76. $userBackendAllowsPasswordConfirmation = true;
  77. if ($this->currentUser !== null) {
  78. $uid = $this->currentUser->getUID();
  79. $backend = $this->currentUser->getBackend();
  80. if ($backend instanceof IPasswordConfirmationBackend) {
  81. $userBackendAllowsPasswordConfirmation = $backend->canConfirmPassword($uid);
  82. } elseif (isset($this->excludedUserBackEnds[$this->currentUser->getBackendClassName()])) {
  83. $userBackendAllowsPasswordConfirmation = false;
  84. }
  85. } else {
  86. $uid = null;
  87. }
  88. // Get the config
  89. $apps_paths = [];
  90. if ($this->currentUser === null) {
  91. $apps = $this->appManager->getInstalledApps();
  92. } else {
  93. $apps = $this->appManager->getEnabledAppsForUser($this->currentUser);
  94. }
  95. foreach ($apps as $app) {
  96. try {
  97. $apps_paths[$app] = $this->appManager->getAppWebPath($app);
  98. } catch (AppPathNotFoundException $e) {
  99. $apps_paths[$app] = false;
  100. }
  101. }
  102. $enableLinkPasswordByDefault = $this->config->getAppValue('core', 'shareapi_enable_link_password_by_default', 'no');
  103. $enableLinkPasswordByDefault = $enableLinkPasswordByDefault === 'yes';
  104. $defaultExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
  105. $defaultExpireDate = $enforceDefaultExpireDate = null;
  106. if ($defaultExpireDateEnabled) {
  107. $defaultExpireDate = (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
  108. $enforceDefaultExpireDate = $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
  109. }
  110. $outgoingServer2serverShareEnabled = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
  111. $defaultInternalExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_internal_expire_date', 'no') === 'yes';
  112. $defaultInternalExpireDate = $defaultInternalExpireDateEnforced = null;
  113. if ($defaultInternalExpireDateEnabled) {
  114. $defaultInternalExpireDate = (int)$this->config->getAppValue('core', 'shareapi_internal_expire_after_n_days', '7');
  115. $defaultInternalExpireDateEnforced = $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no') === 'yes';
  116. }
  117. $defaultRemoteExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_remote_expire_date', 'no') === 'yes';
  118. $defaultRemoteExpireDate = $defaultRemoteExpireDateEnforced = null;
  119. if ($defaultRemoteExpireDateEnabled) {
  120. $defaultRemoteExpireDate = (int)$this->config->getAppValue('core', 'shareapi_remote_expire_after_n_days', '7');
  121. $defaultRemoteExpireDateEnforced = $this->config->getAppValue('core', 'shareapi_enforce_remote_expire_date', 'no') === 'yes';
  122. }
  123. $countOfDataLocation = 0;
  124. $dataLocation = str_replace(\OC::$SERVERROOT . '/', '', $this->config->getSystemValue('datadirectory', ''), $countOfDataLocation);
  125. if ($countOfDataLocation !== 1 || $uid === null || !$this->groupManager->isAdmin($uid)) {
  126. $dataLocation = false;
  127. }
  128. if ($this->currentUser instanceof IUser) {
  129. if ($this->canUserValidatePassword()) {
  130. $lastConfirmTimestamp = $this->session->get('last-password-confirm');
  131. if (!is_int($lastConfirmTimestamp)) {
  132. $lastConfirmTimestamp = 0;
  133. }
  134. } else {
  135. $lastConfirmTimestamp = PHP_INT_MAX;
  136. }
  137. } else {
  138. $lastConfirmTimestamp = 0;
  139. }
  140. $capabilities = $this->capabilitiesManager->getCapabilities(false, true);
  141. $config = [
  142. 'auto_logout' => $this->config->getSystemValue('auto_logout', false),
  143. 'blacklist_files_regex' => FileInfo::BLACKLIST_FILES_REGEX,
  144. 'loglevel' => $this->config->getSystemValue('loglevel_frontend',
  145. $this->config->getSystemValue('loglevel', ILogger::WARN)
  146. ),
  147. 'lost_password_link' => $this->config->getSystemValue('lost_password_link', null),
  148. 'modRewriteWorking' => $this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true',
  149. 'no_unsupported_browser_warning' => $this->config->getSystemValue('no_unsupported_browser_warning', false),
  150. 'session_keepalive' => $this->config->getSystemValue('session_keepalive', true),
  151. 'session_lifetime' => min($this->config->getSystemValue('session_lifetime', $this->iniWrapper->getNumeric('session.gc_maxlifetime')), $this->iniWrapper->getNumeric('session.gc_maxlifetime')),
  152. 'sharing.maxAutocompleteResults' => max(0, $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT)),
  153. 'sharing.minSearchStringLength' => $this->config->getSystemValueInt('sharing.minSearchStringLength', 0),
  154. 'version' => implode('.', Util::getVersion()),
  155. 'versionstring' => \OC_Util::getVersionString(),
  156. 'enable_non-accessible_features' => $this->config->getSystemValueBool('enable_non-accessible_features', true),
  157. ];
  158. $array = [
  159. "_oc_debug" => $this->config->getSystemValue('debug', false) ? 'true' : 'false',
  160. "_oc_isadmin" => $uid !== null && $this->groupManager->isAdmin($uid) ? 'true' : 'false',
  161. "backendAllowsPasswordConfirmation" => $userBackendAllowsPasswordConfirmation ? 'true' : 'false',
  162. "oc_dataURL" => is_string($dataLocation) ? "\"" . $dataLocation . "\"" : 'false',
  163. "_oc_webroot" => "\"" . \OC::$WEBROOT . "\"",
  164. "_oc_appswebroots" => str_replace('\\/', '/', json_encode($apps_paths)), // Ugly unescape slashes waiting for better solution
  165. "datepickerFormatDate" => json_encode($this->l->l('jsdate', null)),
  166. 'nc_lastLogin' => $lastConfirmTimestamp,
  167. 'nc_pageLoad' => time(),
  168. "dayNames" => json_encode([
  169. $this->l->t('Sunday'),
  170. $this->l->t('Monday'),
  171. $this->l->t('Tuesday'),
  172. $this->l->t('Wednesday'),
  173. $this->l->t('Thursday'),
  174. $this->l->t('Friday'),
  175. $this->l->t('Saturday')
  176. ]),
  177. "dayNamesShort" => json_encode([
  178. $this->l->t('Sun.'),
  179. $this->l->t('Mon.'),
  180. $this->l->t('Tue.'),
  181. $this->l->t('Wed.'),
  182. $this->l->t('Thu.'),
  183. $this->l->t('Fri.'),
  184. $this->l->t('Sat.')
  185. ]),
  186. "dayNamesMin" => json_encode([
  187. $this->l->t('Su'),
  188. $this->l->t('Mo'),
  189. $this->l->t('Tu'),
  190. $this->l->t('We'),
  191. $this->l->t('Th'),
  192. $this->l->t('Fr'),
  193. $this->l->t('Sa')
  194. ]),
  195. "monthNames" => json_encode([
  196. $this->l->t('January'),
  197. $this->l->t('February'),
  198. $this->l->t('March'),
  199. $this->l->t('April'),
  200. $this->l->t('May'),
  201. $this->l->t('June'),
  202. $this->l->t('July'),
  203. $this->l->t('August'),
  204. $this->l->t('September'),
  205. $this->l->t('October'),
  206. $this->l->t('November'),
  207. $this->l->t('December')
  208. ]),
  209. "monthNamesShort" => json_encode([
  210. $this->l->t('Jan.'),
  211. $this->l->t('Feb.'),
  212. $this->l->t('Mar.'),
  213. $this->l->t('Apr.'),
  214. $this->l->t('May.'),
  215. $this->l->t('Jun.'),
  216. $this->l->t('Jul.'),
  217. $this->l->t('Aug.'),
  218. $this->l->t('Sep.'),
  219. $this->l->t('Oct.'),
  220. $this->l->t('Nov.'),
  221. $this->l->t('Dec.')
  222. ]),
  223. "firstDay" => json_encode($this->l->l('firstday', null)),
  224. "_oc_config" => json_encode($config),
  225. "oc_appconfig" => json_encode([
  226. 'core' => [
  227. 'defaultExpireDateEnabled' => $defaultExpireDateEnabled,
  228. 'defaultExpireDate' => $defaultExpireDate,
  229. 'defaultExpireDateEnforced' => $enforceDefaultExpireDate,
  230. 'enforcePasswordForPublicLink' => Util::isPublicLinkPasswordRequired(),
  231. 'enableLinkPasswordByDefault' => $enableLinkPasswordByDefault,
  232. 'sharingDisabledForUser' => Util::isSharingDisabledForUser(),
  233. 'resharingAllowed' => Share::isResharingAllowed(),
  234. 'remoteShareAllowed' => $outgoingServer2serverShareEnabled,
  235. 'federatedCloudShareDoc' => $this->urlGenerator->linkToDocs('user-sharing-federated'),
  236. 'allowGroupSharing' => \OC::$server->getShareManager()->allowGroupSharing(),
  237. 'defaultInternalExpireDateEnabled' => $defaultInternalExpireDateEnabled,
  238. 'defaultInternalExpireDate' => $defaultInternalExpireDate,
  239. 'defaultInternalExpireDateEnforced' => $defaultInternalExpireDateEnforced,
  240. 'defaultRemoteExpireDateEnabled' => $defaultRemoteExpireDateEnabled,
  241. 'defaultRemoteExpireDate' => $defaultRemoteExpireDate,
  242. 'defaultRemoteExpireDateEnforced' => $defaultRemoteExpireDateEnforced,
  243. ]
  244. ]),
  245. "_theme" => json_encode([
  246. 'entity' => $this->defaults->getEntity(),
  247. 'name' => $this->defaults->getName(),
  248. 'productName' => $this->defaults->getProductName(),
  249. 'title' => $this->defaults->getTitle(),
  250. 'baseUrl' => $this->defaults->getBaseUrl(),
  251. 'syncClientUrl' => $this->defaults->getSyncClientUrl(),
  252. 'docBaseUrl' => $this->defaults->getDocBaseUrl(),
  253. 'docPlaceholderUrl' => $this->defaults->buildDocLinkToKey('PLACEHOLDER'),
  254. 'slogan' => $this->defaults->getSlogan(),
  255. 'logoClaim' => '',
  256. 'folder' => \OC_Util::getTheme(),
  257. ]),
  258. ];
  259. if ($this->currentUser !== null) {
  260. $array['oc_userconfig'] = json_encode([
  261. 'avatar' => [
  262. 'version' => (int)$this->config->getUserValue($uid, 'avatar', 'version', 0),
  263. 'generated' => $this->config->getUserValue($uid, 'avatar', 'generated', 'true') === 'true',
  264. ]
  265. ]);
  266. }
  267. $this->initialStateService->provideInitialState('core', 'projects_enabled', $this->config->getSystemValueBool('projects.enabled', false));
  268. $this->initialStateService->provideInitialState('core', 'config', $config);
  269. $this->initialStateService->provideInitialState('core', 'capabilities', $capabilities);
  270. // Allow hooks to modify the output values
  271. \OC_Hook::emit('\OCP\Config', 'js', ['array' => &$array]);
  272. $result = '';
  273. // Echo it
  274. foreach ($array as $setting => $value) {
  275. $result .= 'var '. $setting . '='. $value . ';' . PHP_EOL;
  276. }
  277. return $result;
  278. }
  279. protected function canUserValidatePassword(): bool {
  280. try {
  281. $token = $this->tokenProvider->getToken($this->session->getId());
  282. } catch (ExpiredTokenException|WipeTokenException|InvalidTokenException|SessionNotAvailableException) {
  283. // actually we do not know, so we fall back to this statement
  284. return true;
  285. }
  286. $scope = $token->getScopeAsArray();
  287. return !isset($scope['password-unconfirmable']) || $scope['password-unconfirmable'] === false;
  288. }
  289. }