<commit_before><?php

/**
 * Response
 *
 * Handles the HTTP Response for the current execution.
 *
 * @package core
 * @author <EMAIL>
 * @copyright Caffeina srl - 2015 - http://caffeina.it
 */

class Response {
    use Module, Events;

    const TYPE_JSON               = 'application/json',
          TYPE_HTML               = 'text/html',
          TYPE_TEXT               = 'text/plain',
          TYPE_CSS                = 'text/css',
          TYPE_XML                = 'text/xml',
          TYPE_SVG                = 'image/svg+xml',
          TYPE_JS                 = 'application/javascript',
          TYPE_BIN                = 'application/octet-stream';

    protected static $payload     = [],
                     $status      = 200,
                     $charset     = "utf-8",
                     $headers     = ['Content-Type' => ['text/html; charset=utf-8']],
                     $buffer      = null,
                     $force_dl    = false,
                     $link        = null,
                     $sent        = false,
                     $links       = [];


    public static function charset($charset){
        static::$charset = $charset;
    }

    public static function type($mime){
        static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : ''));
    }

    /**
     * Force download of Response body
     * @param  string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content
     * @return [type]        [description]
     */
    public static function download($filename){
        static::$force_dl = $filename;
    }

    /**
     * Start capturing output
     */
    public static function start(){
        static::$buffer = ob_start();
    }

    /**
     * Enable CORS HTTP headers.
     */
    public static function enableCORS($origin='*'){

        // Allow from any origin
        if ($origin = $origin ?:( isset($_SERVER['HTTP_ORIGIN'])
                    ? $_SERVER['HTTP_ORIGIN']
                    : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '*')
        )) {
          static::header('Access-Control-Allow-Origin',      $origin);
          static::header('Access-Control-Allow-Credentials', 'true');
          static::header('Access-Control-Max-Age',           86400);
        }

        // Access-Control headers are received during OPTIONS requests
        if (filter_input(INPUT_SERVER,'REQUEST_METHOD') == 'OPTIONS') {
            static::clean();

            if (filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) {
              static::header('Access-Control-Allow-Methods',
                'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, PATCH, TRACE');
            }
            if ($req_h = filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) {
              static::header('Access-Control-Allow-Headers',$req_h);
            }

            self::trigger('cors.preflight');
            static::send();
            exit;
        }
    }

    public static function sent() {
        return static::$sent;
    }

    /**
     * Finish the output buffer capturing.
     * @return string The captured buffer
     */
    public static function end(){
        if (static::$buffer){
            static::$payload[] = ob_get_contents();
            ob_end_clean();
            static::$buffer = null;
            return end(static::$payload);
        }
    }

    /**
     * Check if an response output buffering is active.
     * @return boolean
     */
    public static function isBuffering(){
        return static::$buffer;
    }

    /**
     * Clear the response body
     */
    public static function clean(){
        static::$payload = [];
        static::$headers = [];
    }

    /**
     * Append a JSON object to the buffer.
     * @param  mixed $payload Data to append to the response buffer
     */
    public static function json($payload){
        static::type(static::TYPE_JSON);
        static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK));
    }

    /**
     * Append a text to the buffer.
     * @param  mixed $payload Text to append to the response buffer
     */
    public static function text(...$args){<commit_msg>[Chg] Added JSON_BIGINT_AS_STRING to standard json response<commit_after><?php

/**
 * Response
 *
 * Handles the HTTP Response for the current execution.
 *
 * @package core
 * @author <EMAIL>
 * @copyright Caffeina srl - 2015 - http://caffeina.it
 */

class Response {
    use Module, Events;

    const TYPE_JSON               = 'application/json',
          TYPE_HTML               = 'text/html',
          TYPE_TEXT               = 'text/plain',
          TYPE_CSS                = 'text/css',
          TYPE_XML                = 'text/xml',
          TYPE_SVG                = 'image/svg+xml',
          TYPE_JS                 = 'application/javascript',
          TYPE_BIN                = 'application/octet-stream';

    protected static $payload     = [],
                     $status      = 200,
                     $charset     = "utf-8",
                     $headers     = ['Content-Type' => ['text/html; charset=utf-8']],
                     $buffer      = null,
                     $force_dl    = false,
                     $link        = null,
                     $sent        = false,
                     $links       = [];


    public static function charset($charset){
        static::$charset = $charset;
    }

    public static function type($mime){
        static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : ''));
    }

    /**
     * Force download of Response body
     * @param  string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content
     * @return [type]        [description]
     */
    public static function download($filename){
        static::$force_dl = $filename;
    }

    /**
     * Start capturing output
     */
    public static function start(){
        static::$buffer = ob_start();
    }

    /**
     * Enable CORS HTTP headers.
     */
    public static function enableCORS($origin='*'){

        // Allow from any origin
        if ($origin = $origin ?:( isset($_SERVER['HTTP_ORIGIN'])
                    ? $_SERVER['HTTP_ORIGIN']
                    : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '*')
        )) {
          static::header('Access-Control-Allow-Origin',      $origin);
          static::header('Access-Control-Allow-Credentials', 'true');
          static::header('Access-Control-Max-Age',           86400);
        }

        // Access-Control headers are received during OPTIONS requests
        if (filter_input(INPUT_SERVER,'REQUEST_METHOD') == 'OPTIONS') {
            static::clean();

            if (filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) {
              static::header('Access-Control-Allow-Methods',
                'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, PATCH, TRACE');
            }
            if ($req_h = filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) {
              static::header('Access-Control-Allow-Headers',$req_h);
            }

            self::trigger('cors.preflight');
            static::send();
            exit;
        }
    }

    public static function sent() {
        return static::$sent;
    }

    /**
     * Finish the output buffer capturing.
     * @return string The captured buffer
     */
    public static function end(){
        if (static::$buffer){
            static::$payload[] = ob_get_contents();
            ob_end_clean();
            static::$buffer = null;
            return end(static::$payload);
        }
    }

    /**
     * Check if an response output buffering is active.
     * @return boolean
     */
    public static function isBuffering(){
        return static::$buffer;
    }

    /**
     * Clear the response body
     */
    public static function clean(){
        static::$payload = [];
        static::$headers = [];
    }

    /**
     * Append a JSON object to the buffer.
     * @param  mixed $payload Data to append to the response buffer
     */
    public static function json($payload){
        static::type(static::TYPE_JSON);
        static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK|JSON_BIGINT_AS_STRING));
    }

    /**
     * Append a text to the buffer.
     * @param  mixed $payload Text to append to the response buffer
     */
    public static function text(...$args){
{'fraction_non_alphanumeric': 0.1274941995359629, 'fraction_numerical': 0.0032482598607888632, 'mean_word_length': 1.7729173367642328, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3472395', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2256, 'n_words': 543}