swad-core/swad_logo.c

452 lines
14 KiB
C
Raw Normal View History

2015-01-17 20:07:13 +01:00
// swad_logo.c: logo of institution, centre or degree
/*
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.
2019-01-07 21:52:19 +01:00
Copyright (C) 1999-2019 Antonio Ca<EFBFBD>as Vargas
2015-01-17 20:07:13 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General 3 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-10-30 22:31:03 +01:00
#define _GNU_SOURCE // For asprintf
#include <stdio.h> // For asprintf
2015-01-17 20:07:13 +01:00
#include <string.h> // For string functions
#include "swad_action.h"
2017-06-10 21:38:10 +02:00
#include "swad_box.h"
2018-11-09 20:47:39 +01:00
#include "swad_form.h"
2015-01-17 20:07:13 +01:00
#include "swad_global.h"
2019-10-23 20:07:56 +02:00
#include "swad_HTML.h"
2015-01-17 20:07:13 +01:00
#include "swad_scope.h"
#include "swad_theme.h"
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Internal constants ****************************/
/*****************************************************************************/
/*****************************************************************************/
/****************************** Internal types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Internal global variables *************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Internal prototypes ***************************/
/*****************************************************************************/
/*****************************************************************************/
/****************************** Draw degree logo *****************************/
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
void Log_DrawLogo (Hie_Level_t Scope,long Cod,const char *AltText,
2015-08-06 12:31:05 +02:00
unsigned Size,const char *Class,bool PutIconIfNotExists)
2015-01-17 20:07:13 +01:00
{
2019-10-30 22:31:03 +01:00
static const char *HieIcon[Hie_NUM_LEVELS] =
2015-02-03 11:34:59 +01:00
{
2019-10-30 22:31:03 +01:00
"sitemap.svg", // Hie_UNK, not applicable here
"sitemap.svg", // Hie_SYS, not applicable here
"sitemap.svg", // Hie_CTY, not applicable here
2019-04-03 20:57:04 +02:00
"university.svg", // Hie_INS
"building.svg", // Hie_CTR
"graduation-cap.svg", // Hie_DEG
2019-10-30 22:31:03 +01:00
"sitemap.svg", // Hie_CRS, not applicable here
2015-02-03 11:34:59 +01:00
};
2015-02-03 18:43:55 +01:00
const char *Folder = NULL; // To avoid warning
2017-01-28 15:58:46 +01:00
char PathLogo[PATH_MAX + 1];
2015-02-03 11:34:59 +01:00
bool LogoFound = false;
2015-02-10 14:58:50 +01:00
long InsCod;
long CtrCod;
long DegCod;
2019-10-30 22:31:03 +01:00
char *URL;
char *Icon;
2019-10-30 23:55:49 +01:00
bool ClassNotEmpty;
2015-01-17 20:07:13 +01:00
/***** Path to logo *****/
2019-10-30 22:31:03 +01:00
if (HieIcon[Scope]) // Scope is correct
2015-01-17 20:07:13 +01:00
{
2015-02-10 14:58:50 +01:00
if (Cod > 0) // Institution, centre or degree exists
2015-02-03 11:34:59 +01:00
{
2015-02-10 14:58:50 +01:00
/* Degree */
2019-04-03 20:57:04 +02:00
if (Scope == Hie_DEG)
2015-02-10 14:58:50 +01:00
{
Folder = Cfg_FOLDER_DEG;
DegCod = Cod;
2018-10-18 02:02:32 +02:00
snprintf (PathLogo,sizeof (PathLogo),
2019-03-20 01:36:36 +01:00
"%s/%02u/%u/logo/%u.png",
2019-10-04 15:19:36 +02:00
Cfg_PATH_DEG_PUBLIC,
2018-10-18 02:02:32 +02:00
(unsigned) (DegCod % 100),
(unsigned) DegCod,
(unsigned) DegCod);
2015-02-10 14:58:50 +01:00
LogoFound = Fil_CheckIfPathExists (PathLogo);
2015-02-10 15:07:28 +01:00
if (LogoFound)
Cod = DegCod;
2015-02-10 14:58:50 +01:00
}
/* Centre */
2019-04-03 20:57:04 +02:00
if (!LogoFound && Scope != Hie_INS)
2015-02-10 14:58:50 +01:00
{
Folder = Cfg_FOLDER_CTR;
2019-04-03 20:57:04 +02:00
if (Scope == Hie_DEG) // && !LogoFound
2015-02-10 14:58:50 +01:00
CtrCod = Deg_GetCtrCodOfDegreeByCod (Cod);
else
CtrCod = Cod;
2018-10-18 02:02:32 +02:00
snprintf (PathLogo,sizeof (PathLogo),
2019-03-20 01:36:36 +01:00
"%s/%02u/%u/logo/%u.png",
Cfg_PATH_CTR_PUBLIC,
2018-10-18 02:02:32 +02:00
(unsigned) (CtrCod % 100),
(unsigned) CtrCod,
(unsigned) CtrCod);
2015-02-10 14:58:50 +01:00
LogoFound = Fil_CheckIfPathExists (PathLogo);
2015-02-10 15:07:28 +01:00
if (LogoFound)
Cod = CtrCod;
2015-02-10 14:58:50 +01:00
}
/* Institution */
if (!LogoFound)
{
Folder = Cfg_FOLDER_INS;
2019-04-03 20:57:04 +02:00
if (Scope == Hie_DEG) // && !LogoFound
2015-02-10 14:58:50 +01:00
InsCod = Deg_GetInsCodOfDegreeByCod (Cod);
2019-04-03 20:57:04 +02:00
else if (Scope == Hie_CTR) // && !LogoFound
2015-02-10 14:58:50 +01:00
InsCod = Ctr_GetInsCodOfCentreByCod (Cod);
else
InsCod = Cod;
2018-10-18 02:02:32 +02:00
snprintf (PathLogo,sizeof (PathLogo),
2019-03-20 01:36:36 +01:00
"%s/%02u/%u/logo/%u.png",
Cfg_PATH_INS_PUBLIC,
2018-10-18 02:02:32 +02:00
(unsigned) (InsCod % 100),
(unsigned) InsCod,
(unsigned) InsCod);
2015-02-10 14:58:50 +01:00
LogoFound = Fil_CheckIfPathExists (PathLogo);
2015-02-10 15:07:28 +01:00
if (LogoFound)
Cod = InsCod;
2015-02-10 14:58:50 +01:00
}
2015-02-03 11:34:59 +01:00
2016-07-27 14:58:26 +02:00
if (LogoFound || PutIconIfNotExists)
{
/***** Draw logo *****/
if (LogoFound)
2019-10-30 22:31:03 +01:00
{
if (asprintf (&URL,"%s/%s/%02u/%u/logo",
Cfg_URL_SWAD_PUBLIC,Folder,
(unsigned) (Cod % 100),
(unsigned) Cod) < 0)
Lay_NotEnoughMemoryExit ();
if (asprintf (&Icon,"%u.png",(unsigned) Cod) < 0)
Lay_NotEnoughMemoryExit ();
}
else
{
if (asprintf (&URL,"%s",Cfg_URL_ICON_PUBLIC) < 0)
Lay_NotEnoughMemoryExit ();
if (asprintf (&Icon,"%s",HieIcon[Scope]) < 0)
Lay_NotEnoughMemoryExit ();
}
2019-10-30 23:55:49 +01:00
ClassNotEmpty = false;
if (Class)
if (Class[0])
ClassNotEmpty = true;
2019-10-30 22:31:03 +01:00
HTM_IMG (URL,Icon,AltText,
2019-10-30 23:55:49 +01:00
"class=\"ICO%ux%u%s%s\"",
Size,Size,
ClassNotEmpty ? " " :
"",
ClassNotEmpty ? Class :
"");
2019-10-30 22:31:03 +01:00
free ((void *) Icon);
free ((void *) URL);
2016-07-27 14:58:26 +02:00
}
2015-02-03 11:34:59 +01:00
}
2015-01-17 20:07:13 +01:00
}
}
/*****************************************************************************/
2016-05-05 20:16:28 +02:00
/************* Put an icon to go to the action used to request ***************/
/************* the logo of institution, centre or degree ***************/
2015-01-17 20:07:13 +01:00
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
void Log_PutIconToChangeLogo (Hie_Level_t Scope)
2015-01-17 20:07:13 +01:00
{
extern const char *Txt_Change_logo;
extern const char *Txt_Upload_logo;
Act_Action_t Action;
long Cod;
const char *Folder;
2017-01-28 15:58:46 +01:00
char PathLogo[PATH_MAX + 1];
2015-01-17 20:07:13 +01:00
bool LogoExists;
/***** Set variables depending on scope *****/
switch (Scope)
{
2019-04-03 20:57:04 +02:00
case Hie_INS:
2015-01-17 20:07:13 +01:00
Action = ActReqInsLog;
2019-04-03 20:57:04 +02:00
Cod = Gbl.Hierarchy.Ins.InsCod;
2015-01-17 20:07:13 +01:00
Folder = Cfg_FOLDER_INS;
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
2015-01-17 20:07:13 +01:00
Action = ActReqCtrLog;
2019-04-03 20:57:04 +02:00
Cod = Gbl.Hierarchy.Ctr.CtrCod;
2015-01-17 20:07:13 +01:00
Folder = Cfg_FOLDER_CTR;
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
2015-01-17 20:07:13 +01:00
Action = ActReqDegLog;
2019-04-03 20:57:04 +02:00
Cod = Gbl.Hierarchy.Deg.DegCod;
2015-01-17 20:07:13 +01:00
Folder = Cfg_FOLDER_DEG;
break;
default:
return; // Nothing to do
}
/***** Check if logo exists *****/
2018-10-18 02:02:32 +02:00
snprintf (PathLogo,sizeof (PathLogo),
"%s/%s/%02u/%u/logo/%u.png",
Cfg_PATH_SWAD_PUBLIC,Folder,
(unsigned) (Cod % 100),
(unsigned) Cod,
(unsigned) Cod);
2015-01-17 20:07:13 +01:00
LogoExists = Fil_CheckIfPathExists (PathLogo);
/***** Link for changing / uploading the logo *****/
2019-01-12 03:00:59 +01:00
Lay_PutContextualLinkOnlyIcon (Action,NULL,NULL,
"shield-alt.svg",
LogoExists ? Txt_Change_logo :
Txt_Upload_logo);
2015-01-17 20:07:13 +01:00
}
/*****************************************************************************/
/**** Show a form for sending a logo of the institution, centre or degree ****/
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
void Log_RequestLogo (Hie_Level_t Scope)
2015-01-17 20:07:13 +01:00
{
2019-02-22 21:47:50 +01:00
extern const char *The_ClassFormInBox[The_NUM_THEMES];
2015-12-13 20:24:39 +01:00
extern const char *Txt_Remove_logo;
2015-04-12 01:40:51 +02:00
extern const char *Txt_Logo;
2016-11-10 12:38:41 +01:00
extern const char *Txt_You_can_send_a_file_with_an_image_in_PNG_format_transparent_background_and_size_X_Y;
2015-01-17 20:07:13 +01:00
extern const char *Txt_File_with_the_logo;
2015-02-03 18:43:55 +01:00
long Cod;
const char *Folder;
Act_Action_t ActionRec;
Act_Action_t ActionRem;
2017-01-28 15:58:46 +01:00
char PathLogo[PATH_MAX + 1];
2015-01-17 20:07:13 +01:00
/***** Set action depending on scope *****/
switch (Scope)
{
2019-04-03 20:57:04 +02:00
case Hie_INS:
Cod = Gbl.Hierarchy.Ins.InsCod;
2015-02-03 18:43:55 +01:00
Folder = Cfg_FOLDER_INS;
ActionRec = ActRecInsLog;
ActionRem = ActRemInsLog;
2015-01-17 20:07:13 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
Cod = Gbl.Hierarchy.Ctr.CtrCod;
2015-02-03 18:43:55 +01:00
Folder = Cfg_FOLDER_CTR;
ActionRec = ActRecCtrLog;
ActionRem = ActRemCtrLog;
2015-01-17 20:07:13 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
Cod = Gbl.Hierarchy.Deg.DegCod;
2015-02-03 18:43:55 +01:00
Folder = Cfg_FOLDER_DEG;
ActionRec = ActRecDegLog;
ActionRem = ActRemDegLog;
2015-01-17 20:07:13 +01:00
break;
default:
return; // Nothing to do
}
2015-02-03 18:43:55 +01:00
/***** Check if logo exists *****/
2018-10-18 02:02:32 +02:00
snprintf (PathLogo,sizeof (PathLogo),
"%s/%s/%02u/%u/logo/%u.png",
Cfg_PATH_SWAD_PUBLIC,Folder,
(unsigned) (Cod % 100),
(unsigned) Cod,
(unsigned) Cod);
2015-02-03 18:43:55 +01:00
if (Fil_CheckIfPathExists (PathLogo))
{
/***** Form to remove photo *****/
2019-10-24 00:04:40 +02:00
HTM_DIV_Begin ("class=\"CM\"");
2019-01-12 03:00:59 +01:00
Lay_PutContextualLinkIconText (ActionRem,NULL,NULL,
"trash.svg",
Txt_Remove_logo);
2019-10-23 20:07:56 +02:00
HTM_DIV_End ();
2015-02-03 18:43:55 +01:00
}
2019-10-20 22:00:28 +02:00
/***** Begin form to upload logo *****/
2018-11-09 20:47:39 +01:00
Frm_StartForm (ActionRec);
2015-04-12 01:40:51 +02:00
2019-10-26 02:19:42 +02:00
/***** Begin box *****/
2019-10-25 22:48:34 +02:00
Box_BoxBegin (NULL,Txt_Logo,NULL,
2017-06-12 15:03:29 +02:00
NULL,Box_NOT_CLOSABLE);
2015-04-12 01:40:51 +02:00
2015-01-17 20:07:13 +01:00
/***** Write help message *****/
2019-02-16 17:20:05 +01:00
Ale_ShowAlert (Ale_INFO,Txt_You_can_send_a_file_with_an_image_in_PNG_format_transparent_background_and_size_X_Y,
64,64);
2015-01-17 20:07:13 +01:00
2015-04-12 01:40:51 +02:00
/***** Upload logo *****/
2019-11-02 23:40:52 +01:00
HTM_LABEL_Begin ("class=\"%s\"",The_ClassFormInBox[Gbl.Prefs.Theme]);
2019-11-03 11:20:14 +01:00
fprintf (Gbl.F.Out,"%s:&nbsp;",Txt_File_with_the_logo);
2019-11-04 10:03:37 +01:00
HTM_INPUT_FILE ("image/png",true);
2019-11-02 12:59:31 +01:00
HTM_LABEL_End ();
2015-04-12 01:40:51 +02:00
2017-06-12 14:16:33 +02:00
/***** End box *****/
2019-10-25 22:48:34 +02:00
Box_BoxEnd ();
2015-04-12 01:40:51 +02:00
/***** End form *****/
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2015-01-17 20:07:13 +01:00
}
/*****************************************************************************/
/******* Receive the logo of the current institution, centre or degree *******/
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
void Log_ReceiveLogo (Hie_Level_t Scope)
2015-01-17 20:07:13 +01:00
{
extern const char *Txt_The_file_is_not_X;
long Cod;
const char *Folder;
2017-01-28 15:58:46 +01:00
char Path[PATH_MAX + 1];
2016-04-01 01:59:27 +02:00
struct Param *Param;
2017-01-28 15:58:46 +01:00
char FileNameLogoSrc[PATH_MAX + 1];
2017-01-15 18:02:52 +01:00
char MIMEType[Brw_MAX_BYTES_MIME_TYPE + 1];
2017-01-28 15:58:46 +01:00
char FileNameLogo[PATH_MAX + 1]; // Full name (including path and .png) of the destination file
2015-01-17 20:07:13 +01:00
bool WrongType = false;
/***** Set variables depending on scope *****/
switch (Scope)
{
2019-04-03 20:57:04 +02:00
case Hie_INS:
Cod = Gbl.Hierarchy.Ins.InsCod;
2015-01-17 20:07:13 +01:00
Folder = Cfg_FOLDER_INS;
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
Cod = Gbl.Hierarchy.Ctr.CtrCod;
2015-01-17 20:07:13 +01:00
Folder = Cfg_FOLDER_CTR;
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
Cod = Gbl.Hierarchy.Deg.DegCod;
2015-01-17 20:07:13 +01:00
Folder = Cfg_FOLDER_DEG;
break;
default:
return; // Nothing to do
}
/***** Creates directories if not exist *****/
2018-10-18 02:02:32 +02:00
snprintf (Path,sizeof (Path),
"%s/%s",
Cfg_PATH_SWAD_PUBLIC,Folder);
2015-01-17 20:07:13 +01:00
Fil_CreateDirIfNotExists (Path);
2018-10-18 02:02:32 +02:00
snprintf (Path,sizeof (Path),
"%s/%s/%02u",
Cfg_PATH_SWAD_PUBLIC,Folder,
(unsigned) (Cod % 100));
2015-01-17 20:07:13 +01:00
Fil_CreateDirIfNotExists (Path);
2018-10-18 02:02:32 +02:00
snprintf (Path,sizeof (Path),
"%s/%s/%02u/%u",
Cfg_PATH_SWAD_PUBLIC,Folder,
(unsigned) (Cod % 100),
(unsigned) Cod);
2015-01-17 20:07:13 +01:00
Fil_CreateDirIfNotExists (Path);
2018-10-18 02:02:32 +02:00
snprintf (Path,sizeof (Path),
"%s/%s/%02u/%u/logo",
Cfg_PATH_SWAD_PUBLIC,Folder,
(unsigned) (Cod % 100),
(unsigned) Cod);
2015-01-17 20:07:13 +01:00
Fil_CreateDirIfNotExists (Path);
/***** Copy in disk the file received from stdin (really from Gbl.F.Tmp) *****/
2016-04-04 12:13:37 +02:00
Param = Fil_StartReceptionOfFile (Fil_NAME_OF_PARAM_FILENAME_ORG,
FileNameLogoSrc,MIMEType);
2015-01-17 20:07:13 +01:00
/* Check if the file type is image/jpeg or image/pjpeg or application/octet-stream */
if (strcmp (MIMEType,"image/png"))
if (strcmp (MIMEType,"image/x-png"))
if (strcmp (MIMEType,"application/octet-stream"))
if (strcmp (MIMEType,"application/octetstream"))
if (strcmp (MIMEType,"application/octet"))
WrongType = true;
if (WrongType)
2019-02-16 17:20:05 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_The_file_is_not_X,
"png");
2015-02-03 18:43:55 +01:00
else
{
/* End the reception of logo in a temporary file */
2018-10-18 02:02:32 +02:00
snprintf (FileNameLogo,sizeof (FileNameLogo),
"%s/%s/%02u/%u/logo/%u.png",
Cfg_PATH_SWAD_PUBLIC,Folder,
(unsigned) (Cod % 100),
(unsigned) Cod,
(unsigned) Cod);
2016-04-01 01:59:27 +02:00
if (!Fil_EndReceptionOfFile (FileNameLogo,Param))
2019-02-16 17:20:05 +01:00
Ale_ShowAlert (Ale_ERROR,"Error copying file.");
2015-02-03 18:43:55 +01:00
}
}
2015-01-17 20:07:13 +01:00
2015-02-03 18:43:55 +01:00
/*****************************************************************************/
/******* Remove the logo of the current institution, centre or degree ********/
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
void Log_RemoveLogo (Hie_Level_t Scope)
2015-02-03 18:43:55 +01:00
{
long Cod;
const char *Folder;
2017-01-28 15:58:46 +01:00
char FileNameLogo[PATH_MAX + 1]; // Full name (including path and .png) of the destination file
2015-02-03 18:43:55 +01:00
/***** Set variables depending on scope *****/
switch (Scope)
{
2019-04-03 20:57:04 +02:00
case Hie_INS:
Cod = Gbl.Hierarchy.Ins.InsCod;
2015-02-03 18:43:55 +01:00
Folder = Cfg_FOLDER_INS;
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
Cod = Gbl.Hierarchy.Ctr.CtrCod;
2015-02-03 18:43:55 +01:00
Folder = Cfg_FOLDER_CTR;
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
Cod = Gbl.Hierarchy.Deg.DegCod;
2015-02-03 18:43:55 +01:00
Folder = Cfg_FOLDER_DEG;
break;
default:
return; // Nothing to do
}
/***** Remove logo *****/
2018-10-18 02:02:32 +02:00
snprintf (FileNameLogo,sizeof (FileNameLogo),
"%s/%s/%02u/%u/logo/%u.png",
Cfg_PATH_SWAD_PUBLIC,Folder,
(unsigned) (Cod % 100),
(unsigned) Cod,
(unsigned) Cod);
2016-10-06 22:18:33 +02:00
Fil_RemoveTree (FileNameLogo);
2015-01-17 20:07:13 +01:00
}