<commit_before>module Middleman
  class PhpMiddleware

    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, response = @app.call(env)

      if env['REQUEST_PATH'] =~ /\.php$/
        response.body.map! do |item|
          `echo #{Shellwords.escape(inject_params(env) + item)} | php`
        end
        headers['Content-Length'] = response.body.join.length.to_s
        headers['Content-Type'] = 'text/html'
      end

      [status, headers, response]
    end

    private

    def inject_params env
      injections = []
      unless env['QUERY_STRING'].empty?
        injections << { values: env['QUERY_STRING'], array: '$_GET' }
      end
      if env['REQUEST_METHOD'] == "POST"
        input = env["rack.input"].read
        unless input.length == 0
          injections << { values: input, array: '$_POST' }
        end
      end
      return '' unless injections.any?
      injections.collect! do |inj|
        "parse_str('#{inj[:values]}', #{inj[:array]});"
      end
      "<?php #{injections.join(' ')} ?>"
    end
<commit_msg>Add Cache-Control header only for PHP requests.<commit_after>module Middleman
  class PhpMiddleware

    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, response = @app.call(env)

      if env['REQUEST_PATH'] =~ /\.php$/
        response.body.map! do |item|
          `echo #{Shellwords.escape(inject_params(env) + item)} | php`
        end
        headers['Content-Length'] = response.body.join.length.to_s
        headers['Content-Type']   = 'text/html'
        headers['Cache-Control']  = 'no-cache, no-store, must-revalidate'
      end

      [status, headers, response]
    end

    private

    def inject_params env
      injections = []
      unless env['QUERY_STRING'].empty?
        injections << { values: env['QUERY_STRING'], array: '$_GET' }
      end
      if env['REQUEST_METHOD'] == "POST"
        input = env["rack.input"].read
        unless input.length == 0
          injections << { values: input, array: '$_POST' }
        end
      end
      return '' unless injections.any?
      injections.collect! do |inj|
        "parse_str('#{inj[:values]}', #{inj[:array]});"
      end
      "<?php #{injections.join(' ')} ?>"
    end

{'fraction_non_alphanumeric': 0.14796819787985865, 'fraction_numerical': 0.0008833922261484099, 'mean_word_length': 2.2873730043541363, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '4099193', 'n_tokens_mistral': 716, 'n_tokens_neox': 687, 'n_words': 160}