7
7
8
8
class Config
9
9
{
10
+ public $ configFileOrigin ;
10
11
private $ running_config = array ();
11
12
private $ default_config = array ();
12
13
14
+ public function parseIniFile ( $ configFile )
15
+ {
16
+ // Load the config file
17
+ $ parsed = parse_ini_file ( $ configFile );
18
+ if ( ! is_array ( $ parsed ) ) {
19
+ throw new \Exception ( 'Could not parse specified ini config file: ' . $ configFile );
20
+ }
21
+ return $ parsed ;
22
+ }
23
+
13
24
/**
14
25
* Default constructor
15
26
* load configuration from database each new session
16
27
* TODO: probably not a good idea when using an installation with multiple subscribers
17
28
*/
18
- public function __construct ($ config_file )
19
- {
20
- /**
29
+ public function __construct ( $ configFile = NULL )
30
+ {
31
+ /**
21
32
* Constants used for debugging and developping
22
33
*/
23
- defined ('DEBUG ' ) ? null : define ('DEBUG ' , true );
24
- defined (
'PHPLIST_DEVELOPER_EMAIL ' ) ?
null :
define (
'PHPLIST_DEVELOPER_EMAIL ' ,
'[email protected] ' );
25
- defined ('PHPLIST_DEV_VERSION ' ) ? null : define ('PHPLIST_DEV_VERSION ' , true );
26
- defined ('PHPLIST_VERSION ' ) ? null : define ('PHPLIST_VERSION ' , '4.0.0 dev ' );
27
-
28
- //do we have a configuration saved in session?
29
- if (isset ($ _SESSION ['running_config ' ])) {
30
- $ this ->running_config = $ _SESSION ['running_config ' ];
34
+ defined ('DEBUG ' ) ? null : define ('DEBUG ' , true );
35
+ defined (
'PHPLIST_DEVELOPER_EMAIL ' ) ?
null :
define (
'PHPLIST_DEVELOPER_EMAIL ' ,
'[email protected] ' );
36
+ defined ('PHPLIST_DEV_VERSION ' ) ? null : define ('PHPLIST_DEV_VERSION ' , true );
37
+ defined ('PHPLIST_VERSION ' ) ? null : define ('PHPLIST_VERSION ' , '4.0.0 dev ' );
38
+
39
+ // Find the config file to use
40
+ $ foundConfigFile = $ this ->findConfigFile ( $ configFile );
41
+ // Check config file is valid
42
+ $ this ->validateConfigFile ( $ foundConfigFile );
43
+ // Load the config file path as an ini file
44
+ $ this ->running_config = $ this ->parseIniFile ( $ this ->configFilePath );
45
+ //Initialise further config
46
+ $ this ->initConfig ();
47
+ }
48
+
49
+ /**
50
+ * Find the right config file to use
51
+ */
52
+ public function findConfigFile ( $ configFile )
53
+ {
54
+ // If no config file path provided
55
+ if ( $ configFile !== NULL ) {
56
+ $ this ->configFileOrigin = "supplied file path " ;
57
+ $ this ->configFilePath = $ configFile ;
58
+ } else { // If no config file specified, look for one
59
+ // determine which config file to use
60
+ if (
61
+ isset ( $ _SESSION ['running_config ' ] )
62
+ && count ( $ _SESSION ['running_config ' ] ) > 15
63
+ ) { // do we have a configuration saved in session?
64
+ // If phpList is being used as a library, the config file may be set in session
65
+ $ this ->configFileOrigin = "session " ;
66
+ $ this ->configFilePath = $ _SESSION ['running_config ' ];
67
+
68
+ } elseif ( isset ( $ GLOBALS ['configfile ' ] ) ) { // Is a phpList 3 config file stored in globals?
69
+
70
+ $ this ->configFileOrigin = "globals: phpList3 ini file path " ;
71
+ $ this ->configFilePath = $ GLOBALS ['configfile ' ];
72
+
73
+ } elseif ( isset ( $ GLOBALS ['phplist4-ini-config-file-path ' ] ) ) { // Is a phpList 4 config file stored in globals?
74
+ $ this ->configFileOrigin = "globals: phpList4 ini file path " ;
75
+ $ this ->configFilePath = $ GLOBALS ['phplist4-ini-config-file-path ' ];
76
+
77
+ } else {
78
+ throw new \Exception ( 'Could not find config file, none specified ' );
79
+ }
80
+ }
81
+ return $ this ->configFilePath ;
82
+ }
83
+
84
+ /**
85
+ * Check that config file is valid
86
+ * @param string $configFilePath Path to check
87
+ */
88
+ public function validateConfigFile ( $ configFilePath )
89
+ {
90
+ if ( ! is_string ( $ configFilePath ) ) {
91
+ throw new \Exception ( 'Config file path is not a string ( ' . gettype ( $ configFilePath ) . ') ' );
92
+ } elseif ( ! is_file ( $ configFilePath ) ) {
93
+ throw new \Exception ( 'Config file is not a file: ' . $ configFilePath );
94
+ } elseif ( ! filesize ( $ configFilePath ) > 20 ) {
95
+ throw new \Exception ( 'Config file too small: ' . $ configFilePath );
96
+ } elseif ( ! parse_ini_file ( $ configFilePath ) ) {
97
+ throw new \Exception ( 'Config file not an INI file: ' . $ configFilePath );
31
98
} else {
32
- if (is_file ($ config_file ) && filesize ($ config_file ) > 20 ) {
33
- //load from config file
34
- $ this ->running_config = parse_ini_file ($ config_file );
35
- //Initialise further config
36
- $ this ->initConfig ();
37
- } else {
38
- throw new \Exception ('Cannot find config file: ' . $ config_file );
39
- }
99
+ return true ;
40
100
}
41
- }
101
+ }
42
102
43
103
/**
44
104
* Run this after db has been initialized, so we get the config from inside the database as well.
@@ -53,7 +113,7 @@ public function runAfterDBInitialised(Database $db){
53
113
* @param Language $lan
54
114
*/
55
115
public function runAfterLanguageInitialised (Language $ lan ){
56
- $ this ->loadDefaultConfig ($ lan );
116
+ // $this->loadDefaultConfig($lan);
57
117
}
58
118
59
119
/**
@@ -322,12 +382,16 @@ private function initConfig()
322
382
$ this ->running_config ['TLD_REFETCH_TIMEOUT ' ] = 15552000 ; ## 180 days, about 6 months
323
383
$ this ->running_config ['SHOW_UNSUBSCRIBELINK ' ] = true ;
324
384
325
- if (function_exists ('hash_algos ' ) && !in_array ($ this ->running_config ['ENCRYPTION_ALGO ' ], hash_algos ())) {
326
- throw new \Exception ('Encryption algorithm " ' . $ this ->running_config ['ENCRYPTION_ALGO ' ] . '" not supported, change your configuration ' );
385
+ // Check if desired hashing algo is supported by server
386
+ if (
387
+ function_exists ( 'hash_algos ' )
388
+ && !in_array ( $ this ->running_config ['ENCRYPTION_ALGO ' ], hash_algos () )
389
+ ) {
390
+ throw new \Exception ( 'Encryption algorithm " ' . $ this ->running_config ['ENCRYPTION_ALGO ' ] . '" not supported, change your configuration ' );
327
391
}
328
- ## remember the length of a hashed string
329
- $ this ->running_config ['hash_length ' ] = strlen (hash ($ this ->running_config ['ENCRYPTION_ALGO ' ],'some text ' ));
330
392
393
+ // check and store the length of a hash using the desired algo
394
+ $ this ->running_config ['hash_length ' ] = strlen ( hash ( $ this ->running_config ['ENCRYPTION_ALGO ' ], 'some text ' ) );
331
395
332
396
$ this ->running_config ['NUMATTACHMENTS ' ] = 1 ;
333
397
$ this ->running_config ['FCKIMAGES_DIR ' ] = 'uploadimages ' ;
@@ -1079,5 +1143,4 @@ private function loadDefaultConfig(Language $lan){
1079
1143
1080
1144
);
1081
1145
}
1082
-
1083
1146
}
0 commit comments