Security.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Document;
  3. use PhpOffice\PhpSpreadsheet\Shared\PasswordHasher;
  4. class Security
  5. {
  6. /**
  7. * LockRevision.
  8. *
  9. * @var bool
  10. */
  11. private $lockRevision = false;
  12. /**
  13. * LockStructure.
  14. *
  15. * @var bool
  16. */
  17. private $lockStructure = false;
  18. /**
  19. * LockWindows.
  20. *
  21. * @var bool
  22. */
  23. private $lockWindows = false;
  24. /**
  25. * RevisionsPassword.
  26. *
  27. * @var string
  28. */
  29. private $revisionsPassword = '';
  30. /**
  31. * WorkbookPassword.
  32. *
  33. * @var string
  34. */
  35. private $workbookPassword = '';
  36. /**
  37. * Create a new Document Security instance.
  38. */
  39. public function __construct()
  40. {
  41. }
  42. /**
  43. * Is some sort of document security enabled?
  44. *
  45. * @return bool
  46. */
  47. public function isSecurityEnabled()
  48. {
  49. return $this->lockRevision ||
  50. $this->lockStructure ||
  51. $this->lockWindows;
  52. }
  53. /**
  54. * Get LockRevision.
  55. *
  56. * @return bool
  57. */
  58. public function getLockRevision()
  59. {
  60. return $this->lockRevision;
  61. }
  62. /**
  63. * Set LockRevision.
  64. *
  65. * @param bool $pValue
  66. *
  67. * @return Security
  68. */
  69. public function setLockRevision($pValue)
  70. {
  71. $this->lockRevision = $pValue;
  72. return $this;
  73. }
  74. /**
  75. * Get LockStructure.
  76. *
  77. * @return bool
  78. */
  79. public function getLockStructure()
  80. {
  81. return $this->lockStructure;
  82. }
  83. /**
  84. * Set LockStructure.
  85. *
  86. * @param bool $pValue
  87. *
  88. * @return Security
  89. */
  90. public function setLockStructure($pValue)
  91. {
  92. $this->lockStructure = $pValue;
  93. return $this;
  94. }
  95. /**
  96. * Get LockWindows.
  97. *
  98. * @return bool
  99. */
  100. public function getLockWindows()
  101. {
  102. return $this->lockWindows;
  103. }
  104. /**
  105. * Set LockWindows.
  106. *
  107. * @param bool $pValue
  108. *
  109. * @return Security
  110. */
  111. public function setLockWindows($pValue)
  112. {
  113. $this->lockWindows = $pValue;
  114. return $this;
  115. }
  116. /**
  117. * Get RevisionsPassword (hashed).
  118. *
  119. * @return string
  120. */
  121. public function getRevisionsPassword()
  122. {
  123. return $this->revisionsPassword;
  124. }
  125. /**
  126. * Set RevisionsPassword.
  127. *
  128. * @param string $pValue
  129. * @param bool $pAlreadyHashed If the password has already been hashed, set this to true
  130. *
  131. * @return Security
  132. */
  133. public function setRevisionsPassword($pValue, $pAlreadyHashed = false)
  134. {
  135. if (!$pAlreadyHashed) {
  136. $pValue = PasswordHasher::hashPassword($pValue);
  137. }
  138. $this->revisionsPassword = $pValue;
  139. return $this;
  140. }
  141. /**
  142. * Get WorkbookPassword (hashed).
  143. *
  144. * @return string
  145. */
  146. public function getWorkbookPassword()
  147. {
  148. return $this->workbookPassword;
  149. }
  150. /**
  151. * Set WorkbookPassword.
  152. *
  153. * @param string $pValue
  154. * @param bool $pAlreadyHashed If the password has already been hashed, set this to true
  155. *
  156. * @return Security
  157. */
  158. public function setWorkbookPassword($pValue, $pAlreadyHashed = false)
  159. {
  160. if (!$pAlreadyHashed) {
  161. $pValue = PasswordHasher::hashPassword($pValue);
  162. }
  163. $this->workbookPassword = $pValue;
  164. return $this;
  165. }
  166. /**
  167. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  168. */
  169. public function __clone()
  170. {
  171. $vars = get_object_vars($this);
  172. foreach ($vars as $key => $value) {
  173. if (is_object($value)) {
  174. $this->$key = clone $value;
  175. } else {
  176. $this->$key = $value;
  177. }
  178. }
  179. }
  180. }