Application.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Mario Danic <mario@lovelyhq.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Citharel <nextcloud@tcit.fr>
  16. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OC\Core;
  34. use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
  35. use OC\Authentication\Events\RemoteWipeFinished;
  36. use OC\Authentication\Events\RemoteWipeStarted;
  37. use OC\Authentication\Listeners\RemoteWipeActivityListener;
  38. use OC\Authentication\Listeners\RemoteWipeEmailListener;
  39. use OC\Authentication\Listeners\RemoteWipeNotificationsListener;
  40. use OC\Authentication\Listeners\UserDeletedFilesCleanupListener;
  41. use OC\Authentication\Listeners\UserDeletedStoreCleanupListener;
  42. use OC\Authentication\Listeners\UserDeletedTokenCleanupListener;
  43. use OC\Authentication\Listeners\UserDeletedWebAuthnCleanupListener;
  44. use OC\Authentication\Notifications\Notifier as AuthenticationNotifier;
  45. use OC\Core\Listener\BeforeTemplateRenderedListener;
  46. use OC\Core\Notification\CoreNotifier;
  47. use OC\DB\Connection;
  48. use OC\DB\MissingColumnInformation;
  49. use OC\DB\MissingIndexInformation;
  50. use OC\DB\MissingPrimaryKeyInformation;
  51. use OC\DB\SchemaWrapper;
  52. use OC\Metadata\FileEventListener;
  53. use OC\TagManager;
  54. use OCP\AppFramework\App;
  55. use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
  56. use OCP\EventDispatcher\IEventDispatcher;
  57. use OCP\Files\Events\Node\NodeDeletedEvent;
  58. use OCP\Files\Events\Node\NodeWrittenEvent;
  59. use OCP\Files\Events\NodeRemovedFromCache;
  60. use OCP\IDBConnection;
  61. use OCP\User\Events\BeforeUserDeletedEvent;
  62. use OCP\User\Events\UserDeletedEvent;
  63. use OCP\Util;
  64. use OCP\IConfig;
  65. use Symfony\Component\EventDispatcher\GenericEvent;
  66. /**
  67. * Class Application
  68. *
  69. * @package OC\Core
  70. */
  71. class Application extends App {
  72. public function __construct() {
  73. parent::__construct('core');
  74. $container = $this->getContainer();
  75. $container->registerService('defaultMailAddress', function () {
  76. return Util::getDefaultEmailAddress('lostpassword-noreply');
  77. });
  78. $server = $container->getServer();
  79. /** @var IEventDispatcher $eventDispatcher */
  80. $eventDispatcher = $server->get(IEventDispatcher::class);
  81. $notificationManager = $server->getNotificationManager();
  82. $notificationManager->registerNotifierService(CoreNotifier::class);
  83. $notificationManager->registerNotifierService(AuthenticationNotifier::class);
  84. $oldEventDispatcher = $server->getEventDispatcher();
  85. $oldEventDispatcher->addListener(IDBConnection::CHECK_MISSING_INDEXES_EVENT,
  86. function (GenericEvent $event) use ($container) {
  87. /** @var MissingIndexInformation $subject */
  88. $subject = $event->getSubject();
  89. $schema = new SchemaWrapper($container->query(Connection::class));
  90. if ($schema->hasTable('share')) {
  91. $table = $schema->getTable('share');
  92. if (!$table->hasIndex('share_with_index')) {
  93. $subject->addHintForMissingSubject($table->getName(), 'share_with_index');
  94. }
  95. if (!$table->hasIndex('parent_index')) {
  96. $subject->addHintForMissingSubject($table->getName(), 'parent_index');
  97. }
  98. if (!$table->hasIndex('owner_index')) {
  99. $subject->addHintForMissingSubject($table->getName(), 'owner_index');
  100. }
  101. if (!$table->hasIndex('initiator_index')) {
  102. $subject->addHintForMissingSubject($table->getName(), 'initiator_index');
  103. }
  104. }
  105. if ($schema->hasTable('filecache')) {
  106. $table = $schema->getTable('filecache');
  107. if (!$table->hasIndex('fs_mtime')) {
  108. $subject->addHintForMissingSubject($table->getName(), 'fs_mtime');
  109. }
  110. if (!$table->hasIndex('fs_size')) {
  111. $subject->addHintForMissingSubject($table->getName(), 'fs_size');
  112. }
  113. if (!$table->hasIndex('fs_id_storage_size')) {
  114. $subject->addHintForMissingSubject($table->getName(), 'fs_id_storage_size');
  115. }
  116. if (!$table->hasIndex('fs_storage_path_prefix') && !$schema->getDatabasePlatform() instanceof PostgreSQL94Platform) {
  117. $subject->addHintForMissingSubject($table->getName(), 'fs_storage_path_prefix');
  118. }
  119. if (!$table->hasIndex('fs_parent')) {
  120. $subject->addHintForMissingSubject($table->getName(), 'fs_parent');
  121. }
  122. }
  123. if ($schema->hasTable('twofactor_providers')) {
  124. $table = $schema->getTable('twofactor_providers');
  125. if (!$table->hasIndex('twofactor_providers_uid')) {
  126. $subject->addHintForMissingSubject($table->getName(), 'twofactor_providers_uid');
  127. }
  128. }
  129. if ($schema->hasTable('login_flow_v2')) {
  130. $table = $schema->getTable('login_flow_v2');
  131. if (!$table->hasIndex('poll_token')) {
  132. $subject->addHintForMissingSubject($table->getName(), 'poll_token');
  133. }
  134. if (!$table->hasIndex('login_token')) {
  135. $subject->addHintForMissingSubject($table->getName(), 'login_token');
  136. }
  137. if (!$table->hasIndex('timestamp')) {
  138. $subject->addHintForMissingSubject($table->getName(), 'timestamp');
  139. }
  140. }
  141. if ($schema->hasTable('whats_new')) {
  142. $table = $schema->getTable('whats_new');
  143. if (!$table->hasIndex('version')) {
  144. $subject->addHintForMissingSubject($table->getName(), 'version');
  145. }
  146. }
  147. if ($schema->hasTable('cards')) {
  148. $table = $schema->getTable('cards');
  149. if (!$table->hasIndex('cards_abid')) {
  150. $subject->addHintForMissingSubject($table->getName(), 'cards_abid');
  151. }
  152. if (!$table->hasIndex('cards_abiduri')) {
  153. $subject->addHintForMissingSubject($table->getName(), 'cards_abiduri');
  154. }
  155. }
  156. if ($schema->hasTable('cards_properties')) {
  157. $table = $schema->getTable('cards_properties');
  158. if (!$table->hasIndex('cards_prop_abid')) {
  159. $subject->addHintForMissingSubject($table->getName(), 'cards_prop_abid');
  160. }
  161. }
  162. if ($schema->hasTable('calendarobjects_props')) {
  163. $table = $schema->getTable('calendarobjects_props');
  164. if (!$table->hasIndex('calendarobject_calid_index')) {
  165. $subject->addHintForMissingSubject($table->getName(), 'calendarobject_calid_index');
  166. }
  167. }
  168. if ($schema->hasTable('schedulingobjects')) {
  169. $table = $schema->getTable('schedulingobjects');
  170. if (!$table->hasIndex('schedulobj_principuri_index')) {
  171. $subject->addHintForMissingSubject($table->getName(), 'schedulobj_principuri_index');
  172. }
  173. $table = $schema->getTable('schedulingobjects');
  174. if (!$table->hasIndex('schedulobj_lastmodified_idx')) {
  175. $subject->addHintForMissingSubject($table->getName(), 'schedulobj_lastmodified_idx');
  176. }
  177. }
  178. if ($schema->hasTable('properties')) {
  179. $table = $schema->getTable('properties');
  180. if (!$table->hasIndex('properties_path_index')) {
  181. $subject->addHintForMissingSubject($table->getName(), 'properties_path_index');
  182. }
  183. if (!$table->hasIndex('properties_pathonly_index')) {
  184. $subject->addHintForMissingSubject($table->getName(), 'properties_pathonly_index');
  185. }
  186. }
  187. if ($schema->hasTable('jobs')) {
  188. $table = $schema->getTable('jobs');
  189. if (!$table->hasIndex('job_lastcheck_reserved')) {
  190. $subject->addHintForMissingSubject($table->getName(), 'job_lastcheck_reserved');
  191. }
  192. }
  193. if ($schema->hasTable('direct_edit')) {
  194. $table = $schema->getTable('direct_edit');
  195. if (!$table->hasIndex('direct_edit_timestamp')) {
  196. $subject->addHintForMissingSubject($table->getName(), 'direct_edit_timestamp');
  197. }
  198. }
  199. if ($schema->hasTable('preferences')) {
  200. $table = $schema->getTable('preferences');
  201. if (!$table->hasIndex('preferences_app_key')) {
  202. $subject->addHintForMissingSubject($table->getName(), 'preferences_app_key');
  203. }
  204. }
  205. if ($schema->hasTable('mounts')) {
  206. $table = $schema->getTable('mounts');
  207. if (!$table->hasIndex('mounts_class_index')) {
  208. $subject->addHintForMissingSubject($table->getName(), 'mounts_class_index');
  209. }
  210. if (!$table->hasIndex('mounts_user_root_path_index')) {
  211. $subject->addHintForMissingSubject($table->getName(), 'mounts_user_root_path_index');
  212. }
  213. }
  214. if ($schema->hasTable('systemtag_object_mapping')) {
  215. $table = $schema->getTable('systemtag_object_mapping');
  216. if (!$table->hasIndex('systag_by_tagid')) {
  217. $subject->addHintForMissingSubject($table->getName(), 'systag_by_tagid');
  218. }
  219. }
  220. }
  221. );
  222. $oldEventDispatcher->addListener(IDBConnection::CHECK_MISSING_PRIMARY_KEYS_EVENT,
  223. function (GenericEvent $event) use ($container) {
  224. /** @var MissingPrimaryKeyInformation $subject */
  225. $subject = $event->getSubject();
  226. $schema = new SchemaWrapper($container->query(Connection::class));
  227. if ($schema->hasTable('federated_reshares')) {
  228. $table = $schema->getTable('federated_reshares');
  229. if (!$table->hasPrimaryKey()) {
  230. $subject->addHintForMissingSubject($table->getName());
  231. }
  232. }
  233. if ($schema->hasTable('systemtag_object_mapping')) {
  234. $table = $schema->getTable('systemtag_object_mapping');
  235. if (!$table->hasPrimaryKey()) {
  236. $subject->addHintForMissingSubject($table->getName());
  237. }
  238. }
  239. if ($schema->hasTable('comments_read_markers')) {
  240. $table = $schema->getTable('comments_read_markers');
  241. if (!$table->hasPrimaryKey()) {
  242. $subject->addHintForMissingSubject($table->getName());
  243. }
  244. }
  245. if ($schema->hasTable('collres_resources')) {
  246. $table = $schema->getTable('collres_resources');
  247. if (!$table->hasPrimaryKey()) {
  248. $subject->addHintForMissingSubject($table->getName());
  249. }
  250. }
  251. if ($schema->hasTable('collres_accesscache')) {
  252. $table = $schema->getTable('collres_accesscache');
  253. if (!$table->hasPrimaryKey()) {
  254. $subject->addHintForMissingSubject($table->getName());
  255. }
  256. }
  257. if ($schema->hasTable('filecache_extended')) {
  258. $table = $schema->getTable('filecache_extended');
  259. if (!$table->hasPrimaryKey()) {
  260. $subject->addHintForMissingSubject($table->getName());
  261. }
  262. }
  263. }
  264. );
  265. $oldEventDispatcher->addListener(IDBConnection::CHECK_MISSING_COLUMNS_EVENT,
  266. function (GenericEvent $event) use ($container) {
  267. /** @var MissingColumnInformation $subject */
  268. $subject = $event->getSubject();
  269. $schema = new SchemaWrapper($container->query(Connection::class));
  270. if ($schema->hasTable('comments')) {
  271. $table = $schema->getTable('comments');
  272. if (!$table->hasColumn('reference_id')) {
  273. $subject->addHintForMissingColumn($table->getName(), 'reference_id');
  274. }
  275. }
  276. }
  277. );
  278. $eventDispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
  279. $eventDispatcher->addServiceListener(RemoteWipeStarted::class, RemoteWipeActivityListener::class);
  280. $eventDispatcher->addServiceListener(RemoteWipeStarted::class, RemoteWipeNotificationsListener::class);
  281. $eventDispatcher->addServiceListener(RemoteWipeStarted::class, RemoteWipeEmailListener::class);
  282. $eventDispatcher->addServiceListener(RemoteWipeFinished::class, RemoteWipeActivityListener::class);
  283. $eventDispatcher->addServiceListener(RemoteWipeFinished::class, RemoteWipeNotificationsListener::class);
  284. $eventDispatcher->addServiceListener(RemoteWipeFinished::class, RemoteWipeEmailListener::class);
  285. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedStoreCleanupListener::class);
  286. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedTokenCleanupListener::class);
  287. $eventDispatcher->addServiceListener(BeforeUserDeletedEvent::class, UserDeletedFilesCleanupListener::class);
  288. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedFilesCleanupListener::class);
  289. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedWebAuthnCleanupListener::class);
  290. // Metadata
  291. /** @var IConfig $config */
  292. $config = $container->get(IConfig::class);
  293. if ($config->getSystemValueBool('enable_file_metadata', true)) {
  294. /** @psalm-suppress InvalidArgument */
  295. $eventDispatcher->addServiceListener(NodeDeletedEvent::class, FileEventListener::class);
  296. /** @psalm-suppress InvalidArgument */
  297. $eventDispatcher->addServiceListener(NodeRemovedFromCache::class, FileEventListener::class);
  298. /** @psalm-suppress InvalidArgument */
  299. $eventDispatcher->addServiceListener(NodeWrittenEvent::class, FileEventListener::class);
  300. }
  301. // Tags
  302. $eventDispatcher->addServiceListener(UserDeletedEvent::class, TagManager::class);
  303. }
  304. }