
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
#!/bin/bash -x

if [ "${NATIVE_DA_HOOK_LOGFILE:-notset}" = notset ]; then
    # redirect output to /var/log/imunify360/native_da.hook_log
    export NATIVE_DA_HOOK_LOGFILE="/var/log/imunify360/native_da.hook_log.$(date +%F).$(date +%s)"
    exec >$NATIVE_DA_HOOK_LOGFILE 2>&1
fi

CMD="$1"
PHP_PATH="${2:-/usr/local/php*/bin/php}"

if [ -z "$PHP_PATH" -o -z "$CMD" ];then
  echo "use --install-i360 [path to php] or --remove-i360 [path to php bin]"
  exit
fi

if [ -f /etc/os-release ]; then
    source /etc/os-release
fi

if [ "$ID" = "debian" -o "$ID_LIKE" = "debian" ]; then
    PHPD=php.d.all
else
    PHPD=php.d.i360
fi

MACHINE_TYPE=`uname -m`
if [ ${MACHINE_TYPE} == 'x86_64' ]; then
    if [ "$ID" = "debian" -o "$ID_LIKE" = "debian" ]; then
        LIBS="/x86_64-linux-gnu"
    else
        LIBS="64"
    fi
else
    LIBS=""
fi


function install_i360()
{
            if [ -e $1 ]; then
                VERS=$($1 --version | head -n1 | cut -d' ' -f2 | cut -c 1,3)
                # Try to get version if php output is broken
                if [ -n "${VERS}" ]; then
                    VERS=$($1 --version | grep -m 1 -oP 'PHP [0-9]+\.[0-9]+\.[0-9]+ \(cli\)' | head -n1 | cut -d' ' -f2 | cut -c 1,3)
                fi
                EXT_DIR=$($1 -i | grep -e "^extension_dir" | cut -d"=" -f2 | tr ">" " " | sed 's/^ *//;s/ *$//')

                # clean up older i360.so that might get stuck on the previous rules db version (DEF-22610)
                PHPDIR=$(echo $1 | sed -E 's/php([0-9][0-9]).*/php\1/')
                [[ "$PHPDIR" =~ .*/php[0-9][0-9] ]] && find $PHPDIR -name i360.so -exec rm -v '{}' \;

                if [ -n "$EXT_DIR" -a \
                     -e "$EXT_DIR" -a \
                     -e /opt/alt/php$VERS/usr/lib$LIBS/php/modules/i360.so -a \
                     "/opt/alt/php$VERS/usr/lib$LIBS/php/modules/i360.so" != "$EXT_DIR/i360.so"  ]
                then
                    rm -f $EXT_DIR/i360.so
                    cp /opt/alt/php$VERS/usr/lib$LIBS/php/modules/i360.so $EXT_DIR
                    if [ -e /usr/sbin/cagefsctl -a -e "$EXT_DIR/i360.so" ]; then
                        echo "$EXT_DIR/i360.so" | /usr/sbin/cagefsctl --update-list
                    fi
                fi
                INI_PATH=$($1 -i | grep "Scan this dir for additional .ini files" | cut -d"=" -f2 | tr ">" " " | sed 's/^ *//;s/ *$//')
                if [  -e "$INI_PATH" -a \
                      -e /opt/alt/php$VERS/etc/$PHPD/i360.ini -a \
                      "/opt/alt/php$VERS/etc/$PHPD/i360.ini" != "$INI_PATH/i360.ini" ]
                then
                    cp /opt/alt/php$VERS/etc/$PHPD/i360.ini $INI_PATH/
                fi
            fi
}


function remove_i360()
{
            if [ -e $1 ]; then
                VERS=$($1 --version | head -n1 | cut -d' ' -f2 | cut -c 1,3)
                if [ -n "${VERS}" ]; then
                    VERS=$($1 --version | grep -m 1 -oP 'PHP [0-9]+\.[0-9]+\.[0-9]+ \(cli\)' | head -n1 | cut -d' ' -f2 | cut -c 1,3)
                fi
                MACHINE_TYPE=`uname -m`
                INI_PATH=$($1 -i | grep "Scan this dir for additional .ini files" | cut -d"=" -f2 | tr ">" " " | sed 's/^ *//;s/ *$//')
                if [ -e $INI_PATH/i360.ini -a \
                     "/opt/alt/php$VERS/etc/$PHPD/i360.ini" != "$INI_PATH/i360.ini" ]; then
                    rm -f ${INI_PATH}/i360.ini
                fi
                EXT_DIR=$($1 -i | grep -e "^extension_dir" | cut -d"=" -f2 | tr ">" " " | sed 's/^ *//;s/ *$//')
                if [ -n "$EXT_DIR" -a -e "$EXT_DIR" -a \
                     "/opt/alt/php$VERS/usr/lib$LIBS/php/modules/i360.so" != "$EXT_DIR/i360.so" ]; then
                    rm -f ${EXT_DIR}/i360.so
                fi
            fi
}


if [ "$CMD" == "--install-i360" ]; then
    for path in $PHP_PATH
    do install_i360 $path
    done
elif [ "$CMD" == "--remove-i360" ]; then
    for path in $PHP_PATH
    do remove_i360 $path
    done
fi
exit
