2014-06-25 17:34:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-26 10:37:37 +00:00
|
|
|
* Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
* Copyright (c) 2014 Jörn Dreyer jfd@owncloud.com
|
|
|
|
* Copyright (c) 2014 Olivier Paroz owncloud@oparoz.com
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
2014-06-25 17:34:39 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-06-25 17:34:39 +00:00
|
|
|
namespace OC\Repair;
|
|
|
|
|
|
|
|
use OC\Hooks\BasicEmitter;
|
|
|
|
|
|
|
|
class RepairMimeTypes extends BasicEmitter implements \OC\RepairStep {
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return 'Repair mime types';
|
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private static function existsStmt() {
|
|
|
|
return \OC_DB::prepare('
|
2014-06-26 14:40:12 +00:00
|
|
|
SELECT count(`mimetype`)
|
|
|
|
FROM `*PREFIX*mimetypes`
|
2014-06-25 17:34:39 +00:00
|
|
|
WHERE `mimetype` = ?
|
|
|
|
');
|
2014-11-26 18:58:59 +00:00
|
|
|
}
|
2014-06-25 17:34:39 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private static function getIdStmt() {
|
|
|
|
return \OC_DB::prepare('
|
2014-06-26 14:40:12 +00:00
|
|
|
SELECT `id`
|
|
|
|
FROM `*PREFIX*mimetypes`
|
|
|
|
WHERE `mimetype` = ?
|
|
|
|
');
|
2014-11-26 18:58:59 +00:00
|
|
|
}
|
2014-06-26 14:40:12 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private static function insertStmt() {
|
|
|
|
return \OC_DB::prepare('
|
2014-06-26 14:40:12 +00:00
|
|
|
INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
|
|
|
|
VALUES ( ? )
|
|
|
|
');
|
2014-11-26 18:58:59 +00:00
|
|
|
}
|
2014-06-26 14:40:12 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private static function updateWrongStmt() {
|
|
|
|
return \OC_DB::prepare('
|
2014-06-26 14:40:12 +00:00
|
|
|
UPDATE `*PREFIX*filecache`
|
|
|
|
SET `mimetype` = (
|
|
|
|
SELECT `id`
|
|
|
|
FROM `*PREFIX*mimetypes`
|
|
|
|
WHERE `mimetype` = ?
|
|
|
|
) WHERE `mimetype` = ?
|
|
|
|
');
|
2014-11-26 18:58:59 +00:00
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private static function deleteStmt() {
|
|
|
|
return \OC_DB::prepare('
|
2014-06-26 14:40:12 +00:00
|
|
|
DELETE FROM `*PREFIX*mimetypes`
|
|
|
|
WHERE `id` = ?
|
|
|
|
');
|
2015-03-20 21:11:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private static function updateByNameStmt() {
|
|
|
|
return \OC_DB::prepare('
|
|
|
|
UPDATE `*PREFIX*filecache`
|
|
|
|
SET `mimetype` = (
|
|
|
|
SELECT `id`
|
|
|
|
FROM `*PREFIX*mimetypes`
|
|
|
|
WHERE `mimetype` = ?
|
2015-03-17 10:05:51 +00:00
|
|
|
) WHERE `name` ILIKE ?
|
2014-11-26 18:58:59 +00:00
|
|
|
');
|
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private function repairMimetypes($wrongMimetypes) {
|
2014-06-25 17:34:39 +00:00
|
|
|
foreach ($wrongMimetypes as $wrong => $correct) {
|
2014-06-26 14:40:12 +00:00
|
|
|
// do we need to remove a wrong mimetype?
|
2014-11-26 18:58:59 +00:00
|
|
|
$result = \OC_DB::executeAudited(self::getIdStmt(), array($wrong));
|
2014-06-26 14:40:12 +00:00
|
|
|
$wrongId = $result->fetchOne();
|
|
|
|
|
|
|
|
if ($wrongId !== false) {
|
|
|
|
// do we need to insert the correct mimetype?
|
2014-11-26 18:58:59 +00:00
|
|
|
$result = \OC_DB::executeAudited(self::existsStmt(), array($correct));
|
2014-06-26 14:40:12 +00:00
|
|
|
$exists = $result->fetchOne();
|
|
|
|
|
2015-03-20 21:11:42 +00:00
|
|
|
if (!is_null($correct)) {
|
|
|
|
if (!$exists) {
|
2014-11-26 18:58:59 +00:00
|
|
|
// insert mimetype
|
|
|
|
\OC_DB::executeAudited(self::insertStmt(), array($correct));
|
|
|
|
}
|
2014-06-26 14:40:12 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
// change wrong mimetype to correct mimetype in filecache
|
|
|
|
\OC_DB::executeAudited(self::updateWrongStmt(), array($correct, $wrongId));
|
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-06-26 14:40:12 +00:00
|
|
|
// delete wrong mimetype
|
2014-11-26 18:58:59 +00:00
|
|
|
\OC_DB::executeAudited(self::deleteStmt(), array($wrongId));
|
2014-06-26 14:40:12 +00:00
|
|
|
|
|
|
|
}
|
2014-06-25 17:34:39 +00:00
|
|
|
}
|
2014-11-26 18:58:59 +00:00
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private function updateMimetypes($updatedMimetypes) {
|
2015-03-20 21:11:42 +00:00
|
|
|
|
|
|
|
foreach ($updatedMimetypes as $extension => $mimetype) {
|
2014-11-26 18:58:59 +00:00
|
|
|
$result = \OC_DB::executeAudited(self::existsStmt(), array($mimetype));
|
2014-06-25 17:34:39 +00:00
|
|
|
$exists = $result->fetchOne();
|
|
|
|
|
2015-03-20 21:11:42 +00:00
|
|
|
if (!$exists) {
|
2014-06-25 17:34:39 +00:00
|
|
|
// insert mimetype
|
2014-11-26 18:58:59 +00:00
|
|
|
\OC_DB::executeAudited(self::insertStmt(), array($mimetype));
|
2014-06-25 17:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// change mimetype for files with x extension
|
2015-03-20 21:11:42 +00:00
|
|
|
\OC_DB::executeAudited(self::updateByNameStmt(), array($mimetype, '%.' . $extension));
|
2014-06-25 17:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private function fixOfficeMimeTypes() {
|
|
|
|
// update wrong mimetypes
|
|
|
|
$wrongMimetypes = array(
|
|
|
|
'application/mspowerpoint' => 'application/vnd.ms-powerpoint',
|
|
|
|
'application/msexcel' => 'application/vnd.ms-excel',
|
|
|
|
);
|
2014-08-12 12:07:10 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
self::repairMimetypes($wrongMimetypes);
|
2014-08-12 12:07:10 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
$updatedMimetypes = array(
|
|
|
|
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
|
|
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
|
|
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
|
|
);
|
2014-08-12 12:07:10 +00:00
|
|
|
|
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
// separate doc from docx etc
|
|
|
|
self::updateMimetypes($updatedMimetypes);
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private function fixApkMimeType() {
|
|
|
|
$updatedMimetypes = array(
|
|
|
|
'apk' => 'application/vnd.android.package-archive',
|
|
|
|
);
|
2014-08-12 12:07:10 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
self::updateMimetypes($updatedMimetypes);
|
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private function fixFontsMimeTypes() {
|
|
|
|
// update wrong mimetypes
|
|
|
|
$wrongMimetypes = array(
|
|
|
|
'font' => null,
|
|
|
|
'font/opentype' => 'application/font-sfnt',
|
|
|
|
'application/x-font-ttf' => 'application/font-sfnt',
|
|
|
|
);
|
2014-08-12 12:07:10 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
self::repairMimetypes($wrongMimetypes);
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
$updatedMimetypes = array(
|
|
|
|
'ttf' => 'application/font-sfnt',
|
|
|
|
'otf' => 'application/font-sfnt',
|
|
|
|
'pfb' => 'application/x-font',
|
|
|
|
);
|
2014-08-12 12:07:10 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
self::updateMimetypes($updatedMimetypes);
|
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
private function fixPostscriptMimeType() {
|
|
|
|
$updatedMimetypes = array(
|
|
|
|
'eps' => 'application/postscript',
|
|
|
|
'ps' => 'application/postscript',
|
|
|
|
);
|
2014-08-12 12:07:10 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
self::updateMimetypes($updatedMimetypes);
|
2014-08-12 12:07:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 15:25:04 +00:00
|
|
|
private function introduceRawMimeType() {
|
|
|
|
$updatedMimetypes = array(
|
|
|
|
'arw' => 'image/x-dcraw',
|
|
|
|
'cr2' => 'image/x-dcraw',
|
|
|
|
'dcr' => 'image/x-dcraw',
|
|
|
|
'dng' => 'image/x-dcraw',
|
|
|
|
'erf' => 'image/x-dcraw',
|
|
|
|
'iiq' => 'image/x-dcraw',
|
|
|
|
'k25' => 'image/x-dcraw',
|
|
|
|
'kdc' => 'image/x-dcraw',
|
|
|
|
'mef' => 'image/x-dcraw',
|
|
|
|
'nef' => 'image/x-dcraw',
|
|
|
|
'orf' => 'image/x-dcraw',
|
|
|
|
'pef' => 'image/x-dcraw',
|
|
|
|
'raf' => 'image/x-dcraw',
|
|
|
|
'rw2' => 'image/x-dcraw',
|
|
|
|
'srf' => 'image/x-dcraw',
|
|
|
|
'sr2' => 'image/x-dcraw',
|
|
|
|
'xrf' => 'image/x-dcraw',
|
|
|
|
);
|
|
|
|
|
|
|
|
self::updateMimetypes($updatedMimetypes);
|
|
|
|
}
|
|
|
|
|
2015-03-20 21:11:42 +00:00
|
|
|
private function introduce3dImagesMimeType() {
|
|
|
|
$updatedMimetypes = array(
|
|
|
|
'jps' => 'image/jpeg',
|
|
|
|
'mpo' => 'image/jpeg',
|
|
|
|
);
|
|
|
|
|
|
|
|
self::updateMimetypes($updatedMimetypes);
|
|
|
|
}
|
|
|
|
|
2014-06-25 17:34:39 +00:00
|
|
|
/**
|
|
|
|
* Fix mime types
|
|
|
|
*/
|
|
|
|
public function run() {
|
|
|
|
if ($this->fixOfficeMimeTypes()) {
|
|
|
|
$this->emit('\OC\Repair', 'info', array('Fixed office mime types'));
|
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
if ($this->fixApkMimeType()) {
|
2014-08-12 12:07:10 +00:00
|
|
|
$this->emit('\OC\Repair', 'info', array('Fixed APK mime type'));
|
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
if ($this->fixFontsMimeTypes()) {
|
|
|
|
$this->emit('\OC\Repair', 'info', array('Fixed fonts mime types'));
|
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
2014-11-26 18:58:59 +00:00
|
|
|
if ($this->fixPostscriptMimeType()) {
|
|
|
|
$this->emit('\OC\Repair', 'info', array('Fixed Postscript mime types'));
|
|
|
|
}
|
2015-03-16 15:25:04 +00:00
|
|
|
|
|
|
|
if ($this->introduceRawMimeType()) {
|
|
|
|
$this->emit('\OC\Repair', 'info', array('Fixed Raw mime types'));
|
|
|
|
}
|
2015-03-20 21:11:42 +00:00
|
|
|
|
|
|
|
if ($this->introduce3dImagesMimeType()) {
|
|
|
|
$this->emit('\OC\Repair', 'info', array('Fixed 3D images mime types'));
|
|
|
|
}
|
2014-06-25 17:34:39 +00:00
|
|
|
}
|
2015-03-16 15:25:04 +00:00
|
|
|
}
|