File: filepro.c

package info (click to toggle)
php3 3%3A3.0.18-0potato1.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 17,736 kB
  • ctags: 11,198
  • sloc: ansic: 108,120; sh: 2,512; php: 2,024; yacc: 1,887; makefile: 1,038; perl: 537; pascal: 238; awk: 90; cpp: 28; sql: 11
file content (493 lines) | stat: -rw-r--r-- 13,154 bytes parent folder | download | duplicates (3)
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
/*
   +----------------------------------------------------------------------+
   | PHP HTML Embedded Scripting Language Version 3.0                     |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2000 PHP Development Team (See Credits file)      |
   +----------------------------------------------------------------------+
   | This program is free software; you can redistribute it and/or modify |
   | it under the terms of one of the following licenses:                 |
   |                                                                      |
   |  A) 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.                                               |
   |                                                                      |
   |  B) the PHP License as published by the PHP Development Team and     |
   |     included in the distribution in the file: LICENSE                |
   |                                                                      |
   | 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 both licenses referred to here.   |
   | If you did not, or have any questions about PHP licensing, please    |
   | contact core@php.net.                                                |
   +----------------------------------------------------------------------+
   | Authors: Chad Robinson <chadr@brttech.com>                           |
   +----------------------------------------------------------------------+

  filePro 4.x support developed by Chad Robinson, chadr@brttech.com
  Contact Chad Robinson at BRT Technical Services Corp. for details.
  filePro is a registered trademark by Fiserv, Inc.  This file contains
  no code or information that is not freely available from the filePro
  web site at http://www.fileproplus.com/

 */

/* $Id: filepro.c,v 1.41 2000/01/01 04:31:15 sas Exp $ */
#include "php.h"
#include "internal_functions.h"
#include "safe_mode.h"
#include "fopen-wrappers.h"
#include <string.h>
#if MSVC5
#include <windows.h>
#else
#include <sys/param.h>
#endif
#include <errno.h>

#include "filepro.h"
#if HAVE_FILEPRO

typedef struct fp_field {
	char *name;
	char *format;
	int width;
	struct fp_field *next;
} FP_FIELD;

#define FP_GLOBAL(a) a
#define FP_TLS_VARS
static char *fp_database = NULL;			/* Database directory */
static signed int fp_fcount = -1;			/* Column count */
static signed int fp_keysize = -1;			/* Size of key records */
static FP_FIELD *fp_fieldlist = NULL;		/* List of fields */

int php3_minit_filepro(INIT_FUNC_ARGS)
{
	FP_GLOBAL(fp_database)=NULL;
	FP_GLOBAL(fp_fcount)=-1;
	FP_GLOBAL(fp_keysize)=-1;
	FP_GLOBAL(fp_fieldlist)=NULL;

	return SUCCESS;
}

int php3_mend_filepro(void){
	return SUCCESS;
}


function_entry filepro_functions[] = {
	{"filepro",				php3_filepro,				NULL},
	{"filepro_rowcount",	php3_filepro_rowcount,		NULL},
	{"filepro_fieldname",	php3_filepro_fieldname,		NULL},
	{"filepro_fieldtype",	php3_filepro_fieldtype,		NULL},
	{"filepro_fieldwidth",	php3_filepro_fieldwidth,	NULL},
	{"filepro_fieldcount",	php3_filepro_fieldcount,	NULL},
	{"filepro_retrieve",	php3_filepro_retrieve,		NULL},
	{NULL, NULL, NULL}
};

php3_module_entry filepro_module_entry = {
	"FilePro", filepro_functions, php3_minit_filepro, php3_mend_filepro, NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES
};


#if COMPILE_DL
#include "dl/phpdl.h"
DLEXPORT php3_module_entry *get_module(void) { return &filepro_module_entry; }
#endif

/*
 * LONG filePro(STRING directory)
 * 
 * Read and verify the map file.  We store the field count and field info
 * internally, which means we become unstable if you modify the table while
 * a user is using it!  We cannot lock anything since Web connections don't
 * provide the ability to later unlock what we locked.  Be smart, be safe.
 */
/* {{{ proto bool filepro(string directory)
   Read and verify the map file */
void php3_filepro(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *dir;
	FILE *fp;
	char workbuf[256]; /* FIX - should really be the max filename length */
	char readbuf[256];
	int i;
	FP_FIELD *new_field, *tmp;
	FP_TLS_VARS;

	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &dir) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_string(dir);

	/* FIX - we should really check and free these if they are used! */
	FP_GLOBAL(fp_database) = NULL;
    FP_GLOBAL(fp_fieldlist) = NULL;
	FP_GLOBAL(fp_fcount) = -1;
    FP_GLOBAL(fp_keysize) = -1;
	
	sprintf(workbuf, "%s/map", dir->value.str.val);

	if (php3_ini.safe_mode && (!_php3_checkuid(workbuf, 2))) {
		RETURN_FALSE;
	}
	
	if (_php3_check_open_basedir(workbuf)) {
		RETURN_FALSE;
	}

	if (!(fp = fopen(workbuf, "r"))) {
		php3_error(E_WARNING, "filePro: cannot open map: [%d] %s",
					errno, strerror(errno));
		RETURN_FALSE;
	}
	if (!fgets(readbuf, 250, fp)) {
		fclose(fp);
		php3_error(E_WARNING, "filePro: cannot read map: [%d] %s",
					errno, strerror(errno));
		RETURN_FALSE;
	}
	
	/* Get the field count, assume the file is readable! */
	if (strcmp(strtok(readbuf, ":"), "map")) {
		php3_error(E_WARNING, "filePro: map file corrupt or encrypted");
		RETURN_FALSE;
	}
	FP_GLOBAL(fp_keysize) = atoi(strtok(NULL, ":"));
	strtok(NULL, ":");
	FP_GLOBAL(fp_fcount) = atoi(strtok(NULL, ":"));
    
    /* Read in the fields themselves */
	for (i = 0; i < FP_GLOBAL(fp_fcount); i++) {
		if (!fgets(readbuf, 250, fp)) {
			fclose(fp);
			php3_error(E_WARNING, "filePro: cannot read map: [%d] %s",
						errno, strerror(errno));
			RETURN_FALSE;
		}
		new_field = emalloc(sizeof(FP_FIELD));
		new_field->next = NULL;
		new_field->name = estrdup(strtok(readbuf, ":"));
		new_field->width = atoi(strtok(NULL, ":"));
		new_field->format = estrdup(strtok(NULL, ":"));
        
		/* Store in forward-order to save time later */
		if (!FP_GLOBAL(fp_fieldlist)) {
			FP_GLOBAL(fp_fieldlist) = new_field;
		} else {
			for (tmp = FP_GLOBAL(fp_fieldlist); tmp; tmp = tmp->next) {
				if (!tmp->next) {
					tmp->next = new_field;
					tmp = new_field;
				}
			}
		}
	}
	fclose(fp);
		
	FP_GLOBAL(fp_database) = estrndup(dir->value.str.val,dir->value.str.len);

	RETVAL_TRUE;
}
/* }}} */


/*
 * LONG filePro_rowcount(void)
 * 
 * Count the used rows in the database.  filePro just marks deleted records
 * as deleted; they are not removed.  Since no counts are maintained we need
 * to go in and count records ourselves.  <sigh>
 * 
 * Errors return false, success returns the row count.
 */
/* {{{ proto int filepro_rowcount(void)
   Find out how many rows are in a filePro database */
void php3_filepro_rowcount(INTERNAL_FUNCTION_PARAMETERS)
{
	FILE *fp;
	char workbuf[MAXPATHLEN];
	char readbuf[256];
	int recsize = 0, records = 0;
	FP_TLS_VARS;

	if (ARG_COUNT(ht) != 0) {
		WRONG_PARAM_COUNT;
	}

	if (!FP_GLOBAL(fp_database)) {
		php3_error(E_WARNING,
					"filePro: must set database directory first!\n");
		RETURN_FALSE;
	}
	
	recsize = FP_GLOBAL(fp_keysize) + 19; /* 20 bytes system info -1 to save time later */
	
	/* Now read the records in, moving forward recsize-1 bytes each time */
	sprintf(workbuf, "%s/key", FP_GLOBAL(fp_database));

	if (php3_ini.safe_mode && (!_php3_checkuid(workbuf, 2))) {
		RETURN_FALSE;
	}
	
	if (_php3_check_open_basedir(workbuf)) {
		RETURN_FALSE;
	}

	if (!(fp = fopen(workbuf, "r"))) {
		php3_error(E_WARNING, "filePro: cannot open key: [%d] %s",
					errno, strerror(errno));
		RETURN_FALSE;
	}
	while (!feof(fp)) {
		if (fread(readbuf, 1, 1, fp) == 1) {
			if (readbuf[0])
				records++;
			fseek(fp, recsize, SEEK_CUR);
		}
	}
    fclose(fp);
	
	RETVAL_LONG(records);
}
/* }}} */


/*
 * STRING filePro_fieldname(LONG field_number)
 * 
 * Errors return false, success returns the name of the field.
 */
/* {{{ proto string filepro_fieldname(int fieldnumber)
   Gets the name of a field */
void php3_filepro_fieldname(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *fno;
	FP_FIELD *lp;
	int i;
	FP_TLS_VARS;

	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &fno) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(fno);

	if (!FP_GLOBAL(fp_database)) {
		php3_error(E_WARNING,
					"filePro: must set database directory first!\n");
		RETURN_FALSE;
	}
	
	for (i = 0, lp = FP_GLOBAL(fp_fieldlist); lp; lp = lp->next, i++) {
		if (i == fno->value.lval) {
			RETURN_STRING(lp->name,1);
		}
	}

	php3_error(E_WARNING,
				"filePro: unable to locate field number %d.\n",
				fno->value.lval);

	RETVAL_FALSE;
}
/* }}} */


/*
 * STRING filePro_fieldtype(LONG field_number)
 * 
 * Errors return false, success returns the type (edit) of the field
 */
/* {{{ proto string filepro_fieldtype(int field_number)
   Gets the type of a field */
void php3_filepro_fieldtype(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *fno;
	FP_FIELD *lp;
	int i;
	FP_TLS_VARS;

	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &fno) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(fno);

	if (!FP_GLOBAL(fp_database)) {
		php3_error(E_WARNING,
					"filePro: must set database directory first!\n");
		RETURN_FALSE;
	}
	
	for (i = 0, lp = FP_GLOBAL(fp_fieldlist); lp; lp = lp->next, i++) {
		if (i == fno->value.lval) {
			RETURN_STRING(lp->format,1);
		}
	}
	php3_error(E_WARNING,
				"filePro: unable to locate field number %d.\n",
				fno->value.lval);
	RETVAL_FALSE;
}
/* }}} */


/*
 * LONG filePro_fieldwidth(int field_number)
 * 
 * Errors return false, success returns the character width of the field.
 */
/* {{{ proto int filepro_fieldwidth(int field_number)
   Gets the width of a field */
void php3_filepro_fieldwidth(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *fno;
	FP_FIELD *lp;
	int i;
	FP_TLS_VARS;

	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &fno) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(fno);

	if (!FP_GLOBAL(fp_database)) {
		php3_error(E_WARNING,
					"filePro: must set database directory first!\n");
		RETURN_FALSE;
	}
	
	for (i = 0, lp = FP_GLOBAL(fp_fieldlist); lp; lp = lp->next, i++) {
		if (i == fno->value.lval) {
			RETURN_LONG(lp->width);
		}
	}
	php3_error(E_WARNING,
				"filePro: unable to locate field number %d.\n",
				fno->value.lval);
	RETVAL_FALSE;
}
/* }}} */


/*
 * LONG filePro_fieldcount(void)
 * 
 * Errors return false, success returns the field count.
 */
/* {{{ proto int filepro_fieldcount(void)
   Find out how many fields are in a filePro database */
void php3_filepro_fieldcount(INTERNAL_FUNCTION_PARAMETERS)
{
	FP_TLS_VARS;

	if (ARG_COUNT(ht) != 0) {
		WRONG_PARAM_COUNT;
	}

	if (!FP_GLOBAL(fp_database)) {
		php3_error(E_WARNING,
					"filePro: must set database directory first!\n");
		RETURN_FALSE;
	}
	
	/* Read in the first line from the map file */
	RETVAL_LONG(FP_GLOBAL(fp_fcount));
}
/* }}} */


/*
 * STRING filePro_retrieve(int row_number, int field_number)
 * 
 * Errors return false, success returns the datum.
 */
/* {{{ proto string filepro_retrieve(int row_number, int field_number)
   Retrieves data from a filePro database */
void php3_filepro_retrieve(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *rno, *fno;
    FP_FIELD *lp;
    FILE *fp;
    char workbuf[MAXPATHLEN];
	char readbuf[1024]; /* FIX - Work out better buffering! */
    int i, fnum, rnum;
    long offset;
	FP_TLS_VARS;

	if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &rno, &fno) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	if (!FP_GLOBAL(fp_database)) {
		php3_error(E_WARNING,
					"filePro: must set database directory first!\n");
		RETURN_FALSE;
	}
	
	convert_to_long(rno);
	convert_to_long(fno);

	fnum = fno->value.lval;
	rnum = rno->value.lval;
    
    if (rnum < 0 || fnum < 0 || fnum >= FP_GLOBAL(fp_fcount)) {
        php3_error(E_WARNING, "filepro: parameters out of range");
		RETURN_FALSE;
    }
    
    offset = (rnum + 1) * (FP_GLOBAL(fp_keysize) + 20) + 20; /* Record location */
    for (i = 0, lp = FP_GLOBAL(fp_fieldlist); lp && i < fnum; lp = lp->next, i++) {
        offset += lp->width;
    }
    if (!lp) {
        php3_error(E_WARNING, "filePro: cannot locate field");
		RETURN_FALSE;
    }
    
	/* Now read the record in */
	sprintf(workbuf, "%s/key", FP_GLOBAL(fp_database));

	if (php3_ini.safe_mode && (!_php3_checkuid(workbuf, 2))) {
		RETURN_FALSE;
	}
	
	if (_php3_check_open_basedir(workbuf)) {
		RETURN_FALSE;
	}

	if (!(fp = fopen(workbuf, "r"))) {
		php3_error(E_WARNING, "filePro: cannot open key: [%d] %s",
					errno, strerror(errno));
	    fclose(fp);
		RETURN_FALSE;
	}
    fseek(fp, offset, SEEK_SET);
	if (fread(readbuf, lp->width, 1, fp) != 1) {
        php3_error(E_WARNING, "filePro: cannot read data: [%d] %s",
					errno, strerror(errno));
	    fclose(fp);
		RETURN_FALSE;
    }
    readbuf[lp->width] = '\0';
    fclose(fp);
	RETURN_STRING(readbuf,1);
}
/* }}} */

#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */