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