-1

I have my database information file called main-custom.php file which have information in array like this

<?php defined('MW_PATH') || exit('No direct script access allowed');

return array(

    // application components
    'components' => array(
        'db' => array(
            'connectionString'  => 'mysql:host=localhost;port=3306;dbname=databasebane',
            'username'          => 'username',
            'password'          => 'password',
            'tablePrefix'       => 'mw_',
        ),
    ),
    // params
    'params' => array(
        'email.custom.header.prefix' => 'X-Ltxf-'
    ),
);

Its in Mailwizz.I am looking for get and use this database information in my another file so I have included it in my index.php file like below

<?php
define("MW_PATH","OK");
require_once("../apps/common/config/main-custom.php");

Now I am not getting idea how I can use this database information for create variable like

$connectionString =?
$username =?
.....

Since main-custom.php file returning array, its making me confused and not able to get information from it to my index.php file.

since mailwizz is using main-custom.php as it so I can not modify it. Is there any way to extract required information from it to index.php file without modify it?

Thanks!

Manisha
  • 195
  • 1
  • 10

3 Answers3

0

There are two considerations here.

Firstly, the constant MW_PATH acts to prevent you from executing the rest of the content, so we need to define that.

Secondly, the actual code is just a return statement. That only makes sense inside a function or class method, so we need to mock up that environment.

This gives us:

// define the constant if its not already defined.
if (!defined('MW_PATH')) define('MW_PATH', 'OK');

// create the function and include the file in it.
function getDBdata() {
require("main-custom.php");
}

// call the function to get the data
$dbData = getDBdata();

$connectionString = $dbData['components']['db']['connectionString']  // mysql:host=localhost...

Note that defining MW_PATH to an arbitrary value may cause problems later in the code if it is redefined, or has an incorrect value.
-1

I have achieved it using file_get_contents like below

$content = str_replace(' ', '', file_get_contents("../apps/common/config/main-custom.php"));

$db_host ="localhost";
$db_port ="3306";
$db_name = get_string_between($content, 'dbname=', "',");
$username = get_string_between($content, "'username'=>'", "',");
$password = get_string_between($content, "'password'=>'", "',");
$tablePrefix = get_string_between($content, "'tablePrefix'=>'", "',");

function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

Its working fine as expected. Thanks!

Manisha
  • 195
  • 1
  • 10
-1

assign $app = require_once("../apps/common/config/main-custom.php")

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
bxg
  • 115
  • 3