db-connect.php
06fbd764
 <?php
 /**
  *  Copyright (C) 2021  Double Bastion LLC
  *
  *  This file is part of Roundpin, which is licensed under the
  *  GNU Affero General Public License Version 3.0. The license terms
  *  are detailed in the "LICENSE.txt" file located in the root directory.
  */
 
 if (!defined('ACCESSCONST')) {
     die();
 }
 
 if (is_file(dirname(__FILE__) . '/roundpin-config.php')) {
 
  // Read the configuration file and extract the database credentials
  $configfilestring = file_get_contents(dirname(__FILE__) . '/roundpin-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__) . '/roundpin-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");
 
 }
 
 ?>