summaryrefslogtreecommitdiff
path: root/plugins/HTTPServer/src/IndexHTML.cpp
blob: 651b74c867816c209c3ba56516939f2e358367aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//This file is part of HTTPServer a Miranda IM plugin
//Copyright (C)2002 Kennet Nielsen
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 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 General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#include "Glob.h"

eIndexCreationMode indexCreationMode;

static char* szIndexHTMLTemplate = NULL;
static const int MAX_PARAM_LENGTH = 5;

// signs below 32 are not used anyway
enum Symbol {
	SY_END = 15,
	SY_FOR_DIRS,
	SY_DIR_URL,
	SY_DIR_NAME,
	SY_FOR_FILES,
	SY_FILE_URL,
	SY_FILE_NAME,
	SY_FILE_SIZE,
	SY_FILE_CREATE_TIME,
	SY_FILE_MODIFY_TIME,
	SY_IS_EVEN,
	SY_IS_ODD,
	SY_IS_FILE_TYPE
};

/////////////////////////////////////////////////////////////////////
// Member Function : LoadIndexHTMLTemplate
// Type            : Global
// Parameters      : -
// Returns         :
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 050901, 05 august 2005
// Developer       : Houdini
/////////////////////////////////////////////////////////////////////

bool LoadIndexHTMLTemplate() {
	if (szIndexHTMLTemplate != NULL)
		return true;

	char  szBuf[10000];
	char* pszBuf = szBuf;

	char  szDestBuf[10000];
	char* pszDestBuf = szDestBuf;

	strcpy(pszBuf, szPluginPath);
	strcat(pszBuf, szIndexHTMLTemplateFile);

	HANDLE hFile = CreateFile(pszBuf, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
	    NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE) {
		MessageBox(NULL, "HTTPServerIndex.html not found in Plugin Path", MSG_BOX_TITEL, MB_OK);
		return false;
	}

	DWORD dwBytesRead = 0;
	if (ReadFile(hFile, pszBuf, sizeof(szBuf), &dwBytesRead, NULL) || dwBytesRead <= 0) {
		while (dwBytesRead > (DWORD)(pszBuf - szBuf)) {
			if (*pszBuf == '[') {
				char* pszKeywordBegin = pszBuf + 1;
				bool  bHasParameters = false;

				do {
					pszBuf++;
				} while (*pszBuf != ']' && *pszBuf != '\0' && *pszBuf != '=');

				if (*pszBuf == '\0')
					break;

				if (*pszBuf == '=')
					bHasParameters = true;

				*pszBuf = '\0';
				*pszDestBuf = '#';

				// signs below 32 are not used anyway
				if (!strcmp(pszKeywordBegin, "End"))              *pszDestBuf = SY_END;
				else if (!strcmp(pszKeywordBegin, "ForDirectoriesDo")) *pszDestBuf = SY_FOR_DIRS;
				else if (!strcmp(pszKeywordBegin, "DirectoryUrl"))     *pszDestBuf = SY_DIR_URL;
				else if (!strcmp(pszKeywordBegin, "DirectoryName"))    *pszDestBuf = SY_DIR_NAME;
				else if (!strcmp(pszKeywordBegin, "ForFilesDo"))       *pszDestBuf = SY_FOR_FILES;
				else if (!strcmp(pszKeywordBegin, "FileUrl"))          *pszDestBuf = SY_FILE_URL;
				else if (!strcmp(pszKeywordBegin, "FileName"))         *pszDestBuf = SY_FILE_NAME;
				else if (!strcmp(pszKeywordBegin, "FileSize"))         *pszDestBuf = SY_FILE_SIZE;
				else if (!strcmp(pszKeywordBegin, "FileCreated"))      *pszDestBuf = SY_FILE_CREATE_TIME;
				else if (!strcmp(pszKeywordBegin, "FileModified"))     *pszDestBuf = SY_FILE_MODIFY_TIME;
				else if (!strcmp(pszKeywordBegin, "IsEven"))           *pszDestBuf = SY_IS_EVEN;
				else if (!strcmp(pszKeywordBegin, "IsOdd"))            *pszDestBuf = SY_IS_ODD;
				else if (!strcmp(pszKeywordBegin, "IsFileType"))       *pszDestBuf = SY_IS_FILE_TYPE;
				else {
					LogEvent("Error in index template", "Unknown Tag");
					// unknown tag
				}

				// remove some useless returns
				if (*pszDestBuf == SY_END)
					while (*(pszBuf + 1) == '\r' || *(pszBuf + 1) == '\n')
						pszBuf++;

				pszDestBuf++;

				// these tags require an End - reserve space for relative jump address (2 byte)
				switch (*(pszDestBuf - 1)) {
					case SY_FOR_DIRS:
					case SY_FOR_FILES:
					case SY_IS_EVEN:
					case SY_IS_ODD:
					case SY_IS_FILE_TYPE: {
							*((WORD*)(pszDestBuf)) = 0x7070;
							pszDestBuf += 2;
						}
				}

				if (bHasParameters) {
					// max MAX_PARAM_LENGTH chars per param (terminated with : when shorter than MAX_PARAM_LENGTH)
					byte  iParamCount = 1;
					char* pcParamCount = (pszDestBuf++);
					char* pszParameterBegin = pszBuf + 1;

					do {
						if (*pszBuf == ',') {
							*pszBuf = ':';
							strncpy(pszDestBuf, pszParameterBegin, MAX_PARAM_LENGTH);
							pszDestBuf += MAX_PARAM_LENGTH;

							pszParameterBegin = pszBuf + 1;
							iParamCount++;
						}
						pszBuf++;
					} while (*pszBuf != ']' && *pszBuf != '\0');

					if (*pszBuf == '\0')
						break;

					*pszBuf = ':';
					strncpy(pszDestBuf, pszParameterBegin, MAX_PARAM_LENGTH);
					pszDestBuf += MAX_PARAM_LENGTH;

					*pcParamCount = iParamCount;
				}
			} else {
				*(pszDestBuf++) = *pszBuf;
			}

			pszBuf++;
		}

		*(pszDestBuf) = '\0';

		pszBuf = szDestBuf;

		while (*pszBuf != '\0') {
			byte iLevel = 0;
			switch (*pszBuf) {
					// these tags require an End - precalculate address of End
				case SY_FOR_DIRS:
				case SY_FOR_FILES:
				case SY_IS_EVEN:
				case SY_IS_ODD:
				case SY_IS_FILE_TYPE:
					iLevel++;
					break;
			}

			pszBuf++;

			// begin of iLevel - find End of iLevel
			if (iLevel > 0) {
				char* pszLevelEnd = pszBuf + 2; // skip for address reserved bytes

				// skip parameters of IsFileType
				if (*(pszBuf - 1) == SY_IS_FILE_TYPE) {
					pszLevelEnd++;
					pszLevelEnd += *(pszLevelEnd - 1) * MAX_PARAM_LENGTH;
				}

				while (*pszLevelEnd != '\0' && iLevel > 0) {
					switch (*pszLevelEnd) {
						case SY_FOR_DIRS:
						case SY_FOR_FILES:
						case SY_IS_EVEN:
						case SY_IS_ODD:
							iLevel++;
							pszLevelEnd += 2;
							break;

						case SY_IS_FILE_TYPE:
							iLevel++;
							pszLevelEnd += 2;
							pszLevelEnd += 1;
							pszLevelEnd += *(pszLevelEnd) * MAX_PARAM_LENGTH;
							break;

						case SY_END:
							iLevel--;
							break;
					}

					pszLevelEnd++;
				}

				if (*pszLevelEnd == '\0') {
					LogEvent("Error in index template", "End is missing");
					break; // Error - End missing
				}

				// write jump address
				*((WORD*)(pszBuf)) = (WORD)((pszLevelEnd - pszBuf - 1) | 0x8000);
				pszBuf += 2;
			}
		}

		*(pszBuf) = '\0';

		//LogEvent("Template", szDestBuf);

		szIndexHTMLTemplate = new char[strlen(szDestBuf)+1];
		strcpy(szIndexHTMLTemplate, szDestBuf);
	}

	CloseHandle(hFile);
	return true;
}

/////////////////////////////////////////////////////////////////////
// Member Function : FreeIndexHTMLTemplate
// Type            : Global
// Parameters      : -
// Returns         :
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 050901, 05 august 2005
// Developer       : Houdini
/////////////////////////////////////////////////////////////////////

void FreeIndexHTMLTemplate() {
	if (szIndexHTMLTemplate != NULL) {
		delete[] szIndexHTMLTemplate;
		szIndexHTMLTemplate = NULL;
	}
}

/////////////////////////////////////////////////////////////////////
// Member Function : bCreateIndexHTML
// Type            : Global
// Parameters      : pszRealPath - ?
// Returns         :
// Description     :
//
// References      : -
// Remarks         : -
// Created         : 050821, 05 august 2005
// Developer       : Houdini
/////////////////////////////////////////////////////////////////////

bool bCreateIndexHTML(const char * pszRealPath, const char * pszIndexPath,
    const char * pszSrvPath, DWORD /* dwRemoteIP */) {
#define RelativeJump(begin) { pszPos += *((WORD*)(begin+1)) & 0x7FFF; }

	if (szIndexHTMLTemplate == NULL)
		return false;

	// check if directory exists
	char szMask[MAX_PATH];
	strcpy(szMask, pszRealPath);
	strcat(szMask, "*");

	WIN32_FIND_DATAA fdFindFileData;
	HANDLE hFind = FindFirstFile(szMask, &fdFindFileData);

	if (hFind == INVALID_HANDLE_VALUE)
		return FALSE;

	FindClose(hFind);
	hFind = 0;

	HANDLE hFile = CreateFile(pszIndexPath, GENERIC_WRITE, FILE_SHARE_READ ,
	    NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL);

	if (hFile == INVALID_HANDLE_VALUE)
		return FALSE;

	char  szBuffer[10000];
	char* pszBuffer = szBuffer;
	DWORD dwBytesWritten = 0;

	char* pszPos = szIndexHTMLTemplate;

	byte  iCurrentAction = 0;
	char* pszLevelBegin[50];
	byte  iLevel = 0;

	char  szName[1000] = "";
	char  szURL[1000] = "";
	int   iFileSize = 0;
	FILETIME ftFileCreateTime;
	FILETIME ftFileModifyTime;
	bool  bEvenOdd = 0;
	bool  bKnownFileType = false;

	strcpy(szBuffer, pszSrvPath);
	char* pszTemp = strrchr(szBuffer, '/');
	if (pszTemp)
		*pszTemp = '\0';

	pszTemp = strrchr(szBuffer, '/');
	if (pszTemp)
		strcpy(szName, pszTemp + 1);

	if (strlen(szName) == 0)
		strcpy(szName, "my Miranda Webserver");

	do {
		switch (*pszPos) {
			case SY_FOR_FILES:
			case SY_FOR_DIRS: {
				if (hFind == 0) {
					pszLevelBegin[iLevel++] = pszPos;
					iCurrentAction = *pszPos;

					hFind = FindFirstFile(szMask, &fdFindFileData);
					if (hFind == 0) {
						iCurrentAction = 0;
						RelativeJump(pszLevelBegin[iLevel-1]);
						break;
					}
				} else {
					if (!FindNextFile(hFind, &fdFindFileData)) {
						FindClose(hFind);
						hFind = 0;
						iCurrentAction = 0;
						RelativeJump(pszLevelBegin[iLevel-1]);
						break;
					}
				}

				while (!strcmp(fdFindFileData.cFileName, ".") ||
				    !strncmp(fdFindFileData.cFileName, "@", 1) ||
				    (!strcmp(fdFindFileData.cFileName, "..") && !strcmp(pszSrvPath, "/")) || // hide .. in root
				    ((*pszPos == 19) == ((fdFindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0))) {
					if (!FindNextFile(hFind, &fdFindFileData)) {
						FindClose(hFind);
						hFind = 0;
						iCurrentAction = 0;
						RelativeJump(pszLevelBegin[iLevel-1]);
						break;
					}
				}

				if (hFind) {
					strcpy(szName, fdFindFileData.cFileName);
					strcpy(szURL, fdFindFileData.cFileName);
					/*char* pszTmp = szURL;
					while(pszTmp = strchr(pszTmp, ' '))
					*pszTmp = '+';*/

					if (*pszPos == SY_FOR_DIRS) { // For Directories
						strcat(szURL, "/");
					} else { // For Files
						iFileSize = fdFindFileData.nFileSizeLow;
						ftFileCreateTime = fdFindFileData.ftCreationTime;
						ftFileModifyTime = fdFindFileData.ftLastWriteTime;
					}

					bKnownFileType = false;
					bEvenOdd = !bEvenOdd;
					pszPos += 2;
				}

				break;
			}

			case SY_END: { // End
				if (iLevel <= 0)
					break; // Error

				if (iCurrentAction == SY_FOR_DIRS || iCurrentAction == SY_FOR_FILES) { // For loops
					pszPos = pszLevelBegin[iLevel-1] - 1; // jump to begin
				} else {
					iLevel--;
					if (iLevel > 0)
						iCurrentAction = *pszLevelBegin[iLevel-1];
					else
						iCurrentAction = 0;
				}

				break;
			}

			case SY_FILE_NAME:
			case SY_DIR_NAME: {
				pszBuffer += mir_snprintf(pszBuffer, 250, "%s", szName);
				break;
			}

			case SY_DIR_URL: {
			case SY_FILE_URL:
				pszBuffer += mir_snprintf(pszBuffer, 250, "%s", szURL);
				break;
			}

			case SY_FILE_CREATE_TIME:
			case SY_FILE_MODIFY_TIME: {
				SYSTEMTIME systemTime;
				FileTimeToSystemTime(
				  (*pszPos == SY_FILE_CREATE_TIME) ? &ftFileCreateTime : &ftFileModifyTime,
				  &systemTime);

				pszBuffer += mir_snprintf(pszBuffer, 100, "%i/%02i/%02i %i:%02i:%02i", 
					systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour,
				  systemTime.wMinute, systemTime.wSecond);
				break;
			}

			case SY_FILE_SIZE: {
				if ((iFileSize >> 10) == 0)
					pszBuffer += mir_snprintf(pszBuffer, 100, "%i Byte", iFileSize);
				else if ((iFileSize >> 20) == 0)
					pszBuffer += mir_snprintf(pszBuffer, 100, "%.1f KB", (float)(iFileSize) / 1024.0f);
				else
					pszBuffer += mir_snprintf(pszBuffer, 100, "%.1f MB", (float)(iFileSize) / (1024.0f * 1024.0f));
				break;
			}

			case SY_IS_EVEN:
			case SY_IS_ODD: {
				pszLevelBegin[iLevel++] = pszPos;
				iCurrentAction = *pszPos;

				if (bEvenOdd != (*pszPos - SY_IS_EVEN == 1)) { // SY_IS_EVEN+1 == SY_IS_ODD
					RelativeJump(pszLevelBegin[iLevel-1]);
				} else {
					pszPos += 2;
				}
				break;
			}

			case SY_IS_FILE_TYPE: {
				pszLevelBegin[iLevel++] = pszPos;
				iCurrentAction = *pszPos;

				byte  iParamCount = *(pszPos + 3);
				char* pszParam = pszPos + 4;
				bool  bSkip = true;

				if (bKnownFileType == false) {
					if (*pszParam == '*') {
						bSkip = false;
					} else {
						for (byte i = 0; i < iParamCount; i++) {
							char szParam[MAX_PARAM_LENGTH+1];
							strncpy(szParam, pszParam, MAX_PARAM_LENGTH);
							szParam[MAX_PARAM_LENGTH] = '\0';
							char* pszTmp = strchr(szParam, ':');
							if (pszTmp)
								*pszTmp = '\0';

							char* pszExt = strrchr(szName, '.');

							if (pszExt && !_stricmp(pszExt + 1, szParam)) {
								bSkip = false;
								break;
							}
							pszParam += MAX_PARAM_LENGTH;
						}
					}
				}

				if (bSkip) {
					RelativeJump(pszLevelBegin[iLevel-1]);
				} else {
					bKnownFileType = true;
					pszPos += 2;
					pszPos += 1;
					pszPos += *(pszPos) * MAX_PARAM_LENGTH;
				}
				break;
			}

			default: {
				*(pszBuffer++) = *pszPos;
			}
		}

		pszPos++;

		// flush the buffer from time to time
		if (*pszPos == '\0' || pszBuffer - szBuffer > 8000) {
			if (!WriteFile(hFile, szBuffer, pszBuffer - szBuffer, &dwBytesWritten, NULL))
				break;

			pszBuffer = szBuffer;
		}
	} while (*pszPos != '\0');

	if (hFind != 0)
		FindClose(hFind);

	SetEndOfFile(hFile);
	CloseHandle(hFile);
	return TRUE;
}