Project

General

Profile

Download (9.48 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" -lt "65" ]; then
52
	APCSHMEMSIZE="1M"
53
fi
54
if [ "$AVAILMEM" -lt "96" ]; then
55
	APCSHMEMSIZE="5M"
56
fi
57
if [ "$AVAILMEM" -lt "128" ]; then
58
	APCSHMEMSIZE="10M"
59
fi
60
if [ "$AVAILMEM" -gt "128" ]; then
61
	APCSHMEMSIZE="15M"
62
fi
63
if [ "$AVAILMEM" -gt "256" ]; then
64
	APCSHMEMSIZE="20M"
65
fi
66
if [ "$AVAILMEM" -gt "384" ]; then
67
	APCSHMEMSIZE="25M"
68
fi
69
if [ "$AVAILMEM" -gt "512" ]; then
70
	APCSHMEMSIZE="30M"
71
fi
72
if [ "$AVAILMEM" -gt "784" ]; then
73
	APCSHMEMSIZE="35M"
74
fi
75

    
76
# Set upload directory
77
if [ "$PLATFORM" = "embedded" -o "$PLATFORM" = "nanobsd" ]; then
78
	UPLOADTMPDIR=`/usr/bin/grep upload_path /etc/inc/globals.inc | /usr/bin/cut -d'"' -f4`
79
else 
80
	UPLOADTMPDIR="/tmp"
81
fi
82

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

    
130
PHP_ZEND_MODULES="ioncube_loader"
131
PHP_ZEND_MODULES_TS="ioncube_loader_ts"
132

    
133
# Modules previously included.
134
# can be turned on by touching
135
# /etc/php_dynamodules/$modulename
136
#	sysvmsg \
137
#	sysvsem \
138
#	sysvshm \
139
#	bcmath \
140
#	tokenizer \
141
#	uploadprogress \
142
#	sockets \
143
#	Reflection \
144
#	mysql \
145
#	bz2	\
146

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

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

    
184
; Extensions
185

    
186
EOF
187

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

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

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

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

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

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

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

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

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

    
260
	/bin/cat >>/usr/local/lib/php.ini <<EOF
261

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

    
267
EOF
268

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

    
275
	/bin/cat >>/usr/local/lib/php.ini <<EOF
276

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

    
292
EOF
293

    
294

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

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

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

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

    
335
# Copy php.ini file to etc/ too (cli)
336
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
(82-82/107)