1
0

JSConfigHelper.php 12 KB

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