<commit_before><?php

/*!
 * https://github.com/illogical-ru/lava-php
 *
 * Copyright illogical
 * Released under the MIT license
 */

namespace Lava;

use Lava\Stash;
use Lava\Env;
use Lava\Args;
use Lava\Cookie;
use Lava\Session;
use Lava\Safe;
use Lava\Route;
use Lava\Validator;
use Lava\SQLBuilder;


class App {

	public	$conf,
		$stash,
		$env, $args, $cookie, $session,
		$safe;

	private	$routes = array();

	static
	private	$types  = array(
		           'application/octet-stream',
		'txt'   => 'text/plain',
		'html'  => 'text/html',
		'js'    => 'text/javascript',
		'json'  => 'application/json',
		'jsonp' => 'application/javascript',
	);


	public function __construct ($conf = NULL) {

		$this->conf    = new Stash ($conf);
		$this->stash   = new Stash;
		$this->env     = new Env;
		$this->args    = new Args;
		$this->cookie  = new Cookie;
		$this->session = new Session;
		$this->safe    = new Safe  ($this->conf->safe());

		if (method_exists($this, 'start')) $this->start();
	}


	public function host ($scheme = NULL, $subdomain = NULL) {

		$host = $this->conf->host	? $this->conf->host
						: $this->env ->host;
		if ($scheme === TRUE)
			$scheme = $this->env->is_https ? 'https' : 'http';
		if ($subdomain)
			$host   = join('.', array_merge(
				(array)$subdomain, array($host)
			));
		if ($scheme)
			$host   = "${scheme}://${host}";

		return  $host;
	}

	public function home ($path = NULL) {

		$home = $this->conf->home	? $this->conf->home
						: getcwd();

		return join('/', array_merge(array($home), (array)$path));
	}

	public function pub  ($path = NULL) {

		$pub  = $this->conf->pub	? $this->conf->pub
						: preg_replace(
							'/\/[^\/]*$/', '',
							$this->env->script
						  );

		return join('/', array_merge(array($pub),  (array)$path));
	}

	public function uri  ($uri = NULL, $data = NULL, $append = FALSE) {

		if     (! isset($uri))
			$uri  = $this->env->uri;
		if     (  isset($this->routes[$uri]))
			$uri  = $this->routes[$uri]->uri($data);
		elseif (  isset($data) || $append) {
			$data = $this->args->_query($data, $append);
			if ($data) $uri	.= (strpos($uri, '?') ? '&' : '?')
					.   $data;
		}

		if     (! preg_match('/^(?:[a-zA-Z]+:\/)?\//', $uri))
			$uri  = $this->pub($uri);

		return  $uri;
	}
	public function uri_ref_or () {

		if   (	     preg_match(
				'/^([a-z]+:\/\/[^\/]+)(([^?]*).*)/',
				$this->env->referer,
				$match
			     )
			&& ! strcasecmp($match[1],   $this->host(TRUE))
			&&              $match[3] != $this->env->uri
		)
			return  $match[2];
		else
			return  call_user_func_array(
				array($this, 'uri'), func_get_args()
			);
	}
	public function url  () {

		$url  = call_user_func_array(
			array($this, 'uri'), func_get_args()
		);
		if (! preg_match('/^[a-zA-Z]+:\/\//', $url))
			$url = $this->host(TRUE) . $url;

		return  $url;
	}

	public function type () {

		if   (  $this->env->is_rewrite) {
			if (preg_match('/\.(\w+)$/', $this->env->uri, $match))
				$type = end($match);
		}
		else	$type = $this->args->type;

		if   (! isset($type))
			$type = $this->conf->type;

		return	strtolower($type);
	}

	public function render ($handler) {

		$type     = $this->type();<commit_msg>-uri_ref_or, +url_ref_or, up host and url<commit_after><?php

/*!
 * https://github.com/illogical-ru/lava-php
 *
 * Copyright illogical
 * Released under the MIT license
 */

namespace Lava;

use Lava\Stash;
use Lava\Env;
use Lava\Args;
use Lava\Cookie;
use Lava\Session;
use Lava\Safe;
use Lava\Route;
use Lava\Validator;
use Lava\SQLBuilder;


class App {

	public	$conf,
		$stash,
		$env, $args, $cookie, $session,
		$safe;

	private	$routes = array();

	static
	private	$types  = array(
		           'application/octet-stream',
		'txt'   => 'text/plain',
		'html'  => 'text/html',
		'js'    => 'text/javascript',
		'json'  => 'application/json',
		'jsonp' => 'application/javascript',
	);


	public function __construct ($conf = NULL) {

		$this->conf    = new Stash ($conf);
		$this->stash   = new Stash;
		$this->env     = new Env;
		$this->args    = new Args;
		$this->cookie  = new Cookie;
		$this->session = new Session;
		$this->safe    = new Safe  ($this->conf->safe());

		if (method_exists($this, 'start')) $this->start();
	}


	public function host ($scheme = NULL, $subdomain = NULL) {

		if   ($this->conf->host && $subdomain !== TRUE)
			$host   = $this->conf->host;
		else
			$host   = $this->env ->host;

		if   ($scheme === TRUE)
			$scheme = $this->env->is_https ? 'https' : 'http';
		if   ($subdomain        && $subdomain !== TRUE)
			$host   = join('.', array_merge(
				(array)$subdomain, array($host)
			));
		if   ($scheme)
			$host   = "${scheme}://${host}";

		return  $host;
	}

	public function home ($path = NULL) {

		$home = $this->conf->home	? $this->conf->home
						: getcwd();

		return join('/', array_merge(array($home), (array)$path));
	}

	public function pub  ($path = NULL) {

		$pub  = $this->conf->pub	? $this->conf->pub
						: preg_replace(
							'|/[^/]*$|', '',
							$this->env->script
						  );

		return join('/', array_merge(array($pub),  (array)$path));
	}

	public function uri  ($uri = NULL, $data = NULL, $append = FALSE) {

		if     (! isset($uri))
			$uri  = $this->env->uri;
		if     (  isset($this->routes[$uri]))
			$uri  = $this->routes[$uri]->uri($data);
		elseif (  isset($data) || $append) {
			$data = $this->args->_query($data, $append);
			if ($data) $uri	.= (strpos($uri, '?') ? '&' : '?')
					.   $data;
		}

		if     (! preg_match('|^(?:[a-z]+:/)?/|i', $uri))
			$uri  = $this->pub($uri);

		return  $uri;
	}

	public function url  () {

		$args      = func_get_args();

		$subdomain = key_exists(3, $args) ? $args[3] : TRUE;

		$url       = call_user_func_array(
			array($this, 'uri'), $args
		);
		if (! preg_match('|^[a-z]+://|i', $url))
			$url = $this->host(TRUE, $subdomain) . $url;

		return  $url;
	}
	public function url_ref_or () {

		$env = $this->env;

		if   (     preg_match(
				'|^[a-z]+://([^/]+)([^?]*)|i',
				$env->referer,
				$match
			   )
			&& (	   strcasecmp($env->host,  $match[1])
				||            $env->uri != $match[2]
			   )
		)
			return  $env->referer;
		else
			return  call_user_func_array(
				array($this, 'url'), func_get_args()
			);
	}

	public function type () {

		if   (  $this->env->is_rewrite) {
			if (preg_match('/\.(\w+)$/', $this->env->uri, $match))
				$type = end($match);
		}
		else	$type = $this->args->type;

		if   (! isset($type))
			$type = $this->conf->type;

		return	strtolower($type);
	}

	public function render ($handler) {

		$type     = $this->type();
{'fraction_non_alphanumeric': 0.22429764616552772, 'fraction_numerical': 0.0010630220197418376, 'mean_word_length': 2.7146080090242526, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 115, 'https://': 2, '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': '6839243', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2531, 'n_words': 511}