From cc16bb0befc906751b4dc29d22ac0497d17c20b4 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 16 May 2017 12:22:55 +0200
Subject: [PATCH] Debug default value feature.

---
 htdocs/admin/defaultvalues.php    |  6 +++
 htdocs/core/lib/functions.lib.php | 74 +++++++++++++++----------------
 htdocs/projet/card.php            | 12 ++---
 3 files changed, 48 insertions(+), 44 deletions(-)

diff --git a/htdocs/admin/defaultvalues.php b/htdocs/admin/defaultvalues.php
index 013be811b5b..7ebc67472d1 100644
--- a/htdocs/admin/defaultvalues.php
+++ b/htdocs/admin/defaultvalues.php
@@ -271,6 +271,12 @@ if ($mode != 'focus')
         $texthelp.='__DAY__<br>';
         $texthelp.='__MONTH__<br>';
         $texthelp.='__YEAR__<br>';
+        $texthelp.='__PREVIOUS_DAY__<br>';
+        $texthelp.='__PREVIOUS_MONTH__<br>';
+        $texthelp.='__PREVIOUS_YEAR__<br>';
+        $texthelp.='__NEXT_DAY__<br>';
+        $texthelp.='__NEXT_MONTH__<br>';
+        $texthelp.='__NEXT_YEAR__<br>';
         if (! empty($conf->multicompany->enabled)) $texthelp.='__ENTITYID__<br>';
         $textvalue=$form->textwithpicto($langs->trans("Value"), $texthelp, 1, 'help', '', 0, 2, '');
     }
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index d2638898a33..63990112be5 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -240,7 +240,7 @@ function dol_shutdown()
  *  Return value of a param into GET or POST supervariable
  *
  *  @param	string	$paramname   Name of parameter to found
- *  @param	string	$check	     Type of check (''=no check,  'int'=check it's numeric, 'alpha'=check it's text and sign, 'aZ'=check it's a-z only, 'array'=check it's array, 'san_alpha'=Use filter_var with FILTER_SANITIZE_STRING (do not use this for free text string), 'day', 'month', 'year', 'custom'= custom filter specify $filter and $options)
+ *  @param	string	$check	     Type of check (''=no check, 'none'=no check, 'int'=check it's numeric, 'alpha'=check it's text and sign, 'aZ'=check it's a-z only, 'array'=check it's array, 'san_alpha'=Use filter_var with FILTER_SANITIZE_STRING (do not use this for free text string), 'day', 'month', 'year', 'custom'= custom filter specify $filter and $options)
  *  @param	int		$method	     Type of method (0 = get then post, 1 = only get, 2 = only post, 3 = post then get, 4 = post then get then cookie)
  *  @param  int     $filter      Filter to apply when $check is set to 'custom'. (See http://php.net/manual/en/filter.filters.php for détails)
  *  @param  mixed   $options     Options to pass to filter_var when $check is set to 'custom'.
@@ -249,6 +249,8 @@ function dol_shutdown()
  */
 function GETPOST($paramname, $check='', $method=0, $filter=NULL, $options=NULL)
 {
+    global $mysoc,$user,$conf;
+    
     if (empty($method)) $out = isset($_GET[$paramname])?$_GET[$paramname]:(isset($_POST[$paramname])?$_POST[$paramname]:'');
 	elseif ($method==1) $out = isset($_GET[$paramname])?$_GET[$paramname]:'';
 	elseif ($method==2) $out = isset($_POST[$paramname])?$_POST[$paramname]:'';
@@ -258,8 +260,6 @@ function GETPOST($paramname, $check='', $method=0, $filter=NULL, $options=NULL)
 	
 	if (empty($method) || $method == 3 || $method == 4)
 	{
-	    global $conf;
-	    
 	    // Management of default values
 	    if (! isset($_GET['sortfield']) && ! empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES))	// If we did a click on a field to sort, we do no apply default values. Same if option MAIN_DISABLE_DEFAULT_VALUES is on
 	    {
@@ -320,43 +320,41 @@ function GETPOST($paramname, $check='', $method=0, $filter=NULL, $options=NULL)
 	if (! empty($check))
 	{
 	    // Replace vars like __DAY__, __MONTH__, __YEAR__, __MYCOUNTRYID__, __USERID__, __ENTITYID__
-	    // TODO Add more var like __PREVIOUSDAY__, __PREVIOUSMONTH__, __PREVIOUSYEAR__
-	    if (! is_array($out) && preg_match('/^__([a-z0-9]+)__$/i', $out, $reg))
+	    if (! is_array($out))
 	    {
-	        if ($reg[1] == 'DAY')
-	        {
-    	        $tmp=dol_getdate(dol_now(), true);
-    	        $out = $tmp['mday'];
-	        }
-	        elseif ($reg[1] == 'MONTH')
-	        {
-    	        $tmp=dol_getdate(dol_now(), true);
-    	        $out = $tmp['mon'];
-	        }
-	        elseif ($reg[1] == 'YEAR')
-	        {
-	            $tmp=dol_getdate(dol_now(), true);
-	            $out = $tmp['year'];
-	        }
-	        elseif ($reg[1] == 'MYCOUNTRYID')
-	        {
-	            global $mysoc;
-	            $out = $mysoc->country_id;
-	        }
-	        elseif ($reg[1] == 'USERID')
-	        {
-	            global $user;
-	            $out = $user->id;
-	        }
-	    	elseif ($reg[1] == 'SUPERVISORID')
-	        {
-	            global $user;
-	            $out = $user->fk_user;
-	        }
-	        elseif ($reg[1] == 'ENTITYID')
+	        $maxloop=20; $loopnb=0;    // Protection against infinite loop
+	        while (preg_match('/__([A-Z0-9]+_?[A-Z0-9]+)__/i', $out, $reg) && ($loopnb < $maxloop))    // Detect '__ABCDEF__' as key 'ABCDEF' and '__ABC_DEF__' as key 'ABC_DEF'
 	        {
-	            global $conf;
-	            $out = $conf->entity;
+	            $loopnb++; $newout = '';
+
+    	        if ($reg[1] == 'DAY')       { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['mday']; }
+    	        elseif ($reg[1] == 'MONTH') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['mon'];  }
+    	        elseif ($reg[1] == 'YEAR')  { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['year']; }
+    	    	elseif ($reg[1] == 'PREVIOUS_DAY')   { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_prev_day($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['day']; }
+    	        elseif ($reg[1] == 'PREVIOUS_MONTH') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_prev_month($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['month']; }
+    	        elseif ($reg[1] == 'PREVIOUS_YEAR')  { $tmp=dol_getdate(dol_now(), true); $newout = ($tmp['year'] - 1); }
+    	    	elseif ($reg[1] == 'NEXT_DAY')   { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_next_day($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['day']; }
+    	        elseif ($reg[1] == 'NEXT_MONTH') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_next_month($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['month']; }
+    	        elseif ($reg[1] == 'NEXT_YEAR')  { $tmp=dol_getdate(dol_now(), true); $newout = ($tmp['year'] + 1); }
+    	        elseif ($reg[1] == 'MYCOUNTRYID')
+    	        {
+    	            $newout = $mysoc->country_id;
+    	        }
+    	        elseif ($reg[1] == 'USERID')
+    	        {
+    	            $newout = $user->id;
+    	        }
+    	    	elseif ($reg[1] == 'SUPERVISORID')
+    	        {
+    	            $newout = $user->fk_user;
+    	        }
+    	        elseif ($reg[1] == 'ENTITYID')
+    	        {
+    	            $newout = $conf->entity;
+    	        }
+    	        else $newout = '';     // Key not found, we replace with empty string
+    	        //var_dump('__'.$reg[1].'__ -> '.$newout);
+    	        $out = preg_replace('/__'.preg_quote($reg[1],'/').'__/', $newout, $out);
 	        }
 	    }
 
diff --git a/htdocs/projet/card.php b/htdocs/projet/card.php
index 791cd6855f0..1ee81f7b917 100644
--- a/htdocs/projet/card.php
+++ b/htdocs/projet/card.php
@@ -142,9 +142,9 @@ if (empty($reshook))
 	        $db->begin();
 
 	        $object->ref             = GETPOST('ref','alpha');
-	        $object->title           = GETPOST('title'); // Do not use 'alpha' here, we want field as it is
+	        $object->title           = GETPOST('title','none'); // Do not use 'alpha' here, we want field as it is
 	        $object->socid           = GETPOST('socid','int');
-	        $object->description     = GETPOST('description'); // Do not use 'alpha' here, we want field as it is
+	        $object->description     = GETPOST('description','none'); // Do not use 'alpha' here, we want field as it is
 	        $object->public          = GETPOST('public','alpha');
 	        $object->opp_amount      = price2num(GETPOST('opp_amount'));
 	        $object->budget_amount   = price2num(GETPOST('budget_amount'));
@@ -243,9 +243,9 @@ if (empty($reshook))
 			$old_start_date = $object->date_start;
 
 	        $object->ref          = GETPOST('ref','alpha');
-	        $object->title        = GETPOST('title'); // Do not use 'alpha' here, we want field as it is
+	        $object->title        = GETPOST('title','none'); // Do not use 'alpha' here, we want field as it is
 	        $object->socid        = GETPOST('socid','int');
-	        $object->description  = GETPOST('description');	// Do not use 'alpha' here, we want field as it is
+	        $object->description  = GETPOST('description','none');	// Do not use 'alpha' here, we want field as it is
 	        $object->public       = GETPOST('public','alpha');
 	        $object->date_start   = empty($_POST["projectstart"])?'':$date_start;
 	        $object->date_end     = empty($_POST["projectend"])?'':$date_end;
@@ -509,7 +509,7 @@ if ($action == 'create' && $user->rights->projet->creer)
     print '</td></tr>';
 
     // Label
-    print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td><td><input size="80" type="text" name="title" value="'.GETPOST("title").'"></td></tr>';
+    print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td><td><input size="80" type="text" name="title" value="'.GETPOST("title",'none').'"></td></tr>';
 
     // Thirdparty
     if ($conf->societe->enabled)
@@ -588,7 +588,7 @@ if ($action == 'create' && $user->rights->projet->creer)
     // Description
     print '<tr><td class="tdtop">'.$langs->trans("Description").'</td>';
     print '<td>';
-    print '<textarea name="description" wrap="soft" class="centpercent" rows="'.ROWS_3.'">'.GETPOST("description").'</textarea>';
+    print '<textarea name="description" wrap="soft" class="centpercent" rows="'.ROWS_3.'">'.dol_escape_htmltag(GETPOST("description",'none')).'</textarea>';
     print '</td></tr>';
 
     if ($conf->categorie->enabled) {
-- 
GitLab