Project

General

Profile

Download (9.6 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
#	rc.php_ini_setup
4
#	Copyright (C) 2010 Scott Ullrich <sullrich@gmail.com>
5
#	All rights reserved.
6
#
7
#	Redistribution and use in source and binary forms, with or without
8
#	modification, are permitted provided that the following conditions are met:
9
#
10
#	1. Redistributions of source code must retain the above copyright notice,
11
#	   this list of conditions and the following disclaimer.
12
#
13
#	2. Redistributions in binary form must reproduce the above copyright
14
#	   notice, this list of conditions and the following disclaimer in the
15
#	   documentation and/or other materials provided with the distribution.
16
#
17
#	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
#	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19
#	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
#	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
#	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
#	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
#	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
#	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
#	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
#	POSSIBILITY OF SUCH DAMAGE.
27

    
28
# Set our operating platform
29
PLATFORM=`/bin/cat /etc/platform`
30

    
31
if [ -d /usr/local/lib/php/20090626 ]; then
32
	EXTENSIONSDIR="/usr/local/lib/php/20090626/"
33
else
34
	EXTENSIONSDIR="/usr/local/lib/php/20060613/"
35
fi
36

    
37
# Grab amount of memory that is detected
38
if [ -f /var/log/dmesg.boot ]; then
39
	AVAILMEM=`/bin/cat /var/log/dmesg.boot |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
40
else 
41
	AVAILMEM=`/sbin/dmesg -a |/usr/bin/awk '/avail memory/ { memory=($4 / 1048576); printf("%0.0f\n", memory); exit}'`
42
fi
43

    
44
if [ -z "$AVAILMEM" ]; then
45
	MEM=`/sbin/sysctl hw.physmem | cut -d':' -f2`
46
	AVAILMEM=`/bin/expr $MEM / 1048576`
47
fi
48

    
49
# Calculate APC SHM size according 
50
# to detected memory values
51
if [ "$AVAILMEM" -gt "135" ]; then
52
	APCSHMEMSIZE="10M"
53
fi
54
if [ "$AVAILMEM" -gt "256" ]; then
55
	APCSHMEMSIZE="20M"
56
fi
57
if [ "$AVAILMEM" -gt "384" ]; then
58
	APCSHMEMSIZE="25M"
59
fi
60
if [ "$AVAILMEM" -gt "512" ]; then
61
	APCSHMEMSIZE="30M"
62
fi
63
if [ "$AVAILMEM" -gt "784" ]; then
64
	APCSHMEMSIZE="50M"
65
fi
66

    
67
# Set upload directory
68
if [ "$PLATFORM" = "embedded" -o "$PLATFORM" = "nanobsd" ]; then
69
	UPLOADTMPDIR=`/usr/bin/grep upload_path /etc/inc/globals.inc | /usr/bin/cut -d'"' -f4`
70
else 
71
	UPLOADTMPDIR="/tmp"
72
fi
73

    
74
# Define php modules.  Do not add .so, it will  
75
# be done automatically by the script below.
76
PHPMODULES="standard"
77
if [  "$AVAILMEM" -gt 135 ]; then
78
	PHPMODULES="$PHPMODULES apc"
79
fi
80
# Config read/write
81
PHPMODULES="$PHPMODULES xml libxml dom"
82
PHPMODULES="$PHPMODULES simplexml xmlreader xmlwriter"
83
# Downloading via HTTP/FTP (pkg mgr, etc)
84
PHPMODULES="$PHPMODULES curl date"
85
# Internationalization 
86
PHPMODULES="$PHPMODULES gettext"
87
# User manager
88
PHPMODULES="$PHPMODULES ldap openssl pcntl"
89
PHPMODULES="$PHPMODULES hash"
90
PHPMODULES="$PHPMODULES mcrypt"
91
# Regexs, PERL style!
92
PHPMODULES="$PHPMODULES pcre"
93
# The mighty posix!
94
PHPMODULES="$PHPMODULES posix"
95
PHPMODULES="$PHPMODULES readline"
96
# Login sessions
97
PHPMODULES="$PHPMODULES session"
98
# Extra sanity seatbelts
99
PHPMODULES="$PHPMODULES suhosin"
100
# Firewall rules edit
101
PHPMODULES="$PHPMODULES ctype"
102
# firewall_rules_edit.php
103
PHPMODULES="$PHPMODULES mbstring"
104
# Synchronization primitives
105
PHPMODULES="$PHPMODULES shmop"
106
# Page compression
107
PHPMODULES="$PHPMODULES zlib"
108
# SQLlite & Database
109
PHPMODULES="$PHPMODULES spl"
110
PHPMODULES="$PHPMODULES pdo"
111
PHPMODULES="$PHPMODULES sqlite"
112
# RADIUS
113
PHPMODULES="$PHPMODULES radius"
114
# ZeroMQ
115
PHPMODULES="$PHPMODULES zmq"
116
# SSH2
117
PHPMODULES="$PHPMODULES ssh2"
118
# pfSense extensions
119
PHPMODULES="$PHPMODULES pfSense"
120
# json
121
PHPMODULES="$PHPMODULES json"
122
# bcmath
123
PHPMODULES="$PHPMODULES bcmath"
124

    
125
PHP_ZEND_MODULES="ioncube_loader"
126
PHP_ZEND_MODULES_TS="ioncube_loader_ts"
127

    
128
# Modules previously included.
129
# can be turned on by touching
130
# /etc/php_dynamodules/$modulename
131
#	sysvmsg \
132
#	sysvsem \
133
#	sysvshm \
134
#	bcmath \
135
#	tokenizer \
136
#	uploadprogress \
137
#	sockets \
138
#	Reflection \
139
#	mysql \
140
#	bz2	\
141

    
142
# Clear the .ini file to make sure we are clean
143
if [ -f /usr/local/etc/php.ini ]; then
144
	/bin/rm /usr/local/etc/php.ini
145
fi
146
if [ -f /usr/local/lib/php.ini ]; then
147
	/bin/rm /usr/local/lib/php.ini
148
fi
149
LOADED_MODULES=`/usr/local/bin/php -m | /usr/bin/grep -v "\["`
150

    
151
# Fetch the timezone from the XML and set it here. We set it later too in the running scripts
152
TIMEZONE=`cat /conf/config.xml | egrep -E '<timezone>(.*?)</timezone>' | awk -F'>' '{print $2}'|awk -F'<' '{print $1}'`
153

    
154
# Get a loaded module list in the stock php
155
# Populate a dummy php.ini to avoid
156
# the file being clobbered and the firewall
157
# not being able to boot back up.
158
/bin/cat >/usr/local/lib/php.ini <<EOF
159
; File generated from /etc/rc.php_ini_setup
160
output_buffering = "0"
161
expose_php = Off
162
implicit_flush = true
163
magic_quotes_gpc = Off
164
max_execution_time = 900
165
max_input_time = 1800
166
register_argc_argv = On
167
file_uploads = On
168
upload_tmp_dir = ${UPLOADTMPDIR}
169
upload_max_filesize = 200M
170
post_max_size = 200M
171
html_errors = Off
172
zlib.output_compression = Off
173
zlib.output_compression_level = 1
174
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg"
175
display_startup_errors=on
176
display_errors=on
177
log_errors=on
178
error_log=/tmp/PHP_errors.log
179
extension_dir=${EXTENSIONSDIR}
180
date.timezone="${TIMEZONE}"
181

    
182
; Extensions
183

    
184
EOF
185

    
186
# Copy php.ini file to etc/ too (cli)
187
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
188

    
189
# Ensure directory exists
190
if [ ! -d /etc/php_dynamodules ]; then
191
	/bin/mkdir /etc/php_dynamodules
192
fi
193
if [ ! -d /etc/php_dynamodules_zend ]; then
194
	/bin/mkdir /etc/php_dynamodules_zend
195
fi
196
if [ ! -d /etc/php_dynamodules_zend_ts ]; then
197
	/bin/mkdir /etc/php_dynamodules_zend_ts
198
fi
199

    
200
# Read in dynamodules
201
if [ -d /etc/php_dynamodules ]; then
202
	DYNA_MODULES=`/bin/ls -Utr /etc/php_dynamodules/`
203
	PHPMODULES="$PHPMODULES $DYNA_MODULES"
204
fi
205

    
206
# Read in zend modules
207
if [ -d /etc/php_dynamodules_zend ]; then
208
	DYNA_MODULES=`/bin/ls /etc/php_dynamodules_zend/`
209
	PHP_ZEND_MODULES="$PHP_ZEND_MODULES $DYNA_MODULES"
210
fi
211

    
212
# Read in zend threaded modules
213
if [ -d /etc/php_dynamodules_zend_ts ]; then
214
	DYNA_MODULES=`/bin/ls /etc/php_dynamodules_zend_ts/`
215
	PHP_ZEND_MODULES_TS="$PHP_ZEND_MODULES_TS $DYNA_MODULES"
216
fi
217

    
218
# Loop through and generate modules to load.
219
# Take into account modules built into php.
220
for EXT in $PHPMODULES; do
221
	SHOULDADD="true"
222
	# Check to see if module is compiled into php statically
223
	for LM in $LOADED_MODULES; do
224
		if [ "$EXT" = "$LM" ]; then
225
			SHOULDADD="false"
226
		fi
227
	done
228
	if [ "$SHOULDADD" = "true" ]; then
229
		# Ensure extension exists before adding.
230
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
231
			echo "extension=${EXT}.so" >> /usr/local/lib/php.ini
232
		fi
233
	fi
234
done
235

    
236
# Zend modules
237
for EXT in $PHP_ZEND_MODULES; do
238
	# Ensure extension exists before adding.
239
	if [ -f "${EXTENSIONSDIR}/ioncube/${EXT}.so" ]; then
240
		echo "zend_extension=${EXTENSIONSDIR}/ioncube/${EXT}.so" >> /usr/local/lib/php.ini
241
	fi
242
done
243

    
244
# Zend threaded modules
245
for EXT in $PHP_ZEND_MODULES_TS; do
246
	# Ensure extension exists before adding.
247
	if [ -f "${EXTENSIONSDIR}/ioncube/${EXT}.so" ]; then
248
		echo "zend_extension_ts=${EXTENSIONSDIR}/ioncube/${EXT}.so" >> /usr/local/lib/php.ini
249
	fi
250
done
251

    
252
# Get amount of ram installed on this system
253
RAM=`/sbin/sysctl hw.realmem | /usr/bin/awk '{print $2/1000000}' | /usr/bin/awk -F '.' '{print $1}'`
254
export RAM
255
export LOWMEM
256
if [  "$RAM" -gt 135 ]; then
257

    
258
	/bin/cat >>/usr/local/lib/php.ini <<EOF
259

    
260
; APC Settings
261
apc.enabled="1"
262
apc.enable_cli="0"
263
apc.shm_size="${APCSHMEMSIZE}"
264

    
265
EOF
266

    
267
else
268
	LOWMEM="TRUE"
269
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC."
270
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC." | /usr/bin/logger -p daemon.info -i -t rc.php_ini_setup
271
fi
272

    
273
	/bin/cat >>/usr/local/lib/php.ini <<EOF
274

    
275
[suhosin]
276
suhosin.get.max_array_depth = 5000
277
suhosin.get.max_array_index_length = 256
278
suhosin.get.max_vars = 5000
279
suhosin.get.max_value_length = 500000
280
suhosin.post.max_array_depth = 5000
281
suhosin.post.max_array_index_length = 256
282
suhosin.post.max_vars = 5000
283
suhosin.post.max_value_length = 500000
284
suhosin.request.max_array_depth = 5000
285
suhosin.request.max_array_index_length = 256
286
suhosin.request.max_vars = 5000
287
suhosin.request.max_value_length = 500000
288
suhosin.memory_limit = 512435456
289

    
290
EOF
291

    
292

    
293
# Copy php.ini file to etc/ too (cli)
294
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
295

    
296
# Remove old log file if it exists.
297
if [ -f /var/run/php_modules_load_errors.txt ]; then
298
	/bin/rm /var/run/php_modules_load_errors.txt
299
fi 
300

    
301
for EXT in $PHPMODULES; do
302
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
303
done
304

    
305
# Check loaded modules and remove anything that did not load correctly
306
LOADED_MODULES=`/usr/local/bin/php -m | /usr/bin/tr '[:upper:]' '[:lower:]' 2>/dev/null | /usr/bin/grep -v "\["`
307
for EXT in $PHPMODULESLC; do
308
	SHOULDREMOVE="true"
309
	for LM in $LOADED_MODULES; do
310
		if [ "$EXT" = "$LM" ]; then
311
			SHOULDREMOVE="false"
312
		fi		
313
	done
314
	# Handle low memory situations
315
	if [ "$LOWMEM" = "TRUE" ]; then
316
		if [ "$EXT" = "apc" ]; then
317
			SHOULDREMOVE="true"
318
		fi
319
		if [ "$EXT" = "xcache" ]; then
320
			SHOULDREMOVE="true"
321
		fi
322
	fi
323
	if [ "$SHOULDREMOVE" = "true" ]; then
324
		if [ -f "${EXTENSIONSDIR}${EXT}.so" ]; then
325
			echo ">>> ${EXT} did not load correctly.  Removing from php.ini..." >> /var/run/php_modules_load_errors.txt
326
			/bin/cat /usr/local/lib/php.ini | /usr/bin/grep -v $EXT > /tmp/php.ini
327
			/bin/rm -f /usr/local/lib/php.ini
328
			/bin/mv /tmp/php.ini /usr/local/lib/php.ini
329
		fi
330
	fi
331
done
332

    
333
# Copy php.ini file to etc/ too (cli)
334
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
(85-85/110)