<commit_before><?php

/*
 * This file is part of the 'octris/core' package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Octris\Core\Tpl;

/**
 * Sandbox to execute templates in.
 *
 * @copyright   copyright (c) 2010-2014 by <NAME>
 * @author      <NAME> <<EMAIL>>
 */
class Sandbox
{
    /**
     * Template data.
     *
     * @type    array
     */
    public $data = array();

    /**
     * Storage for sandbox internal data objects.
     *
     * @type    \Octris\Core\Tpl\Sandbox\Storage
     */
    protected $storage;

    /**
     * Internal storage for meta data required for block functions.
     *
     * @type    array
     */
    protected $meta = array();

    /**
     * Internal storage for cut/copied buffers.
     *
     * @type    array
     */
    protected $pastebin = array();

    /**
     * Function registry.
     *
     * @type    array
     */
    protected $registry = array();

    /**
     * Name of file that is rendered by the sandbox instance.
     *
     * @type    string
     */
    protected $filename = '';

    /**
     * File handle for error messages output.
     *
     * @type    resource
     */
    protected $errout = null;

    /**
     * Instance of locale class.
     *
     * @type    \Octris\Core\L10n
     */
    protected $l10n;

    /**
     * Instance of caching backend for template snippets.
     *
     * @type    \Octris\Core\Cache|null
     */
    protected $cache = null;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->storage = \Octris\Core\Tpl\Sandbox\Storage::getInstance();
        $this->errout = fopen('php://output', 'w');
    }

    /**
     * Set location for error output.
     *
     * @param   string|resource             $errout     Location for error output.
     */
    public function setErrorOutput($errout)
    {
        if (!is_resource($errout)) {
            throw new \Exception('Provided argument is not a resource "' . $errout . '"');
        }

        $this->errout = $errout;
    }

    /**
     * Set l10n dependency.
     *
     * @param   \Octris\Core\L10n       $l10n       Instance of l10n class.
     */
    public function setL10n(\Octris\Core\L10n $l10n)
    {
        $this->l10n = $l10n;
    }

    /**
     * Determine line number an error occured.
     * 
     * @return  int                     Determined line number.
     */
    public function getErrorLineNumber()
    {
        $trace = debug_backtrace();
        
        return $trace[2]['line'];
    }

    /**
     * Magic caller for registered template functions.
     *
     * @param   string      $name       Name of function to call.
     * @param   mixed       $args       Function arguments.
     * @return  mixed                   Return value of called function.
     */
    public function __call($name, $args)
    {
        if (!isset($this->registry[$name])) {
            $this->error(sprintf('"%s" -- unknown function', $name), $this->getErrorLineNumber(), __LINE__);
        } elseif (!is_callable($this->registry[$name]['callback'])) {
            $this->error(sprintf('"%s" -- unable to call function', $name), $this->getErrorLineNumber(), __LINE__);
        } elseif (count($args) < $this->registry[$name]['args']['min']) {
            $this->error(sprintf('"%s" -- not enough arguments', $name), $this->getErrorLineNumber(), __LINE__);
        } elseif (count($args) > $this->registry[$name]['args']['max']) {
            $this->error(sprintf('"%s" -- too many arguments', $name), $this->getErrorLineNumber(), __LINE__);
        } else {
            return call_user_func_array($this->registry[$name]['callback'], $args);
        }
    }

    /**
     * Trigger an error and stop processing template.
     *
     * @param   string      $msg        Additional error message.
     * @param   int         $line       Line in template the error occured (0, if it's in the class library).
     * @param   int         $cline      Line in the class that triggered the error.
     */
    public function error($msg, $line = 0, $cline = __LINE__, $filename = null)
    {
        if (($pre = (php_sapi_name() != 'cli' && stream_get_meta_data($this->errout)['uri'] == 'php://output'))) {
            fputs($this->errout, "<pre>");

            $prepare = function ($str) {
                return htmlentities($str, ENT_QUOTES);
            };
        } else {
            $prepare = function ($str) {
                return $str;
            };
        }

        fputs($this->errout, sprintf("\n** ERROR: sandbox(%d)**\n", $cline));
        fputs($this->errout, sprintf("   line :    %d\n", $line));
        fputs($this->errout, sprintf("   file:     %s\n", (is_null($filename) ? $this->filename : $filename)));
        fputs($this->errout, sprintf("   message:  %s\n", $msg));

        if ($pre) {
            fputs($this->errout, "</pre>");
        }
    }
<commit_msg>escape entities in error values when in web sapi<commit_after><?php

/*
 * This file is part of the 'octris/core' package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Octris\Core\Tpl;

/**
 * Sandbox to execute templates in.
 *
 * @copyright   copyright (c) 2010-2014 by <NAME>
 * @author      <NAME> <<EMAIL>>
 */
class Sandbox
{
    /**
     * Template data.
     *
     * @type    array
     */
    public $data = array();

    /**
     * Storage for sandbox internal data objects.
     *
     * @type    \Octris\Core\Tpl\Sandbox\Storage
     */
    protected $storage;

    /**
     * Internal storage for meta data required for block functions.
     *
     * @type    array
     */
    protected $meta = array();

    /**
     * Internal storage for cut/copied buffers.
     *
     * @type    array
     */
    protected $pastebin = array();

    /**
     * Function registry.
     *
     * @type    array
     */
    protected $registry = array();

    /**
     * Name of file that is rendered by the sandbox instance.
     *
     * @type    string
     */
    protected $filename = '';

    /**
     * File handle for error messages output.
     *
     * @type    resource
     */
    protected $errout = null;

    /**
     * Instance of locale class.
     *
     * @type    \Octris\Core\L10n
     */
    protected $l10n;

    /**
     * Instance of caching backend for template snippets.
     *
     * @type    \Octris\Core\Cache|null
     */
    protected $cache = null;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->storage = \Octris\Core\Tpl\Sandbox\Storage::getInstance();
        $this->errout = fopen('php://output', 'w');
    }

    /**
     * Set location for error output.
     *
     * @param   string|resource             $errout     Location for error output.
     */
    public function setErrorOutput($errout)
    {
        if (!is_resource($errout)) {
            throw new \Exception('Provided argument is not a resource "' . $errout . '"');
        }

        $this->errout = $errout;
    }

    /**
     * Set l10n dependency.
     *
     * @param   \Octris\Core\L10n       $l10n       Instance of l10n class.
     */
    public function setL10n(\Octris\Core\L10n $l10n)
    {
        $this->l10n = $l10n;
    }

    /**
     * Determine line number an error occured.
     * 
     * @return  int                     Determined line number.
     */
    public function getErrorLineNumber()
    {
        $trace = debug_backtrace();
        
        return $trace[2]['line'];
    }

    /**
     * Magic caller for registered template functions.
     *
     * @param   string      $name       Name of function to call.
     * @param   mixed       $args       Function arguments.
     * @return  mixed                   Return value of called function.
     */
    public function __call($name, $args)
    {
        if (!isset($this->registry[$name])) {
            $this->error(sprintf('"%s" -- unknown function', $name), $this->getErrorLineNumber(), __LINE__);
        } elseif (!is_callable($this->registry[$name]['callback'])) {
            $this->error(sprintf('"%s" -- unable to call function', $name), $this->getErrorLineNumber(), __LINE__);
        } elseif (count($args) < $this->registry[$name]['args']['min']) {
            $this->error(sprintf('"%s" -- not enough arguments', $name), $this->getErrorLineNumber(), __LINE__);
        } elseif (count($args) > $this->registry[$name]['args']['max']) {
            $this->error(sprintf('"%s" -- too many arguments', $name), $this->getErrorLineNumber(), __LINE__);
        } else {
            return call_user_func_array($this->registry[$name]['callback'], $args);
        }
    }

    /**
     * Trigger an error and stop processing template.
     *
     * @param   string      $msg        Additional error message.
     * @param   int         $line       Line in template the error occured (0, if it's in the class library).
     * @param   int         $cline      Line in the class that triggered the error.
     */
    public function error($msg, $line = 0, $cline = __LINE__, $filename = null)
    {
        if (($pre = (php_sapi_name() != 'cli' && stream_get_meta_data($this->errout)['uri'] == 'php://output'))) {
            fputs($this->errout, "<pre>");

            $prepare = function ($str) {
                return htmlentities($str, ENT_QUOTES);
            };
        } else {
            $prepare = function ($str) {
                return $str;
            };
        }

        fputs($this->errout, sprintf("\n** ERROR: sandbox(%d)**\n", $cline));
        fputs($this->errout, sprintf("   line :    %d\n", $line));
        fputs($this->errout, sprintf("   file:     %s\n", $prepare((is_null($filename) ? $this->filename : $filename))));
        fputs($this->errout, sprintf("   message:  %s\n", $prepare($msg)));

        if ($pre) {
            fputs($this->errout, "</pre>");
        }
    }

{'fraction_non_alphanumeric': 0.1502431279150541, 'fraction_numerical': 0.006549568323905924, 'mean_word_length': 1.8769626034827291, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '727738', 'n_tokens_mistral': 3075, 'n_tokens_neox': 3014, 'n_words': 768}