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.

JSConfigHelper.php 12KB

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