<commit_before><?php
// This is the 'header' file in all php files containing shared functions etc.
// Go to installer if no credentials found.

if ((!file_exists('conf/mysql_credentials.php')) && (!file_exists('/etc/kirjuri/conf/mysql_credentials.php'))) {
    header('Location: install.php');
    die;
}

// Load dependencies
require __DIR__.'/vendor/autoload.php';
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$loader = new Twig_Loader_Filesystem('views/');
$twig = new Twig_Environment($loader, array(
  'debug' => true, // if you remove the cache directive, remember to remove the trailing comma from this line.
  'cache' => 'cache' // This may be a source of errors if the WWW server process does not have ownership of the cache folder.
));
$twig->addExtension(new Twig_Extension_Debug());

$pur_config = HTMLPurifier_Config::createDefault();
$pur_config->set('Cache.SerializerPath', './cache');
$purifier = new HTMLPurifier($pur_config);


session_start();

foreach($_GET as $key => $value) // Sanitize all GET variables
{
  $strip_chars = array("<", ">", "'", ";");
  $value = str_replace($strip_chars, "", $value);
  $_GET[$key] = isset($value) ? $value : '';
}

// Declare variables
$_SESSION['message_set'] = isset($_SESSION['message_set']) ? $_SESSION['message_set'] : '';
$_SESSION['user'] = isset($_SESSION['user']) ? $_SESSION['user'] : ''; //

// If message has been set, do not clear it. Invidial files set message as shown before rendering page.
if ($_SESSION['message_set'] === false) {
    $_SESSION['message']['type'] = '';
    $_SESSION['message']['content'] = '';
}

// Check user access level before rendering page. User details are stored in a session variable.
function protect_page($required_access_level)
{
    if ((empty($_SESSION['user']) && $_SERVER['PHP_SELF'] !== '/api.php')) {
        // Check if user variable is set.

    header('Location: login.php');
        die;
    } else {
        if ($_SESSION['user']['access'] > $required_access_level) {
            message('error', $_SESSION['lang']['insufficient_privileges']);
            header('Location: '.$_SERVER['HTTP_REFERER']);
            die;
        } else {
            return true;
        }
    }
}

function sanitize_raw($string) // Purify HTML content for raw presentation.
{
  if(empty($string))
  {
    return "";
  }
  else
  {
    global $purifier;
    $out = $purifier->purify($string);
    if(strcmp($out, $string) !== 0)
    {
      logline('Info', 'Purified string: ' . $string . ' => ' . $out);
    }
    if(empty($out))
    {
      message('error', 'Invalid HTML input.');
      header('Location: '.$_SERVER['HTTP_REFERER']);
      die;
    }
    return $out;
  }

}

function num($a)  // Filter out everything but numbers.
{
    return preg_replace('/[^0-9]/', '', $a);
}

 function encrypt($in, $key) // Encrypt a string with AES-256-CBC
 {
     $iv = trim(substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 16));<commit_msg>Release 127, see changelog for details.<commit_after><?php
// This is the 'header' file in all php files containing shared functions etc.
// Go to installer if no credentials found.

if ((!file_exists('conf/mysql_credentials.php')) && (!file_exists('/etc/kirjuri/conf/mysql_credentials.php'))) {
    header('Location: install.php');
    die;
}

// Load dependencies
require __DIR__.'/vendor/autoload.php';
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$loader = new Twig_Loader_Filesystem('views/');
$twig = new Twig_Environment($loader, array(
  'debug' => true, // if you remove the cache directive, remember to remove the trailing comma from this line.
  'cache' => 'cache' // This may be a source of errors if the WWW server process does not have ownership of the cache folder.
));
$twig->addExtension(new Twig_Extension_Debug());

$pur_config = HTMLPurifier_Config::createDefault();
$pur_config->set('Cache.SerializerPath', './cache');
$purifier = new HTMLPurifier($pur_config);


session_start();

foreach($_GET as $key => $value) // Sanitize all GET variables
{
  $strip_chars = array("<", ">", "'", ";");
  $value = str_replace($strip_chars, "", $value);
  $_GET[$key] = isset($value) ? $value : '';
}

// Declare variables
$_SESSION['message_set'] = isset($_SESSION['message_set']) ? $_SESSION['message_set'] : '';
$_SESSION['user'] = isset($_SESSION['user']) ? $_SESSION['user'] : ''; //

// If message has been set, do not clear it. Invidial files set message as shown before rendering page.
if ($_SESSION['message_set'] === false) {
    $_SESSION['message']['type'] = '';
    $_SESSION['message']['content'] = '';
}

// Check user access level before rendering page. User details are stored in a session variable.
function protect_page($required_access_level)
{
    if ((empty($_SESSION['user']) && $_SERVER['PHP_SELF'] !== '/api.php')) {
        // Check if user variable is set.

    header('Location: login.php');
        die;
    } else {
        if ($_SESSION['user']['access'] > $required_access_level) {
            message('error', $_SESSION['lang']['insufficient_privileges']);
            header('Location: '.$_SERVER['HTTP_REFERER']);
            die;
        } else {
            return true;
        }
    }
}

function sanitize_raw($string) // Purify HTML content for raw presentation.
{
  if(empty($string))
  {
    return "";
  }
  else
  {
    global $purifier;
    $out = $purifier->purify($string);
/*

//  Disabled logging purify events due to too much noise in the logs.

    if(strcmp($out, $string) !== 0)
    {
      logline('Info', 'Purified string: ' . $string . ' => ' . $out);
    } 
*/
    if(empty($out))
    {
      message('error', 'Invalid HTML input.');
      header('Location: '.$_SERVER['HTTP_REFERER']);
      die;
    }
    return $out;
  }

}

function num($a)  // Filter out everything but numbers.
{
    return preg_replace('/[^0-9]/', '', $a);
}

 function encrypt($in, $key) // Encrypt a string with AES-256-CBC
 {
     $iv = trim(substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 16));
{'fraction_non_alphanumeric': 0.17300411522633743, 'fraction_numerical': 0.006748971193415638, 'mean_word_length': 3.6991492652745555, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '1739151', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1897, 'n_words': 484}