PageBuilder.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. use Shaarli\Config\ConfigManager;
  3. /**
  4. * This class is in charge of building the final page.
  5. * (This is basically a wrapper around RainTPL which pre-fills some fields.)
  6. * $p = new PageBuilder();
  7. * $p->assign('myfield','myvalue');
  8. * $p->renderPage('mytemplate');
  9. */
  10. class PageBuilder
  11. {
  12. /**
  13. * @var RainTPL RainTPL instance.
  14. */
  15. private $tpl;
  16. /**
  17. * @var ConfigManager $conf Configuration Manager instance.
  18. */
  19. protected $conf;
  20. /**
  21. * PageBuilder constructor.
  22. * $tpl is initialized at false for lazy loading.
  23. *
  24. * @param ConfigManager $conf Configuration Manager instance (reference).
  25. */
  26. public function __construct(&$conf)
  27. {
  28. $this->tpl = false;
  29. $this->conf = $conf;
  30. }
  31. /**
  32. * Initialize all default tpl tags.
  33. */
  34. private function initialize()
  35. {
  36. $this->tpl = new RainTPL();
  37. try {
  38. $version = ApplicationUtils::checkUpdate(
  39. shaarli_version,
  40. $this->conf->get('resource.update_check'),
  41. $this->conf->get('updates.check_updates_interval'),
  42. $this->conf->get('updates.check_updates'),
  43. isLoggedIn(),
  44. $this->conf->get('updates.check_updates_branch')
  45. );
  46. $this->tpl->assign('newVersion', escape($version));
  47. $this->tpl->assign('versionError', '');
  48. } catch (Exception $exc) {
  49. logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
  50. $this->tpl->assign('newVersion', '');
  51. $this->tpl->assign('versionError', escape($exc->getMessage()));
  52. }
  53. $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
  54. $searchcrits = ''; // Search criteria
  55. if (!empty($_GET['searchtags'])) {
  56. $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
  57. }
  58. if (!empty($_GET['searchterm'])) {
  59. $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
  60. }
  61. $this->tpl->assign('searchcrits', $searchcrits);
  62. $this->tpl->assign('source', index_url($_SERVER));
  63. $this->tpl->assign('version', shaarli_version);
  64. $this->tpl->assign('scripturl', index_url($_SERVER));
  65. $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
  66. $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
  67. if ($this->conf->exists('general.header_link')) {
  68. $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
  69. }
  70. $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
  71. $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
  72. $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
  73. $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
  74. $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
  75. $this->tpl->assign('token', getToken($this->conf));
  76. // To be removed with a proper theme configuration.
  77. $this->tpl->assign('conf', $this->conf);
  78. }
  79. /**
  80. * The following assign() method is basically the same as RainTPL (except lazy loading)
  81. *
  82. * @param string $placeholder Template placeholder.
  83. * @param mixed $value Value to assign.
  84. */
  85. public function assign($placeholder, $value)
  86. {
  87. if ($this->tpl === false) {
  88. $this->initialize();
  89. }
  90. $this->tpl->assign($placeholder, $value);
  91. }
  92. /**
  93. * Assign an array of data to the template builder.
  94. *
  95. * @param array $data Data to assign.
  96. *
  97. * @return false if invalid data.
  98. */
  99. public function assignAll($data)
  100. {
  101. if ($this->tpl === false) {
  102. $this->initialize();
  103. }
  104. if (empty($data) || !is_array($data)){
  105. return false;
  106. }
  107. foreach ($data as $key => $value) {
  108. $this->assign($key, $value);
  109. }
  110. return true;
  111. }
  112. /**
  113. * Render a specific page (using a template file).
  114. * e.g. $pb->renderPage('picwall');
  115. *
  116. * @param string $page Template filename (without extension).
  117. */
  118. public function renderPage($page)
  119. {
  120. if ($this->tpl === false) {
  121. $this->initialize();
  122. }
  123. $this->tpl->draw($page);
  124. }
  125. /**
  126. * Render a 404 page (uses the template : tpl/404.tpl)
  127. * usage : $PAGE->render404('The link was deleted')
  128. *
  129. * @param string $message A messate to display what is not found
  130. */
  131. public function render404($message = 'The page you are trying to reach does not exist or has been deleted.')
  132. {
  133. header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
  134. $this->tpl->assign('error_message', $message);
  135. $this->renderPage('404');
  136. }
  137. }