Manager.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. 30 => [new Section('theming', $this->l->t('Theming'), 0, $this->url->imagePath('settings', 'theming-dark.svg'))],
  165. 50 => [new Section('groupware', $this->l->t('Groupware'), 0, $this->url->imagePath('core', 'places/contacts.svg'))],
  166. 98 => [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))],
  167. ];
  168. $appSections = $this->getSections('admin');
  169. foreach ($appSections as $section) {
  170. /** @var ISection $section */
  171. if (!isset($sections[$section->getPriority()])) {
  172. $sections[$section->getPriority()] = [];
  173. }
  174. $sections[$section->getPriority()][] = $section;
  175. }
  176. ksort($sections);
  177. return $sections;
  178. }
  179. /**
  180. * @param string $section
  181. *
  182. * @return ISection[]
  183. */
  184. private function getBuiltInAdminSettings($section): array {
  185. $forms = [];
  186. if ($section === 'overview') {
  187. /** @var ISettings $form */
  188. $form = $this->container->query(Admin\Overview::class);
  189. $forms[$form->getPriority()] = [$form];
  190. }
  191. if ($section === 'server') {
  192. /** @var ISettings $form */
  193. $form = $this->container->query(Admin\Server::class);
  194. $forms[$form->getPriority()] = [$form];
  195. $form = $this->container->query(Admin\Mail::class);
  196. $forms[$form->getPriority()] = [$form];
  197. }
  198. if ($section === 'security') {
  199. /** @var ISettings $form */
  200. $form = $this->container->query(Admin\Security::class);
  201. $forms[$form->getPriority()] = [$form];
  202. }
  203. if ($section === 'theming') {
  204. $form = $this->container->query(Theming\ServerInfo::class);
  205. $forms[$form->getPriority()] = [$form];
  206. }
  207. if ($section === 'sharing') {
  208. /** @var ISettings $form */
  209. $form = $this->container->query(Admin\Sharing::class);
  210. $forms[$form->getPriority()] = [$form];
  211. }
  212. return $forms;
  213. }
  214. /**
  215. * @param string $section
  216. *
  217. * @return ISection[]
  218. */
  219. private function getBuiltInPersonalSettings($section): array {
  220. $forms = [];
  221. if ($section === 'personal-info') {
  222. /** @var ISettings $form */
  223. $form = $this->container->query(Personal\PersonalInfo::class);
  224. $forms[$form->getPriority()] = [$form];
  225. $form = new Personal\ServerDevNotice();
  226. $forms[$form->getPriority()] = [$form];
  227. }
  228. if ($section === 'security') {
  229. /** @var ISettings $form */
  230. $form = $this->container->query(Personal\Security::class);
  231. $forms[$form->getPriority()] = [$form];
  232. }
  233. if ($section === 'additional') {
  234. /** @var ISettings $form */
  235. $form = $this->container->query(Personal\Additional::class);
  236. $forms[$form->getPriority()] = [$form];
  237. }
  238. return $forms;
  239. }
  240. /**
  241. * @inheritdoc
  242. */
  243. public function getAdminSettings($section): array {
  244. $settings = $this->getBuiltInAdminSettings($section);
  245. $appSettings = $this->getSettings('admin', $section);
  246. foreach ($appSettings as $setting) {
  247. if (!isset($settings[$setting->getPriority()])) {
  248. $settings[$setting->getPriority()] = [];
  249. }
  250. $settings[$setting->getPriority()][] = $setting;
  251. }
  252. ksort($settings);
  253. return $settings;
  254. }
  255. /**
  256. * @inheritdoc
  257. */
  258. public function getPersonalSections(): array {
  259. $sections = [
  260. 0 => [new Section('personal-info', $this->l->t('Personal info'), 0, $this->url->imagePath('core', 'actions/info.svg'))],
  261. 5 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('settings', 'password.svg'))],
  262. 15 => [new Section('sync-clients', $this->l->t('Mobile & desktop'), 0, $this->url->imagePath('core', 'clients/phone.svg'))],
  263. ];
  264. $legacyForms = \OC_App::getForms('personal');
  265. if (!empty($legacyForms) && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
  266. $sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
  267. }
  268. $appSections = $this->getSections('personal');
  269. foreach ($appSections as $section) {
  270. /** @var ISection $section */
  271. if (!isset($sections[$section->getPriority()])) {
  272. $sections[$section->getPriority()] = [];
  273. }
  274. $sections[$section->getPriority()][] = $section;
  275. }
  276. ksort($sections);
  277. return $sections;
  278. }
  279. /**
  280. * @param string[] $forms
  281. *
  282. * @return bool
  283. */
  284. private function hasLegacyPersonalSettingsToRender(array $forms): bool {
  285. foreach ($forms as $form) {
  286. if (trim($form) !== '') {
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. /**
  293. * @inheritdoc
  294. */
  295. public function getPersonalSettings($section): array {
  296. $settings = $this->getBuiltInPersonalSettings($section);
  297. $appSettings = $this->getSettings('personal', $section);
  298. foreach ($appSettings as $setting) {
  299. if (!isset($settings[$setting->getPriority()])) {
  300. $settings[$setting->getPriority()] = [];
  301. }
  302. $settings[$setting->getPriority()][] = $setting;
  303. }
  304. ksort($settings);
  305. return $settings;
  306. }
  307. }