swad-core/swad_main.c

208 lines
6.5 KiB
C
Raw Normal View History

2014-12-01 23:55:08 +01:00
// swad.c: main
/*
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-2024 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 *********************************/
/*****************************************************************************/
2019-12-29 12:39:00 +01:00
#include <stddef.h> // For NULL
2014-12-01 23:55:08 +01:00
#include <stdlib.h> // For exit
#include <string.h>
#include <unistd.h> // For sleep
#include "swad_action.h"
#include "swad_action_list.h"
2019-04-01 23:15:17 +02:00
#include "swad_announcement.h"
2014-12-01 23:55:08 +01:00
#include "swad_config.h"
#include "swad_connected_database.h"
2014-12-01 23:55:08 +01:00
#include "swad_database.h"
#include "swad_error.h"
2019-02-13 13:32:11 +01:00
#include "swad_firewall.h"
#include "swad_firewall_database.h"
2014-12-01 23:55:08 +01:00
#include "swad_global.h"
2016-11-14 16:47:46 +01:00
#include "swad_hierarchy.h"
#include "swad_hierarchy_type.h"
2016-06-03 13:42:28 +02:00
#include "swad_MFU.h"
2014-12-01 23:55:08 +01:00
#include "swad_notification.h"
2019-03-26 11:53:21 +01:00
#include "swad_parameter.h"
#include "swad_session.h"
#include "swad_session_database.h"
2019-03-26 11:53:21 +01:00
#include "swad_setting.h"
2019-04-01 23:15:17 +02:00
#include "swad_user.h"
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/******************************** Constants **********************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/****************************** Main function ********************************/
/*****************************************************************************/
2017-06-03 21:48:02 +02:00
int main (void)
2014-12-01 23:55:08 +01:00
{
2018-04-24 13:21:53 +02:00
void (*FunctionPriori) (void);
void (*FunctionPosteriori) (void);
2015-03-09 00:16:18 +01:00
/*
"touch swad.lock" in CGI directory if you want to disable SWAD
"rm swad.lock" in CGI directory if you want to enable SWAD
*/
if (Fil_CheckIfPathExists ("./swad.lock"))
{
fprintf (stdout,"Content-type: text/html; charset=windows-1252\r\n"
"Status: 503 Service Temporarily Unavailable\r\n\r\n"
"<html lang=\"es\">"
"<head><title>%s</title></head>"
"<body><br /><br /><br /><br />"
2019-10-15 15:23:38 +02:00
"<h1 class=\"CM\">"
2020-04-30 01:55:23 +02:00
"%s est&aacute; parado un momento por mantenimiento."
2015-08-06 13:18:36 +02:00
"</h1>"
2019-10-15 15:23:38 +02:00
"<h2 class=\"CM\">"
2020-04-30 01:55:23 +02:00
"Intente acceder pasados unos minutos, por favor."
2015-08-06 13:18:36 +02:00
"</h2>"
2015-03-09 00:16:18 +01:00
"</body>"
"</html>",
2016-03-20 22:31:57 +01:00
Cfg_PLATFORM_SHORT_NAME,
2015-03-09 00:16:18 +01:00
Cfg_PLATFORM_SHORT_NAME);
exit (0);
}
2014-12-01 23:55:08 +01:00
/***** Initialize global variables *****/
Gbl_InitializeGlobals ();
Cfg_GetConfigFromFile ();
/***** Open database connection *****/
DB_OpenDBConnection ();
/***** Read parameters *****/
if (Par_GetQueryString ())
{
2019-02-15 21:09:18 +01:00
/***** Get parameters *****/
Par_CreateListOfPars ();
Par_GetMainPars ();
2019-02-13 13:32:11 +01:00
/***** Kick out banned IPs *****/
Fir_CheckFirewallAndExitIfBanned ();
2019-02-11 21:35:36 +01:00
/**** Initialize current country, institution, center, degree and course *****/
2016-11-14 17:26:32 +01:00
Hie_InitHierarchy ();
2014-12-01 23:55:08 +01:00
if (!Gbl.WebService.IsWebService)
{
2014-12-21 14:47:04 +01:00
/***** Create file for HTML output *****/
Fil_CreateFileForHTMLOutput ();
2014-12-01 23:55:08 +01:00
/***** Remove old (expired) sessions *****/
Ses_DB_RemoveExpiredSessions ();
2014-12-01 23:55:08 +01:00
/***** Remove old users from connected list *****/
Con_DB_RemoveOldConnected ();
2014-12-01 23:55:08 +01:00
/***** Get number of sessions *****/
2019-04-19 13:11:54 +02:00
switch (Act_GetBrowserTab (Gbl.Action.Act))
{
case Act_1ST:
case Act_REF:
2019-04-19 13:11:54 +02:00
Ses_GetNumSessions ();
break;
default:
break;
}
2014-12-01 23:55:08 +01:00
/***** Check user and get user's data *****/
Usr_ChkUsrAndGetUsrData ();
}
/***** Mitigate automatized attacks from the same IP-user *****/
// If this execution is web service, no user is logged at this moment...
// ...so only IP is checked and it could be banned...
// ...if many users use the web service from the same IP
Fir_DB_LogAccess ();
Fir_CheckFirewallAndExitIfTooManyRequests ();
2014-12-01 23:55:08 +01:00
/***** Check if the user have permission to execute the action *****/
if (Act_CheckIfICanExecuteAction (Gbl.Action.Act) == Usr_CAN_NOT)
Err_NoPermissionExit ();
2014-12-01 23:55:08 +01:00
/***** Update most frequently used actions *****/
2016-06-03 13:42:28 +02:00
MFU_UpdateMFUActions ();
2014-12-01 23:55:08 +01:00
/***** Execute a function depending on the action *****/
2018-04-24 13:21:53 +02:00
FunctionPriori = Act_GetFunctionPriori (Gbl.Action.Act);
if (FunctionPriori != NULL)
FunctionPriori ();
2014-12-01 23:55:08 +01:00
if (Act_GetBrowserTab (Gbl.Action.Act) == Act_204)
2017-10-04 12:20:58 +02:00
/***** Write HTTP Status 204 No Content *****/
Lay_WriteHTTPStatus204NoContent ();
/***** Begin writing HTML output *****/
2014-12-01 23:55:08 +01:00
Lay_WriteStartOfPage ();
2019-04-01 23:15:17 +02:00
/***** If I am been redirected from another action... *****/
switch (Gbl.Action.Original)
{
case ActLogIn:
case ActLogInNew:
case ActLogInLan:
Usr_WelcomeUsr ();
break;
case ActAnnSee:
Ann_MarkAnnouncementAsSeen ();
break;
default:
break;
}
2014-12-01 23:55:08 +01:00
/***** Make a processing or other depending on the action *****/
2018-04-24 13:21:53 +02:00
FunctionPosteriori = Act_GetFunctionPosteriori (Gbl.Action.Act);
if (FunctionPosteriori != NULL)
FunctionPosteriori ();
2019-04-19 13:11:54 +02:00
/* When updating a small zone via AJAX, all output is already done */
switch (Act_GetBrowserTab (Gbl.Action.Act))
{
case Act_AJA:
case Act_REF:
case Act_SVC:
2019-04-19 13:11:54 +02:00
/* All the output is made, so don't write anymore */
Gbl.Layout.DivsEndWritten =
Gbl.Layout.HTMLEndWritten = true;
break;
default:
break;
}
2014-12-01 23:55:08 +01:00
}
/***** Cleanup and exit *****/
Err_ShowErrorAndExit (NULL);
2014-12-01 23:55:08 +01:00
return 0; // Control don't reach this point. Used to avoid warning.
}