2011-01-28 15:55:26 +00:00
|
|
|
#!/bin/bash
|
2007-09-09 18:18:19 +00:00
|
|
|
|
2012-08-26 17:20:10 +00:00
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# AlternC - Web Hosting System
|
|
|
|
# Copyright (C) 2000-2012 by the AlternC Development Team.
|
|
|
|
# https://alternc.org/
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# LICENSE
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License (GPL)
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# To read the license please visit http://www.gnu.org/copyleft/gpl.html
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# Purpose of file: Main functions used for any bash script for AlternC.
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Init some vars
|
2011-01-28 15:55:26 +00:00
|
|
|
. /etc/alternc/local.sh
|
|
|
|
|
2012-08-26 17:20:10 +00:00
|
|
|
|
2011-01-28 15:55:26 +00:00
|
|
|
# Init some other vars
|
2011-01-29 15:12:26 +00:00
|
|
|
MYSQL_DO="/usr/bin/mysql --defaults-file=/etc/alternc/my.cnf -Bs -e "
|
2011-02-01 22:44:18 +00:00
|
|
|
mysql_query() { /usr/bin/mysql --defaults-file=/etc/alternc/my.cnf -Bs -e "$@" ; }
|
2011-01-28 15:55:26 +00:00
|
|
|
DOMAIN_LOG_FILE="/var/log/alternc/update_domains.log"
|
2011-01-29 15:46:33 +00:00
|
|
|
VHOST_FILE="$VHOST_DIR/vhosts_all.conf"
|
2012-10-15 14:20:38 +00:00
|
|
|
VHOST_MANUALCONF="$VHOST_DIR/manual/"
|
2013-09-12 09:28:31 +00:00
|
|
|
LOCK_JOBS="/var/run/alternc/jobs-lock"
|
2011-01-28 15:55:26 +00:00
|
|
|
|
2012-08-26 17:20:10 +00:00
|
|
|
|
|
|
|
# Some useful miscellaneous shell functions
|
2007-09-09 18:21:57 +00:00
|
|
|
print_domain_letter() {
|
2012-04-20 16:52:16 +00:00
|
|
|
local domain=$1
|
|
|
|
domain=${domain/.${domain/*./}/}
|
|
|
|
domain=${domain/*./}
|
|
|
|
domain=${domain:0:1}
|
2012-08-26 17:20:10 +00:00
|
|
|
# Bash match a 'é' when we give him [a-z]. Strange
|
2012-04-20 16:52:16 +00:00
|
|
|
if [[ "$domain" =~ [ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0-9]{1} ]]; then
|
|
|
|
echo $domain
|
|
|
|
else
|
|
|
|
echo '_'
|
|
|
|
fi
|
2007-09-09 18:21:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 14:56:47 +00:00
|
|
|
get_uid_by_path() {
|
|
|
|
local path="$1"
|
|
|
|
local sizepath=${#path}
|
|
|
|
local lastcar=${ALTERNC_HTML: -1}
|
|
|
|
|
|
|
|
if [ "$lastcar" != "/" ]
|
|
|
|
then
|
|
|
|
ALTERNC_HTML=$ALTERNC_HTML"/"
|
|
|
|
fi
|
|
|
|
|
|
|
|
local sizebasepath=${#ALTERNC_HTML}
|
|
|
|
local basepath=${path:0:($sizebasepath +2)}
|
|
|
|
local uid=`ls -n $basepath | head -n 2|tail -n 1| awk '{print $3}'`
|
|
|
|
echo $uid
|
|
|
|
}
|
|
|
|
|
2013-02-18 13:11:25 +00:00
|
|
|
# Return the html path for a account name
|
|
|
|
get_html_path_by_name() {
|
|
|
|
local name="$1"
|
2013-10-17 14:13:26 +00:00
|
|
|
if [[ ! "$name" =~ ^([a-z0-9]+)$ ]] ; then
|
2013-02-18 13:18:21 +00:00
|
|
|
# Error on error flux
|
|
|
|
echo "Account name is incorrect." >&2
|
2013-02-18 13:11:25 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
echo "$ALTERNC_HTML/${name:0:1}/$name"
|
|
|
|
}
|
2012-08-26 17:20:10 +00:00
|
|
|
|
|
|
|
# echoes the first letter of an alternc account name.
|
2007-09-09 18:21:57 +00:00
|
|
|
print_user_letter() {
|
|
|
|
local user="$1"
|
2012-02-06 08:28:40 +00:00
|
|
|
echo ${user:0:1}
|
2007-09-09 18:21:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-26 17:20:10 +00:00
|
|
|
|
|
|
|
# return the uid of an alternc account
|
2011-05-17 16:36:01 +00:00
|
|
|
get_uid_by_name() {
|
|
|
|
mysql_query 'SELECT uid FROM membres WHERE login="'"$1"'" LIMIT 1;'
|
|
|
|
}
|
|
|
|
|
2013-07-22 16:37:44 +00:00
|
|
|
get_variable_from_db() {
|
|
|
|
mysql_query 'SELECT value FROM membres WHERE name="'"$1"'" LIMIT 1;'
|
|
|
|
}
|
|
|
|
|
2012-08-26 17:20:10 +00:00
|
|
|
|
2007-09-09 18:18:19 +00:00
|
|
|
# imprime le nom d'usager associé au domaine
|
|
|
|
get_account_by_domain() {
|
2011-02-01 22:44:18 +00:00
|
|
|
mysql_query 'SELECT a.login FROM membres a, sub_domaines b WHERE a.uid = b.compte AND \
|
2011-01-28 15:55:26 +00:00
|
|
|
CONCAT(IF(sub="", "", CONCAT(sub, ".")), domaine) = "'"$1"'" LIMIT 1;'
|
2007-09-09 18:18:19 +00:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:20:38 +00:00
|
|
|
get_uid_by_domain() {
|
|
|
|
mysql_query 'SELECT b.compte as uid FROM sub_domaines b WHERE \
|
|
|
|
CONCAT(IF(sub="", "", CONCAT(sub, ".")), domaine) = "'"$1"'" LIMIT 1;'
|
|
|
|
}
|
2012-08-26 17:20:10 +00:00
|
|
|
|
|
|
|
# Log (echoes+log) an error and exit the current script with an error.
|
2011-01-28 15:55:26 +00:00
|
|
|
log_error() {
|
|
|
|
local error=$1
|
|
|
|
echo "`date` $0 : $1" | tee -a "$DOMAIN_LOG_FILE" >&2
|
|
|
|
exit 1
|
2007-09-09 18:18:19 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 14:41:55 +00:00
|
|
|
generate_string() {
|
|
|
|
local size=$1
|
|
|
|
if [ -z "$size" ] ; then
|
|
|
|
size=20
|
|
|
|
fi
|
|
|
|
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-$size}
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
2013-09-12 09:28:31 +00:00
|
|
|
lock_jobs() {
|
|
|
|
test -d "$(dirname "$LOCK_JOBS")" || mkdir -p "$(dirname "$LOCK_JOBS")"
|
|
|
|
touch "$LOCK_JOBS"
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock_jobs() {
|
|
|
|
test -e "$LOCK_JOBS" && rm -f "$LOCK_JOBS"
|
|
|
|
}
|
|
|
|
|
|
|
|
are_jobs_locked() {
|
|
|
|
return $(test -e "$LOCK_JOBS")
|
|
|
|
}
|
|
|
|
|
|
|
|
stop_if_jobs_locked() {
|
|
|
|
are_jobs_locked || return
|
|
|
|
echo "There is a file $LOCK_JOBS"
|
|
|
|
echo "So no jobs are allowed, not even for $0"
|
|
|
|
echo "Did you launch alternc.install ?"
|
|
|
|
exit 42
|
|
|
|
}
|