<commit_before><?php
namespace phpList;

use phpList\helper\Database;
use phpList\helper\Language;
use phpList\helper\Validation;

class Config
{
    public $configFileOrigin;
    private $running_config = array();
    private $default_config = array();

    public function parseIniFile( $configFile )
    {
        // Load the config file
        $parsed = parse_ini_file( $configFile );
        if( ! is_array( $parsed ) ) {
            throw new \Exception( 'Could not parse specified ini config file: ' . $configFile );
        }
        return $parsed;
    }

    /**
     * Default constructor
     * load configuration from database each new session
     * TODO: probably not a good idea when using an installation with multiple subscribers
     */
     public function __construct( $configFile = NULL )
     {
         /**
         * Constants used for debugging and developping
         */
         defined('DEBUG') ? null : define('DEBUG', true);
         defined('PHPLIST_DEVELOPER_EMAIL') ? null : define('PHPLIST_DEVELOPER_EMAIL', '<EMAIL>');
         defined('PHPLIST_DEV_VERSION') ? null : define('PHPLIST_DEV_VERSION', true);
         defined('PHPLIST_VERSION') ? null : define('PHPLIST_VERSION', '4.0.0 dev');

         // Find the config file to use
         $foundConfigFile = $this->findConfigFile( $configFile );
         // Check config file is valid
         $this->validateConfigFile( $foundConfigFile );
         // Load the config file path as an ini file
         $this->running_config = $this->parseIniFile( $this->configFilePath );
         //Initialise further config
         $this->initConfig();
     }

     /**
      * Find the right config file to use
      */
     public function findConfigFile( $configFile )
     {
         // If no config file path provided
         if( $configFile !== NULL ) {
                 $this->configFileOrigin = "supplied file path";
                 $this->configFilePath = $configFile;
         } else { // If no config file specified, look for one
             // determine which config file to use
             if ( isset( $_SESSION['running_config'] ) ) { // do we have a configuration saved in session?
                 // If phpList is being used as a library, the config file may be set in session
                 $this->configFileOrigin = "session";
                 $this->configFilePath = $_SESSION['running_config'];

             } elseif( isset( $GLOBALS['phplist4-ini-config-file-path'] ) ) { // Is a phpList 4 config file stored in globals?
                 $this->configFileOrigin = "globals: phpList4 ini file path";
                 $this->configFilePath = $GLOBALS['phplist4-ini-config-file-path'];

             } elseif( isset( $GLOBALS['configfile'] ) ) { // Is a phpList 3 config file stored in globals?

                 $this->configFileOrigin = "globals: phpList3 ini file path";
                 $this->configFilePath = $GLOBALS['configfile'];

             } else {
                 throw new \Exception( 'Could not find config file, none specified' );
             }
         }
         return $this->configFilePath;
     }

     /**
      * Check that config file is valid
      * @param string $configFilePath Path to check
      */
     public function validateConfigFile( $configFilePath )
     {
         if (
             is_file( $configFilePath )
             && filesize( $configFilePath ) > 20
         ) {
             return true;
         } else {
             throw new \Exception( 'Could not load specified config file: ' . $configFile );
         }
     }

    /**
     * Run this after db has been initialized, so we get the config from inside the database as well.
     * @param Database $db
     */
    public function runAfterDBInitialised(Database $db){
        $this->loadDBConfig($db);
    }
<commit_msg>Added additional config file checks in validation method; Added more descriptive exception messages<commit_after><?php
namespace phpList;

use phpList\helper\Database;
use phpList\helper\Language;
use phpList\helper\Validation;

class Config
{
    public $configFileOrigin;
    private $running_config = array();
    private $default_config = array();

    public function parseIniFile( $configFile )
    {
        // Load the config file
        $parsed = parse_ini_file( $configFile );
        if( ! is_array( $parsed ) ) {
            throw new \Exception( 'Could not parse specified ini config file: ' . $configFile );
        }
        return $parsed;
    }

    /**
     * Default constructor
     * load configuration from database each new session
     * TODO: probably not a good idea when using an installation with multiple subscribers
     */
     public function __construct( $configFile = NULL )
     {
         /**
         * Constants used for debugging and developping
         */
         defined('DEBUG') ? null : define('DEBUG', true);
         defined('PHPLIST_DEVELOPER_EMAIL') ? null : define('PHPLIST_DEVELOPER_EMAIL', '<EMAIL>');
         defined('PHPLIST_DEV_VERSION') ? null : define('PHPLIST_DEV_VERSION', true);
         defined('PHPLIST_VERSION') ? null : define('PHPLIST_VERSION', '4.0.0 dev');

         // Find the config file to use
         $foundConfigFile = $this->findConfigFile( $configFile );
         // Check config file is valid
         $this->validateConfigFile( $foundConfigFile );
         // Load the config file path as an ini file
         $this->running_config = $this->parseIniFile( $this->configFilePath );
         //Initialise further config
         $this->initConfig();
     }

     /**
      * Find the right config file to use
      */
     public function findConfigFile( $configFile )
     {
         // If no config file path provided
         if( $configFile !== NULL ) {
                 $this->configFileOrigin = "supplied file path";
                 $this->configFilePath = $configFile;
         } else { // If no config file specified, look for one
             // determine which config file to use
             if (
                isset( $_SESSION['running_config'] )
                && count( $_SESSION['running_config'] ) > 15
            ) { // do we have a configuration saved in session?
                 // If phpList is being used as a library, the config file may be set in session
                 $this->configFileOrigin = "session";
                 $this->configFilePath = $_SESSION['running_config'];

             } elseif( isset( $GLOBALS['configfile'] ) ) { // Is a phpList 3 config file stored in globals?

                 $this->configFileOrigin = "globals: phpList3 ini file path";
                 $this->configFilePath = $GLOBALS['configfile'];

             } elseif( isset( $GLOBALS['phplist4-ini-config-file-path'] ) ) { // Is a phpList 4 config file stored in globals?
                 $this->configFileOrigin = "globals: phpList4 ini file path";
                 $this->configFilePath = $GLOBALS['phplist4-ini-config-file-path'];

             } else {
                 throw new \Exception( 'Could not find config file, none specified' );
             }
         }
         return $this->configFilePath;
     }

     /**
      * Check that config file is valid
      * @param string $configFilePath Path to check
      */
     public function validateConfigFile( $configFilePath )
     {
        if ( ! is_string( $configFilePath ) ) {
            throw new \Exception( 'Config file path is not a string (' . gettype( $configFilePath ) . ')' );
        } elseif ( ! is_file( $configFilePath ) ) {
            throw new \Exception( 'Config file is not a file: ' . $configFilePath );
        } elseif( ! filesize( $configFilePath ) > 20 ) {
            throw new \Exception( 'Config file too small: ' . $configFilePath );
        } elseif(  ! parse_ini_file( $configFilePath ) ) {
            throw new \Exception( 'Config file not an INI file: ' . $configFilePath );
        } else {
            return true;
        }
     }

    /**
     * Run this after db has been initialized, so we get the config from inside the database as well.
     * @param Database $db
     */
    public function runAfterDBInitialised(Database $db){
        $this->loadDBConfig($db);
    }

{'fraction_non_alphanumeric': 0.10594409862077384, 'fraction_numerical': 0.0029293299157817647, 'mean_word_length': 2.0932427331068326, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 40, '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': '7610192', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2126, 'n_words': 680}