Application.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. }
  174. if ($schema->hasTable('properties')) {
  175. $table = $schema->getTable('properties');
  176. if (!$table->hasIndex('properties_path_index')) {
  177. $subject->addHintForMissingSubject($table->getName(), 'properties_path_index');
  178. }
  179. if (!$table->hasIndex('properties_pathonly_index')) {
  180. $subject->addHintForMissingSubject($table->getName(), 'properties_pathonly_index');
  181. }
  182. }
  183. if ($schema->hasTable('jobs')) {
  184. $table = $schema->getTable('jobs');
  185. if (!$table->hasIndex('job_lastcheck_reserved')) {
  186. $subject->addHintForMissingSubject($table->getName(), 'job_lastcheck_reserved');
  187. }
  188. }
  189. if ($schema->hasTable('direct_edit')) {
  190. $table = $schema->getTable('direct_edit');
  191. if (!$table->hasIndex('direct_edit_timestamp')) {
  192. $subject->addHintForMissingSubject($table->getName(), 'direct_edit_timestamp');
  193. }
  194. }
  195. if ($schema->hasTable('preferences')) {
  196. $table = $schema->getTable('preferences');
  197. if (!$table->hasIndex('preferences_app_key')) {
  198. $subject->addHintForMissingSubject($table->getName(), 'preferences_app_key');
  199. }
  200. }
  201. if ($schema->hasTable('mounts')) {
  202. $table = $schema->getTable('mounts');
  203. if (!$table->hasIndex('mounts_class_index')) {
  204. $subject->addHintForMissingSubject($table->getName(), 'mounts_class_index');
  205. }
  206. if (!$table->hasIndex('mounts_user_root_path_index')) {
  207. $subject->addHintForMissingSubject($table->getName(), 'mounts_user_root_path_index');
  208. }
  209. }
  210. if ($schema->hasTable('systemtag_object_mapping')) {
  211. $table = $schema->getTable('systemtag_object_mapping');
  212. if (!$table->hasIndex('systag_by_tagid')) {
  213. $subject->addHintForMissingSubject($table->getName(), 'systag_by_tagid');
  214. }
  215. }
  216. }
  217. );
  218. $oldEventDispatcher->addListener(IDBConnection::CHECK_MISSING_PRIMARY_KEYS_EVENT,
  219. function (GenericEvent $event) use ($container) {
  220. /** @var MissingPrimaryKeyInformation $subject */
  221. $subject = $event->getSubject();
  222. $schema = new SchemaWrapper($container->query(Connection::class));
  223. if ($schema->hasTable('federated_reshares')) {
  224. $table = $schema->getTable('federated_reshares');
  225. if (!$table->hasPrimaryKey()) {
  226. $subject->addHintForMissingSubject($table->getName());
  227. }
  228. }
  229. if ($schema->hasTable('systemtag_object_mapping')) {
  230. $table = $schema->getTable('systemtag_object_mapping');
  231. if (!$table->hasPrimaryKey()) {
  232. $subject->addHintForMissingSubject($table->getName());
  233. }
  234. }
  235. if ($schema->hasTable('comments_read_markers')) {
  236. $table = $schema->getTable('comments_read_markers');
  237. if (!$table->hasPrimaryKey()) {
  238. $subject->addHintForMissingSubject($table->getName());
  239. }
  240. }
  241. if ($schema->hasTable('collres_resources')) {
  242. $table = $schema->getTable('collres_resources');
  243. if (!$table->hasPrimaryKey()) {
  244. $subject->addHintForMissingSubject($table->getName());
  245. }
  246. }
  247. if ($schema->hasTable('collres_accesscache')) {
  248. $table = $schema->getTable('collres_accesscache');
  249. if (!$table->hasPrimaryKey()) {
  250. $subject->addHintForMissingSubject($table->getName());
  251. }
  252. }
  253. if ($schema->hasTable('filecache_extended')) {
  254. $table = $schema->getTable('filecache_extended');
  255. if (!$table->hasPrimaryKey()) {
  256. $subject->addHintForMissingSubject($table->getName());
  257. }
  258. }
  259. }
  260. );
  261. $oldEventDispatcher->addListener(IDBConnection::CHECK_MISSING_COLUMNS_EVENT,
  262. function (GenericEvent $event) use ($container) {
  263. /** @var MissingColumnInformation $subject */
  264. $subject = $event->getSubject();
  265. $schema = new SchemaWrapper($container->query(Connection::class));
  266. if ($schema->hasTable('comments')) {
  267. $table = $schema->getTable('comments');
  268. if (!$table->hasColumn('reference_id')) {
  269. $subject->addHintForMissingColumn($table->getName(), 'reference_id');
  270. }
  271. }
  272. }
  273. );
  274. $eventDispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
  275. $eventDispatcher->addServiceListener(RemoteWipeStarted::class, RemoteWipeActivityListener::class);
  276. $eventDispatcher->addServiceListener(RemoteWipeStarted::class, RemoteWipeNotificationsListener::class);
  277. $eventDispatcher->addServiceListener(RemoteWipeStarted::class, RemoteWipeEmailListener::class);
  278. $eventDispatcher->addServiceListener(RemoteWipeFinished::class, RemoteWipeActivityListener::class);
  279. $eventDispatcher->addServiceListener(RemoteWipeFinished::class, RemoteWipeNotificationsListener::class);
  280. $eventDispatcher->addServiceListener(RemoteWipeFinished::class, RemoteWipeEmailListener::class);
  281. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedStoreCleanupListener::class);
  282. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedTokenCleanupListener::class);
  283. $eventDispatcher->addServiceListener(BeforeUserDeletedEvent::class, UserDeletedFilesCleanupListener::class);
  284. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedFilesCleanupListener::class);
  285. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedWebAuthnCleanupListener::class);
  286. // Metadata
  287. /** @var IConfig $config */
  288. $config = $container->get(IConfig::class);
  289. if ($config->getSystemValueBool('enable_file_metadata', true)) {
  290. /** @psalm-suppress InvalidArgument */
  291. $eventDispatcher->addServiceListener(NodeDeletedEvent::class, FileEventListener::class);
  292. /** @psalm-suppress InvalidArgument */
  293. $eventDispatcher->addServiceListener(NodeRemovedFromCache::class, FileEventListener::class);
  294. /** @psalm-suppress InvalidArgument */
  295. $eventDispatcher->addServiceListener(NodeWrittenEvent::class, FileEventListener::class);
  296. }
  297. // Tags
  298. $eventDispatcher->addServiceListener(UserDeletedEvent::class, TagManager::class);
  299. }
  300. }