Skip to content

Commit 0c05dfa

Browse files
authored
Merge pull request #89 from bramley/read_config_file
Read the config.php file only once
2 parents ac5dfc4 + 83f7e9e commit 0c05dfa

File tree

1 file changed

+27
-44
lines changed

1 file changed

+27
-44
lines changed

index.php

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ class updater
2323
'ut.php',
2424
'api.php',
2525
);
26+
private $database_host;
27+
private $database_name;
28+
private $database_user;
29+
private $database_password;
30+
private $table_prefix;
31+
32+
public function __construct()
33+
{
34+
if (isset($_SERVER['ConfigFile']) && is_file($_SERVER['ConfigFile'])) {
35+
include $_SERVER['ConfigFile'];
36+
} elseif (file_exists($f = $this->getConfigFilePath())) {
37+
include $f;
38+
} else {
39+
throw new \UpdateException("Error: Cannot find config file");
40+
}
41+
42+
$this->database_host = $database_host;
43+
$this->database_name = $database_name;
44+
$this->database_user = $database_user;
45+
$this->database_password = $database_password;
46+
$this->table_prefix = isset($table_prefix) ? $table_prefix : 'phplist_';
47+
}
2648

2749
public function isAuthenticated()
2850
{
@@ -298,31 +320,15 @@ function getConfigFilePath()
298320
*/
299321
function getConnection()
300322
{
301-
if (isset($_SERVER['ConfigFile']) && is_file($_SERVER['ConfigFile'])) {
302-
include $_SERVER['ConfigFile'];
303-
304-
} elseif (file_exists($this->getConfigFilePath())) {
305-
include $this->getConfigFilePath();
306-
} else {
307-
throw new \UpdateException("Error: Cannot find config file");
308-
}
309-
310323
$charset = 'utf8mb4';
311-
312-
/** @var string $database_host
313-
* @var string $database_name
314-
* @var string $database_user
315-
* @var string $database_password
316-
*/
317-
318-
$dsn = "mysql:host=$database_host;dbname=$database_name;charset=$charset";
324+
$dsn = "mysql:host=$this->database_host;dbname=$this->database_name;charset=$charset";
319325
$options = array(
320326
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
321327
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
322328
PDO::ATTR_EMULATE_PREPARES => false,
323329
);
324330
try {
325-
$pdo = new PDO($dsn, $database_user, $database_password, $options);
331+
$pdo = new PDO($dsn, $this->database_user, $this->database_password, $options);
326332
} catch (\PDOException $e) {
327333
throw new \PDOException($e->getMessage(), (int)$e->getCode());
328334
}
@@ -336,18 +342,7 @@ function getConnection()
336342
*/
337343
function addMaintenanceMode()
338344
{
339-
if (isset($_SERVER['ConfigFile']) && is_file($_SERVER['ConfigFile'])) {
340-
include $_SERVER['ConfigFile'];
341-
} elseif (file_exists($this->getConfigFilePath())) {
342-
include $this->getConfigFilePath();
343-
} else {
344-
throw new \UpdateException("Error: Cannot find config file");
345-
}
346-
if (isset($table_prefix)) {
347-
$table_name = $table_prefix . 'config';
348-
} else {
349-
$table_name = 'phplist_config';
350-
}
345+
$table_name = $this->table_prefix . 'config';
351346
$prepStmt = $this->getConnection()->prepare("SELECT * FROM {$table_name} WHERE item=?");
352347
$prepStmt->execute(array('update_in_progress'));
353348
$result = $prepStmt->fetch(PDO::FETCH_ASSOC);
@@ -356,8 +351,7 @@ function addMaintenanceMode()
356351
$this->getConnection()
357352
->prepare("INSERT INTO {$table_name}(`item`,`editable`,`value`) VALUES (?,0,?)")
358353
->execute(array('update_in_progress', 1));
359-
}
360-
if ($result['update_in_progress'] == 0) {
354+
} elseif ($result['value'] == 0) {
361355
$this->getConnection()
362356
->prepare("UPDATE {$table_name} SET `value`=? WHERE `item`=?")
363357
->execute(array(1, 'update_in_progress'));
@@ -377,18 +371,7 @@ function addMaintenanceMode()
377371
*/
378372
function removeMaintenanceMode()
379373
{
380-
if (isset($_SERVER['ConfigFile']) && is_file($_SERVER['ConfigFile'])) {
381-
include $_SERVER['ConfigFile'];
382-
} elseif (file_exists($this->getConfigFilePath())) {
383-
include $this->getConfigFilePath();
384-
} else {
385-
throw new \UpdateException("Error: Cannot find config file");
386-
}
387-
if (isset($table_prefix)) {
388-
$table_name = $table_prefix . 'config';
389-
} else {
390-
$table_name = 'phplist_config';
391-
}
374+
$table_name = $this->table_prefix . 'config';
392375
$name = 'maintenancemode';
393376
$value = '';
394377
$sql = "UPDATE {$table_name} SET value =?, editable =? where item =? ";

0 commit comments

Comments
 (0)