1
0

JSConfigHelper.php 12 KB

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