5554f5de340d0439e4bdd34d70978f250f2f14fe
[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                 need_restart=1
82         fi
83 fi
84
85
86 # Check for PHP5 SAPI directories and configuration files.
87 #
88 for SAPI in apache2 cli; do
89
90         if [ ! -d "$PHP5DIR/$SAPI" ]; then
91                 cp_echo "CN: Creating configuration directory $PHP5DIR/$SAPI/"
92                 mkdir -p $PHP5DIR/$SAPI/
93         fi
94
95         ini_file="$PHP5DIR/$SAPI/php.ini"
96
97         if [ ! -f "$ini_file" ]; then
98
99                 cp_echo "CN: Generating configuration file $ini_file"
100
101                 ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
102                 if [ "$SAPI" = "cli" ]; then
103                     if [ -f "/usr/share/php5/php.ini-dist.cli" ]; then
104                         cat /usr/share/php5/php.ini-dist.cli > $ini_file_tmp
105                     fi
106                 else
107                     if [ -f "/usr/share/php5/php.ini-dist" ]; then
108                         cat /usr/share/php5/php.ini-dist > $ini_file_tmp
109                     fi
110                 fi
111                 cp_mv $ini_file_tmp $ini_file
112                 need_restart=1
113         fi
114         chmod 644 $ini_file
115 done
116
117
118 # Check for /etc/php5/conf.d/ directory.
119 #
120 if [ ! -d "$PHP5CONFD" ]; then
121         cp_echo "CN: Creating configuration directory $PHP5CONFD"
122         mkdir -p $PHP5CONFD/
123 fi
124
125
126 # Check if PHP5 extensions are enabled.
127 #
128 for php5ext in $EXTENSIONS; do
129
130         php5ext_re="^[[:space:]]*extension[[:space:]]*=[[:space:]]*$php5ext\.so"
131
132         # Remove extension entry from /etc/php5/(apache2|cli).ini
133         # configuration files.
134         for SAPI in apache2 cli; do
135
136                 ini_file="$PHP5DIR/$SAPI/php.ini"
137
138                 if [ -f "$ini_file" ]; then
139
140                         if egrep -q "$php5ext_re" "$ini_file"; then
141                                 cp_echo "CN: Removing $php5ext extension from file $ini_file"
142                         fi
143
144                         cp_check_and_sed "$php5ext_re" \
145                                 "/$php5ext_re/d" \
146                                 "$ini_file" && need_restart=1 || true
147                 fi
148         done
149
150         # Check extension configuration in /etc/php5/conf.d/ directory.
151         if [ ! -f "$PHP5CONFD/$php5ext.ini" ]; then
152
153                 cp_echo "CN: Creating configuration file $PHP5CONFD/$php5ext.ini"
154                 
155                 php5ext_up=`echo $php5ext | tr [:lower:] [:upper:] | sed 's/Y/y/'`
156                 INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
157
158                 cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
159                 
160                 printf "# configuration for php %s module\nextension=%s.so\n" "${php5ext_up}" "${php5ext}" >> "$INITMP"
161                 cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
162                 
163                 need_restart=1
164         else
165
166                 if ! grep -q "$php5ext_re" "$PHP5CONFD/$php5ext.ini"; then
167
168                         cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
169                         
170                         INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
171                         cat "$PHP5CONFD/$php5ext.ini" > "$INITMP"
172                         echo "extension=$php5ext.so" >> "$INITMP"
173                         cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
174                         
175                         need_restart=1
176                 fi
177         fi
178         chmod 644 $PHP5CONFD/$php5ext.ini
179 done
180
181
182 # Enable some PHP5 tweaks for Apache2 web server (/etc/php5/apache2/php.ini).
183 #
184 #   * upload_max_filesize = 256M
185 #   * post_max_size, memory_limit = depends on system memory, we are using
186 #     phpmemlimit() function.
187 #
188 for SAPI in apache2 cli; do
189
190         if [ ! -d "$PHP5DIR/$SAPI" ]; then
191                 continue
192         fi
193
194         ini_file="$PHP5DIR/$SAPI/php.ini"
195         db_get php5-cn/${SAPI} || true
196         if [ "$RET" = "true" ]; then
197
198             cp_echo "CN: Checking and enabling some specific parameters in file $ini_file"
199             
200             phplimit="$(phpmemlimit)M"
201         
202             if [ -f "$ini_file" ]; then
203
204                 cp_check_and_sed "^[[:space:]]*upload_max_filesize[[:space:]]*=" \
205                         's/^[[:space:]]*upload_max_filesize[[:space:]]*=.*/upload_max_filesize = 256M/' \
206                         "$ini_file" && need_restart=1 || true
207
208                 cp_check_and_sed "^[[:space:]]*post_max_size[[:space:]]*=" \
209                         "s/^[[:space:]]*post_max_size[[:space:]]*=.*/post_max_size = ${phplimit}/" \
210                         "$ini_file" && need_restart=1 || true
211
212                 cp_check_and_sed "^[[:space:]]*memory_limit[[:space:]]*=" \
213                         "s/^[[:space:]]*memory_limit[[:space:]]*=.*/memory_limit = ${phplimit}/" \
214                         "$ini_file" && need_restart=1 || true
215             fi
216                 
217             ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
218             if [ -f "$ini_file" ]; then
219                 cat $ini_file > $ini_file_tmp
220             fi
221
222             if ! egrep -q "^[[:space:]]*upload_max_filesize[[:space:]]*=" $ini_file_tmp; then
223                 echo "upload_max_filesize = 256M" >> "$ini_file_tmp"
224                 need_restart=1
225             fi
226             if ! egrep -q "^[[:space:]]*post_max_size[[:space:]]*=" $ini_file_tmp; then
227                 echo "post_max_size = ${phplimit}" >> "$ini_file_tmp"
228                 need_restart=1
229             fi
230             if ! egrep -q "^[[:space:]]*memory_limit[[:space:]]*=" $ini_file_tmp; then
231                 echo "memory_limit = ${phplimit}" >> "$ini_file_tmp"
232                 need_restart=1
233             fi
234             cp_mv "$ini_file_tmp" "$ini_file"
235             chmod 644 "$ini_file"
236
237             if [ -e "$ini_file_tmp" ]; then
238                 rm -f "$ini_file_tmp"
239             fi
240         fi
241
242         # Validate php.ini values.
243         if [ -f "$ini_file" ]; then
244                 php5_inivalues="$(phpinivalidate "$ini_file")"
245                 if [ -n "$php5_inivalues" ]; then
246                         db_fset php5-cn/inivalues seen false
247                         db_title php5-cn - konfiguracija za $(echo ${SAPI} | sed 's/a/A/;s/cli/CLI/')
248                         db_subst php5-cn/inivalues php5_sapi $(echo ${SAPI} | sed 's/a/A/;s/cli/CLI/')
249                         db_subst php5-cn/inivalues ini_file "$ini_file"
250                         db_capb escape
251                         db_subst php5-cn/inivalues php5_inivalues "$(echo -n "$php5_inivalues" | debconf-escape -e)"
252                         db_input critical php5-cn/inivalues || true
253                         db_go || true
254                 fi
255         fi
256 done
257
258
259 # Compare active PHP5 extensions with PHP4 ones, if PHP4 configuration still exists.
260 #
261 if [ -d "${PHP4DIR}" ] && [ -f "${PHP4DIR}/apache2/php.ini" ]; then
262
263         phpext_re='^[[:space:]]*extension[[:space:]]*=[[:space:]]*["]{0,1}(.*)\.so["]{0,1}'
264
265         # Get all active PHP4 extensions.
266         php4_ext="$(find ${PHP4DIR} -type f -name *.ini | 
267                  egrep "\/(conf\.d|apache2|cli)\/" | 
268                  xargs sed -rn 's/'$phpext_re'/\1/Ip')" || true
269
270         # Get all active PHP5 extensions.
271         php5_ext=""
272         if [ -d "${PHP5DIR}" ]; then
273             php5_ext="$(find ${PHP5DIR} -type f -name *.ini | 
274                      egrep "\/(conf\.d|apache2|cli)\/" | 
275                      xargs sed -rn 's/'$phpext_re'/\1/Ip')" || true
276         fi
277
278         # Compare PHP5 <-> PHP4 extensions.
279         php5_ext_mis=""
280         for ext in $php4_ext; do
281             if [ "$(echo $php5_ext | egrep $ext)" = "" ]; then
282                 php5_ext_mis="\n${ext}${php5_ext_mis}"
283             fi
284         done
285
286         # Some PHP5 extensions are not active.
287         if [ -n "$php5_ext_mis" ]; then
288             db_fset php5-cn/extensions seen false
289             db_title php5-cn - konfiguracija
290             db_capb escape
291             db_subst php5-cn/extensions php5_extensions "${php5_ext_mis#*\n}"
292             db_input critical php5-cn/extensions || true
293             db_go || true
294         fi
295 fi
296
297
298 db_stop || true
299
300
301 # Restart Apache2 web server if needed.
302 #
303 if [ $need_restart -eq 1 ]; then
304         
305         # Check Apache2 web server configuration.
306         if /usr/sbin/apache2ctl configtest 2>/dev/null; then
307
308                 # Restart Apache2 web server.
309                 if [ -x "/etc/init.d/apache2" ]; then
310                     if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
311                         invoke-rc.d apache2 force-reload || true
312                     else
313                         /etc/init.d/apache2 force-reload || true
314                     fi
315                 fi
316         else
317         
318                 # Something is broken.
319                 cp_echo "CN: Your Apache2 configuration is broken."
320                 cp_echo "CN: Please, check the service after the installation finishes!"
321         fi
322 fi
323
324
325 # Mail root
326 #
327 cp_mail "$PKG"
328
329 exit 0