Issues (2756)

includes/Config/Config.php (3 issues)

1
<?php
2
3
/**
4
 * Define the YOURLS config
5
 */
6
7
namespace YOURLS\Config;
8
9
use YOURLS\Exceptions\ConfigException;
10
11
class Config {
12
13
    /**
14
     * @param string
15
     */
16
    protected $root;
17
18
    /**
19
     * @param mixed
20
     */
21
    protected $config;
22
23
    /**
24
     * @since  1.7.3
25
     * @param  mixed $config   Optional user defined config path
26
     */
27 3
    public function __construct($config = false) {
28 3
        $this->set_root( $this->fix_win32_path( dirname( dirname( __DIR__ ) ) ) );
29 3
        $this->set_config($config);
30 3
    }
31
32
    /**
33
     * Convert antislashes to slashes
34
     *
35
     * @since  1.7.3
36
     * @param  string  $path
37
     * @return string  path with \ converted to /
38
     */
39 3
    public function fix_win32_path($path) {
40 3
        return str_replace('\\', '/', $path);
41
    }
42
43
    /**
44
     * @since  1.7.3
45
     * @param  string  path to config file
46
     * @return void
47
     */
48 3
    public function set_config($config) {
49 3
        $this->config = $config;
50 3
    }
51
52
    /**
53
     * @since  1.7.3
54
     * @param  string  path to YOURLS root directory
55
     * @return void
56
     */
57 3
    public function set_root($root) {
58 3
        $this->root = $root;
59 3
    }
60
61
    /**
62
     * Find config.php, either user defined or from standard location
63
     *
64
     * @since  1.7.3
65
     * @return string         path to found config file
66
     * @throws ConfigException
67
     */
68 3
    public function find_config() {
69
70 3
        $config = $this->fix_win32_path($this->config);
71
72 3
        if (!empty($config) && is_readable($config)) {
73 1
            return $config;
74
        }
75
76 2
        if (!empty($config) && !is_readable($config)) {
77 1
            throw new ConfigException("User defined config not found at '$config'");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $config instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
78
        }
79
80
        // config.php in /user/
81 1
        if (file_exists($this->root . '/user/config.php')) {
82
            return $this->root . '/user/config.php';
83
        }
84
85
        // config.php in /includes/
86 1
        if (file_exists($this->root . '/includes/config.php')) {
87
            return $this->root . '/includes/config.php';
88
        }
89
90
        // config.php not found :(
91
92 1
        throw new ConfigException('Cannot find config.php. Please read the readme.html to learn how to install YOURLS');
0 ignored issues
show
This line exceeds maximum limit of 100 characters; contains 120 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
93
    }
94
95
    /**
96
     * Define core constants that have not been user defined in config.php
97
     *
98
     * @since  1.7.3
99
     * @return void
100
     * @throws ConfigException
101
     */
102 1
    public function define_core_constants() {
103
        // Check minimal config job has been properly done
104 1
        $must_haves = array('YOURLS_DB_USER', 'YOURLS_DB_PASS', 'YOURLS_DB_NAME', 'YOURLS_DB_HOST', 'YOURLS_DB_PREFIX', 'YOURLS_SITE');
105 1
        foreach($must_haves as $must_have) {
106 1
            if (!defined($must_have)) {
107
                throw new ConfigException('Config is incomplete (missing at least '.$must_have.') Check config-sample.php and edit your config accordingly');
0 ignored issues
show
This line exceeds maximum limit of 100 characters; contains 157 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
108
            }
109
        }
110
111
        /**
112
         * The following has an awful CRAP index and it would be much shorter reduced to something like
113
         * defining an array of ('YOURLS_SOMETHING' => 'default value') and then a simple loop over the
114
         * array, checking if $current is defined as a constant and otherwise define said constant with
115
         * its default value. I did not wrote it that way because that would make it difficult for code
116
         * parsers to identify which constants are defined and where. So, here it is, that long list of
117
         * if (!defined) define(). Ho and by the way, such beautiful comment, much right aligned, wow !
118
         */
119
120
        // physical path of YOURLS root
121 1
        if (!defined( 'YOURLS_ABSPATH' ))
122
            define('YOURLS_ABSPATH', $this->root);
123
124
        // physical path of includes directory
125 1
        if (!defined( 'YOURLS_INC' ))
126
            define('YOURLS_INC', YOURLS_ABSPATH.'/includes');
127
128
        // physical path of user directory
129 1
        if (!defined( 'YOURLS_USERDIR' ))
130
            define( 'YOURLS_USERDIR', YOURLS_ABSPATH.'/user' );
131
132
        // URL of user directory
133 1
        if (!defined( 'YOURLS_USERURL' ))
134
            define( 'YOURLS_USERURL', trim(YOURLS_SITE, '/').'/user' );
135
136
        // physical path of asset directory
137 1
        if( !defined( 'YOURLS_ASSETDIR' ) )
138
            define( 'YOURLS_ASSETDIR', YOURLS_ABSPATH.'/assets' );
139
140
        // URL of asset directory
141 1
        if( !defined( 'YOURLS_ASSETURL' ) )
142
            define( 'YOURLS_ASSETURL', trim(YOURLS_SITE, '/').'/assets' );
143
144
        // physical path of translations directory
145 1
        if (!defined( 'YOURLS_LANG_DIR' ))
146
            define( 'YOURLS_LANG_DIR', YOURLS_USERDIR.'/languages' );
147
148
        // physical path of plugins directory
149 1
        if (!defined( 'YOURLS_PLUGINDIR' ))
150
            define( 'YOURLS_PLUGINDIR', YOURLS_USERDIR.'/plugins' );
151
152
        // URL of plugins directory
153 1
        if (!defined( 'YOURLS_PLUGINURL' ))
154
            define( 'YOURLS_PLUGINURL', YOURLS_USERURL.'/plugins' );
155
156
        // physical path of themes directory
157 1
        if( !defined( 'YOURLS_THEMEDIR' ) )
158
            define( 'YOURLS_THEMEDIR', YOURLS_USERDIR.'/themes' );
159
160
        // URL of themes directory
161 1
        if( !defined( 'YOURLS_THEMEURL' ) )
162
            define( 'YOURLS_THEMEURL', YOURLS_USERURL.'/themes' );
163
164
        // physical path of pages directory
165 1
        if (!defined( 'YOURLS_PAGEDIR' ))
166
            define('YOURLS_PAGEDIR', YOURLS_USERDIR.'/pages' );
167
168
        // table to store URLs
169 1
        if (!defined( 'YOURLS_DB_TABLE_URL' ))
170
            define( 'YOURLS_DB_TABLE_URL', YOURLS_DB_PREFIX.'url' );
171
172
        // table to store options
173 1
        if (!defined( 'YOURLS_DB_TABLE_OPTIONS' ))
174
            define( 'YOURLS_DB_TABLE_OPTIONS', YOURLS_DB_PREFIX.'options' );
175
176
        // table to store hits, for stats
177 1
        if (!defined( 'YOURLS_DB_TABLE_LOG' ))
178
            define( 'YOURLS_DB_TABLE_LOG', YOURLS_DB_PREFIX.'log' );
179
180
        // minimum delay in sec before a same IP can add another URL. Note: logged in users are not throttled down.
181 1
        if (!defined( 'YOURLS_FLOOD_DELAY_SECONDS' ))
182
            define( 'YOURLS_FLOOD_DELAY_SECONDS', 15 );
183
184
        // comma separated list of IPs that can bypass flood check.
185 1
        if (!defined( 'YOURLS_FLOOD_IP_WHITELIST' ))
186
            define( 'YOURLS_FLOOD_IP_WHITELIST', '' );
187
188
        // life span of an auth cookie in seconds (60*60*24*7 = 7 days)
189 1
        if (!defined( 'YOURLS_COOKIE_LIFE' ))
190
            define( 'YOURLS_COOKIE_LIFE', 60*60*24*7 );
191
192
        // life span of a nonce in seconds
193 1
        if (!defined( 'YOURLS_NONCE_LIFE' ))
194
            define( 'YOURLS_NONCE_LIFE', 43200 ); // 3600 * 12
195
196
        // if set to true, disable stat logging (no use for it, too busy servers, ...)
197 1
        if (!defined( 'YOURLS_NOSTATS' ))
198
            define( 'YOURLS_NOSTATS', false );
199
200
        // if set to true, force https:// in the admin area
201 1
        if (!defined( 'YOURLS_ADMIN_SSL' ))
202
            define( 'YOURLS_ADMIN_SSL', false );
203
204
        // if set to true, verbose debug infos. Will break things. Don't enable.
205 1
        if (!defined( 'YOURLS_DEBUG' ))
206
            define( 'YOURLS_DEBUG', false );
207
208
        // Error reporting
209 1
        if (defined( 'YOURLS_DEBUG' ) && YOURLS_DEBUG == true ) {
210 1
            error_reporting( -1 );
211
        } else {
212
            error_reporting( E_ERROR | E_PARSE );
213
        }
214 1
    }
215
216
}
217