model.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /**
  13. * model
  14. *
  15. * Factory of PrivateBin instance models.
  16. */
  17. class model
  18. {
  19. /**
  20. * Configuration.
  21. *
  22. * @var configuration
  23. */
  24. private $_conf;
  25. /**
  26. * Data storage.
  27. *
  28. * @var privatebin_abstract
  29. */
  30. private $_store = null;
  31. /**
  32. * Factory constructor.
  33. *
  34. * @param configuration $conf
  35. * @return void
  36. */
  37. public function __construct(configuration $conf)
  38. {
  39. $this->_conf = $conf;
  40. }
  41. /**
  42. * Get a paste, optionally a specific instance.
  43. *
  44. * @param string $pasteId
  45. * @return model_paste
  46. */
  47. public function getPaste($pasteId = null)
  48. {
  49. $paste = new model_paste($this->_conf, $this->_getStore());
  50. if ($pasteId !== null) $paste->setId($pasteId);
  51. return $paste;
  52. }
  53. /**
  54. * Checks if a purge is necessary and triggers it if yes.
  55. *
  56. * @return void
  57. */
  58. public function purge()
  59. {
  60. purgelimiter::setConfiguration($this->_conf);
  61. if (purgelimiter::canPurge())
  62. {
  63. $this->_getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
  64. }
  65. }
  66. /**
  67. * Gets, and creates if neccessary, a store object
  68. *
  69. * @return privatebin_abstract
  70. */
  71. private function _getStore()
  72. {
  73. if ($this->_store === null)
  74. {
  75. $this->_store = forward_static_call(
  76. array($this->_conf->getKey('class', 'model'), 'getInstance'),
  77. $this->_conf->getSection('model_options')
  78. );
  79. }
  80. return $this->_store;
  81. }
  82. }