From f3a1978b9a55514a8f5f96cd5ec195d208e8a368 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Thu, 22 Oct 2009 01:04:23 +0000
Subject: [PATCH] Fix: Use preg functions

---
 dev/samples/create_order.php                    | 10 +++++-----
 dev/samples/create_product.php                  | 10 +++++-----
 dev/skeletons/build_class_from_table.php        | 15 +++++++--------
 dev/skeletons/build_webservice_from_class.php   | 15 +++++++--------
 dev/skeletons/skeleton_script.php               | 10 +++++-----
 dev/translation/autotranslator.php              |  8 ++++----
 scripts/company/sync_contacts_dolibarr2ldap.php | 10 +++++-----
 scripts/emailings/mailing-send.php              | 14 +++++++-------
 scripts/invoices/rebuild_merge_pdf.php          |  8 ++++----
 scripts/members/sync_members_dolibarr2ldap.php  |  9 +++++----
 scripts/members/sync_members_ldap2dolibarr.php  | 10 +++++-----
 scripts/user/sync_groups_dolibarr2ldap.php      | 10 +++++-----
 scripts/user/sync_users_dolibarr2ldap.php       | 10 +++++-----
 scripts/withdrawals/prelevement-verif.php       | 12 ++++++------
 scripts/withdrawals/prelevement.php             | 12 ++++++------
 15 files changed, 81 insertions(+), 82 deletions(-)

diff --git a/dev/samples/create_order.php b/dev/samples/create_order.php
index 7439f5f7460..872dd86a0e3 100644
--- a/dev/samples/create_order.php
+++ b/dev/samples/create_order.php
@@ -24,14 +24,14 @@
  *		\remarks	Put here some comments
  */
 
-// Test if batch mode
 $sapi_type = php_sapi_name();
-$script_file = __FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file = '/'.$reg[1].'/i';
-$path=preg_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-    echo "Error: You are using PHP for CGI/Web. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
 }
 
diff --git a/dev/samples/create_product.php b/dev/samples/create_product.php
index cd3372abea5..fb323dee79d 100644
--- a/dev/samples/create_product.php
+++ b/dev/samples/create_product.php
@@ -24,14 +24,14 @@
  *		\remarks	Put here some comments
  */
 
-// Test if batch mode
 $sapi_type = php_sapi_name();
-$script_file = __FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file = '/'.$reg[1].'/i';
-$path=preg_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-    echo "Error: You are using PHP for CGI/Web. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
 }
 
diff --git a/dev/skeletons/build_class_from_table.php b/dev/skeletons/build_class_from_table.php
index f6e7ab0798f..28ba84e4545 100644
--- a/dev/skeletons/build_class_from_table.php
+++ b/dev/skeletons/build_class_from_table.php
@@ -23,16 +23,15 @@
  *  \version    $Id$
  */
 
-// Test if batch mode
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file = '/'.$reg[1].'/i';
-$path=preg_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
-if (substr($sapi_type, 0, 3) == 'cgi')
-{
-	echo "Error: You use PHP for CGI mode. To execute $script_file as a command line program, you must use PHP for CLI mode (try php-cli).\n";
-	exit;
+// Test if batch mode
+if (substr($sapi_type, 0, 3) == 'cgi') {
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
+    exit;
 }
 
 // Include Dolibarr environment
diff --git a/dev/skeletons/build_webservice_from_class.php b/dev/skeletons/build_webservice_from_class.php
index dc5efecece3..83389fe76e3 100644
--- a/dev/skeletons/build_webservice_from_class.php
+++ b/dev/skeletons/build_webservice_from_class.php
@@ -23,16 +23,15 @@
  *  \version    $Id$
  */
 
-// Test if batch mode
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file = '/'.$reg[1].'/i';
-$path=preg_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
-if (substr($sapi_type, 0, 3) == 'cgi')
-{
-	echo "Error: You use PHP for CGI mode. To execute $script_file as a command line program, you must use PHP for CLI mode (try php-cli).\n";
-	exit;
+// Test if batch mode
+if (substr($sapi_type, 0, 3) == 'cgi') {
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
+    exit;
 }
 
 // Include Dolibarr environment
diff --git a/dev/skeletons/skeleton_script.php b/dev/skeletons/skeleton_script.php
index 4ca769b753c..936ab71b52a 100644
--- a/dev/skeletons/skeleton_script.php
+++ b/dev/skeletons/skeleton_script.php
@@ -26,14 +26,14 @@
  *		\remarks	Put here some comments
  */
 
-// Test if batch mode and define path of script
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file = '/'.$reg[1].'/i';
-$path=preg_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-    echo "Error: You are using PHP for CGI/Web. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
 }
 
diff --git a/dev/translation/autotranslator.php b/dev/translation/autotranslator.php
index aef9e3b37cd..cde77595a0a 100644
--- a/dev/translation/autotranslator.php
+++ b/dev/translation/autotranslator.php
@@ -25,12 +25,12 @@
  *		\remarks	Put here some comments
  */
 
-// Test if batch mode
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file = '/'.$reg[1].'/i';
-$path=preg_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
     echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
diff --git a/scripts/company/sync_contacts_dolibarr2ldap.php b/scripts/company/sync_contacts_dolibarr2ldap.php
index 667d784bc19..8faa8939d00 100644
--- a/scripts/company/sync_contacts_dolibarr2ldap.php
+++ b/scripts/company/sync_contacts_dolibarr2ldap.php
@@ -24,13 +24,14 @@
  *		\version	$Id$
  */
 
-// Test si mode CLI
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file=$reg[1];
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-    echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer $script_file en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
 }
 
@@ -42,7 +43,6 @@ $now=$argv[1];
 
 // Recupere env dolibarr
 $version='$Revision$';
-$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
 
 require_once($path."../../htdocs/master.inc.php");
 require_once(DOL_DOCUMENT_ROOT."/contact.class.php");
diff --git a/scripts/emailings/mailing-send.php b/scripts/emailings/mailing-send.php
index b75dadc48f1..131416e0f6e 100644
--- a/scripts/emailings/mailing-send.php
+++ b/scripts/emailings/mailing-send.php
@@ -27,11 +27,15 @@
  */
 
 
-// Test if CLI mode
 $sapi_type = php_sapi_name();
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
+
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-	echo "Error: You are using PH for CGI/Web. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
-	exit;
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
+    exit;
 }
 
 if (! isset($argv[1]) || ! $argv[1]) {
@@ -40,10 +44,6 @@ if (! isset($argv[1]) || ! $argv[1]) {
 }
 $id=$argv[1];
 
-// Recupere root dolibarr
-$path=str_replace('mailing-send.php','',$_SERVER["PHP_SELF"]);
-
-
 require_once ($path."../../htdocs/master.inc.php");
 require_once (DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php");
 
diff --git a/scripts/invoices/rebuild_merge_pdf.php b/scripts/invoices/rebuild_merge_pdf.php
index 16b0006d44e..e88a4520853 100644
--- a/scripts/invoices/rebuild_merge_pdf.php
+++ b/scripts/invoices/rebuild_merge_pdf.php
@@ -24,14 +24,14 @@
  *		\version	$Id$
  */
 
-// Test if batch mode
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file=$reg[1];
+$script_file = basename(__FILE__);
 $path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-    echo "Error: You are using PH for CGI/Web. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
 }
 
diff --git a/scripts/members/sync_members_dolibarr2ldap.php b/scripts/members/sync_members_dolibarr2ldap.php
index 4e27b6c5893..55d63d89242 100644
--- a/scripts/members/sync_members_dolibarr2ldap.php
+++ b/scripts/members/sync_members_dolibarr2ldap.php
@@ -24,13 +24,14 @@
  * 		\version	$Id$
  */
 
-// Test si mode batch
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file=$reg[1];
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-    echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer $script_file en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
 }
 
diff --git a/scripts/members/sync_members_ldap2dolibarr.php b/scripts/members/sync_members_ldap2dolibarr.php
index 2993937e11d..bf592ec3c49 100644
--- a/scripts/members/sync_members_ldap2dolibarr.php
+++ b/scripts/members/sync_members_ldap2dolibarr.php
@@ -24,19 +24,19 @@
  * 		\version	$Id$
  */
 
-// Test si mode batch
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file=$reg[1];
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-    echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer $script_file en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
 }
 
 // Main
 $version='$Revision$';
-$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
 @set_time_limit(0);
 $error=0;
 $forcecommit=0;
diff --git a/scripts/user/sync_groups_dolibarr2ldap.php b/scripts/user/sync_groups_dolibarr2ldap.php
index ce9679c41b2..07e5327989e 100644
--- a/scripts/user/sync_groups_dolibarr2ldap.php
+++ b/scripts/user/sync_groups_dolibarr2ldap.php
@@ -24,13 +24,14 @@
  * 		\version	$Id$
  */
 
-// Test si mode batch
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file=$reg[1];
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-    echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer $script_file en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
 }
 
@@ -42,7 +43,6 @@ $now=$argv[1];
 
 // Recupere env dolibarr
 $version='$Revision$';
-$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
 
 require_once($path."../../htdocs/master.inc.php");
 require_once(DOL_DOCUMENT_ROOT."/lib/ldap.class.php");
diff --git a/scripts/user/sync_users_dolibarr2ldap.php b/scripts/user/sync_users_dolibarr2ldap.php
index 571b1d0b699..f082f4e49fc 100644
--- a/scripts/user/sync_users_dolibarr2ldap.php
+++ b/scripts/user/sync_users_dolibarr2ldap.php
@@ -24,13 +24,14 @@
  *		\version	$Id$
  */
 
-// Test si mode batch
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file=$reg[1];
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-    echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer $script_file en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
     exit;
 }
 
@@ -42,7 +43,6 @@ $now=$argv[1];
 
 // Recupere env dolibarr
 $version='$Revision$';
-$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
 
 require_once($path."../../htdocs/master.inc.php");
 require_once(DOL_DOCUMENT_ROOT."/lib/ldap.class.php");
diff --git a/scripts/withdrawals/prelevement-verif.php b/scripts/withdrawals/prelevement-verif.php
index dec873f9841..4582b28a160 100644
--- a/scripts/withdrawals/prelevement-verif.php
+++ b/scripts/withdrawals/prelevement-verif.php
@@ -24,19 +24,19 @@
  *		\version	$Id$
  */
 
-// Test si mode CLI
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file=$reg[1];
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-	echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer $script_file en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
-	exit;
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
+    exit;
 }
 
 // Recupere env dolibarr
 $version='$Revision$';
-$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
 
 require_once($path."../../htdocs/master.inc.php");
 require_once(DOL_DOCUMENT_ROOT."/facture.class.php");
diff --git a/scripts/withdrawals/prelevement.php b/scripts/withdrawals/prelevement.php
index 774b2b17107..b9bbd557c75 100644
--- a/scripts/withdrawals/prelevement.php
+++ b/scripts/withdrawals/prelevement.php
@@ -24,19 +24,19 @@
  * 		\version	$Id$
  */
 
-// Test si mode CLI
 $sapi_type = php_sapi_name();
-$script_file=__FILE__;
-if (preg_match('/([^\\/]+)$/i',$script_file,$reg)) $script_file=$reg[1];
+$script_file = basename(__FILE__);
+$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
+$path=preg_replace('@[\\\/]+$@','',$path).'/';
 
+// Test if batch mode
 if (substr($sapi_type, 0, 3) == 'cgi') {
-	echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer $script_file en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
-	exit;
+    echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
+    exit;
 }
 
 // Recupere env dolibarr
 $version='$Revision$';
-$path=str_replace($script_file,'',$_SERVER["PHP_SELF"]);
 
 require_once($path."../../htdocs/master.inc.php");
 require_once(DOL_DOCUMENT_ROOT."/compta/prelevement/bon-prelevement.class.php");
-- 
GitLab