<commit_before><?php

/**
 * Landing CMS
 *
 * A simple CMS for Landing Pages.
 *
 * @package    Landing CMS
 * @author     <NAME> <<EMAIL>>
 * @copyright  2017, Landing CMS (https://github.com/Elias-Black/Landing-CMS)
 * @license    https://opensource.org/licenses/LGPL-2.1  LGPL-2.1
 * @version    Release: 0.0.5
 * @link       https://github.com/Elias-Black/Landing-CMS
 */

defined('CORE') OR die('403');

/**
* The Class for working with DB
*/

class DB
{

	const SECURE_TEXT = '<?php die; ?>';
	const SECURITY_LENGTH = 13;

	const PASSWORD_DB_PATH = 'cms/_db/password.php';
	const PRIVATE_DB_PATH = 'cms/_db/private.php';
	const PUBLIC_DB_PATH = 'cms/_db/public.php';



	/* PUBLIC API */

	public static function getPrivateContent($safe_replace = false)
	{

		$result = array();


		$private_db_path = Utils::getPath(self::PRIVATE_DB_PATH);

		try
		{

			if( is_readable($private_db_path) !== TRUE )
			{
				throw new Exception();
			}

			$content = file_get_contents($private_db_path);

			if($content === FALSE)
			{
				throw new Exception();
			}

		}
		catch(Exception $e)
		{

			$result['error'] = true;
			$result['error_message'] = 'Can not read from the Private database. Check permissions on <a href="'.Utils::getLink('install.php').'" target="_blank">this</a> helper.';

			return $result;

		}

		$content = unserialize( substr($content, self::SECURITY_LENGTH) );

		if(!$content)
		{
			$content = array();
		}

		$result['error'] = false;
		$result['db_content'] = $safe_replace ? Utils::replaceQuotesInArray($content) : $content;


		return $result;

	}

	public static function updateContent($content)
	{

		$result = array();


		$private_updated = self::updatePrivateContent($content);

		if($private_updated['error'] === true)
		{
			return $private_updated;
		}


		$public_updated = self::updatePublicContent($content);

		if($public_updated['error'] === true)
		{
			return $public_updated;
		}


		$result['error'] = false;
		$result['success_message'] = 'Saved successfully.';

		return $result;

	}

	public static function getPassword()
	{

		$result = array();


		$password_db_path = Utils::getPath(self::PASSWORD_DB_PATH);

		try
		{

			if( is_readable($password_db_path) !== TRUE )
			{
				throw new Exception();
			}

			$content = file_get_contents($password_db_path);

			if($content === false)
			{
				throw new Exception();
			}

		}
		catch(Exception $e)
		{

			$result['error'] = true;
			$result['error_message'] = 'Can not read from the Password database. Check permissions on <a href="'.Utils::getLink('install.php').'" target="_blank">this</a> helper.';

			return $result;

		}

		$result['error'] = false;
		$result['db_password'] = substr($content, self::SECURITY_LENGTH);


		return $result;

	}

	public static function updatePassword($password)
	{<commit_msg>Fix for install without DB files.<commit_after><?php

/**
 * Landing CMS
 *
 * A simple CMS for Landing Pages.
 *
 * @package    Landing CMS
 * @author     <NAME> <<EMAIL>>
 * @copyright  2017, Landing CMS (https://github.com/Elias-Black/Landing-CMS)
 * @license    https://opensource.org/licenses/LGPL-2.1  LGPL-2.1
 * @version    Release: 0.0.5
 * @link       https://github.com/Elias-Black/Landing-CMS
 */

defined('CORE') OR die('403');

/**
* The Class for working with DB
*/

class DB
{

	const SECURE_TEXT = '<?php die; ?>';
	const SECURITY_LENGTH = 13;

	const PASSWORD_DB_PATH = 'cms/_db/password.php';
	const PRIVATE_DB_PATH = 'cms/_db/private.php';
	const PUBLIC_DB_PATH = 'cms/_db/public.php';



	/* PUBLIC API */

	public static function getPrivateContent($safe_replace = false)
	{

		$result = array();


		$private_db_path = Utils::getPath(self::PRIVATE_DB_PATH);

		try
		{

			if( is_readable($private_db_path) !== TRUE )
			{
				throw new Exception();
			}

			$content = file_get_contents($private_db_path);

			if($content === FALSE)
			{
				throw new Exception();
			}

		}
		catch(Exception $e)
		{

			$result['error'] = true;
			$result['error_message'] = 'Can not read from the Private database. Check permissions on <a href="'.Utils::getLink('install.php').'" target="_blank">this</a> helper.';

			return $result;

		}

		$content = unserialize( substr($content, self::SECURITY_LENGTH) );

		if(!$content)
		{
			$content = array();
		}

		$result['error'] = false;
		$result['db_content'] = $safe_replace ? Utils::replaceQuotesInArray($content) : $content;


		return $result;

	}

	public static function updateContent($content)
	{

		$result = array();


		$private_updated = self::updatePrivateContent($content);

		if($private_updated['error'] === true)
		{
			return $private_updated;
		}


		$public_updated = self::updatePublicContent($content);

		if($public_updated['error'] === true)
		{
			return $public_updated;
		}


		$result['error'] = false;
		$result['success_message'] = 'Saved successfully.';

		return $result;

	}

	public static function getPassword()
	{

		$result = array();


		$password_db_path = Utils::getPath(self::PASSWORD_DB_PATH);

		try
		{

			if( file_exists($password_db_path) )
			{

				if( is_readable($password_db_path) !== TRUE )
				{
					throw new Exception();
				}

				$content = file_get_contents($password_db_path);

				if($content === false)
				{
					throw new Exception();
				}

			}
			else
			{
				$content = '';
			}

		}
		catch(Exception $e)
		{

			$result['error'] = true;
			$result['error_message'] = 'Can not read from the Password database. Check permissions on <a href="'.Utils::getLink('install.php').'" target="_blank">this</a> helper.';

			return $result;

		}

		$result['error'] = false;
		$result['db_password'] = substr($content, self::SECURITY_LENGTH);


		return $result;

	}

	public static function updatePassword($password)
	{
{'fraction_non_alphanumeric': 0.16197916666666667, 'fraction_numerical': 0.005555555555555556, 'mean_word_length': 3.6051159072741807, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 6, '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': '3068438', 'n_tokens_mistral': 2296, 'n_tokens_neox': 1937, 'n_words': 399}