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