render.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. define('page_template',
  3. '<html>
  4. <head>
  5. <meta http-equiv="cache-control" content="no-cache"/>
  6. <meta http-equiv="pragma" content="no-cache"/>
  7. <title>%s</title>
  8. %s
  9. </head>
  10. <body>
  11. %s
  12. <div id="content">
  13. <h1>%s</h1>
  14. %s
  15. </div>
  16. </body>
  17. </html>');
  18. define('logged_in_pat', 'You are logged in as %s (URL: %s)');
  19. /**
  20. * HTTP response line contstants
  21. */
  22. define('http_bad_request', 'HTTP/1.1 400 Bad Request');
  23. define('http_found', 'HTTP/1.1 302 Found');
  24. define('http_ok', 'HTTP/1.1 200 OK');
  25. define('http_internal_error', 'HTTP/1.1 500 Internal Error');
  26. /**
  27. * HTTP header constants
  28. */
  29. define('header_connection_close', 'Connection: close');
  30. define('header_content_text', 'Content-Type: text/plain; charset=us-ascii');
  31. define('redirect_message',
  32. 'Please wait; you are being redirected to <%s>');
  33. /**
  34. * Return a string containing an anchor tag containing the given URL
  35. *
  36. * The URL does not need to be quoted, but if text is passed in, then
  37. * it does.
  38. */
  39. function link_render($url, $text=null) {
  40. $esc_url = htmlspecialchars($url, ENT_QUOTES);
  41. $text = ($text === null) ? $esc_url : $text;
  42. return sprintf('<a href="%s">%s</a>', $esc_url, $text);
  43. }
  44. /**
  45. * Return an HTTP redirect response
  46. */
  47. function redirect_render($redir_url)
  48. {
  49. $headers = array(http_found,
  50. header_content_text,
  51. header_connection_close,
  52. 'Location: ' . $redir_url,
  53. );
  54. $body = sprintf(redirect_message, $redir_url);
  55. return array($headers, $body);
  56. }
  57. function navigation_render($msg, $items)
  58. {
  59. $what = link_render(buildURL(), 'PHP OpenID Server');
  60. if ($msg) {
  61. $what .= ' &mdash; ' . $msg;
  62. }
  63. if ($items) {
  64. $s = '<p>' . $what . '</p><ul class="bottom">';
  65. foreach ($items as $action => $text) {
  66. $url = buildURL($action);
  67. $s .= sprintf('<li>%s</li>', link_render($url, $text));
  68. }
  69. $s .= '</ul>';
  70. } else {
  71. $s = '<p class="bottom">' . $what . '</p>';
  72. }
  73. return sprintf('<div class="navigation">%s</div>', $s);
  74. }
  75. /**
  76. * Render an HTML page
  77. */
  78. function page_render($body, $user, $title, $h1=null, $login=false)
  79. {
  80. $h1 = $h1 ? $h1 : $title;
  81. if ($user) {
  82. $msg = sprintf(logged_in_pat, link_render(idURL($user), $user),
  83. link_render(idURL($user)));
  84. $nav = array('logout' => 'Log Out');
  85. $navigation = navigation_render($msg, $nav);
  86. } else {
  87. if (!$login) {
  88. $msg = link_render(buildURL('login'), 'Log In');
  89. $navigation = navigation_render($msg, array());
  90. } else {
  91. $navigation = '';
  92. }
  93. }
  94. $style = getStyle();
  95. $text = sprintf(page_template, $title, $style, $navigation, $h1, $body);
  96. // No special headers here
  97. $headers = array();
  98. return array($headers, $text);
  99. }
  100. ?>