<commit_before><?php

/**
 * JSONDB - JSON Database Manager
 *
 * Manage JSON files as databases with JSON Query Language (JQL)
 *
 * This content is released under the MIT License (MIT)
 *
 * Copyright (c) 2016-2018 Aliens Group, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @category  Library
 * @package   JSONDB
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016-2018 Aliens Group, Inc.
 * @license   MIT <https://github.com/ElementaryFramework/JSONDB/blob/master/LICENSE>
 * @version   2.0.0
 * @link      http://php.jsondb.na2axl.tk
 */

namespace ElementaryFramework\JSONDB\Data;

use ElementaryFramework\JSONDB\Exceptions\AuthenticationException;
use ElementaryFramework\JSONDB\Exceptions\DatabaseException;
use ElementaryFramework\JSONDB\Utilities\Benchmark;
use ElementaryFramework\JSONDB\Utilities\Configuration;
use ElementaryFramework\JSONDB\Utilities\Util;
use ElementaryFramework\JSONDB\Query\Query;

/**
 * Class Database
 *
 * @package  JSONDB
 * @category Data
 * @author   <NAME> <<EMAIL>>
 * @link     http://php.jsondb.na2axl.tk/docs/api/jsondb/Data/Database
 */
class Database
{
    /**
     * The name of the server.
     *
     * @var string
     */
    private $_serverName;

    /**
     * The name of the database.
     *
     * @var string
     */
    private $_databaseName;

    /**
     * The current logged in username
     *
     * @var string
     */
    private $_username = '';

    /**
     * Database __constructor
     *
     * @param string $server
     * @param string $username
     * @param string $password
     * @param string|null $database
     *
     * @throws AuthenticationException
     * @throws DatabaseException
     */
    public function __construct(string $server, string $username, string $password, string $database = null)
    {
        $userFound = false;

        Benchmark::mark('Database_(connect)_start');
        {
            $config = Configuration::getConfig('users');

            if (!array_key_exists($server, $config)) {
                Benchmark::mark('Database_(connect)_end');
                throw new AuthenticationException("JSONDB Error: There is no registered server with the name \"{$server}\".");
            }

            foreach ($config[$server] as $user) {
                $userFound = $user["username"] === Util::crypt($username) && $user["password"] === Util::crypt($password);
                if ($userFound) break;
            }

            if (!$userFound) {
                Benchmark::mark('Database_(connect)_end');
                throw new AuthenticationException("JSONDB Error: User's authentication failed for user \"{$username}\" on server \"{$server}\". Access denied.");
            }

            $this->_serverName = realpath(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'servers' . DIRECTORY_SEPARATOR . $server);
            $this->_username = $username;

            if (null !== $database) {
                try {
                    $this->setDatabase($database);
                } catch (AuthenticationException|DatabaseException $e) {
                    Benchmark::mark('Database_(connect)_end');
                    throw $e;
                }
            }
        }<commit_msg>Fix Database connection issues<commit_after><?php

/**
 * JSONDB - JSON Database Manager
 *
 * Manage JSON files as databases with JSON Query Language (JQL)
 *
 * This content is released under the MIT License (MIT)
 *
 * Copyright (c) 2016-2018 Aliens Group, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @category  Library
 * @package   JSONDB
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016-2018 Aliens Group, Inc.
 * @license   MIT <https://github.com/ElementaryFramework/JSONDB/blob/master/LICENSE>
 * @version   2.0.0
 * @link      http://php.jsondb.na2axl.tk
 */

namespace ElementaryFramework\JSONDB\Data;

use ElementaryFramework\JSONDB\Exceptions\AuthenticationException;
use ElementaryFramework\JSONDB\Exceptions\DatabaseException;
use ElementaryFramework\JSONDB\JSONDB;
use ElementaryFramework\JSONDB\JSONDBConfig;
use ElementaryFramework\JSONDB\Query\Query;
use ElementaryFramework\JSONDB\Utilities\Benchmark;
use ElementaryFramework\JSONDB\Utilities\Configuration;
use ElementaryFramework\JSONDB\Utilities\Util;

/**
 * Class Database
 *
 * @package  JSONDB
 * @category Data
 * @author   <NAME> <<EMAIL>>
 * @link     http://php.jsondb.na2axl.tk/docs/api/jsondb/Data/Database
 */
class Database
{
    /**
     * The name of the server.
     *
     * @var string
     */
    private $_serverName;

    /**
     * The name of the database.
     *
     * @var string
     */
    private $_databaseName;

    /**
     * The current logged in username
     *
     * @var string
     */
    private $_username = '';

    /**
     * Database __constructor
     *
     * @param string $server
     * @param string $username
     * @param string $password
     * @param string|null $database
     *
     * @throws AuthenticationException
     * @throws DatabaseException
     */
    public function __construct(string $server, string $username, string $password, string $database = null)
    {
        $userFound = false;

        Benchmark::mark('Database_(connect)_start');
        {
            $config = Configuration::getConfig('users');

            if (!array_key_exists($server, $config)) {
                Benchmark::mark('Database_(connect)_end');
                throw new AuthenticationException("JSONDB Error: There is no registered server with the name \"{$server}\".");
            }

            foreach ($config as $user) {
                $userFound = $user["username"] === Util::crypt($username) && $user["password"] === Util::crypt($password);
                if ($userFound) break;
            }

            if (!$userFound) {
                Benchmark::mark('Database_(connect)_end');
                throw new AuthenticationException("JSONDB Error: User's authentication failed for user \"{$username}\" on server \"{$server}\". Access denied.");
            }

            $this->_serverName = Util::makePath(JSONDB::getConfigValue(JSONDBConfig::CONFIG_STORAGE_PATH), "servers", $server);
            $this->_username = $username;

            if (null !== $database) {
                try {
                    $this->setDatabase($database);
                } catch (AuthenticationException|DatabaseException $e) {
                    Benchmark::mark('Database_(connect)_end');
                    throw $e;
                }
            }
        }
{'fraction_non_alphanumeric': 0.10608005601587116, 'fraction_numerical': 0.004901388726805928, 'mean_word_length': 2.7554776511831727, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, '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': '3685276', 'n_tokens_mistral': 2310, 'n_tokens_neox': 2192, 'n_words': 735}