Passed
Push — master ( 81ea2b...335ed7 )
by Andreas
43:43
created

midcom_config_test::check_memcached()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.675

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 14
ccs 7
cts 10
cp 0.7
crap 5.675
rs 9.6111
1
<?php
2
/**
3
 * @package midcom
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Collection of simple helper methods for testing site configuration
11
 *
12
 * @package midcom
13
 */
14
class midcom_config_test
15
{
16
    const OK = 0;
17
    const WARNING = 1;
18
    const ERROR = 2;
19
20
    private $messages = [
21
        'midcom' => [],
22
        'php' => [],
23
        'external' => []
24
    ];
25
26
    private $section;
27
28
    private $status = self::OK;
29
30 1
    public function check()
31
    {
32 1
        $this->check_midcom();
33 1
        $this->check_php();
34 1
        $this->check_external();
35 1
    }
36
37 1
    public function get_status() : int
38
    {
39 1
        return $this->status;
40
    }
41
42 1
    private function add(string $testname, int $result_code, string $recommendations = '&nbsp;')
43
    {
44 1
        $this->messages[$this->section][$testname] = [
45 1
            'result' => $result_code,
46 1
            'message' => $recommendations
47
        ];
48 1
        $this->status = max($this->status, $result_code);
49 1
    }
50
51 1
    private function check_midcom()
52
    {
53 1
        $this->section = 'midcom';
54
55
        // Validate the Cache Base Directory.
56 1
        $cachedir = midcom::get()->getCacheDir();
57 1
        if (!is_dir($cachedir)) {
58
            $this->add('MidCOM cache base directory', self::ERROR, "The configured MidCOM cache base directory ({$cachedir}) does not exist or is not a directory. You have to create it as a directory writable by the Apache user.");
59 1
        } elseif (!is_writable($cachedir)) {
60
            $this->add('MidCOM cache base directory', self::ERROR, "The configured MidCOM cache base directory ({$cachedir}) is not writable by the Apache user. You have to create it as a directory writable by the Apache user.");
61
        } else {
62 1
            $this->add('MidCOM cache base directory', self::OK, $cachedir);
63
        }
64
65 1
        $lang = midcom::get()->i18n->get_current_language();
66 1
        $locale = Locale::getDefault();
67 1
        if (!str_starts_with($locale, $lang)) {
68
            $this->add('MidCOM language', self::WARNING, 'Language is set to "' . $lang . '", but the locale "' . $locale . '" is used. This might lead to problems in datamanager number inputs if decimal separators diverge');
69
        } else {
70 1
            $this->add('MidCOM language', self::OK, $locale);
71
        }
72
73 1
        $this->check_rcs();
74 1
    }
75
76 1
    private function check_rcs()
77
    {
78 1
        $config = new midcom_services_rcs_config(midcom::get()->config);
79 1
        if ($config->use_rcs()) {
80
            try {
81 1
                $dummy = new stdClass;
82 1
                $dummy->guid = 'ab';
83 1
                $backend = midcom::get()->rcs->load_backend($dummy);
84 1
                $this->add("MidCOM RCS", self::OK, get_class($backend));
85
            } catch (midcom_error $e) {
86 1
                $this->add("MidCOM RCS", self::ERROR, $e->getMessage());
87
            }
88
        } else {
89
            $this->add("MidCOM RCS", self::WARNING, "The MidCOM RCS service is disabled.");
90
        }
91 1
    }
92
93 1
    private function check_php()
94
    {
95 1
        $this->section = 'php';
96
97 1
        $cur_limit = $this->ini_get_filesize('memory_limit');
98 1
        if ($cur_limit >= (40 * 1024 * 1024)) {
99 1
            $this->add('Setting: memory_limit', self::OK, ini_get('memory_limit'));
100
        } else {
101
            $this->add('Setting: memory_limit', self::ERROR, "MidCOM requires a minimum memory limit of 40 MB to operate correctly. Smaller amounts will lead to PHP Errors. Detected limit was {$cur_limit}.");
102
        }
103
104 1
        $upload_limit = $this->ini_get_filesize('upload_max_filesize');
105 1
        if ($upload_limit >= (50 * 1024 * 1024)) {
106
            $this->add('Setting: upload_max_filesize', self::OK, ini_get('upload_max_filesize'));
107
        } else {
108 1
            $this->add('Setting: upload_max_filesize',
109 1
                             self::WARNING, "To make bulk uploads (for exampe in the Image Gallery) useful, you should increase the Upload limit to something above 50 MB. (Current setting: {$upload_limit})");
110
        }
111
112 1
        $post_limit = $this->ini_get_filesize('post_max_size');
113 1
        if ($post_limit >= $upload_limit) {
114 1
            $this->add('Setting: post_max_size', self::OK, ini_get('post_max_size'));
115
        } else {
116
            $this->add('Setting: post_max_size', self::WARNING, 'post_max_size should be larger than upload_max_filesize, as both limits apply during uploads.');
117
        }
118
119 1
        $timeout = midcom::get()->config->get('auth_login_session_timeout');
120 1
        if (ini_get('session.gc_maxlifetime') > $timeout) {
121
            $this->add('Setting: session.gc_maxlifetime', self::OK, ini_get('session.gc_maxlifetime'));
122
        } else {
123 1
            $this->add('Setting: session.gc_maxlifetime', self::WARNING, 'session.gc_maxlifetime should be greater than auth_login_session_timeout (' . $timeout . '), otherwise login sessions will time our earlier than intended.');
124
        }
125
126 1
        if (ini_get("opcache.enable") == "1") {
127 1
            $this->add("OPCache", self::OK);
128
        } else {
129
            $this->add("OPCache", self::WARNING, "OPCache is recommended for efficient MidCOM operation");
130
        }
131
132 1
        $this->check_memcached();
133
134 1
        if (!function_exists('exif_read_data')) {
135
            $this->add('EXIF reader', self::WARNING, 'PHP-EXIF is not available. It required for proper operation of Image Gallery components.');
136
        } else {
137 1
            $this->add('EXIF reader', self::OK);
138
        }
139 1
    }
140
141 1
    private function check_memcached()
142
    {
143 1
        if (midcom::get()->config->get('cache_module_memcache_backend') !== 'memcached') {
144
            $this->add('Memcache', self::WARNING, 'Configured backend: ' . midcom::get()->config->get('cache_module_memcache_backend'));
145 1
        } elseif (!class_exists('Memcached')) {
146
            $this->add('Memcache', self::WARNING, 'The PHP memcached module is recommended for efficient MidCOM operation.');
147
        } else {
148 1
            $config = midcom::get()->config->get('cache_module_memcache_backend_config');
149 1
            $memcached = midcom_services_cache_module_memcache::prepare_memcached($config);
0 ignored issues
show
Bug introduced by
It seems like $config can also be of type null; however, parameter $config of midcom_services_cache_mo...he::prepare_memcached() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            $memcached = midcom_services_cache_module_memcache::prepare_memcached(/** @scrutinizer ignore-type */ $config);
Loading history...
150
            // Sometimes, addServer returns true even if the server is not running, so we call a command to make sure it's actually working
151 1
            if ($memcached && $memcached->getVersion()) {
152 1
                $this->add('Memcache', self::OK);
153
            } else {
154
                $this->add('Memcache', self::ERROR, "The PHP memcached module is available and set to be in use, but it cannot be connected to.");
155
            }
156
        }
157 1
    }
158
159 1
    private function ini_get_filesize(string $setting) : int
160
    {
161 1
        $result = ini_get($setting);
162 1
        $last_char = substr($result, -1);
163 1
        if ($last_char == 'M') {
164 1
            $result = substr($result, 0, -1) * 1024 * 1024;
165
        } elseif ($last_char == 'K') {
166
            $result = substr($result, 0, -1) * 1024;
167
        } elseif ($last_char == 'G') {
168
            $result = substr($result, 0, -1) * 1024 * 1024 * 1024;
169
        }
170 1
        return $result;
171
    }
172
173 1
    private function check_external()
174
    {
175 1
        $this->section = 'external';
176
        // ImageMagick
177 1
        $cmd = midcom::get()->config->get('utility_imagemagick_base') . "identify -version";
178 1
        exec($cmd, $output, $result);
179 1
        if ($result !== 0 && $result !== 1) {
180
            $this->add('ImageMagick', self::ERROR, 'The existence ImageMagick toolkit could not be verified, it is required for all kinds of image processing in MidCOM.');
181
        } else {
182 1
            $this->add('ImageMagick', self::OK, implode("<br>", $output));
183
        }
184
185 1
        $this->check_for_utility('jpegtran', self::WARNING, 'The jpegtran utility is used for lossless JPEG operations, even though ImageMagick can do the same conversions, the lossless features provided by this utility are used where appropriate, so its installation is recommended unless it is known to cause problems.', 'The jpegtran utility is used for lossless rotations of JPEG images. If there are problems with image rotations, disabling jpegtran, which will cause ImageMagick to be used instead, probably helps.');
186
187 1
        if (midcom::get()->config->get('indexer_backend')) {
188
            $this->check_for_utility('catdoc', self::ERROR, 'Catdoc is required to properly index Microsoft Word documents. It is strongly recommended to install it, otherwise Word documents will be indexed as binary files.');
189
            $this->check_for_utility('pdftotext', self::ERROR, 'pdftotext is required to properly index Adobe PDF documents. It is strongly recommended to install it, otherwise PDF documents will be indexed as binary files.');
190
            $this->check_for_utility('unrtf', self::ERROR, 'unrtf is required to properly index Rich Text Format documents. It is strongly recommended to install it, otherwise RTF documents will be indexed as binary files.');
191
        }
192 1
    }
193
194 1
    private function check_for_utility(string $testname, int $fail_code, string $fail_recommendations, string $recommendations = '')
195
    {
196 1
        $executable = midcom::get()->config->get("utility_{$testname}");
197 1
        if ($executable === null) {
198
            $this->add($testname, $fail_code, "The path to the utility {$testname} is not configured. {$fail_recommendations}");
199 1
        } elseif (!exec('which which')) {
200
            $this->add('which', self::ERROR, "The 'which' utility cannot be found.");
201
        } else {
202 1
            exec("which {$executable}", $output, $exitcode);
203 1
            if ($exitcode == 0) {
204
                $this->add($testname, self::OK, $recommendations);
205
            } else {
206 1
                $this->add($testname, $fail_code, "The utility {$testname} is not correctly configured: File ({$executable}) not found. {$fail_recommendations}");
207
            }
208
        }
209 1
    }
210
211
    public function show()
212
    {
213
        echo '<table>';
214
215
        $this->print_section('MidCOM ' . midcom::VERSION, $this->messages['midcom']);
216
        $this->print_section($_SERVER['SERVER_SOFTWARE'], $this->messages['php']);
217
        $this->print_section('External Utilities', $this->messages['external']);
218
219
        echo '</table>';
220
    }
221
222
    private function print_section(string $heading, array $messages)
223
    {
224
        echo "  <tr>\n";
225
        echo "    <th colspan=\"2\">{$heading}</th>\n";
226
        echo "  </tr>\n";
227
228
        foreach ($messages as $testname => $data) {
229
            echo "  <tr class=\"test\">\n    <th>\n";
230
            switch ($data['result']) {
231
                case self::OK:
232
                    echo "    <i class='fa fa-check' style='color: green;' title='OK'></i>";
233
                    break;
234
235
                case self::WARNING:
236
                    echo "    <i class='fa fa-exclamation-triangle' style='color: orange;' title='WARNING'></i>";
237
                    break;
238
239
                case self::ERROR:
240
                    echo "    <i class='fa fa-exclamation-circle' style='color: red;' title='ERROR'></i>";
241
                    break;
242
243
                default:
244
                    throw new midcom_error("Unknown error code {$data['result']}.");
245
            }
246
247
            echo " {$testname}</th>\n";
248
            echo "    <td>{$data['message']}</td>\n";
249
            echo "  </tr>\n";
250
        }
251
    }
252
}
253