summaryrefslogtreecommitdiff
blob: 0eb4ae455961f382779d2e555b124c9c1004ef1b (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# Copyright 2010-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

inherit config multilib

DESCRIPTION="Manage php installations"
MAINTAINER="php-bugs@gentoo.org"

MODULES="cli apache2 fpm cgi phpdbg"


# Most of the SAPIs (apache2 excluded) provide executables that we
# symlink to a predictable location. Given a SAPI name, we output
# that location.
#
# For example, the "cgi" SAPI has its active symlink at /usr/bin/php-cgi.
#
# Note that the "cli" SAPI actually provides three binaries -- we
# return the path for only one. This is an API wart, not by design.
#
# INPUT:
#
# The name of a SAPI that provides an executable.
#
# OUTPUT:
#
# The path of the symlink for the executable provided by the active
# version of the given SAPI. An error is raised if the given SAPI
# does not provide an executable.
#
sapi_active_bin_link_path() {
	local sapi="${1}"
	local bin_dir="${EROOT}/usr/bin/"
	case "${sapi}" in
		cli) echo "${bin_dir}/php" ;;
		fpm) echo "${bin_dir}/php-fpm" ;;
		cgi) echo "${bin_dir}/php-cgi" ;;
		dbg) echo "${bin_dir}/phpdbg" ;;
        *)   die "SAPI ${sapi} does not provide an executable" ;;
	esac
}


# Parse and return the major version from a target name. For example,
# the "php5.6" target has a major version of "5".
#
# INPUT:
#
# The name of a valid PHP target, like php5.6 or php7.0.
#
# OUTPUT:
#
# A major version number. An error is raised if the given target is
# not valid.
#
parse_target_major_version() {
	local target="${1}"
	local major="${target:3:1}"
	case "${major}" in
		5|7) echo "${major}" ;;
		*)   die "invalid PHP target name: ${target}"
	esac
}

cleanup_sapis() {
	local m
	local link
	for m in $MODULES ; do
		cleanup_sapi $m
	done
}

cleanup_sapi() {
	local l=${1}_link
	local link=${!l}
	if [[ -L $link && ! -e $link ]] ; then
		echo -n "Broken link for $1"
		if update_sapi $1 ; then
			echo ", updated version to $(get_active_$1)"
			return
		else
			rm $link || die "failed to remove ${link}"

			return
		fi
	fi

	if [[ "${1}" == "apache2" ]]; then
		rm -f "${EROOT}$(get_active_libdir)/apache2/modules/libphp[57].so" \
		   || die "failed to remove old libphp.so symlink"
	fi

	return 1
}

update_sapi() {
	local target=$(find_targets_$1 | tail -n 1)
	local current=$(get_active_$1)
	[[ -z $target ]] && return 1
	[[ $current = $target ]] && return 1
	set_$1 $target
}

get_libdirs() {
	local dir libdirs
	for dir in $(list_libdirs); do
		[[ -L ${EROOT}/usr/${dir} ]] && continue
		ls "${EROOT}"/usr/${dir}/php*.* > /dev/null 2>&1 || continue

		libdirs+=' '/usr/${dir}
	done
	echo ${libdirs:-/usr/lib}
}

get_active_libdir() {
	local dir
	for dir in $(get_libdirs); do
		echo ${dir}
		return
	done
	echo /usr/lib
}

find_targets() {
	local dir dirs libdir
	for libdir in $(get_libdirs); do
		for dir in "${EROOT}"${libdir}/php*.*; do
			t=$(basename $dir)
			has $t $dirs || dirs="${dirs} $t"
		done
	done
  echo $dirs
}

# List all valid apache2 targets. The list is obtained by searching
# for libphp*.so in locations determined by find_targets(). This list
# should therefore be a subset of find_targets().
#
# INPUT:
#
# None.
#
# OUTPUT:
#
# The "display name" of every available apache PHP module, one per line.
# For example,
#
#   php5.6
#   php7.0
#
find_targets_apache2() {
	local libs target libdir
	for target in $(find_targets); do
		for libdir in $(get_libdirs); do
			libs="${EROOT}${libdir}/${target}/apache2/libphp[57].so"
			for lib in $libs; do
				[[ -f "${lib}" ]] && echo $target
			done
		done
	done | @SORT@ | @UNIQ@
}

find_targets_cli() {
	local target libdir
	for target in $(find_targets); do
		for libdir in $(get_libdirs); do
			[[ -f ${EROOT}${libdir}/$target/bin/php ]] && echo $target
		done
	done | @SORT@ | @UNIQ@
}

find_targets_fpm() {
	local target libdir
	for target in $(find_targets); do
		for libdir in $(get_libdirs); do
			[[ -f ${EROOT}${libdir}/$target/bin/php-fpm ]] && echo $target
		done
	done | @SORT@ | @UNIQ@
}

find_targets_cgi() {
	local target libdir
	for target in $(find_targets); do
		for libdir in $(get_libdirs); do
			[[ -f ${EROOT}${libdir}/$target/bin/php-cgi ]] && echo $target
		done
	done | @SORT@ | @UNIQ@
}

find_targets_phpdbg() {
	local target libdir
	for target in $(find_targets); do
		for libdir in $(get_libdirs); do
			[[ -f ${EROOT}${libdir}/$target/bin/phpdbg ]] && echo $target
		done
	done | @SORT@ | @UNIQ@
}

get_active_cli() {
	# See get_active_apache2() for an explanation of the sed call.
	local target=$(canonicalise "$(sapi_active_bin_link_path cli)")
	local ver="s:.*/usr/.*/\(php[0-9]\.[0-9][0-9]*\)/bin/php:\1:p"
	[[ -a "${target}" ]] &&	echo "${target}" | @SED@ -ne "${ver}"
}

get_active_cgi() {
	# See get_active_apache2() for an explanation of the sed call.
	local target=$(canonicalise "$(sapi_active_bin_link_path cgi)")
	local ver="s:.*/usr/.*/\(php[0-9]\.[0-9][0-9]*\)/bin/php-cgi:\1:p"
	[[ -a "${target}" ]] &&	echo "${target}" | @SED@ -ne "${ver}"
}

get_active_fpm() {
	# See get_active_apache2() for an explanation of the sed call.
	local target=$(canonicalise "$(sapi_active_bin_link_path fpm)")
	local ver="s:.*/usr/.*/\(php[0-9]\.[0-9][0-9]*\)/bin/php-fpm:\1:p"
	[[ -a "${target}" ]] &&	echo "${target}" | @SED@ -ne "${ver}"
}

get_active_phpdbg() {
	# See get_active_apache2() for an explanation of the sed call.
	local target=$(canonicalise "$(sapi_active_bin_link_path dbg)")
	local ver="s:.*/usr/.*/\(php[0-9]\.[0-9][0-9]*\)/bin/phpdbg:\1:p"
	[[ -a "${target}" ]] &&	echo "${target}" | @SED@ -ne "${ver}"
}

# The path to the active version of the apache2 module, which should
# be a symlink. This is the path used by our apache configuration to
# load the PHP module. The path is unversioned (that is, it has no "5"
# or "7" in it) so that the apache configuration does not need to
# change after the user eselects a different version.
#
# INPUT:
#
# None.
#
# OUTPUT:
#
# The path to our mod_php.so symlink, which should (but is not
# guaranteed to) point to a real apache DSO.
#
get_apache2_active_symlink_path() {
	echo "${EROOT}$(get_active_libdir)/apache2/modules/mod_php.so"
}

# Find the active (selected) version of the apache2 module. Used to
# decorate the output of the `eselect php list apache2` command.
#
# INPUT:
#
# None.
#
# OUTPUT:
#
# The "display name" of the active apache2 module. For example,
# "php5.6" or "php7.0".
#
get_active_apache2() {
	local active_symlink target ver

	# The symlink to our active module.
	active_symlink="$(get_apache2_active_symlink_path)"

	# This sed expression finds the "display name" of the PHP version
	# corresponding to a copy of libphp. For example, it parses the
	# string "php5.6" out of "/usr/lib64/php5.6/apache2/libphp5.so".
	ver="s:.*/usr/.*/\(php[0-9]\.[0-9]\)/apache2/libphp[57].so:\1:p"

	if [[ -L "${active_symlink}" ]] ; then
		target=$(canonicalise "${active_symlink}")
		if [[ -a "${target}" ]] ; then
			echo "${target}" | @SED@ -ne "${ver}"
		fi
	fi
}

# Write an apache configuration file to load the active version of
# mod_php. The 5.x and 7.x series (at least...) have different module
# names, and so require a different apache configuration when
# switching between the two.
#
# INPUT:
#
# The name of the target (php5.6, php7.0) for which to write the
# configuration file.
#
# OUTPUT:
#
# None.
#
write_mod_php_conf() {
	local target="${1}"
	local conf_dir="${EROOT}"/var/lib/eselect-php
	local conf_path="${conf_dir}/mod_php.conf"

	@MKDIR_P@ "${conf_dir}" || die "failed to create ${conf_dir}"

	local major=$(parse_target_major_version "${target}")
	cat <<-EOF > "${conf_path}" || die "failed to write mod_php.conf"
	<IfModule !php${major}_module>
	    LoadModule php${major}_module modules/mod_php.so
	</IfModule>
	EOF
}

resolv_target() {
	local targets=( $(find_targets_$1) )
	if is_number $2; then
		if [[ $2 -le ${#targets[@]} && $2 -gt 0 ]] ; then
			echo "${targets[ $(( $2 - 1 )) ]}"
		fi
	elif has $2 ${targets[@]}; then
	  echo $2
	fi
}

check_module() {
	has $1 $(echo $MODULES) || \
		die -q "Please choose one of the following modules: ${MODULES}"
}

## Actual actions

list_apache2() {
	local targets
	local a
	targets=( $(find_targets_apache2) )
	a=$(get_active_apache2)
	for (( i = 0; i < ${#targets[@]}; i++ )) ; do
		if [[ $a == ${targets[i]} ]] ; then
			targets[i]=$(highlight_marker "${targets[i]}")
		fi
	done
	write_numbered_list -m "(none found)" "${targets[@]}"
}

list_cli() {
	local targets
	local a
	targets=( $(find_targets_cli) )
	a=$(get_active_cli)
	for (( i = 0; i < ${#targets[@]}; i++ )) ; do
		if [[ $a == ${targets[i]} ]] ; then
			targets[i]=$(highlight_marker "${targets[i]}")
		fi
	done
	write_numbered_list -m "(none found)" "${targets[@]}"
}

list_cgi() {
	local targets
	local a
	targets=( $(find_targets_cgi) )
	a=$(get_active_cgi)
	for (( i = 0; i < ${#targets[@]}; i++ )) ; do
		if [[ $a == ${targets[i]} ]] ; then
			targets[i]=$(highlight_marker "${targets[i]}")
		fi
	done
	write_numbered_list -m "(none found)" "${targets[@]}"
}

list_fpm() {
	local targets
	local a
	targets=( $(find_targets_fpm) )
	a=$(get_active_fpm)
	for (( i = 0; i < ${#targets[@]}; i++ )) ; do
		if [[ $a == ${targets[i]} ]] ; then
			targets[i]=$(highlight_marker "${targets[i]}")
		fi
	done
	write_numbered_list -m "(none found)" "${targets[@]}"
}

list_phpdbg() {
	local targets
	local a
	targets=( $(find_targets_phpdbg) )
	a=$(get_active_phpdbg)
	for (( i = 0; i < ${#targets[@]}; i++ )) ; do
		if [[ $a == ${targets[i]} ]] ; then
			targets[i]=$(highlight_marker "${targets[i]}")
		fi
	done
	write_numbered_list -m "(none found)" "${targets[@]}"
}

set_apache2() {
	local active_symlink libdir major target=$(resolv_target apache2 $1)
	active_symlink="$(get_apache2_active_symlink_path)"
	major=$(parse_target_major_version "${target}")

	[[ -z $target ]] && die -q "invalid target"

	for libdir in $(get_libdirs); do
		rm --force "${active_symlink}" || \
			die "failed to remove active module symlink"

		@LN_S@ --force "../../${target}/apache2/libphp${major}.so" \
			"${active_symlink}" || \
			die -q "failed to create active mod_php symlink"
	done

	write_mod_php_conf "${target}"
	echo "Please restart apache for the changes to take effect."
}

set_cli() {
	local file libdir t=$(resolv_target cli $1)
	[[ -z $t ]] && die -q "invalid target"
	for file in php phpize php-config; do
		@LN_S@ --force "../..$(get_active_libdir)/${t}/bin/${file}" \
			"${EROOT}/usr/bin/${file}" || \
			die -q "failed to create active ${file} symlink"
	done
}

set_cgi() {
	t=$(resolv_target cgi $1)
	[[ -z $t ]] && die -q "invalid target"
	@LN_S@ --force "../..$(get_active_libdir)/${t}/bin/php-cgi" \
		"$(sapi_active_bin_link_path cgi)" || \
		die -q "failed to create active php-cgi symlink"
}

set_phpdbg() {
	t=$(resolv_target phpdbg $1)
	[[ -z $t ]] && die -q "invalid target"
	@LN_S@ --force "../..$(get_active_libdir)/${t}/bin/phpdbg" \
		"$(sapi_active_bin_link_path dbg)" || \
		die -q "failed to create active phpdbg symlink"
}

set_fpm() {
	local t=$(resolv_target fpm $1)
	[[ -z $t ]] && die -q "invalid target"
	@LN_S@ --force "../..$(get_active_libdir)/${t}/bin/php-fpm" \
		"$(sapi_active_bin_link_path fpm)" || \
		die -q "failed to create symlink for the php-fpm binary"
	echo "Please restart php-fpm for the changes to take effect."
}
## set action

describe_set() {
	echo "Sets the current configuration for a module"
}

describe_set_parameters() {
	echo "<module> <target>"
}

describe_set_options() {
	echo "module:	one of ${MODULES}"
	echo "target:	Target name or number (from the 'list' action)"
}


do_set() {
	check_module $1
	set_$1 $2
}



## List action

describe_list() {
	echo "Lists available php installs for a module"
}

describe_list_parameters() {
	echo "<module>"
}

describe_list_options() {
	echo "module: one of ${MODULES}"
}

do_list() {
	check_module $1
	list_$1
}

## Show action

describe_show() {
	echo "Lists available php installs for a module"
}

describe_show_parameters() {
	echo "<module>"
}

describe_show_options() {
	echo "module: one of ${MODULES}"
}

do_show() {
	check_module $1
	get_active_$1
}

## update action

describe_update() {
	echo "Automatically update the php versions"
}

describe_update_parameters() {
	echo "<module> [ifunset]"
}

describe_update_options() {
	echo "module: one of ${MODULES}"
	echo "ifunset : Do not override existing implementation"
}

do_update() {
    check_module $1
	[[ -z ${2} || ( -z ${3} && ( ${2} == ifunset || ${2} == '--if-unset' ) ) ]] || \
		die -q "Usage error"

	if [[ (${2} == ifunset || ${2} == '--if-unset') && -n $(get_active_$1) ]];
	then
		return
	fi

    update_sapi $1 || echo "Nothing to update"
}

## cleanup action

describe_cleanup() {
	echo "Automatically clean up stale links"
}

describe_cleanup_parameters() {
	echo
}

describe_cleanup_options() {
	echo
}

do_cleanup() {
    cleanup_sapis
}