From dfb7ec15b5f20ea7a5c47f625fb731ce148b184d Mon Sep 17 00:00:00 2001 From: Ache Date: Sun, 7 Aug 2016 20:21:53 +0200 Subject: Init commit --- main.c | 453 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 453 insertions(+) create mode 100755 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100755 index 0000000..366ac50 --- /dev/null +++ b/main.c @@ -0,0 +1,453 @@ +#include "main.h" + + +char** allocRegex(const char* str_req,const char* str_regex, int nbExp) +{ + int err; + regex_t preg; + + err = regcomp (&preg, str_regex, REG_EXTENDED); + if (err == 0) + { + int match; + size_t nmatch = 0; + regmatch_t *pmatch = NULL; + nmatch = preg.re_nsub; + pmatch = malloc (sizeof (*pmatch) * nmatch); + if (pmatch) + { + match = regexec (&preg, str_req, nmatch, pmatch, 0); + regfree (&preg); + + if (match == 0) + { + char** arg = malloc( nbExp * sizeof *arg); + int i; + for(i = 0; i < nbExp ; ++i) + { + int start = pmatch[i].rm_so; + int end = pmatch[i].rm_eo; + size_t size = end - start; + arg[i] = malloc (sizeof *arg[i] * (size + 1)); + if (arg[i]) + { + strncpy (arg[i], &str_req[start], size); + arg[i][size] = '\0'; + } + } + return arg; + } + else + goto regexErr; + } + else + goto regexErr2; + } + + regexErr: + fprintf(stderr, "L'analyse de %s a provoquer une erreur\n", str_req); + exit (EXIT_FAILURE); + regexErr2: + fprintf (stderr, "L'analyse de %s a provoquer 2 erreurs\n", str_req); + exit (EXIT_FAILURE); +} +#define puts(b) FCGX_FPrintF(req->out,"%s\n", b) +#define printf(...) FCGX_FPrintF(req->out, __VA_ARGS__) +void start(char* titre, char* description, FCGX_Request *req) { + puts("Content-Type: text/html;\r\n\r"); + printf("\n" + " " + " " + "%s" + "" + "" //" + "" + "" + "" + "" + "" + "\n" + , titre, description); +} +void end(FCGX_Request *req) +{ + puts("
"); + puts("" + ""); +} +void sideBar(FCGX_Request *req) { + puts(""); +} +void erreur(const char* motif, FCGX_Request *req) +{ + start("Erreur", "Page d'erreur", req); + puts("Error

"); + puts(motif); + end(req); + exit(0); +} +void afficherSVG(char* chemin, FCGX_Request* req) { + char* c = 0; + char ligne[201] = {0}; + FILE* fichier = fopen(chemin, "r"); + if(fichier) + { + while( c = fgets (ligne, 200, fichier) ) { + if( strstr(ligne, "

Voir aussi :

"); + while(fgets(t, 50, fichier))++nbArt; + rewind(fichier); + + if( nbArt<5 ) + { + while( fgets(t, 100, fichier)) + { + char date[20] = {0}, titre[50] = {0}, ident[50] = {0}; + for(tmp_h=t;*tmp_h;tmp_h++) + if( isspace(*tmp_h) ) + *tmp_h = '\0'; + + getinfo(t, titre, date, ident); + printf("
%s
", i++, titre); + } + fclose(fichier); + } + else + { + char tab[4] = {-1, -1, -1, -1}; + while(i<4) + { + char date[20] = {0}, titre[50] = {0}, ident[50] = {0}; + int has, z; + rewind(fichier); + do + has = rand() % nbArt; + while( memchr(tab, has, 4) ); + z = has; + while( fgets(t, 100, fichier) && has-- ); + for(tmp_h=t;*tmp_h;tmp_h++) + if( isspace(*tmp_h) ) + *tmp_h = '\0'; + getinfo(t, titre, date, ident); + printf("
%s
", z, titre); + tab[i++] = z; + } + fclose(fichier); + } + printf(""); +} +void slideContenu(char* article, int num, FCGX_Request* req) +{ + char d[20], t[50] = "Vive la lettre H !", i[50]; + int type = getinfo(article, t, d, i); + (void)num; + int year, month, day; + sscanf(d, "%d/%d/%d", &day, &month, &year); + printf( "
"); + switch(type) + { + case 0: + { + char tab[50] = {0}; + strncpy(tab, i, strrchr(i, '.')-i); + strcat(tab, ".png"); + printf("" + "\"Logo" + "

%s

" + "
",i,tab,i); + } + break; + case 1: + printf("\"%s\"", i, i); + break; + case 2: + printf("", i); + } + printf( + "
" + "

%s

" + "

Posté le

" + "
" + , t, year, month, day, d); + + puts("
"); + lireIntro(article, req); + puts("
"); + puts("
"); +} +void contenu(char* article, int num, FCGX_Request* req) +{ + char d[20], t[50] = "Vive la lettre H !", i[50]; + int type = getinfo(article, t, d, i); + (void)num; + int year, month, day; + sscanf(d, "%d/%d/%d", &day, &month, &year); + printf( + "
" + "
" + "

%s

" + "

Posté le

" + "
" + , t, year, month, day, d); + switch(type) + { + case 0: + { + char tab[50] = {0}; + strncpy(tab, i, strrchr(i, '.')-i); + strcat(tab, ".png"); + printf("" + "\"Logo" + "

%s

" + "
",i,tab,i); + } + break; + case 1: + printf("\"%s\"", i, i); + break; + case 2: + printf("", i); + } + puts("
"); + lireArticle(article, req); + puts("
"); + puts("
"); +} +#define putchar(a) FCGX_PutChar( a, req->out) +void lireArticle(char* chemin, FCGX_Request* req) +{ + int c = 0; + FILE* fichier = fopen(chemin, "r"); + if(fichier) + { + while((c = fgetc (fichier)) != '\n'); + while((c = fgetc (fichier)) != EOF) + { + if(c != 195) + if(c != '\n') + putchar(c); + else + printf("%s", "
"); + else + { + c = fgetc (fichier); + switch(c) + { + CaSe(160, "à");// a + CaSe(162, "à"); + CaSe(164, "â"); + CaSe(128, "À"); + CaSe(130, "Â"); + CaSe(132, "Ä"); + CaSe(169, "é");//e + CaSe(168, "è"); + CaSe(170, "è"); + CaSe(171, "ë"); + CaSe(137, "É"); + CaSe(136, "È"); + CaSe(138, "Ê"); + CaSe(139, "Ë");// o + CaSe(180, "õ"); + CaSe(182, "ö"); + CaSe(148, "Ô"); + CaSe(150, "Ö");// u + CaSe(185, "ù"); + CaSe(187, "û"); + CaSe(188, "ü"); + CaSe(153, "Ù"); + CaSe(155, "Û"); + CaSe(156, "Ü"); + CaSe(174, "î");// i + CaSe(175, "ï"); + CaSe(142, "Î"); + CaSe(143, "Ï"); + CaSe(167, "ç");// c + CaSe(135, "Ç"); + default: + printf("%d", c); + } + } + } + fclose(fichier); + } +} +void lireIntro(char* chemin, FCGX_Request* req) +{ + int c = 0; + FILE* fichier = fopen(chemin, "r"); + if(fichier) + { + while((c = fgetc (fichier)) != '\n'); + while((c = fgetc (fichier)) != EOF && c != '\n') { + if(c != 195) + if(c != '\n') + putchar(c); + else + printf("%s", "
"); + else + { + c = fgetc (fichier); + switch(c) + { + CaSe(160, "à");// a + CaSe(162, "à"); + CaSe(164, "â"); + CaSe(128, "À"); + CaSe(130, "Â"); + CaSe(132, "Ä"); + CaSe(169, "é");//e + CaSe(168, "è"); + CaSe(170, "è"); + CaSe(171, "ë"); + CaSe(137, "É"); + CaSe(136, "È"); + CaSe(138, "Ê"); + CaSe(139, "Ë");// o + CaSe(180, "õ"); + CaSe(182, "ö"); + CaSe(148, "Ô"); + CaSe(150, "Ö");// u + CaSe(185, "ù"); + CaSe(187, "û"); + CaSe(188, "ü"); + CaSe(153, "Ù"); + CaSe(155, "Û"); + CaSe(156, "Ü"); + CaSe(174, "î");// i + CaSe(175, "ï"); + CaSe(142, "Î"); + CaSe(143, "Ï"); + CaSe(167, "ç");// c + CaSe(135, "Ç"); + default: + printf("%d", c); + } + } + } + fclose(fichier); + } +} +#undef putchar + + +#undef puts +#undef printf +/* +void confirmation(void) +{ + puts("
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "

" + "Voulez confirmez l'envoye de ce ??? ? " + "
" + "
"); +}*/ +int getinfo(char* chemin, char* titre, char* date, char* image) +{ + char line[300] = "", type[5] = ""; + char** infos; + + FILE* fichier = fopen(chemin, "r"); + if(fichier) + { +///* + fgets(line, 300, fichier); + line[strlen(line)-1] = 0; + infos = allocRegex(line, "([[:print:]]+)\\|([[:print:]]+)\\|([[:print:]]+)\\|([[:print:]]+)()", 5); + fclose(fichier); + + strcpy(titre, infos[1]) ; + strcpy(date , infos[2]) ; + strcpy(type , infos[3]) ; + strcpy(image, infos[4]) ; + + free(infos[0]), free(infos[1]), free(infos[2]), free(infos[3]), free(infos[4]), free(infos); +/* + fscanf(fichier, "%[^|\n]s", titre); + fscanf(fichier, "|%[^\n|]s", date); + fscanf(fichier, "|%[^\n|]s", type); + fscanf(fichier, "|%[^\n|]s", image); + fclose(fichier); + */ + return ((!strcmp(type, "svg")) ? 0 : (!strcmp(type, "png")) ? 1 : (!strcmp(type, "js")) ? 2 : -1); + } + return -2; +} +void imput_(char* str) +{ + char* str2 = str; + do + { + if(*str == '+') + *str2++ = ' '; + else if(*str == '%') + { + int u = 0; + char t[3] = ""; + sscanf(++str, "%2c", t); + sscanf(t, "%x", &u); + *str2++ = u; + ++str; + } + else + *str2++ = *str; + }while(*str++); +} -- cgit v1.2.3-54-g00ecf