diff --git a/src/PhpWord/Metadata/Protection.php b/src/PhpWord/Metadata/Protection.php index 634751fb..35391cb2 100644 --- a/src/PhpWord/Metadata/Protection.php +++ b/src/PhpWord/Metadata/Protection.php @@ -162,7 +162,7 @@ class Protection * @param $algorithm * @return self */ - public function setMswordAlgorithmSid($algorithm) + public function setAlgorithm($algorithm) { $this->algorithm = $algorithm; diff --git a/src/PhpWord/Shared/Microsoft/PasswordEncoder.php b/src/PhpWord/Shared/Microsoft/PasswordEncoder.php index a6a607a1..d3a03d97 100644 --- a/src/PhpWord/Shared/Microsoft/PasswordEncoder.php +++ b/src/PhpWord/Shared/Microsoft/PasswordEncoder.php @@ -162,6 +162,17 @@ class PasswordEncoder return $algorithm; } + /** + * Returns the algorithm ID + * + * @param sting $algorithmName + * @return int + */ + public static function getAlgorithmId($algorithmName) + { + return self::$algorithmMapping[$algorithmName][0]; + } + /** * Build combined key from low-order word and high-order word * diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 6ac5ec4a..e56e2612 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -183,7 +183,7 @@ class Settings extends AbstractPart private function setDocumentProtection($documentProtection) { if ($documentProtection->getEditing() !== null) { - if (empty($documentProtection->getPassword())) { + if ($documentProtection->getPassword() == null) { $this->settings['w:documentProtection'] = array( '@attributes' => array( 'w:enforcement' => 1, @@ -202,7 +202,7 @@ class Settings extends AbstractPart 'w:cryptProviderType' => 'rsaFull', 'w:cryptAlgorithmClass' => 'hash', 'w:cryptAlgorithmType' => 'typeAny', - 'w:cryptAlgorithmSid' => $documentProtection->getMswordAlgorithmSid(), + 'w:cryptAlgorithmSid' => PasswordEncoder::getAlgorithmId($documentProtection->getAlgorithm()), 'w:cryptSpinCount' => $documentProtection->getSpinCount(), 'w:hash' => $passwordHash, 'w:salt' => base64_encode($documentProtection->getSalt()), diff --git a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php index 7a355042..1e6af567 100644 --- a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php @@ -24,6 +24,7 @@ use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\SimpleType\Zoom; use PhpOffice\PhpWord\Style\Language; use PhpOffice\PhpWord\TestHelperDOCX; +use PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Settings @@ -65,7 +66,7 @@ class SettingsTest extends \PHPUnit\Framework\TestCase $phpWord->getSettings()->getDocumentProtection()->setEditing('readOnly'); $phpWord->getSettings()->getDocumentProtection()->setPassword('testÄö@€!$&'); $phpWord->getSettings()->getDocumentProtection()->setSalt(base64_decode('uq81pJRRGFIY5U+E9gt8tA==')); - $phpWord->getSettings()->getDocumentProtection()->setMswordAlgorithmSid(1); + $phpWord->getSettings()->getDocumentProtection()->setAlgorithm(PasswordEncoder::ALGORITHM_MD2); $phpWord->getSettings()->getDocumentProtection()->setSpinCount(10); $doc = TestHelperDOCX::getDocument($phpWord);