model.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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) $paste->setId($pasteId);
  53. return $paste;
  54. }
  55. /**
  56. * Checks if a purge is necessary and triggers it if yes.
  57. *
  58. * @return void
  59. */
  60. public function purge()
  61. {
  62. purgelimiter::setConfiguration($this->_conf);
  63. if (purgelimiter::canPurge())
  64. {
  65. $this->_getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
  66. }
  67. }
  68. /**
  69. * Gets, and creates if neccessary, a store object
  70. *
  71. * @return privatebin_abstract
  72. */
  73. private function _getStore()
  74. {
  75. // FIXME
  76. // Workaround so that config value don't need to be changed
  77. $callable = str_replace(
  78. array('privatebin_data', 'privatebin_db'),
  79. array('PrivateBin\\data\\data', 'PrivateBin\\data\\db'),
  80. $this->_conf->getKey('class', 'model')
  81. );
  82. if ($this->_store === null)
  83. {
  84. $this->_store = forward_static_call(
  85. array($callable, 'getInstance'),
  86. $this->_conf->getSection('model_options')
  87. );
  88. }
  89. return $this->_store;
  90. }
  91. }