<commit_before><?php defined('BLUDIT') or die('Bludit CMS.');

class Page {

	private $vars;

	function __construct($key)
	{
		global $pages;

		$this->vars['key'] = $key;

		// If key is FALSE, the page is create with default values, like an empty page
		// Useful for Page Not Found
		if ($key===false) {
			$row = $pages->getDefaultFields();
		} else {
			if (Text::isEmpty($key) || !$pages->exists($key)) {
				$errorMessage = 'Page not found in database by key ['.$key.']';
				Log::set(__METHOD__.LOG_SEP.$errorMessage);
				throw new Exception($errorMessage);
			}
			$row = $pages->getPageDB($key);
		}

		foreach ($row as $field=>$value) {
			if ($field=='date') {
				$this->setField('dateRaw', $value);
			} else {
				$this->setField($field, $value);
			}
		}
	}

	public function getValue($field)
	{
		if (isset($this->vars[$field])) {
			return $this->vars[$field];
		}
		return false;
	}

	public function setField($field, $value)
	{
		$this->vars[$field] = $value;
		return true;
	}

	// Returns the raw content
	// This content is not markdown parser
	// (boolean) $sanitize, TRUE returns the content sanitized
	public function contentRaw($sanitize=false)
	{
		$key = $this->key();
		$filePath = PATH_PAGES.$key.DS.FILENAME;
		$contentRaw = file_get_contents($filePath);

		if ($sanitize) {
			return Sanitize::html($contentRaw);
		}
		return $contentRaw;
	}

	// Returns the content
	// This content is markdown parser
	// (boolean) $fullContent, TRUE returns all content, if FALSE returns the first part of the content
	// (boolean) $noSanitize, TRUE returns the content without sanitized
	public function content($sanitize=false)
	{
		// If already set the content, return it
		$content = $this->getValue('content');
		if (!empty($content)) {
			return $content;
		}

		$contentRaw = $this->contentRaw();

		// Parse pre code with htmlentities
		$content = Text::pre2htmlentities($contentRaw);

		// Parse Markdown
		$parsedown = new Parsedown();
		$content = $parsedown->text($content);

		// Parse img src relative to absolute (with domain)
		if (IMAGE_RELATIVE_TO_ABSOLUTE) {
			$domain = IMAGE_RESTRICT?DOMAIN_UPLOADS_PAGES.$this->uuid().'/':DOMAIN_UPLOADS;
			$content = Text::imgRel2Abs($content, $domain);
		}

		if ($sanitize) {
			return Sanitize::html($content);<commit_msg>bug fix for #813<commit_after><?php defined('BLUDIT') or die('Bludit CMS.');

class Page {

	private $vars;

	function __construct($key)
	{
		global $pages;

		$this->vars['key'] = $key;

		// If key is FALSE, the page is create with default values, like an empty page
		// Useful for Page Not Found
		if ($key===false) {
			$row = $pages->getDefaultFields();
		} else {
			if (Text::isEmpty($key) || !$pages->exists($key)) {
				$errorMessage = 'Page not found in database by key ['.$key.']';
				Log::set(__METHOD__.LOG_SEP.$errorMessage);
				throw new Exception($errorMessage);
			}
			$row = $pages->getPageDB($key);
		}

		foreach ($row as $field=>$value) {
			if ($field=='date') {
				$this->setField('dateRaw', $value);
			} else {
				$this->setField($field, $value);
			}
		}
	}

	public function getValue($field)
	{
		if (isset($this->vars[$field])) {
			return $this->vars[$field];
		}
		return false;
	}

	public function setField($field, $value)
	{
		$this->vars[$field] = $value;
		return true;
	}

	// Returns the raw content
	// This content is not markdown parser
	// (boolean) $sanitize, TRUE returns the content sanitized
	public function contentRaw($sanitize=false)
	{
		$key = $this->key();
		$filePath = PATH_PAGES.$key.DS.FILENAME;
		$contentRaw = file_get_contents($filePath);

		if ($sanitize) {
			return Sanitize::html($contentRaw);
		}
		return $contentRaw;
	}

	// Returns the content
	// This content is markdown parser
	// (boolean) $fullContent, TRUE returns all content, if FALSE returns the first part of the content
	// (boolean) $noSanitize, TRUE returns the content without sanitized
	public function content($sanitize=false)
	{
		// If already set the content, return it
		$content = $this->getValue('content');
		if (!empty($content)) {
			return $content;
		}

		$content = $this->contentRaw();

		// Parse Markdown
		$parsedown = new Parsedown();
		$content = $parsedown->text($content);

		// Parse img src relative to absolute (with domain)
		if (IMAGE_RELATIVE_TO_ABSOLUTE) {
			$domain = IMAGE_RESTRICT?DOMAIN_UPLOADS_PAGES.$this->uuid().'/':DOMAIN_UPLOADS;
			$content = Text::imgRel2Abs($content, $domain);
		}

		if ($sanitize) {
			return Sanitize::html($content);
{'fraction_non_alphanumeric': 0.15866342111086523, 'fraction_numerical': 0.0013277273733126797, 'mean_word_length': 3.818763326226013, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '2327402', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1515, 'n_words': 395}