ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Protection.php
Go to the documentation of this file.
1 <?php
2 
4 
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 
25  private $sheet = false;
26 
32  private $objects = false;
33 
39  private $scenarios = false;
40 
46  private $formatCells = false;
47 
53  private $formatColumns = false;
54 
60  private $formatRows = false;
61 
67  private $insertColumns = false;
68 
74  private $insertRows = false;
75 
81  private $insertHyperlinks = false;
82 
88  private $deleteColumns = false;
89 
95  private $deleteRows = false;
96 
102  private $selectLockedCells = false;
103 
109  private $sort = false;
110 
116  private $autoFilter = false;
117 
123  private $pivotTables = false;
124 
130  private $selectUnlockedCells = false;
131 
137  private $password = '';
138 
144  private $algorithm = '';
145 
151  private $salt = '';
152 
158  private $spinCount = 10000;
159 
163  public function __construct()
164  {
165  }
166 
172  public function isProtectionEnabled()
173  {
174  return $this->sheet ||
175  $this->objects ||
176  $this->scenarios ||
177  $this->formatCells ||
178  $this->formatColumns ||
179  $this->formatRows ||
180  $this->insertColumns ||
181  $this->insertRows ||
182  $this->insertHyperlinks ||
183  $this->deleteColumns ||
184  $this->deleteRows ||
185  $this->selectLockedCells ||
186  $this->sort ||
187  $this->autoFilter ||
188  $this->pivotTables ||
190  }
191 
197  public function getSheet()
198  {
199  return $this->sheet;
200  }
201 
209  public function setSheet($pValue)
210  {
211  $this->sheet = $pValue;
212 
213  return $this;
214  }
215 
221  public function getObjects()
222  {
223  return $this->objects;
224  }
225 
233  public function setObjects($pValue)
234  {
235  $this->objects = $pValue;
236 
237  return $this;
238  }
239 
245  public function getScenarios()
246  {
247  return $this->scenarios;
248  }
249 
257  public function setScenarios($pValue)
258  {
259  $this->scenarios = $pValue;
260 
261  return $this;
262  }
263 
269  public function getFormatCells()
270  {
271  return $this->formatCells;
272  }
273 
281  public function setFormatCells($pValue)
282  {
283  $this->formatCells = $pValue;
284 
285  return $this;
286  }
287 
293  public function getFormatColumns()
294  {
295  return $this->formatColumns;
296  }
297 
305  public function setFormatColumns($pValue)
306  {
307  $this->formatColumns = $pValue;
308 
309  return $this;
310  }
311 
317  public function getFormatRows()
318  {
319  return $this->formatRows;
320  }
321 
329  public function setFormatRows($pValue)
330  {
331  $this->formatRows = $pValue;
332 
333  return $this;
334  }
335 
341  public function getInsertColumns()
342  {
343  return $this->insertColumns;
344  }
345 
353  public function setInsertColumns($pValue)
354  {
355  $this->insertColumns = $pValue;
356 
357  return $this;
358  }
359 
365  public function getInsertRows()
366  {
367  return $this->insertRows;
368  }
369 
377  public function setInsertRows($pValue)
378  {
379  $this->insertRows = $pValue;
380 
381  return $this;
382  }
383 
389  public function getInsertHyperlinks()
390  {
392  }
393 
401  public function setInsertHyperlinks($pValue)
402  {
403  $this->insertHyperlinks = $pValue;
404 
405  return $this;
406  }
407 
413  public function getDeleteColumns()
414  {
415  return $this->deleteColumns;
416  }
417 
425  public function setDeleteColumns($pValue)
426  {
427  $this->deleteColumns = $pValue;
428 
429  return $this;
430  }
431 
437  public function getDeleteRows()
438  {
439  return $this->deleteRows;
440  }
441 
449  public function setDeleteRows($pValue)
450  {
451  $this->deleteRows = $pValue;
452 
453  return $this;
454  }
455 
461  public function getSelectLockedCells()
462  {
464  }
465 
473  public function setSelectLockedCells($pValue)
474  {
475  $this->selectLockedCells = $pValue;
476 
477  return $this;
478  }
479 
485  public function getSort()
486  {
487  return $this->sort;
488  }
489 
497  public function setSort($pValue)
498  {
499  $this->sort = $pValue;
500 
501  return $this;
502  }
503 
509  public function getAutoFilter()
510  {
511  return $this->autoFilter;
512  }
513 
521  public function setAutoFilter($pValue)
522  {
523  $this->autoFilter = $pValue;
524 
525  return $this;
526  }
527 
533  public function getPivotTables()
534  {
535  return $this->pivotTables;
536  }
537 
545  public function setPivotTables($pValue)
546  {
547  $this->pivotTables = $pValue;
548 
549  return $this;
550  }
551 
557  public function getSelectUnlockedCells()
558  {
560  }
561 
569  public function setSelectUnlockedCells($pValue)
570  {
571  $this->selectUnlockedCells = $pValue;
572 
573  return $this;
574  }
575 
581  public function getPassword()
582  {
583  return $this->password;
584  }
585 
594  public function setPassword($pValue, $pAlreadyHashed = false)
595  {
596  if (!$pAlreadyHashed) {
597  $salt = $this->generateSalt();
598  $this->setSalt($salt);
599  $pValue = PasswordHasher::hashPassword($pValue, $this->getAlgorithm(), $this->getSalt(), $this->getSpinCount());
600  }
601 
602  $this->password = $pValue;
603 
604  return $this;
605  }
606 
610  private function generateSalt(): string
611  {
612  return base64_encode(random_bytes(16));
613  }
614 
618  public function getAlgorithm(): string
619  {
620  return $this->algorithm;
621  }
622 
626  public function setAlgorithm(string $algorithm): void
627  {
628  $this->algorithm = $algorithm;
629  }
630 
634  public function getSalt(): string
635  {
636  return $this->salt;
637  }
638 
642  public function setSalt(string $salt): void
643  {
644  $this->salt = $salt;
645  }
646 
650  public function getSpinCount(): int
651  {
652  return $this->spinCount;
653  }
654 
658  public function setSpinCount(int $spinCount): void
659  {
660  $this->spinCount = $spinCount;
661  }
662 
666  public function verify(string $password): bool
667  {
668  if (!$this->isProtectionEnabled()) {
669  return true;
670  }
671 
672  $hash = PasswordHasher::hashPassword($password, $this->getAlgorithm(), $this->getSalt(), $this->getSpinCount());
673 
674  return $this->getPassword() === $hash;
675  }
676 
680  public function __clone()
681  {
682  $vars = get_object_vars($this);
683  foreach ($vars as $key => $value) {
684  if (is_object($value)) {
685  $this->$key = clone $value;
686  } else {
687  $this->$key = $value;
688  }
689  }
690  }
691 }
getSelectUnlockedCells()
Get SelectUnlockedCells.
Definition: Protection.php:557
setSelectUnlockedCells($pValue)
Set SelectUnlockedCells.
Definition: Protection.php:569
__clone()
Implement PHP __clone to create a deep clone, not just a shallow copy.
Definition: Protection.php:680
getSelectLockedCells()
Get SelectLockedCells.
Definition: Protection.php:461
setAlgorithm(string $algorithm)
Set algorithm name.
Definition: Protection.php:626
static hashPassword(string $password, string $algorithm='', string $salt='', int $spinCount=10000)
Create a password hash from a given string by a specific algorithm.
setSalt(string $salt)
Set salt value.
Definition: Protection.php:642
setPivotTables($pValue)
Set PivotTables.
Definition: Protection.php:545
setInsertHyperlinks($pValue)
Set InsertHyperlinks.
Definition: Protection.php:401
setFormatCells($pValue)
Set FormatCells.
Definition: Protection.php:281
setInsertColumns($pValue)
Set InsertColumns.
Definition: Protection.php:353
setFormatColumns($pValue)
Set FormatColumns.
Definition: Protection.php:305
__construct()
Create a new Protection.
Definition: Protection.php:163
setSpinCount(int $spinCount)
Set spin count.
Definition: Protection.php:658
generateSalt()
Create a pseudorandom string.
Definition: Protection.php:610
setDeleteColumns($pValue)
Set DeleteColumns.
Definition: Protection.php:425
setPassword($pValue, $pAlreadyHashed=false)
Set Password.
Definition: Protection.php:594
verify(string $password)
Verify that the given non-hashed password can "unlock" the protection.
Definition: Protection.php:666
isProtectionEnabled()
Is some sort of protection enabled?
Definition: Protection.php:172
$key
Definition: croninfo.php:18
setSelectLockedCells($pValue)
Set SelectLockedCells.
Definition: Protection.php:473