PageBuilder.php 5.9 KB

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