php-src/ext/filepro/filepro.c

593 lines
14 KiB
C
Raw Normal View History

1999-04-21 22:05:45 +00:00
/*
+----------------------------------------------------------------------+
2001-12-11 15:32:16 +00:00
| PHP Version 4 |
1999-04-21 22:05:45 +00:00
+----------------------------------------------------------------------+
2002-12-31 16:08:15 +00:00
| Copyright (c) 1997-2003 The PHP Group |
1999-04-21 22:05:45 +00:00
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
1999-07-16 13:13:16 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
1999-04-21 22:05:45 +00:00
+----------------------------------------------------------------------+
2002-02-28 08:29:35 +00:00
| Author: Chad Robinson <chadr@brttech.com> |
1999-04-21 22:05:45 +00:00
+----------------------------------------------------------------------+
*/
1999-04-21 22:05:45 +00:00
/* $Id$ */
/*
1999-04-21 22:05:45 +00:00
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/
*/
1999-04-23 20:06:01 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
1999-04-21 22:05:45 +00:00
#include "php.h"
#include "safe_mode.h"
2001-02-23 22:07:16 +00:00
#include "fopen_wrappers.h"
1999-04-21 22:05:45 +00:00
#include <string.h>
2000-02-11 15:59:30 +00:00
#ifdef PHP_WIN32
1999-04-21 22:05:45 +00:00
#include <windows.h>
#else
#include <sys/param.h>
#endif
#include <errno.h>
#include "php_globals.h"
#include "php_filepro.h"
1999-04-21 22:05:45 +00:00
#if HAVE_FILEPRO
typedef struct fp_field {
char *name;
char *format;
int width;
struct fp_field *next;
} FP_FIELD;
#ifdef THREAD_SAFE
DWORD FPTls;
static int numthreads=0;
typedef struct fp_global_struct{
char *fp_database;
signed int fp_fcount;
signed int fp_keysize;
FP_FIELD *fp_fieldlist;
}fp_global_struct;
#define FP_GLOBAL(a) fp_globals->a
#define FP_TLS_VARS \
fp_global_struct *fp_globals; \
fp_globals=TlsGetValue(FPTls);
#else
#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 */
#endif
/* {{{ PHP_MINIT_FUNCTION
*/
1999-07-27 19:44:46 +00:00
PHP_MINIT_FUNCTION(filepro)
1999-04-21 22:05:45 +00:00
{
#ifdef THREAD_SAFE
fp_global_struct *fp_globals;
2000-05-23 09:33:51 +00:00
#ifdef COMPILE_DL_FILEPRO
1999-04-21 22:05:45 +00:00
CREATE_MUTEX(fp_mutex,"FP_TLS");
SET_MUTEX(fp_mutex);
numthreads++;
if (numthreads==1){
if ((FPTls=TlsAlloc())==0xFFFFFFFF){
FREE_MUTEX(fp_mutex);
return 0;
}}
FREE_MUTEX(fp_mutex);
#endif
fp_globals = (fp_global_struct *) LocalAlloc(LPTR, sizeof(fp_global_struct));
TlsSetValue(FPTls, (void *) fp_globals);
#endif
FP_GLOBAL(fp_database)=NULL;
FP_GLOBAL(fp_fcount)=-1;
FP_GLOBAL(fp_keysize)=-1;
FP_GLOBAL(fp_fieldlist)=NULL;
return SUCCESS;
}
/* }}} */
1999-04-21 22:05:45 +00:00
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
1999-07-27 19:44:46 +00:00
PHP_MSHUTDOWN_FUNCTION(filepro)
{
1999-04-21 22:05:45 +00:00
#ifdef THREAD_SAFE
fp_global_struct *fp_globals;
fp_globals = TlsGetValue(FPTls);
if (fp_globals != 0)
LocalFree((HLOCAL) fp_globals);
2000-05-23 09:33:51 +00:00
#ifdef COMPILE_DL_FILEPRO
1999-04-21 22:05:45 +00:00
SET_MUTEX(fp_mutex);
numthreads--;
if (!numthreads){
if (!TlsFree(FPTls)){
FREE_MUTEX(fp_mutex);
return 0;
}
}
1999-04-21 22:05:45 +00:00
FREE_MUTEX(fp_mutex);
#endif
#endif
return SUCCESS;
}
/* }}} */
1999-04-21 22:05:45 +00:00
function_entry filepro_functions[] = {
1999-07-27 19:44:46 +00:00
PHP_FE(filepro, NULL)
PHP_FE(filepro_rowcount, NULL)
PHP_FE(filepro_fieldname, NULL)
PHP_FE(filepro_fieldtype, NULL)
PHP_FE(filepro_fieldwidth, NULL)
PHP_FE(filepro_fieldcount, NULL)
PHP_FE(filepro_retrieve, NULL)
1999-04-21 22:05:45 +00:00
{NULL, NULL, NULL}
};
zend_module_entry filepro_module_entry = {
STANDARD_MODULE_HEADER,
"filepro", filepro_functions, PHP_MINIT(filepro), PHP_MSHUTDOWN(filepro), NULL, NULL, NULL, NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
1999-04-21 22:05:45 +00:00
};
2000-05-23 09:33:51 +00:00
#ifdef COMPILE_DL_FILEPRO
ZEND_GET_MODULE(filepro)
#if defined(PHP_WIN32) && defined(THREAD_SAFE)
1999-04-21 22:05:45 +00:00
/*NOTE: You should have an odbc.def file where you
export DllMain*/
BOOL WINAPI DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch( ul_reason_for_call ) {
case DLL_PROCESS_ATTACH:
if ((FPTls=TlsAlloc())==0xFFFFFFFF) {
return 0;
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
if (!TlsFree(FPTls)) {
return 0;
}
break;
}
return 1;
1999-04-21 22:05:45 +00:00
}
#endif
#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.
*/
2000-05-23 22:11:56 +00:00
/* {{{ proto bool filepro(string directory)
Read and verify the map file */
PHP_FUNCTION(filepro)
1999-04-21 22:05:45 +00:00
{
pval *dir;
FILE *fp;
char workbuf[MAXPATHLEN];
1999-04-21 22:05:45 +00:00
char readbuf[256];
char *strtok_buf = NULL;
1999-04-21 22:05:45 +00:00
int i;
FP_FIELD *new_field, *tmp, *next;
1999-04-21 22:05:45 +00:00
FP_TLS_VARS;
if (ZEND_NUM_ARGS() != 1 || getParameters(ht, 1, &dir) == FAILURE) {
1999-04-21 22:05:45 +00:00
WRONG_PARAM_COUNT;
}
convert_to_string(dir);
/* free memory */
if (FP_GLOBAL(fp_database) != NULL) {
efree (FP_GLOBAL(fp_database));
}
/* free linked list of fields */
tmp = FP_GLOBAL(fp_fieldlist);
while (tmp != NULL) {
next = tmp->next;
efree(tmp);
tmp = next;
}
/* init the global vars */
1999-04-21 22:05:45 +00:00
FP_GLOBAL(fp_database) = NULL;
FP_GLOBAL(fp_fieldlist) = NULL;
1999-04-21 22:05:45 +00:00
FP_GLOBAL(fp_fcount) = -1;
FP_GLOBAL(fp_keysize) = -1;
1999-04-21 22:05:45 +00:00
snprintf(workbuf, sizeof(workbuf), "%s/map", Z_STRVAL_P(dir));
if (PG(safe_mode) && (!php_checkuid(workbuf, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
if (php_check_open_basedir(workbuf TSRMLS_CC)) {
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
if (!(fp = VCWD_FOPEN(workbuf, "r"))) {
2002-06-27 06:54:51 +00:00
php_error(E_WARNING, "%s(): Cannot open map: [%d] %s", get_active_function_name(TSRMLS_C),
1999-04-21 22:05:45 +00:00
errno, strerror(errno));
RETURN_FALSE;
}
if (!fgets(readbuf, sizeof(readbuf), fp)) {
1999-04-21 22:05:45 +00:00
fclose(fp);
2002-06-27 06:54:51 +00:00
php_error(E_WARNING, "%s(): Cannot read map: [%d] %s", get_active_function_name(TSRMLS_C),
1999-04-21 22:05:45 +00:00
errno, strerror(errno));
RETURN_FALSE;
}
/* Get the field count, assume the file is readable! */
if (strcmp(php_strtok_r(readbuf, ":", &strtok_buf), "map")) {
2002-06-27 06:54:51 +00:00
php_error(E_WARNING, "%s(): Map file corrupt or encrypted", get_active_function_name(TSRMLS_C));
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
FP_GLOBAL(fp_keysize) = atoi(php_strtok_r(NULL, ":", &strtok_buf));
php_strtok_r(NULL, ":", &strtok_buf);
FP_GLOBAL(fp_fcount) = atoi(php_strtok_r(NULL, ":", &strtok_buf));
/* Read in the fields themselves */
1999-04-21 22:05:45 +00:00
for (i = 0; i < FP_GLOBAL(fp_fcount); i++) {
if (!fgets(readbuf, sizeof(readbuf), fp)) {
1999-04-21 22:05:45 +00:00
fclose(fp);
2002-06-27 06:54:51 +00:00
php_error(E_WARNING, "%s(): Cannot read map: [%d] %s", get_active_function_name(TSRMLS_C),
1999-04-21 22:05:45 +00:00
errno, strerror(errno));
RETURN_FALSE;
}
new_field = emalloc(sizeof(FP_FIELD));
new_field->next = NULL;
new_field->name = estrdup(php_strtok_r(readbuf, ":", &strtok_buf));
new_field->width = atoi(php_strtok_r(NULL, ":", &strtok_buf));
new_field->format = estrdup(php_strtok_r(NULL, ":", &strtok_buf));
1999-04-21 22:05:45 +00:00
/* 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(Z_STRVAL_P(dir), Z_STRLEN_P(dir));
1999-04-21 22:05:45 +00:00
RETVAL_TRUE;
}
2000-05-23 22:11:56 +00:00
/* }}} */
1999-04-21 22:05:45 +00:00
/*
* 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.
*/
2000-05-23 22:11:56 +00:00
/* {{{ proto int filepro_rowcount(void)
Find out how many rows are in a filePro database */
PHP_FUNCTION(filepro_rowcount)
1999-04-21 22:05:45 +00:00
{
FILE *fp;
char workbuf[MAXPATHLEN];
char readbuf[256];
int recsize = 0, records = 0;
FP_TLS_VARS;
if (ZEND_NUM_ARGS() != 0) {
1999-04-21 22:05:45 +00:00
WRONG_PARAM_COUNT;
}
if (!FP_GLOBAL(fp_database)) {
php_error(E_WARNING,
2002-06-27 06:54:51 +00:00
"%s(): Must set database directory first!", get_active_function_name(TSRMLS_C));
1999-04-21 22:05:45 +00:00
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 */
snprintf(workbuf, sizeof(workbuf), "%s/key", FP_GLOBAL(fp_database));
1999-04-21 22:05:45 +00:00
if (PG(safe_mode) && (!php_checkuid(workbuf, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
if (php_check_open_basedir(workbuf TSRMLS_CC)) {
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
if (!(fp = VCWD_FOPEN(workbuf, "r"))) {
2002-06-27 06:54:51 +00:00
php_error(E_WARNING, "%s(): Cannot open key: [%d] %s", get_active_function_name(TSRMLS_C),
1999-04-21 22:05:45 +00:00
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);
1999-04-21 22:05:45 +00:00
RETVAL_LONG(records);
}
2000-05-23 22:11:56 +00:00
/* }}} */
1999-04-21 22:05:45 +00:00
/*
* STRING filePro_fieldname(LONG field_number)
*
* Errors return false, success returns the name of the field.
*/
2000-05-23 22:11:56 +00:00
/* {{{ proto string filepro_fieldname(int fieldnumber)
Gets the name of a field */
PHP_FUNCTION(filepro_fieldname)
1999-04-21 22:05:45 +00:00
{
pval *fno;
FP_FIELD *lp;
int i;
FP_TLS_VARS;
if (ZEND_NUM_ARGS() != 1 || getParameters(ht, 1, &fno) == FAILURE) {
1999-04-21 22:05:45 +00:00
WRONG_PARAM_COUNT;
}
convert_to_long(fno);
if (!FP_GLOBAL(fp_database)) {
php_error(E_WARNING,
2002-06-27 06:54:51 +00:00
"%s(): Must set database directory first!", get_active_function_name(TSRMLS_C));
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
for (i = 0, lp = FP_GLOBAL(fp_fieldlist); lp; lp = lp->next, i++) {
if (i == Z_LVAL_P(fno)) {
2001-08-11 16:39:07 +00:00
RETURN_STRING(lp->name, 1);
1999-04-21 22:05:45 +00:00
}
}
php_error(E_WARNING,
2002-06-27 06:54:51 +00:00
"%s(): Unable to locate field number %d.", get_active_function_name(TSRMLS_C),
Z_LVAL_P(fno));
1999-04-21 22:05:45 +00:00
RETVAL_FALSE;
}
2000-05-23 22:11:56 +00:00
/* }}} */
1999-04-21 22:05:45 +00:00
/*
* STRING filePro_fieldtype(LONG field_number)
*
* Errors return false, success returns the type (edit) of the field
*/
2000-05-23 22:11:56 +00:00
/* {{{ proto string filepro_fieldtype(int field_number)
Gets the type of a field */
PHP_FUNCTION(filepro_fieldtype)
1999-04-21 22:05:45 +00:00
{
pval *fno;
FP_FIELD *lp;
int i;
FP_TLS_VARS;
if (ZEND_NUM_ARGS() != 1 || getParameters(ht, 1, &fno) == FAILURE) {
1999-04-21 22:05:45 +00:00
WRONG_PARAM_COUNT;
}
convert_to_long(fno);
if (!FP_GLOBAL(fp_database)) {
php_error(E_WARNING,
2002-06-27 06:54:51 +00:00
"%s(): Must set database directory first!", get_active_function_name(TSRMLS_C));
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
for (i = 0, lp = FP_GLOBAL(fp_fieldlist); lp; lp = lp->next, i++) {
if (i == Z_LVAL_P(fno)) {
2001-08-11 16:39:07 +00:00
RETURN_STRING(lp->format, 1);
1999-04-21 22:05:45 +00:00
}
}
php_error(E_WARNING,
2002-06-27 06:54:51 +00:00
"%s(): Unable to locate field number %d.", get_active_function_name(TSRMLS_C),
Z_LVAL_P(fno));
1999-04-21 22:05:45 +00:00
RETVAL_FALSE;
}
2000-05-23 22:11:56 +00:00
/* }}} */
1999-04-21 22:05:45 +00:00
/*
* STRING filePro_fieldwidth(int field_number)
*
* Errors return false, success returns the character width of the field.
*/
2000-05-23 22:11:56 +00:00
/* {{{ proto int filepro_fieldwidth(int field_number)
Gets the width of a field */
PHP_FUNCTION(filepro_fieldwidth)
1999-04-21 22:05:45 +00:00
{
pval *fno;
FP_FIELD *lp;
int i;
FP_TLS_VARS;
if (ZEND_NUM_ARGS() != 1 || getParameters(ht, 1, &fno) == FAILURE) {
1999-04-21 22:05:45 +00:00
WRONG_PARAM_COUNT;
}
convert_to_long(fno);
if (!FP_GLOBAL(fp_database)) {
php_error(E_WARNING,
2002-06-27 06:54:51 +00:00
"%s(): Must set database directory first!", get_active_function_name(TSRMLS_C));
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
for (i = 0, lp = FP_GLOBAL(fp_fieldlist); lp; lp = lp->next, i++) {
if (i == Z_LVAL_P(fno)) {
1999-04-21 22:05:45 +00:00
RETURN_LONG(lp->width);
}
}
php_error(E_WARNING,
2002-06-27 06:54:51 +00:00
"%s(): Unable to locate field number %d.", get_active_function_name(TSRMLS_C),
Z_LVAL_P(fno));
1999-04-21 22:05:45 +00:00
RETVAL_FALSE;
}
2000-05-23 22:11:56 +00:00
/* }}} */
1999-04-21 22:05:45 +00:00
/*
* LONG filePro_fieldcount(void)
*
* Errors return false, success returns the field count.
*/
2000-05-23 22:11:56 +00:00
/* {{{ proto int filepro_fieldcount(void)
Find out how many fields are in a filePro database */
PHP_FUNCTION(filepro_fieldcount)
1999-04-21 22:05:45 +00:00
{
FP_TLS_VARS;
if (ZEND_NUM_ARGS() != 0) {
1999-04-21 22:05:45 +00:00
WRONG_PARAM_COUNT;
}
if (!FP_GLOBAL(fp_database)) {
php_error(E_WARNING,
2002-06-27 06:54:51 +00:00
"%s(): Must set database directory first!", get_active_function_name(TSRMLS_C));
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
/* Read in the first line from the map file */
RETVAL_LONG(FP_GLOBAL(fp_fcount));
}
2000-05-23 22:11:56 +00:00
/* }}} */
1999-04-21 22:05:45 +00:00
/*
* STRING filePro_retrieve(int row_number, int field_number)
*
* Errors return false, success returns the datum.
*/
2000-05-23 22:11:56 +00:00
/* {{{ proto string filepro_retrieve(int row_number, int field_number)
Retrieves data from a filePro database */
PHP_FUNCTION(filepro_retrieve)
1999-04-21 22:05:45 +00:00
{
pval *rno, *fno;
FP_FIELD *lp;
FILE *fp;
char workbuf[MAXPATHLEN];
char *readbuf;
int i, fnum, rnum;
long offset;
1999-04-21 22:05:45 +00:00
FP_TLS_VARS;
if (ZEND_NUM_ARGS() != 2 || getParameters(ht, 2, &rno, &fno) == FAILURE) {
1999-04-21 22:05:45 +00:00
WRONG_PARAM_COUNT;
}
if (!FP_GLOBAL(fp_database)) {
php_error(E_WARNING,
2002-06-27 06:54:51 +00:00
"%s(): Must set database directory first!", get_active_function_name(TSRMLS_C));
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
convert_to_long(rno);
convert_to_long(fno);
fnum = Z_LVAL_P(fno);
rnum = Z_LVAL_P(rno);
1999-04-21 22:05:45 +00:00
if (rnum < 0 || fnum < 0 || fnum >= FP_GLOBAL(fp_fcount)) {
2002-06-27 06:54:51 +00:00
php_error(E_WARNING, "%s(): Parameters out of range", get_active_function_name(TSRMLS_C));
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
1999-04-21 22:05:45 +00:00
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) {
2002-06-27 06:54:51 +00:00
php_error(E_WARNING, "%s(): Cannot locate field", get_active_function_name(TSRMLS_C));
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
1999-04-21 22:05:45 +00:00
/* Now read the record in */
snprintf(workbuf, sizeof(workbuf), "%s/key", FP_GLOBAL(fp_database));
1999-04-21 22:05:45 +00:00
if (PG(safe_mode) && (!php_checkuid(workbuf, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
if (php_check_open_basedir(workbuf TSRMLS_CC)) {
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
if (!(fp = VCWD_FOPEN(workbuf, "r"))) {
2002-06-27 06:54:51 +00:00
php_error(E_WARNING, "%s(): Cannot open key: [%d] %s", get_active_function_name(TSRMLS_C),
1999-04-21 22:05:45 +00:00
errno, strerror(errno));
fclose(fp);
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
fseek(fp, offset, SEEK_SET);
readbuf = emalloc (lp->width+1);
1999-04-21 22:05:45 +00:00
if (fread(readbuf, lp->width, 1, fp) != 1) {
2002-06-27 06:54:51 +00:00
php_error(E_WARNING, "%s(): Cannot read data: [%d] %s", get_active_function_name(TSRMLS_C),
1999-04-21 22:05:45 +00:00
errno, strerror(errno));
efree(readbuf);
fclose(fp);
1999-04-21 22:05:45 +00:00
RETURN_FALSE;
}
readbuf[lp->width] = '\0';
fclose(fp);
RETURN_STRING(readbuf, 0);
1999-04-21 22:05:45 +00:00
}
2000-05-23 22:11:56 +00:00
/* }}} */
1999-04-21 22:05:45 +00:00
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
1999-04-21 22:05:45 +00:00
*/