Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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