Highway to PSR2
[openemr.git] / interface / modules / zend_modules / config / autoload / global.php
blobe7671fd026ec0fc17f792fcbde359edc744a7d7d
1 <?php
2 /**
3 * Global Configuration Override
5 * You can use this file for overriding configuration values from modules, etc.
6 * You would place values in here that are agnostic to the environment and not
7 * sensitive to security.
9 * @NOTE: In practice, this file will typically be INCLUDED in your source
10 * control, so do not include passwords or other sensitive information in this
11 * file.
13 * @Author: Oshri R <oshri.rozmarin@gmail.com>
17 // If to use utf-8 or not in my sql query
18 $tmp = $GLOBALS['disable_utf8_flag'] ? "SET sql_mode = ''" : "SET NAMES 'UTF8', sql_mode = ''";
19 $tmp .= ", time_zone = '" . (new DateTime())->format("P") . "'";
20 $utf8 = array(PDO::MYSQL_ATTR_INIT_COMMAND => $tmp);
22 // Sets default factory using the default database
23 $factories = array(
24 'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
25 $adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
26 $adapter = $adapterFactory->createService($serviceManager);
27 \Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
28 return $adapter;
32 // This settings can be change in the global settings under security tab
33 if ($GLOBALS['allow_multiple_databases']) {
34 // Open pdo connection
35 $dbh = new PDO('mysql:dbname=' . $GLOBALS['dbase'] . ';host=' . $GLOBALS['host'], $GLOBALS['login'], $GLOBALS['pass']);
36 $adapters = array();
37 $res = $dbh->prepare('SELECT * FROM multiple_db');
38 if ($res->execute()) {
39 foreach ($res->fetchAll() as $row) {
40 // Create new adapters using data from database
41 $adapters[$row['namespace']] = array(
42 'driver' => 'Pdo',
43 'dsn' => 'mysql:dbname=' . $row['dbname'] . ';host=' . $row['host'] . '',
44 'driver_options' => $utf8,
45 'port' => $row['port'],
46 'username' => $row['username'],
47 'password' => my_decrypt($row['password']),
50 // Create new factories using data from custom database
51 $factories[$row['namespace']] = function ($serviceManager) use ($row) {
52 $adapterAbstractServiceFactory = new Zend\Db\Adapter\AdapterAbstractServiceFactory();
53 $adapter = $adapterAbstractServiceFactory->createServiceWithName($serviceManager, '', $row['namespace']);
54 return $adapter;
59 $dbh = null; // Close pdo connection
63 return array(
64 'db' => array(
65 'driver' => 'Pdo',
66 'dsn' => 'mysql:dbname='.$GLOBALS['dbase'].';host='.$GLOBALS['host'],
67 'username' => $GLOBALS['login'],
68 'password' => $GLOBALS['pass'],
69 'port' => $GLOBALS['port'],
70 'driver_options' => $utf8,
71 'adapters' => $adapters
74 'service_manager' => array(
75 'factories' => $factories
81 /**
82 * Encrypts the string
83 * @param $value
84 * @return bool|string
86 function my_encrypt($data)
88 // Remove the base64 encoding from our key
89 $encryption_key = base64_decode($GLOBALS['safe_key_database']);
90 // Generate an initialization vector
91 $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
92 // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
93 $encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
94 // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
95 return base64_encode($encrypted . '::' . $iv);
98 /**
99 * Decrypts the string
100 * @param $value
101 * @return bool|string
104 function my_decrypt($data)
106 // Remove the base64 encoding from our key
107 $encryption_key = base64_decode($GLOBALS['safe_key_database']);
108 // To decrypt, split the encrypted data from our IV - our unique separator used was "::"
109 list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
110 return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);