Completed
Push — master ( e8fccc...474902 )
by Andreas
23:28
created

midcom_config_test::check_midcom()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.1574

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 6
nop 0
dl 0
loc 23
ccs 11
cts 14
cp 0.7856
crap 4.1574
rs 9.7666
c 0
b 0
f 0
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 ($lang != substr($locale, 0, 2)) {
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
                midcom::get()->rcs->load_backend($dummy);
84 1
                $this->add("MidCOM RCS", self::OK);
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
        if (ini_get("opcache.enable") == "1") {
120 1
            $this->add("OPCache", self::OK);
121
        } else {
122
            $this->add("OPCache", self::WARNING, "OPCache is recommended for efficient MidCOM operation");
123
        }
124
125 1
        $this->check_memcached();
126
127 1
        if (!function_exists('exif_read_data')) {
128
            $this->add('EXIF reader', self::WARNING, 'PHP-EXIF is not available. It required for proper operation of Image Gallery components.');
129
        } else {
130 1
            $this->add('EXIF reader', self::OK);
131
        }
132 1
    }
133
134 1
    private function check_memcached()
135
    {
136 1
        if (midcom::get()->config->get('cache_module_memcache_backend') !== 'memcached') {
137
            $this->add('Memcache', self::WARNING, 'Configured backend: ' . midcom::get()->config->get('cache_module_memcache_backend'));
138 1
        } elseif (!class_exists('Memcached')) {
139
            $this->add('Memcache', self::WARNING, 'The PHP memcached module is recommended for efficient MidCOM operation.');
140
        } else {
141 1
            $config = midcom::get()->config->get('cache_module_memcache_backend_config');
142 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

142
            $memcached = midcom_services_cache_module_memcache::prepare_memcached(/** @scrutinizer ignore-type */ $config);
Loading history...
143
            // Sometimes, addServer returns true even if the server is not running, so we call a command to make sure it's actually working
144 1
            if ($memcached && $memcached->getVersion()) {
145 1
                $this->add('Memcache', self::OK);
146
            } else {
147
                $this->add('Memcache', self::ERROR, "The PHP memcached module is available and set to be in use, but it cannot be connected to.");
148
            }
149
        }
150 1
    }
151
152 1
    private function ini_get_filesize(string $setting) : int
153
    {
154 1
        $result = ini_get($setting);
155 1
        $last_char = substr($result, -1);
156 1
        if ($last_char == 'M') {
157 1
            $result = substr($result, 0, -1) * 1024 * 1024;
158
        } elseif ($last_char == 'K') {
159
            $result = substr($result, 0, -1) * 1024;
160
        } elseif ($last_char == 'G') {
161
            $result = substr($result, 0, -1) * 1024 * 1024 * 1024;
162
        }
163 1
        return $result;
164
    }
165
166 1
    private function check_external()
167
    {
168 1
        $this->section = 'external';
169
        // ImageMagick
170 1
        $cmd = midcom::get()->config->get('utility_imagemagick_base') . "identify -version";
171 1
        exec($cmd, $output, $result);
172 1
        if ($result !== 0 && $result !== 1) {
173
            $this->add('ImageMagick', self::ERROR, 'The existence ImageMagick toolkit could not be verified, it is required for all kinds of image processing in MidCOM.');
174
        } else {
175 1
            $this->add('ImageMagick', self::OK, implode("<br>", $output));
176
        }
177
178 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.');
179
180 1
        if (midcom::get()->config->get('indexer_backend')) {
181
            $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.');
182
            $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.');
183
            $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.');
184
        }
185 1
    }
186
187 1
    private function check_for_utility(string $testname, int $fail_code, string $fail_recommendations, string $recommendations = '&nbsp;')
188
    {
189 1
        $executable = midcom::get()->config->get("utility_{$testname}");
190 1
        if ($executable === null) {
191
            $this->add($testname, $fail_code, "The path to the utility {$testname} is not configured. {$fail_recommendations}");
192 1
        } elseif (!exec('which which')) {
193
            $this->add('which', self::ERROR, "The 'which' utility cannot be found.");
194
        } else {
195 1
            exec("which {$executable}", $output, $exitcode);
196 1
            if ($exitcode == 0) {
197
                $this->add($testname, self::OK, $recommendations);
198
            } else {
199 1
                $this->add($testname, $fail_code, "The utility {$testname} is not correctly configured: File ({$executable}) not found. {$fail_recommendations}");
200
            }
201
        }
202 1
    }
203
204
    public function show()
205
    {
206
        echo '<table>';
207
208
        $this->print_section('MidCOM ' . midcom::VERSION, $this->messages['midcom']);
209
        $this->print_section($_SERVER['SERVER_SOFTWARE'], $this->messages['php']);
210
        $this->print_section('External Utilities', $this->messages['external']);
211
212
        echo '</table>';
213
    }
214
215
    private function print_section(string $heading, array $messages)
216
    {
217
        echo "  <tr>\n";
218
        echo "    <th colspan=\"2\">{$heading}</th>\n";
219
        echo "  </tr>\n";
220
221
        foreach ($messages as $testname => $data) {
222
            echo "  <tr class=\"test\">\n    <th>\n";
223
            switch ($data['result']) {
224
                case self::OK:
225
                    echo "    <i class='fa fa-check' style='color: green;' title='OK'></i>";
226
                    break;
227
228
                case self::WARNING:
229
                    echo "    <i class='fa fa-exclamation-triangle' style='color: orange;' title='WARNING'></i>";
230
                    break;
231
232
                case self::ERROR:
233
                    echo "    <i class='fa fa-exclamation-circle' style='color: red;' title='ERROR'></i>";
234
                    break;
235
236
                default:
237
                    throw new midcom_error("Unknown error code {$data['result']}.");
238
            }
239
240
            echo " {$testname}</th>\n";
241
            echo "    <td>{$data['message']}</td>\n";
242
            echo "  </tr>\n";
243
        }
244
    }
245
}
246