Debian Codegraph @

lib/checkEnvironment.inc

raw
1 <?php
2
3 namespace LAM\ENVIRONMENT_CHECK;
4
5 use Exception;
6 use LAM\PERSISTENCE\ConfigurationDatabase;
7 use LAMCfgMain;
8 use function LAM\LOGIN\displayLoginHeader;
9
10 /*
11
12 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
13 Copyright (C) 2010 - 2025 Roland Gruber
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
29 */
30
31 /**
32 * Checks if the environment fits to run LAM.
33 * This code checks if the PHP version is ok, the required extensions are active
34 * and the memory limit is high enough.
35 *
36 * @author Roland Gruber
37 * @package main
38 */
39
40 /** status messages */
41 include_once(__DIR__ . "/../lib/status.inc");
42
43 /** config */
44 include_once(__DIR__ . "/../lib/config.inc");
45
46 /** persistence */
47 include_once(__DIR__ . "/../lib/persistence.inc");
48
49 // check PHP version
50 if (version_compare(phpversion(), '8.2.0') < 0) {
51 echo "<!DOCTYPE html>\n\n";
52 echo "<html>\n<head>\n";
53 echo "<meta name=\"robots\" content=\"noindex, nofollow\">\n";
54 echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n";
55 echo "<meta http-equiv=\"pragma\" content=\"no-cache\">\n <meta http-equiv=\"cache-control\" content=\"no-cache\">\n";
56 printHeaderContents('LDAP Account Manager', '..');
57 echo "</head><body>\n";
58 StatusMessage("ERROR", "LAM needs a PHP version which is greater or equal than 8.2.0.", "Please upgrade your PHP installation. The found version is " . phpversion());
59 echo "<br><br>";
60 echo "</body></html>";
61 exit();
62 }
63
64 // check environment
65 $criticalErrors = [];
66 // check if PHP has session support
67 if (!function_exists('session_start')) {
68 $criticalErrors[] = ["ERROR", "Your PHP has no session support!", "Please install the session extension for PHP."];
69 }
70 // check if PHP has SPL support
71 if (!function_exists('class_implements')) {
72 $criticalErrors[] = ["ERROR", "Your PHP has no SPL support!", "Please install the SPL extension for PHP."];
73 }
74 // check if PHP has LDAP support
75 if (!function_exists('ldap_search')) {
76 $criticalErrors[] = ["ERROR", "Your PHP has no LDAP support!", "Please install the LDAP extension for PHP."];
77 }
78 // check if PHP has gettext support
79 if (!function_exists('gettext') || !function_exists('_')) {
80 $criticalErrors[] = ["ERROR", "Your PHP has no gettext support!", "Please install gettext for PHP."];
81 }
82 // check if PHP has openssl support
83 if (!function_exists('openssl_x509_parse')) {
84 $criticalErrors[] = ["ERROR", "Your PHP has no openssl support!", "Please install openssl for PHP."];
85 }
86 // check if PHP has XML support
87 if (!extension_loaded('xml')) {
88 $criticalErrors[] = ["ERROR", "Your PHP has no XML support!", "Please install the XML extension for PHP."];
89 }
90 if (!extension_loaded('libxml')) {
91 $criticalErrors[] = ["ERROR", "Your PHP has no Lib XML support!", "Please install the Lib XML extension for PHP."];
92 }
93 if (!class_exists('XMLReader')) {
94 $criticalErrors[] = ["ERROR", "Your PHP has no XMLReader support!", "Please install XMLReader for PHP."];
95 }
96 if (!class_exists('XMLWriter')) {
97 $criticalErrors[] = ["ERROR", "Your PHP has no XMLWriter support!", "Please install XMLWriter for PHP."];
98 }
99 if (!function_exists('mb_strlen')) {
100 $criticalErrors[] = ["ERROR", "Your PHP has no MB String support!", "Please install the MB String extension for PHP."];
101 }
102 // imagick/GD
103 if (!extension_loaded('imagick') && !extension_loaded('gd')) {
104 $criticalErrors[] = ["ERROR", "Your PHP has no imagick or GD support.", "Please install the imagick or GD extension for PHP."];
105 }
106 // check if PHP has JSON support
107 if (!function_exists('json_encode')) {
108 $criticalErrors[] = ["ERROR", "Your PHP has no JSON support!", "Please install the JSON extension for PHP."];
109 }
110 // check if PHP has GMP support
111 if (!extension_loaded('gmp')) {
112 $criticalErrors[] = ["ERROR", "Your PHP has no GMP support!", "Please install the GMP extension for PHP."];
113 }
114 // check if PHP has filter support
115 if (!extension_loaded('filter')) {
116 $criticalErrors[] = ["ERROR", "Your PHP has no filter support!", "Please install the filter extension for PHP."];
117 }
118 // check if PHP has iconv support
119 if (!extension_loaded('iconv')) {
120 $criticalErrors[] = ["ERROR", "Your PHP has no iconv support!", "Please install the iconv extension for PHP."];
121 }
122 // check if PHP has hash support
123 if (!extension_loaded('hash')) {
124 $criticalErrors[] = ["ERROR", "Your PHP has no hash support!", "Please install the hash extension for PHP."];
125 }
126 // check file permissions
127 $writableDirs = ['sess', 'tmp'];
128 for ($i = 0; $i < count($writableDirs); $i++) {
129 $path = realpath('../') . "/" . $writableDirs[$i];
130 if (!is_writable($path)) {
131 $criticalErrors[] = ["ERROR", 'The directory %s is not writable for the web server. Please change your file permissions.', '', [$path]];
132 }
133 }
134 // check session auto start
135 if (ini_get("session.auto_start") == "1") {
136 $criticalErrors[] = ["ERROR", "Please deactivate session.auto_start in your php.ini. LAM will not work if it is activated."];
137 }
138 // check memory limit
139 $memLimit = ini_get('memory_limit');
140 if (($memLimit != '')
141 && (substr(strtoupper($memLimit), strlen($memLimit) - 1) === 'M')
142 && (intval(substr($memLimit, 0, strlen($memLimit) - 1)) < 128)) {
143 $criticalErrors[] = ["ERROR", "Please increase the \"memory_limit\" parameter in your php.ini to at least \"128M\".",
144 "Your current memory limit is $memLimit."];
145 }
146 // check PCRE regex system
147 if (!@preg_match('/^\p{L}+$/u', "abc")) {
148 $criticalErrors[] = ["ERROR", "Your PCRE library has no complete Unicode support. Please upgrade libpcre or compile with \"--enable-unicode-properties\"."];
149 }
150 // check ZIP support for PDF files in file upload
151 if (!extension_loaded('zip')) {
152 $criticalErrors[] = ["ERROR", "Your PHP has no ZIP support.", "Please install the ZIP extension for PHP."];
153 }
154 // check for main config
155 try {
156 $mainConfig = new LAMCfgMain();
157 if (!$mainConfig->isConfigFileExisting() && !$mainConfig->installSampleConfig()) {
158 $criticalErrors[] = ["ERROR", "The main config file (config.cfg) does not exist.", "Please see the manual for installation instructions."];
159 }
160 $mainConfig = new LAMCfgMain();
161 $configurationDatabase = new ConfigurationDatabase($mainConfig);
162 $configurationDatabase->checkConnection();
163 }
164 catch (Exception $e) {
165 logNewMessage(LOG_ERR, 'Unable to connect to configuration database: ' . $e->getMessage());
166 $criticalErrors[] = ['ERROR', _('Unable to connect to configuration database.')];
167 }
168 // stop login if errors occurred
169 if (!empty($criticalErrors[0])) {
170 echo "<!DOCTYPE html>\n\n";
171 echo "<html>\n<head>\n";
172 echo "<meta name=\"robots\" content=\"noindex, nofollow\">\n";
173 echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n";
174 echo "<meta http-equiv=\"pragma\" content=\"no-cache\">\n";
175 echo "<meta http-equiv=\"cache-control\" content=\"no-cache\">\n";
176 printHeaderContents('LDAP Account Manager', '..');
177 echo "</head><body>\n";
178 foreach ($criticalErrors as $criticalError) {
179 call_user_func_array(StatusMessage(...), $criticalError);
180 echo "<br><br>";
181 }
182 echo "</body></html>";
183 exit();
184 }