summaryrefslogtreecommitdiff
path: root/plugins/HTTPServer/src/IndexXML.cpp
blob: b34ef3c5b0f97a6ce72eaaa2131b382cdc9150c0 (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
//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"

static const char szXmlHeader1[] =	"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"
    "<?xml-stylesheet type=\"text/xsl\" href=\"";

static const char szXmlHeader2[] =	"\"?>\r\n"
    "<config>\r\n";

static const char szXmlTail[] =	"</config>";


static void ReplaceSign(TCHAR *pszSrc, int MaxLength, const TCHAR pszReplace, const TCHAR *pszNew)
{
	static TCHAR szBuffer[1024];
	TCHAR *pszSign = _tcschr(pszSrc, pszReplace);

	if (pszSign) {
		_tcscpy(szBuffer, pszSrc);

		do {
			_tcscpy(szBuffer + (pszSign - pszSrc), pszNew);
			_tcscpy(szBuffer + (pszSign - pszSrc) + _tcslen(pszNew), pszSign + 1);
			*pszSign = ' ';

		} while (pszSign = _tcschr(pszSrc, pszReplace));

		_tcsncpy(pszSrc, szBuffer, MaxLength);

		pszSrc[MaxLength-1] = '\0';
	}
}


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

bool bCreateIndexXML(const TCHAR *pszRealPath, const TCHAR *pszIndexPath, const TCHAR *pszSrvPath, DWORD dwRemoteIP)
{
	TCHAR szMask[MAX_PATH+1];
	_tcscpy(szMask, pszRealPath);
	_tcscat(szMask, _T("*"));

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

	if (hFind == INVALID_HANDLE_VALUE)
		return FALSE;

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

	if (hFile == INVALID_HANDLE_VALUE) {
		FindClose(hFind);
		return FALSE;
	}

	const int BUFFER_SIZE = 1000;
	TCHAR  szBuffer[BUFFER_SIZE+1];
	TCHAR *pszBuffer = szBuffer;
	TCHAR  szFileName[MAX_PATH+1] = _T("");
	TCHAR* pszExt;
	DWORD dwBytesWritten = 0;

	// Generate Dirname
	_tcscpy(szBuffer, pszSrvPath);
	TCHAR *pszTemp = _tcsrchr(szBuffer, '/');
	if (pszTemp)
		*pszTemp = '\0';

	pszTemp = _tcsrchr(szBuffer, '/');
	if (pszTemp)
		_tcscpy(szFileName, pszTemp + 1);

	// Write Header
	WriteFile(hFile, szXmlHeader1, sizeof(szXmlHeader1) - 1, &dwBytesWritten, NULL);

	// check if a index.xsl exists in the same directory otherwise use the global
	_tcscpy(szMask, pszRealPath);
	_tcscat(szMask, _T("index.xsl"));

	HANDLE hFileExists = CreateFile(szMask, GENERIC_READ, 
		FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 
		FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFileExists == INVALID_HANDLE_VALUE) {
		_tcscpy(szBuffer, _T("/index.xsl"));
	} else {
		CloseHandle(hFileExists);
		_tcscpy(szBuffer, _T("index.xsl"));
	}

	WriteFile(hFile, szBuffer, (DWORD)_tcslen(szBuffer), &dwBytesWritten, NULL);

	WriteFile(hFile, szXmlHeader2, sizeof(szXmlHeader2) - 1, &dwBytesWritten, NULL);

	// Write dirname
	ReplaceSign(szFileName, MAX_PATH, '&', _T("&amp;"));
	pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
		_T("  <dirname>%s</dirname>\r\n"), szFileName);
	WriteFile(hFile, szBuffer, pszBuffer - szBuffer, &dwBytesWritten, NULL);

	// Find files and directories
	do {
		if (_tcscmp(fdFindFileData.cFileName, _T(".")) &&
			_tcsncmp(fdFindFileData.cFileName, _T("@"), _tcslen(_T("@"))) &&
			(_tcscmp(fdFindFileData.cFileName, _T("..")) || _tcscmp(pszSrvPath, _T("/")))) { // hide .. in root
			pszBuffer = szBuffer;

			_tcscpy(szFileName, fdFindFileData.cFileName);
			ReplaceSign(szFileName, MAX_PATH, '&', _T("&amp;"));

			if (fdFindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
				pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
					_T("  <item name=\"%s\" isdir=\"true\"/>\r\n"), szFileName);
			} else {
				pszExt = _tcsrchr(szFileName, '.');

				if (pszExt != NULL) {
					*pszExt = '\0';
					pszExt++;
				}

				pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
					_T("  <item name=\"%s\" ext=\"%s\" size=\"%i\" "),
				  szFileName, (pszExt == NULL) ? _T("") : pszExt, fdFindFileData.nFileSizeLow);

				SYSTEMTIME systemTime;
				FileTimeToSystemTime(&fdFindFileData.ftCreationTime, &systemTime);
				pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
					_T("created=\"%i/%02i/%02i %i:%02i:%02i\" "),
					systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour,
					systemTime.wMinute, systemTime.wSecond);

				FileTimeToSystemTime(&fdFindFileData.ftLastWriteTime, &systemTime);
				pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
					_T("modified=\"%i/%02i/%02i %i:%02i:%02i\" "),
					systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour,
					systemTime.wMinute, systemTime.wSecond);

				pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
					_T("/>\r\n"));
			}

			if (!WriteFile(hFile, szBuffer, pszBuffer - szBuffer, &dwBytesWritten, NULL))
				break;
		}

	} while (FindNextFile(hFind, &fdFindFileData));

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

	// Add other shared files & directories
	for (CLFileShareNode * pclCur = pclFirstNode; pclCur ; pclCur = pclCur->pclNext) {
		if (!((pclCur->st.dwAllowedIP ^ dwRemoteIP) & pclCur->st.dwAllowedMask) &&  // hide inaccessible shares
		    (size_t)(pclCur->nGetSrvPathLen()) > _tcslen(pszSrvPath) &&
		    !_tcsstr(pclCur->st.pszRealPath, _T("\\@")) &&
		    !_tcsncmp(pclCur->st.pszSrvPath, pszSrvPath, _tcslen(pszSrvPath))) {
			pszBuffer = szBuffer;

			_tcscpy(szFileName, &pclCur->st.pszSrvPath[_tcslen(pszSrvPath)]);
			ReplaceSign(szFileName, MAX_PATH, '&', _T("&amp;"));

			if (pclCur->bIsDirectory()) {
				szFileName[_tcslen(szFileName)-1] = '\0';
				if (!_tcschr(szFileName, '/')) { // only one level deeper
					pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
						_T("  <item name=\"%s\" isdir=\"true\"/>\r\n"), szFileName);

					if (!WriteFile(hFile, szBuffer, pszBuffer - szBuffer, &dwBytesWritten, NULL))
						break;
				}
			} else {
				if (!_tcschr(szFileName, '/') &&   // only one level deeper
				    _tcsncmp(pszRealPath, pclCur->st.pszRealPath, _tcslen(pszRealPath))) { // no duplicates
					pszExt = _tcsrchr(szFileName, '.');

					if (pszExt != NULL) {
						*pszExt = '\0';
						pszExt++;
					}

					DWORD dwFileSize = 0;
					FILETIME ftFileCreateTime;
					FILETIME ftFileAccessTime;
					FILETIME ftFileModifyTime;
					HANDLE hFileS = CreateFile(pclCur->st.pszRealPath, GENERIC_READ, 
						FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
					if (hFileS != INVALID_HANDLE_VALUE) {
						dwFileSize = GetFileSize(hFileS, NULL);
						GetFileTime(hFileS, &ftFileCreateTime, &ftFileAccessTime, &ftFileModifyTime);
						CloseHandle(hFileS);
					}

					pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
						_T("  <item name=\"%s\" ext=\"%s\" size=\"%i\" "),
						szFileName, (pszExt == NULL) ? _T("") : pszExt, dwFileSize);

					SYSTEMTIME systemTime;
					FileTimeToSystemTime(&ftFileCreateTime, &systemTime);
					pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
						_T("created=\"%i/%02i/%02i %i:%02i:%02i\" "),
						systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour,
						systemTime.wMinute, systemTime.wSecond);

					FileTimeToSystemTime(&ftFileModifyTime, &systemTime);
					pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
						_T("modified=\"%i/%02i/%02i %i:%02i:%02i\" "),
						systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour,
						systemTime.wMinute, systemTime.wSecond);

					pszBuffer += mir_sntprintf(pszBuffer, BUFFER_SIZE - (pszBuffer - szBuffer), 
						_T("/>\r\n"));

					if (!WriteFile(hFile, szBuffer, pszBuffer - szBuffer, &dwBytesWritten, NULL))
						break;
				}
			}
		}
	}

	WriteFile(hFile, szXmlTail, sizeof(szXmlTail) - 1, &dwBytesWritten, NULL);

	SetEndOfFile(hFile);
	CloseHandle(hFile);

	return TRUE;
}