From 527c0f3f0efd0e3703239b620333e9be203989b2 Mon Sep 17 00:00:00 2001
From: Alan Garcia <a.garcia@nnx.com>
Date: Sat, 29 Jan 2011 15:12:26 +0000
Subject: [PATCH] Conf web : CREATION ok

---
 install/upgrades/1.0.sql    |  1 +
 src/dns.sh                  |  4 ++--
 src/functions.sh            | 14 +++++++-------
 src/hosting_functions_v2.sh | 14 ++++++--------
 src/update_domains.sh       | 17 ++++++++++++++---
 5 files changed, 30 insertions(+), 20 deletions(-)

diff --git a/install/upgrades/1.0.sql b/install/upgrades/1.0.sql
index 52a0052e..0b1d1bbb 100644
--- a/install/upgrades/1.0.sql
+++ b/install/upgrades/1.0.sql
@@ -27,6 +27,7 @@ INSERT IGNORE INTO `domaines_type` (name, description, target, entry, compatibil
 ('cname', 'cname entry', 'DOMAIN', '%SUB% CNAME %TARGET%', 'txt',true, true),
 ('txt', 'txt entry', 'TXT', '%SUB% IN TXT "%TARGET%"','local,url,ip,webmail,ipv6,cname,txt',true, true),
 ('mx', 'mx entry', 'IP', '%SUB% IN MX %TARGET%', 'local,url,ip,webmail,ipv6,cname,txt',true, false)
+('panel', 'Panel redirection', 'NONE', '%SUB% IN A @@PUBLIC_IP@@', 'local,url,ip,webmail,ipv6,cname,txt',true, false)
 ;
 
 -- Changing stanby use
diff --git a/src/dns.sh b/src/dns.sh
index 4fb38da0..6774b0b5 100755
--- a/src/dns.sh
+++ b/src/dns.sh
@@ -93,12 +93,12 @@ dns_regenerate() {
     # Add the entry
     file=$(
         echo -e "$file"
-        $MYSQL_DO "select replace(replace(dt.entry,'%TARGET%',sd.valeur), '%SUB%', if(length(sd.sub)>0,sd.sub,'@')) from sub_domaines sd,domaines_type dt where sd.type=dt.name and sd.domaine='$domain';"|sort
+        $MYSQL_DO "select distinct replace(replace(dt.entry,'%TARGET%',sd.valeur), '%SUB%', if(length(sd.sub)>0,sd.sub,'@')) as entry from sub_domaines sd,domaines_type dt where sd.type=dt.name and sd.domaine='$domain' order by entry ;"
     )
 
 
     # Get some usefull vars
-    local mx=$( $MYSQL_DO "select mx from domaines where domaine='$domain';")
+    local mx=$( $MYSQL_DO "select mx from domaines where domaine='$domain' limit 1;")
 
     # Replace the vars by their values
     # Here we can add dynamic value for the default MX
diff --git a/src/functions.sh b/src/functions.sh
index 19541d72..aeef9a93 100755
--- a/src/functions.sh
+++ b/src/functions.sh
@@ -5,7 +5,7 @@
 . /etc/alternc/local.sh
 
 # Init some other vars
-MYSQL_DO="mysql --defaults-file=/etc/alternc/my.cnf -Bs -e "
+MYSQL_DO="/usr/bin/mysql --defaults-file=/etc/alternc/my.cnf -Bs -e "
 DOMAIN_LOG_FILE="/var/log/alternc/update_domains.log"
 
 # Some usefull miscellaneous shell functions
@@ -27,16 +27,16 @@ print_user_letter() {
 # imprime le nom d'usager associé au domaine
 get_account_by_domain() {
     # les admintools ne sont peut-être pas là
-    if [ -x "/usr/bin/get_account_by_domain" ]
-    then
-        # only first field, only first line
-        /usr/bin/get_account_by_domain "$1"|head -1|awk '{ print $1;}'
-    else
+#    if [ -x "/usr/bin/get_account_by_domain" ]
+#    then
+#        # only first field, only first line
+#        /usr/bin/get_account_by_domain "$1"|head -1|awk '{ print $1;}'
+#    else
         # implantons localement ce que nous avons besoin, puisque admintools
         # n'est pas là
         $MYSQL_DO 'SELECT a.login FROM membres a, sub_domaines b WHERE a.uid = b.compte AND \
         CONCAT(IF(sub="", "", CONCAT(sub, ".")), domaine) = "'"$1"'" LIMIT 1;'
-    fi
+#    fi
 }
 
 log_error() {
diff --git a/src/hosting_functions_v2.sh b/src/hosting_functions_v2.sh
index d374d0bd..eb7d175e 100644
--- a/src/hosting_functions_v2.sh
+++ b/src/hosting_functions_v2.sh
@@ -7,7 +7,6 @@ HOSTING_DIR="/usr/lib/alternc/hosting_functions"
 
 HTML_HOME="$ALTERNC_LOC/html"
 VHOST_DIR="$ALTERNC_LOC/apache-vhost"
-VHOST_FILE="$VHOST_DIR/vhosts_all.conf"
 
 
 host_create() {
@@ -41,17 +40,17 @@ host_create() {
 
     # First, usefull vars. Some may be empty or false, it's
     # OK, it will be solve in the "case" below
-    local USER=$2
-    local FQDN=$3
-    local REDIRECT=$4   # Yes, TARGET_DIR and REDIRECT are the same
-    local TARGET_DIR=$4 # It's used by different template
+    local FQDN=$2
+    local REDIRECT=$3   # Yes, TARGET_DIR and REDIRECT are the same
+    local TARGET_DIR=$3 # It's used by different template
+    local USER=$(get_account_by_domain $FQDN)
     local user_letter=`print_user_letter "$USER"`
     local DOCUMENT_ROOT="${HTML_HOME}/${user_letter}/${USER}/$TARGET_DIR"
     local FILE_TARGET="$VHOST_DIR/${user_letter}/$USER/$FQDN.conf"
 
-
     # In case VTYPE don't have the same name as the template file, 
     # here we can define it
+    local TEMPLATE=''
     case $VTYPE in
 #      "example")
 #        TEMPLATE="$TEMPLATE_DIR/an-example.conf"
@@ -114,7 +113,7 @@ host_change_enable() {
     fi
 
     local FQDN=$2
-    local USER=`get_account_by_domain $FQDN`
+    local USER=$(get_account_by_domain $FQDN)
     local user_letter=`print_user_letter "$USER"`
     local FENABLED="$VHOST_DIR/${user_letter}/$USER/$FQDN.conf"
     local FDISABLED="$FENABLED-disabled"
@@ -167,4 +166,3 @@ host_delete() {
     [ -w "$FDISABLED" ] && rm -f "$FDISABLED"
 }
 
-
diff --git a/src/update_domains.sh b/src/update_domains.sh
index 6af029b6..b50816f1 100644
--- a/src/update_domains.sh
+++ b/src/update_domains.sh
@@ -42,7 +42,7 @@ $MYSQL_DO "update sub_domaines sd, domaines d set sd.web_action = 'DELETE' where
 
 # Sub_domaines we want to delete
 # sub_domaines.web_action = delete
-for sub in $( $MYSQL_DO "select if (length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine) from sub_domaines sd where web_action ='DELETE';") ; do
+for sub in $( $MYSQL_DO "select if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine) from sub_domaines sd where web_action ='DELETE';") ; do
     echo $sub
     # TODO Do the conf
     # TODO Update the entry in the DB with the result and the action
@@ -50,8 +50,19 @@ done
 
 # Sub domaines we want to update
 # sub_domaines.web_action = update and sub_domains.only_dns = false
-# TODO do the conf
-# TODO Update the entry in the DB with the result and the action
+params=$( $MYSQL_DO "
+  select concat_ws('|µ',lower(sd.type), if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine), valeur) 
+  from sub_domaines sd, domaines_type dt
+  where sd.web_action ='UPDATE'
+  and lower(sd.type) = lower(dt.name)
+  and dt.only_dns = false
+  ;")
+for sub in $params;do
+    echo  host_create $(echo $sub|tr '|µ' ' ')
+    host_create $(echo $sub|tr '|µ' ' ')
+    # TODO Update the entry in the DB with the result and the action
+done
+unset IFS
 
 # Domains we do not want to be the DNS serveur anymore :
 # domaines.dns_action = UPDATE and domaines.gesdns = 0