|
| 1 | +#!/bin/bash |
| 2 | +# info: add composer (php dependency manager) for a user |
| 3 | +# options: USER |
| 4 | +# |
| 5 | +# The function adds support for composer (php dependency manager) |
| 6 | +# Homepage: https://getcomposer.org/ |
| 7 | + |
| 8 | + |
| 9 | +#----------------------------------------------------------# |
| 10 | +# Variable&Function # |
| 11 | +#----------------------------------------------------------# |
| 12 | + |
| 13 | +# Argument definition |
| 14 | +user=$1 |
| 15 | + |
| 16 | +if [ -z "$HESTIA" ]; then |
| 17 | + HESTIA="/usr/local/hestia" |
| 18 | +fi |
| 19 | +source $HESTIA/func/main.sh |
| 20 | + |
| 21 | + |
| 22 | +#----------------------------------------------------------# |
| 23 | +# Verifications # |
| 24 | +#----------------------------------------------------------# |
| 25 | + |
| 26 | +check_args '1' "$#" 'USER' |
| 27 | +is_format_valid 'user' |
| 28 | +is_object_valid 'user' 'USER' "$user" |
| 29 | +is_object_unsuspended 'user' 'USER' "$user" |
| 30 | + |
| 31 | +[ -z "$HOMEDIR" ] && check_result $E_NOTEXIST "Hestia environment vars not present" |
| 32 | + |
| 33 | +# Perform verification if read-only mode is enabled |
| 34 | +check_hestia_demo_mode |
| 35 | + |
| 36 | + |
| 37 | +#----------------------------------------------------------# |
| 38 | +# Action # |
| 39 | +#----------------------------------------------------------# |
| 40 | + |
| 41 | +COMPOSER_DIR="$HOMEDIR/$user/.composer" |
| 42 | +COMPOSER_BIN="$COMPOSER_DIR/composer" |
| 43 | + |
| 44 | +if [ -f "$COMPOSER_BIN" ]; then |
| 45 | + echo "Composer already available" |
| 46 | + exit |
| 47 | +fi |
| 48 | + |
| 49 | +mkdir -p "$COMPOSER_DIR" |
| 50 | +chown $user: "$COMPOSER_DIR" |
| 51 | + |
| 52 | +COMPOSER_SETUP_FILE=$(mktemp) |
| 53 | +check_result $? "Create temp file" |
| 54 | +chown $user: "$COMPOSER_SETUP_FILE" |
| 55 | + |
| 56 | +signature="$(curl --silent --show-error https://composer.github.io/installer.sig)" |
| 57 | +check_result $? "Download signature" |
| 58 | + |
| 59 | +user_exec wget --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache https://getcomposer.org/installer --quiet -O "$COMPOSER_SETUP_FILE" |
| 60 | +check_result $? "Download composer installer" |
| 61 | + |
| 62 | +if [[ "$signature" != $(sha384sum $COMPOSER_SETUP_FILE | cut -f 1 -d " ") ]]; then |
| 63 | + rm -f "$COMPOSER_SETUP_FILE" |
| 64 | + check_result $E_INVALID "Composer signature does not match" |
| 65 | +fi |
| 66 | + |
| 67 | +COMPOSER_HOME="$HOMEDIR/$user/.config/composer" user_exec /usr/bin/php "$COMPOSER_SETUP_FILE" --quiet --install-dir="$COMPOSER_DIR" --filename=composer |
| 68 | +check_result $? "Composer install failed" |
| 69 | + |
| 70 | +[ -f "$COMPOSER_SETUP_FILE" ] && rm -f "$COMPOSER_SETUP_FILE" |
| 71 | + |
| 72 | + |
| 73 | +# Logging |
| 74 | +log_history "Enabled composer for user $user" |
| 75 | +log_event "$OK" "$ARGUMENTS" |
| 76 | + |
| 77 | +exit |
0 commit comments