Skip to content

Commit 200c2f1

Browse files
authored
Merge pull request #1019 from jun-i-corn/feature/EnablePasswordProtection
enable password protection (Word)
2 parents 8929917 + 7908491 commit 200c2f1

File tree

10 files changed

+629
-19
lines changed

10 files changed

+629
-19
lines changed

composer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@
3434
"name": "Antoine de Troostembergh"
3535
}
3636
],
37+
"scripts": {
38+
"check": [
39+
"./vendor/bin/php-cs-fixer fix --ansi --dry-run --diff",
40+
"./vendor/bin/phpcs --report-width=200 --report-summary --report-full samples/ src/ tests/ --ignore=src/PhpWord/Shared/PCLZip --standard=PSR2 -n",
41+
"./vendor/bin/phpmd src/,tests/ text ./phpmd.xml.dist --exclude pclzip.lib.php",
42+
"./vendor/bin/phpunit --color=always"
43+
],
44+
"fix": [
45+
"./vendor/bin/php-cs-fixer fix --ansi"
46+
]
47+
},
3748
"require": {
3849
"php": ">=5.3.3",
3950
"ext-xml": "*",

docs/general.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,17 @@ points to twips.
272272
// 2 cm right margin
273273
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));
274274
275+
Document protection
276+
-------------------
277+
278+
The document (or parts of it) can be password protected.
279+
280+
.. code-block:: php
281+
282+
$documentProtection = $phpWord->getSettings()->getDocumentProtection();
283+
$documentProtection->setEditing(DocProtect::READ_ONLY);
284+
$documentProtection->setPassword('myPassword');
285+
275286
Automatically Recalculate Fields on Open
276287
----------------------------------------
277288

samples/Sample_38_Protection.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
use PhpOffice\PhpWord\SimpleType\DocProtect;
3+
4+
include_once 'Sample_Header.php';
5+
6+
// New Word Document
7+
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
8+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
9+
10+
$documentProtection = $phpWord->getSettings()->getDocumentProtection();
11+
$documentProtection->setEditing(DocProtect::READ_ONLY);
12+
$documentProtection->setPassword('myPassword');
13+
14+
$section = $phpWord->addSection();
15+
$section->addText('this document is password protected');
16+
17+
// Save file
18+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
19+
if (!CLI) {
20+
include_once 'Sample_Footer.php';
21+
}

src/PhpWord/Metadata/Protection.php

Lines changed: 135 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,63 @@
1717

1818
namespace PhpOffice\PhpWord\Metadata;
1919

20+
use PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder;
21+
use PhpOffice\PhpWord\SimpleType\DocProtect;
22+
2023
/**
2124
* Document protection class
2225
*
2326
* @since 0.12.0
24-
* @see http://www.datypic.com/sc/ooxml/t-w_CT_DocProtect.html
25-
* @todo Password!
27+
* @see http://www.datypic.com/sc/ooxml/t-w_CT_DocProtect.html
2628
*/
2729
class Protection
2830
{
2931
/**
30-
* Editing restriction readOnly|comments|trackedChanges|forms
32+
* Editing restriction none|readOnly|comments|trackedChanges|forms
3133
*
3234
* @var string
3335
* @see http://www.datypic.com/sc/ooxml/a-w_edit-1.html
3436
*/
3537
private $editing;
3638

39+
/**
40+
* password
41+
*
42+
* @var string
43+
*/
44+
private $password;
45+
46+
/**
47+
* Iterations to Run Hashing Algorithm
48+
*
49+
* @var int
50+
*/
51+
private $spinCount = 100000;
52+
53+
/**
54+
* Cryptographic Hashing Algorithm (see constants defined in \PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder)
55+
*
56+
* @var string
57+
*/
58+
private $algorithm = PasswordEncoder::ALGORITHM_SHA_1;
59+
60+
/**
61+
* Salt for Password Verifier
62+
*
63+
* @var string
64+
*/
65+
private $salt;
66+
3767
/**
3868
* Create a new instance
3969
*
4070
* @param string $editing
4171
*/
4272
public function __construct($editing = null)
4373
{
44-
$this->setEditing($editing);
74+
if ($editing != null) {
75+
$this->setEditing($editing);
76+
}
4577
}
4678

4779
/**
@@ -57,13 +89,111 @@ public function getEditing()
5789
/**
5890
* Set editing protection
5991
*
60-
* @param string $editing
92+
* @param string $editing Any value of \PhpOffice\PhpWord\SimpleType\DocProtect
6193
* @return self
6294
*/
6395
public function setEditing($editing = null)
6496
{
97+
DocProtect::validate($editing);
6598
$this->editing = $editing;
6699

67100
return $this;
68101
}
102+
103+
/**
104+
* Get password
105+
*
106+
* @return string
107+
*/
108+
public function getPassword()
109+
{
110+
return $this->password;
111+
}
112+
113+
/**
114+
* Set password
115+
*
116+
* @param $password
117+
* @return self
118+
*/
119+
public function setPassword($password)
120+
{
121+
$this->password = $password;
122+
123+
return $this;
124+
}
125+
126+
/**
127+
* Get count for hash iterations
128+
*
129+
* @return int
130+
*/
131+
public function getSpinCount()
132+
{
133+
return $this->spinCount;
134+
}
135+
136+
/**
137+
* Set count for hash iterations
138+
*
139+
* @param $spinCount
140+
* @return self
141+
*/
142+
public function setSpinCount($spinCount)
143+
{
144+
$this->spinCount = $spinCount;
145+
146+
return $this;
147+
}
148+
149+
/**
150+
* Get algorithm
151+
*
152+
* @return string
153+
*/
154+
public function getAlgorithm()
155+
{
156+
return $this->algorithm;
157+
}
158+
159+
/**
160+
* Set algorithm
161+
*
162+
* @param $algorithm
163+
* @return self
164+
*/
165+
public function setAlgorithm($algorithm)
166+
{
167+
$this->algorithm = $algorithm;
168+
169+
return $this;
170+
}
171+
172+
/**
173+
* Get salt
174+
*
175+
* @return string
176+
*/
177+
public function getSalt()
178+
{
179+
return $this->salt;
180+
}
181+
182+
/**
183+
* Set salt. Salt HAS to be 16 characters, or an exception will be thrown.
184+
*
185+
* @param $salt
186+
* @throws \InvalidArgumentException
187+
* @return self
188+
*/
189+
public function setSalt($salt)
190+
{
191+
if ($salt !== null && strlen($salt) !== 16) {
192+
throw new \InvalidArgumentException('salt has to be of exactly 16 bytes length');
193+
}
194+
195+
$this->salt = $salt;
196+
197+
return $this;
198+
}
69199
}

0 commit comments

Comments
 (0)