JSConfigHelper.php 11 KB

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