PageBuilder.php 5.6 KB

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