<?php
/**
 * @copyright 2024 Double Bastion LLC <www.doublebastion.com>
 *
 * @author Double Bastion LLC
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

if (!defined('ACCESSCONST')) {
    exit();
}

if (is_file(dirname(__FILE__) . '/panel-config.php')) {

 // Read the configuration file and extract the database credentials
 $configfilestring = file_get_contents(dirname(__FILE__) . '/panel-config.php');

 if (preg_match_all('[include|include_once|require|require_once]', $configfilestring) != 0) {

     if (strpos($configfilestring, "'") !== false) {
             $continit = explode("'", $configfilestring);
             $configfilepath = $continit[1];
     } elseif (strpos($configfilestring, "\"") !== false) {
             $continit = explode("\"", $configfilestring);
             $configfilepath = $continit[1];
       }

     $configfilelines = file($configfilepath);

     if (count($configfilelines) != 0) {

        foreach ($configfilelines as $keyfile => $valuefile) {

           if (strpos($valuefile, "\$databasename") !== false) {
                 $dbname_init = explode("'", $valuefile);
                 $dbname = $dbname_init[1];
           } elseif (strpos($valuefile, "\$username") !== false) {
                 $dbusername_init = explode("'", $valuefile);
                 $dbusername = $dbusername_init[1];
           } elseif (strpos($valuefile, "\$password") !== false) {
                 $dbuserpass_init = explode("'", $valuefile);
                 $dbuserpass = $dbuserpass_init[1];
             }
        }
     }

 } else {

     $configfilelines = file(dirname(__FILE__) . '/panel-config.php');

     if (count($configfilelines) != 0) {

        foreach ($configfilelines as $keyfile => $valuefile) {

           if (strpos($valuefile, "\$databasename") !== false) {
                 $dbname_init = explode("'", $valuefile);
                 $dbname = $dbname_init[1];
           } elseif (strpos($valuefile, "\$username") !== false) {
                 $dbusername_init = explode("'", $valuefile);
                 $dbusername = $dbusername_init[1];
           } elseif (strpos($valuefile, "\$password") !== false) {
                 $dbuserpass_init = explode("'", $valuefile);
                 $dbuserpass = $dbuserpass_init[1];
             }
        }
     }
   }

 mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

 // Connect to the database
 $mysqli = new mysqli('localhost', $dbusername, $dbuserpass, $dbname);

 if ($mysqli->connect_error) {
     exit('Error connecting to the database !');
 }

 $mysqli->set_charset("utf8mb4");

}

?>
