piwik /plugins/Installation/SystemCheck.php

Language PHP Lines 341
MD5 Hash b65f6e3f3e74f9e7182a76aa816bc930 Estimated Cost $6,064 (why?)
Repository https://github.com/CodeYellowBV/piwik.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\Installation;

use Piwik\CliMulti;
use Piwik\CliMulti\Process;
use Piwik\Common;
use Piwik\Config;
use Piwik\Db;
use Piwik\Db\Adapter;
use Piwik\DbHelper;
use Piwik\Filechecks;
use Piwik\Filesystem;
use Piwik\Http;
use Piwik\Piwik;
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\SettingsServer;

class SystemCheck
{

    /**
     * Get system information
     */
    public static function getSystemInformation()
    {
        global $piwik_minimumPHPVersion;
        $minimumMemoryLimit = Config::getInstance()->General['minimum_memory_limit'];

        $infos = array();

        $directoriesToCheck = array(
            '/tmp/',
            '/tmp/assets/',
            '/tmp/cache/',
            '/tmp/climulti/',
            '/tmp/latest/',
            '/tmp/logs/',
            '/tmp/sessions/',
            '/tmp/tcpdf/',
            '/tmp/templates_c/',
        );

        if (!DbHelper::isInstalled()) {
            // at install, need /config to be writable (so we can create config.ini.php)
            $directoriesToCheck[] = '/config/';
        }

        $infos['directories'] = Filechecks::checkDirectoriesWritable($directoriesToCheck);

        $infos['can_auto_update'] = Filechecks::canAutoUpdate();

        self::initServerFilesForSecurity();

        $infos['phpVersion_minimum'] = $piwik_minimumPHPVersion;
        $infos['phpVersion'] = PHP_VERSION;
        $infos['phpVersion_ok'] = self::isPhpVersionValid($infos['phpVersion']);

        // critical errors
        $extensions = @get_loaded_extensions();
        $needed_extensions = array(
            'zlib',
            'SPL',
            'iconv',
            'json',
            'mbstring',
        );
        // HHVM provides the required subset of Reflection but lists Reflections as missing
        if (!defined('HHVM_VERSION')) {
            $needed_extensions[] = 'Reflection';
        }
        $infos['needed_extensions'] = $needed_extensions;
        $infos['missing_extensions'] = array();
        foreach ($needed_extensions as $needed_extension) {
            if (!in_array($needed_extension, $extensions)) {
                $infos['missing_extensions'][] = $needed_extension;
            }
        }

        // Special case for mbstring
        if (!function_exists('mb_get_info')
            || ((int)ini_get('mbstring.func_overload')) != 0) {
            $infos['missing_extensions'][] = 'mbstring';
        }

        $infos['pdo_ok'] = false;
        if (in_array('PDO', $extensions)) {
            $infos['pdo_ok'] = true;
        }

        $infos['adapters'] = Adapter::getAdapters();

        $needed_functions = array(
            'debug_backtrace',
            'create_function',
            'eval',
            'gzcompress',
            'gzuncompress',
            'pack',
        );
        $infos['needed_functions'] = $needed_functions;
        $infos['missing_functions'] = array();
        foreach ($needed_functions as $needed_function) {
            if (!self::functionExists($needed_function)) {
                $infos['missing_functions'][] = $needed_function;
            }
        }

        // warnings
        $desired_extensions = array(
            'json',
            'libxml',
            'dom',
            'SimpleXML',
        );
        $infos['desired_extensions'] = $desired_extensions;
        $infos['missing_desired_extensions'] = array();
        foreach ($desired_extensions as $desired_extension) {
            if (!in_array($desired_extension, $extensions)) {
                $infos['missing_desired_extensions'][] = $desired_extension;
            }
        }
        $desired_functions = array(
            'set_time_limit',
            'mail',
            'parse_ini_file',
            'glob',
        );
        $infos['missing_desired_functions'] = array();
        foreach ($desired_functions as $desired_function) {
            if (!self::functionExists($desired_function)) {
                $infos['missing_desired_functions'][] = $desired_function;
            }
        }

        $sessionAutoStarted = (int)ini_get('session.auto_start');
        if($sessionAutoStarted) {
            $infos['missing_desired_functions'][] = 'session.auto_start';
        }

        $desired_settings = array(
            'session.auto_start',
        );
        $infos['desired_functions'] = array_merge($desired_functions, $desired_settings);

        $infos['openurl'] = Http::getTransportMethod();

        $infos['gd_ok'] = SettingsServer::isGdExtensionEnabled();

        $serverSoftware = isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : '';
        $infos['serverVersion'] = addslashes($serverSoftware);
        $infos['serverOs'] = @php_uname();
        $infos['serverTime'] = date('H:i:s');

        $infos['memoryMinimum'] = $minimumMemoryLimit;

        $infos['memory_ok'] = true;
        $infos['memoryCurrent'] = '';

        $raised = SettingsServer::raiseMemoryLimitIfNecessary();
        if (($memoryValue = SettingsServer::getMemoryLimitValue()) > 0) {
            $infos['memoryCurrent'] = $memoryValue . 'M';
            $infos['memory_ok'] = $memoryValue >= $minimumMemoryLimit;
        }

        $infos['isWindows'] = SettingsServer::isWindows();

        $integrityInfo = Filechecks::getFileIntegrityInformation();
        $infos['integrity'] = $integrityInfo[0];

        $infos['integrityErrorMessages'] = array();
        if (isset($integrityInfo[1])) {
            if ($infos['integrity'] == false) {
                $infos['integrityErrorMessages'][] = Piwik::translate('General_FileIntegrityWarningExplanation');
            }
            $infos['integrityErrorMessages'] = array_merge($infos['integrityErrorMessages'], array_slice($integrityInfo, 1));
        }

        $infos['timezone'] = SettingsServer::isTimezoneSupportEnabled();

        $process = new CliMulti();
        $infos['cli_process_ok'] = $process->supportsAsync();

        $infos['tracker_status'] = Common::getRequestVar('trackerStatus', 0, 'int');

        // check if filesystem is NFS, if it is file based sessions won't work properly
        $infos['is_nfs'] = Filesystem::checkIfFileSystemIsNFS();
        $infos = self::enrichSystemChecks($infos);


        return $infos;
    }

    /**
     * @param $infos
     * @return mixed
     */
    public static function enrichSystemChecks($infos)
    {
        // determine whether there are any errors/warnings from the checks done above
        $infos['has_errors'] = false;
        $infos['has_warnings'] = false;
        if (in_array(0, $infos['directories']) // if a directory is not writable
            || !$infos['phpVersion_ok']
            || !empty($infos['missing_extensions'])
            || empty($infos['adapters'])
            || !empty($infos['missing_functions'])
        ) {
            $infos['has_errors'] = true;
        }

        if (   !empty($infos['missing_desired_extensions'])
            || !$infos['gd_ok']
            || !$infos['memory_ok']
            || !empty($infos['integrityErrorMessages'])
            || !$infos['timezone'] // if timezone support isn't available
            || $infos['tracker_status'] != 0
            || $infos['is_nfs']
        ) {
            $infos['has_warnings'] = true;
        }
        return $infos;
    }

    /**
     * Test if function exists.  Also handles case where function is disabled via Suhosin.
     *
     * @param string $functionName Function name
     * @return bool True if function exists (not disabled); False otherwise.
     */
    public static function functionExists($functionName)
    {
        // eval() is a language construct
        if ($functionName == 'eval') {
            // does not check suhosin.executor.eval.whitelist (or blacklist)
            if (extension_loaded('suhosin')) {
                return @ini_get("suhosin.executor.disable_eval") != "1";
            }
            return true;
        }

        $exists = function_exists($functionName);
        if (extension_loaded('suhosin')) {
            $blacklist = @ini_get("suhosin.executor.func.blacklist");
            if (!empty($blacklist)) {
                $blacklistFunctions = array_map('strtolower', array_map('trim', explode(',', $blacklist)));
                return $exists && !in_array($functionName, $blacklistFunctions);
            }
        }
        return $exists;
    }

    /**
     * Performs extra system checks for the 'System Check' admin page. These
     * checks are not performed during Installation.
     *
     * The following checks are performed:
     *  - Check for whether LOAD DATA INFILE can be used. The result of the check
     *    is stored in $result['load_data_infile_available']. The error message is
     *    stored in $result['load_data_infile_error'].
     *
     * - Check whether geo location is setup correctly
     *
     * @return array
     */
    public static function performAdminPageOnlySystemCheck()
    {
        $result = array();
        self::checkLoadDataInfile($result);
        self::checkGeolocation($result);
        return $result;
    }


    private static function checkGeolocation(&$result)
    {
        $currentProviderId = LocationProvider::getCurrentProviderId();
        $allProviders = LocationProvider::getAllProviderInfo();
        $isRecommendedProvider = in_array($currentProviderId, array( LocationProvider\GeoIp\Php::ID, $currentProviderId == LocationProvider\GeoIp\Pecl::ID));
        $isProviderInstalled = ($allProviders[$currentProviderId]['status'] == LocationProvider::INSTALLED);

        $result['geolocation_using_non_recommended'] = $result['geolocation_ok'] = false;
        if ($isRecommendedProvider && $isProviderInstalled) {
            $result['geolocation_ok'] = true;
        } elseif ($isProviderInstalled) {
            $result['geolocation_using_non_recommended'] = true;
        }
    }

    private static function checkLoadDataInfile(&$result)
    {
        // check if LOAD DATA INFILE works
        $optionTable = Common::prefixTable('option');
        $testOptionNames = array('test_system_check1', 'test_system_check2');

        $result['load_data_infile_available'] = false;
        try {
            $result['load_data_infile_available'] = \Piwik\Db\BatchInsert::tableInsertBatch(
                $optionTable,
                array('option_name', 'option_value'),
                array(
                    array($testOptionNames[0], '1'),
                    array($testOptionNames[1], '2'),
                ),
                $throwException = true
            );
        } catch (\Exception $ex) {
            $result['load_data_infile_error'] = str_replace("\n", "<br/>", $ex->getMessage());
        }

        // delete the temporary rows that were created
        Db::exec("DELETE FROM `$optionTable` WHERE option_name IN ('" . implode("','", $testOptionNames) . "')");
    }


    protected static function initServerFilesForSecurity()
    {
        ServerFilesGenerator::createWebConfigFiles();
        ServerFilesGenerator::createHtAccessFiles();

        ServerFilesGenerator::createWebRootFiles();
    }

    /**
     * @param $piwik_minimumPHPVersion
     * @param $infos
     * @return bool
     */
    public static function isPhpVersionValid($phpVersion)
    {
        global $piwik_minimumPHPVersion;
        return version_compare($piwik_minimumPHPVersion, $phpVersion) === -1;
    }

}
Back to Top