PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/pages/PageBase.php

http://manx.codeplex.com
PHP | 263 lines | 226 code | 28 blank | 9 comment | 15 complexity | 32030c0989e6a3ac21daae8ae51170fe MD5 | raw file
  1. <?php
  2. require_once 'IManx.php';
  3. class MenuType
  4. {
  5. const Undefined = 0;
  6. const Search = 1;
  7. const About = 2;
  8. const Help = 3;
  9. const Publication = 4;
  10. const Mirror = 5;
  11. const Company = 6;
  12. const Copy = 7;
  13. const Site = 8;
  14. const UrlWizard = 9;
  15. const SizeReport = 10;
  16. const MD5Report = 11;
  17. const BitSavers = 12;
  18. const News = 13;
  19. }
  20. abstract class PageBase
  21. {
  22. /** @var IManx */
  23. protected $_manx;
  24. /** @var IManxDatabase */
  25. protected $_manxDb;
  26. protected $_topDir;
  27. /** @var IUser */
  28. protected $_user;
  29. public function __construct(IManx $manx)
  30. {
  31. $this->_manx = $manx;
  32. $this->_manxDb = $manx->getDatabase();
  33. $this->_topDir = array_key_exists('PATH_INFO', $_SERVER) ?
  34. str_repeat('../', count(explode('/', $_SERVER['PATH_INFO'])) - 1)
  35. : '';
  36. $this->_user = $this->_manx->getUserFromSession();
  37. }
  38. public function __destruct()
  39. {
  40. $this->_manx = null;
  41. }
  42. public function renderPage()
  43. {
  44. $this->renderHeader();
  45. $this->renderBody();
  46. }
  47. protected function getTitle()
  48. {
  49. return "Manx";
  50. }
  51. protected function getMenuType()
  52. {
  53. return MenuType::Undefined;
  54. }
  55. protected function renderLink($rel, $type, $href, $attributes = array())
  56. {
  57. $attributeText = '';
  58. foreach ($attributes as $name => $value)
  59. {
  60. $attributeText .= sprintf('%s="%s" ', $name, htmlspecialchars(trim($value)));
  61. }
  62. printf('<link rel="%s" type="%s" href="%s" %s/>' . "\n", $rel, $type, $this->_topDir . $href, $attributeText);
  63. }
  64. protected function renderHeader()
  65. {
  66. header("Content-Type: text/html; charset=utf-8");
  67. print <<<EOH
  68. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/tr/html4/strict.dtd">
  69. <html lang="en">
  70. <head>
  71. EOH;
  72. printf("<title>%s</title>\n", $this->getTitle());
  73. $this->renderLink("stylesheet", "text/css", "assets/manx.css");
  74. $this->renderLink("shortcut icon", "image/x-icon", "assets/manx.ico");
  75. $this->renderLink("alternate", "application/rss+xml", "rss.php", array('title' => 'Manx New Documents'));
  76. $this->renderHeaderContent();
  77. print "</head>\n";
  78. }
  79. protected function renderHeaderContent()
  80. {
  81. }
  82. protected function renderBody()
  83. {
  84. $this->renderBodyHeader();
  85. $this->renderBodyContent();
  86. $this->renderBodyFooter();
  87. }
  88. abstract protected function renderBodyContent();
  89. private function renderFirstMenuItem($selected, $href, $text)
  90. {
  91. $this->renderMenuItemWithAttributes($selected, 'firston', 'class="first" ', $text, $href);
  92. }
  93. private function renderMenuItem($selected, $href, $text)
  94. {
  95. $this->renderMenuItemWithAttributes($selected, 'on', '', $text, $href);
  96. }
  97. private function renderMenuItemWithAttributes($selected, $selectedClass, $unselectedAttribute, $text, $href)
  98. {
  99. if ($selected)
  100. {
  101. printf('<a class="%s">%s</a>', $selectedClass, $text);
  102. }
  103. else
  104. {
  105. printf('<a %shref="%s">%s</a>', $unselectedAttribute, $this->_topDir . $href, $text);
  106. }
  107. }
  108. private function renderMenuSeparator()
  109. {
  110. print '<span class="nodisp"> | </span>';
  111. }
  112. private function renderAdminMenu($menu)
  113. {
  114. if ($this->_user->isAdmin())
  115. {
  116. print "<div class=\"menu\">\n";
  117. //$this->renderMenuSeparator();
  118. //$this->renderMenuItem($menu == MenuType::Company, "company.php", "Company");
  119. $this->renderFirstMenuItem($menu == MenuType::UrlWizard, "url-wizard.php", "URL Wizard");
  120. $this->renderMenuItem($menu == MenuType::BitSavers, "bitsavers.php", "BitSavers");
  121. //$this->renderMenuItem($menu == MenuType::Publication, "publication.php", "Publication");
  122. //$this->renderMenuSeparator();
  123. //$this->renderMenuItem($menu == MenuType::Copy, "copy.php", "Copy");
  124. //$this->renderMenuSeparator();
  125. $this->renderMenuSeparator();
  126. $this->renderMenuItem($menu == MenuType::Site, "site.php", "Site");
  127. $this->renderMenuSeparator();
  128. $this->renderMenuItem($menu == MenuType::Mirror, "mirror.php", "Mirror");
  129. $this->renderMenuItem($menu == MenuType::SizeReport, "size-report.php", "Size Report");
  130. $this->renderMenuItem($menu == MenuType::MD5Report, "md5-report.php", "MD5 Report");
  131. $this->renderMenuSeparator();
  132. print "</div>\n";
  133. }
  134. }
  135. private function renderMenu()
  136. {
  137. $menu = $this->getMenuType();
  138. print '<div class="menu">';
  139. $this->renderFirstMenuItem(($menu == MenuType::Search), "search.php", "Search");
  140. $this->renderMenuSeparator();
  141. $this->renderMenuItem(($menu == MenuType::News), "news.php", "News");
  142. $this->renderMenuSeparator();
  143. $this->renderMenuItem(($menu == MenuType::About), "about.php", "About");
  144. $this->renderMenuSeparator();
  145. $this->renderMenuItem(($menu == MenuType::Help), "help.php", "Help");
  146. print "</div>\n";
  147. $this->renderAdminMenu($menu);
  148. }
  149. protected function renderBodyHeader()
  150. {
  151. print <<<EOH
  152. <body id="VT100-NET">
  153. <div id="HEADER">
  154. EOH;
  155. $this->renderAuthorization();
  156. print <<<EOH
  157. <div id="LOGO"><h1><span>Manx &ndash; a catalogue of online computer manuals</span></h1></div>
  158. EOH;
  159. $this->renderMenu();
  160. print <<<EOH
  161. </div>
  162. <div class="det">
  163. EOH;
  164. }
  165. protected function renderBodyFooter()
  166. {
  167. print <<<EOH
  168. </div></body></html>
  169. EOH;
  170. }
  171. protected function redirect($target)
  172. {
  173. header("Status: 303 See Also");
  174. header("Location: " . $target);
  175. header("Content-Type: text/plain");
  176. print "Redirecting to " . $target;
  177. }
  178. public static function getRelativePrefixFromPathInfo()
  179. {
  180. return array_key_exists('PATH_INFO', $_SERVER) ?
  181. str_repeat('../', count(explode('/', $_SERVER['PATH_INFO'])) - 1)
  182. : '';
  183. }
  184. public static function getAbsolutePrefixFromScriptName($server)
  185. {
  186. $parts = explode('/', $server['SCRIPT_NAME']);
  187. array_shift($parts);
  188. if (count($parts) > 1)
  189. {
  190. array_pop($parts);
  191. $prefix = implode('/', $parts) . '/';
  192. }
  193. else
  194. {
  195. $prefix = '';
  196. }
  197. return $prefix;
  198. }
  199. public function renderLoginLink($server)
  200. {
  201. $redirect = $server['PHP_SELF'];
  202. if (array_key_exists('QUERY_STRING', $server) and strlen($server['QUERY_STRING']) > 0)
  203. {
  204. $redirect = sprintf("%s?%s", $redirect, $server['QUERY_STRING']);
  205. }
  206. $prefix = PageBase::getAbsolutePrefixFromScriptName($server);
  207. printf('<a href="https://%s/%slogin.php?redirect=%s">Login</a>',
  208. $server['SERVER_NAME'], $prefix, urlencode($redirect));
  209. }
  210. private function renderLogoutLink($server)
  211. {
  212. $prefix = PageBase::getRelativePrefixFromPathInfo();
  213. $absolutePrefix = PageBase::getAbsolutePrefixFromScriptName($server);
  214. printf('<a href="http://%s/%slogin.php?LOGO=1&redirect=%ssearch.php">Logout</a>',
  215. $server['SERVER_NAME'], $absolutePrefix, $prefix);
  216. }
  217. protected function renderAuthorization()
  218. {
  219. $user = User::getInstanceFromSession($this->_manxDb);
  220. print "<div id=\"AUTH\"><table>\n<tr><td>" . $user->displayName() . ' | ';
  221. if ($user->isLoggedIn())
  222. {
  223. $this->renderLogoutLink($_SERVER);
  224. }
  225. else
  226. {
  227. $this->renderLoginLink($_SERVER);
  228. }
  229. printf("</td></tr>\n"
  230. . "<tr><td>&nbsp;</td></tr>\n"
  231. . "<tr><td class=\"version\" align=\"right\">version %s</td></tr>\n"
  232. . "</table></div>\n",
  233. $this->_manxDb->getManxVersion());
  234. }
  235. }