You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
12 KiB

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