PluginManager.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud GmbH.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\AppInfo;
  28. use OC\ServerContainer;
  29. use OCA\DAV\CalDAV\AppCalendar\AppCalendarPlugin;
  30. use OCA\DAV\CalDAV\Integration\ICalendarProvider;
  31. use OCA\DAV\CardDAV\Integration\IAddressBookProvider;
  32. use OCP\App\IAppManager;
  33. use OCP\AppFramework\QueryException;
  34. use Sabre\DAV\Collection;
  35. use Sabre\DAV\ServerPlugin;
  36. use function array_map;
  37. use function class_exists;
  38. use function is_array;
  39. /**
  40. * Manager for DAV plugins from apps, used to register them
  41. * to the Sabre server.
  42. */
  43. class PluginManager {
  44. /**
  45. * @var ServerContainer
  46. */
  47. private $container;
  48. /**
  49. * @var IAppManager
  50. */
  51. private $appManager;
  52. /**
  53. * App plugins
  54. *
  55. * @var ServerPlugin[]
  56. */
  57. private $plugins = [];
  58. /**
  59. * App collections
  60. *
  61. * @var Collection[]
  62. */
  63. private $collections = [];
  64. /**
  65. * Address book plugins
  66. *
  67. * @var IAddressBookProvider[]
  68. */
  69. private $addressBookPlugins = [];
  70. /**
  71. * Calendar plugins
  72. *
  73. * @var ICalendarProvider[]
  74. */
  75. private $calendarPlugins = [];
  76. /** @var bool */
  77. private $populated = false;
  78. /**
  79. * Contstruct a PluginManager
  80. *
  81. * @param ServerContainer $container server container for resolving plugin classes
  82. * @param IAppManager $appManager app manager to loading apps and their info
  83. */
  84. public function __construct(ServerContainer $container, IAppManager $appManager) {
  85. $this->container = $container;
  86. $this->appManager = $appManager;
  87. }
  88. /**
  89. * Returns an array of app-registered plugins
  90. *
  91. * @return ServerPlugin[]
  92. */
  93. public function getAppPlugins() {
  94. $this->populate();
  95. return $this->plugins;
  96. }
  97. /**
  98. * Returns an array of app-registered collections
  99. *
  100. * @return array
  101. */
  102. public function getAppCollections() {
  103. $this->populate();
  104. return $this->collections;
  105. }
  106. /**
  107. * @return IAddressBookProvider[]
  108. */
  109. public function getAddressBookPlugins(): array {
  110. $this->populate();
  111. return $this->addressBookPlugins;
  112. }
  113. /**
  114. * Returns an array of app-registered calendar plugins
  115. *
  116. * @return ICalendarProvider[]
  117. */
  118. public function getCalendarPlugins():array {
  119. $this->populate();
  120. return $this->calendarPlugins;
  121. }
  122. /**
  123. * Retrieve plugin and collection list and populate attributes
  124. */
  125. private function populate(): void {
  126. if ($this->populated) {
  127. return;
  128. }
  129. $this->populated = true;
  130. $this->calendarPlugins[] = $this->container->get(AppCalendarPlugin::class);
  131. foreach ($this->appManager->getInstalledApps() as $app) {
  132. // load plugins and collections from info.xml
  133. $info = $this->appManager->getAppInfo($app);
  134. if (!isset($info['types']) || !in_array('dav', $info['types'], true)) {
  135. continue;
  136. }
  137. $plugins = $this->loadSabrePluginsFromInfoXml($this->extractPluginList($info));
  138. foreach ($plugins as $plugin) {
  139. $this->plugins[] = $plugin;
  140. }
  141. $collections = $this->loadSabreCollectionsFromInfoXml($this->extractCollectionList($info));
  142. foreach ($collections as $collection) {
  143. $this->collections[] = $collection;
  144. }
  145. $addresbookPlugins = $this->loadSabreAddressBookPluginsFromInfoXml($this->extractAddressBookPluginList($info));
  146. foreach ($addresbookPlugins as $addresbookPlugin) {
  147. $this->addressBookPlugins[] = $addresbookPlugin;
  148. }
  149. $calendarPlugins = $this->loadSabreCalendarPluginsFromInfoXml($this->extractCalendarPluginList($info));
  150. foreach ($calendarPlugins as $calendarPlugin) {
  151. $this->calendarPlugins[] = $calendarPlugin;
  152. }
  153. }
  154. }
  155. /**
  156. * @param array $array
  157. * @return string[]
  158. */
  159. private function extractPluginList(array $array): array {
  160. if (isset($array['sabre']) && is_array($array['sabre'])) {
  161. if (isset($array['sabre']['plugins']) && is_array($array['sabre']['plugins'])) {
  162. if (isset($array['sabre']['plugins']['plugin'])) {
  163. $items = $array['sabre']['plugins']['plugin'];
  164. if (!is_array($items)) {
  165. $items = [$items];
  166. }
  167. return $items;
  168. }
  169. }
  170. }
  171. return [];
  172. }
  173. /**
  174. * @param array $array
  175. * @return string[]
  176. */
  177. private function extractCollectionList(array $array): array {
  178. if (isset($array['sabre']) && is_array($array['sabre'])) {
  179. if (isset($array['sabre']['collections']) && is_array($array['sabre']['collections'])) {
  180. if (isset($array['sabre']['collections']['collection'])) {
  181. $items = $array['sabre']['collections']['collection'];
  182. if (!is_array($items)) {
  183. $items = [$items];
  184. }
  185. return $items;
  186. }
  187. }
  188. }
  189. return [];
  190. }
  191. /**
  192. * @param array $array
  193. * @return string[]
  194. */
  195. private function extractAddressBookPluginList(array $array): array {
  196. if (!isset($array['sabre']) || !is_array($array['sabre'])) {
  197. return [];
  198. }
  199. if (!isset($array['sabre']['address-book-plugins']) || !is_array($array['sabre']['address-book-plugins'])) {
  200. return [];
  201. }
  202. if (!isset($array['sabre']['address-book-plugins']['plugin'])) {
  203. return [];
  204. }
  205. $items = $array['sabre']['address-book-plugins']['plugin'];
  206. if (!is_array($items)) {
  207. $items = [$items];
  208. }
  209. return $items;
  210. }
  211. /**
  212. * @param array $array
  213. * @return string[]
  214. */
  215. private function extractCalendarPluginList(array $array): array {
  216. if (isset($array['sabre']) && is_array($array['sabre'])) {
  217. if (isset($array['sabre']['calendar-plugins']) && is_array($array['sabre']['calendar-plugins'])) {
  218. if (isset($array['sabre']['calendar-plugins']['plugin'])) {
  219. $items = $array['sabre']['calendar-plugins']['plugin'];
  220. if (!is_array($items)) {
  221. $items = [$items];
  222. }
  223. return $items;
  224. }
  225. }
  226. }
  227. return [];
  228. }
  229. private function createClass(string $className): object {
  230. try {
  231. return $this->container->get($className);
  232. } catch (QueryException $e) {
  233. if (class_exists($className)) {
  234. return new $className();
  235. }
  236. }
  237. throw new \Exception('Could not load ' . $className, 0, $e);
  238. }
  239. /**
  240. * @param string[] $classes
  241. * @return ServerPlugin[]
  242. * @throws \Exception
  243. */
  244. private function loadSabrePluginsFromInfoXml(array $classes): array {
  245. return array_map(function (string $className): ServerPlugin {
  246. $instance = $this->createClass($className);
  247. if (!($instance instanceof ServerPlugin)) {
  248. throw new \Exception('Sabre server plugin ' . $className . ' does not implement the ' . ServerPlugin::class . ' interface');
  249. }
  250. return $instance;
  251. }, $classes);
  252. }
  253. /**
  254. * @param string[] $classes
  255. * @return Collection[]
  256. */
  257. private function loadSabreCollectionsFromInfoXml(array $classes): array {
  258. return array_map(function (string $className): Collection {
  259. $instance = $this->createClass($className);
  260. if (!($instance instanceof Collection)) {
  261. throw new \Exception('Sabre collection plugin ' . $className . ' does not implement the ' . Collection::class . ' interface');
  262. }
  263. return $instance;
  264. }, $classes);
  265. }
  266. /**
  267. * @param string[] $classes
  268. * @return IAddressBookProvider[]
  269. */
  270. private function loadSabreAddressBookPluginsFromInfoXml(array $classes): array {
  271. return array_map(function (string $className): IAddressBookProvider {
  272. $instance = $this->createClass($className);
  273. if (!($instance instanceof IAddressBookProvider)) {
  274. throw new \Exception('Sabre address book plugin class ' . $className . ' does not implement the ' . IAddressBookProvider::class . ' interface');
  275. }
  276. return $instance;
  277. }, $classes);
  278. }
  279. /**
  280. * @param string[] $classes
  281. * @return ICalendarProvider[]
  282. */
  283. private function loadSabreCalendarPluginsFromInfoXml(array $classes): array {
  284. return array_map(function (string $className): ICalendarProvider {
  285. $instance = $this->createClass($className);
  286. if (!($instance instanceof ICalendarProvider)) {
  287. throw new \Exception('Sabre calendar plugin class ' . $className . ' does not implement the ' . ICalendarProvider::class . ' interface');
  288. }
  289. return $instance;
  290. }, $classes);
  291. }
  292. }