1
0

Manager.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Marius Blüm <marius@lineone.io>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OC\Settings;
  30. use OCP\AppFramework\QueryException;
  31. use OCP\IL10N;
  32. use OCP\ILogger;
  33. use OCP\IServerContainer;
  34. use OCP\IURLGenerator;
  35. use OCP\Settings\ISettings;
  36. use OCP\Settings\IManager;
  37. use OCP\Settings\ISection;
  38. class Manager implements IManager {
  39. /** @var ILogger */
  40. private $log;
  41. /** @var IL10N */
  42. private $l;
  43. /** @var IURLGenerator */
  44. private $url;
  45. /** @var IServerContainer */
  46. private $container;
  47. public function __construct(
  48. ILogger $log,
  49. IL10N $l10n,
  50. IURLGenerator $url,
  51. IServerContainer $container
  52. ) {
  53. $this->log = $log;
  54. $this->l = $l10n;
  55. $this->url = $url;
  56. $this->container = $container;
  57. }
  58. /** @var array */
  59. protected $sectionClasses = [];
  60. /** @var array */
  61. protected $sections = [];
  62. /**
  63. * @param string $type 'admin' or 'personal'
  64. * @param string $section Class must implement OCP\Settings\ISection
  65. *
  66. * @return void
  67. */
  68. public function registerSection(string $type, string $section) {
  69. if (!isset($this->sectionClasses[$type])) {
  70. $this->sectionClasses[$type] = [];
  71. }
  72. $this->sectionClasses[$type][] = $section;
  73. }
  74. /**
  75. * @param string $type 'admin' or 'personal'
  76. *
  77. * @return ISection[]
  78. */
  79. protected function getSections(string $type): array {
  80. if (!isset($this->sections[$type])) {
  81. $this->sections[$type] = [];
  82. }
  83. if (!isset($this->sectionClasses[$type])) {
  84. return $this->sections[$type];
  85. }
  86. foreach ($this->sectionClasses[$type] as $index => $class) {
  87. try {
  88. /** @var ISection $section */
  89. $section = \OC::$server->query($class);
  90. } catch (QueryException $e) {
  91. $this->log->logException($e, ['level' => ILogger::INFO]);
  92. continue;
  93. }
  94. if (!$section instanceof ISection) {
  95. $this->log->logException(new \InvalidArgumentException('Invalid settings section registered'), ['level' => ILogger::INFO]);
  96. continue;
  97. }
  98. $sectionID = $section->getID();
  99. if (isset($this->sections[$type][$sectionID])) {
  100. $this->log->logException(new \InvalidArgumentException('Section with the same ID already registered'), ['level' => ILogger::INFO]);
  101. continue;
  102. }
  103. $this->sections[$type][$sectionID] = $section;
  104. unset($this->sectionClasses[$type][$index]);
  105. }
  106. return $this->sections[$type];
  107. }
  108. /** @var array */
  109. protected $settingClasses = [];
  110. /** @var array */
  111. protected $settings = [];
  112. /**
  113. * @param string $type 'admin' or 'personal'
  114. * @param string $setting Class must implement OCP\Settings\ISetting
  115. *
  116. * @return void
  117. */
  118. public function registerSetting(string $type, string $setting) {
  119. $this->settingClasses[$setting] = $type;
  120. }
  121. /**
  122. * @param string $type 'admin' or 'personal'
  123. * @param string $section
  124. *
  125. * @return ISettings[]
  126. */
  127. protected function getSettings(string $type, string $section): array {
  128. if (!isset($this->settings[$type])) {
  129. $this->settings[$type] = [];
  130. }
  131. if (!isset($this->settings[$type][$section])) {
  132. $this->settings[$type][$section] = [];
  133. }
  134. foreach ($this->settingClasses as $class => $settingsType) {
  135. try {
  136. /** @var ISettings $setting */
  137. $setting = \OC::$server->query($class);
  138. } catch (QueryException $e) {
  139. $this->log->logException($e, ['level' => ILogger::INFO]);
  140. continue;
  141. }
  142. if (!$setting instanceof ISettings) {
  143. $this->log->logException(new \InvalidArgumentException('Invalid settings setting registered (' . $class . ')'), ['level' => ILogger::INFO]);
  144. continue;
  145. }
  146. if (!isset($this->settings[$settingsType][$setting->getSection()])) {
  147. $this->settings[$settingsType][$setting->getSection()] = [];
  148. }
  149. $this->settings[$settingsType][$setting->getSection()][] = $setting;
  150. unset($this->settingClasses[$class]);
  151. }
  152. return $this->settings[$type][$section];
  153. }
  154. /**
  155. * @inheritdoc
  156. */
  157. public function getAdminSections(): array {
  158. // built-in sections
  159. $sections = [
  160. 0 => [new Section('overview', $this->l->t('Overview'), 0, $this->url->imagePath('settings', 'admin.svg'))],
  161. 1 => [new Section('server', $this->l->t('Basic settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))],
  162. 5 => [new Section('sharing', $this->l->t('Sharing'), 0, $this->url->imagePath('core', 'actions/share.svg'))],
  163. 10 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
  164. 50 => [new Section('groupware', $this->l->t('Groupware'), 0, $this->url->imagePath('core', 'places/contacts.svg'))],
  165. 98 => [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))],
  166. ];
  167. $appSections = $this->getSections('admin');
  168. foreach ($appSections as $section) {
  169. /** @var ISection $section */
  170. if (!isset($sections[$section->getPriority()])) {
  171. $sections[$section->getPriority()] = [];
  172. }
  173. $sections[$section->getPriority()][] = $section;
  174. }
  175. ksort($sections);
  176. return $sections;
  177. }
  178. /**
  179. * @param string $section
  180. *
  181. * @return ISection[]
  182. */
  183. private function getBuiltInAdminSettings($section): array {
  184. $forms = [];
  185. if ($section === 'overview') {
  186. /** @var ISettings $form */
  187. $form = $this->container->query(Admin\Overview::class);
  188. $forms[$form->getPriority()] = [$form];
  189. }
  190. if ($section === 'server') {
  191. /** @var ISettings $form */
  192. $form = $this->container->query(Admin\Server::class);
  193. $forms[$form->getPriority()] = [$form];
  194. $form = $this->container->query(Admin\Mail::class);
  195. $forms[$form->getPriority()] = [$form];
  196. }
  197. if ($section === 'security') {
  198. /** @var ISettings $form */
  199. $form = $this->container->query(Admin\Security::class);
  200. $forms[$form->getPriority()] = [$form];
  201. }
  202. if ($section === 'sharing') {
  203. /** @var ISettings $form */
  204. $form = $this->container->query(Admin\Sharing::class);
  205. $forms[$form->getPriority()] = [$form];
  206. }
  207. return $forms;
  208. }
  209. /**
  210. * @param string $section
  211. *
  212. * @return ISection[]
  213. */
  214. private function getBuiltInPersonalSettings($section): array {
  215. $forms = [];
  216. if ($section === 'personal-info') {
  217. /** @var ISettings $form */
  218. $form = $this->container->query(Personal\PersonalInfo::class);
  219. $forms[$form->getPriority()] = [$form];
  220. $form = new Personal\ServerDevNotice();
  221. $forms[$form->getPriority()] = [$form];
  222. }
  223. if ($section === 'security') {
  224. /** @var ISettings $form */
  225. $form = $this->container->query(Personal\Security::class);
  226. $forms[$form->getPriority()] = [$form];
  227. }
  228. if ($section === 'additional') {
  229. /** @var ISettings $form */
  230. $form = $this->container->query(Personal\Additional::class);
  231. $forms[$form->getPriority()] = [$form];
  232. }
  233. return $forms;
  234. }
  235. /**
  236. * @inheritdoc
  237. */
  238. public function getAdminSettings($section): array {
  239. $settings = $this->getBuiltInAdminSettings($section);
  240. $appSettings = $this->getSettings('admin', $section);
  241. foreach ($appSettings as $setting) {
  242. if (!isset($settings[$setting->getPriority()])) {
  243. $settings[$setting->getPriority()] = [];
  244. }
  245. $settings[$setting->getPriority()][] = $setting;
  246. }
  247. ksort($settings);
  248. return $settings;
  249. }
  250. /**
  251. * @inheritdoc
  252. */
  253. public function getPersonalSections(): array {
  254. $sections = [
  255. 0 => [new Section('personal-info', $this->l->t('Personal info'), 0, $this->url->imagePath('core', 'actions/info.svg'))],
  256. 5 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('settings', 'password.svg'))],
  257. 15 => [new Section('sync-clients', $this->l->t('Mobile & desktop'), 0, $this->url->imagePath('core', 'clients/phone.svg'))],
  258. ];
  259. $legacyForms = \OC_App::getForms('personal');
  260. if (!empty($legacyForms) && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
  261. $sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
  262. }
  263. $appSections = $this->getSections('personal');
  264. foreach ($appSections as $section) {
  265. /** @var ISection $section */
  266. if (!isset($sections[$section->getPriority()])) {
  267. $sections[$section->getPriority()] = [];
  268. }
  269. $sections[$section->getPriority()][] = $section;
  270. }
  271. ksort($sections);
  272. return $sections;
  273. }
  274. /**
  275. * @param string[] $forms
  276. *
  277. * @return bool
  278. */
  279. private function hasLegacyPersonalSettingsToRender(array $forms): bool {
  280. foreach ($forms as $form) {
  281. if (trim($form) !== '') {
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. /**
  288. * @inheritdoc
  289. */
  290. public function getPersonalSettings($section): array {
  291. $settings = $this->getBuiltInPersonalSettings($section);
  292. $appSettings = $this->getSettings('personal', $section);
  293. foreach ($appSettings as $setting) {
  294. if (!isset($settings[$setting->getPriority()])) {
  295. $settings[$setting->getPriority()] = [];
  296. }
  297. $settings[$setting->getPriority()][] = $setting;
  298. }
  299. ksort($settings);
  300. return $settings;
  301. }
  302. }