Protection.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Metadata;
  18. use PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder;
  19. use PhpOffice\PhpWord\SimpleType\DocProtect;
  20. /**
  21. * Document protection class
  22. *
  23. * @since 0.12.0
  24. * @see http://www.datypic.com/sc/ooxml/t-w_CT_DocProtect.html
  25. */
  26. class Protection
  27. {
  28. /**
  29. * Editing restriction none|readOnly|comments|trackedChanges|forms
  30. *
  31. * @var string
  32. * @see http://www.datypic.com/sc/ooxml/a-w_edit-1.html
  33. */
  34. private $editing;
  35. /**
  36. * password
  37. *
  38. * @var string
  39. */
  40. private $password;
  41. /**
  42. * Iterations to Run Hashing Algorithm
  43. *
  44. * @var int
  45. */
  46. private $spinCount = 100000;
  47. /**
  48. * Cryptographic Hashing Algorithm (see constants defined in \PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder)
  49. *
  50. * @var string
  51. */
  52. private $algorithm = PasswordEncoder::ALGORITHM_SHA_1;
  53. /**
  54. * Salt for Password Verifier
  55. *
  56. * @var string
  57. */
  58. private $salt;
  59. /**
  60. * Create a new instance
  61. *
  62. * @param string $editing
  63. */
  64. public function __construct($editing = null)
  65. {
  66. if ($editing != null) {
  67. $this->setEditing($editing);
  68. }
  69. }
  70. /**
  71. * Get editing protection
  72. *
  73. * @return string
  74. */
  75. public function getEditing()
  76. {
  77. return $this->editing;
  78. }
  79. /**
  80. * Set editing protection
  81. *
  82. * @param string $editing Any value of \PhpOffice\PhpWord\SimpleType\DocProtect
  83. * @return self
  84. */
  85. public function setEditing($editing = null)
  86. {
  87. DocProtect::validate($editing);
  88. $this->editing = $editing;
  89. return $this;
  90. }
  91. /**
  92. * Get password
  93. *
  94. * @return string
  95. */
  96. public function getPassword()
  97. {
  98. return $this->password;
  99. }
  100. /**
  101. * Set password
  102. *
  103. * @param string $password
  104. * @return self
  105. */
  106. public function setPassword($password)
  107. {
  108. $this->password = $password;
  109. return $this;
  110. }
  111. /**
  112. * Get count for hash iterations
  113. *
  114. * @return int
  115. */
  116. public function getSpinCount()
  117. {
  118. return $this->spinCount;
  119. }
  120. /**
  121. * Set count for hash iterations
  122. *
  123. * @param int $spinCount
  124. * @return self
  125. */
  126. public function setSpinCount($spinCount)
  127. {
  128. $this->spinCount = $spinCount;
  129. return $this;
  130. }
  131. /**
  132. * Get algorithm
  133. *
  134. * @return string
  135. */
  136. public function getAlgorithm()
  137. {
  138. return $this->algorithm;
  139. }
  140. /**
  141. * Set algorithm
  142. *
  143. * @param string $algorithm
  144. * @return self
  145. */
  146. public function setAlgorithm($algorithm)
  147. {
  148. $this->algorithm = $algorithm;
  149. return $this;
  150. }
  151. /**
  152. * Get salt
  153. *
  154. * @return string
  155. */
  156. public function getSalt()
  157. {
  158. return $this->salt;
  159. }
  160. /**
  161. * Set salt. Salt HAS to be 16 characters, or an exception will be thrown.
  162. *
  163. * @param string $salt
  164. * @throws \InvalidArgumentException
  165. * @return self
  166. */
  167. public function setSalt($salt)
  168. {
  169. if ($salt !== null && strlen($salt) !== 16) {
  170. throw new \InvalidArgumentException('salt has to be of exactly 16 bytes length');
  171. }
  172. $this->salt = $salt;
  173. return $this;
  174. }
  175. }