PageBuilder.php 6.7 KB

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