| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Document; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\PasswordHasher; |
| 6 |
|
| 7 |
class Security |
| 8 |
{ |
| 9 |
/** |
| 10 |
* LockRevision. |
| 11 |
*/ |
| 12 |
private bool $lockRevision = false; |
| 13 |
|
| 14 |
/** |
| 15 |
* LockStructure. |
| 16 |
*/ |
| 17 |
private bool $lockStructure = false; |
| 18 |
|
| 19 |
/** |
| 20 |
* LockWindows. |
| 21 |
*/ |
| 22 |
private bool $lockWindows = false; |
| 23 |
|
| 24 |
/** |
| 25 |
* RevisionsPassword. |
| 26 |
*/ |
| 27 |
private string $revisionsPassword = ''; |
| 28 |
|
| 29 |
/** |
| 30 |
* WorkbookPassword. |
| 31 |
*/ |
| 32 |
private string $workbookPassword = ''; |
| 33 |
|
| 34 |
private string $workbookAlgorithmName = ''; |
| 35 |
|
| 36 |
private string $workbookHashValue = ''; |
| 37 |
|
| 38 |
private string $workbookSaltValue = ''; |
| 39 |
|
| 40 |
private int $workbookSpinCount = 0; |
| 41 |
|
| 42 |
private string $revisionsAlgorithmName = ''; |
| 43 |
|
| 44 |
private string $revisionsHashValue = ''; |
| 45 |
|
| 46 |
private string $revisionsSaltValue = ''; |
| 47 |
|
| 48 |
private int $revisionsSpinCount = 0; |
| 49 |
|
| 50 |
/** |
| 51 |
* Is some sort of document security enabled? |
| 52 |
*/ |
| 53 |
public function isSecurityEnabled(): bool |
| 54 |
{ |
| 55 |
return $this->lockRevision |
| 56 |
|| $this->lockStructure |
| 57 |
|| $this->lockWindows; |
| 58 |
} |
| 59 |
|
| 60 |
public function getLockRevision(): bool |
| 61 |
{ |
| 62 |
return $this->lockRevision; |
| 63 |
} |
| 64 |
|
| 65 |
public function setLockRevision(?bool $locked): self |
| 66 |
{ |
| 67 |
if ($locked !== null) { |
| 68 |
$this->lockRevision = $locked; |
| 69 |
} |
| 70 |
|
| 71 |
return $this; |
| 72 |
} |
| 73 |
|
| 74 |
public function getLockStructure(): bool |
| 75 |
{ |
| 76 |
return $this->lockStructure; |
| 77 |
} |
| 78 |
|
| 79 |
public function setLockStructure(?bool $locked): self |
| 80 |
{ |
| 81 |
if ($locked !== null) { |
| 82 |
$this->lockStructure = $locked; |
| 83 |
} |
| 84 |
|
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
public function getLockWindows(): bool |
| 89 |
{ |
| 90 |
return $this->lockWindows; |
| 91 |
} |
| 92 |
|
| 93 |
public function setLockWindows(?bool $locked): self |
| 94 |
{ |
| 95 |
if ($locked !== null) { |
| 96 |
$this->lockWindows = $locked; |
| 97 |
} |
| 98 |
|
| 99 |
return $this; |
| 100 |
} |
| 101 |
|
| 102 |
public function getRevisionsPassword(): string |
| 103 |
{ |
| 104 |
return $this->revisionsPassword; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Set RevisionsPassword. |
| 109 |
* |
| 110 |
* @param bool $alreadyHashed If the password has already been hashed, set this to true |
| 111 |
* |
| 112 |
* @return $this |
| 113 |
*/ |
| 114 |
public function setRevisionsPassword(?string $password, bool $alreadyHashed = false) |
| 115 |
{ |
| 116 |
if ($password !== null) { |
| 117 |
if ($this->advancedRevisionsPassword()) { |
| 118 |
if (!$alreadyHashed) { |
| 119 |
$password = PasswordHasher::hashPassword($password, $this->revisionsAlgorithmName, $this->revisionsSaltValue, $this->revisionsSpinCount); |
| 120 |
} |
| 121 |
$this->revisionsHashValue = $password; |
| 122 |
$this->revisionsPassword = ''; |
| 123 |
} else { |
| 124 |
if (!$alreadyHashed) { |
| 125 |
$password = PasswordHasher::hashPassword($password); |
| 126 |
} |
| 127 |
$this->revisionsPassword = $password; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $this; |
| 132 |
} |
| 133 |
|
| 134 |
public function getWorkbookPassword(): string |
| 135 |
{ |
| 136 |
return $this->workbookPassword; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Set WorkbookPassword. |
| 141 |
* |
| 142 |
* @param bool $alreadyHashed If the password has already been hashed, set this to true |
| 143 |
* |
| 144 |
* @return $this |
| 145 |
*/ |
| 146 |
public function setWorkbookPassword(?string $password, bool $alreadyHashed = false) |
| 147 |
{ |
| 148 |
if ($password !== null) { |
| 149 |
if ($this->advancedPassword()) { |
| 150 |
if (!$alreadyHashed) { |
| 151 |
$password = PasswordHasher::hashPassword($password, $this->workbookAlgorithmName, $this->workbookSaltValue, $this->workbookSpinCount); |
| 152 |
} |
| 153 |
$this->workbookHashValue = $password; |
| 154 |
$this->workbookPassword = ''; |
| 155 |
} else { |
| 156 |
if (!$alreadyHashed) { |
| 157 |
$password = PasswordHasher::hashPassword($password); |
| 158 |
} |
| 159 |
$this->workbookPassword = $password; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return $this; |
| 164 |
} |
| 165 |
|
| 166 |
public function getWorkbookHashValue(): string |
| 167 |
{ |
| 168 |
return $this->advancedPassword() ? $this->workbookHashValue : ''; |
| 169 |
} |
| 170 |
|
| 171 |
public function advancedPassword(): bool |
| 172 |
{ |
| 173 |
return $this->workbookAlgorithmName !== '' && $this->workbookSaltValue !== '' && $this->workbookSpinCount > 0; |
| 174 |
} |
| 175 |
|
| 176 |
public function getWorkbookAlgorithmName(): string |
| 177 |
{ |
| 178 |
return $this->workbookAlgorithmName; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* @return static |
| 183 |
*/ |
| 184 |
public function setWorkbookAlgorithmName(string $workbookAlgorithmName) |
| 185 |
{ |
| 186 |
$this->workbookAlgorithmName = $workbookAlgorithmName; |
| 187 |
|
| 188 |
return $this; |
| 189 |
} |
| 190 |
|
| 191 |
public function getWorkbookSpinCount(): int |
| 192 |
{ |
| 193 |
return $this->workbookSpinCount; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* @return static |
| 198 |
*/ |
| 199 |
public function setWorkbookSpinCount(int $workbookSpinCount) |
| 200 |
{ |
| 201 |
$this->workbookSpinCount = $workbookSpinCount; |
| 202 |
|
| 203 |
return $this; |
| 204 |
} |
| 205 |
|
| 206 |
public function getWorkbookSaltValue(): string |
| 207 |
{ |
| 208 |
return $this->workbookSaltValue; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* @return static |
| 213 |
*/ |
| 214 |
public function setWorkbookSaltValue(string $workbookSaltValue, bool $base64Required) |
| 215 |
{ |
| 216 |
$this->workbookSaltValue = $base64Required ? base64_encode($workbookSaltValue) : $workbookSaltValue; |
| 217 |
|
| 218 |
return $this; |
| 219 |
} |
| 220 |
|
| 221 |
public function getRevisionsHashValue(): string |
| 222 |
{ |
| 223 |
return $this->advancedRevisionsPassword() ? $this->revisionsHashValue : ''; |
| 224 |
} |
| 225 |
|
| 226 |
public function advancedRevisionsPassword(): bool |
| 227 |
{ |
| 228 |
return $this->revisionsAlgorithmName !== '' && $this->revisionsSaltValue !== '' && $this->revisionsSpinCount > 0; |
| 229 |
} |
| 230 |
|
| 231 |
public function getRevisionsAlgorithmName(): string |
| 232 |
{ |
| 233 |
return $this->revisionsAlgorithmName; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* @return static |
| 238 |
*/ |
| 239 |
public function setRevisionsAlgorithmName(string $revisionsAlgorithmName) |
| 240 |
{ |
| 241 |
$this->revisionsAlgorithmName = $revisionsAlgorithmName; |
| 242 |
|
| 243 |
return $this; |
| 244 |
} |
| 245 |
|
| 246 |
public function getRevisionsSpinCount(): int |
| 247 |
{ |
| 248 |
return $this->revisionsSpinCount; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* @return static |
| 253 |
*/ |
| 254 |
public function setRevisionsSpinCount(int $revisionsSpinCount) |
| 255 |
{ |
| 256 |
$this->revisionsSpinCount = $revisionsSpinCount; |
| 257 |
|
| 258 |
return $this; |
| 259 |
} |
| 260 |
|
| 261 |
public function getRevisionsSaltValue(): string |
| 262 |
{ |
| 263 |
return $this->revisionsSaltValue; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* @return static |
| 268 |
*/ |
| 269 |
public function setRevisionsSaltValue(string $revisionsSaltValue, bool $base64Required) |
| 270 |
{ |
| 271 |
$this->revisionsSaltValue = $base64Required ? base64_encode($revisionsSaltValue) : $revisionsSaltValue; |
| 272 |
|
| 273 |
return $this; |
| 274 |
} |
| 275 |
} |
| 276 |
|