diff --git a/bin/md5_validation.php b/bin/md5_validation.php new file mode 100644 index 0000000..15d3653 --- /dev/null +++ b/bin/md5_validation.php @@ -0,0 +1,148 @@ +. + * + * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place, + * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "Powered by + * KnowledgeTree" logo and retain the original copyright notice. If the display of the + * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices + * must display the words "Powered by KnowledgeTree" and retain the original + * copyright notice. + * Contributor( s): ______________________________________ + */ + +require_once('../config/dmsDefaults.php'); + +print "KnowledgeTree MD5 Validation Tool\n"; +print "=================================\n\n"; +print "NOTE: This utility make take some time to run to completion!\n\n"; + +$sql = "SELECT + dcv.id, dmv.document_id, MAX(dmv.id) AS metadata_version_id, MAX(dmv.metadata_version) AS metadata_version + FROM + document_content_version AS dcv + INNER JOIN document_metadata_version AS dmv ON dcv.id = dmv.content_version_id + GROUP BY + dcv.id + ORDER BY dcv.document_id"; + +$rows = DBUtil::getResultArray(array($sql)); + +$total = count($rows); +$problem = 0; +$ok = 0; +$no_hash = 0; +$current = 0; +$dots = 0; + +foreach($rows as $row) +{ + if (++$current % 100 == 0) + { + print '.'; + if ($dots++ % 60 == 0) + { + print "\n"; + } + } + $content_id = $row['id']; + $document_id = $row['document_id']; + $metadata_version = $row['metadata_version']; + $metadata_version_id = $row['metadata_version_id']; + + $document = Document::get($document_id, $metadata_version_id); + $core = &$document->_oDocumentContentVersion; + + $filename = $document->getFileName(); + $md5 = $core->getStorageHash(); + + if (empty($md5)) + { + if ($dots > 0) print "\n"; + print("Document Id: $document_id - Content Id: $content_id - No MD5 hash available.\n"); + + $no_hash++; + $current = 0; $dots = 0; + // don't exit here, we do so later + } + + $storage = KTStorageManagerUtil::getSingleton(); + $storage_path = $storage->temporaryFile($document); + if (PEAR::isError($storage_path)) + { + if ($dots > 0) print "\n"; + print("Document Id: $document_id - Content Id: $content_id - Storage engine reported an error: " . $storage_path->getMessage() . "\n"); + + $no_hash++; + $current = 0; $dots = 0; + continue; + } + if (!file_exists($storage_path)) + { + if ($dots > 0) print "\n"; + print("Document Id: $document_id - Content Id: $content_id - File '$storage_path' cannot be found!\n"); + + $no_hash++; + $current = 0; $dots = 0; + continue; + } + + $actual_md5 = md5_file($storage_path); + + $storage->freeTemporaryFile($storage_path); + + if (empty($md5)) + { + $core->setStorageHash($actual_md5); + $core->update(); + print("\tHash set to: $actual_md5\n"); + continue; + } + + + if ($md5 != $actual_md5) + { + if ($dots > 0) print "\n"; + print("Document Id: $document_id - Content ID: $content_id - MD5 difference\n"); + print("\tStored MD5: $md5\n"); + print("\tCurrent MD5: $actual_md5\n"); + $problem++; + $current = 0; $dots = 0; + continue; + } + $ok++; +} + +print("\nStatistics:\n"); +print("\tNo Problem:\t$ok\n"); +print("\tProblem:\t$problem\n"); +print("\tNo Hash:\t$no_hash\n"); +print("\tTotal:\t\t$total\n"); + + +?> \ No newline at end of file diff --git a/lib/documentmanagement/documentcontentversion.inc.php b/lib/documentmanagement/documentcontentversion.inc.php index dbf0f9a..199b265 100644 --- a/lib/documentmanagement/documentcontentversion.inc.php +++ b/lib/documentmanagement/documentcontentversion.inc.php @@ -5,32 +5,32 @@ * KnowledgeTree Open Source Edition * Document Management Made Simple * Copyright (C) 2004 - 2008 The Jam Warehouse Software (Pty) Limited - * + * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU 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 General Public License for more * details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * + * * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place, * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com. - * + * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. - * + * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by - * KnowledgeTree" logo and retain the original copyright notice. If the display of the + * KnowledgeTree" logo and retain the original copyright notice. If the display of the * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices - * must display the words "Powered by KnowledgeTree" and retain the original - * copyright notice. + * must display the words "Powered by KnowledgeTree" and retain the original + * copyright notice. * Contributor( s): ______________________________________ * */ @@ -61,6 +61,8 @@ class KTDocumentContentVersion extends KTEntity { /** Where in the storage this file can be found */ var $sStoragePath; + var $md5hash; + var $_aFieldToSelect = array( "iId" => "id", @@ -72,13 +74,14 @@ class KTDocumentContentVersion extends KTEntity { "iMajorVersion" => 'major_version', "iMinorVersion" => 'minor_version', "sStoragePath" => 'storage_path', + 'md5hash' => 'md5hash' ); - function KTDocumentContentVersion() { } function getFileName() { return $this->sFileName; } function setFileName($sNewValue) { $this->sFileName = $sNewValue; } + function getDocumentId() { return $this->iDocumentId; } function getFileSize() { return $this->iSize; } function setFileSize($iNewValue) { $this->iSize = $iNewValue; } function getSize() { return $this->iSize; } @@ -91,6 +94,8 @@ class KTDocumentContentVersion extends KTEntity { function setMinorVersionNumber($iNewValue) { $this->iMinorVersion = $iNewValue; } function getStoragePath() { return $this->sStoragePath; } function setStoragePath($sNewValue) { $this->sStoragePath = $sNewValue; } + function getStorageHash() { return $this->md5hash; } + function setStorageHash($sNewValue) { $this->md5hash = $sNewValue; } function getVersion() { return sprintf("%s.%s", $this->getMajorVersionNumber(), $this->getMinorVersionNumber()); @@ -101,9 +106,12 @@ class KTDocumentContentVersion extends KTEntity { } function &createFromArray($aOptions) { - return KTEntityUtil::createFromArray('KTDocumentContentVersion', $aOptions); + return KTEntityUtil::createFromArray('KTDocumentContentVersion', $aOptions); } + + + function create() { if (empty($this->iSize)) { $this->iSize = 0; diff --git a/lib/documentmanagement/documentutil.inc.php b/lib/documentmanagement/documentutil.inc.php index c3c93a9..d7bb292 100644 --- a/lib/documentmanagement/documentutil.inc.php +++ b/lib/documentmanagement/documentutil.inc.php @@ -727,6 +727,14 @@ class KTDocumentUtil { return PEAR::raiseError(sprintf(_kt("Couldn't store contents: %s"), _kt('The uploaded file does not exist.'))); } + $md5hash = md5_file($sFilename); + $content = $oDocument->_oDocumentContentVersion; + $content->setStorageHash($md5hash); + $content->update(); + + if (empty($aOptions)) $aOptions = array(); + $aOptions['md5hash'] = $md5hash; + $sType = KTMime::getMimeTypeFromFile($sFilename); $iMimeTypeId = KTMime::getMimeTypeID($sType, $oDocument->getFileName()); $oDocument->setMimeTypeId($iMimeTypeId); @@ -1121,11 +1129,11 @@ class KTDocumentUtil { $oDocument->setModifiedUserId($oUser->getId()); $oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1); $oDocument->_oDocumentContentVersion->setFilename($sNewFilename); - + $sType = KTMime::getMimeTypeFromFile($sNewFilename); $iMimeTypeId = KTMime::getMimeTypeID($sType, $sNewFilename); $oDocument->setMimeTypeId($iMimeTypeId); - + $bSuccess = $oDocument->update(); if ($bSuccess !== true) { if (PEAR::isError($bSuccess)) { diff --git a/sql/mysql/install/structure.sql b/sql/mysql/install/structure.sql index 65d920b..ac52111 100644 --- a/sql/mysql/install/structure.sql +++ b/sql/mysql/install/structure.sql @@ -253,6 +253,7 @@ CREATE TABLE `document_content_version` ( `major_version` int(11) NOT NULL default '0', `minor_version` int(11) NOT NULL default '0', `storage_path` varchar(250) default NULL, + `md5hash` char(32) default NULL, PRIMARY KEY (`id`), KEY `document_id` (`document_id`), KEY `mime_id` (`mime_id`), diff --git a/sql/mysql/upgrade/3.5.3/content_md5hash.sql b/sql/mysql/upgrade/3.5.3/content_md5hash.sql new file mode 100644 index 0000000..4f3210a --- /dev/null +++ b/sql/mysql/upgrade/3.5.3/content_md5hash.sql @@ -0,0 +1 @@ +ALTER TABLE document_content_version ADD md5hash char(32); \ No newline at end of file