{header-html} {body} "; /** * The main content template that is used to render normal wiki pages. * @var string * @package core */ public static $main_content_template = "{navigation-bar}

{sitename}

{content}
{extra} {navigation-bar-bottom} {all-pages-datalist}"; /** * A specially minified content template that doesn't include the navbar and * other elements not suitable for printing. * @var string * @package core */ public static $minimal_content_template = "
{content}
"; /** * An array of items indicating the resources to ask the web server to push * down to the client with HTTP/2.0 server push. * Format: [ [type, path], [type, path], .... ] * @var array[] */ protected static $http2_push_items = []; /** * A string of extrar HTML that should be included at the bottom of the page . * @var string */ private static $extraHeaderHTML = ""; /** * The javascript snippets that will be included in the page. * @var string[] * @package core */ private static $jsSnippets = []; /** * The urls of the external javascript files that should be referenced * by the page. * @var string[] * @package core */ private static $jsLinks = []; /** * The navigation bar divider. * @package core * @var string */ public static $nav_divider = " | "; /** * An array of functions that have been registered to process the * find / replace array before the page is rendered. Note that the function * should take a *reference* to an array as its only argument. * @var array * @package core */ protected static $part_processors = []; /** * Registers a function as a part post processor. * This function's use is more complicated to explain. Pepperminty Wiki * renders pages with a very simple templating system. For example, in the * template a page's content is denoted by `{content}`. A function * registered here will be passed all the components of a page _just_ * before they are dropped into the template. Note that the function you * pass in here should take a *reference* to the components, as the return * value of the function passed is discarded. * @package core * @param callable $function The part preprocessor to register. */ public static function register_part_preprocessor($function) { global $settings; // Make sure that the function we are about to register is valid if(!is_callable($function)) { http_response_code(500); $admin_email = hide_email($settings->admindetails_email); exit(page_renderer::render("$settings->sitename - Module Error", "

$settings->sitename has got a misbehaving module installed that tried to register an invalid HTML handler with the page renderer. Please contact $settings->sitename's administrator {$settings->admindetails_name} at $admin_email.")); } self::$part_processors[] = $function; return true; } /** * Renders a HTML page with the content specified. * @package core * @param string $title The title of the page. * @param string $content The (HTML) content of the page. * @param bool $body_template The HTML content template to use. * @return string The rendered HTML, ready to send to the client :-) */ public static function render($title, $content, $body_template = false) { global $settings, $start_time, $version; if($body_template === false) $body_template = self::$main_content_template; if(strlen($settings->logo_url) > 0) { // A logo url has been specified $logo_html = ""; switch($settings->logo_position) { case "left": $logo_html = "$logo_html $settings->sitename"; break; case "right": $logo_html .= " $settings->sitename"; break; default: throw new Exception("Invalid logo_position '$settings->logo_position'. Valid values are either \"left\" or \"right\" and are case sensitive."); } } // Push the logo via HTTP/2.0 if possible if($settings->favicon[0] === "/") self::$http2_push_items[] = ["image", $settings->favicon]; $parts = [ "{body}" => $body_template, "{sitename}" => $logo_html, "{version}" => $version, "{favicon-url}" => $settings->favicon, "{header-html}" => self::get_header_html(), "{navigation-bar}" => self::render_navigation_bar($settings->nav_links, $settings->nav_links_extra, "top"), "{navigation-bar-bottom}" => self::render_navigation_bar($settings->nav_links_bottom, [], "bottom"), "{admin-details-name}" => $settings->admindetails_name, "{admin-details-email}" => $settings->admindetails_email, "{admins-name-list}" => implode(", ", array_map(function($username) { return page_renderer::render_username($username); }, $settings->admins)), "{generation-date}" => date("l jS \of F Y \a\\t h:ia T"), "{all-pages-datalist}" => self::generate_all_pages_datalist(), "{footer-message}" => $settings->footer_message, /// Secondary Parts /// "{content}" => $content, "{extra}" => "", "{title}" => $title, ]; // Pass the parts through the part processors foreach(self::$part_processors as $function) { $function($parts); } $result = self::$html_template; $result = str_replace(array_keys($parts), array_values($parts), $result); $result = str_replace("{generation-time-taken}", round((microtime(true) - $start_time)*1000, 2), $result); // Send the HTTP/2.0 server push indicators if possible - but not if we're sending a redirect page if(!headers_sent() && (http_response_code() < 300 || http_response_code() >= 400)) self::send_server_push_indicators(); return $result; } /** * Renders a normal HTML page. * @package core * @param string $title The title of the page. * @param string $content The content of the page. * @return string The rendered page. */ public static function render_main($title, $content) { return self::render($title, $content, self::$main_content_template); } /** * Renders a minimal HTML page. Useful for printable pages. * @package core * @param string $title The title of the page. * @param string $content The content of the page. * @return string The rendered page. */ public static function render_minimal($title, $content) { return self::render($title, $content, self::$minimal_content_template); } /** * Sends the currently registered HTTP2 server push items to the client. * @return int|false The number of resource hints included in the link: header, or false if server pushing is disabled. */ public static function send_server_push_indicators() { global $settings; if(!$settings->http2_server_push) return false; // Render the preload directives $link_header_parts = []; foreach(self::$http2_push_items as $push_item) $link_header_parts[] = "<{$push_item[1]}>; rel=preload; as={$push_item[0]}"; // Send them in a link: header if(!empty($link_header_parts)) header("link: " . implode(", ", $link_header_parts)); return count(self::$http2_push_items); } /** * Renders the header HTML. * @package core * @return string The rendered HTML that goes in the header. */ public static function get_header_html() { global $settings; $result = self::$extraHeaderHTML; $result .= self::get_css_as_html(); $result .= self::_get_js(); // We can't use module_exists here because sometimes global $modules // hasn't populated yet when we get called O.o if(class_exists("search")) $result .= "\t\t\n"; if(!empty($settings->enable_math_rendering)) { $result .= ""; } return $result; } /** * Figures out whether $settings->css is a url, or a string of css. * A url is something starting with "protocol://" or simply a "/". * Before v0.20, this method took no arguments and checked $settings->css directly. * @apiVerion 0.20.0 * @param string $str The CSS string to check. * @return bool True if it's a url - false if we assume it's a string of css. */ public static function is_css_url($str) { global $settings; return preg_match("/^[^\/]*\/\/|^\/[^\*]/", $str); } /** * Renders all the CSS as HTML. * @package core * @return string The css as HTML, ready to be included in the HTML header. */ public static function get_css_as_html() { global $settings, $defaultCSS; $result = ""; $css = ""; if(self::is_css_url($settings->css)) { if($settings->css[0] === "/") // Push it if it's a relative resource self::add_server_push_indicator("style", $settings->css); $result .= "\n"; } else { $css .= $settings->css == "auto" ? $defaultCSS : $settings->css; if(!empty($settings->optimize_pages)) $css = minify_css($css); } if(!empty($settings->css_custom)) { if(self::is_css_url($settings->css_custom)) { if($settings->css_custom[0] === "/") // Push it if it's a relative resource self::add_server_push_indicator("style", $settings->css); $result .= "\n"; } $css .= "\n/*** Custom CSS ***/\n"; $css .= !empty($settings->optimize_pages) ? minify_css($settings->css_custom) : $settings->css_custom; $css .= "\n/******************/"; } $result .= "\n"; return $result; } /** * Adds the specified url to a javascript file as a reference to the page. * @package core * @param string $scriptUrl The url of the javascript file to reference. */ public static function add_js_link(string $scriptUrl) { static::$jsLinks[] = $scriptUrl; } /** * Adds a javascript snippet to the page. * @package core * @param string $script The snippet of javascript to add. */ public static function add_js_snippet(string $script) { static::$jsSnippets[] = $script; } /** * Renders the included javascript header for inclusion in the final * rendered page. * @package core * @return string The rendered javascript ready for inclusion in the page. */ private static function _get_js() { $result = "\n"; foreach(static::$jsSnippets as $snippet) $result .= "\n"; foreach(static::$jsLinks as $link) { // Push it via HTTP/2.0 if it's relative if($link[0] === "/") self::add_server_push_indicator("script", $link); $result .= "\n"; } return $result; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * Adds a string of HTML to the header of the rendered page. * @param string $html The string of HTML to add. */ public static function add_header_html($html) { self::$extraHeaderHTML .= $html; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * Adds a resource to the list of items to indicate that the web server should send via HTTP/2.0 Server Push. * Note: Only specify static files here, as you might end up with strange (and possibly dangerous) results! * @param string $type The resource type. See https://fetch.spec.whatwg.org/#concept-request-destination for more information. * @param string $path The *relative url path* to the resource. */ public static function add_server_push_indicator($type, $path) { self::$http2_push_items[] = [ $type, $path ]; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * Renders a navigation bar from an array of links. See * $settings->nav_links for format information. * @package core * @param array $nav_links The links to add to the navigation bar. * @param array $nav_links_extra The extra nav links to add to * the "More..." menu. * @param string $class The class(es) to assign to the rendered * navigation bar. */ public static function render_navigation_bar($nav_links, $nav_links_extra, $class = "") { global $settings, $env; $mega_menu = false; if(is_object($nav_links)) { $mega_menu = true; $class = trim("$class mega-menu"); $links_list = []; $keys = array_keys(get_object_vars($nav_links)); foreach($keys as $key) { $links_list[] = "category\0$key"; $links_list = array_merge( $links_list, $nav_links->$key ); } $nav_links = $links_list; } $result = "

"; return $result; } /** * Renders a username for inclusion in a page. * @package core * @param string $name The username to render. * @return string The username rendered in HTML. */ public static function render_username($name) { global $settings; $result = ""; $result .= ""; if($settings->avatars_show) $result .= " "; if(in_array($name, $settings->admins)) $result .= $settings->admindisplaychar; $result .= htmlentities($name); $result .= ""; return $result; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * Renders the datalist for the search box as HTML. * @package core * @return string The search box datalist as HTML. */ public static function generate_all_pages_datalist() { global $settings, $pageindex; $result = "\n"; // If dynamic page sugggestions are enabled, then we should send a loading message instead. if($settings->dynamic_page_suggestion_count > 0) { $result .= ""; return $result; } } // HTTP/2.0 Server Push static items foreach($settings->http2_server_push_items as $push_item) { page_renderer::add_server_push_indicator($push_item[0], $push_item[1]); } // Math rendering support if(!empty($settings->enable_math_rendering)) { page_renderer::add_js_link("https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"); } // alt+enter support in the search box page_renderer::add_js_snippet('// Alt + Enter support in the top search box window.addEventListener("load", function(event) { document.querySelector("input[type=search]").addEventListener("keyup", function(event) { // Listen for Alt + Enter if(event.keyCode == 13 && event.altKey) { event.stopPropagation(); event.preventDefault(); event.cancelBubble = true; event.target.form.setAttribute("target", "_blank"); event.target.form.submit(); event.target.form.removeAttribute("target"); return false; // Required by some browsers } }); }); ');