JSConfigHelper.php 8.9 KB

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