PageBuilder.php 5.3 KB

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