PageBuilder.php 4.8 KB

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