1/*
2 *  Name:         HTML creation functions
3 *
4 *  Purpose:      Create a nice HTML page with all the statistics
5 *
6 *  Author:       Pedro de Oliveira <falso@rdk.homeip.net>
7 *
8 *  Copyright (c) 2004-2011 Pedro de Oliveira ( falso@rdk.homeip-net )
9 *
10 *  This file is part of aMule.
11 *
12 *  This program is free software; you can redistribute it and/or modify
13 *  it under the terms of the GNU General Public License as published by
14 *  the Free Software Foundation; either version 2 of the License, or
15 *  (at your option) any later version.
16 *
17 *  This program is distributed in the hope that it will be useful,
18 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 *  GNU General Public License for more details.
21 *
22 *  You should have received a copy of the GNU General Public License
23 *  along with this program; if not, write to the
24 *  Free Software Foundation, Inc.,
25 *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
26 */
27
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <fcntl.h>
35#include <string.h>
36
37#include "html.h"
38#include "functions.h"
39#include "version.h"
40
41int create_html(char *stats[20], char *lines[6], char template[120], char *path_for_html)
42{
43	/* Strings */
44	char *path = NULL;
45	char version[25], upload[25], download[25];
46	char *search[] = {"#VERSION#", "#CLIENT#", "#NICK#", "#UPLOADRATE#" ,
47		"#DOWNLOADRATE#" , "#QUEUE#" , "#NUMSHARE#" , "#SESSIONUP#" ,
48		"#SESSIONDOWN#" , "#TOTALUP#", "#TOTALDOWN#" , "#SERVER#" , "#IP#",
49		"#PORT#" };
50
51	snprintf(version, 25, "cas %s", CAS_VERSION);
52	snprintf(upload, 25, "%s kB/s", stats[7]);
53	snprintf(download, 25, "%s kB/s", stats[6]);
54
55	char *repl[] = { version , lines[0] , stats[10] , upload , download ,
56		stats[8] , stats[9] , stats[15] , stats[14] , stats[12] , stats[11] ,
57		stats[1] , stats[2] , stats[3] };
58
59	/* get some memory to read the template into */
60	int fdTmpl;
61	if ((fdTmpl = open(template, O_RDONLY)) < 0)
62	{
63		printf("\n\n%s\n",template);
64		perror("Could not open file");
65		exit (43);
66	}
67
68	struct stat sb;
69	if (fstat(fdTmpl, &sb) < 0)
70	{
71		perror("Could not stat file");
72		exit(43);
73	}
74	close(fdTmpl);
75
76	/* 2 times the size of the template should be enough */
77	/* st_size is defined as off_t, but size_t seems more reasonable */
78	size_t size = sb.st_size*2;
79	char *mem = calloc(size, 1);
80	if (NULL == mem)
81	{
82		perror("Could not calloc\n");
83		exit(44);
84	}
85
86	/* read the template into the memory */
87	size_t len = 0;
88	int ler;
89	FILE *fTmpl = fopen(template,"r");
90	while ((ler=fgetc(fTmpl)) != EOF && len+1 < size)
91	{
92		mem[len++] = ler;
93	}
94	fclose(fTmpl);
95
96	/* printf ("HTML: %s\n", mem); */
97
98	int t;
99	for (t=0; t<=13; t++)
100	{
101		/* replace the special tags */
102		replace(mem, search[t], repl[t]);
103	}
104
105	/* printf("FINAL: %s\n",mem); */
106
107	path = get_amule_path("aMule-online-sign.html", 0, path_for_html);
108
109	if (NULL == path)
110	{
111		perror("could not get the HTML path\n");
112		free(mem);
113		return 0;
114	}
115
116	FILE *fHTML = NULL;
117	if ((fHTML = fopen(path, "w")) == NULL)
118	{
119		perror("Unable to create file\n");
120		free(path);
121		free(mem);
122		exit(44);
123	}
124	free(path);
125
126	fprintf(fHTML, "%s", mem);
127	fclose(fHTML);
128	free(mem);
129
130	printf("HTML file created.\n");
131
132	return 1;
133}
134