Protection.php 9.2 KB

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