PluginManager.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <pvince81@owncloud.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\Integration\ICalendarProvider;
  30. use OCA\DAV\CardDAV\Integration\IAddressBookProvider;
  31. use OCP\App\IAppManager;
  32. use OCP\AppFramework\QueryException;
  33. use function array_map;
  34. use function class_exists;
  35. use function is_array;
  36. /**
  37. * Manager for DAV plugins from apps, used to register them
  38. * to the Sabre server.
  39. */
  40. class PluginManager {
  41. /**
  42. * @var ServerContainer
  43. */
  44. private $container;
  45. /**
  46. * @var IAppManager
  47. */
  48. private $appManager;
  49. /**
  50. * App plugins
  51. *
  52. * @var array
  53. */
  54. private $plugins = null;
  55. /**
  56. * App collections
  57. *
  58. * @var array
  59. */
  60. private $collections = null;
  61. /**
  62. * Address book plugins
  63. *
  64. * @var IAddressBookProvider[]|null
  65. */
  66. private $addressBookPlugins = null;
  67. /**
  68. * Calendar plugins
  69. *
  70. * @var array
  71. */
  72. private $calendarPlugins = null;
  73. /**
  74. * Contstruct a PluginManager
  75. *
  76. * @param ServerContainer $container server container for resolving plugin classes
  77. * @param IAppManager $appManager app manager to loading apps and their info
  78. */
  79. public function __construct(ServerContainer $container, IAppManager $appManager) {
  80. $this->container = $container;
  81. $this->appManager = $appManager;
  82. }
  83. /**
  84. * Returns an array of app-registered plugins
  85. *
  86. * @return array
  87. */
  88. public function getAppPlugins() {
  89. if (null === $this->plugins) {
  90. $this->populate();
  91. }
  92. return $this->plugins;
  93. }
  94. /**
  95. * Returns an array of app-registered collections
  96. *
  97. * @return array
  98. */
  99. public function getAppCollections() {
  100. if (null === $this->collections) {
  101. $this->populate();
  102. }
  103. return $this->collections;
  104. }
  105. /**
  106. * @return IAddressBookProvider[]
  107. */
  108. public function getAddressBookPlugins(): array {
  109. if ($this->addressBookPlugins === null) {
  110. $this->populate();
  111. }
  112. return $this->addressBookPlugins;
  113. }
  114. /**
  115. * Returns an array of app-registered calendar plugins
  116. *
  117. * @return array
  118. */
  119. public function getCalendarPlugins():array {
  120. if (null === $this->calendarPlugins) {
  121. $this->populate();
  122. }
  123. return $this->calendarPlugins;
  124. }
  125. /**
  126. * Retrieve plugin and collection list and populate attributes
  127. */
  128. private function populate() {
  129. $this->plugins = [];
  130. $this->addressBookPlugins = [];
  131. $this->calendarPlugins = [];
  132. $this->collections = [];
  133. foreach ($this->appManager->getInstalledApps() as $app) {
  134. // load plugins and collections from info.xml
  135. $info = $this->appManager->getAppInfo($app);
  136. if (!isset($info['types']) || !in_array('dav', $info['types'], true)) {
  137. continue;
  138. }
  139. $this->loadSabrePluginsFromInfoXml($this->extractPluginList($info));
  140. $this->loadSabreCollectionsFromInfoXml($this->extractCollectionList($info));
  141. $this->loadSabreAddressBookPluginsFromInfoXml($this->extractAddressBookPluginList($info));
  142. $this->loadSabreCalendarPluginsFromInfoXml($this->extractCalendarPluginList($info));
  143. }
  144. }
  145. private function extractPluginList(array $array) {
  146. if (isset($array['sabre']) && is_array($array['sabre'])) {
  147. if (isset($array['sabre']['plugins']) && is_array($array['sabre']['plugins'])) {
  148. if (isset($array['sabre']['plugins']['plugin'])) {
  149. $items = $array['sabre']['plugins']['plugin'];
  150. if (!is_array($items)) {
  151. $items = [$items];
  152. }
  153. return $items;
  154. }
  155. }
  156. }
  157. return [];
  158. }
  159. private function extractCollectionList(array $array) {
  160. if (isset($array['sabre']) && is_array($array['sabre'])) {
  161. if (isset($array['sabre']['collections']) && is_array($array['sabre']['collections'])) {
  162. if (isset($array['sabre']['collections']['collection'])) {
  163. $items = $array['sabre']['collections']['collection'];
  164. if (!is_array($items)) {
  165. $items = [$items];
  166. }
  167. return $items;
  168. }
  169. }
  170. }
  171. return [];
  172. }
  173. /**
  174. * @param array $array
  175. *
  176. * @return string[]
  177. */
  178. private function extractAddressBookPluginList(array $array): array {
  179. if (!isset($array['sabre']) || !is_array($array['sabre'])) {
  180. return [];
  181. }
  182. if (!isset($array['sabre']['address-book-plugins']) || !is_array($array['sabre']['address-book-plugins'])) {
  183. return [];
  184. }
  185. if (!isset($array['sabre']['address-book-plugins']['plugin'])) {
  186. return [];
  187. }
  188. $items = $array['sabre']['address-book-plugins']['plugin'];
  189. if (!is_array($items)) {
  190. $items = [$items];
  191. }
  192. return $items;
  193. }
  194. private function extractCalendarPluginList(array $array):array {
  195. if (isset($array['sabre']) && is_array($array['sabre'])) {
  196. if (isset($array['sabre']['calendar-plugins']) && is_array($array['sabre']['calendar-plugins'])) {
  197. if (isset($array['sabre']['calendar-plugins']['plugin'])) {
  198. $items = $array['sabre']['calendar-plugins']['plugin'];
  199. if (!is_array($items)) {
  200. $items = [$items];
  201. }
  202. return $items;
  203. }
  204. }
  205. }
  206. return [];
  207. }
  208. private function loadSabrePluginsFromInfoXml(array $plugins) {
  209. foreach ($plugins as $plugin) {
  210. try {
  211. $this->plugins[] = $this->container->query($plugin);
  212. } catch (QueryException $e) {
  213. if (class_exists($plugin)) {
  214. $this->plugins[] = new $plugin();
  215. } else {
  216. throw new \Exception("Sabre plugin class '$plugin' is unknown and could not be loaded");
  217. }
  218. }
  219. }
  220. }
  221. private function loadSabreCollectionsFromInfoXml(array $collections) {
  222. foreach ($collections as $collection) {
  223. try {
  224. $this->collections[] = $this->container->query($collection);
  225. } catch (QueryException $e) {
  226. if (class_exists($collection)) {
  227. $this->collections[] = new $collection();
  228. } else {
  229. throw new \Exception("Sabre collection class '$collection' is unknown and could not be loaded");
  230. }
  231. }
  232. }
  233. }
  234. private function createPluginInstance(string $className) {
  235. try {
  236. return $this->container->query($className);
  237. } catch (QueryException $e) {
  238. if (class_exists($className)) {
  239. return new $className();
  240. }
  241. }
  242. throw new \Exception("Sabre plugin class '$className' is unknown and could not be loaded");
  243. }
  244. /**
  245. * @param string[] $plugin
  246. */
  247. private function loadSabreAddressBookPluginsFromInfoXml(array $plugins): void {
  248. $providers = array_map(function(string $className): IAddressBookProvider {
  249. $instance = $this->createPluginInstance($className);
  250. if (!($instance instanceof IAddressBookProvider)) {
  251. throw new \Exception("Sabre address book plugin class '$className' does not implement the \OCA\DAV\CardDAV\Integration\IAddressBookProvider interface");
  252. }
  253. return $instance;
  254. }, $plugins);
  255. foreach ($providers as $provider) {
  256. $this->addressBookPlugins[] = $provider;
  257. }
  258. }
  259. private function loadSabreCalendarPluginsFromInfoXml(array $calendarPlugins):void {
  260. foreach ($calendarPlugins as $calendarPlugin) {
  261. try {
  262. $instantiatedCalendarPlugin = $this->container->query($calendarPlugin);
  263. } catch (QueryException $e) {
  264. if (class_exists($calendarPlugin)) {
  265. $instantiatedCalendarPlugin = new $calendarPlugin();
  266. } else {
  267. throw new \Exception("Sabre calendar-plugin class '$calendarPlugin' is unknown and could not be loaded");
  268. }
  269. }
  270. if (!($instantiatedCalendarPlugin instanceof ICalendarProvider)) {
  271. throw new \Exception("Sabre calendar-plugin class '$calendarPlugin' does not implement ICalendarProvider interface");
  272. }
  273. $this->calendarPlugins[] = $instantiatedCalendarPlugin;
  274. }
  275. }
  276. }