1
0

Application.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. }
  120. if ($schema->hasTable('twofactor_providers')) {
  121. $table = $schema->getTable('twofactor_providers');
  122. if (!$table->hasIndex('twofactor_providers_uid')) {
  123. $subject->addHintForMissingSubject($table->getName(), 'twofactor_providers_uid');
  124. }
  125. }
  126. if ($schema->hasTable('login_flow_v2')) {
  127. $table = $schema->getTable('login_flow_v2');
  128. if (!$table->hasIndex('poll_token')) {
  129. $subject->addHintForMissingSubject($table->getName(), 'poll_token');
  130. }
  131. if (!$table->hasIndex('login_token')) {
  132. $subject->addHintForMissingSubject($table->getName(), 'login_token');
  133. }
  134. if (!$table->hasIndex('timestamp')) {
  135. $subject->addHintForMissingSubject($table->getName(), 'timestamp');
  136. }
  137. }
  138. if ($schema->hasTable('whats_new')) {
  139. $table = $schema->getTable('whats_new');
  140. if (!$table->hasIndex('version')) {
  141. $subject->addHintForMissingSubject($table->getName(), 'version');
  142. }
  143. }
  144. if ($schema->hasTable('cards')) {
  145. $table = $schema->getTable('cards');
  146. if (!$table->hasIndex('cards_abid')) {
  147. $subject->addHintForMissingSubject($table->getName(), 'cards_abid');
  148. }
  149. if (!$table->hasIndex('cards_abiduri')) {
  150. $subject->addHintForMissingSubject($table->getName(), 'cards_abiduri');
  151. }
  152. }
  153. if ($schema->hasTable('cards_properties')) {
  154. $table = $schema->getTable('cards_properties');
  155. if (!$table->hasIndex('cards_prop_abid')) {
  156. $subject->addHintForMissingSubject($table->getName(), 'cards_prop_abid');
  157. }
  158. }
  159. if ($schema->hasTable('calendarobjects_props')) {
  160. $table = $schema->getTable('calendarobjects_props');
  161. if (!$table->hasIndex('calendarobject_calid_index')) {
  162. $subject->addHintForMissingSubject($table->getName(), 'calendarobject_calid_index');
  163. }
  164. }
  165. if ($schema->hasTable('schedulingobjects')) {
  166. $table = $schema->getTable('schedulingobjects');
  167. if (!$table->hasIndex('schedulobj_principuri_index')) {
  168. $subject->addHintForMissingSubject($table->getName(), 'schedulobj_principuri_index');
  169. }
  170. }
  171. if ($schema->hasTable('properties')) {
  172. $table = $schema->getTable('properties');
  173. if (!$table->hasIndex('properties_path_index')) {
  174. $subject->addHintForMissingSubject($table->getName(), 'properties_path_index');
  175. }
  176. if (!$table->hasIndex('properties_pathonly_index')) {
  177. $subject->addHintForMissingSubject($table->getName(), 'properties_pathonly_index');
  178. }
  179. }
  180. if ($schema->hasTable('jobs')) {
  181. $table = $schema->getTable('jobs');
  182. if (!$table->hasIndex('job_lastcheck_reserved')) {
  183. $subject->addHintForMissingSubject($table->getName(), 'job_lastcheck_reserved');
  184. }
  185. }
  186. if ($schema->hasTable('direct_edit')) {
  187. $table = $schema->getTable('direct_edit');
  188. if (!$table->hasIndex('direct_edit_timestamp')) {
  189. $subject->addHintForMissingSubject($table->getName(), 'direct_edit_timestamp');
  190. }
  191. }
  192. if ($schema->hasTable('preferences')) {
  193. $table = $schema->getTable('preferences');
  194. if (!$table->hasIndex('preferences_app_key')) {
  195. $subject->addHintForMissingSubject($table->getName(), 'preferences_app_key');
  196. }
  197. }
  198. if ($schema->hasTable('mounts')) {
  199. $table = $schema->getTable('mounts');
  200. if (!$table->hasIndex('mounts_class_index')) {
  201. $subject->addHintForMissingSubject($table->getName(), 'mounts_class_index');
  202. }
  203. }
  204. }
  205. );
  206. $oldEventDispatcher->addListener(IDBConnection::CHECK_MISSING_PRIMARY_KEYS_EVENT,
  207. function (GenericEvent $event) use ($container) {
  208. /** @var MissingPrimaryKeyInformation $subject */
  209. $subject = $event->getSubject();
  210. $schema = new SchemaWrapper($container->query(Connection::class));
  211. if ($schema->hasTable('federated_reshares')) {
  212. $table = $schema->getTable('federated_reshares');
  213. if (!$table->hasPrimaryKey()) {
  214. $subject->addHintForMissingSubject($table->getName());
  215. }
  216. }
  217. if ($schema->hasTable('systemtag_object_mapping')) {
  218. $table = $schema->getTable('systemtag_object_mapping');
  219. if (!$table->hasPrimaryKey()) {
  220. $subject->addHintForMissingSubject($table->getName());
  221. }
  222. }
  223. if ($schema->hasTable('comments_read_markers')) {
  224. $table = $schema->getTable('comments_read_markers');
  225. if (!$table->hasPrimaryKey()) {
  226. $subject->addHintForMissingSubject($table->getName());
  227. }
  228. }
  229. if ($schema->hasTable('collres_resources')) {
  230. $table = $schema->getTable('collres_resources');
  231. if (!$table->hasPrimaryKey()) {
  232. $subject->addHintForMissingSubject($table->getName());
  233. }
  234. }
  235. if ($schema->hasTable('collres_accesscache')) {
  236. $table = $schema->getTable('collres_accesscache');
  237. if (!$table->hasPrimaryKey()) {
  238. $subject->addHintForMissingSubject($table->getName());
  239. }
  240. }
  241. if ($schema->hasTable('filecache_extended')) {
  242. $table = $schema->getTable('filecache_extended');
  243. if (!$table->hasPrimaryKey()) {
  244. $subject->addHintForMissingSubject($table->getName());
  245. }
  246. }
  247. }
  248. );
  249. $oldEventDispatcher->addListener(IDBConnection::CHECK_MISSING_COLUMNS_EVENT,
  250. function (GenericEvent $event) use ($container) {
  251. /** @var MissingColumnInformation $subject */
  252. $subject = $event->getSubject();
  253. $schema = new SchemaWrapper($container->query(Connection::class));
  254. if ($schema->hasTable('comments')) {
  255. $table = $schema->getTable('comments');
  256. if (!$table->hasColumn('reference_id')) {
  257. $subject->addHintForMissingColumn($table->getName(), 'reference_id');
  258. }
  259. }
  260. }
  261. );
  262. $eventDispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
  263. $eventDispatcher->addServiceListener(RemoteWipeStarted::class, RemoteWipeActivityListener::class);
  264. $eventDispatcher->addServiceListener(RemoteWipeStarted::class, RemoteWipeNotificationsListener::class);
  265. $eventDispatcher->addServiceListener(RemoteWipeStarted::class, RemoteWipeEmailListener::class);
  266. $eventDispatcher->addServiceListener(RemoteWipeFinished::class, RemoteWipeActivityListener::class);
  267. $eventDispatcher->addServiceListener(RemoteWipeFinished::class, RemoteWipeNotificationsListener::class);
  268. $eventDispatcher->addServiceListener(RemoteWipeFinished::class, RemoteWipeEmailListener::class);
  269. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedStoreCleanupListener::class);
  270. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedTokenCleanupListener::class);
  271. $eventDispatcher->addServiceListener(BeforeUserDeletedEvent::class, UserDeletedFilesCleanupListener::class);
  272. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedFilesCleanupListener::class);
  273. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedWebAuthnCleanupListener::class);
  274. // Metadata
  275. /** @var IConfig $config */
  276. $config = $container->get(IConfig::class);
  277. if ($config->getSystemValueBool('enable_file_metadata', true)) {
  278. /** @psalm-suppress InvalidArgument */
  279. $eventDispatcher->addServiceListener(NodeDeletedEvent::class, FileEventListener::class);
  280. /** @psalm-suppress InvalidArgument */
  281. $eventDispatcher->addServiceListener(NodeRemovedFromCache::class, FileEventListener::class);
  282. /** @psalm-suppress InvalidArgument */
  283. $eventDispatcher->addServiceListener(NodeWrittenEvent::class, FileEventListener::class);
  284. }
  285. // Tags
  286. $eventDispatcher->addServiceListener(UserDeletedEvent::class, TagManager::class);
  287. }
  288. }