Protection::getFormatColumns()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
use PhpOffice\PhpSpreadsheet\Shared\PasswordHasher;
6
7
class Protection
8
{
9
    const ALGORITHM_MD2 = 'MD2';
10
    const ALGORITHM_MD4 = 'MD4';
11
    const ALGORITHM_MD5 = 'MD5';
12
    const ALGORITHM_SHA_1 = 'SHA-1';
13
    const ALGORITHM_SHA_256 = 'SHA-256';
14
    const ALGORITHM_SHA_384 = 'SHA-384';
15
    const ALGORITHM_SHA_512 = 'SHA-512';
16
    const ALGORITHM_RIPEMD_128 = 'RIPEMD-128';
17
    const ALGORITHM_RIPEMD_160 = 'RIPEMD-160';
18
    const ALGORITHM_WHIRLPOOL = 'WHIRLPOOL';
19
20
    /**
21
     * Autofilters are locked when sheet is protected, default true.
22
     */
23
    private ?bool $autoFilter = null;
24
25
    /**
26
     * Deleting columns is locked when sheet is protected, default true.
27
     */
28
    private ?bool $deleteColumns = null;
29
30
    /**
31
     * Deleting rows is locked when sheet is protected, default true.
32
     */
33
    private ?bool $deleteRows = null;
34
35
    /**
36
     * Formatting cells is locked when sheet is protected, default true.
37
     */
38
    private ?bool $formatCells = null;
39
40
    /**
41
     * Formatting columns is locked when sheet is protected, default true.
42
     */
43
    private ?bool $formatColumns = null;
44
45
    /**
46
     * Formatting rows is locked when sheet is protected, default true.
47
     */
48
    private ?bool $formatRows = null;
49
50
    /**
51
     * Inserting columns is locked when sheet is protected, default true.
52
     */
53
    private ?bool $insertColumns = null;
54
55
    /**
56
     * Inserting hyperlinks is locked when sheet is protected, default true.
57
     */
58
    private ?bool $insertHyperlinks = null;
59
60
    /**
61
     * Inserting rows is locked when sheet is protected, default true.
62
     */
63
    private ?bool $insertRows = null;
64
65
    /**
66
     * Objects are locked when sheet is protected, default false.
67
     */
68
    private ?bool $objects = null;
69
70
    /**
71
     * Pivot tables are locked when the sheet is protected, default true.
72
     */
73
    private ?bool $pivotTables = null;
74
75
    /**
76
     * Scenarios are locked when sheet is protected, default false.
77
     */
78
    private ?bool $scenarios = null;
79
80
    /**
81
     * Selection of locked cells is locked when sheet is protected, default false.
82
     */
83
    private ?bool $selectLockedCells = null;
84
85
    /**
86
     * Selection of unlocked cells is locked when sheet is protected, default false.
87
     */
88
    private ?bool $selectUnlockedCells = null;
89
90
    /**
91
     * Sheet is locked when sheet is protected, default false.
92
     */
93
    private ?bool $sheet = null;
94
95
    /**
96
     * Sorting is locked when sheet is protected, default true.
97
     */
98
    private ?bool $sort = null;
99
100
    /**
101
     * Hashed password.
102
     */
103
    private string $password = '';
104
105
    /**
106
     * Algorithm name.
107
     */
108
    private string $algorithm = '';
109
110
    /**
111
     * Salt value.
112
     */
113
    private string $salt = '';
114
115
    /**
116
     * Spin count.
117
     */
118
    private int $spinCount = 10000;
119
120
    /**
121
     * Create a new Protection.
122
     */
123 10621
    public function __construct()
124
    {
125 10621
    }
126
127
    /**
128
     * Is some sort of protection enabled?
129
     */
130 429
    public function isProtectionEnabled(): bool
131
    {
132 429
        return
133 429
            $this->password !== ''
134 429
            || isset($this->sheet)
135 429
            || isset($this->objects)
136 429
            || isset($this->scenarios)
137 429
            || isset($this->formatCells)
138 429
            || isset($this->formatColumns)
139 429
            || isset($this->formatRows)
140 429
            || isset($this->insertColumns)
141 429
            || isset($this->insertRows)
142 429
            || isset($this->insertHyperlinks)
143 429
            || isset($this->deleteColumns)
144 429
            || isset($this->deleteRows)
145 429
            || isset($this->selectLockedCells)
146 429
            || isset($this->sort)
147 429
            || isset($this->autoFilter)
148 429
            || isset($this->pivotTables)
149 429
            || isset($this->selectUnlockedCells);
150
    }
151
152 146
    public function getSheet(): ?bool
153
    {
154 146
        return $this->sheet;
155
    }
156
157 48
    public function setSheet(?bool $sheet): self
158
    {
159 48
        $this->sheet = $sheet;
160
161 48
        return $this;
162
    }
163
164 140
    public function getObjects(): ?bool
165
    {
166 140
        return $this->objects;
167
    }
168
169 124
    public function setObjects(?bool $objects): self
170
    {
171 124
        $this->objects = $objects;
172
173 124
        return $this;
174
    }
175
176 140
    public function getScenarios(): ?bool
177
    {
178 140
        return $this->scenarios;
179
    }
180
181 122
    public function setScenarios(?bool $scenarios): self
182
    {
183 122
        $this->scenarios = $scenarios;
184
185 122
        return $this;
186
    }
187
188 140
    public function getFormatCells(): ?bool
189
    {
190 140
        return $this->formatCells;
191
    }
192
193 165
    public function setFormatCells(?bool $formatCells): self
194
    {
195 165
        $this->formatCells = $formatCells;
196
197 165
        return $this;
198
    }
199
200 140
    public function getFormatColumns(): ?bool
201
    {
202 140
        return $this->formatColumns;
203
    }
204
205 162
    public function setFormatColumns(?bool $formatColumns): self
206
    {
207 162
        $this->formatColumns = $formatColumns;
208
209 162
        return $this;
210
    }
211
212 140
    public function getFormatRows(): ?bool
213
    {
214 140
        return $this->formatRows;
215
    }
216
217 162
    public function setFormatRows(?bool $formatRows): self
218
    {
219 162
        $this->formatRows = $formatRows;
220
221 162
        return $this;
222
    }
223
224 140
    public function getInsertColumns(): ?bool
225
    {
226 140
        return $this->insertColumns;
227
    }
228
229 162
    public function setInsertColumns(?bool $insertColumns): self
230
    {
231 162
        $this->insertColumns = $insertColumns;
232
233 162
        return $this;
234
    }
235
236 140
    public function getInsertRows(): ?bool
237
    {
238 140
        return $this->insertRows;
239
    }
240
241 164
    public function setInsertRows(?bool $insertRows): self
242
    {
243 164
        $this->insertRows = $insertRows;
244
245 164
        return $this;
246
    }
247
248 140
    public function getInsertHyperlinks(): ?bool
249
    {
250 140
        return $this->insertHyperlinks;
251
    }
252
253 162
    public function setInsertHyperlinks(?bool $insertHyperLinks): self
254
    {
255 162
        $this->insertHyperlinks = $insertHyperLinks;
256
257 162
        return $this;
258
    }
259
260 140
    public function getDeleteColumns(): ?bool
261
    {
262 140
        return $this->deleteColumns;
263
    }
264
265 162
    public function setDeleteColumns(?bool $deleteColumns): self
266
    {
267 162
        $this->deleteColumns = $deleteColumns;
268
269 162
        return $this;
270
    }
271
272 140
    public function getDeleteRows(): ?bool
273
    {
274 140
        return $this->deleteRows;
275
    }
276
277 161
    public function setDeleteRows(?bool $deleteRows): self
278
    {
279 161
        $this->deleteRows = $deleteRows;
280
281 161
        return $this;
282
    }
283
284 140
    public function getSelectLockedCells(): ?bool
285
    {
286 140
        return $this->selectLockedCells;
287
    }
288
289 123
    public function setSelectLockedCells(?bool $selectLockedCells): self
290
    {
291 123
        $this->selectLockedCells = $selectLockedCells;
292
293 123
        return $this;
294
    }
295
296 140
    public function getSort(): ?bool
297
    {
298 140
        return $this->sort;
299
    }
300
301 165
    public function setSort(?bool $sort): self
302
    {
303 165
        $this->sort = $sort;
304
305 165
        return $this;
306
    }
307
308 140
    public function getAutoFilter(): ?bool
309
    {
310 140
        return $this->autoFilter;
311
    }
312
313 163
    public function setAutoFilter(?bool $autoFilter): self
314
    {
315 163
        $this->autoFilter = $autoFilter;
316
317 163
        return $this;
318
    }
319
320 140
    public function getPivotTables(): ?bool
321
    {
322 140
        return $this->pivotTables;
323
    }
324
325 161
    public function setPivotTables(?bool $pivotTables): self
326
    {
327 161
        $this->pivotTables = $pivotTables;
328
329 161
        return $this;
330
    }
331
332 140
    public function getSelectUnlockedCells(): ?bool
333
    {
334 140
        return $this->selectUnlockedCells;
335
    }
336
337 122
    public function setSelectUnlockedCells(?bool $selectUnlockedCells): self
338
    {
339 122
        $this->selectUnlockedCells = $selectUnlockedCells;
340
341 122
        return $this;
342
    }
343
344
    /**
345
     * Get hashed password.
346
     */
347 39
    public function getPassword(): string
348
    {
349 39
        return $this->password;
350
    }
351
352
    /**
353
     * Set Password.
354
     *
355
     * @param bool $alreadyHashed If the password has already been hashed, set this to true
356
     *
357
     * @return $this
358
     */
359 74
    public function setPassword(string $password, bool $alreadyHashed = false): static
360
    {
361 74
        if (!$alreadyHashed) {
362 12
            $salt = $this->generateSalt();
363 12
            $this->setSalt($salt);
364 12
            $password = PasswordHasher::hashPassword($password, $this->getAlgorithm(), $this->getSalt(), $this->getSpinCount());
365
        }
366
367 74
        $this->password = $password;
368
369 74
        return $this;
370
    }
371
372 2
    public function setHashValue(string $password): self
373
    {
374 2
        return $this->setPassword($password, true);
375
    }
376
377
    /**
378
     * Create a pseudorandom string.
379
     */
380 12
    private function generateSalt(): string
381
    {
382 12
        return base64_encode(random_bytes(16));
383
    }
384
385
    /**
386
     * Get algorithm name.
387
     */
388 39
    public function getAlgorithm(): string
389
    {
390 39
        return $this->algorithm;
391
    }
392
393
    /**
394
     * Set algorithm name.
395
     */
396 70
    public function setAlgorithm(string $algorithm): self
397
    {
398 70
        return $this->setAlgorithmName($algorithm);
399
    }
400
401
    /**
402
     * Set algorithm name.
403
     */
404 70
    public function setAlgorithmName(string $algorithm): self
405
    {
406 70
        $this->algorithm = $algorithm;
407
408 70
        return $this;
409
    }
410
411 13
    public function getSalt(): string
412
    {
413 13
        return $this->salt;
414
    }
415
416 13
    public function setSalt(string $salt): self
417
    {
418 13
        return $this->setSaltValue($salt);
419
    }
420
421 13
    public function setSaltValue(string $salt): self
422
    {
423 13
        $this->salt = $salt;
424
425 13
        return $this;
426
    }
427
428
    /**
429
     * Get spin count.
430
     */
431 14
    public function getSpinCount(): int
432
    {
433 14
        return $this->spinCount;
434
    }
435
436
    /**
437
     * Set spin count.
438
     */
439 3
    public function setSpinCount(int $spinCount): self
440
    {
441 3
        $this->spinCount = $spinCount;
442
443 3
        return $this;
444
    }
445
446
    /**
447
     * Verify that the given non-hashed password can "unlock" the protection.
448
     */
449 3
    public function verify(string $password): bool
450
    {
451 3
        if ($this->password === '') {
452 2
            return true;
453
        }
454
455 3
        $hash = PasswordHasher::hashPassword($password, $this->getAlgorithm(), $this->getSalt(), $this->getSpinCount());
456
457 3
        return $this->getPassword() === $hash;
458
    }
459
460
    /**
461
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
462
     */
463 1
    public function __clone()
464
    {
465 1
        $vars = get_object_vars($this);
466 1
        foreach ($vars as $key => $value) {
467 1
            if (is_object($value)) {
468
                $this->$key = clone $value;
469
            } else {
470 1
                $this->$key = $value;
471
            }
472
        }
473
    }
474
}
475