Project

General

Profile

Download (9.73 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" -le "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
# bcmath
130
PHPMODULES="$PHPMODULES bcmath"
131

    
132
PHP_ZEND_MODULES="ioncube_loader"
133
PHP_ZEND_MODULES_TS="ioncube_loader_ts"
134

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

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

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

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

    
189
; Extensions
190

    
191
EOF
192

    
193
# Copy php.ini file to etc/ too (cli)
194
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
195

    
196
# Ensure directory exists
197
if [ ! -d /etc/php_dynamodules ]; then
198
	/bin/mkdir /etc/php_dynamodules
199
fi
200
if [ ! -d /etc/php_dynamodules_zend ]; then
201
	/bin/mkdir /etc/php_dynamodules_zend
202
fi
203
if [ ! -d /etc/php_dynamodules_zend_ts ]; then
204
	/bin/mkdir /etc/php_dynamodules_zend_ts
205
fi
206

    
207
# Read in dynamodules
208
if [ -d /etc/php_dynamodules ]; then
209
	DYNA_MODULES=`/bin/ls -Utr /etc/php_dynamodules/`
210
	PHPMODULES="$PHPMODULES $DYNA_MODULES"
211
fi
212

    
213
# Read in zend modules
214
if [ -d /etc/php_dynamodules_zend ]; then
215
	DYNA_MODULES=`/bin/ls /etc/php_dynamodules_zend/`
216
	PHP_ZEND_MODULES="$PHP_ZEND_MODULES $DYNA_MODULES"
217
fi
218

    
219
# Read in zend threaded modules
220
if [ -d /etc/php_dynamodules_zend_ts ]; then
221
	DYNA_MODULES=`/bin/ls /etc/php_dynamodules_zend_ts/`
222
	PHP_ZEND_MODULES_TS="$PHP_ZEND_MODULES_TS $DYNA_MODULES"
223
fi
224

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

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

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

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

    
265
	/bin/cat >>/usr/local/lib/php.ini <<EOF
266

    
267
; APC Settings
268
apc.enabled="1"
269
apc.enable_cli="0"
270
apc.shm_size="${APCSHMEMSIZE}"
271

    
272
EOF
273

    
274
else
275
	LOWMEM="TRUE"
276
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC."
277
	echo ">>> WARNING!  under 128 megabytes of ram detected.  Not enabling APC." | /usr/bin/logger -p daemon.info -i -t rc.php_ini_setup
278
fi
279

    
280
	/bin/cat >>/usr/local/lib/php.ini <<EOF
281

    
282
[suhosin]
283
suhosin.get.max_array_depth = 5000
284
suhosin.get.max_array_index_length = 256
285
suhosin.get.max_vars = 5000
286
suhosin.get.max_value_length = 500000
287
suhosin.post.max_array_depth = 5000
288
suhosin.post.max_array_index_length = 256
289
suhosin.post.max_vars = 5000
290
suhosin.post.max_value_length = 500000
291
suhosin.request.max_array_depth = 5000
292
suhosin.request.max_array_index_length = 256
293
suhosin.request.max_vars = 5000
294
suhosin.request.max_value_length = 500000
295
suhosin.memory_limit = 512435456
296

    
297
EOF
298

    
299

    
300
# Copy php.ini file to etc/ too (cli)
301
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
302

    
303
# Remove old log file if it exists.
304
if [ -f /var/run/php_modules_load_errors.txt ]; then
305
	/bin/rm /var/run/php_modules_load_errors.txt
306
fi 
307

    
308
for EXT in $PHPMODULES; do
309
	PHPMODULESLC="$PHPMODULESLC `echo "$EXT" | /usr/bin/tr '[:upper:]' '[:lower:]'`"
310
done
311

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

    
340
# Copy php.ini file to etc/ too (cli)
341
/bin/cp /usr/local/lib/php.ini /usr/local/etc/php.ini
(83-83/108)