Version 22.50.6: Oct 20, 2022 Code refactoring in syllabus.

This commit is contained in:
acanas 2022-10-21 08:46:07 +02:00
parent bb93618e1f
commit 611668070c
9 changed files with 107 additions and 58 deletions

View File

@ -516,7 +516,7 @@ static int API_GenerateNewAPIKey (struct soap *soap,
API_DB_RemoveOldAPIKeys ();
/***** Create a unique name for the key *****/
Str_Copy (APIKey,Gbl.UniqueNameEncrypted,API_BYTES_KEY);
Str_Copy (APIKey,Cry_GetUniqueNameEncrypted (),API_BYTES_KEY);
/***** Check that key does not exist in database *****/
if (API_DB_CheckIfAPIKeyExists (APIKey))
@ -1334,7 +1334,7 @@ static int API_WriteSyllabusIntoHTMLBuffer (struct soap *soap,
{
/***** Create a unique name for the file *****/
snprintf (FileNameHTMLTmp,sizeof (FileNameHTMLTmp),"%s/%s_syllabus.html",
Cfg_PATH_OUT_PRIVATE,Gbl.UniqueNameEncrypted);
Cfg_PATH_OUT_PRIVATE,Cry_GetUniqueNameEncrypted ());
/***** Create a new temporary file for writing and reading *****/
if ((FileHTMLTmp = fopen (FileNameHTMLTmp,"w+b")) == NULL)
@ -1411,7 +1411,7 @@ static int API_WritePlainTextIntoHTMLBuffer (struct soap *soap,
{
/***** Create a unique name for the file *****/
snprintf (FileNameHTMLTmp,sizeof (FileNameHTMLTmp),"%s/%s_info.html",
Cfg_PATH_OUT_PRIVATE,Gbl.UniqueNameEncrypted);
Cfg_PATH_OUT_PRIVATE,Cry_GetUniqueNameEncrypted ());
/***** Create a new temporary file for writing and reading *****/
if ((FileHTMLTmp = fopen (FileNameHTMLTmp,"w+b")) == NULL)
@ -4705,7 +4705,7 @@ int swad__getDirectoryTree (struct soap *soap,
/* Create a unique name for the file */
snprintf (XMLFileName,sizeof (XMLFileName),"%s/%s.xml",
Cfg_PATH_OUT_PRIVATE,Gbl.UniqueNameEncrypted);
Cfg_PATH_OUT_PRIVATE,Cry_GetUniqueNameEncrypted ());
/* Open file for writing and reading */
if ((Gbl.F.XML = fopen (XMLFileName,"w+t")) == NULL)

View File

@ -4105,6 +4105,7 @@ void Brw_CreateDirDownloadTmp (void)
// This happens when the trees of assignments and works of several users are being listed
char PathUniqueDirL[PATH_MAX + 1];
char PathUniqueDirR[PATH_MAX + 1 + NAME_MAX + 1];
const char *UniqueNameEncrypted = Cry_GetUniqueNameEncrypted ();
/* Example: /var/www/html/swad/tmp/SS/ujCNWsy4ZOdmgMKYBe0sKPAJu6szaZOQlIlJs_QIY */
@ -4116,15 +4117,15 @@ void Brw_CreateDirDownloadTmp (void)
is limited to 32K in Linux ==> create directories in two levels *****/
/* 1. Build the name of the directory, splitted in two parts: */
/* 1a: 2 leftmost chars */
Gbl.FileBrowser.TmpPubDir.L[0] = Gbl.UniqueNameEncrypted[0];
Gbl.FileBrowser.TmpPubDir.L[1] = Gbl.UniqueNameEncrypted[1];
Gbl.FileBrowser.TmpPubDir.L[0] = UniqueNameEncrypted[0];
Gbl.FileBrowser.TmpPubDir.L[1] = UniqueNameEncrypted[1];
Gbl.FileBrowser.TmpPubDir.L[2] = '\0';
/* 1b: rest of chars */
if (NumDir)
snprintf (Gbl.FileBrowser.TmpPubDir.R,sizeof (Gbl.FileBrowser.TmpPubDir.R),
"%s_%u",&Gbl.UniqueNameEncrypted[2],NumDir);
"%s_%u",&UniqueNameEncrypted[2],NumDir);
else
Str_Copy (Gbl.FileBrowser.TmpPubDir.R,&Gbl.UniqueNameEncrypted[2],
Str_Copy (Gbl.FileBrowser.TmpPubDir.R,&UniqueNameEncrypted[2],
sizeof (Gbl.FileBrowser.TmpPubDir.R) - 1);
/* 2. Create the left directory */

View File

@ -605,10 +605,18 @@ TODO: Fix bug: error al enviar un mensaje a dos recipientes, error on duplicate
TODO: Attach pdf files in multimedia.
*/
#define Log_PLATFORM_VERSION "SWAD 22.50.5 (2022-10-20)"
#define Log_PLATFORM_VERSION "SWAD 22.50.6 (2022-10-21)"
#define CSS_FILE "swad22.49.4.css"
#define JS_FILE "swad22.49.js"
/*
Sugerencia de J.L Bernier. Al eliminar los estudiantes, que se eliminen los trabajos opcionalmente marcando una casilla.
En Actividades, no permitir acceso a documentos mientras dura la actividad, también opcionalmente.
Que no haya dos sesiones del mismo usuario abiertas al entregar actividades, o hacer algo como en los exámenes, log.
Que al subir un fichero por defecto esté oculto, aunque sea opcional.
Al subir cosas para los grupos, en documentos, resltar más los grupos porque no son conscientes...
Exportar listas en CSV.
Version 22.50.6: Oct 20, 2022 Code refactoring in syllabus. (333492 lines)
Version 22.50.5: Oct 20, 2022 Code refactoring in configuration. (333447 lines)
Version 22.50.4: Oct 20, 2022 Code refactoring in email sending. (333438 lines)
Version 22.50.3: Oct 19, 2022 Fixed bug in keys when playing matches. (333456 lines)

View File

@ -33,6 +33,18 @@
#include "swad_cryptography.h"
#include "swad_global.h"
/*****************************************************************************/
/**************************** Private constants ******************************/
/*****************************************************************************/
/***** SHA-256 algorithm *****/
#define BITS_SHA256_ENCRYPTION 256
#define BYTES_SHA256_ENCRYPTION (BITS_SHA256_ENCRYPTION/8)
/***** SHA-512 algorithm *****/
#define BITS_SHA512_ENCRYPTION 512
#define BYTES_SHA512_ENCRYPTION (BITS_SHA512_ENCRYPTION/8)
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
@ -44,15 +56,7 @@ extern const char Str_BIN_TO_BASE64URL[64 + 1];
/************************* Private global variables **************************/
/*****************************************************************************/
/***** SHA-256 algorithm *****/
#define BITS_SHA256_ENCRYPTION 256
#define BYTES_SHA256_ENCRYPTION (BITS_SHA256_ENCRYPTION/8)
/***** SHA-512 algorithm *****/
#define BITS_SHA512_ENCRYPTION 512
#define BYTES_SHA512_ENCRYPTION (BITS_SHA512_ENCRYPTION/8)
static char Cry_UniqueNameEncrypted[Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64 + 1]; // Used for session id, temporary directory names, etc.
/*****************************************************************************/
/*************** Encrypt a plain text using SHA=256 algorithm ****************/
@ -129,10 +133,21 @@ void Cry_EncryptSHA512Base64 (const char *PlainText,
void Cry_CreateUniqueNameEncrypted (char UniqueNameEncrypted[Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64 + 1])
{
static unsigned NumCall = 0; // When this function is called several times in the same execution of the program, each time a new name is created
char UniqueNamePlain[Cns_MAX_BYTES_IP + Cns_MAX_DECIMAL_DIGITS_LONG + Cns_MAX_DECIMAL_DIGITS_LONG + Cns_MAX_DECIMAL_DIGITS_UINT + 1];
char UniqueNamePlain[Cns_MAX_BYTES_IP +
Cns_MAX_DECIMAL_DIGITS_LONG +
Cns_MAX_DECIMAL_DIGITS_LONG +
Cns_MAX_DECIMAL_DIGITS_UINT + 1];
NumCall++;
snprintf (UniqueNamePlain,sizeof (UniqueNamePlain),"%s-%lx-%x-%x",
Gbl.IP,Dat_GetStartExecutionTimeUTC (),Gbl.PID,NumCall);
snprintf (UniqueNamePlain,sizeof (UniqueNamePlain),"%s-%lx-%lx-%x",
Gbl.IP,
(long) Dat_GetStartExecutionTimeUTC (),
(long) Gbl.PID,
NumCall);
Cry_EncryptSHA256Base64 (UniqueNamePlain,UniqueNameEncrypted); // Make difficult to guess a unique name
}
const char *Cry_GetUniqueNameEncrypted (void)
{
return Cry_UniqueNameEncrypted;
}

View File

@ -48,5 +48,6 @@ void Cry_EncryptSHA512Base64 (const char *PlainText,
char EncryptedText[Cry_LENGTH_ENCRYPTED_STR_SHA512_BASE64 + 1]);
void Cry_CreateUniqueNameEncrypted (char UniqueNameEncrypted[Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64 + 1]);
const char *Cry_GetUniqueNameEncrypted (void);
#endif

View File

@ -53,7 +53,6 @@ struct Globals
struct Fil_Files F;
pid_t PID; // PID of current process
char IP[Cns_MAX_BYTES_IP + 1];
char UniqueNameEncrypted[Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64 + 1]; // Used for session id, temporary directory names, etc.
struct
{
bool WritingHTMLStart; // Used to avoid writing the HTML head when aborting program on error
@ -61,7 +60,6 @@ struct Globals
bool DivsEndWritten; // Used to avoid writing more than once the HTML end
bool HTMLEndWritten; // Used to avoid writing more than once the HTML end
} Layout;
struct
{
Lan_Language_t Language;

View File

@ -1370,29 +1370,37 @@ void Lay_RefreshNotifsAndConnected (void)
if (!(Gbl.PID % 11))
Ntf_SendPendingNotifByEMailToAllUsrs (); // Send pending notifications by email
else if (!(Gbl.PID % 19))
Fir_DB_PurgeFirewallLog (); // Remove old clicks from firewall
Fir_DB_PurgeFirewallLog (); // Remove old clicks from firewall
else if (!(Gbl.PID % 23))
Fil_RemoveOldTmpFiles (Cfg_PATH_FILE_BROWSER_TMP_PUBLIC ,Cfg_TIME_TO_DELETE_BROWSER_TMP_FILES ,false); // Remove the oldest temporary public directories used for downloading
Fil_RemoveOldTmpFiles (Cfg_PATH_FILE_BROWSER_TMP_PUBLIC,
Cfg_TIME_TO_DELETE_BROWSER_TMP_FILES,false); // Remove the oldest temporary public directories used for downloading
else if (!(Gbl.PID % 101))
Brw_DB_RemoveExpiredExpandedFolders (); // Remove old expanded folders (from all users)
else if (!(Gbl.PID % 103))
Set_DB_RemoveOldSettingsFromIP (); // Remove old settings from IP
Set_DB_RemoveOldSettingsFromIP (); // Remove old settings from IP
else if (!(Gbl.PID % 107))
Log_DB_RemoveOldEntriesRecentLog (); // Remove old entries in recent log table, it's a slow query
else if (!(Gbl.PID % 109))
Fil_RemoveOldTmpFiles (Cfg_PATH_OUT_PRIVATE ,Cfg_TIME_TO_DELETE_HTML_OUTPUT ,false);
Fil_RemoveOldTmpFiles (Cfg_PATH_OUT_PRIVATE,
Cfg_TIME_TO_DELETE_HTML_OUTPUT ,false);
else if (!(Gbl.PID % 113))
Fil_RemoveOldTmpFiles (Cfg_PATH_PHOTO_TMP_PUBLIC ,Cfg_TIME_TO_DELETE_PHOTOS_TMP_FILES ,false);
Fil_RemoveOldTmpFiles (Cfg_PATH_PHOTO_TMP_PUBLIC,
Cfg_TIME_TO_DELETE_PHOTOS_TMP_FILES ,false);
else if (!(Gbl.PID % 127))
Fil_RemoveOldTmpFiles (Cfg_PATH_PHOTO_TMP_PRIVATE ,Cfg_TIME_TO_DELETE_PHOTOS_TMP_FILES ,false);
Fil_RemoveOldTmpFiles (Cfg_PATH_PHOTO_TMP_PRIVATE,
Cfg_TIME_TO_DELETE_PHOTOS_TMP_FILES ,false);
else if (!(Gbl.PID % 131))
Fil_RemoveOldTmpFiles (Cfg_PATH_MEDIA_TMP_PRIVATE ,Cfg_TIME_TO_DELETE_MEDIA_TMP_FILES ,false);
Fil_RemoveOldTmpFiles (Cfg_PATH_MEDIA_TMP_PRIVATE,
Cfg_TIME_TO_DELETE_MEDIA_TMP_FILES ,false);
else if (!(Gbl.PID % 137))
Fil_RemoveOldTmpFiles (Cfg_PATH_ZIP_PRIVATE ,Cfg_TIME_TO_DELETE_BROWSER_ZIP_FILES ,false);
Fil_RemoveOldTmpFiles (Cfg_PATH_ZIP_PRIVATE,
Cfg_TIME_TO_DELETE_BROWSER_ZIP_FILES,false);
else if (!(Gbl.PID % 139))
Fil_RemoveOldTmpFiles (Cfg_PATH_MARK_PRIVATE ,Cfg_TIME_TO_DELETE_MARKS_TMP_FILES ,false);
Fil_RemoveOldTmpFiles (Cfg_PATH_MARK_PRIVATE,
Cfg_TIME_TO_DELETE_MARKS_TMP_FILES ,false);
else if (!(Gbl.PID % 149))
Fil_RemoveOldTmpFiles (Cfg_PATH_TEST_PRIVATE ,Cfg_TIME_TO_DELETE_TEST_TMP_FILES ,false);
Fil_RemoveOldTmpFiles (Cfg_PATH_TEST_PRIVATE,
Cfg_TIME_TO_DELETE_TEST_TMP_FILES ,false);
/***** Send, before the HTML, the refresh time *****/
HTM_TxtF ("%lu|",Gbl.Usrs.Connected.TimeToRefreshInMs);

View File

@ -123,7 +123,9 @@ static void Syl_ChangePlaceItemSyllabus (Syl_ChangePosItem_t UpOrDownPos);
static void Syl_ChangeLevelItemSyllabus (Syl_ChangeLevelItem_t IncreaseOrDecreaseLevel);
static void Syl_OpenSyllabusFile (const struct Syl_Syllabus *Syllabus,
char PathFile[PATH_MAX + 1]);
char PathFile[PATH_MAX + 1],
FILE **XML);
static void Syl_CloseXMLFile (FILE **XML);
/*****************************************************************************/
/************************** Reset syllabus context ***************************/
@ -347,6 +349,7 @@ void Syl_LoadListItemsSyllabusIntoMemory (struct Syl_Syllabus *Syllabus,
long CrsCod)
{
char PathFile[PATH_MAX + 1];
FILE *XML = NULL; // XML file for syllabus
long PostBeginList;
unsigned NumItem = 0;
int N;
@ -361,18 +364,18 @@ void Syl_LoadListItemsSyllabusIntoMemory (struct Syl_Syllabus *Syllabus,
Cfg_SYLLABUS_FOLDER_PRACTICALS);
/***** Open the file with the syllabus *****/
Syl_OpenSyllabusFile (Syllabus,PathFile);
Syl_OpenSyllabusFile (Syllabus,PathFile,&XML);
/***** Go to the start of the list of items *****/
if (!Str_FindStrInFile (Gbl.F.XML,"<lista>",Str_NO_SKIP_HTML_COMMENTS))
if (!Str_FindStrInFile (XML,"<lista>",Str_NO_SKIP_HTML_COMMENTS))
Err_WrongSyllabusFormatExit ();
/***** Save the position of the start of the list *****/
PostBeginList = ftell (Gbl.F.XML);
PostBeginList = ftell (XML);
/***** Loop to count the number of items *****/
for (Syl_LstItemsSyllabus.NumItems = 0;
Str_FindStrInFile (Gbl.F.XML,"<item",Str_NO_SKIP_HTML_COMMENTS);
Str_FindStrInFile (XML,"<item",Str_NO_SKIP_HTML_COMMENTS);
Syl_LstItemsSyllabus.NumItems++);
/***** Allocate memory for the list of items *****/
@ -381,7 +384,7 @@ void Syl_LoadListItemsSyllabusIntoMemory (struct Syl_Syllabus *Syllabus,
Err_NotEnoughMemoryExit ();
/***** Return to the start of the list *****/
fseek (Gbl.F.XML,PostBeginList,SEEK_SET);
fseek (XML,PostBeginList,SEEK_SET);
for (N = 1;
N <= Syl_MAX_LEVELS_SYLLABUS;
@ -412,11 +415,11 @@ void Syl_LoadListItemsSyllabusIntoMemory (struct Syl_Syllabus *Syllabus,
NumItem++)
{
/* Go to the start of the item */
if (!Str_FindStrInFile (Gbl.F.XML,"<item",Str_NO_SKIP_HTML_COMMENTS))
if (!Str_FindStrInFile (XML,"<item",Str_NO_SKIP_HTML_COMMENTS))
Err_WrongSyllabusFormatExit ();
/* Get the level */
Syl_LstItemsSyllabus.Lst[NumItem].Level = Syl_ReadLevelItemSyllabus ();
Syl_LstItemsSyllabus.Lst[NumItem].Level = Syl_ReadLevelItemSyllabus (XML);
if (Syl_LstItemsSyllabus.Lst[NumItem].Level > Syl_LstItemsSyllabus.NumLevels)
Syl_LstItemsSyllabus.NumLevels = Syl_LstItemsSyllabus.Lst[NumItem].Level;
@ -432,12 +435,12 @@ void Syl_LoadListItemsSyllabusIntoMemory (struct Syl_Syllabus *Syllabus,
Syl_LstItemsSyllabus.Lst[NumItem].CodItem[N] = CodItem[N];
/* Get the text of the item */
Result = Str_ReadFileUntilBoundaryStr (Gbl.F.XML,Syl_LstItemsSyllabus.Lst[NumItem].Text,
Result = Str_ReadFileUntilBoundaryStr (XML,Syl_LstItemsSyllabus.Lst[NumItem].Text,
"</item>",strlen ("</item>"),
(unsigned long long) Syl_MAX_BYTES_TEXT_ITEM);
if (Result == 0) // Str too long
{
if (!Str_FindStrInFile (Gbl.F.XML,"</item>",Str_NO_SKIP_HTML_COMMENTS)) // End the search
if (!Str_FindStrInFile (XML,"</item>",Str_NO_SKIP_HTML_COMMENTS)) // End the search
Err_WrongSyllabusFormatExit ();
}
else if (Result == -1)
@ -445,7 +448,7 @@ void Syl_LoadListItemsSyllabusIntoMemory (struct Syl_Syllabus *Syllabus,
}
/***** Close the file with the syllabus *****/
Fil_CloseXMLFile ();
Syl_CloseXMLFile (&XML);
/***** Initialize other fields in the list *****/
if (Syl_LstItemsSyllabus.NumItems)
@ -485,19 +488,19 @@ void Syl_FreeListItemsSyllabus (void)
// XML file with the syllabus must be positioned after <item
// XML with the syllabus becomes positioned after <item nivel="x">
int Syl_ReadLevelItemSyllabus (void)
int Syl_ReadLevelItemSyllabus (FILE *XML)
{
int Level;
char StrlLevel[11 + 1];
if (!Str_FindStrInFile (Gbl.F.XML,"nivel=\"",Str_NO_SKIP_HTML_COMMENTS))
if (!Str_FindStrInFile (XML,"nivel=\"",Str_NO_SKIP_HTML_COMMENTS))
Err_WrongSyllabusFormatExit ();
if (Str_ReadFileUntilBoundaryStr (Gbl.F.XML,StrlLevel,"\"",1,
if (Str_ReadFileUntilBoundaryStr (XML,StrlLevel,"\"",1,
(unsigned long long) (11 + 1)) != 1)
Err_WrongSyllabusFormatExit ();
if (sscanf (StrlLevel,"%d",&Level) != 1)
Err_WrongSyllabusFormatExit ();
Str_FindStrInFile (Gbl.F.XML,">",Str_NO_SKIP_HTML_COMMENTS);
Str_FindStrInFile (XML,">",Str_NO_SKIP_HTML_COMMENTS);
if (Level < 1)
Level = 1;
else if (Level > Syl_MAX_LEVELS_SYLLABUS)
@ -1371,16 +1374,17 @@ void Syl_BuildPathFileSyllabus (const struct Syl_Syllabus *Syllabus,
/*****************************************************************************/
static void Syl_OpenSyllabusFile (const struct Syl_Syllabus *Syllabus,
char PathFile[PATH_MAX + 1])
char PathFile[PATH_MAX + 1],
FILE **XML)
{
if (Gbl.F.XML == NULL) // If it's not open in this moment...
if (*XML == NULL) // If it's not open in this moment...
{
/* If the directory does not exist, create it */
Fil_CreateDirIfNotExists (Syllabus->PathDir);
/* Open the file for reading */
Syl_BuildPathFileSyllabus (Syllabus,PathFile);
if ((Gbl.F.XML = fopen (PathFile,"rb")) == NULL)
if ((*XML = fopen (PathFile,"rb")) == NULL)
{
/* Can't open the file */
if (!Fil_CheckIfPathExists (Syllabus->PathDir)) // Strange error, since it is just created
@ -1388,19 +1392,33 @@ static void Syl_OpenSyllabusFile (const struct Syl_Syllabus *Syllabus,
else
{
/* Create a new empty syllabus */
if ((Gbl.F.XML = fopen (PathFile,"wb")) == NULL)
if ((*XML = fopen (PathFile,"wb")) == NULL)
Err_ShowErrorAndExit ("Can not create syllabus file.");
Syl_WriteStartFileSyllabus (Gbl.F.XML);
Syl_WriteEndFileSyllabus (Gbl.F.XML);
Fil_CloseXMLFile ();
Syl_WriteStartFileSyllabus (*XML);
Syl_WriteEndFileSyllabus (*XML);
Syl_CloseXMLFile (XML);
/* Open of new the file for reading */
if ((Gbl.F.XML = fopen (PathFile,"rb")) == NULL)
if ((*XML = fopen (PathFile,"rb")) == NULL)
Err_ShowErrorAndExit ("Can not open syllabus file.");
}
}
}
else // Go to the start of the file
rewind (Gbl.F.XML);
rewind (*XML);
}
/*****************************************************************************/
/**************************** Close XML file *********************************/
/*****************************************************************************/
static void Syl_CloseXMLFile (FILE **XML)
{
if (*XML)
{
fclose (*XML);
*XML = NULL; // To indicate that it is not open
}
}
/*****************************************************************************/

View File

@ -91,7 +91,7 @@ void Syl_LoadListItemsSyllabusIntoMemory (struct Syl_Syllabus *Syllabus,
long CrsCod);
void Syl_FreeListItemsSyllabus (void);
int Syl_ReadLevelItemSyllabus (void);
int Syl_ReadLevelItemSyllabus (FILE *XML);
void Syl_WriteSyllabusIntoHTMLTmpFile (FILE *FileHTMLTmp);