PageBuilder.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
  78. $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
  79. $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
  80. if ($this->conf->exists('general.header_link')) {
  81. $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
  82. }
  83. $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
  84. $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
  85. $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
  86. $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
  87. $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
  88. $this->tpl->assign('token', $this->token);
  89. if ($this->linkDB !== null) {
  90. $this->tpl->assign('tags', $this->linkDB->linksCountPerTag());
  91. }
  92. // To be removed with a proper theme configuration.
  93. $this->tpl->assign('conf', $this->conf);
  94. }
  95. /**
  96. * The following assign() method is basically the same as RainTPL (except lazy loading)
  97. *
  98. * @param string $placeholder Template placeholder.
  99. * @param mixed $value Value to assign.
  100. */
  101. public function assign($placeholder, $value)
  102. {
  103. if ($this->tpl === false) {
  104. $this->initialize();
  105. }
  106. $this->tpl->assign($placeholder, $value);
  107. }
  108. /**
  109. * Assign an array of data to the template builder.
  110. *
  111. * @param array $data Data to assign.
  112. *
  113. * @return false if invalid data.
  114. */
  115. public function assignAll($data)
  116. {
  117. if ($this->tpl === false) {
  118. $this->initialize();
  119. }
  120. if (empty($data) || !is_array($data)){
  121. return false;
  122. }
  123. foreach ($data as $key => $value) {
  124. $this->assign($key, $value);
  125. }
  126. return true;
  127. }
  128. /**
  129. * Render a specific page (using a template file).
  130. * e.g. $pb->renderPage('picwall');
  131. *
  132. * @param string $page Template filename (without extension).
  133. */
  134. public function renderPage($page)
  135. {
  136. if ($this->tpl === false) {
  137. $this->initialize();
  138. }
  139. $this->tpl->draw($page);
  140. }
  141. /**
  142. * Render a 404 page (uses the template : tpl/404.tpl)
  143. * usage : $PAGE->render404('The link was deleted')
  144. *
  145. * @param string $message A messate to display what is not found
  146. */
  147. public function render404($message = '')
  148. {
  149. if (empty($message)) {
  150. $message = t('The page you are trying to reach does not exist or has been deleted.');
  151. }
  152. header($_SERVER['SERVER_PROTOCOL'] .' '. t('404 Not Found'));
  153. $this->tpl->assign('error_message', $message);
  154. $this->renderPage('404');
  155. }
  156. }