Pure PHP Extension - execute code on every page load

Sometimes we need to be able to execute code on every page load or post back - for instance to add content to the template (e.g. a search box), to synchronize data with an external resource, to clean up old data, or similar tasks.
For that the Pure PHP Extension features .run.php files. Files with the .run.php suffix will be executed on every page load.
example.run.php
<?php

// ==================================================================================

// This file is called multiple times, during every stage of the life cycle. Use the
// $cfg configuration array to make sure the code is only executed once, or during
// the stages that makes sense. Completely removing the line below is NOT recommended!
// By default your code gets executed during the PreTemplateUpdate stage.
// Possible values: PreInit, Init, InitComplete, PreRender, Render (only if this
// extension executes privileged), RenderComplete, PreTemplateUpdate,
// TemplateUpdateComplete, PreOutput, OutputComplete, Unload, Finalize.
if ($cfg["Stage"] === "PreTemplateUpdate") {

// ==================================================================================

// YOUR CODE GOES HERE.
// Using print/echo will not work - this file is not intended to print
// content to the content area. Either use the templating engine if content
// needs to be changed ($this->context->GetTemplate()) or create
// a Pure PHP extension with the .extension.php suffix, which adds content
// directly to the content area when using print/echo.

$content = "
<div style=\"border: 5px solid green; background-color: whitesmoke; padding: 10px;
        position: absolute; z-index: 999; top: 20px; left: 20px\">
    Please enter your name and click Go:
    <input type=\"text\" name=\"PurePhpNameBox\">
    <input type=\"submit\" value=\"Go!\">
";

if (isset($_POST["PurePhpNameBox"]) && $_POST["PurePhpNameBox"] !== "")
    $content .= "<br><br>Welcome <b>" . $_POST["PurePhpNameBox"] . "</b>, good to see you!";

$content .= "</div>";

$template = $this->context->GetTemplate();
$template->SetBodyContent($content . $template->GetBodyContent());

// ==================================================================================

}

?>
The example above adds a box in the upper left corner, allowing the user to enter his/her name, and have a brief welcome message displayed. Using a place holder to place the box in the template would probably be a good idea, although it would require the user to modify the template, which for some would be to complicated, which is why a floating box was used in this example.