File: postinst

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (294 lines) | stat: -rw-r--r-- 7,721 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash -e

# Postinst script for PHPWiki.
# Written by Matt Brown <debian@mattb.net.nz>. 
# Based on the original PHPWiki postinst (in Perl) by Matthew Palmer
# Released under the GPL version 2.

# Use Debconf
. /usr/share/debconf/confmodule
# Use dbconfig-common
. /usr/share/dbconfig-common/dpkg/postinst

# The following functions are copied from the webapps-common package
# written by Sean Finney and hosted on Alioth. The package is not yet
# in unstable, so we have to copy the required functions into here for
# now. Upstream source can be found at 
# http://alioth.debian.org/projects/webapps-common/
# --- BEGIN webapps-common --
wc_httpd_apaches="apache apache-ssl apache-perl apache2"
wc_httpd_supported="$wc_httpd_apaches"
wc_httpd_installed(){
	local httpds
	if [ "$*" ]; then
		httpds=$*
	else
		httpds=$wc_httpd_supported
	fi
	for f in $httpds; do
		if test -x /usr/sbin/$f; then
			echo $f
		fi
	done
}
wc_httpd_running(){
	local httpds
	if [ "$*" ]; then
		httpds=$*
	else
		httpds=$wc_httpd_supported
	fi
	for f in $httpds; do
		if pgrep -x $f >/dev/null; then
			echo $f
		fi
	done
}
wc_httpd_invoke(){
	local httpds cmd err
	if [ ! "$1" ]; then
		echo "i need at least a command!" 2>&1
		return 1
	fi
	cmd="$1"
	shift
	if [ "$*" ]; then
		httpds=$*
	else
		httpds=`wc_httpd_running`
	fi
	for f in $httpds; do
		if [ -x /etc/init.d/$f ]; then
			invoke-rc.d $f $cmd || return $?
		fi
	done
}
wc_httpd_apache_include(){
	local h incfile httpds confdir
	if [ ! "$1" ]; then
		echo "i need at least a file!" 2>&1
		return 1
	fi
	incfile="$1"
	shift
	if [ ! "$1" ]; then
		echo "i also need a name!" 2>&1
		return 1
	fi
	name="$1"
	shift
	if [ ! -e "$incfile" ]; then
		echo "include file $incfile does not exist!" 2>&1
		return 1
	fi
	if [ "$*" ]; then
		httpds=$*
	else
		httpds=`wc_httpd_installed $wc_httpd_supported`
	fi
	for h in $httpds; do
		confdir="/etc/$h/conf.d"
		conflink="$confdir/${name}.conf"
		if [ -d "$confdir" -a ! -e "$conflink" ]; then
			ln -s "$incfile" "$conflink"
		fi
	done
}
# --- END webapps-common --
    
function upgrade_config_ini()
{
    confout=`tempfile`
    
    # Can only upgrade if there is an existing configuration
    if [ ! -f '/etc/phpwiki/index.php' ]; then
        handle_config_ini
        return;
    fi
    
    # Run the migration script
    /usr/share/phpwiki/migrate-phpwiki-config /usr/share/phpwiki/config/config-template.ini < /etc/phpwiki/index.php > $confout
    
    # Put the configuration file in place
    /usr/bin/ucf --debconf-ok $confout /etc/phpwiki/config.ini
    rm -f $confout

    # Ensure perms are all good
    chmod 0640 /etc/phpwiki/config.ini
    chgrp www-data /etc/phpwiki/config.ini
}

function handle_config_ini () 
{
    
    # The DB config fragment we need to dynamically generate
    dbconf=''
    dbtype='SQL'
    
    if [ -f '/etc/phpwiki/config.ini' ]; then
        n=`egrep "^[[:space:]]*DATABASE_TYPE[[:space:]]*=[[:space:]]*dba" /etc/phpwiki/config.ini | wc -l | sed 's/ //g'`
        if [ "$n" -ne "0" ]; then
            # We're still using dba
            dbtype='dba'
        fi
    fi
    
    if [ "$dbtype" == "dba" ]; then
        dbconf="DATABASE_TYPE = dba\n"
        dbconf="${dbconf}DATABASE_DBA_HANDLER = db4\n"
        dbconf="${dbconf}DATABASE_DIRECTORY = /var/lib/phpwiki\n"
    else
        # dbconfig-common managed installation
        . /etc/phpwiki/dbconf.tmp
        # Set some defaults incase the user declined dbconfig common's help
        # These defaults won't actually work, but will give the user an idea 
        # of what goes where.
        if [ -z "$dbtype" ]; then
            dbtype="sqlite"
            basepath="/var/lib/phpwiki"
            dbname="phpwiki_pagedb.sqlite"
        fi
        dbconf="DATABASE_TYPE = SQL\n"
        dbconf="${dbconf}DATABASE_DSN = \"$dbtype://"
        if [ "$dbtype" == "sqlite" ]; then
            dbconf="${dbconf}/$basepath/$dbname\"\n"
        else
            dbconf="${dbconf}${dbuser}"
            if [ "$dbpass" != "" ]; then
                dbconf="${dbconf}:${dbpass}"
            fi
            dbconf="${dbconf}@${dbserver}"
            if [ "$dbport" != "" ]; then
                dbconf="${dbconf}:${dbport}"
            fi
            dbconf="${dbconf}/${dbname}\"\n"
        fi
        dbconf="${dbconf}DATABASE_TIMEOUT = 10\n"
        dbconf="${dbconf}DATABASE_SESSION_TABLE = session\n"
    fi	
    
    confout=`tempfile`
    sed "s~##DBPARAMS##~$dbconf~g" < /usr/share/phpwiki/config/config-dist.ini > $confout
    
    /usr/bin/ucf --debconf-ok $confout /etc/phpwiki/config.ini
    rm -f $confout
    rm -f /etc/phpwiki/dbconf.tmp
    ucf --purge /etc/phpwiki/dbconf.tmp

    # Ensure perms are all good
    chmod 0640 /etc/phpwiki/config.ini
    chgrp www-data /etc/phpwiki/config.ini
    
}

function write_apache_conf ()
{
    
    apacheconf=`tempfile`;
    
    # Get configured docroot. Make sure it doesn't end in /
    db_get phpwiki/system/documentroot
    docroot="${RET/%\//}"
    
    # Access Control
    db_get phpwiki/system/accessible
    if [ "$RET" == "global" ]; then
        allowstr="allow from all"
    elif [ "$RET" == "localhost only" ]; then
        allowstr="allow from 127.0.0.1"
    else
        db_get phpwiki/system/localnet
        allowstr="allow from $RET"
    fi
    
    # Write the config
    cat > $apacheconf << __EOF__
Alias /phpwikidata              /usr/share/phpwiki/
Alias ${docroot}/               /usr/share/phpwiki/index.php/

<Directory /usr/share/phpwiki/>
    DirectoryIndex index.php
    Options +FollowSymLinks
    AllowOverride None
    Order allow,deny
    $allowstr
</Directory>
__EOF__

    /usr/bin/ucf --debconf-ok $apacheconf /etc/phpwiki/apache.conf
    rm -f $apacheconf
    chmod 0644 /etc/phpwiki/apache.conf
    
}

# Clear out any lingering Include directives which were used before 1.3.7-2
function clean_apache_config () 
{
    
    for srv in "apache apache-ssl apache-perl"; do
        dir="/etc/$srv"
        if [ ! -d $dir ]; then
            continue
        fi
        
        n=`grep "/etc/phpwiki/apache.conf" $dir/httpd.conf | wc -l | sed 's/ //g'`
        if [ "$n" -ne "0" ]; then
            tmpfile=`tempfile`
            grep -v '^Include /etc/phpwiki/apache.conf\$' $dir/httpd.conf > $tmpfile
            mv $tmpfile $dir/httpd.conf
        fi
    done
    
}

# Let dbconfig-common do its work
dbc_first_version="1.3.12p3-2"
dbc_generate_include="sh:/etc/phpwiki/dbconf.tmp"
dbc_dbfile_owner="root:www-data"
dbc_dbfile_perms="0664"
dbc_go phpwiki $@

# Process Command Line Arguments
if [ "$1" == "configure" ]; then
    
    if [ "$2" == "" -o "$2" == "<>" ]; then
        
        # Fresh Install
        handle_config_ini
        
    else
        # Upgrade

        # Remove old apache stuff
        if /usr/bin/dpkg --compare-versions "$2" lt "1.3.7-2"; then
            clean_apache_config
        fi
        
        # This is the biggest upgrade!
        # Upgrade config file and database
        if /usr/bin/dpkg --compare-versions "$2" lt "1.3.10-1"; then
            upgrade_config_ini
            db_title "PHPWiki Configuration Upgraded"
            db_input high phpwiki/notes/configupgrade || true
            db_go
        else 
            # Regenerate the config file incase new options are present
            # UCF will handle merging and user changes
            handle_config_ini
        fi
        
    fi
    
    # Always configure apache
    write_apache_conf
    wc_httpd_apache_include /etc/phpwiki/apache.conf phpwiki 
    wc_httpd_invoke reload
    
    # Make sure PHPwiki can write to the plugin cache directory
    chgrp www-data /var/cache/phpwiki
    chmod 0775 /var/cache/phpwiki

fi

#DEBHELPER#