nextcloud/lib/repair/repairmimetypes.php

299 lines
7.7 KiB
PHP
Raw Normal View History

<?php
/**
2015-06-25 12:43:55 +03:00
* @author Joas Schilling <nickvergessen@owncloud.com>
2015-03-26 13:44:34 +03:00
* @author Morris Jobke <hey@morrisjobke.de>
* @author Normal Ra <normalraw@gmail.com>
* @author Olivier Paroz <github@oparoz.com>
2015-06-25 12:43:55 +03:00
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
2015-03-26 13:44:34 +03:00
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Repair;
use OC\Hooks\BasicEmitter;
class RepairMimeTypes extends BasicEmitter implements \OC\RepairStep {
/**
* @var \OCP\IConfig
*/
protected $config;
/**
* @param \OCP\IConfig $config
*/
public function __construct($config) {
$this->config = $config;
}
public function getName() {
return 'Repair mime types';
}
private static function existsStmt() {
return \OC_DB::prepare('
2014-06-26 18:40:12 +04:00
SELECT count(`mimetype`)
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
');
}
private static function getIdStmt() {
return \OC_DB::prepare('
2014-06-26 18:40:12 +04:00
SELECT `id`
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
');
}
2014-06-26 18:40:12 +04:00
private static function insertStmt() {
return \OC_DB::prepare('
2014-06-26 18:40:12 +04:00
INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
VALUES ( ? )
');
}
2014-06-26 18:40:12 +04:00
private static function updateWrongStmt() {
return \OC_DB::prepare('
2014-06-26 18:40:12 +04:00
UPDATE `*PREFIX*filecache`
SET `mimetype` = (
SELECT `id`
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
) WHERE `mimetype` = ?
');
}
private static function deleteStmt() {
return \OC_DB::prepare('
2014-06-26 18:40:12 +04:00
DELETE FROM `*PREFIX*mimetypes`
WHERE `id` = ?
');
}
private static function updateByNameStmt() {
return \OC_DB::prepare('
UPDATE `*PREFIX*filecache`
2015-06-09 01:02:26 +03:00
SET `mimetype` = ?
WHERE `mimetype` <> ? AND `name` ILIKE ?
');
}
private function repairMimetypes($wrongMimetypes) {
foreach ($wrongMimetypes as $wrong => $correct) {
2014-06-26 18:40:12 +04:00
// do we need to remove a wrong mimetype?
$result = \OC_DB::executeAudited(self::getIdStmt(), array($wrong));
2014-06-26 18:40:12 +04:00
$wrongId = $result->fetchOne();
if ($wrongId !== false) {
// do we need to insert the correct mimetype?
$result = \OC_DB::executeAudited(self::existsStmt(), array($correct));
2014-06-26 18:40:12 +04:00
$exists = $result->fetchOne();
if (!is_null($correct)) {
if (!$exists) {
// insert mimetype
\OC_DB::executeAudited(self::insertStmt(), array($correct));
}
2014-06-26 18:40:12 +04:00
// change wrong mimetype to correct mimetype in filecache
\OC_DB::executeAudited(self::updateWrongStmt(), array($correct, $wrongId));
}
2014-06-26 18:40:12 +04:00
// delete wrong mimetype
\OC_DB::executeAudited(self::deleteStmt(), array($wrongId));
2014-06-26 18:40:12 +04:00
}
}
}
private function updateMimetypes($updatedMimetypes) {
foreach ($updatedMimetypes as $extension => $mimetype) {
$result = \OC_DB::executeAudited(self::existsStmt(), array($mimetype));
$exists = $result->fetchOne();
if (!$exists) {
// insert mimetype
\OC_DB::executeAudited(self::insertStmt(), array($mimetype));
}
2015-06-09 01:02:26 +03:00
// get target mimetype id
$result = \OC_DB::executeAudited(self::getIdStmt(), array($mimetype));
$mimetypeId = $result->fetchOne();
// change mimetype for files with x extension
2015-06-09 01:02:26 +03:00
\OC_DB::executeAudited(self::updateByNameStmt(), array($mimetypeId, $mimetypeId, '%.' . $extension));
}
}
private function fixOfficeMimeTypes() {
// update wrong mimetypes
$wrongMimetypes = array(
'application/mspowerpoint' => 'application/vnd.ms-powerpoint',
'application/msexcel' => 'application/vnd.ms-excel',
);
2014-08-12 16:07:10 +04:00
self::repairMimetypes($wrongMimetypes);
2014-08-12 16:07:10 +04: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 16:07:10 +04:00
// separate doc from docx etc
self::updateMimetypes($updatedMimetypes);
}
private function fixApkMimeType() {
$updatedMimetypes = array(
'apk' => 'application/vnd.android.package-archive',
);
2014-08-12 16:07:10 +04:00
self::updateMimetypes($updatedMimetypes);
}
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 16:07:10 +04:00
self::repairMimetypes($wrongMimetypes);
$updatedMimetypes = array(
'ttf' => 'application/font-sfnt',
'otf' => 'application/font-sfnt',
'pfb' => 'application/x-font',
);
2014-08-12 16:07:10 +04:00
self::updateMimetypes($updatedMimetypes);
}
private function fixPostscriptMimeType() {
$updatedMimetypes = array(
'eps' => 'application/postscript',
'ps' => 'application/postscript',
);
2014-08-12 16:07:10 +04:00
self::updateMimetypes($updatedMimetypes);
2014-08-12 16:07:10 +04:00
}
2015-03-16 18:25:04 +03: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);
}
private function introduce3dImagesMimeType() {
$updatedMimetypes = array(
'jps' => 'image/jpeg',
'mpo' => 'image/jpeg',
);
self::updateMimetypes($updatedMimetypes);
}
private function introduceConfMimeType() {
$updatedMimetypes = array(
'conf' => 'text/plain',
'cnf' => 'text/plain',
);
self::updateMimetypes($updatedMimetypes);
}
private function introduceYamlMimeType() {
$updatedMimetypes = array(
'yaml' => 'application/yaml',
'yml' => 'application/yaml',
);
self::updateMimetypes($updatedMimetypes);
}
/**
* Fix mime types
*/
public function run() {
$ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
// NOTE TO DEVELOPERS: when adding new mime types, please make sure to
// add a version comparison to avoid doing it every time
// only update mime types if necessary as it can be expensive
if (version_compare($ocVersionFromBeforeUpdate, '8.2.0', '<')) {
if ($this->fixOfficeMimeTypes()) {
$this->emit('\OC\Repair', 'info', array('Fixed office mime types'));
}
2015-03-16 18:25:04 +03:00
if ($this->fixApkMimeType()) {
$this->emit('\OC\Repair', 'info', array('Fixed APK mime type'));
}
if ($this->fixFontsMimeTypes()) {
$this->emit('\OC\Repair', 'info', array('Fixed fonts mime types'));
}
if ($this->fixPostscriptMimeType()) {
$this->emit('\OC\Repair', 'info', array('Fixed Postscript mime types'));
}
if ($this->introduceRawMimeType()) {
$this->emit('\OC\Repair', 'info', array('Fixed Raw mime types'));
}
if ($this->introduce3dImagesMimeType()) {
$this->emit('\OC\Repair', 'info', array('Fixed 3D images mime types'));
}
if ($this->introduceConfMimeType()) {
$this->emit('\OC\Repair', 'info', array('Fixed Conf/cnf mime types'));
}
if ($this->introduceYamlMimeType()) {
$this->emit('\OC\Repair', 'info', array('Fixed Yaml/Yml mime types'));
}
}
}
2015-03-16 18:25:04 +03:00
}