swad-core/swad_parameter.c

1110 lines
40 KiB
C
Raw Normal View History

2014-12-01 23:55:08 +01:00
// swad_parameter.c: CGI parameters
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
2021-02-09 12:43:45 +01:00
Copyright (C) 1999-2021 Antonio Ca<EFBFBD>as Vargas
2014-12-01 23:55:08 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************** Headers **********************************/
/*****************************************************************************/
#include <ctype.h> // For isprint, isspace, etc.
2019-12-29 12:39:00 +01:00
#include <stddef.h> // For NULL
2014-12-01 23:55:08 +01:00
#include <stdlib.h> // For calloc
#include <string.h> // For string functions
#include "swad_action.h"
#include "swad_config.h"
#include "swad_global.h"
2019-11-11 00:15:44 +01:00
#include "swad_HTML.h"
2014-12-01 23:55:08 +01:00
#include "swad_parameter.h"
#include "swad_password.h"
2019-03-26 11:53:21 +01:00
#include "swad_setting.h"
2015-01-04 15:48:24 +01:00
#include "swad_tab.h"
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/*************** External global variables from others modules ***************/
/*****************************************************************************/
extern struct Globals Gbl;
2019-11-03 13:19:32 +01:00
/*****************************************************************************/
/***************************** Public constants ******************************/
/*****************************************************************************/
2019-11-04 23:33:59 +01:00
const char *Par_SEPARATOR_PARAM_MULTIPLE = "\x0a"; // Must be 1 <= character <= 31
2019-11-03 13:19:32 +01:00
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/*********************** Private types and constants *************************/
/*****************************************************************************/
/*****************************************************************************/
/****************************** Private variables ****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2016-03-31 15:03:00 +02:00
static void Par_GetBoundary (void);
2016-03-30 17:37:56 +02:00
static void Par_CreateListOfParamsFromQueryString (void);
static void Par_CreateListOfParamsFromTmpFile (void);
2016-03-31 02:15:17 +02:00
static int Par_ReadTmpFileUntilQuote (void);
static int Par_ReadTmpFileUntilReturn (void);
2016-03-30 14:25:04 +02:00
2016-03-31 19:04:13 +02:00
static bool Par_CheckIsParamCanBeUsedInGETMethod (const char *ParamName);
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/*** Read all parameters passed to this CGI and store for later processing ***/
/*****************************************************************************/
2017-03-07 19:55:29 +01:00
#define Par_MAX_BYTES_METHOD (128 - 1)
#define Par_MAX_BYTES_CONTENT_TYPE (128 - 1)
2017-01-15 22:58:26 +01:00
2014-12-01 23:55:08 +01:00
bool Par_GetQueryString (void)
{
2017-03-07 19:55:29 +01:00
char Method[Par_MAX_BYTES_METHOD + 1];
char ContentType[Par_MAX_BYTES_CONTENT_TYPE + 1];
2019-11-08 01:10:32 +01:00
char UnsignedLongStr[Cns_MAX_DECIMAL_DIGITS_ULONG + 1];
2016-05-01 16:26:20 +02:00
unsigned long UnsignedLong;
2014-12-01 23:55:08 +01:00
Str_Copy (Method,getenv ("REQUEST_METHOD"),sizeof (Method) - 1);
2014-12-01 23:55:08 +01:00
if (!strcmp (Method,"GET"))
{
2016-03-30 10:50:31 +02:00
/***** GET method *****/
2016-03-30 14:25:04 +02:00
Gbl.Params.GetMethod = true;
2016-10-21 00:38:34 +02:00
Gbl.ContentReceivedByCGI = Act_CONT_NORM;
2016-03-30 10:50:31 +02:00
/* Get content length */
2016-03-30 14:25:04 +02:00
Gbl.Params.ContentLength = strlen (getenv ("QUERY_STRING"));
2016-03-30 10:50:31 +02:00
/* Allocate memory for query string */
if ((Gbl.Params.QueryString = malloc (Gbl.Params.ContentLength + 1)) == NULL)
2016-03-30 10:50:31 +02:00
return false;
/* Copy query string from environment variable */
2017-01-15 22:58:26 +01:00
Str_Copy (Gbl.Params.QueryString,getenv ("QUERY_STRING"),
Gbl.Params.ContentLength);
2014-12-01 23:55:08 +01:00
}
else
{
2016-03-30 10:50:31 +02:00
/***** PUSH method *****/
/* Get content length */
if (getenv ("CONTENT_LENGTH"))
{
2017-01-17 03:10:43 +01:00
Str_Copy (UnsignedLongStr,getenv ("CONTENT_LENGTH"),
sizeof (UnsignedLongStr) - 1);
2016-05-01 16:26:20 +02:00
if (sscanf (UnsignedLongStr,"%lu",&UnsignedLong) != 1)
2016-03-30 10:50:31 +02:00
return false;
2016-05-01 16:26:20 +02:00
Gbl.Params.ContentLength = (size_t) UnsignedLong;
2016-03-30 10:50:31 +02:00
}
else
return false;
2014-12-01 23:55:08 +01:00
/* If data are received ==> the environment variable CONTENT_TYPE will hold:
multipart/form-data; boundary=---------------------------7d13ca2e948
*/
if (getenv ("CONTENT_TYPE") == NULL)
return false;
Str_Copy (ContentType,getenv ("CONTENT_TYPE"),sizeof (ContentType) - 1);
2014-12-01 23:55:08 +01:00
if (!strncmp (ContentType,"multipart/form-data",strlen ("multipart/form-data")))
{
2016-10-21 00:38:34 +02:00
Gbl.ContentReceivedByCGI = Act_CONT_DATA;
2016-03-31 15:03:00 +02:00
Par_GetBoundary ();
2014-12-01 23:55:08 +01:00
return Fil_ReadStdinIntoTmpFile ();
}
else if (!strncmp (ContentType,"text/xml",strlen ("text/xml")))
{
Gbl.WebService.IsWebService = true;
}
else
{
2016-10-21 00:38:34 +02:00
Gbl.ContentReceivedByCGI = Act_CONT_NORM;
2014-12-01 23:55:08 +01:00
2016-03-30 10:50:31 +02:00
/* Allocate memory for query string */
if ((Gbl.Params.QueryString = malloc (Gbl.Params.ContentLength + 1)) == NULL)
2016-03-30 10:50:31 +02:00
return false;
/* Copy query string from stdin */
2019-11-06 19:45:20 +01:00
if (fread (Gbl.Params.QueryString,sizeof (char),Gbl.Params.ContentLength,stdin) != Gbl.Params.ContentLength)
2016-03-30 10:50:31 +02:00
{
2016-03-30 14:25:04 +02:00
Gbl.Params.QueryString[0] = '\0';
2016-03-30 10:50:31 +02:00
return false;
}
2016-03-30 14:25:04 +02:00
Gbl.Params.QueryString[Gbl.Params.ContentLength] = '\0';
2014-12-01 23:55:08 +01:00
}
}
2016-03-30 14:25:04 +02:00
2014-12-01 23:55:08 +01:00
return true;
}
2016-03-31 10:11:36 +02:00
2016-03-31 15:03:00 +02:00
/*****************************************************************************/
/**************************** Get boundary string ****************************/
/*****************************************************************************/
static void Par_GetBoundary (void)
{
const char *PtrToBoundary;
/*
If data are received ==> the environment variable CONTENT_TYPE will hold:
multipart/form-data; boundary=---------------------------7d13ca2e948
Gbl.Boundary.StrWithCRLF will be set to:
"\r\n-----------------------------7d13ca2e948"
I.e. 0x0D, 0x0A, '-', '-', and boundary.
*/
/***** Get pointer to boundary string *****/
PtrToBoundary = strstr (getenv ("CONTENT_TYPE"),"boundary=") + strlen ("boundary=");
/***** Check that boundary string is not too long *****/
2017-03-07 11:03:05 +01:00
if (2 + 2 + strlen (PtrToBoundary) > Par_MAX_BYTES_BOUNDARY_WITH_CR_LF)
2016-03-31 15:03:00 +02:00
Lay_ShowErrorAndExit ("Delimiter string too long.");
/***** Create boundary strings *****/
2018-10-18 02:02:32 +02:00
snprintf (Gbl.Boundary.StrWithoutCRLF,sizeof (Gbl.Boundary.StrWithoutCRLF),
"--%s",PtrToBoundary);
2018-10-18 02:02:32 +02:00
snprintf (Gbl.Boundary.StrWithCRLF,sizeof (Gbl.Boundary.StrWithCRLF),
"%c%c%s",0x0D,0x0A,Gbl.Boundary.StrWithoutCRLF);
2016-03-31 15:03:00 +02:00
/***** Compute lengths *****/
Gbl.Boundary.LengthWithoutCRLF = strlen (Gbl.Boundary.StrWithoutCRLF);
Gbl.Boundary.LengthWithCRLF = 2 + Gbl.Boundary.LengthWithoutCRLF;
}
2016-03-30 17:37:56 +02:00
/*****************************************************************************/
/************************ Create list of parameters **************************/
/*****************************************************************************/
2016-03-31 02:15:17 +02:00
/*
Parameter #1 Parameter #n
+------------------+ +------------------+
List --> |Name.Start | -> |Name.Start |
+------------------+ / +------------------+
|Name.Length | | |Name.Length |
+------------------+ | +------------------+
2016-04-01 01:59:27 +02:00
|FileName.Start | | |FileName.Start |
2016-03-31 02:15:17 +02:00
+------------------+ | +------------------+
2016-04-01 01:59:27 +02:00
|FileName.Lenght | | |FileName.Lenght |
2016-03-31 02:15:17 +02:00
+------------------+ . +------------------+
|ContentType.Start | . |ContentType.Start |
+------------------+ . +------------------+
|ContentType.Lenght| | |ContentType.Lenght|
+------------------+ | +------------------+
|Value.Start | | |Value.Start |
+------------------+ | +------------------+
|Value.Lengh | | |Value.Lengh |
+------------------+ / +------------------+
| Next -------- | NULL |
+------------------+ +------------------+
2016-03-30 17:48:18 +02:00
*/
2016-03-30 17:37:56 +02:00
2016-03-31 15:03:00 +02:00
void Par_CreateListOfParams (void)
2016-03-30 17:37:56 +02:00
{
2016-03-31 10:11:36 +02:00
/***** Initialize empty list of parameters *****/
Gbl.Params.List = NULL;
/***** Get list *****/
if (Gbl.Params.ContentLength)
switch (Gbl.ContentReceivedByCGI)
{
2016-10-21 00:38:34 +02:00
case Act_CONT_NORM:
2016-03-31 10:11:36 +02:00
Par_CreateListOfParamsFromQueryString ();
break;
2016-10-21 00:38:34 +02:00
case Act_CONT_DATA:
2016-03-31 10:11:36 +02:00
Par_CreateListOfParamsFromTmpFile ();
break;
}
2016-03-30 17:37:56 +02:00
}
/*****************************************************************************/
/**************** Create list of parameters from query string ****************/
/*****************************************************************************/
2016-03-30 17:48:18 +02:00
2016-03-30 17:37:56 +02:00
static void Par_CreateListOfParamsFromQueryString (void)
{
unsigned long CurPos; // Current position in query string
2016-04-11 22:46:08 +02:00
struct Param *Param = NULL; // Initialized to avoid warning
2016-03-30 17:37:56 +02:00
struct Param *NewParam;
/***** Check if query string is empty *****/
2016-03-31 10:11:36 +02:00
if (!Gbl.Params.QueryString) return;
if (!Gbl.Params.QueryString[0]) return;
2016-03-30 17:37:56 +02:00
/***** Go over the query string
getting start positions and lengths of parameters *****/
for (CurPos = 0;
CurPos < Gbl.Params.ContentLength;
)
{
2016-03-31 02:15:17 +02:00
/* Allocate space for a new parameter initialized to 0 */
if ((NewParam = calloc (1,sizeof (*NewParam))) == NULL)
Lay_NotEnoughMemoryExit ();
2016-03-30 17:37:56 +02:00
/* Link the previous element in list with the current element */
if (CurPos == 0)
2016-03-30 17:48:18 +02:00
Gbl.Params.List = NewParam; // Pointer to first param
2016-03-30 17:37:56 +02:00
else
2016-03-30 17:48:18 +02:00
Param->Next = NewParam; // Pointer from former param to new param
2016-03-30 17:37:56 +02:00
/* Make the current element to be the just created */
Param = NewParam;
/* Get parameter name */
Param->Name.Start = CurPos;
Param->Name.Length = strcspn (&Gbl.Params.QueryString[CurPos],"=");
CurPos += Param->Name.Length;
/* Get parameter value */
if (CurPos < Gbl.Params.ContentLength)
if (Gbl.Params.QueryString[CurPos] == '=')
{
CurPos++; // Skip '='
if (CurPos < Gbl.Params.ContentLength)
{
2016-03-31 02:15:17 +02:00
Param->Value.Start = CurPos;
2016-03-30 17:37:56 +02:00
Param->Value.Length = strcspn (&Gbl.Params.QueryString[CurPos],"&");
CurPos += Param->Value.Length;
if (CurPos < Gbl.Params.ContentLength)
if (Gbl.Params.QueryString[CurPos] == '&')
CurPos++; // Skip '&'
}
}
}
}
/*****************************************************************************/
/*************** Create list of parameters from temporary file ***************/
/*****************************************************************************/
// TODO: Rename Gbl.F.Tmp to Gbl.F.In (InFile, QueryFile)?
2017-01-28 15:58:46 +01:00
#define Par_LENGTH_OF_STR_BEFORE_PARAM 38 // Length of "Content-Disposition: form-data; name=\""
#define Par_LENGTH_OF_STR_FILENAME 12 // Length of "; filename=\""
#define Par_LENGTH_OF_STR_CONTENT_TYPE 14 // Length of "Content-Type: "
#define Par_MAX_BYTES_STR_AUX (38 + 1) // Space to read any of the three preceding strings
2016-03-31 02:15:17 +02:00
2016-03-30 17:37:56 +02:00
static void Par_CreateListOfParamsFromTmpFile (void)
{
2016-03-31 02:15:17 +02:00
static const char *StringBeforeParam = "Content-Disposition: form-data; name=\"";
static const char *StringFilename = "; filename=\"";
static const char *StringContentType = "Content-Type: ";
2016-03-31 10:11:36 +02:00
unsigned long CurPos; // Current position in temporal file
2016-04-11 22:46:08 +02:00
struct Param *Param = NULL; // Initialized to avoid warning
2016-03-31 02:15:17 +02:00
struct Param *NewParam;
int Ch;
2017-01-28 15:58:46 +01:00
char StrAux[Par_MAX_BYTES_STR_AUX + 1];
2016-03-31 02:15:17 +02:00
/***** Go over the file
getting start positions and lengths of parameters *****/
2016-04-01 03:09:45 +02:00
if (Str_ReadFileUntilBoundaryStr (Gbl.F.Tmp,NULL,
Gbl.Boundary.StrWithoutCRLF,
Gbl.Boundary.LengthWithoutCRLF,
Fil_MAX_FILE_SIZE) == 1) // Delimiter string found
2016-03-31 02:15:17 +02:00
for (CurPos = 0;
CurPos < Gbl.Params.ContentLength;
)
{
/***** Skip \r\n after delimiter string *****/
2016-03-31 10:11:36 +02:00
if (fgetc (Gbl.F.Tmp) != 0x0D) break; // '\r'
if (fgetc (Gbl.F.Tmp) != 0x0A) break; // '\n'
2016-03-31 02:15:17 +02:00
Str_GetNextStrFromFileConvertingToLower (Gbl.F.Tmp,StrAux,
Par_LENGTH_OF_STR_BEFORE_PARAM);
if (!strcasecmp (StrAux,StringBeforeParam)) // Start of a parameter
{
/* Allocate space for a new parameter initialized to 0 */
if ((NewParam = calloc (1,sizeof (*NewParam))) == NULL)
Lay_NotEnoughMemoryExit ();
2016-03-31 02:15:17 +02:00
/* Link the previous element in list with the current element */
if (CurPos == 0)
Gbl.Params.List = NewParam; // Pointer to first param
else
Param->Next = NewParam; // Pointer from former param to new param
/* Make the current element to be the just created */
Param = NewParam;
/***** Get parameter name *****/
2016-03-31 15:03:00 +02:00
CurPos = (unsigned long) ftell (Gbl.F.Tmp); // At start of parameter name
2016-03-31 02:15:17 +02:00
Param->Name.Start = CurPos;
Ch = Par_ReadTmpFileUntilQuote ();
2016-03-31 15:03:00 +02:00
CurPos = (unsigned long) ftell (Gbl.F.Tmp); // Just after quote
Param->Name.Length = CurPos - 1 - Param->Name.Start;
2016-03-31 19:04:13 +02:00
2016-03-31 02:15:17 +02:00
/* Check if last character read after parameter name is a quote */
2016-03-31 10:11:36 +02:00
if (Ch != (int) '\"') break; // '\"'
2016-03-31 02:15:17 +02:00
/* Get next char after parameter name */
Ch = fgetc (Gbl.F.Tmp);
/***** Check if filename is present *****/
if (Ch == (int) StringFilename[0])
{
Str_GetNextStrFromFileConvertingToLower (Gbl.F.Tmp,StrAux,
Par_LENGTH_OF_STR_FILENAME-1);
2017-01-28 15:58:46 +01:00
if (!strcasecmp (StrAux,StringFilename + 1)) // Start of filename
2016-03-31 02:15:17 +02:00
{
/* Get filename */
2016-03-31 15:03:00 +02:00
CurPos = (unsigned long) ftell (Gbl.F.Tmp); // At start of filename
2016-04-01 01:59:27 +02:00
Param->FileName.Start = CurPos;
2016-03-31 02:15:17 +02:00
Ch = Par_ReadTmpFileUntilQuote ();
2016-03-31 15:03:00 +02:00
CurPos = (unsigned long) ftell (Gbl.F.Tmp); // Just after quote
2016-04-01 01:59:27 +02:00
Param->FileName.Length = CurPos - 1 - Param->FileName.Start;
2016-03-31 02:15:17 +02:00
/* Check if last character read after filename is a quote */
2016-03-31 10:11:36 +02:00
if (Ch != (int) '\"') break; // '\"'
2016-03-31 02:15:17 +02:00
/* Skip \r\n */
2016-03-31 10:11:36 +02:00
if (fgetc (Gbl.F.Tmp) != 0x0D) break; // '\r'
if (fgetc (Gbl.F.Tmp) != 0x0A) break; // '\n'
2016-03-31 02:15:17 +02:00
/* Check if Content-Type is present */
Str_GetNextStrFromFileConvertingToLower (Gbl.F.Tmp,StrAux,
Par_LENGTH_OF_STR_CONTENT_TYPE);
if (!strcasecmp (StrAux,StringContentType)) // Start of Content-Type
{
/* Get content type */
2016-03-31 15:03:00 +02:00
CurPos = (unsigned long) ftell (Gbl.F.Tmp); // At start of content type
2016-03-31 02:15:17 +02:00
Param->ContentType.Start = CurPos;
Ch = Par_ReadTmpFileUntilReturn ();
2016-03-31 15:03:00 +02:00
CurPos = (unsigned long) ftell (Gbl.F.Tmp); // Just after return
Param->ContentType.Length = CurPos - 1 - Param->ContentType.Start;
2016-03-31 02:15:17 +02:00
}
}
}
/***** Now \r\n\r\n is expected just before parameter value or file content *****/
/* Check if last character read is '\r' */
2016-03-31 10:11:36 +02:00
if (Ch != 0x0D) break; // '\r'
2016-03-31 02:15:17 +02:00
/* Skip \n\r\n */
2016-03-31 10:11:36 +02:00
if (fgetc (Gbl.F.Tmp) != 0x0A) break; // '\n'
if (fgetc (Gbl.F.Tmp) != 0x0D) break; // '\r'
if (fgetc (Gbl.F.Tmp) != 0x0A) break; // '\n'
2016-03-31 02:15:17 +02:00
/***** Get parameter value or file content *****/
2016-03-31 15:03:00 +02:00
CurPos = (unsigned long) ftell (Gbl.F.Tmp); // At start of value or file content
2016-04-01 03:09:45 +02:00
if (Str_ReadFileUntilBoundaryStr (Gbl.F.Tmp,NULL,
Gbl.Boundary.StrWithCRLF,
Gbl.Boundary.LengthWithCRLF,
Fil_MAX_FILE_SIZE) != 1) break; // Boundary string not found
2016-03-31 02:15:17 +02:00
// Delimiter string found
Param->Value.Start = CurPos;
2016-03-31 10:11:36 +02:00
CurPos = (unsigned long) ftell (Gbl.F.Tmp); // Just after delimiter string
2016-03-31 15:03:00 +02:00
Param->Value.Length = CurPos - Gbl.Boundary.LengthWithCRLF -
Param->Value.Start;
2016-03-31 02:15:17 +02:00
}
}
}
/*****************************************************************************/
/******************** Read from file until quote '\"' ************************/
/*****************************************************************************/
// Return last char read
static int Par_ReadTmpFileUntilQuote (void)
{
int Ch;
/***** Read until quote if found *****/
do
Ch = fgetc (Gbl.F.Tmp);
while (Ch != EOF && Ch != (int) '\"');
return Ch;
}
/*****************************************************************************/
/*************************** Read from file until \r *************************/
/*****************************************************************************/
// Return last char read
static int Par_ReadTmpFileUntilReturn (void)
{
int Ch;
/***** Read until \r if found *****/
do
Ch = fgetc (Gbl.F.Tmp);
while (Ch != EOF && Ch != 0x0D); // '\r'
return Ch;
2016-03-30 17:37:56 +02:00
}
2014-12-01 23:55:08 +01:00
2016-03-30 10:50:31 +02:00
/*****************************************************************************/
/***************** Free memory allocated for query string ********************/
/*****************************************************************************/
2016-03-30 16:33:08 +02:00
void Par_FreeParams (void)
2016-03-30 10:50:31 +02:00
{
2016-03-30 16:33:08 +02:00
struct Param *Param;
struct Param *NextParam;
/***** Free list of parameters *****/
for (Param = Gbl.Params.List;
Param != NULL;
Param = NextParam)
{
NextParam = Param->Next;
2019-11-06 19:45:20 +01:00
free (Param);
2016-03-30 16:33:08 +02:00
}
/***** Free query string *****/
2016-03-30 14:25:04 +02:00
if (Gbl.Params.QueryString)
2019-11-06 19:45:20 +01:00
free (Gbl.Params.QueryString);
2016-03-30 10:50:31 +02:00
}
2016-03-30 17:37:56 +02:00
/*****************************************************************************/
/************************* Get the value of a parameter **********************/
/*****************************************************************************/
// Return the number of parameters found
2016-04-01 09:46:09 +02:00
// If ParamPtr is not NULL, on return it will point to the first ocurrence in list of parameters
// ParamValue can be NULL (if so, no value is copied)
2016-03-30 17:37:56 +02:00
unsigned Par_GetParameter (tParamType ParamType,const char *ParamName,
2016-03-31 20:09:47 +02:00
char *ParamValue,size_t MaxBytes,
2016-04-01 01:59:27 +02:00
struct Param **ParamPtr) // NULL if not used
2016-03-30 17:37:56 +02:00
{
2019-11-03 13:19:32 +01:00
extern const char *Par_SEPARATOR_PARAM_MULTIPLE;
2016-03-30 17:37:56 +02:00
size_t BytesAlreadyCopied = 0;
unsigned i;
struct Param *Param;
char *PtrDst;
2016-03-31 19:36:30 +02:00
unsigned NumTimes;
2016-03-30 17:37:56 +02:00
bool ParamFound = false;
unsigned ParamNameLength;
2016-04-01 01:59:27 +02:00
bool FindMoreThanOneOcurrence;
2019-02-17 01:14:55 +01:00
char ErrorTxt[256];
2016-03-31 15:03:00 +02:00
2016-03-31 19:36:30 +02:00
/***** Default values returned *****/
2016-04-01 09:46:09 +02:00
if (ParamValue)
ParamValue[0] = '\0'; // By default, the value of the parameter will be an empty string
2016-03-31 15:03:00 +02:00
/***** Only some selected parameters can be passed by GET method *****/
if (Gbl.Params.GetMethod)
2016-03-31 19:04:13 +02:00
if (!Par_CheckIsParamCanBeUsedInGETMethod (ParamName))
return 0; // Return no-parameters-found
2016-03-31 15:03:00 +02:00
2016-03-31 19:36:30 +02:00
/***** Initializations *****/
2016-03-30 17:37:56 +02:00
ParamNameLength = strlen (ParamName);
2016-03-31 15:03:00 +02:00
PtrDst = ParamValue;
2016-04-01 01:59:27 +02:00
FindMoreThanOneOcurrence = (ParamType == Par_PARAM_MULTIPLE);
2016-03-31 15:03:00 +02:00
2016-03-31 19:36:30 +02:00
/***** For multiple parameters, loop for any ocurrence of the parameter
For unique parameter, find only the first ocurrence *****/
2016-03-31 15:03:00 +02:00
for (Param = Gbl.Params.List, NumTimes = 0;
2016-04-05 02:59:34 +02:00
Param != NULL && (FindMoreThanOneOcurrence || NumTimes == 0);
)
2016-03-31 15:03:00 +02:00
/***** Find next ocurrence of parameter in list of parameters *****/
for (ParamFound = false;
Param != NULL && !ParamFound;
Param = Param->Next)
{
if (Param->Name.Length == ParamNameLength)
2016-03-30 17:37:56 +02:00
{
2016-03-31 15:03:00 +02:00
// The current element in the list has the length of the searched parameter
// Check if the name of the parameter is the same
switch (Gbl.ContentReceivedByCGI)
2016-03-30 17:37:56 +02:00
{
2016-10-21 00:38:34 +02:00
case Act_CONT_NORM:
2016-03-31 15:03:00 +02:00
ParamFound = !strncmp (ParamName,&Gbl.Params.QueryString[Param->Name.Start],
Param->Name.Length);
break;
2016-10-21 00:38:34 +02:00
case Act_CONT_DATA:
2016-03-31 15:03:00 +02:00
fseek (Gbl.F.Tmp,Param->Name.Start,SEEK_SET);
for (i = 0, ParamFound = true;
i < Param->Name.Length && ParamFound;
i++)
2016-03-31 19:04:13 +02:00
if (ParamName[i] != (char) fgetc (Gbl.F.Tmp))
2016-03-31 15:03:00 +02:00
ParamFound = false;
break;
2016-03-30 17:37:56 +02:00
}
2016-03-31 15:03:00 +02:00
if (ParamFound)
{
2016-04-05 02:59:34 +02:00
NumTimes++;
if (NumTimes == 1) // NumTimes == 1 ==> the first ocurrence of this parameter
2016-03-31 15:03:00 +02:00
{
2016-03-31 20:09:47 +02:00
/***** Get the first ocurrence of this parameter in list *****/
if (ParamPtr)
2016-04-01 01:59:27 +02:00
*ParamPtr = Param;
2016-04-01 09:46:09 +02:00
/***** If this parameter is a file ==> do not find more ocurrences ******/
if (Param->FileName.Start != 0) // It's a file
FindMoreThanOneOcurrence = false;
2016-03-31 20:09:47 +02:00
}
2016-04-05 02:59:34 +02:00
else // NumTimes > 1 ==> not the first ocurrence of this parameter
2016-03-31 20:09:47 +02:00
{
/***** Add separator when param multiple *****/
2016-03-31 15:03:00 +02:00
/* Check if there is space to copy separator */
if (BytesAlreadyCopied + 1 > MaxBytes)
{
2019-02-17 01:14:55 +01:00
snprintf (ErrorTxt,sizeof (ErrorTxt),
2018-10-16 21:56:01 +02:00
"Multiple parameter <strong>%s</strong> too large,"
" it exceed the maximum allowed size (%lu bytes).",
ParamName,(unsigned long) MaxBytes);
2019-02-17 01:14:55 +01:00
Lay_ShowErrorAndExit (ErrorTxt);
2016-03-31 15:03:00 +02:00
}
/* Copy separator */
2016-04-01 09:46:09 +02:00
if (PtrDst)
2019-11-03 13:19:32 +01:00
*PtrDst++ = Par_SEPARATOR_PARAM_MULTIPLE[0]; // Separator in the destination string
2016-03-31 15:03:00 +02:00
BytesAlreadyCopied++;
}
/***** Copy parameter value *****/
if (Param->Value.Length)
{
/* Check if there is space to copy the parameter value */
2016-04-01 09:46:09 +02:00
if (BytesAlreadyCopied + Param->Value.Length > MaxBytes)
2016-03-31 15:03:00 +02:00
{
2019-02-17 01:14:55 +01:00
snprintf (ErrorTxt,sizeof (ErrorTxt),
2018-10-16 21:56:01 +02:00
"Parameter <strong>%s</strong> too large,"
" it exceed the maximum allowed size (%lu bytes).",
ParamName,(unsigned long) MaxBytes);
2019-02-17 01:14:55 +01:00
Lay_ShowErrorAndExit (ErrorTxt);
2016-03-31 15:03:00 +02:00
}
/* Copy parameter value */
switch (Gbl.ContentReceivedByCGI)
{
2016-10-21 00:38:34 +02:00
case Act_CONT_NORM:
2016-04-01 09:46:09 +02:00
if (PtrDst)
2017-01-17 03:10:43 +01:00
strncpy (PtrDst,&Gbl.Params.QueryString[Param->Value.Start],
Param->Value.Length);
2016-03-31 15:03:00 +02:00
break;
2016-10-21 00:38:34 +02:00
case Act_CONT_DATA:
2016-04-01 09:46:09 +02:00
if (Param->FileName.Start == 0 && // Copy into destination only if it's not a file
PtrDst)
{
fseek (Gbl.F.Tmp,Param->Value.Start,SEEK_SET);
2019-11-06 19:45:20 +01:00
if (fread (PtrDst,sizeof (char),Param->Value.Length,Gbl.F.Tmp) !=
2016-04-01 09:46:09 +02:00
Param->Value.Length)
Lay_ShowErrorAndExit ("Error while getting value of parameter.");
}
2016-03-31 15:03:00 +02:00
break;
}
2016-04-01 09:46:09 +02:00
BytesAlreadyCopied += Param->Value.Length;
if (PtrDst)
PtrDst += Param->Value.Length;
2016-03-31 15:03:00 +02:00
}
}
}
}
2016-03-30 17:37:56 +02:00
2016-04-01 09:46:09 +02:00
if (PtrDst)
*PtrDst = '\0'; // Add the final NULL
2016-03-31 15:03:00 +02:00
2016-03-30 17:37:56 +02:00
return NumTimes;
}
2016-03-31 19:04:13 +02:00
/*****************************************************************************/
/*************** Check if parameter can be used in GET method ****************/
/*****************************************************************************/
static bool Par_CheckIsParamCanBeUsedInGETMethod (const char *ParamName)
{
static const char *ValidParamsInGETMethod[] =
{
"cty", // To enter directly to a country
"ins", // To enter directly to an institution
"ctr", // To enter directly to a centre
"deg", // To enter directly to a degree
"crs", // To enter directly to a course
2016-12-03 23:48:03 +01:00
"usr", // To enter directly to a user's profile
2016-12-04 03:50:25 +01:00
"agd", // To view user's public agenda
2016-03-31 19:04:13 +02:00
"act", // To execute directly an action (allowed only for fully public actions)
"ses", // To use an open session when redirecting from one language to another
"key", // To verify an email address
};
#define NUM_VALID_PARAMS (sizeof (ValidParamsInGETMethod) / sizeof (ValidParamsInGETMethod[0]))
unsigned i;
for (i = 0;
i < NUM_VALID_PARAMS;
i++)
if (!strcmp (ParamName,ValidParamsInGETMethod[i]))
return true;
return false;
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/****************** Get the parameters sent to this CGI **********************/
/*****************************************************************************/
void Par_GetMainParameters (void)
{
2017-01-28 15:58:46 +01:00
extern Act_Action_t Act_FromActCodToAction[1 + Act_MAX_ACTION_COD];
2014-12-01 23:55:08 +01:00
extern const char *The_ThemeId[The_NUM_THEMES];
extern const char *Ico_IconSetId[Ico_NUM_ICON_SETS];
2017-01-30 01:08:25 +01:00
long ActCod;
char Nickname[Nck_MAX_BYTES_NICK_FROM_FORM + 1];
2019-03-20 01:36:36 +01:00
char URL[PATH_MAX + 1];
2019-11-08 01:10:32 +01:00
char LongStr[Cns_MAX_DECIMAL_DIGITS_LONG + 1];
2014-12-01 23:55:08 +01:00
2015-03-05 01:55:46 +01:00
/***** Reset codes of country, institution, centre, degree and course *****/
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Cty.CtyCod =
Gbl.Hierarchy.Ins.InsCod =
Gbl.Hierarchy.Ctr.CtrCod =
Gbl.Hierarchy.Deg.DegCod =
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod = -1L;
2014-12-01 23:55:08 +01:00
// First of all, get action, and session identifier.
// So, if other parameters have been stored in the database, there will be no problems to get them.
/***** Get action to perform *****/
if (Gbl.WebService.IsWebService)
{
2019-02-14 19:22:38 +01:00
Gbl.Action.Act = Gbl.Action.Original = ActWebSvc;
2015-01-04 15:48:24 +01:00
Tab_SetCurrentTab ();
2014-12-01 23:55:08 +01:00
return;
}
2016-10-09 22:22:47 +02:00
/***** Set default action *****/
2019-02-14 19:22:38 +01:00
Gbl.Action.Act = Gbl.Action.Original = ActUnk;
2016-01-14 01:39:02 +01:00
/***** Get another user's nickname, if exists
2016-01-26 09:47:35 +01:00
(this nickname is used to go to another user's profile,
2016-01-14 01:39:02 +01:00
not to get the logged user) *****/
if (Par_GetParToText ("usr",Nickname,Nck_MAX_BYTES_NICK_FROM_FORM))
2016-12-04 03:50:25 +01:00
{
2016-01-14 01:39:02 +01:00
if (Nickname[0])
2016-01-26 09:47:35 +01:00
{
2016-12-05 13:26:12 +01:00
/* Set another user's nickname */
Str_RemoveLeadingArrobas (Nickname);
2017-01-15 22:58:26 +01:00
Str_Copy (Gbl.Usrs.Other.UsrDat.Nickname,Nickname, // without arroba
sizeof (Gbl.Usrs.Other.UsrDat.Nickname) - 1);
2016-12-05 13:26:12 +01:00
2016-01-26 09:47:35 +01:00
// This user's code is used to go to public profile
// and to refresh old publishings in user's timeline
// If user does not exist ==> UsrCod = -1
2016-12-05 13:26:12 +01:00
Gbl.Usrs.Other.UsrDat.UsrCod = Nck_GetUsrCodFromNickname (Gbl.Usrs.Other.UsrDat.Nickname);
2019-02-14 19:22:38 +01:00
Gbl.Action.Act = Gbl.Action.Original = ActSeeOthPubPrf; // Set default action if no other is specified
2016-01-26 09:47:35 +01:00
}
2016-12-04 03:50:25 +01:00
}
else if (Par_GetParToText ("agd",Nickname,Nck_MAX_BYTES_NICK_FROM_FORM))
2016-12-04 03:50:25 +01:00
{
if (Nickname[0])
{
2016-12-05 13:26:12 +01:00
/* Set another user's nickname */
Str_RemoveLeadingArrobas (Nickname);
2017-01-15 22:58:26 +01:00
Str_Copy (Gbl.Usrs.Other.UsrDat.Nickname,Nickname, // without arroba
sizeof (Gbl.Usrs.Other.UsrDat.Nickname) - 1);
2016-12-05 13:26:12 +01:00
2016-12-04 03:50:25 +01:00
// This user's code is used to go to public agenda
// If user does not exist ==> UsrCod = -1
2016-12-05 13:26:12 +01:00
Gbl.Usrs.Other.UsrDat.UsrCod = Nck_GetUsrCodFromNickname (Gbl.Usrs.Other.UsrDat.Nickname);
2019-02-14 19:22:38 +01:00
Gbl.Action.Act = Gbl.Action.Original = ActFrmLogInUsrAgd; // Set default action if no other is specified
2016-12-04 03:50:25 +01:00
}
}
2016-01-14 01:39:02 +01:00
/***** Get action to perform *****/
2017-01-30 01:08:25 +01:00
ActCod = Par_GetParToLong ("act");
if (ActCod >= 0 && ActCod <= Act_MAX_ACTION_COD)
2019-02-14 19:22:38 +01:00
Gbl.Action.Act = Gbl.Action.Original = Act_FromActCodToAction[ActCod];
2016-12-04 03:50:25 +01:00
/***** Some preliminary adjusts depending on action *****/
2019-04-19 13:11:54 +02:00
switch (Act_GetBrowserTab (Gbl.Action.Act))
2017-02-05 22:23:41 +01:00
{
2019-04-19 13:11:54 +02:00
case Act_AJAX_NORMAL:
Gbl.Action.UsesAJAX = true;
Gbl.Action.IsAJAXAutoRefresh = false;
break;
case Act_AJAX_RFRESH:
Gbl.Action.UsesAJAX = true;
Gbl.Action.IsAJAXAutoRefresh = true;
break;
default:
Gbl.Action.UsesAJAX = false;
Gbl.Action.IsAJAXAutoRefresh = false;
break;
2017-02-05 22:23:41 +01:00
}
2014-12-01 23:55:08 +01:00
/***** Get session identifier, if exists *****/
2018-10-17 01:08:42 +02:00
Par_GetParToText ("ses",Gbl.Session.Id,Cns_BYTES_SESSION_ID);
2014-12-01 23:55:08 +01:00
if (Gbl.Session.Id[0])
{
/***** Get user's code, password, current degree and current course from stored session *****/
if (Ses_GetSessionData ())
Gbl.Session.IsOpen = true;
else
{
Gbl.Session.HasBeenDisconnected = true;
Gbl.Session.Id[0] = '\0';
}
}
2015-03-09 00:16:18 +01:00
else
{
// Try old parameter "IdSes" (allowed for compatibility, to be removed soon)
2018-10-17 01:08:42 +02:00
Par_GetParToText ("IdSes",Gbl.Session.Id,Cns_BYTES_SESSION_ID);
2015-03-09 00:16:18 +01:00
if (Gbl.Session.Id[0])
{
/***** Get user's code, password, current degree and current course from stored session *****/
if (Ses_GetSessionData ())
Gbl.Session.IsOpen = true;
else
{
Gbl.Session.HasBeenDisconnected = true;
Gbl.Session.Id[0] = '\0';
}
}
}
2014-12-01 23:55:08 +01:00
/***** Get user password and login *****/
2016-01-17 15:10:54 +01:00
switch (Gbl.Action.Act)
2014-12-01 23:55:08 +01:00
{
2016-12-05 13:26:12 +01:00
case ActLogIn:
2016-12-04 04:22:36 +01:00
case ActFrmLogInUsrAgd:
case ActLogInUsrAgd: // This action is necessary here when log in fails
2014-12-01 23:55:08 +01:00
Pwd_GetParamUsrPwdLogin ();
2018-10-04 21:57:25 +02:00
/* falls through */
/* no break */
2014-12-01 23:55:08 +01:00
case ActReqSndNewPwd:
case ActSndNewPwd:
Usr_GetParamUsrIdLogin ();
break;
}
2019-03-26 11:53:21 +01:00
/***** Try to get settings changed from current IP *****/
Set_GetSettingsFromIP ();
2014-12-01 23:55:08 +01:00
if (!Gbl.Session.IsOpen) // When no session open (no logged user)...
{
2019-03-26 11:53:21 +01:00
/***** Try to get settings changed from current IP *****/
2014-12-01 23:55:08 +01:00
if (Gbl.Prefs.Theme == The_THEME_UNKNOWN)
Gbl.Prefs.Theme = The_THEME_DEFAULT;
/***** Set path of theme and path of icon set *****/
snprintf (URL,sizeof (URL),"%s/%s",
Cfg_URL_ICON_THEMES_PUBLIC,The_ThemeId[Gbl.Prefs.Theme ]);
Str_Copy (Gbl.Prefs.URLTheme ,URL,sizeof (Gbl.Prefs.URLTheme ) - 1);
snprintf (URL,sizeof (URL),"%s/%s",
2019-03-20 01:36:36 +01:00
Cfg_URL_ICON_SETS_PUBLIC,Ico_IconSetId[Gbl.Prefs.IconSet]);
Str_Copy (Gbl.Prefs.URLIconSet,URL,sizeof (Gbl.Prefs.URLIconSet) - 1);
2014-12-01 23:55:08 +01:00
}
/***** Get country if exists (from menu) *****/
2019-11-08 01:10:32 +01:00
Par_GetParToText ("cty",LongStr,Cns_MAX_DECIMAL_DIGITS_LONG);
2015-03-07 21:08:44 +01:00
if (LongStr[0]) // Parameter "cty" available
2014-12-01 23:55:08 +01:00
{
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Cty.CtyCod = Str_ConvertStrCodToLongCod (LongStr);
Gbl.Hierarchy.Ins.InsCod =
Gbl.Hierarchy.Ctr.CtrCod =
Gbl.Hierarchy.Deg.DegCod =
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod = -1L;
2014-12-01 23:55:08 +01:00
}
/***** Get institution if exists (from menu) *****/
2019-11-08 01:10:32 +01:00
Par_GetParToText ("ins",LongStr,Cns_MAX_DECIMAL_DIGITS_LONG);
2015-03-07 21:08:44 +01:00
if (LongStr[0]) // Parameter "ins" available
2014-12-01 23:55:08 +01:00
{
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ins.InsCod = Str_ConvertStrCodToLongCod (LongStr);
Gbl.Hierarchy.Ctr.CtrCod =
Gbl.Hierarchy.Deg.DegCod =
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod = -1L;
2014-12-01 23:55:08 +01:00
}
/***** Get centre if exists (from menu) *****/
2019-11-08 01:10:32 +01:00
Par_GetParToText ("ctr",LongStr,Cns_MAX_DECIMAL_DIGITS_LONG);
2015-03-07 21:08:44 +01:00
if (LongStr[0]) // Parameter "ctr" available
2014-12-01 23:55:08 +01:00
{
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ctr.CtrCod = Str_ConvertStrCodToLongCod (LongStr);
Gbl.Hierarchy.Deg.DegCod =
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod = -1L;
2014-12-01 23:55:08 +01:00
}
/***** Get numerical degree code if exists (from menu) *****/
2019-11-08 01:10:32 +01:00
Par_GetParToText ("deg",LongStr,Cns_MAX_DECIMAL_DIGITS_LONG);
2015-03-07 21:08:44 +01:00
if (LongStr[0]) // Parameter "deg" available
2014-12-01 23:55:08 +01:00
{
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Deg.DegCod = Str_ConvertStrCodToLongCod (LongStr);
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod = -1L; // Reset possible course from session
2014-12-01 23:55:08 +01:00
}
/***** Get numerical course code if exists (from menu) *****/
2019-11-08 01:10:32 +01:00
Par_GetParToText ("crs",LongStr,Cns_MAX_DECIMAL_DIGITS_LONG);
2015-03-07 21:08:44 +01:00
if (LongStr[0]) // Parameter "crs" available
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod = Str_ConvertStrCodToLongCod (LongStr); // Overwrite CrsCod from session
2014-12-01 23:55:08 +01:00
/***** Get tab to activate *****/
2016-01-17 15:10:54 +01:00
Gbl.Action.Tab = TabUnk;
if (Gbl.Action.Act == ActMnu)
2014-12-01 23:55:08 +01:00
{
2017-01-29 21:41:08 +01:00
Gbl.Action.Tab = (Tab_Tab_t)
Par_GetParToUnsignedLong ("NxtTab",
(unsigned long) TabUnk,
Tab_NUM_TABS - 1,
(unsigned long) TabUnk);
2017-01-29 12:42:19 +01:00
Tab_DisableIncompatibleTabs ();
2014-12-01 23:55:08 +01:00
}
else // Set tab depending on current action
2015-01-04 15:48:24 +01:00
Tab_SetCurrentTab ();
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/******* Get the value of a single parameter and change format to text *******/
/*****************************************************************************/
// Return the number of parameters found
unsigned Par_GetParToText (const char *ParamName,char *ParamValue,size_t MaxBytes)
{
return Par_GetParAndChangeFormat (ParamName,ParamValue,MaxBytes,
Str_TO_TEXT,true);
}
2017-01-29 12:42:19 +01:00
/*****************************************************************************/
/****************** Get the unsigned value of a parameter ********************/
/*****************************************************************************/
2017-01-29 21:41:08 +01:00
unsigned long Par_GetParToUnsignedLong (const char *ParamName,
unsigned long Min,
unsigned long Max,
unsigned long Default)
2017-01-29 12:42:19 +01:00
{
2019-11-08 01:10:32 +01:00
char UnsignedLongStr[Cns_MAX_DECIMAL_DIGITS_ULONG + 1];
2017-01-29 21:41:08 +01:00
unsigned long UnsignedLongNum;
2017-01-29 12:42:19 +01:00
/***** Get parameter with unsigned number *****/
2019-11-08 01:10:32 +01:00
Par_GetParToText (ParamName,UnsignedLongStr,Cns_MAX_DECIMAL_DIGITS_ULONG);
2017-01-29 21:41:08 +01:00
if (sscanf (UnsignedLongStr,"%lu",&UnsignedLongNum) == 1)
if (UnsignedLongNum >= Min && UnsignedLongNum <= Max)
return UnsignedLongNum;
2017-01-29 12:42:19 +01:00
return Default;
}
2017-01-28 20:32:50 +01:00
/*****************************************************************************/
/******************** Get the long value of a parameter **********************/
/*****************************************************************************/
long Par_GetParToLong (const char *ParamName)
{
2019-11-08 01:10:32 +01:00
char LongStr[Cns_MAX_DECIMAL_DIGITS_LONG + 1];
2017-01-28 20:32:50 +01:00
/***** Get parameter with long number *****/
2019-11-08 01:10:32 +01:00
Par_GetParToText (ParamName,LongStr,Cns_MAX_DECIMAL_DIGITS_LONG);
2017-01-28 20:32:50 +01:00
return Str_ConvertStrCodToLongCod (LongStr);
}
/*****************************************************************************/
/************************** Get a boolean parameter **************************/
/*****************************************************************************/
bool Par_GetParToBool (const char *ParamName)
{
char YN[1 + 1];
/***** Get parameter "Y"/"N" and convert to boolean *****/
Par_GetParToText (ParamName,YN,1);
return (Str_ConvertToUpperLetter (YN[0]) == 'Y');
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/*** Get value of a single parameter and change format to not rigorous HTML **/
/*****************************************************************************/
// Return the number of parameters found
unsigned Par_GetParToHTML (const char *ParamName,char *ParamValue,size_t MaxBytes)
{
return Par_GetParAndChangeFormat (ParamName,ParamValue,MaxBytes,
Str_TO_HTML,true);
}
/*****************************************************************************/
/****** Get the value of a multiple parameter and change format to text ******/
/*****************************************************************************/
// Return the number of parameters found
unsigned Par_GetParMultiToText (const char *ParamName,char *ParamValue,size_t MaxBytes)
{
unsigned NumTimes = Par_GetParameter (Par_PARAM_MULTIPLE,ParamName,
2016-03-31 20:09:47 +02:00
ParamValue,MaxBytes,NULL);
2017-01-17 14:24:54 +01:00
2014-12-01 23:55:08 +01:00
Str_ChangeFormat (Str_FROM_FORM,Str_TO_TEXT,
ParamValue,MaxBytes,true);
return NumTimes;
}
/*****************************************************************************/
/*********** Get the value of a single parameter and change format ***********/
/*****************************************************************************/
// Return the number of parameters found
unsigned Par_GetParAndChangeFormat (const char *ParamName,char *ParamValue,size_t MaxBytes,
Str_ChangeTo_t ChangeTo,bool RemoveLeadingAndTrailingSpaces)
{
unsigned NumTimes = Par_GetParameter (Par_PARAM_SINGLE,ParamName,
2016-03-31 20:09:47 +02:00
ParamValue,MaxBytes,NULL);
2016-04-07 23:35:47 +02:00
2014-12-01 23:55:08 +01:00
Str_ChangeFormat (Str_FROM_FORM,ChangeTo,
ParamValue,MaxBytes,RemoveLeadingAndTrailingSpaces);
return NumTimes;
}
/*****************************************************************************/
/***** Search in the string StrSrc the next string until find separator ******/
/*****************************************************************************/
// Modifies *StrSrc
// When StrDst is NULL, nothing is stored
// Return true if characters found
bool Par_GetNextStrUntilSeparParamMult (const char **StrSrc,char *StrDst,size_t LongMax)
{
size_t i = 0;
unsigned char Ch; // Must be unsigned to work with characters > 127
bool CharsFound = false;
do
if ((Ch = (unsigned char) **StrSrc))
(*StrSrc)++;
2019-09-24 01:41:51 +02:00
while (Ch && Ch < 32); // Skip special characters
// (the separator will be a special character
// less than 32)
2014-12-01 23:55:08 +01:00
2019-09-24 01:41:51 +02:00
while (Ch >= 32) // Until special character or end
2014-12-01 23:55:08 +01:00
{
CharsFound = true;
if (i < LongMax)
if (StrDst)
StrDst[i++] = (char) Ch;
if ((Ch = (unsigned char) **StrSrc))
(*StrSrc)++;
}
if (StrDst)
StrDst[i] = '\0';
return CharsFound;
}
/*****************************************************************************/
2020-05-12 02:45:03 +02:00
/***** Search in the string StrSrc the next string until find separator ******/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2020-05-12 02:45:03 +02:00
// Modifies *StrSrc
// When StrDst is NULL, nothing is stored
// Return true if characters found
2014-12-01 23:55:08 +01:00
2020-05-12 02:45:03 +02:00
bool Par_GetNextStrUntilComma (const char **StrSrc,char *StrDst,size_t LongMax)
2014-12-01 23:55:08 +01:00
{
2020-05-12 02:45:03 +02:00
size_t i = 0;
unsigned char Ch; // Must be unsigned to work with characters > 127
bool CharsFound = false;
do
if ((Ch = (unsigned char) **StrSrc))
(*StrSrc)++;
while (Ch == ','); // Skip commas
while (Ch && Ch != ',') // Until special character or end
{
CharsFound = true;
if (i < LongMax)
if (StrDst)
StrDst[i++] = (char) Ch;
if ((Ch = (unsigned char) **StrSrc))
(*StrSrc)++;
}
if (StrDst)
StrDst[i] = '\0';
return CharsFound;
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/******** Replace each comma by the separator of multiple parameters *********/
/*****************************************************************************/
2020-05-12 02:45:03 +02:00
void Par_ReplaceSeparatorMultipleByComma (char *Str)
2014-12-01 23:55:08 +01:00
{
for (;
*Str;
Str++)
2020-05-12 02:45:03 +02:00
if ((unsigned char) *Str < 32)
*Str = ',';
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/********************** Put an unsigned hidden parameter *********************/
/*****************************************************************************/
2019-11-03 13:19:32 +01:00
void Par_PutHiddenParamUnsigned (const char *Id,const char *ParamName,unsigned Value)
2014-12-01 23:55:08 +01:00
{
2019-11-11 00:15:44 +01:00
HTM_Txt ("<input type=\"hidden\"");
2019-11-03 13:19:32 +01:00
if (Id)
if (Id[0])
2019-11-11 00:15:44 +01:00
HTM_TxtF (" id=\"%s\"",Id);
HTM_TxtF (" name=\"%s\" value=\"%u\" />",ParamName,Value);
2019-11-03 13:19:32 +01:00
}
void Par_PutHiddenParamUnsignedDisabled (const char *Id,const char *ParamName,unsigned Value)
{
2019-11-11 00:15:44 +01:00
HTM_Txt ("<input type=\"hidden\"");
2019-11-03 13:19:32 +01:00
if (Id)
if (Id[0])
2019-11-11 00:15:44 +01:00
HTM_TxtF (" id=\"%s\"",Id);
HTM_TxtF (" name=\"%s\" value=\"%u\" disabled=\"disabled\" />",
ParamName,Value);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/************************* Put a long hidden parameter ***********************/
/*****************************************************************************/
2019-11-03 13:19:32 +01:00
void Par_PutHiddenParamLong (const char *Id,const char *ParamName,long Value)
2014-12-01 23:55:08 +01:00
{
2019-11-11 00:15:44 +01:00
HTM_Txt ("<input type=\"hidden\"");
2019-11-03 13:19:32 +01:00
if (Id)
if (Id[0])
2019-11-11 00:15:44 +01:00
HTM_TxtF (" id=\"%s\"",Id);
HTM_TxtF (" name=\"%s\" value=\"%ld\" />",ParamName,Value);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/************************* Put a char hidden parameter ***********************/
/*****************************************************************************/
void Par_PutHiddenParamChar (const char *ParamName,char Value)
{
2019-11-11 00:15:44 +01:00
HTM_TxtF ("<input type=\"hidden\" name=\"%s\" value=\"%c\" />",
ParamName,Value);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/************************ Put a string hidden parameter **********************/
/*****************************************************************************/
2020-03-26 21:39:44 +01:00
void Par_PutHiddenParamString (const char *Id,const char *ParamName,
const char *Value)
2014-12-01 23:55:08 +01:00
{
2019-11-11 00:15:44 +01:00
HTM_Txt ("<input type=\"hidden\"");
2019-11-03 13:19:32 +01:00
if (Id)
if (Id[0])
2019-11-11 00:15:44 +01:00
HTM_TxtF (" id=\"%s\"",Id);
2020-03-26 21:39:44 +01:00
HTM_TxtF (" name=\"%s\" value=\"%s\" />",
ParamName,Value ? Value :
"");
2014-12-01 23:55:08 +01:00
}