From 75eae394f14353faf64cec1f5d71f5ca7e532461 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Fri, 11 Sep 2015 20:36:45 +0200
Subject: [PATCH] Fix confirmation of scripts make scripts not working with
 cron

---
 .../company/sync_contacts_dolibarr2ldap.php   | 35 ++++++++++++-------
 .../members/sync_members_dolibarr2ldap.php    | 25 ++++++++-----
 .../members/sync_members_ldap2dolibarr.php    | 13 ++++---
 scripts/user/sync_groups_ldap2dolibarr.php    | 12 ++++---
 scripts/user/sync_users_ldap2dolibarr.php     | 13 ++++---
 5 files changed, 64 insertions(+), 34 deletions(-)

diff --git a/scripts/company/sync_contacts_dolibarr2ldap.php b/scripts/company/sync_contacts_dolibarr2ldap.php
index 8a582a77f74..2223cd1ca8f 100755
--- a/scripts/company/sync_contacts_dolibarr2ldap.php
+++ b/scripts/company/sync_contacts_dolibarr2ldap.php
@@ -34,12 +34,6 @@ if (substr($sapi_type, 0, 3) == 'cgi') {
 	exit(-1);
 }
 
-if (! isset($argv[1]) || ! $argv[1]) {
-    print "Usage: $script_file now\n";
-	exit(-1);
-}
-$now=$argv[1];
-
 
 require_once($path."../../htdocs/master.inc.php");
 require_once(DOL_DOCUMENT_ROOT."/contact/class/contact.class.php");
@@ -49,6 +43,7 @@ require_once(DOL_DOCUMENT_ROOT."/core/class/ldap.class.php");
 // Global variables
 $version=DOL_VERSION;
 $error=0;
+$confirmed=0;
 
 
 /*
@@ -59,6 +54,18 @@ $error=0;
 print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
 dol_syslog($script_file." launched with arg ".join(',',$argv));
 
+if (! isset($argv[1]) || ! $argv[1]) {
+    print "Usage: $script_file now [-y]\n";
+	exit(-1);
+}
+
+foreach($argv as $key => $val)
+{
+	if (preg_match('/-y$/',$val,$reg)) $confirmed=1;
+}
+
+$now=$argv[1];
+
 print "Mails sending disabled (useless in batch mode)\n";
 $conf->global->MAIN_DISABLE_ALL_MAILS=1;	// On bloque les mails
 print "\n";
@@ -77,12 +84,16 @@ print "login=".$conf->global->LDAP_ADMIN_DN."\n";
 print "pass=".preg_replace('/./i','*',$conf->global->LDAP_ADMIN_PASS)."\n";
 print "DN target=".$conf->global->LDAP_CONTACT_DN."\n";
 print "\n";
-print "Press a key to confirm...\n";
-$input = trim(fgets(STDIN));
-print "Warning, this operation may result in data loss if it failed.\n";
-print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
-print "Hit Enter to continue or CTRL+C to stop...\n";
-$input = trim(fgets(STDIN));
+
+if (! $confirmed)
+{
+	print "Press a key to confirm...\n";
+	$input = trim(fgets(STDIN));
+	print "Warning, this operation may result in data loss if it failed.\n";
+	print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
+	print "Hit Enter to continue or CTRL+C to stop...\n";
+	$input = trim(fgets(STDIN));
+}
 
 /*
 if (! $conf->global->LDAP_CONTACT_ACTIVE)
diff --git a/scripts/members/sync_members_dolibarr2ldap.php b/scripts/members/sync_members_dolibarr2ldap.php
index 13529561a80..bfe16330b79 100755
--- a/scripts/members/sync_members_dolibarr2ldap.php
+++ b/scripts/members/sync_members_dolibarr2ldap.php
@@ -43,7 +43,7 @@ $langs->load("main");
 // Global variables
 $version=DOL_VERSION;
 $error=0;
-
+$confirmed=0;
 
 
 /*
@@ -55,9 +55,15 @@ print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
 dol_syslog($script_file." launched with arg ".join(',',$argv));
 
 if (! isset($argv[1]) || ! $argv[1]) {
-    print "Usage: $script_file now\n";
+    print "Usage: $script_file now [-y]\n";
 	exit(-1);
 }
+
+foreach($argv as $key => $val)
+{
+	if (preg_match('/-y$/',$val,$reg)) $confirmed=1;
+}
+
 $now=$argv[1];
 
 print "Mails sending disabled (useless in batch mode)\n";
@@ -78,13 +84,16 @@ print "login=".$conf->global->LDAP_ADMIN_DN."\n";
 print "pass=".preg_replace('/./i','*',$conf->global->LDAP_ADMIN_PASS)."\n";
 print "DN target=".$conf->global->LDAP_MEMBER_DN."\n";
 print "\n";
-print "Press a key to confirm...\n";
-$input = trim(fgets(STDIN));
-print "Warning, this operation may result in data loss if it failed.\n";
-print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
 
-print "Hit Enter to continue or CTRL+C to stop...\n";
-$input = trim(fgets(STDIN));
+if (! $confirmed)
+{
+	print "Press a key to confirm...\n";
+	$input = trim(fgets(STDIN));
+	print "Warning, this operation may result in data loss if it failed.\n";
+	print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
+	print "Hit Enter to continue or CTRL+C to stop...\n";
+	$input = trim(fgets(STDIN));
+}
 
 /*
 if (! $conf->global->LDAP_MEMBER_ACTIVE)
diff --git a/scripts/members/sync_members_ldap2dolibarr.php b/scripts/members/sync_members_ldap2dolibarr.php
index 4ed5e36f696..cf18a75e508 100755
--- a/scripts/members/sync_members_ldap2dolibarr.php
+++ b/scripts/members/sync_members_ldap2dolibarr.php
@@ -48,7 +48,7 @@ $langs->load("errors");
 $version=DOL_VERSION;
 $error=0;
 $forcecommit=0;
-
+$confirmed=0;
 
 
 /*
@@ -94,7 +94,7 @@ $required_fields=array_unique(array_values(array_filter($required_fields, "dolVa
 
 
 if (! isset($argv[2]) || ! is_numeric($argv[2])) {
-    print "Usage:  $script_file (nocommitiferror|commitiferror) id_member_type  [--server=ldapserverhost]\n";
+    print "Usage:  $script_file (nocommitiferror|commitiferror) id_member_type  [--server=ldapserverhost] [-y]\n";
 	exit(-1);
 }
 
@@ -103,6 +103,7 @@ foreach($argv as $key => $val)
 {
 	if ($val == 'commitiferror') $forcecommit=1;
 	if (preg_match('/--server=([^\s]+)$/',$val,$reg)) $conf->global->LDAP_SERVER_HOST=$reg[1];
+	if (preg_match('/-y$/',$val,$reg)) $confirmed=1;
 }
 
 print "Mails sending disabled (useless in batch mode)\n";
@@ -139,9 +140,11 @@ if ($typeid <= 0)
 }
 
 
-print "Hit Enter to continue or CTRL+C to stop...\n";
-$input = trim(fgets(STDIN));
-
+if (! $confirmed)
+{
+	print "Hit Enter to continue or CTRL+C to stop...\n";
+	$input = trim(fgets(STDIN));
+}
 
 // Load table of correspondence of countries
 $hashlib2rowid=array();
diff --git a/scripts/user/sync_groups_ldap2dolibarr.php b/scripts/user/sync_groups_ldap2dolibarr.php
index 93406276ead..b48a4af974e 100755
--- a/scripts/user/sync_groups_ldap2dolibarr.php
+++ b/scripts/user/sync_groups_ldap2dolibarr.php
@@ -49,6 +49,7 @@ $langs->load("errors");
 $version=DOL_VERSION;
 $error=0;
 $forcecommit=0;
+$confirmed=0;
 
 
 /*
@@ -73,7 +74,7 @@ $required_fields=array_unique(array_values(array_filter($required_fields, "dolVa
 
 if (! isset($argv[1])) {
 	//print "Usage:  $script_file (nocommitiferror|commitiferror) [id_group]\n";
-	print "Usage:  $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...]\n";
+	print "Usage:  $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
 	exit(-1);
 }
 
@@ -82,6 +83,7 @@ foreach($argv as $key => $val)
 	if ($val == 'commitiferror') $forcecommit=1;
 	if (preg_match('/--server=([^\s]+)$/',$val,$reg)) $conf->global->LDAP_SERVER_HOST=$reg[1];
 	if (preg_match('/--excludeuser=([^\s]+)$/',$val,$reg)) $excludeuser=explode(',',$reg[1]);
+	if (preg_match('/-y$/',$val,$reg)) $confirmed=1;
 }
 
 print "Mails sending disabled (useless in batch mode)\n";
@@ -105,9 +107,11 @@ print "commitiferror=".$forcecommit."\n";
 print "Mapped LDAP fields=".join(',',$required_fields)."\n";
 print "\n";
 
-print "Hit Enter to continue or CTRL+C to stop...\n";
-$input = trim(fgets(STDIN));
-
+if (! $confirmed)
+{
+	print "Hit Enter to continue or CTRL+C to stop...\n";
+	$input = trim(fgets(STDIN));
+}
 
 if (empty($conf->global->LDAP_GROUP_DN))
 {
diff --git a/scripts/user/sync_users_ldap2dolibarr.php b/scripts/user/sync_users_ldap2dolibarr.php
index 296db5ec0b2..53a48cc03ca 100755
--- a/scripts/user/sync_users_ldap2dolibarr.php
+++ b/scripts/user/sync_users_ldap2dolibarr.php
@@ -48,7 +48,7 @@ $version=DOL_VERSION;
 $error=0;
 $forcecommit=0;
 $excludeuser=array();
-
+$confirmed=0;
 
 /*
  * Main
@@ -85,7 +85,7 @@ $required_fields = array(
 $required_fields=array_unique(array_values(array_filter($required_fields, "dolValidElement")));
 
 if (! isset($argv[1])) {
-	print "Usage:  $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...]\n";
+	print "Usage:  $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
     exit(-1);
 }
 
@@ -94,6 +94,7 @@ foreach($argv as $key => $val)
 	if ($val == 'commitiferror') $forcecommit=1;
 	if (preg_match('/--server=([^\s]+)$/',$val,$reg)) $conf->global->LDAP_SERVER_HOST=$reg[1];
 	if (preg_match('/--excludeuser=([^\s]+)$/',$val,$reg)) $excludeuser=explode(',',$reg[1]);
+	if (preg_match('/-y$/',$val,$reg)) $confirmed=1;
 }
 
 print "Mails sending disabled (useless in batch mode)\n";
@@ -118,9 +119,11 @@ print "excludeuser=".join(',',$excludeuser)."\n";
 print "Mapped LDAP fields=".join(',',$required_fields)."\n";
 print "\n";
 
-print "Hit Enter to continue or CTRL+C to stop...\n";
-$input = trim(fgets(STDIN));
-
+if (! $confirmed)
+{
+	print "Hit Enter to continue or CTRL+C to stop...\n";
+	$input = trim(fgets(STDIN));
+}
 
 if (empty($conf->global->LDAP_USER_DN))
 {
-- 
GitLab