diff --git a/Makefile b/Makefile index 4ad050100..eae1adafb 100644 --- a/Makefile +++ b/Makefile @@ -61,10 +61,10 @@ OBJS = swad_account.o swad_account_database.o swad_action.o swad_admin.o \ swad_institution_database.o \ swad_language.o swad_layout.o swad_link.o swad_link_database.o \ swad_log.o swad_log_database.o swad_logo.o \ - swad_MAC.o swad_mail.o swad_main.o swad_maintenance.o swad_map.o \ - swad_mark.o swad_match.o swad_match_print.o swad_match_result.o \ - swad_media.o swad_media_database.o swad_menu.o swad_message.o \ - swad_MFU.o \ + swad_MAC.o swad_mail.o swad_mail_database.o swad_main.o \ + swad_maintenance.o swad_map.o swad_mark.o swad_match.o \ + swad_match_print.o swad_match_result.o swad_media.o \ + swad_media_database.o swad_menu.o swad_message.o swad_MFU.o \ swad_network.o swad_nickname.o swad_notice.o swad_notification.o \ swad_notification_database.o swad_pagination.o swad_parameter.o \ swad_password.o swad_photo.o swad_place.o swad_plugin.o swad_privacy.o \ diff --git a/swad_changelog.h b/swad_changelog.h index 096b0c1a1..fb1b2949a 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo. */ -#define Log_PLATFORM_VERSION "SWAD 21.13 (2021-09-22)" +#define Log_PLATFORM_VERSION "SWAD 21.14 (2021-09-22)" #define CSS_FILE "swad20.45.css" #define JS_FILE "swad20.69.1.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 21.14: Sep 22, 2021 New module swad_mail_database for database queries related to mail domains. (316594 lines) Version 21.13: Sep 22, 2021 New module swad_log_database for database queries related to access logs. (316510 lines) Version 21.12: Sep 22, 2021 New module swad_link_database for database queries related to institutional links. (316361 lines) Version 21.11.1: Sep 22, 2021 Queries moved to module swad_info_database. (316237 lines) diff --git a/swad_info_database.c b/swad_info_database.c index 7fa3bf28e..a5732e0ff 100644 --- a/swad_info_database.c +++ b/swad_info_database.c @@ -25,24 +25,10 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ -// #include // For maximum values -// #include // For PATH_MAX, NAME_MAX -// #include // For NULL -// #include // For getenv, etc -// #include // For SOAP_OK and soap functions #include // For string functions -// #include // For unlink -// #include "swad_action.h" -// #include "swad_box.h" #include "swad_database.h" -// #include "swad_error.h" -// #include "swad_form.h" #include "swad_global.h" -// #include "swad_HTML.h" -// #include "swad_info.h" -// #include "swad_parameter.h" -// #include "swad_string.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_info_database.h b/swad_info_database.h index ecf5749e8..5edbeb79c 100644 --- a/swad_info_database.h +++ b/swad_info_database.h @@ -27,11 +27,7 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ -// #include // For boolean type - -// #include "swad_constant.h" #include "swad_info.h" -// #include "swad_syllabus.h" /*****************************************************************************/ /***************************** Public constants ******************************/ diff --git a/swad_link_database.c b/swad_link_database.c index 84eb542e5..505fd20a4 100644 --- a/swad_link_database.c +++ b/swad_link_database.c @@ -25,29 +25,13 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ -// #include // For NULL -// #include // For calloc -// #include // For string functions - -// #include "swad_banner.h" -// #include "swad_box.h" -// #include "swad_constant.h" #include "swad_database.h" -// #include "swad_error.h" -// #include "swad_form.h" -// #include "swad_global.h" -// #include "swad_HTML.h" -// #include "swad_language.h" -// #include "swad_link.h" #include "swad_link_database.h" -// #include "swad_parameter.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ /*****************************************************************************/ -// extern struct Globals Gbl; - /*****************************************************************************/ /***************************** Private constants *****************************/ /*****************************************************************************/ diff --git a/swad_log_database.c b/swad_log_database.c index faa2f24d8..910267428 100644 --- a/swad_log_database.c +++ b/swad_log_database.c @@ -25,24 +25,10 @@ /*********************************** Headers *********************************/ /*****************************************************************************/ -// #include // For free -// #include // For strlen - -// #include "swad_action.h" -// #include "swad_banner.h" -// #include "swad_center_database.h" #include "swad_config.h" #include "swad_database.h" -// #include "swad_degree_database.h" -// #include "swad_exam_log.h" #include "swad_global.h" -// #include "swad_hierarchy.h" -// #include "swad_HTML.h" -// #include "swad_institution_database.h" #include "swad_log.h" -// #include "swad_profile.h" -// #include "swad_role.h" -// #include "swad_statistic.h" /*****************************************************************************/ /****************************** Public constants *****************************/ diff --git a/swad_mail.c b/swad_mail.c index 842aea9bf..2575f374a 100644 --- a/swad_mail.c +++ b/swad_mail.c @@ -40,6 +40,7 @@ #include "swad_HTML.h" #include "swad_language.h" #include "swad_mail.h" +#include "swad_mail_database.h" #include "swad_parameter.h" #include "swad_QR.h" #include "swad_tab.h" @@ -77,19 +78,14 @@ static void Mai_EditMailDomainsInternal (void); static void Mai_GetListMailDomainsAllowedForNotif (void); static void Mai_GetMailDomain (const char *Email, char MailDomain[Cns_MAX_BYTES_EMAIL_ADDRESS + 1]); -static bool Mai_DB_CheckIfMailDomainIsAllowedForNotif (const char MailDomain[Cns_MAX_BYTES_EMAIL_ADDRESS + 1]); static void Mai_ListMailDomainsForEdition (void); static void Mai_PutParamMaiCod (void *MaiCod); static void Mai_RenameMailDomain (Cns_ShrtOrFullName_t ShrtOrFullName); -static bool Mai_DB_CheckIfMailDomainNameExists (const char *FieldName,const char *Name,long MaiCod); -static void Mai_DB_UpdateMailDomainName (long MaiCod, - const char *FieldName,const char *NewMaiName); static void Mai_PutFormToCreateMailDomain (void); static void Mai_PutHeadMailDomains (void); -static void Mai_DB_CreateMailDomain (struct Mail *Mai); static void Mai_PutFormToSelectUsrsToListEmails (__attribute__((unused)) void *Args); static void Mai_ListEmails (__attribute__((unused)) void *Args); @@ -253,18 +249,6 @@ static void Mai_EditMailDomainsInternal (void) static void Mai_GetListMailDomainsAllowedForNotif (void) { - static const char *OrderBySubQuery[Mai_NUM_ORDERS] = - { - [Mai_ORDER_BY_DOMAIN] = "Domain," - "Info," - "N DESC", - [Mai_ORDER_BY_INFO ] = "Info," - "Domain," - "N DESC", - [Mai_ORDER_BY_USERS ] = "N DESC," - "Info," - "Domain", - }; MYSQL_RES *mysql_res; MYSQL_ROW row; unsigned NumMai; @@ -275,42 +259,12 @@ static void Mai_GetListMailDomainsAllowedForNotif (void) // ...because a unique temporary table can not be used twice in the same query /***** Create temporary table with all the mail domains present in users' emails table *****/ - DB_Query ("can not remove temporary tables", - "DROP TEMPORARY TABLE IF EXISTS T1,T2"); + Mai_DB_RemoveTmpTables (); - DB_Query ("can not create temporary table", - "CREATE TEMPORARY TABLE T1 ENGINE=MEMORY" - " SELECT SUBSTRING_INDEX(E_mail,'@',-1) AS Domain,COUNT(*) as N" - " FROM usr_emails" - " GROUP BY Domain"); - - DB_Query ("can not create temporary table", - "CREATE TEMPORARY TABLE T2 ENGINE=MEMORY" - " SELECT *" - " FROM T1"); + Mai_DB_CreateTmpTables (); /***** Get mail domains from database *****/ - Gbl.Mails.Num = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get mail domains", - "(SELECT ntf_mail_domains.MaiCod," // row[0] - "ntf_mail_domains.Domain AS Domain," // row[1] - "ntf_mail_domains.Info AS Info," // row[2] - "T1.N AS N" // row[3] - " FROM ntf_mail_domains,T1" - " WHERE ntf_mail_domains.Domain=T1.Domain COLLATE 'latin1_bin')" - " UNION " - "(SELECT MaiCod," // row[0] - "Domain," // row[1] - "Info," // row[2] - "0 AS N" // row[3] - " FROM ntf_mail_domains" - " WHERE Domain NOT IN" - " (SELECT Domain COLLATE 'latin1_bin'" - " FROM T2))" - " ORDER BY %s", // COLLATE necessary to avoid error in comparisons - OrderBySubQuery[Gbl.Mails.SelectedOrder]); - - if (Gbl.Mails.Num) // Mail domains found... + if ((Gbl.Mails.Num = Mai_DB_GetMailDomains (&mysql_res))) // Mail domains found... { /***** Create list with places *****/ if ((Gbl.Mails.Lst = calloc ((size_t) Gbl.Mails.Num, @@ -345,9 +299,7 @@ static void Mai_GetListMailDomainsAllowedForNotif (void) DB_FreeMySQLResult (&mysql_res); /***** Drop temporary table *****/ - DB_Query ("can not remove temporary tables", - "DROP TEMPORARY TABLE IF EXISTS T1," - "T2"); + Mai_DB_RemoveTmpTables (); } /*****************************************************************************/ @@ -387,21 +339,6 @@ static void Mai_GetMailDomain (const char *Email, } } -/*****************************************************************************/ -/************ Check if a mail domain is allowed for notifications ************/ -/*****************************************************************************/ - -static bool Mai_DB_CheckIfMailDomainIsAllowedForNotif (const char MailDomain[Cns_MAX_BYTES_EMAIL_ADDRESS + 1]) - { - /***** Get number of mail_domains with a name from database *****/ - return (DB_QueryCOUNT ("can not check if a mail domain" - " is allowed for notifications", - "SELECT COUNT(*)" - " FROM ntf_mail_domains" - " WHERE Domain='%s'", - MailDomain) != 0); - } - /*****************************************************************************/ /***************** Show warning about notifications via email ****************/ /*****************************************************************************/ @@ -439,12 +376,7 @@ void Mai_GetDataOfMailDomainByCod (struct Mail *Mai) if (Mai->MaiCod > 0) { /***** Get data of a mail domain from database *****/ - if (DB_QuerySELECT (&mysql_res,"can not get data of a mail domain", - "SELECT Domain," // row[0] - "Info" // row[1] - " FROM ntf_mail_domains" - " WHERE MaiCod=%ld", - Mai->MaiCod)) // Mail found... + if (Mai_DB_GetDataOfMailDomainByCod (&mysql_res,Mai->MaiCod)) // Mail found... { /* Get row */ row = mysql_fetch_row (mysql_res); @@ -576,18 +508,15 @@ void Mai_RemoveMailDomain (void) /***** Mail domain constructor *****/ Mai_EditingMailDomainConstructor (); - /***** Get mail code *****/ + /***** Get mail domain code *****/ if ((Mai_EditingMai->MaiCod = Mai_GetParamMaiCod ()) <= 0) Err_WrongMailDomainExit (); - /***** Get data of the mail from database *****/ + /***** Get data of the mail domain rom database *****/ Mai_GetDataOfMailDomainByCod (Mai_EditingMai); - /***** Remove mail *****/ - DB_QueryDELETE ("can not remove a mail domain", - "DELETE FROM ntf_mail_domains" - " WHERE MaiCod=%ld", - Mai_EditingMai->MaiCod); + /***** Remove mail domain *****/ + Mai_DB_RemoveMailDomain (Mai_EditingMai->MaiCod); /***** Write message to show the change made *****/ Ale_CreateAlert (Ale_SUCCESS,NULL, @@ -698,38 +627,6 @@ static void Mai_RenameMailDomain (Cns_ShrtOrFullName_t ShrtOrFullName) Str_Copy (CurrentMaiName,NewMaiName,MaxBytes); } -/*****************************************************************************/ -/********************** Check if the name of mail exists *********************/ -/*****************************************************************************/ - -static bool Mai_DB_CheckIfMailDomainNameExists (const char *FieldName,const char *Name,long MaiCod) - { - /***** Get number of mail_domains with a name from database *****/ - return (DB_QueryCOUNT ("can not check if the name" - " of a mail domain already existed", - "SELECT COUNT(*)" - " FROM ntf_mail_domains" - " WHERE %s='%s'" - " AND MaiCod<>%ld", - FieldName,Name,MaiCod) != 0); - } - -/*****************************************************************************/ -/****************** Update name in table of mail domains *********************/ -/*****************************************************************************/ - -static void Mai_DB_UpdateMailDomainName (long MaiCod, - const char *FieldName,const char *NewMaiName) - { - /***** Update mail domain changing old name by new name */ - DB_QueryUPDATE ("can not update the name of a mail domain", - "UPDATE ntf_mail_domains" - " SET %s='%s'" - " WHERE MaiCod=%ld", - FieldName,NewMaiName, - MaiCod); - } - /*****************************************************************************/ /******* Show alerts after changing a mail domain and continue editing *******/ /*****************************************************************************/ @@ -861,22 +758,6 @@ void Mai_ReceiveFormNewMailDomain (void) Txt_You_must_specify_the_short_name_and_the_full_name_of_the_new_email_domain); } -/*****************************************************************************/ -/************************** Create a new mail domain *************************/ -/*****************************************************************************/ - -static void Mai_DB_CreateMailDomain (struct Mail *Mai) - { - /***** Create a new mail *****/ - DB_QueryINSERT ("can not create mail domain", - "INSERT INTO ntf_mail_domains" - " (Domain,Info)" - " VALUES" - " ('%s','%s')", - Mai->Domain, - Mai->Info); - } - /*****************************************************************************/ /************************ Request users to list emails ***********************/ /*****************************************************************************/ diff --git a/swad_mail_database.c b/swad_mail_database.c new file mode 100644 index 000000000..f78a40c01 --- /dev/null +++ b/swad_mail_database.c @@ -0,0 +1,229 @@ +// swad_mail_database.c: all the stuff related to email, operations with database + +/* + 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. + Copyright (C) 1999-2021 Antonio Caņas Vargas + + 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 . +*/ +/*****************************************************************************/ +/********************************* Headers ***********************************/ +/*****************************************************************************/ + +// #include // For NULL +// #include // For calloc +// #include // For string functions +// #include // For the macro WEXITSTATUS +// #include // For access, lstat, getpid, chdir, symlink, unlink + +// #include "swad_account.h" +// #include "swad_box.h" +#include "swad_database.h" +// #include "swad_error.h" +// #include "swad_form.h" +#include "swad_global.h" +// #include "swad_HTML.h" +// #include "swad_language.h" +#include "swad_mail.h" +#include "swad_mail_database.h" +// #include "swad_parameter.h" +// #include "swad_QR.h" +// #include "swad_tab.h" + +/*****************************************************************************/ +/************** External global variables from others modules ****************/ +/*****************************************************************************/ + +extern struct Globals Gbl; + +/*****************************************************************************/ +/***************************** Private constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************* Private types *******************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private variables *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private prototypes ****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Create temporary tables with all the mail domains in users' emails table **/ +/*****************************************************************************/ + +void Mai_DB_CreateTmpTables (void) + { + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE T1 ENGINE=MEMORY" + " SELECT SUBSTRING_INDEX(E_mail,'@',-1) AS Domain," + "COUNT(*) as N" + " FROM usr_emails" + " GROUP BY Domain"); + + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE T2 ENGINE=MEMORY" + " SELECT *" + " FROM T1"); + } + +/*****************************************************************************/ +/************************** Create a new mail domain *************************/ +/*****************************************************************************/ + +void Mai_DB_CreateMailDomain (const struct Mail *Mai) + { + DB_QueryINSERT ("can not create mail domain", + "INSERT INTO ntf_mail_domains" + " (Domain,Info)" + " VALUES" + " ('%s','%s')", + Mai->Domain, + Mai->Info); + } + +/*****************************************************************************/ +/****************** Update name in table of mail domains *********************/ +/*****************************************************************************/ + +void Mai_DB_UpdateMailDomainName (long MaiCod, + const char *FieldName,const char *NewMaiName) + { + DB_QueryUPDATE ("can not update the name of a mail domain", + "UPDATE ntf_mail_domains" + " SET %s='%s'" + " WHERE MaiCod=%ld", + FieldName,NewMaiName, + MaiCod); + } + +/*****************************************************************************/ +/****************************** Get mail domains *****************************/ +/*****************************************************************************/ + +unsigned Mai_DB_GetMailDomains (MYSQL_RES **mysql_res) + { + static const char *OrderBySubQuery[Mai_NUM_ORDERS] = + { + [Mai_ORDER_BY_DOMAIN] = "Domain," + "Info," + "N DESC", + [Mai_ORDER_BY_INFO ] = "Info," + "Domain," + "N DESC", + [Mai_ORDER_BY_USERS ] = "N DESC," + "Info," + "Domain", + }; + + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get mail domains", + "(SELECT ntf_mail_domains.MaiCod," // row[0] + "ntf_mail_domains.Domain AS Domain," // row[1] + "ntf_mail_domains.Info AS Info," // row[2] + "T1.N AS N" // row[3] + " FROM ntf_mail_domains," + "T1" + " WHERE ntf_mail_domains.Domain=T1.Domain COLLATE 'latin1_bin')" + " UNION " + "(SELECT MaiCod," // row[0] + "Domain," // row[1] + "Info," // row[2] + "0 AS N" // row[3] + " FROM ntf_mail_domains" + " WHERE Domain NOT IN" + " (SELECT Domain COLLATE 'latin1_bin'" + " FROM T2))" + " ORDER BY %s", // COLLATE necessary to avoid error in comparisons + OrderBySubQuery[Gbl.Mails.SelectedOrder]); + } + + +/*****************************************************************************/ +/**************************** Get mail domain data ***************************/ +/*****************************************************************************/ + +unsigned Mai_DB_GetDataOfMailDomainByCod (MYSQL_RES **mysql_res,long MaiCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get data of a mail domain", + "SELECT Domain," // row[0] + "Info" // row[1] + " FROM ntf_mail_domains" + " WHERE MaiCod=%ld", + MaiCod); + } + +/*****************************************************************************/ +/********************** Check if the name of mail exists *********************/ +/*****************************************************************************/ + +bool Mai_DB_CheckIfMailDomainNameExists (const char *FieldName,const char *Name,long MaiCod) + { + /***** Get number of mail_domains with a name from database *****/ + return (DB_QueryCOUNT ("can not check if the name" + " of a mail domain already existed", + "SELECT COUNT(*)" + " FROM ntf_mail_domains" + " WHERE %s='%s'" + " AND MaiCod<>%ld", + FieldName,Name, + MaiCod) != 0); + } + +/*****************************************************************************/ +/************ Check if a mail domain is allowed for notifications ************/ +/*****************************************************************************/ + +bool Mai_DB_CheckIfMailDomainIsAllowedForNotif (const char MailDomain[Cns_MAX_BYTES_EMAIL_ADDRESS + 1]) + { + /***** Get number of mail_domains with a name from database *****/ + return (DB_QueryCOUNT ("can not check if a mail domain" + " is allowed for notifications", + "SELECT COUNT(*)" + " FROM ntf_mail_domains" + " WHERE Domain='%s'", + MailDomain) != 0); + } + +/*****************************************************************************/ +/***************************** Remove mail domain ****************************/ +/*****************************************************************************/ + +void Mai_DB_RemoveMailDomain (long MaiCod) + { + DB_QueryDELETE ("can not remove a mail domain", + "DELETE FROM ntf_mail_domains" + " WHERE MaiCod=%ld", + MaiCod); + } + +/*****************************************************************************/ +/* Remove temporary tables with all the mail domains in users' emails table **/ +/*****************************************************************************/ + +void Mai_DB_RemoveTmpTables (void) + { + DB_Query ("can not remove temporary tables", + "DROP TEMPORARY TABLE IF EXISTS T1," + "T2"); + } diff --git a/swad_mail_database.h b/swad_mail_database.h new file mode 100644 index 000000000..b4d22604d --- /dev/null +++ b/swad_mail_database.h @@ -0,0 +1,53 @@ +// swad_mail_database.h: all the stuff related to email, operations with database + +#ifndef _SWAD_MAI_DB +#define _SWAD_MAI_DB +/* + SWAD (Shared Workspace At a Distance in Spanish), + is a web platform developed at the University of Granada (Spain), + and used to support university teaching. + + This file is part of SWAD core. + Copyright (C) 1999-2021 Antonio Caņas Vargas + + 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 . +*/ +/*****************************************************************************/ +/********************************* Headers ***********************************/ +/*****************************************************************************/ + +#include "swad_constant.h" + +/*****************************************************************************/ +/************************** Public types and constants ***********************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Public prototypes *****************************/ +/*****************************************************************************/ + +void Mai_DB_CreateTmpTables (void); +void Mai_DB_CreateMailDomain (const struct Mail *Mai); +void Mai_DB_UpdateMailDomainName (long MaiCod, + const char *FieldName,const char *NewMaiName); + +unsigned Mai_DB_GetMailDomains (MYSQL_RES **mysql_res); +unsigned Mai_DB_GetDataOfMailDomainByCod (MYSQL_RES **mysql_res,long MaiCod); +bool Mai_DB_CheckIfMailDomainNameExists (const char *FieldName,const char *Name,long MaiCod); +bool Mai_DB_CheckIfMailDomainIsAllowedForNotif (const char MailDomain[Cns_MAX_BYTES_EMAIL_ADDRESS + 1]); + +void Mai_DB_RemoveMailDomain (long MaiCod); +void Mai_DB_RemoveTmpTables (void); + +#endif