JSConfigHelper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\ILogger;
  47. use OCP\ISession;
  48. use OCP\IURLGenerator;
  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. 'enable_non-accessible_features' => $this->config->getSystemValueBool('enable_non-accessible_features', true),
  167. ];
  168. $array = [
  169. "_oc_debug" => $this->config->getSystemValue('debug', false) ? 'true' : 'false',
  170. "_oc_isadmin" => $uid !== null && $this->groupManager->isAdmin($uid) ? 'true' : 'false',
  171. "backendAllowsPasswordConfirmation" => $userBackendAllowsPasswordConfirmation ? 'true' : 'false',
  172. "oc_dataURL" => is_string($dataLocation) ? "\"" . $dataLocation . "\"" : 'false',
  173. "_oc_webroot" => "\"" . \OC::$WEBROOT . "\"",
  174. "_oc_appswebroots" => str_replace('\\/', '/', json_encode($apps_paths)), // Ugly unescape slashes waiting for better solution
  175. "datepickerFormatDate" => json_encode($this->l->l('jsdate', null)),
  176. 'nc_lastLogin' => $lastConfirmTimestamp,
  177. 'nc_pageLoad' => time(),
  178. "dayNames" => json_encode([
  179. $this->l->t('Sunday'),
  180. $this->l->t('Monday'),
  181. $this->l->t('Tuesday'),
  182. $this->l->t('Wednesday'),
  183. $this->l->t('Thursday'),
  184. $this->l->t('Friday'),
  185. $this->l->t('Saturday')
  186. ]),
  187. "dayNamesShort" => json_encode([
  188. $this->l->t('Sun.'),
  189. $this->l->t('Mon.'),
  190. $this->l->t('Tue.'),
  191. $this->l->t('Wed.'),
  192. $this->l->t('Thu.'),
  193. $this->l->t('Fri.'),
  194. $this->l->t('Sat.')
  195. ]),
  196. "dayNamesMin" => json_encode([
  197. $this->l->t('Su'),
  198. $this->l->t('Mo'),
  199. $this->l->t('Tu'),
  200. $this->l->t('We'),
  201. $this->l->t('Th'),
  202. $this->l->t('Fr'),
  203. $this->l->t('Sa')
  204. ]),
  205. "monthNames" => json_encode([
  206. $this->l->t('January'),
  207. $this->l->t('February'),
  208. $this->l->t('March'),
  209. $this->l->t('April'),
  210. $this->l->t('May'),
  211. $this->l->t('June'),
  212. $this->l->t('July'),
  213. $this->l->t('August'),
  214. $this->l->t('September'),
  215. $this->l->t('October'),
  216. $this->l->t('November'),
  217. $this->l->t('December')
  218. ]),
  219. "monthNamesShort" => json_encode([
  220. $this->l->t('Jan.'),
  221. $this->l->t('Feb.'),
  222. $this->l->t('Mar.'),
  223. $this->l->t('Apr.'),
  224. $this->l->t('May.'),
  225. $this->l->t('Jun.'),
  226. $this->l->t('Jul.'),
  227. $this->l->t('Aug.'),
  228. $this->l->t('Sep.'),
  229. $this->l->t('Oct.'),
  230. $this->l->t('Nov.'),
  231. $this->l->t('Dec.')
  232. ]),
  233. "firstDay" => json_encode($this->l->l('firstday', null)),
  234. "_oc_config" => json_encode($config),
  235. "oc_appconfig" => json_encode([
  236. 'core' => [
  237. 'defaultExpireDateEnabled' => $defaultExpireDateEnabled,
  238. 'defaultExpireDate' => $defaultExpireDate,
  239. 'defaultExpireDateEnforced' => $enforceDefaultExpireDate,
  240. 'enforcePasswordForPublicLink' => Util::isPublicLinkPasswordRequired(),
  241. 'enableLinkPasswordByDefault' => $enableLinkPasswordByDefault,
  242. 'sharingDisabledForUser' => Util::isSharingDisabledForUser(),
  243. 'resharingAllowed' => Share::isResharingAllowed(),
  244. 'remoteShareAllowed' => $outgoingServer2serverShareEnabled,
  245. 'federatedCloudShareDoc' => $this->urlGenerator->linkToDocs('user-sharing-federated'),
  246. 'allowGroupSharing' => \OC::$server->getShareManager()->allowGroupSharing(),
  247. 'defaultInternalExpireDateEnabled' => $defaultInternalExpireDateEnabled,
  248. 'defaultInternalExpireDate' => $defaultInternalExpireDate,
  249. 'defaultInternalExpireDateEnforced' => $defaultInternalExpireDateEnforced,
  250. 'defaultRemoteExpireDateEnabled' => $defaultRemoteExpireDateEnabled,
  251. 'defaultRemoteExpireDate' => $defaultRemoteExpireDate,
  252. 'defaultRemoteExpireDateEnforced' => $defaultRemoteExpireDateEnforced,
  253. ]
  254. ]),
  255. "_theme" => json_encode([
  256. 'entity' => $this->defaults->getEntity(),
  257. 'name' => $this->defaults->getName(),
  258. 'productName' => $this->defaults->getProductName(),
  259. 'title' => $this->defaults->getTitle(),
  260. 'baseUrl' => $this->defaults->getBaseUrl(),
  261. 'syncClientUrl' => $this->defaults->getSyncClientUrl(),
  262. 'docBaseUrl' => $this->defaults->getDocBaseUrl(),
  263. 'docPlaceholderUrl' => $this->defaults->buildDocLinkToKey('PLACEHOLDER'),
  264. 'slogan' => $this->defaults->getSlogan(),
  265. 'logoClaim' => '',
  266. 'folder' => \OC_Util::getTheme(),
  267. ]),
  268. ];
  269. if ($this->currentUser !== null) {
  270. $array['oc_userconfig'] = json_encode([
  271. 'avatar' => [
  272. 'version' => (int)$this->config->getUserValue($uid, 'avatar', 'version', 0),
  273. 'generated' => $this->config->getUserValue($uid, 'avatar', 'generated', 'true') === 'true',
  274. ]
  275. ]);
  276. }
  277. $this->initialStateService->provideInitialState('core', 'projects_enabled', $this->config->getSystemValueBool('projects.enabled', false));
  278. $this->initialStateService->provideInitialState('core', 'config', $config);
  279. $this->initialStateService->provideInitialState('core', 'capabilities', $capabilities);
  280. // Allow hooks to modify the output values
  281. \OC_Hook::emit('\OCP\Config', 'js', ['array' => &$array]);
  282. $result = '';
  283. // Echo it
  284. foreach ($array as $setting => $value) {
  285. $result .= 'var '. $setting . '='. $value . ';' . PHP_EOL;
  286. }
  287. return $result;
  288. }
  289. }