Dodatne provjere PHP5 postavki uz prikladnu informaciju.
[php5-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
6
7 case "$1" in
8     configure)
9     # continue below
10     ;;
11
12     abort-upgrade|abort-remove|abort-deconfigure)
13     exit 0
14     ;;
15
16     *)
17     echo "postinst called with unknown argument \`$1'" >&2
18     exit 0
19     ;;
20 esac
21
22
23 # Load debconf
24 . /usr/share/debconf/confmodule
25
26 # Include CARNet functions
27 . /usr/share/carnet-tools/functions.sh
28
29 PKG="php5-cn"
30 A2MODEDIR="/etc/apache2/mods-enabled"
31 EXTENSIONS="mysql"
32 PHP5DIR="/etc/php5"
33 PHP5CONFD="/etc/php5/conf.d"
34 PHP4DIR="/etc/php4"
35
36 need_restart=0
37
38
39 # phpmemlimit ()
40 #
41 #   Calculate memory size for PHP memory_limit option.
42 #
43 phpmemlimit () {
44
45     local memtotal memlimit
46     memtotal=`awk 'tolower($1) == "memtotal:" { printf("%i", $2/1024); }' /proc/meminfo`
47
48     if [ $memtotal -lt 512 ]; then memlimit=16; fi
49     if [ $memtotal -ge 512 ]; then memlimit=32; fi
50     if [ $memtotal -gt 1024 ]; then memlimit=64; fi
51
52     echo $memlimit
53 }
54
55 # phpinivalidate ()
56 #
57 #   Validate php.ini values.
58 #
59 phpinivalidate () {
60
61     local ini_file
62     ini_file="$1"
63
64     awk -F'[[:space:]]*=[[:space:]]*' \
65         '/^[[:space:]]*[[:alpha:]_]+[[:space:]]*=[[:space:]]*[[:digit:]]+[MGK]B/ {print $1" = "$2}' \
66         "$ini_file"
67 }
68
69
70 # Disable PHP4 module and enable Apache2 PHP5 module.
71 #
72 if [ -e /etc/apache2/apache2.conf ]; then
73     if [ -e "$A2MODEDIR/php4.load" ]; then
74         cp_echo "CN: Disabling PHP4 module for Apache2 web server"
75         a2dismod php4 >/dev/null || true
76         need_restart=1
77     fi
78     if [ ! -e "$A2MODEDIR/php5.load" ] || [ ! -e "$A2MODEDIR/php5.conf" ]; then
79         cp_echo "CN: Enabling PHP5 module for Apache2 web server"
80         a2enmod php5 >/dev/null || true
81
82         if [ -e "$A2MODEDIR/php5.conf" ]; then
83             if egrep -qi "^[[:space:]]*php_admin_value engine Off" "$A2MODEDIR/php5.conf"; then
84                 db_fset php5-cn/userdir seen false
85                 db_title php5-cn - konfiguracija
86                 db_subst php5-cn/userdir php5_conf "$A2MODEDIR/php5.conf"
87                 db_input critical php5-cn/userdir || true
88                 db_go || true
89             fi
90         fi
91
92         need_restart=1
93     fi
94 fi
95
96
97 # Check for PHP5 SAPI directories and configuration files.
98 #
99 for SAPI in apache2 cli; do
100
101     if [ ! -d "$PHP5DIR/$SAPI" ]; then
102         cp_echo "CN: Creating configuration directory $PHP5DIR/$SAPI/"
103         mkdir -p $PHP5DIR/$SAPI/
104     fi
105
106     ini_file="$PHP5DIR/$SAPI/php.ini"
107
108     if [ ! -f "$ini_file" ]; then
109
110         cp_echo "CN: Generating configuration file $ini_file"
111
112         ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
113         if [ "$SAPI" = "cli" ]; then
114             if [ -f "/usr/share/php5/php.ini-production.cli" ]; then
115                 cat /usr/share/php5/php.ini-production.cli > $ini_file_tmp
116             fi
117         else
118             if [ -f "/usr/share/php5/php.ini-production" ]; then
119                 cat /usr/share/php5/php.ini-production > $ini_file_tmp
120             fi
121         fi
122         cp_mv $ini_file_tmp $ini_file
123         need_restart=1
124     fi
125     chmod 644 $ini_file
126 done
127
128
129 # Check for /etc/php5/conf.d/ directory.
130 #
131 if [ ! -d "$PHP5CONFD" ]; then
132     cp_echo "CN: Creating configuration directory $PHP5CONFD"
133     mkdir -p $PHP5CONFD/
134 fi
135
136
137 # Check if PHP5 extensions are enabled.
138 #
139 for php5ext in $EXTENSIONS; do
140
141     php5ext_re="^[[:space:]]*extension[[:space:]]*=[[:space:]]*$php5ext\.so"
142
143     # Remove extension entry from /etc/php5/(apache2|cli).ini
144     # configuration files.
145     for SAPI in apache2 cli; do
146
147         ini_file="$PHP5DIR/$SAPI/php.ini"
148
149         if [ -f "$ini_file" ]; then
150
151             if egrep -q "$php5ext_re" "$ini_file"; then
152                 cp_echo "CN: Removing $php5ext extension from file $ini_file"
153             fi
154
155             cp_check_and_sed "$php5ext_re" \
156                 "/$php5ext_re/d" \
157                 "$ini_file" && need_restart=1 || true
158         fi
159     done
160
161     # Check extension configuration in /etc/php5/conf.d/ directory.
162     if [ ! -f "$PHP5CONFD/$php5ext.ini" ]; then
163
164         cp_echo "CN: Creating configuration file $PHP5CONFD/$php5ext.ini"
165
166         php5ext_up=`echo $php5ext | tr [:lower:] [:upper:] | sed 's/Y/y/'`
167         INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
168
169         cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
170
171         printf "# configuration for php %s module\nextension=%s.so\n" "${php5ext_up}" "${php5ext}" >> "$INITMP"
172         cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
173
174         need_restart=1
175     else
176
177         if ! grep -q "$php5ext_re" "$PHP5CONFD/$php5ext.ini"; then
178
179             cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
180
181             INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
182             cat "$PHP5CONFD/$php5ext.ini" > "$INITMP"
183             echo "extension=$php5ext.so" >> "$INITMP"
184             cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
185
186             need_restart=1
187         fi
188     fi
189     chmod 644 $PHP5CONFD/$php5ext.ini
190 done
191
192
193 # Enable some PHP5 tweaks for Apache2 web server (/etc/php5/apache2/php.ini).
194 #
195 #   * upload_max_filesize = 256M
196 #   * post_max_size, memory_limit = depends on system memory, we are using
197 #     phpmemlimit() function.
198 #   * error_reporting = E_ERROR
199 #
200 for SAPI in apache2 cli; do
201
202     if [ ! -d "$PHP5DIR/$SAPI" ]; then
203         continue
204     fi
205
206     ini_file="$PHP5DIR/$SAPI/php.ini"
207     db_get php5-cn/${SAPI} || true
208     if [ "$RET" = "true" ]; then
209
210         cp_echo "CN: Checking and enabling some specific parameters in file $ini_file"
211
212         phplimit="$(phpmemlimit)M"
213
214         if [ -f "$ini_file" ]; then
215
216             cp_check_and_sed "^[[:space:]]*upload_max_filesize[[:space:]]*=" \
217                 's/^[[:space:]]*upload_max_filesize[[:space:]]*=.*/upload_max_filesize = 256M/' \
218                 "$ini_file" && need_restart=1 || true
219
220             cp_check_and_sed "^[[:space:]]*post_max_size[[:space:]]*=" \
221                 "s/^[[:space:]]*post_max_size[[:space:]]*=.*/post_max_size = ${phplimit}/" \
222                 "$ini_file" && need_restart=1 || true
223
224             cp_check_and_sed "^[[:space:]]*memory_limit[[:space:]]*=" \
225                 "s/^[[:space:]]*memory_limit[[:space:]]*=.*/memory_limit = ${phplimit}/" \
226                 "$ini_file" && need_restart=1 || true
227
228             cp_check_and_sed "^[[:space:]]*error_reporting[[:space:]]*=" \
229                 "s/^[[:space:]]*error_reporting[[:space:]]*=.*/error_reporting = E_ERROR/" \
230                 "$ini_file" && need_restart=1 || true
231         fi
232
233         ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
234         if [ -f "$ini_file" ]; then
235             cat $ini_file > $ini_file_tmp
236         fi
237
238         if ! egrep -q "^[[:space:]]*upload_max_filesize[[:space:]]*=" $ini_file_tmp; then
239             echo "upload_max_filesize = 256M" >> "$ini_file_tmp"
240             need_restart=1
241         fi
242         if ! egrep -q "^[[:space:]]*post_max_size[[:space:]]*=" $ini_file_tmp; then
243             echo "post_max_size = ${phplimit}" >> "$ini_file_tmp"
244             need_restart=1
245         fi
246         if ! egrep -q "^[[:space:]]*memory_limit[[:space:]]*=" $ini_file_tmp; then
247             echo "memory_limit = ${phplimit}" >> "$ini_file_tmp"
248             need_restart=1
249         fi
250         if ! egrep -q "^[[:space:]]*error_reporting[[:space:]]*=" $ini_file_tmp; then
251             echo "error_reporting = E_ERROR" >> "$ini_file_tmp"
252             need_restart=1
253         fi
254         cp_mv "$ini_file_tmp" "$ini_file"
255         chmod 644 "$ini_file"
256
257         if [ -e "$ini_file_tmp" ]; then
258             rm -f "$ini_file_tmp"
259         fi
260     fi
261
262     # Validate php.ini values.
263     if [ -f "$ini_file" ]; then
264         php5_inivalues="$(phpinivalidate "$ini_file")"
265         if [ -n "$php5_inivalues" ]; then
266             db_fset php5-cn/inivalues seen false
267             db_title php5-cn - konfiguracija za $(echo ${SAPI} | sed 's/a/A/;s/cli/CLI/')
268             db_subst php5-cn/inivalues php5_sapi $(echo ${SAPI} | sed 's/a/A/;s/cli/CLI/')
269             db_subst php5-cn/inivalues ini_file "$ini_file"
270             db_capb escape
271             db_subst php5-cn/inivalues php5_inivalues "$(echo -n "$php5_inivalues" | debconf-escape -e)"
272             db_input critical php5-cn/inivalues || true
273             db_go || true
274         fi
275     fi
276 done
277
278
279 # Compare active PHP5 extensions with PHP4 ones, if PHP4 configuration still exists.
280 #
281 if [ -d "${PHP4DIR}" ] && [ -f "${PHP4DIR}/apache2/php.ini" ]; then
282
283     phpext_re='^[[:space:]]*extension[[:space:]]*=[[:space:]]*["]{0,1}(.*)\.so["]{0,1}'
284
285     # Get all active PHP4 extensions.
286     php4_ext="$(find ${PHP4DIR} -type f -name *.ini | 
287          egrep "\/(conf\.d|apache2|cli)\/" | 
288          xargs sed -rn 's/'$phpext_re'/\1/Ip')" || true
289
290     # Get all active PHP5 extensions.
291     php5_ext=""
292     if [ -d "${PHP5DIR}" ]; then
293         php5_ext="$(find ${PHP5DIR} -type f -name *.ini | 
294              egrep "\/(conf\.d|apache2|cli)\/" | 
295              xargs sed -rn 's/'$phpext_re'/\1/Ip')" || true
296     fi
297
298     # Compare PHP5 <-> PHP4 extensions.
299     php5_ext_mis=""
300     for ext in $php4_ext; do
301         if [ "$(echo $php5_ext | egrep $ext)" = "" ]; then
302             php5_ext_mis="\n${ext}${php5_ext_mis}"
303         fi
304     done
305
306     # Some PHP5 extensions are not active.
307     if [ -n "$php5_ext_mis" ]; then
308         db_fset php5-cn/extensions seen false
309         db_title php5-cn - konfiguracija
310         db_capb escape
311         db_subst php5-cn/extensions php5_extensions "${php5_ext_mis#*\n}"
312         db_input critical php5-cn/extensions || true
313         db_go || true
314     fi
315 fi
316
317
318 db_stop || true
319
320
321 # Restart Apache2 web server if needed.
322 #
323 if [ $need_restart -eq 1 ]; then
324
325     # Check Apache2 web server configuration.
326     if apache2ctl configtest 2>/dev/null; then
327         invoke-rc.d apache2 force-reload || true
328     else
329         # Something is broken.
330         cp_echo "CN: Your Apache2 configuration is broken."
331         cp_echo "CN: Please, check the service after the installation finishes!"
332     fi
333 fi
334
335
336 # Mail root
337 #
338 cp_mail "$PKG"
339
340 #DEBHELPER#
341
342 exit 0