RepairMimeTypes.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Faruk Uzun <farukuzun@collabora.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Normal Ra <normalraw@gmail.com>
  9. * @author Olivier Paroz <github@oparoz.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Repair;
  31. use OCP\Migration\IOutput;
  32. use OCP\Migration\IRepairStep;
  33. class RepairMimeTypes implements IRepairStep {
  34. /**
  35. * @var \OCP\IConfig
  36. */
  37. protected $config;
  38. /**
  39. * @var int
  40. */
  41. protected $folderMimeTypeId;
  42. /**
  43. * @param \OCP\IConfig $config
  44. */
  45. public function __construct($config) {
  46. $this->config = $config;
  47. }
  48. public function getName() {
  49. return 'Repair mime types';
  50. }
  51. private static function existsStmt() {
  52. return \OC_DB::prepare('
  53. SELECT count(`mimetype`)
  54. FROM `*PREFIX*mimetypes`
  55. WHERE `mimetype` = ?
  56. ');
  57. }
  58. private static function getIdStmt() {
  59. return \OC_DB::prepare('
  60. SELECT `id`
  61. FROM `*PREFIX*mimetypes`
  62. WHERE `mimetype` = ?
  63. ');
  64. }
  65. private static function insertStmt() {
  66. return \OC_DB::prepare('
  67. INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
  68. VALUES ( ? )
  69. ');
  70. }
  71. private static function updateByNameStmt() {
  72. return \OC_DB::prepare('
  73. UPDATE `*PREFIX*filecache`
  74. SET `mimetype` = ?
  75. WHERE `mimetype` <> ? AND `mimetype` <> ? AND `name` ILIKE ?
  76. ');
  77. }
  78. private function updateMimetypes($updatedMimetypes) {
  79. if (empty($this->folderMimeTypeId)) {
  80. $result = \OC_DB::executeAudited(self::getIdStmt(), array('httpd/unix-directory'));
  81. $this->folderMimeTypeId = (int)$result->fetchOne();
  82. }
  83. foreach ($updatedMimetypes as $extension => $mimetype) {
  84. $result = \OC_DB::executeAudited(self::existsStmt(), array($mimetype));
  85. $exists = $result->fetchOne();
  86. if (!$exists) {
  87. // insert mimetype
  88. \OC_DB::executeAudited(self::insertStmt(), array($mimetype));
  89. }
  90. // get target mimetype id
  91. $result = \OC_DB::executeAudited(self::getIdStmt(), array($mimetype));
  92. $mimetypeId = $result->fetchOne();
  93. // change mimetype for files with x extension
  94. \OC_DB::executeAudited(self::updateByNameStmt(), array($mimetypeId, $this->folderMimeTypeId, $mimetypeId, '%.' . $extension));
  95. }
  96. }
  97. private function introduceWindowsProgramTypes() {
  98. $updatedMimetypes = array(
  99. 'htaccess' => 'text/plain',
  100. 'bat' => 'application/x-msdos-program',
  101. 'cmd' => 'application/cmd',
  102. );
  103. $this->updateMimetypes($updatedMimetypes);
  104. }
  105. /**
  106. * Fix mime types
  107. */
  108. public function run(IOutput $out) {
  109. $ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
  110. // NOTE TO DEVELOPERS: when adding new mime types, please make sure to
  111. // add a version comparison to avoid doing it every time
  112. if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) {
  113. $out->info('Fixed windows program mime types');
  114. }
  115. }
  116. }