model.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 0.22
  11. */
  12. namespace PrivateBin;
  13. use PrivateBin\model\paste;
  14. /**
  15. * model
  16. *
  17. * Factory of PrivateBin instance models.
  18. */
  19. class model
  20. {
  21. /**
  22. * Configuration.
  23. *
  24. * @var configuration
  25. */
  26. private $_conf;
  27. /**
  28. * Data storage.
  29. *
  30. * @var privatebin_abstract
  31. */
  32. private $_store = null;
  33. /**
  34. * Factory constructor.
  35. *
  36. * @param configuration $conf
  37. * @return void
  38. */
  39. public function __construct(configuration $conf)
  40. {
  41. $this->_conf = $conf;
  42. }
  43. /**
  44. * Get a paste, optionally a specific instance.
  45. *
  46. * @param string $pasteId
  47. * @return model_paste
  48. */
  49. public function getPaste($pasteId = null)
  50. {
  51. $paste = new paste($this->_conf, $this->_getStore());
  52. if ($pasteId !== null) {
  53. $paste->setId($pasteId);
  54. }
  55. return $paste;
  56. }
  57. /**
  58. * Checks if a purge is necessary and triggers it if yes.
  59. *
  60. * @return void
  61. */
  62. public function purge()
  63. {
  64. purgelimiter::setConfiguration($this->_conf);
  65. if (purgelimiter::canPurge()) {
  66. $this->_getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
  67. }
  68. }
  69. /**
  70. * Gets, and creates if neccessary, a store object
  71. *
  72. * @return privatebin_abstract
  73. */
  74. private function _getStore()
  75. {
  76. if ($this->_store === null) {
  77. $this->_store = forward_static_call(
  78. array($this->_conf->getKey('class', 'model'), 'getInstance'),
  79. $this->_conf->getSection('model_options')
  80. );
  81. }
  82. return $this->_store;
  83. }
  84. }