X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/TemplateParser.php diff --git a/includes/TemplateParser.php b/includes/TemplateParser.php new file mode 100644 index 00000000..2293dabb --- /dev/null +++ b/includes/TemplateParser.php @@ -0,0 +1,220 @@ +templateDir = $templateDir ?: __DIR__ . '/templates'; + $this->forceRecompile = $forceRecompile; + } + + /** + * Enable/disable the use of recursive partials. + * @param bool $enable + */ + public function enableRecursivePartials( $enable ) { + if ( $enable ) { + $this->compileFlags = $this->compileFlags | LightnCandy::FLAG_RUNTIMEPARTIAL; + } else { + $this->compileFlags = $this->compileFlags & ~LightnCandy::FLAG_RUNTIMEPARTIAL; + } + } + + /** + * Constructs the location of the the source Mustache template + * @param string $templateName The name of the template + * @return string + * @throws UnexpectedValueException If $templateName attempts upwards directory traversal + */ + protected function getTemplateFilename( $templateName ) { + // Prevent path traversal. Based on Language::isValidCode(). + // This is for paranoia. The $templateName should never come from + // untrusted input. + if ( + strcspn( $templateName, ":/\\\000&<>'\"%" ) !== strlen( $templateName ) + ) { + throw new UnexpectedValueException( "Malformed \$templateName: $templateName" ); + } + + return "{$this->templateDir}/{$templateName}.mustache"; + } + + /** + * Returns a given template function if found, otherwise throws an exception. + * @param string $templateName The name of the template (without file suffix) + * @return callable + * @throws RuntimeException + */ + protected function getTemplate( $templateName ) { + $templateKey = $templateName . '|' . $this->compileFlags; + + // If a renderer has already been defined for this template, reuse it + if ( isset( $this->renderers[$templateKey] ) && + is_callable( $this->renderers[$templateKey] ) + ) { + return $this->renderers[$templateKey]; + } + + $filename = $this->getTemplateFilename( $templateName ); + + if ( !file_exists( $filename ) ) { + throw new RuntimeException( "Could not locate template: {$filename}" ); + } + + // Read the template file + $fileContents = file_get_contents( $filename ); + + // Generate a quick hash for cache invalidation + $fastHash = md5( $this->compileFlags . '|' . $fileContents ); + + // Fetch a secret key for building a keyed hash of the PHP code + $config = MediaWikiServices::getInstance()->getMainConfig(); + $secretKey = $config->get( 'SecretKey' ); + + if ( $secretKey ) { + // See if the compiled PHP code is stored in cache. + $cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING ); + $key = $cache->makeKey( 'template', $templateName, $fastHash ); + $code = $this->forceRecompile ? null : $cache->get( $key ); + + if ( $code ) { + // Verify the integrity of the cached PHP code + $keyedHash = substr( $code, 0, 64 ); + $code = substr( $code, 64 ); + if ( $keyedHash !== hash_hmac( 'sha256', $code, $secretKey ) ) { + // If the integrity check fails, don't use the cached code + // We'll update the invalid cache below + $code = null; + } + } + if ( !$code ) { + $code = $this->compileForEval( $fileContents, $filename ); + + // Prefix the cached code with a keyed hash (64 hex chars) as an integrity check + $cache->set( $key, hash_hmac( 'sha256', $code, $secretKey ) . $code ); + } + // If there is no secret key available, don't use cache + } else { + $code = $this->compileForEval( $fileContents, $filename ); + } + + $renderer = eval( $code ); + if ( !is_callable( $renderer ) ) { + throw new RuntimeException( "Requested template, {$templateName}, is not callable" ); + } + $this->renderers[$templateKey] = $renderer; + return $renderer; + } + + /** + * Wrapper for compile() function that verifies successful compilation and strips + * out the 'compile( $fileContents ); + + if ( !$code ) { + throw new RuntimeException( "Could not compile template: {$filename}" ); + } + + // Strip the " $this->compileFlags, + 'basedir' => $this->templateDir, + 'fileext' => '.mustache', + ] + ); + } + + /** + * Returns HTML for a given template by calling the template function with the given args + * + * @code + * echo $templateParser->processTemplate( + * 'ExampleTemplate', + * [ + * 'username' => $user->getName(), + * 'message' => 'Hello!' + * ] + * ); + * @endcode + * @param string $templateName The name of the template + * @param mixed $args + * @param array $scopes + * @return string + */ + public function processTemplate( $templateName, $args, array $scopes = [] ) { + $template = $this->getTemplate( $templateName ); + return call_user_func( $template, $args, $scopes ); + } +}