PageBuilder.php 6.6 KB

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