swad-core/swad_mail.c

1784 lines
61 KiB
C
Raw Normal View History

// swad_mail.c: everything related to email
2014-12-01 23:55:08 +01:00
/*
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-2023 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 ***********************************/
/*****************************************************************************/
#define _GNU_SOURCE // For asprintf
2019-12-29 12:39:00 +01:00
#include <stddef.h> // For NULL
#include <stdio.h> // For asprintf
2014-12-01 23:55:08 +01:00
#include <stdlib.h> // For calloc
#include <string.h> // For string functions
#include <sys/wait.h> // For the macro WEXITSTATUS
#include <unistd.h> // For access, lstat, getpid, chdir, symlink, unlink
2014-12-12 22:39:55 +01:00
#include "swad_account.h"
#include "swad_action_list.h"
#include "swad_alert.h"
2017-06-10 21:38:10 +02:00
#include "swad_box.h"
2014-12-01 23:55:08 +01:00
#include "swad_database.h"
#include "swad_error.h"
2018-11-09 20:47:39 +01:00
#include "swad_form.h"
2014-12-01 23:55:08 +01:00
#include "swad_global.h"
2019-10-23 19:05:05 +02:00
#include "swad_HTML.h"
2018-12-08 16:43:13 +01:00
#include "swad_language.h"
2014-12-01 23:55:08 +01:00
#include "swad_mail.h"
#include "swad_mail_database.h"
2014-12-01 23:55:08 +01:00
#include "swad_parameter.h"
#include "swad_QR.h"
2016-10-12 14:02:56 +02:00
#include "swad_tab.h"
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/******************************* Private types *******************************/
/*****************************************************************************/
struct Mai_Mails
{
unsigned Num; // Number of mail domains
struct Mail *Lst; // List of mail domains
Mai_DomainsOrder_t SelectedOrder;
};
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/***************************** Private variables *****************************/
/*****************************************************************************/
2019-04-09 14:04:04 +02:00
static const char *Mai_EMAIL_SECTION_ID = "email_section";
static struct Mail *Mai_EditingMai = NULL; // Static variable to keep the mail domain being edited
2018-10-16 01:36:13 +02:00
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
static Mai_DomainsOrder_t Mai_GetParMaiOrder (void);
2020-04-08 19:42:03 +02:00
static void Mai_PutIconToEditMailDomains (__attribute__((unused)) void *Args);
2019-04-09 14:04:04 +02:00
static void Mai_EditMailDomainsInternal (void);
static void Mai_GetListMailDomainsAllowedForNotif (struct Mai_Mails *Mails);
static void Mai_GetMailDomain (const char *Email,
char MailDomain[Cns_MAX_BYTES_EMAIL_ADDRESS + 1]);
static void Mai_GetMailDomainDataFromRow (MYSQL_RES *mysql_res,
struct Mail *Mai,
bool GetNumUsrs);
static void Mai_FreeListMailDomains (struct Mai_Mails *Mails);
2016-10-12 14:02:56 +02:00
static void Mai_ListMailDomainsForEdition (const struct Mai_Mails *Mails);
static void Mai_PutParMaiCod (void *MaiCod);
2017-03-09 11:16:17 +01:00
2016-10-28 10:03:37 +02:00
static void Mai_RenameMailDomain (Cns_ShrtOrFullName_t ShrtOrFullName);
2017-03-09 11:16:17 +01:00
2014-12-01 23:55:08 +01:00
static void Mai_PutFormToCreateMailDomain (void);
static void Mai_PutHeadMailDomains (void);
2020-04-08 19:42:03 +02:00
static void Mai_PutFormToSelectUsrsToListEmails (__attribute__((unused)) void *Args);
static void Mai_ListEmails (__attribute__((unused)) void *Args);
2019-04-11 21:37:11 +02:00
static void Mai_ShowFormChangeUsrEmail (Usr_MeOrOther_t MeOrOther,
2020-10-14 00:59:24 +02:00
bool IMustFillInEmail,
bool IShouldConfirmEmail);
static void Mai_PutParsRemoveMyEmail (void *Email);
static void Mai_PutParsRemoveOtherEmail (void *Email);
2018-10-15 14:07:12 +02:00
static void Mai_RemoveEmail (struct Usr_Data *UsrDat);
static void Mai_ChangeUsrEmail (struct Usr_Data *UsrDat,Usr_MeOrOther_t MeOrOther);
2017-03-13 13:17:53 +01:00
static void Mai_InsertMailKey (const char Email[Cns_MAX_BYTES_EMAIL_ADDRESS + 1],
2017-01-28 15:58:46 +01:00
const char MailKey[Mai_LENGTH_EMAIL_CONFIRM_KEY + 1]);
2014-12-01 23:55:08 +01:00
static void Mai_InitializeMailDomainList (struct Mai_Mails *Mails);
2019-04-09 14:04:04 +02:00
static void Mai_EditingMailDomainConstructor (void);
static void Mai_EditingMailDomainDestructor (void);
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/*************************** List all mail domains ***************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
void Mai_SeeMailDomains (void)
{
2019-03-12 21:25:55 +01:00
extern const char *Hlp_START_Domains;
2016-11-16 23:19:52 +01:00
extern const char *Txt_Email_domains_allowed_for_notifications;
extern const char *Txt_EMAIL_DOMAIN_HELP_ORDER[3];
extern const char *Txt_EMAIL_DOMAIN_ORDER[3];
2017-01-29 12:42:19 +01:00
Mai_DomainsOrder_t Order;
2014-12-01 23:55:08 +01:00
unsigned NumMai;
struct Mai_Mails Mails;
/***** Initialize mail domain list *****/
Mai_InitializeMailDomainList (&Mails);
2014-12-01 23:55:08 +01:00
/***** Get parameter with the type of order in the list of mail domains *****/
Mails.SelectedOrder = Mai_GetParMaiOrder ();
2014-12-01 23:55:08 +01:00
/***** Get list of mail domains *****/
Mai_GetListMailDomainsAllowedForNotif (&Mails);
2014-12-01 23:55:08 +01:00
2019-10-26 02:19:42 +02:00
/***** Begin box and table *****/
2020-03-26 02:54:30 +01:00
if (Gbl.Usrs.Me.Role.Logged == Rol_SYS_ADM)
Box_BoxTableBegin (NULL,Txt_Email_domains_allowed_for_notifications,
2020-04-08 19:42:03 +02:00
Mai_PutIconToEditMailDomains,NULL,
2020-03-26 02:54:30 +01:00
Hlp_START_Domains,Box_NOT_CLOSABLE,2);
else
Box_BoxTableBegin (NULL,Txt_Email_domains_allowed_for_notifications,
NULL,NULL,
Hlp_START_Domains,Box_NOT_CLOSABLE,2);
2017-06-12 14:16:33 +02:00
/***** Write heading *****/
2019-10-23 19:05:05 +02:00
HTM_TR_Begin (NULL);
for (Order = (Mai_DomainsOrder_t) 0;
Order <= (Mai_DomainsOrder_t) (Mai_NUM_ORDERS - 1);
Order++)
{
HTM_TH_Begin (HTM_HEAD_LEFT);
Frm_BeginForm (ActSeeMai);
Par_PutParUnsigned (NULL,"Order",(unsigned) Order);
HTM_BUTTON_Submit_Begin (Txt_EMAIL_DOMAIN_HELP_ORDER[Order],
"class=\"BT_LINK\"");
if (Order == Mails.SelectedOrder)
HTM_U_Begin ();
HTM_Txt (Txt_EMAIL_DOMAIN_ORDER[Order]);
if (Order == Mails.SelectedOrder)
HTM_U_End ();
HTM_BUTTON_End ();
Frm_EndForm ();
HTM_TH_End ();
}
2019-10-23 19:05:05 +02:00
HTM_TR_End ();
2014-12-01 23:55:08 +01:00
/***** Write all mail domains *****/
2014-12-01 23:55:08 +01:00
for (NumMai = 0;
NumMai < Mails.Num;
2014-12-01 23:55:08 +01:00
NumMai++)
2019-10-04 14:42:59 +02:00
{
2014-12-01 23:55:08 +01:00
/* Write data of this mail domain */
2019-10-23 19:05:05 +02:00
HTM_TR_Begin (NULL);
2019-10-07 17:36:41 +02:00
HTM_TD_Begin ("class=\"LT DAT_%s\"",The_GetSuffix ());
HTM_Txt (Mails.Lst[NumMai].Domain);
HTM_TD_End ();
2019-10-07 17:36:41 +02:00
HTM_TD_Begin ("class=\"LT DAT_%s\"",The_GetSuffix ());
HTM_Txt (Mails.Lst[NumMai].Info);
HTM_TD_End ();
2019-10-07 17:36:41 +02:00
HTM_TD_Begin ("class=\"RT DAT_%s\"",The_GetSuffix ());
HTM_Unsigned (Mails.Lst[NumMai].NumUsrs);
HTM_TD_End ();
2019-10-07 17:36:41 +02:00
2019-10-23 19:05:05 +02:00
HTM_TR_End ();
2019-10-04 14:42:59 +02:00
}
2014-12-01 23:55:08 +01:00
2017-06-12 14:16:33 +02:00
/***** End table and box *****/
2019-11-25 23:18:08 +01:00
Box_BoxTableEnd ();
2014-12-01 23:55:08 +01:00
/***** Free list of mail domains *****/
Mai_FreeListMailDomains (&Mails);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/******* Get parameter with the type or order in list of mail domains ********/
/*****************************************************************************/
static Mai_DomainsOrder_t Mai_GetParMaiOrder (void)
2014-12-01 23:55:08 +01:00
{
return (Mai_DomainsOrder_t)
Par_GetParUnsignedLong ("Order",
0,
Mai_NUM_ORDERS - 1,
(unsigned long) Mai_ORDER_DEFAULT);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
2016-03-19 00:40:20 +01:00
/************************ Put icon to edit mail domains **********************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2020-04-08 19:42:03 +02:00
static void Mai_PutIconToEditMailDomains (__attribute__((unused)) void *Args)
2014-12-01 23:55:08 +01:00
{
2020-04-08 19:42:03 +02:00
Ico_PutContextualIconToEdit (ActEdiMai,NULL,
NULL,NULL);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/*********************** Put forms to edit mail domains **********************/
/*****************************************************************************/
void Mai_EditMailDomains (void)
2019-04-09 14:04:04 +02:00
{
/***** Mail domain constructor *****/
Mai_EditingMailDomainConstructor ();
/***** Edit mail domains *****/
Mai_EditMailDomainsInternal ();
/***** Mail domain destructor *****/
Mai_EditingMailDomainDestructor ();
}
static void Mai_EditMailDomainsInternal (void)
2014-12-01 23:55:08 +01:00
{
struct Mai_Mails Mails;
/***** Initialize mail domain list *****/
Mai_InitializeMailDomainList (&Mails);
2014-12-01 23:55:08 +01:00
/***** Get list of mail domains *****/
Mai_GetListMailDomainsAllowedForNotif (&Mails);
2014-12-01 23:55:08 +01:00
/***** Forms to edit mail domains *****/
Mai_ListMailDomainsForEdition (&Mails);
2014-12-01 23:55:08 +01:00
/***** Free list of mail domains *****/
Mai_FreeListMailDomains (&Mails);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/*************************** List all mail domains ***************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
static void Mai_GetListMailDomainsAllowedForNotif (struct Mai_Mails *Mails)
2014-12-01 23:55:08 +01:00
{
MYSQL_RES *mysql_res;
unsigned NumMai;
// Query uses temporary tables for speed
// Query uses two identical temporary tables...
// ...because a unique temporary table can not be used twice in the same query
/***** Create temporary table with all mail domains present in users' emails table *****/
Mai_DB_RemoveTmpTables ();
2014-12-01 23:55:08 +01:00
Mai_DB_CreateTmpTables ();
2014-12-01 23:55:08 +01:00
/***** Get mail domains from database *****/
if ((Mails->Num = Mai_DB_GetMailDomains (&mysql_res,Mails->SelectedOrder))) // Mail domains found...
2014-12-01 23:55:08 +01:00
{
/***** Create list with places *****/
if ((Mails->Lst = calloc ((size_t) Mails->Num,
sizeof (struct Mail))) == NULL)
Err_NotEnoughMemoryExit ();
2014-12-01 23:55:08 +01:00
/***** Get the mail domains *****/
for (NumMai = 0;
NumMai < Mails->Num;
2014-12-01 23:55:08 +01:00
NumMai++)
Mai_GetMailDomainDataFromRow (mysql_res,&Mails->Lst[NumMai],
true); // Get number of users
2014-12-01 23:55:08 +01:00
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Drop temporary table *****/
Mai_DB_RemoveTmpTables ();
2014-12-01 23:55:08 +01:00
}
2016-10-12 14:02:56 +02:00
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/************ Check if user can receive notifications via email **************/
2016-10-12 14:02:56 +02:00
/*****************************************************************************/
bool Mai_CheckIfUsrCanReceiveEmailNotif (const struct Usr_Data *UsrDat)
2016-10-12 14:02:56 +02:00
{
2017-03-13 13:17:53 +01:00
char MailDomain[Cns_MAX_BYTES_EMAIL_ADDRESS + 1];
2016-10-12 14:02:56 +02:00
/***** Check #1: is email empty or filled? *****/
if (!UsrDat->Email[0])
return false;
/***** Check #2: is email address confirmed? *****/
2016-10-12 14:02:56 +02:00
if (!UsrDat->EmailConfirmed)
return false;
/***** Check #3: check if there is mail domain *****/
2017-03-07 11:03:05 +01:00
Mai_GetMailDomain (UsrDat->Email,MailDomain);
if (!MailDomain[0])
return false;
/***** Check #4: is mail domain allowed? *****/
return Mai_DB_CheckIfMailDomainIsAllowedForNotif (MailDomain);
2016-10-12 14:02:56 +02:00
}
2017-01-17 03:10:43 +01:00
/*****************************************************************************/
/********************** Get mailbox from email address ***********************/
/*****************************************************************************/
static void Mai_GetMailDomain (const char *Email,
char MailDomain[Cns_MAX_BYTES_EMAIL_ADDRESS + 1])
2017-01-17 03:10:43 +01:00
{
const char *Ptr;
2017-03-07 11:03:05 +01:00
MailDomain[0] = '\0'; // Return empty mailbox on error
2017-01-17 03:10:43 +01:00
if ((Ptr = strchr (Email,(int) '@'))) // Find first '@' in address
if (Ptr != Email) // '@' is not the first character in Email
{
Ptr++; // Skip '@'
if (strchr (Ptr,(int) '@') == NULL) // No more '@' found
Str_Copy (MailDomain,Ptr,Cns_MAX_BYTES_EMAIL_ADDRESS);
2017-01-17 03:10:43 +01:00
}
}
2016-10-12 14:02:56 +02:00
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/***************** Show warning about notifications via email ****************/
2016-10-12 14:02:56 +02:00
/*****************************************************************************/
void Mai_WriteWarningEmailNotifications (void)
{
extern const char *Txt_You_can_only_receive_email_notifications_if_;
2016-12-28 17:22:25 +01:00
extern const char *Txt_TABS_TXT[Tab_NUM_TABS];
2016-10-12 14:02:56 +02:00
extern const char *Txt_MENU_TITLE[Tab_NUM_TABS][Act_MAX_OPTIONS_IN_MENU_PER_TAB];
2017-03-24 20:07:29 +01:00
extern const char *Txt_Domains;
2018-04-24 13:21:53 +02:00
Tab_Tab_t TabMyAccount = Act_GetTab (ActFrmMyAcc );
Tab_Tab_t TabMailDomains = Act_GetTab (ActSeeMai);
2016-10-12 14:02:56 +02:00
2019-02-16 17:20:05 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_You_can_only_receive_email_notifications_if_,
Txt_TABS_TXT [TabMyAccount ],
Txt_MENU_TITLE[TabMyAccount ][Act_GetIndexInMenu (ActFrmMyAcc)],
Txt_TABS_TXT [TabMailDomains],
Txt_MENU_TITLE[TabMailDomains][Act_GetIndexInMenu (ActSeeMai )],
Txt_Domains);
2016-10-12 14:02:56 +02:00
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/******************* Get mail domain data using its code *********************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
void Mai_GetMailDomainDataByCod (struct Mail *Mai)
2014-12-01 23:55:08 +01:00
{
MYSQL_RES *mysql_res;
/***** Clear data *****/
Mai->Domain[0] = Mai->Info[0] = '\0';
/***** Check if mail code is correct *****/
if (Mai->MaiCod > 0)
{
/***** Get data of a mail domain from database *****/
if (Mai_DB_GetMailDomainDataByCod (&mysql_res,Mai->MaiCod)) // Mail found...
Mai_GetMailDomainDataFromRow (mysql_res,Mai,
false); // Don't get number of users
2014-12-01 23:55:08 +01:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
}
/*****************************************************************************/
/****************** Get mail domain data from database row *******************/
/*****************************************************************************/
static void Mai_GetMailDomainDataFromRow (MYSQL_RES *mysql_res,
struct Mail *Mai,
bool GetNumUsrs)
{
MYSQL_ROW row;
/***** Get next row from result *****/
row = mysql_fetch_row (mysql_res);
/***** Get mail code (row[0]) *****/
if ((Mai->MaiCod = Str_ConvertStrCodToLongCod (row[0])) <= 0)
Err_WrongMailDomainExit ();
/***** Get the mail domain (row[1]) and the mail domain info (row[2]) *****/
Str_Copy (Mai->Domain,row[1],sizeof (Mai->Domain) - 1);
Str_Copy (Mai->Info ,row[2],sizeof (Mai->Info ) - 1);
/***** Get number of users (row[3]) *****/
if (GetNumUsrs)
{
if (sscanf (row[3],"%u",&(Mai->NumUsrs)) != 1)
Mai->NumUsrs = 0;
}
else
Mai->NumUsrs = 0;
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/************************** Free list of mail domains ************************/
/*****************************************************************************/
static void Mai_FreeListMailDomains (struct Mai_Mails *Mails)
2014-12-01 23:55:08 +01:00
{
if (Mails->Num && Mails->Lst)
2014-12-01 23:55:08 +01:00
{
free (Mails->Lst);
Mails->Lst = NULL;
Mails->Num = 0;
2014-12-01 23:55:08 +01:00
}
}
/*****************************************************************************/
/************************** List all mail domains ****************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
static void Mai_ListMailDomainsForEdition (const struct Mai_Mails *Mails)
2014-12-01 23:55:08 +01:00
{
2019-03-12 21:25:55 +01:00
extern const char *Hlp_START_Domains_edit;
2016-11-16 23:19:52 +01:00
extern const char *Txt_Email_domains_allowed_for_notifications;
2014-12-01 23:55:08 +01:00
unsigned NumMai;
struct Mail *Mai;
/***** Begin box *****/
Box_BoxBegin (NULL,Txt_Email_domains_allowed_for_notifications,
NULL,NULL,
Hlp_START_Domains_edit,Box_NOT_CLOSABLE);
2014-12-01 23:55:08 +01:00
/***** Put a form to create a new mail *****/
Mai_PutFormToCreateMailDomain ();
2019-10-09 15:09:42 +02:00
if (Mails->Num)
{
/***** Begin table *****/
HTM_TABLE_BeginWidePadding (2);
/***** Write heading *****/
Mai_PutHeadMailDomains ();
/***** Write all mail domains *****/
for (NumMai = 0;
NumMai < Mails->Num;
NumMai++)
{
Mai = &Mails->Lst[NumMai];
HTM_TR_Begin (NULL);
/* Put icon to remove mail domain */
HTM_TD_Begin ("class=\"BM\"");
Ico_PutContextualIconToRemove (ActRemMai,NULL,
Mai_PutParMaiCod,&Mai->MaiCod);
HTM_TD_End ();
/* Mail domain code */
HTM_TD_Begin ("class=\"CODE DAT_%s\"",The_GetSuffix ());
HTM_Long (Mai->MaiCod);
HTM_TD_End ();
/* Mail domain */
HTM_TD_Begin ("class=\"CM\"");
Frm_BeginForm (ActRenMaiSho);
ParCod_PutPar (ParCod_Mai,Mai->MaiCod);
HTM_INPUT_TEXT ("Domain",Cns_MAX_CHARS_EMAIL_ADDRESS,Mai->Domain,
HTM_SUBMIT_ON_CHANGE,
"size=\"15\" class=\"INPUT_%s\"",
The_GetSuffix ());
Frm_EndForm ();
HTM_TD_End ();
/* Mail domain info */
HTM_TD_Begin ("class=\"CM\"");
Frm_BeginForm (ActRenMaiFul);
ParCod_PutPar (ParCod_Mai,Mai->MaiCod);
HTM_INPUT_TEXT ("Info",Mai_MAX_CHARS_MAIL_INFO,Mai->Info,
HTM_SUBMIT_ON_CHANGE,
"size=\"40\" class=\"INPUT_%s\"",
The_GetSuffix ());
Frm_EndForm ();
HTM_TD_End ();
/* Number of users */
HTM_TD_Begin ("class=\"RM DAT_%s\"",The_GetSuffix ());
HTM_Unsigned (Mai->NumUsrs);
HTM_TD_End ();
HTM_TR_End ();
}
/***** End table *****/
HTM_TABLE_End ();
}
2014-12-01 23:55:08 +01:00
/***** End box *****/
Box_BoxEnd ();
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/******************** Write parameter with code of mail **********************/
/*****************************************************************************/
static void Mai_PutParMaiCod (void *MaiCod)
2014-12-01 23:55:08 +01:00
{
2020-10-13 22:34:31 +02:00
if (MaiCod)
ParCod_PutPar (ParCod_Mai,*((long *) MaiCod));
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/******************************* Remove a mail *******************************/
/*****************************************************************************/
void Mai_RemoveMailDomain (void)
{
2016-11-16 23:19:52 +01:00
extern const char *Txt_Email_domain_X_removed;
2019-04-09 14:04:04 +02:00
/***** Mail domain constructor *****/
Mai_EditingMailDomainConstructor ();
2014-12-01 23:55:08 +01:00
/***** Get mail domain code *****/
Mai_EditingMai->MaiCod = ParCod_GetAndCheckPar (ParCod_Mai);
2014-12-01 23:55:08 +01:00
/***** Get data of the mail domain rom database *****/
Mai_GetMailDomainDataByCod (Mai_EditingMai);
2014-12-01 23:55:08 +01:00
/***** Remove mail domain *****/
Mai_DB_RemoveMailDomain (Mai_EditingMai->MaiCod);
2014-12-01 23:55:08 +01:00
/***** Write message to show the change made *****/
2019-04-09 14:04:04 +02:00
Ale_CreateAlert (Ale_SUCCESS,NULL,
Txt_Email_domain_X_removed,
Mai_EditingMai->Domain);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/********************* Change the short name of a mail ***********************/
/*****************************************************************************/
void Mai_RenameMailDomainShort (void)
{
2019-04-09 14:04:04 +02:00
/***** Mail domain constructor *****/
Mai_EditingMailDomainConstructor ();
/***** Rename mail domain *****/
2016-10-28 10:03:37 +02:00
Mai_RenameMailDomain (Cns_SHRT_NAME);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/********************* Change the full name of a mail ************************/
/*****************************************************************************/
void Mai_RenameMailDomainFull (void)
{
2019-04-09 14:04:04 +02:00
/***** Mail domain constructor *****/
Mai_EditingMailDomainConstructor ();
/***** Rename mail domain *****/
2014-12-01 23:55:08 +01:00
Mai_RenameMailDomain (Cns_FULL_NAME);
}
/*****************************************************************************/
2016-10-28 10:03:37 +02:00
/************************* Change the name of a mail *************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2016-10-28 10:03:37 +02:00
static void Mai_RenameMailDomain (Cns_ShrtOrFullName_t ShrtOrFullName)
2014-12-01 23:55:08 +01:00
{
2016-11-16 23:19:52 +01:00
extern const char *Txt_The_email_domain_X_already_exists;
extern const char *Txt_The_email_domain_X_has_been_renamed_as_Y;
extern const char *Txt_The_email_domain_X_has_not_changed;
const char *ParName = NULL; // Initialized to avoid warning
const char *FldName = NULL; // Initialized to avoid warning
unsigned MaxBytes = 0; // Initialized to avoid warning
char *CurrentMaiName = NULL; // Initialized to avoid warning
2017-03-07 19:55:29 +01:00
char NewMaiName[Mai_MAX_BYTES_MAIL_INFO + 1];
2014-12-01 23:55:08 +01:00
2016-10-28 10:03:37 +02:00
switch (ShrtOrFullName)
2014-12-01 23:55:08 +01:00
{
2016-10-28 10:03:37 +02:00
case Cns_SHRT_NAME:
ParName = "Domain";
FldName = "Domain";
2017-03-13 13:17:53 +01:00
MaxBytes = Cns_MAX_BYTES_EMAIL_ADDRESS;
2019-04-09 14:04:04 +02:00
CurrentMaiName = Mai_EditingMai->Domain;
2014-12-01 23:55:08 +01:00
break;
case Cns_FULL_NAME:
ParName = "Info";
FldName = "Info";
2017-03-07 19:55:29 +01:00
MaxBytes = Mai_MAX_BYTES_MAIL_INFO;
2019-04-09 14:04:04 +02:00
CurrentMaiName = Mai_EditingMai->Info;
2014-12-01 23:55:08 +01:00
break;
}
/***** Get parameters from form *****/
/* Get the code of the mail */
Mai_EditingMai->MaiCod = ParCod_GetAndCheckPar (ParCod_Mai);
2014-12-01 23:55:08 +01:00
/* Get the new name for the mail */
Par_GetParText (ParName,NewMaiName,MaxBytes);
2014-12-01 23:55:08 +01:00
/***** Get from the database the old names of the mail *****/
Mai_GetMailDomainDataByCod (Mai_EditingMai);
2014-12-01 23:55:08 +01:00
/***** Check if new name is empty *****/
2019-12-20 00:30:54 +01:00
if (NewMaiName[0])
2014-12-01 23:55:08 +01:00
{
2019-01-02 15:10:51 +01:00
/***** Check if old and new names are the same
(this happens when return is pressed without changes) *****/
2014-12-01 23:55:08 +01:00
if (strcmp (CurrentMaiName,NewMaiName)) // Different names
{
/***** If mail was in database... *****/
if (Mai_DB_CheckIfMailDomainNameExists (ParName,NewMaiName,Mai_EditingMai->MaiCod))
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_WARNING,Mai_EMAIL_SECTION_ID,
Txt_The_email_domain_X_already_exists,
NewMaiName);
2014-12-01 23:55:08 +01:00
else
{
/* Update the table changing old name by new name */
Mai_DB_UpdateMailDomainName (Mai_EditingMai->MaiCod,FldName,NewMaiName);
2014-12-01 23:55:08 +01:00
2017-03-09 11:16:17 +01:00
/* Write message to show the change made */
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_SUCCESS,Mai_EMAIL_SECTION_ID,
Txt_The_email_domain_X_has_been_renamed_as_Y,
CurrentMaiName,NewMaiName);
2014-12-01 23:55:08 +01:00
}
}
else // The same name
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_INFO,Mai_EMAIL_SECTION_ID,
Txt_The_email_domain_X_has_not_changed,
CurrentMaiName);
2014-12-01 23:55:08 +01:00
}
2019-12-20 00:30:54 +01:00
else
Ale_CreateAlertYouCanNotLeaveFieldEmpty ();
2014-12-01 23:55:08 +01:00
2019-04-09 14:04:04 +02:00
/***** Update name *****/
Str_Copy (CurrentMaiName,NewMaiName,MaxBytes);
2014-12-01 23:55:08 +01:00
}
2019-04-09 14:04:04 +02:00
/*****************************************************************************/
/******* Show alerts after changing a mail domain and continue editing *******/
/*****************************************************************************/
void Mai_ContEditAfterChgMai (void)
{
/***** Write message to show the change made *****/
Ale_ShowAlerts (NULL);
/***** Show the form again *****/
Mai_EditMailDomainsInternal ();
/***** Mail domain destructor *****/
Mai_EditingMailDomainDestructor ();
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/*********************** Put a form to create a new mail *********************/
/*****************************************************************************/
static void Mai_PutFormToCreateMailDomain (void)
{
/***** Begin form to create *****/
Frm_BeginFormTable (ActNewMai,NULL,NULL,NULL);
2014-12-01 23:55:08 +01:00
/***** Write heading *****/
Mai_PutHeadMailDomains ();
2014-12-01 23:55:08 +01:00
/***** Second row *****/
HTM_TR_Begin (NULL);
/* Column to remove mail domain, disabled here */
HTM_TD_Begin ("class=\"BM\"");
HTM_TD_End ();
/* Mail domain code */
HTM_TD_Begin ("class=\"CODE\"");
HTM_TD_End ();
/* Mail domain */
HTM_TD_Begin ("class=\"CM\"");
HTM_INPUT_TEXT ("Domain",Cns_MAX_CHARS_EMAIL_ADDRESS,Mai_EditingMai->Domain,
HTM_DONT_SUBMIT_ON_CHANGE,
"size=\"15\" class=\"INPUT_%s\""
" required=\"required\"",
The_GetSuffix ());
HTM_TD_End ();
/* Mail domain info */
HTM_TD_Begin ("class=\"CM\"");
HTM_INPUT_TEXT ("Info",Mai_MAX_CHARS_MAIL_INFO,Mai_EditingMai->Info,
HTM_DONT_SUBMIT_ON_CHANGE,
"size=\"40\" class=\"INPUT_%s\""
" required=\"required\"",
The_GetSuffix ());
HTM_TD_End ();
/* Number of users */
HTM_TD_Begin ("class=\"RM DAT_%s\"",The_GetSuffix ());
HTM_Unsigned (0);
HTM_TD_End ();
HTM_TR_End ();
/***** End form to create *****/
Frm_EndFormTable (Btn_CREATE_BUTTON);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/********************* Write header with fields of a mail ********************/
/*****************************************************************************/
static void Mai_PutHeadMailDomains (void)
{
extern const char *Txt_Code;
2016-11-16 23:19:52 +01:00
extern const char *Txt_EMAIL_DOMAIN_ORDER[3];
2014-12-01 23:55:08 +01:00
2019-10-23 19:05:05 +02:00
HTM_TR_Begin (NULL);
HTM_TH_Span (NULL ,HTM_HEAD_CENTER,1,1,"BT");
HTM_TH (Txt_Code ,HTM_HEAD_RIGHT );
HTM_TH (Txt_EMAIL_DOMAIN_ORDER[Mai_ORDER_BY_DOMAIN],HTM_HEAD_LEFT );
HTM_TH (Txt_EMAIL_DOMAIN_ORDER[Mai_ORDER_BY_INFO ],HTM_HEAD_LEFT );
HTM_TH (Txt_EMAIL_DOMAIN_ORDER[Mai_ORDER_BY_USERS ],HTM_HEAD_RIGHT );
2019-10-23 19:05:05 +02:00
HTM_TR_End ();
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/******************* Receive form to create a new mail ***********************/
/*****************************************************************************/
2020-05-05 21:49:00 +02:00
void Mai_ReceiveFormNewMailDomain (void)
2014-12-01 23:55:08 +01:00
{
2016-11-16 23:19:52 +01:00
extern const char *Txt_The_email_domain_X_already_exists;
2019-04-09 14:04:04 +02:00
extern const char *Txt_Created_new_email_domain_X;
2014-12-01 23:55:08 +01:00
2019-04-09 14:04:04 +02:00
/***** Mail domain constructor *****/
Mai_EditingMailDomainConstructor ();
2014-12-01 23:55:08 +01:00
/***** Get parameters from form *****/
/* Get mail short name */
Par_GetParText ("Domain",Mai_EditingMai->Domain,Cns_MAX_BYTES_EMAIL_ADDRESS);
2014-12-01 23:55:08 +01:00
/* Get mail full name */
Par_GetParText ("Info",Mai_EditingMai->Info,Mai_MAX_BYTES_MAIL_INFO);
2014-12-01 23:55:08 +01:00
2019-04-09 14:04:04 +02:00
if (Mai_EditingMai->Domain[0] &&
Mai_EditingMai->Info[0]) // If there's a mail name
2014-12-01 23:55:08 +01:00
{
/***** If name of mail was in database... *****/
if (Mai_DB_CheckIfMailDomainNameExists ("Domain",Mai_EditingMai->Domain,-1L))
2019-04-09 14:04:04 +02:00
Ale_CreateAlert (Ale_WARNING,NULL,
Txt_The_email_domain_X_already_exists,
Mai_EditingMai->Domain);
else if (Mai_DB_CheckIfMailDomainNameExists ("Info",Mai_EditingMai->Info,-1L))
2019-04-09 14:04:04 +02:00
Ale_CreateAlert (Ale_WARNING,NULL,
Txt_The_email_domain_X_already_exists,
Mai_EditingMai->Info);
2014-12-01 23:55:08 +01:00
else // Add new mail to database
2019-04-09 14:04:04 +02:00
{
Mai_DB_CreateMailDomain (Mai_EditingMai);
2019-04-09 14:04:04 +02:00
Ale_ShowAlert (Ale_SUCCESS,Txt_Created_new_email_domain_X,
Mai_EditingMai->Domain);
}
2014-12-01 23:55:08 +01:00
}
else // If there is not a mail name
Ale_CreateAlertYouMustSpecifyTheShortNameAndTheFullName ();
2014-12-01 23:55:08 +01:00
}
2019-04-11 21:37:11 +02:00
/*****************************************************************************/
2020-04-08 19:42:03 +02:00
/************************ Request users to list emails ***********************/
2019-04-11 21:37:11 +02:00
/*****************************************************************************/
2020-04-08 19:42:03 +02:00
void Mai_ReqUsrsToListEmails (void)
{
Mai_PutFormToSelectUsrsToListEmails (NULL);
}
static void Mai_PutFormToSelectUsrsToListEmails (__attribute__((unused)) void *Args)
2019-04-11 21:37:11 +02:00
{
2020-09-26 17:20:01 +02:00
extern const char *Hlp_COMMUNICATION_Email;
2019-04-11 23:15:40 +02:00
extern const char *Txt_Email;
2019-04-11 21:37:11 +02:00
extern const char *Txt_View_email_addresses;
2020-04-08 19:42:03 +02:00
/***** List users to select some of them *****/
Usr_PutFormToSelectUsrsToGoToAct (&Gbl.Usrs.Selected,
ActMaiUsr,
NULL,NULL,
Txt_Email,
2020-09-26 17:20:01 +02:00
Hlp_COMMUNICATION_Email,
2020-04-08 19:42:03 +02:00
Txt_View_email_addresses,
false); // Do not put form with date range
2019-04-11 21:37:11 +02:00
}
/*****************************************************************************/
/****** Get and check list of selected users, and show users' emails ********/
/*****************************************************************************/
void Mai_GetSelUsrsAndListEmails (void)
2019-04-11 21:37:11 +02:00
{
2019-11-15 03:34:48 +01:00
Usr_GetSelectedUsrsAndGoToAct (&Gbl.Usrs.Selected,
2020-04-08 19:42:03 +02:00
Mai_ListEmails,NULL, // when user(s) selected
Mai_PutFormToSelectUsrsToListEmails,NULL); // when no user selected
2019-04-11 21:37:11 +02:00
}
2016-07-01 11:38:53 +02:00
/*****************************************************************************/
/******** List the emails of all students to creates an email message ********/
2016-07-01 11:38:53 +02:00
/*****************************************************************************/
2017-03-07 19:55:29 +01:00
#define Mai_MAX_BYTES_STR_ADDR (256 * 1024 - 1)
2016-07-01 11:38:53 +02:00
2020-04-08 19:42:03 +02:00
static void Mai_ListEmails (__attribute__((unused)) void *Args)
2016-07-01 11:38:53 +02:00
{
2020-09-26 17:20:01 +02:00
extern const char *Hlp_COMMUNICATION_Email;
2019-04-11 21:37:11 +02:00
extern const char *Txt_Email_addresses;
extern const char *Txt_X_users_who_have_email;
extern const char *Txt_X_users_who_have_accepted_and_who_have_email;
2016-11-16 23:19:52 +01:00
extern const char *Txt_Create_email_message;
2019-04-11 21:37:11 +02:00
unsigned NumUsrsWithEmail = 0;
unsigned NumAcceptedUsrsWithEmail = 0;
2017-03-07 19:55:29 +01:00
char StrAddresses[Mai_MAX_BYTES_STR_ADDR + 1]; // TODO: Use malloc depending on the number of students
2016-07-01 11:38:53 +02:00
unsigned int LengthStrAddr = 0;
struct Usr_Data UsrDat;
2019-04-11 21:37:11 +02:00
const char *Ptr;
2016-07-01 11:38:53 +02:00
/***** Begin the box used to list the emails *****/
2020-03-26 02:54:30 +01:00
Box_BoxBegin (NULL,Txt_Email_addresses,
NULL,NULL,
2020-09-26 17:20:01 +02:00
Hlp_COMMUNICATION_Email,Box_NOT_CLOSABLE);
2016-11-25 03:21:02 +01:00
/***** Begin list with users' email addresses *****/
HTM_DIV_Begin ("class=\"CM DAT_SMALL_%s\"",The_GetSuffix ());
2017-05-25 11:04:38 +02:00
/***** Initialize structure with user's data *****/
Usr_UsrDataConstructor (&UsrDat);
2016-11-25 03:21:02 +01:00
/***** Get email addresses of the selected users *****/
StrAddresses[0] = '\0';
Ptr = Gbl.Usrs.Selected.List[Rol_UNK];
while (*Ptr)
{
/* Get next user */
Par_GetNextStrUntilSeparParMult (&Ptr,UsrDat.EnUsrCod,
Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64);
Usr_GetUsrCodFromEncryptedUsrCod (&UsrDat);
2019-04-11 21:37:11 +02:00
/* Get user's email */
Mai_GetEmailFromUsrCod (&UsrDat);
2019-04-11 21:37:11 +02:00
if (UsrDat.Email[0])
2016-07-26 02:41:31 +02:00
{
NumUsrsWithEmail++;
/* Check if users has accepted inscription in current course */
UsrDat.Accepted = Enr_CheckIfUsrHasAcceptedInCurrentCrs (&UsrDat);
if (UsrDat.Accepted) // If student has email and has accepted
{
if (NumAcceptedUsrsWithEmail > 0)
{
HTM_Txt (", ");
LengthStrAddr ++;
if (LengthStrAddr > Mai_MAX_BYTES_STR_ADDR)
Err_ShowErrorAndExit ("The space allocated to store email addresses is full.");
Str_Concat (StrAddresses,",",sizeof (StrAddresses) - 1);
}
LengthStrAddr += strlen (UsrDat.Email);
if (LengthStrAddr > Mai_MAX_BYTES_STR_ADDR)
Err_ShowErrorAndExit ("The space allocated to store email addresses is full.");
Str_Concat (StrAddresses,UsrDat.Email,sizeof (StrAddresses) - 1);
HTM_A_Begin ("href=\"mailto:%s?subject=%s\"",
UsrDat.Email,Gbl.Hierarchy.Crs.FullName);
HTM_Txt (UsrDat.Email);
HTM_A_End ();
NumAcceptedUsrsWithEmail++;
}
2016-07-26 02:41:31 +02:00
}
2019-04-11 21:37:11 +02:00
}
2016-07-01 11:38:53 +02:00
/***** Free memory used for user's data *****/
Usr_UsrDataDestructor (&UsrDat);
/***** End list with users' email addresses *****/
HTM_DIV_End ();
/***** Show a message with the number of users with email ****/
HTM_DIV_Begin ("class=\"CM DAT_%s\"",The_GetSuffix ());
HTM_TxtF (Txt_X_users_who_have_email,NumUsrsWithEmail);
HTM_DIV_End ();
/***** Show a message with the number of users who have accepted and have email ****/
HTM_DIV_Begin ("class=\"CM DAT_%s\"",The_GetSuffix ());
HTM_TxtF (Txt_X_users_who_have_accepted_and_who_have_email,
NumAcceptedUsrsWithEmail);
HTM_DIV_End ();
/***** Contextual menu *****/
Mnu_ContextMenuBegin ();
/* Open the client email program */
HTM_A_Begin ("href=\"mailto:%s?subject=%s&cc=%s&bcc=%s\""
" title=\"%s\" class=\"FORM_OUT_%s BOLD\"",
Gbl.Usrs.Me.UsrDat.Email,
Gbl.Hierarchy.Crs.FullName,
Gbl.Usrs.Me.UsrDat.Email,
StrAddresses,
Txt_Create_email_message,
The_GetSuffix ());
Ico_PutIconTextLink ("marker.svg",Ico_BLACK,
Txt_Create_email_message);
HTM_A_End ();
Mnu_ContextMenuEnd ();
2017-05-25 11:04:38 +02:00
2017-06-12 14:16:33 +02:00
/***** End the box used to list the emails *****/
2019-10-25 22:48:34 +02:00
Box_BoxEnd ();
2016-07-01 11:38:53 +02:00
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/**************** Check whether an email address if valid ********************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
bool Mai_CheckIfEmailIsValid (const char *Email)
{
unsigned Length = strlen (Email);
unsigned LastPosArroba = Length - 4;
const char *Ptr;
unsigned Pos;
bool ArrobaFound = false;
2016-11-16 23:19:52 +01:00
/***** An email address must have a number of characters
2017-03-13 13:17:53 +01:00
5 <= Length <= Cns_MAX_BYTES_EMAIL_ADDRESS *****/
2014-12-01 23:55:08 +01:00
if (Length < 5 ||
2017-03-13 13:17:53 +01:00
Length > Cns_MAX_BYTES_EMAIL_ADDRESS)
2014-12-01 23:55:08 +01:00
return false;
2016-11-16 23:19:52 +01:00
/***** An email address can have digits, letters, '.', '-' and '_';
2014-12-01 23:55:08 +01:00
and must have one and only '@' (not in any position) *****/
for (Ptr = Email, Pos = 0;
*Ptr;
Ptr++)
if ((*Ptr >= 'a' && *Ptr <= 'z') ||
(*Ptr >= 'A' && *Ptr <= 'Z') ||
(*Ptr >= '0' && *Ptr <= '9') ||
*Ptr == '.' ||
*Ptr == '-' ||
*Ptr == '_')
Pos++;
else if (*Ptr == '@')
{
if (ArrobaFound)
return false;
/* Example: a@b.c
01234
Length = 5
LastPosArroba = 5 - 4 = 1 */
if (Pos == 0 || Pos > LastPosArroba)
return false;
ArrobaFound = true;
}
else
return false;
return ArrobaFound;
}
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/********** Get email address of a user from his/her user's code *************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
bool Mai_GetEmailFromUsrCod (struct Usr_Data *UsrDat)
2014-12-01 23:55:08 +01:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
bool Found;
/***** Get current (last updated) user's nickname from database *****/
if (Mai_DB_GetEmailFromUsrCod (&mysql_res,UsrDat->UsrCod))
2014-12-01 23:55:08 +01:00
{
2016-11-16 23:19:52 +01:00
/* Get email */
2017-01-15 18:02:52 +01:00
row = mysql_fetch_row (mysql_res);
Str_Copy (UsrDat->Email,row[0],sizeof (UsrDat->Email) - 1);
2016-09-07 18:48:10 +02:00
UsrDat->EmailConfirmed = (row[1][0] == 'Y');
2014-12-01 23:55:08 +01:00
Found = true;
}
else
{
UsrDat->Email[0] = '\0';
UsrDat->EmailConfirmed = false;
Found = false;
}
2014-12-01 23:55:08 +01:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return Found;
}
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/*********************** Show form to change my email ************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2019-02-15 21:09:18 +01:00
void Mai_ShowFormChangeMyEmail (bool IMustFillInEmail,bool IShouldConfirmEmail)
2018-10-15 14:07:12 +02:00
{
extern const char *Hlp_PROFILE_Account;
extern const char *Txt_Email;
2019-11-08 01:10:32 +01:00
char StrRecordWidth[Cns_MAX_DECIMAL_DIGITS_UINT + 2 + 1];
2018-10-15 14:07:12 +02:00
/***** Begin section *****/
2019-10-26 01:56:36 +02:00
HTM_SECTION_Begin (Mai_EMAIL_SECTION_ID);
2018-10-15 14:07:12 +02:00
/***** Begin box *****/
snprintf (StrRecordWidth,sizeof (StrRecordWidth),"%upx",Rec_RECORD_WIDTH);
Box_BoxBegin (StrRecordWidth,Txt_Email,
Acc_PutLinkToRemoveMyAccount,NULL,
Hlp_PROFILE_Account,Box_NOT_CLOSABLE);
2018-10-15 14:07:12 +02:00
/***** Show form to change email *****/
Mai_ShowFormChangeUsrEmail (Usr_ME,
IMustFillInEmail,
IShouldConfirmEmail);
2018-10-15 14:07:12 +02:00
/***** End box *****/
Box_BoxEnd ();
2018-10-15 14:07:12 +02:00
/***** End section *****/
2019-10-26 01:56:36 +02:00
HTM_SECTION_End ();
2018-10-15 14:07:12 +02:00
}
/*****************************************************************************/
/****************** Show form to change another user's email *****************/
/*****************************************************************************/
void Mai_ShowFormChangeOtherUsrEmail (void)
{
2018-10-16 01:36:13 +02:00
extern const char *Hlp_PROFILE_Account;
extern const char *Txt_Email;
2019-11-08 01:10:32 +01:00
char StrRecordWidth[Cns_MAX_DECIMAL_DIGITS_UINT + 2 + 1];
2018-10-16 01:36:13 +02:00
/***** Begin section *****/
2019-10-26 01:56:36 +02:00
HTM_SECTION_Begin (Mai_EMAIL_SECTION_ID);
2018-10-15 14:07:12 +02:00
/***** Begin box *****/
snprintf (StrRecordWidth,sizeof (StrRecordWidth),"%upx",Rec_RECORD_WIDTH);
Box_BoxBegin (StrRecordWidth,Txt_Email,
NULL,NULL,
Hlp_PROFILE_Account,Box_NOT_CLOSABLE);
2018-10-16 01:36:13 +02:00
/***** Show form to change email *****/
Mai_ShowFormChangeUsrEmail (Usr_OTHER,
false, // IMustFillInEmail
false); // IShouldConfirmEmail
2018-10-15 14:07:12 +02:00
/***** End box *****/
Box_BoxEnd ();
2018-10-16 01:36:13 +02:00
2018-10-15 14:07:12 +02:00
/***** End section *****/
2019-10-26 01:56:36 +02:00
HTM_SECTION_End ();
2018-10-15 14:07:12 +02:00
}
/*****************************************************************************/
/********************** Show form to change user's email *********************/
/*****************************************************************************/
static void Mai_ShowFormChangeUsrEmail (Usr_MeOrOther_t MeOrOther,
2020-10-14 00:59:24 +02:00
bool IMustFillInEmail,
bool IShouldConfirmEmail)
2014-12-01 23:55:08 +01:00
{
2019-02-15 21:09:18 +01:00
extern const char *Txt_Before_going_to_any_other_option_you_must_fill_in_your_email_address;
2018-10-16 15:16:32 +02:00
extern const char *Txt_Please_confirm_your_email_address;
2014-12-01 23:55:08 +01:00
extern const char *Txt_Current_email;
extern const char *Txt_Other_emails;
extern const char *Txt_Email_X_confirmed;
extern const char *Txt_Confirm_email;
extern const char *Txt_Use_this_email;
extern const char *Txt_New_email;
extern const char *Txt_Email;
extern const char *Txt_Change_email;
2019-02-18 18:27:45 +01:00
extern const char *Txt_Save_changes;
2014-12-01 23:55:08 +01:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumEmails;
unsigned NumEmail;
bool Confirmed;
char *Icon;
static const struct
{
Act_Action_t Remove;
Act_Action_t Change;
} NextAction[Rol_NUM_ROLES] =
{
[Rol_UNK ] = {ActRemMaiOth,ActChgMaiOth},
[Rol_GST ] = {ActRemMaiOth,ActChgMaiOth},
[Rol_USR ] = {ActRemMaiOth,ActChgMaiOth},
[Rol_STD ] = {ActRemMaiStd,ActChgMaiStd},
[Rol_NET ] = {ActRemMaiTch,ActChgMaiTch},
[Rol_TCH ] = {ActRemMaiTch,ActChgMaiTch},
[Rol_DEG_ADM] = {ActRemMaiOth,ActChgMaiOth},
[Rol_CTR_ADM] = {ActRemMaiOth,ActChgMaiOth},
[Rol_INS_ADM] = {ActRemMaiOth,ActChgMaiOth},
[Rol_SYS_ADM] = {ActRemMaiOth,ActChgMaiOth},
};
static const struct Usr_Data *UsrDat[Usr_NUM_ME_OR_OTHER] =
{
[Usr_ME ] = &Gbl.Usrs.Me.UsrDat,
[Usr_OTHER] = &Gbl.Usrs.Other.UsrDat
};
static void (*FuncParsRemove[Usr_NUM_ME_OR_OTHER]) (void *ID) =
{
[Usr_ME ] = Mai_PutParsRemoveMyEmail,
[Usr_OTHER] = Mai_PutParsRemoveOtherEmail
};
struct
{
Act_Action_t Remove;
Act_Action_t Change;
} ActMail[Rol_NUM_ROLES] =
{
[Usr_ME ] = {.Remove = ActRemMyMai,
.Change = ActChgMyMai},
[Usr_OTHER] = {.Remove = NextAction[Gbl.Usrs.Other.UsrDat.Roles.InCurrentCrs].Remove,
.Change = NextAction[Gbl.Usrs.Other.UsrDat.Roles.InCurrentCrs].Change}
};
2014-12-01 23:55:08 +01:00
2019-03-09 20:12:44 +01:00
/***** Show possible alerts *****/
Ale_ShowAlerts (Mai_EMAIL_SECTION_ID);
2018-10-15 14:07:12 +02:00
/***** Help message *****/
2019-02-15 21:09:18 +01:00
if (IMustFillInEmail)
2019-02-16 17:20:05 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_Before_going_to_any_other_option_you_must_fill_in_your_email_address);
2018-10-15 14:07:12 +02:00
else if (IShouldConfirmEmail)
2019-02-16 17:20:05 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_Please_confirm_your_email_address);
2018-10-15 14:07:12 +02:00
2016-11-16 23:19:52 +01:00
/***** Get my emails *****/
NumEmails = Mai_DB_GetMyEmails (&mysql_res,UsrDat[MeOrOther]->UsrCod);
2014-12-01 23:55:08 +01:00
2019-10-20 22:00:28 +02:00
/***** Begin table *****/
2019-10-23 19:05:05 +02:00
HTM_TABLE_BeginWidePadding (2);
2018-10-15 14:07:12 +02:00
/***** List emails *****/
for (NumEmail = 1;
NumEmail <= NumEmails;
NumEmail++)
2019-10-04 14:42:59 +02:00
{
/* Get email */
row = mysql_fetch_row (mysql_res);
Confirmed = (row[1][0] == 'Y');
2019-12-27 15:45:19 +01:00
if (NumEmail == 1)
{
HTM_TR_Begin (NULL);
2019-10-07 17:36:41 +02:00
/* The first mail is the current one */
/* Label */
Frm_LabelColumn ("REC_C1_BOT RT",NULL,Txt_Current_email);
2019-12-27 15:45:19 +01:00
/* Data */
HTM_TD_Begin ("class=\"REC_C2_BOT LT USR_ID\"");
}
else if (NumEmail == 2)
{
HTM_TR_Begin (NULL);
2019-10-07 17:36:41 +02:00
/* Label */
Frm_LabelColumn ("REC_C1_BOT RT",NULL,Txt_Other_emails);
2014-12-01 23:55:08 +01:00
/* Data */
HTM_TD_Begin ("class=\"REC_C2_BOT LT DAT_%s\"",
The_GetSuffix ());
}
/* Form to remove email */
Ico_PutContextualIconToRemove (ActMail[MeOrOther].Remove,Mai_EMAIL_SECTION_ID,
FuncParsRemove[MeOrOther],row[0]);
2014-12-01 23:55:08 +01:00
/* Email */
HTM_Txt (row[0]);
2014-12-01 23:55:08 +01:00
/* Email confirmed? */
if (Confirmed)
{
if (asprintf (&Icon,Txt_Email_X_confirmed,row[0]) < 0)
Err_NotEnoughMemoryExit ();
Ico_PutIcon ("check-circle.svg",Ico_GREEN,Icon,"ICO16x16");
free (Icon);
}
/* Form to change user's email */
if (NumEmail > 1 || (MeOrOther == Usr_ME && !Confirmed))
{
HTM_BR ();
Frm_BeginFormAnchor (ActMail[MeOrOther].Change,Mai_EMAIL_SECTION_ID);
if (MeOrOther == Usr_OTHER)
Usr_PutParUsrCodEncrypted (Gbl.Usrs.Other.UsrDat.EnUsrCod);
Par_PutParString (NULL,"NewEmail",row[0]);
Btn_PutConfirmButtonInline ((MeOrOther == Usr_ME && NumEmail == 1) ? Txt_Confirm_email :
Txt_Use_this_email);
Frm_EndForm ();
}
if (NumEmail == 1 ||
NumEmail == NumEmails)
{
HTM_TD_End ();
HTM_TR_End ();
}
else
HTM_BR ();
2014-12-01 23:55:08 +01:00
}
/***** Form to enter new email *****/
HTM_TR_Begin (NULL);
/* Label */
Frm_LabelColumn ("REC_C1_BOT RT","NewEmail",
NumEmails ? Txt_New_email : // A new email
Txt_Email); // The first email
/* Data */
HTM_TD_Begin ("class=\"REC_C2_BOT LT DAT_%s\"",
The_GetSuffix ());
Frm_BeginFormAnchor (ActMail[MeOrOther].Change,Mai_EMAIL_SECTION_ID);
if (MeOrOther == Usr_OTHER)
Usr_PutParUsrCodEncrypted (Gbl.Usrs.Other.UsrDat.EnUsrCod);
HTM_INPUT_EMAIL ("NewEmail",Cns_MAX_CHARS_EMAIL_ADDRESS,UsrDat[MeOrOther]->Email,
"id=\"NewEmail\" class=\"INPUT_%s\" size=\"18\"",
The_GetSuffix ());
HTM_BR ();
Btn_PutCreateButtonInline (NumEmails ? Txt_Change_email : // User already has an email address
Txt_Save_changes); // User has no email address yet
Frm_EndForm ();
HTM_TD_End ();
2019-10-07 17:36:41 +02:00
HTM_TR_End ();
2018-10-15 14:07:12 +02:00
/***** End table *****/
2019-10-23 19:05:05 +02:00
HTM_TABLE_End ();
2014-12-01 23:55:08 +01:00
}
static void Mai_PutParsRemoveMyEmail (void *Email)
2020-10-14 00:59:24 +02:00
{
if (Email)
Par_PutParString (NULL,"Email",Email);
2020-10-14 00:59:24 +02:00
}
static void Mai_PutParsRemoveOtherEmail (void *Email)
2020-10-14 00:59:24 +02:00
{
if (Email)
{
Usr_PutParUsrCodEncrypted (Gbl.Usrs.Other.UsrDat.EnUsrCod);
Par_PutParString (NULL,"Email",Email);
2020-10-14 00:59:24 +02:00
}
}
2015-11-16 14:54:12 +01:00
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/******************** Remove one of my user's emails *************************/
2015-11-16 14:54:12 +01:00
/*****************************************************************************/
void Mai_RemoveMyUsrEmail (void)
{
2016-11-16 23:19:52 +01:00
/***** Remove user's email *****/
2015-11-16 14:54:12 +01:00
Mai_RemoveEmail (&Gbl.Usrs.Me.UsrDat);
/***** Show my account again *****/
2018-10-15 14:07:12 +02:00
Acc_ShowFormChgMyAccount ();
2015-11-16 14:54:12 +01:00
}
/*****************************************************************************/
/**************** Remove one of the user's IDs of another user ***************/
/*****************************************************************************/
void Mai_RemoveOtherUsrEmail (void)
{
/***** Get other user's code from form and get user's data *****/
if (Usr_GetParOtherUsrCodEncryptedAndGetUsrData ())
2015-11-16 14:54:12 +01:00
{
2018-10-16 08:48:51 +02:00
if (Usr_ICanEditOtherUsr (&Gbl.Usrs.Other.UsrDat))
{
/***** Remove user's email *****/
Mai_RemoveEmail (&Gbl.Usrs.Other.UsrDat);
2015-11-16 14:54:12 +01:00
2018-10-16 08:48:51 +02:00
/***** Show form again *****/
Acc_ShowFormChgOtherUsrAccount ();
}
else
2019-03-09 20:12:44 +01:00
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
2015-11-16 14:54:12 +01:00
}
else // User not found
2019-03-09 20:12:44 +01:00
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
2015-11-16 14:54:12 +01:00
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/************************** Remove email address *****************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
static void Mai_RemoveEmail (struct Usr_Data *UsrDat)
2014-12-01 23:55:08 +01:00
{
extern const char *Txt_Email_X_removed;
2017-03-13 13:17:53 +01:00
char Email[Cns_MAX_BYTES_EMAIL_ADDRESS + 1];
2014-12-01 23:55:08 +01:00
2017-01-28 18:08:06 +01:00
if (Usr_ICanEditOtherUsr (UsrDat))
2014-12-01 23:55:08 +01:00
{
2016-11-16 23:19:52 +01:00
/***** Get new email from form *****/
Par_GetParText ("Email",Email,Cns_MAX_BYTES_EMAIL_ADDRESS);
2015-11-16 14:54:12 +01:00
2016-11-16 23:19:52 +01:00
/***** Remove one of user's old email addresses *****/
Mai_DB_RemoveEmail (UsrDat->UsrCod,Email);
2014-12-01 23:55:08 +01:00
2019-03-09 20:12:44 +01:00
/***** Create alert *****/
Ale_CreateAlert (Ale_SUCCESS,Mai_EMAIL_SECTION_ID,
Txt_Email_X_removed,
Email);
2015-11-16 17:39:59 +01:00
2016-11-16 23:19:52 +01:00
/***** Update list of emails *****/
2015-11-16 17:39:59 +01:00
Mai_GetEmailFromUsrCod (UsrDat);
2014-12-01 23:55:08 +01:00
}
else
2019-03-09 20:12:44 +01:00
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
2014-12-01 23:55:08 +01:00
}
2015-11-16 14:54:12 +01:00
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/************************* New user's email for me ***************************/
2015-11-16 14:54:12 +01:00
/*****************************************************************************/
void May_NewMyUsrEmail (void)
{
2016-11-16 23:19:52 +01:00
/***** Remove user's email *****/
Mai_ChangeUsrEmail (&Gbl.Usrs.Me.UsrDat,Usr_ME);
2015-11-16 14:54:12 +01:00
/***** Show my account again *****/
2018-10-15 14:07:12 +02:00
Acc_ShowFormChgMyAccount ();
2015-11-16 14:54:12 +01:00
}
/*****************************************************************************/
/************************ Change another user's email ************************/
2015-11-16 14:54:12 +01:00
/*****************************************************************************/
void Mai_ChangeOtherUsrEmail (void)
2015-11-16 14:54:12 +01:00
{
/***** Get other user's code from form and get user's data *****/
if (Usr_GetParOtherUsrCodEncryptedAndGetUsrData ())
2015-11-16 14:54:12 +01:00
{
2018-10-16 08:48:51 +02:00
if (Usr_ICanEditOtherUsr (&Gbl.Usrs.Other.UsrDat))
{
/***** Change user's ID *****/
Mai_ChangeUsrEmail (&Gbl.Usrs.Other.UsrDat,
Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod));
2015-11-16 14:54:12 +01:00
2018-10-16 08:48:51 +02:00
/***** Show form again *****/
Acc_ShowFormChgOtherUsrAccount ();
}
else
2019-03-09 20:12:44 +01:00
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
2015-11-16 14:54:12 +01:00
}
else // User not found
2019-03-09 20:12:44 +01:00
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
2015-11-16 14:54:12 +01:00
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/*************************** Change email address ****************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
static void Mai_ChangeUsrEmail (struct Usr_Data *UsrDat,Usr_MeOrOther_t MeOrOther)
2014-12-01 23:55:08 +01:00
{
2015-11-16 14:54:12 +01:00
extern const char *Txt_The_email_address_X_matches_one_previously_registered;
extern const char *Txt_The_email_address_X_has_been_registered_successfully;
2014-12-01 23:55:08 +01:00
extern const char *Txt_The_email_address_X_had_been_registered_by_another_user;
extern const char *Txt_The_email_address_entered_X_is_not_valid;
2017-03-13 13:17:53 +01:00
char NewEmail[Cns_MAX_BYTES_EMAIL_ADDRESS + 1];
2014-12-01 23:55:08 +01:00
2017-01-28 18:08:06 +01:00
if (Usr_ICanEditOtherUsr (UsrDat))
2014-12-01 23:55:08 +01:00
{
2016-11-16 23:19:52 +01:00
/***** Get new email from form *****/
Par_GetParText ("NewEmail",NewEmail,Cns_MAX_BYTES_EMAIL_ADDRESS);
2015-11-16 14:54:12 +01:00
2016-11-16 23:19:52 +01:00
if (Mai_CheckIfEmailIsValid (NewEmail)) // New email is valid
2014-12-01 23:55:08 +01:00
{
2016-11-16 23:19:52 +01:00
/***** Check if new email exists in database *****/
2015-11-16 14:54:12 +01:00
if (UsrDat->EmailConfirmed &&
!strcmp (UsrDat->Email,NewEmail)) // User's current confirmed email match exactly the new email
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_WARNING,Mai_EMAIL_SECTION_ID,
Txt_The_email_address_X_matches_one_previously_registered,
NewEmail);
2014-12-01 23:55:08 +01:00
else
2015-11-16 14:54:12 +01:00
{
if (Mai_UpdateEmailInDB (UsrDat,NewEmail))
{
2016-11-16 23:19:52 +01:00
/***** Email updated sucessfully *****/
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_SUCCESS,Mai_EMAIL_SECTION_ID,
Txt_The_email_address_X_has_been_registered_successfully,
NewEmail);
2015-11-16 14:54:12 +01:00
2016-11-16 23:19:52 +01:00
/***** Update list of emails *****/
2015-11-16 17:39:59 +01:00
Mai_GetEmailFromUsrCod (UsrDat);
/***** Send message via email
to confirm the new email address *****/
if (MeOrOther == Usr_ME)
2016-10-10 20:02:47 +02:00
Mai_SendMailMsgToConfirmEmail ();
2015-11-16 14:54:12 +01:00
}
else
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_WARNING,Mai_EMAIL_SECTION_ID,
Txt_The_email_address_X_had_been_registered_by_another_user,
NewEmail);
2014-12-01 23:55:08 +01:00
}
2015-11-16 14:54:12 +01:00
}
2016-11-16 23:19:52 +01:00
else // New email is not valid
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_WARNING,Mai_EMAIL_SECTION_ID,
Txt_The_email_address_entered_X_is_not_valid,
NewEmail);
2014-12-01 23:55:08 +01:00
}
2015-11-16 14:54:12 +01:00
else
2019-03-09 20:12:44 +01:00
Ale_CreateAlertUserNotFoundOrYouDoNotHavePermission ();
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/************************ Update email in database ***************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
// Return true if email is successfully updated
// Return false if email can not be updated beacuse it is registered by another user
2014-12-01 23:55:08 +01:00
bool Mai_UpdateEmailInDB (const struct Usr_Data *UsrDat,const char NewEmail[Cns_MAX_BYTES_EMAIL_ADDRESS + 1])
2014-12-01 23:55:08 +01:00
{
2016-11-16 23:19:52 +01:00
/***** Check if the new email matches any of the confirmed emails of other users *****/
if (Mai_DB_CheckIfEmailBelongToAnotherUsr (UsrDat->UsrCod,NewEmail)) // An email of another user is the same that my email
2014-12-01 23:55:08 +01:00
return false; // Don't update
2016-11-16 23:19:52 +01:00
/***** Delete email (not confirmed) for other users *****/
Mai_DB_RemovePendingEmailForOtherUsrs (UsrDat->UsrCod,NewEmail);
Mai_DB_RemoveNotConfirmedEmailForOtherUsrs (UsrDat->UsrCod,NewEmail);
2014-12-01 23:55:08 +01:00
2016-11-16 23:19:52 +01:00
/***** Update email in database *****/
Mai_DB_UpdateEmail (UsrDat->UsrCod,NewEmail);
2014-12-01 23:55:08 +01:00
return true; // Successfully updated
}
2016-10-10 19:25:25 +02:00
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/************** Send mail message to confirm my email address ****************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
// Return true on success
// Return false on error
bool Mai_SendMailMsgToConfirmEmail (void)
{
extern const char *Txt_If_you_just_requested_from_X_the_confirmation_of_your_email_Y_NO_HTML;
2014-12-01 23:55:08 +01:00
extern const char *Txt_Confirmation_of_your_email_NO_HTML;
2018-10-15 14:07:12 +02:00
extern const char *Txt_A_message_has_been_sent_to_email_address_X_to_confirm_that_address;
2014-12-01 23:55:08 +01:00
extern const char *Txt_There_was_a_problem_sending_an_email_automatically;
2020-04-11 15:21:30 +02:00
char FileNameMail[PATH_MAX + 1];
FILE *FileMail;
Lan_Language_t ToUsrLanguage;
const char *UniqueNameEncrypted = Cry_GetUniqueNameEncrypted ();
2014-12-01 23:55:08 +01:00
int ReturnCode;
/***** Create temporary file for mail content *****/
2020-04-11 15:21:30 +02:00
Mai_CreateFileNameMail (FileNameMail,&FileMail);
2014-12-01 23:55:08 +01:00
/***** If I have no language, set language to current language *****/
ToUsrLanguage = Gbl.Usrs.Me.UsrDat.Prefs.Language;
if (ToUsrLanguage == Lan_LANGUAGE_UNKNOWN)
ToUsrLanguage = Gbl.Prefs.Language;
2014-12-01 23:55:08 +01:00
/***** Write mail content into file and close file *****/
/* Welcome note */
Mai_WriteWelcomeNoteEMail (FileMail,&Gbl.Usrs.Me.UsrDat,ToUsrLanguage);
2014-12-01 23:55:08 +01:00
/* Store encrypted key in database */
Mai_InsertMailKey (Gbl.Usrs.Me.UsrDat.Email,UniqueNameEncrypted);
2014-12-01 23:55:08 +01:00
/* Message body */
2020-04-11 15:21:30 +02:00
fprintf (FileMail,
Txt_If_you_just_requested_from_X_the_confirmation_of_your_email_Y_NO_HTML,
2016-07-08 12:43:48 +02:00
Cfg_URL_SWAD_CGI,Gbl.Usrs.Me.UsrDat.Email,
Cfg_URL_SWAD_CGI,Act_GetActCod (ActCnfMai),UniqueNameEncrypted,
2016-07-08 12:43:48 +02:00
Cfg_URL_SWAD_CGI);
2014-12-01 23:55:08 +01:00
/* Footer note */
Mai_WriteFootNoteEMail (FileMail,ToUsrLanguage);
2014-12-01 23:55:08 +01:00
2020-04-11 15:21:30 +02:00
fclose (FileMail);
2014-12-01 23:55:08 +01:00
2016-11-16 23:19:52 +01:00
/***** Call the script to send an email *****/
ReturnCode = Mai_SendMailMsg (FileNameMail,
Txt_Confirmation_of_your_email_NO_HTML,
Gbl.Usrs.Me.UsrDat.Email);
2014-12-01 23:55:08 +01:00
/***** Remove temporary file *****/
2020-04-11 15:21:30 +02:00
unlink (FileNameMail);
2014-12-01 23:55:08 +01:00
/***** Write message depending on return code *****/
switch (ReturnCode)
{
case 0: // Message sent successfully
2016-10-10 19:50:24 +02:00
Gbl.Usrs.Me.ConfirmEmailJustSent = true;
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_SUCCESS,Mai_EMAIL_SECTION_ID,
Txt_A_message_has_been_sent_to_email_address_X_to_confirm_that_address,
Gbl.Usrs.Me.UsrDat.Email);
2014-12-01 23:55:08 +01:00
return true;
case 1:
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_ERROR,Mai_EMAIL_SECTION_ID,
Txt_There_was_a_problem_sending_an_email_automatically);
2014-12-01 23:55:08 +01:00
return false;
default:
2019-03-09 20:12:44 +01:00
Ale_CreateAlert (Ale_ERROR,Mai_EMAIL_SECTION_ID,
"Internal error: an email message has not been sent successfully."
" Error code returned by the script: %d",
ReturnCode);
2014-12-01 23:55:08 +01:00
return false;
}
}
2016-10-10 19:50:24 +02:00
/*****************************************************************************/
/************************* Insert mail hey in database ***********************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2017-03-13 13:17:53 +01:00
static void Mai_InsertMailKey (const char Email[Cns_MAX_BYTES_EMAIL_ADDRESS + 1],
2017-01-28 15:58:46 +01:00
const char MailKey[Mai_LENGTH_EMAIL_CONFIRM_KEY + 1])
2014-12-01 23:55:08 +01:00
{
2016-11-16 23:19:52 +01:00
/***** Remove expired pending emails from database *****/
Mai_DB_RemoveExpiredPendingEmails ();
2014-12-01 23:55:08 +01:00
/***** Insert mail key in database *****/
Mai_DB_InsertPendingEmail (Email,MailKey);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/************************* Confirm my email address **************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
void Mai_ConfirmEmail (void)
{
extern const char *Txt_Failed_email_confirmation_key;
2014-12-01 23:55:08 +01:00
extern const char *Txt_Email_X_has_already_been_confirmed_before;
extern const char *Txt_The_email_X_has_been_confirmed;
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2017-01-28 15:58:46 +01:00
char MailKey[Mai_LENGTH_EMAIL_CONFIRM_KEY + 1];
2014-12-01 23:55:08 +01:00
long UsrCod;
2017-03-13 13:17:53 +01:00
char Email[Cns_MAX_BYTES_EMAIL_ADDRESS + 1];
2018-11-09 21:02:52 +01:00
bool KeyIsCorrect;
2014-12-01 23:55:08 +01:00
/***** Get parameter Key *****/
Par_GetParText ("key",MailKey,Mai_LENGTH_EMAIL_CONFIRM_KEY);
2014-12-01 23:55:08 +01:00
2016-11-16 23:19:52 +01:00
/***** Get user's code and email from key *****/
if (Mai_DB_GetPendingEmail (&mysql_res,MailKey))
2014-12-01 23:55:08 +01:00
{
row = mysql_fetch_row (mysql_res);
/* Get user's code (row[0]) */
2014-12-01 23:55:08 +01:00
UsrCod = Str_ConvertStrCodToLongCod (row[0]);
/* Get user's email (row[1]) */
Str_Copy (Email,row[1],sizeof (Email) - 1);
2014-12-01 23:55:08 +01:00
KeyIsCorrect = true;
}
2018-11-09 21:02:52 +01:00
else
{
row = NULL;
UsrCod = -1L;
KeyIsCorrect = false;
}
2014-12-01 23:55:08 +01:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
if (KeyIsCorrect)
{
/***** Delete this pending email *****/
Mai_DB_RemovePendingEmail (MailKey);
2014-12-01 23:55:08 +01:00
2016-11-16 23:19:52 +01:00
/***** Check user's code and email
and get if email is already confirmed *****/
switch (Mai_DB_CheckIfEmailIsConfirmed (UsrCod,Email))
{
case '\0':
Ale_ShowAlert (Ale_WARNING,Txt_Failed_email_confirmation_key);
break;
case 'Y':
2019-02-16 17:20:05 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_Email_X_has_already_been_confirmed_before,
Email);
break;
default:
/***** Confirm email *****/
Mai_DB_ConfirmEmail (UsrCod,Email);
2019-02-16 17:20:05 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_The_email_X_has_been_confirmed,
Email);
break;
}
2014-12-01 23:55:08 +01:00
}
/***** Form to log in *****/
2016-12-05 13:26:12 +01:00
Usr_WriteFormLogin (ActLogIn,NULL);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/****************** Create temporary file for mail content *******************/
/*****************************************************************************/
2020-04-11 15:21:30 +02:00
void Mai_CreateFileNameMail (char FileNameMail[PATH_MAX + 1],FILE **FileMail)
2014-12-01 23:55:08 +01:00
{
snprintf (FileNameMail,PATH_MAX + 1,"%s/%s_mail.txt",
Cfg_PATH_OUT_PRIVATE,Cry_GetUniqueNameEncrypted ());
2020-04-11 15:21:30 +02:00
if ((*FileMail = fopen (FileNameMail,"wb")) == NULL)
Err_ShowErrorAndExit ("Can not open file to send email.");
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/************ Write a welcome note heading the automatic email ***************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
void Mai_WriteWelcomeNoteEMail (FILE *FileMail,const struct Usr_Data *UsrDat,
Lan_Language_t ToUsrLanguage)
2014-12-01 23:55:08 +01:00
{
2018-12-08 16:43:13 +01:00
extern const char *Txt_Dear_NO_HTML[Usr_NUM_SEXS][1 + Lan_NUM_LANGUAGES];
extern const char *Txt_user_NO_HTML[Usr_NUM_SEXS][1 + Lan_NUM_LANGUAGES];
2014-12-01 23:55:08 +01:00
2020-04-11 15:21:30 +02:00
fprintf (FileMail,"%s %s:\n",
Txt_Dear_NO_HTML[UsrDat->Sex][ToUsrLanguage],
UsrDat->FrstName[0] ? UsrDat->FrstName :
Txt_user_NO_HTML[UsrDat->Sex][ToUsrLanguage]);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/****************** Write a foot note in the automatic email *****************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2020-04-11 15:21:30 +02:00
void Mai_WriteFootNoteEMail (FILE *FileMail,Lan_Language_t Language)
2014-12-01 23:55:08 +01:00
{
2018-12-08 16:43:13 +01:00
extern const char *Txt_Please_do_not_reply_to_this_automatically_generated_email_NO_HTML[1 + Lan_NUM_LANGUAGES];
2014-12-01 23:55:08 +01:00
2020-04-11 15:21:30 +02:00
fprintf (FileMail,"%s\n"
"%s\n"
"%s\n",
2014-12-01 23:55:08 +01:00
Txt_Please_do_not_reply_to_this_automatically_generated_email_NO_HTML[Language],
2015-07-24 11:34:39 +02:00
Cfg_PLATFORM_SHORT_NAME,
2016-07-08 12:43:48 +02:00
Cfg_URL_SWAD_CGI);
2014-12-01 23:55:08 +01:00
}
2016-04-23 13:23:09 +02:00
/*****************************************************************************/
2016-11-16 23:19:52 +01:00
/**************** Check if I can see another user's email ********************/
2016-04-23 13:23:09 +02:00
/*****************************************************************************/
bool Mai_ICanSeeOtherUsrEmail (const struct Usr_Data *UsrDat)
2016-04-23 13:23:09 +02:00
{
/***** I can see my email *****/
if (Usr_ItsMe (UsrDat->UsrCod) == Usr_ME)
2016-04-23 13:23:09 +02:00
return true;
2017-05-22 14:52:11 +02:00
/***** Check if I have permission to see another user's email *****/
2017-06-04 18:18:54 +02:00
switch (Gbl.Usrs.Me.Role.Logged)
2016-04-23 13:23:09 +02:00
{
2017-05-18 19:13:41 +02:00
case Rol_STD:
2017-01-27 18:45:38 +01:00
/* If I am a student in the current course,
I can see the email of confirmed teachers */
return (UsrDat->Roles.InCurrentCrs == Rol_NET || // A non-editing teacher
UsrDat->Roles.InCurrentCrs == Rol_TCH) && // or a teacher
2017-05-22 14:52:11 +02:00
UsrDat->Accepted; // who accepted registration
case Rol_NET:
2017-05-18 19:13:41 +02:00
case Rol_TCH:
2017-01-27 18:45:38 +01:00
/* If I am a teacher in the current course,
I can see the email of confirmed students and teachers */
return Enr_CheckIfUsrBelongsToCurrentCrs (UsrDat) && // A user belonging to the current course
2017-06-08 15:32:33 +02:00
UsrDat->Accepted; // who accepted registration
2016-04-23 13:23:09 +02:00
case Rol_DEG_ADM:
/* If I am an administrator of current degree,
2016-11-16 23:19:52 +01:00
I only can see the user's email of users from current degree */
return Deg_CheckIfUsrBelongsToDeg (UsrDat->UsrCod,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Deg.DegCod);
2016-04-23 13:23:09 +02:00
case Rol_CTR_ADM:
/* If I am an administrator of current center,
I only can see the user's email of users from current center */
return Ctr_CheckIfUsrBelongsToCtr (UsrDat->UsrCod,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ctr.CtrCod);
2016-04-23 13:23:09 +02:00
case Rol_INS_ADM:
/* If I am an administrator of current institution,
2016-11-16 23:19:52 +01:00
I only can see the user's email of users from current institution */
return Ins_CheckIfUsrBelongsToIns (UsrDat->UsrCod,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ins.InsCod);
2016-04-23 13:23:09 +02:00
case Rol_SYS_ADM:
return true;
default:
return false;
}
}
2019-04-09 14:04:04 +02:00
/*****************************************************************************/
/************************* Initialize mail domain list ***********************/
/*****************************************************************************/
static void Mai_InitializeMailDomainList (struct Mai_Mails *Mails)
{
Mails->Num = 0;
Mails->Lst = NULL;
Mails->SelectedOrder = Mai_ORDER_DEFAULT;
}
2019-04-09 14:04:04 +02:00
/*****************************************************************************/
/********************** Mail domain constructor/destructor *******************/
/*****************************************************************************/
static void Mai_EditingMailDomainConstructor (void)
{
/***** Pointer must be NULL *****/
if (Mai_EditingMai != NULL)
Err_WrongMailDomainExit ();
2019-04-09 14:04:04 +02:00
/***** Allocate memory for mail domain *****/
if ((Mai_EditingMai = malloc (sizeof (*Mai_EditingMai))) == NULL)
Err_NotEnoughMemoryExit ();
2019-04-09 14:04:04 +02:00
/***** Reset place *****/
Mai_EditingMai->MaiCod = -1L;
Mai_EditingMai->Domain[0] = '\0';
Mai_EditingMai->Info[0] = '\0';
Mai_EditingMai->NumUsrs = 0;
}
static void Mai_EditingMailDomainDestructor (void)
{
/***** Free memory used for mail domain *****/
if (Mai_EditingMai != NULL)
{
2019-11-06 19:45:20 +01:00
free (Mai_EditingMai);
2019-04-09 14:04:04 +02:00
Mai_EditingMai = NULL;
}
}
/*****************************************************************************/
/***************************** Send mail message *****************************/
/*****************************************************************************/
// Return 0 on success
// Return != 0 on error
int Mai_SendMailMsg (const char FileNameMail[PATH_MAX + 1],
const char *Subject,
const char ToEmail[Cns_MAX_BYTES_EMAIL_ADDRESS + 1])
{
char Command[2048 +
Cfg_MAX_BYTES_SMTP_PASSWORD +
Cns_MAX_BYTES_EMAIL_ADDRESS +
PATH_MAX]; // Command to execute for sending an email
int ReturnCode;
/***** Call the script to send an email *****/
snprintf (Command,sizeof (Command),
"%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"[%s] %s\" \"%s\"",
Cfg_COMMAND_SEND_AUTOMATIC_EMAIL,
Cfg_AUTOMATIC_EMAIL_SMTP_SERVER,
Cfg_AUTOMATIC_EMAIL_SMTP_PORT,
Cfg_AUTOMATIC_EMAIL_FROM,
Cfg_GetSMTPPassword (),
ToEmail,
Cfg_PLATFORM_SHORT_NAME,Subject,
FileNameMail);
ReturnCode = system (Command);
if (ReturnCode == -1) // The value returned is -1 on error
Err_ShowErrorAndExit ("Error when running script to send email.");
/* The exit code of the command will be WEXITSTATUS (ReturnCode) */
return WEXITSTATUS (ReturnCode);
}