/* $Id$ */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include "php.h"
#if HAVE_YAZ
char *addinfo;
Yaz_ResultSet resultSets;
int persistent;
+ int in_use;
int order;
int state;
int mask_select;
p->error = 0;
p->addinfo = 0;
p->resultSets = 0;
+ p->in_use = 0;
p->order = 0;
p->state = PHP_YAZ_STATE_CLOSED;
p->mask_select = 0;
static MUTEX_T yaz_mutex;
#endif
+ZEND_DECLARE_MODULE_GLOBALS(yaz);
+
static Yaz_Association *shared_associations;
static int order_associations;
+static int le_link;
static unsigned char third_argument_force_ref[] = {
3, BYREF_NONE, BYREF_NONE, BYREF_FORCE };
{NULL, NULL, NULL}
};
-static Yaz_Association get_assoc (pval **id)
+static void get_assoc (INTERNAL_FUNCTION_PARAMETERS, pval **id,
+ Yaz_Association *assocp)
{
- Yaz_Association assoc;
- int i;
- convert_to_long_ex(id);
- i = (*id)->value.lval;
+ Yaz_Association *as = 0;
+ YAZSLS_FETCH();
#ifdef ZTS
tsrm_mutex_lock (yaz_mutex);
#endif
- if (i < 1 || i > MAX_ASSOC || !shared_associations ||
- !(assoc = shared_associations[i-1]))
+ ZEND_FETCH_RESOURCE(as, Yaz_Association *, id, -1,
+ "YAZ link", le_link);
+ if (as && *as && (*as)->order == YAZSG(assoc_seq) && (*as)->in_use)
+ *assocp = *as;
+ else
{
#ifdef ZTS
tsrm_mutex_unlock (yaz_mutex);
#endif
php_error(E_WARNING, "Invalid YAZ handle");
- return 0;
+ *assocp = 0;
}
- return assoc;
}
static void release_assoc (Yaz_Association assoc)
static void do_connect (Yaz_Association p)
{
void *addr;
- char *cp;
p->reconnect_flag = 0;
p->cs = cs_create (tcpip_type, 0, PROTO_Z3950);
void scan_response (Yaz_Association t, Z_ScanResponse *res)
{
NMEM nmem = odr_extract_mem (t->odr_in);
- if (res->entries && res->entries->nonsurrogateDiagnostics)
+ if (res->entries && res->entries->nonsurrogateDiagnostics)
response_diag(t, res->entries->nonsurrogateDiagnostics[0]);
t->scan_response = res;
nmem_transfer (t->odr_scan->mem, nmem);
if (a == 0)
abort();
sprintf (str, "send_APDU t=%p type=%d", t, a->which);
-#if 0
- php_error (E_WARNING, str);
-#endif
if (t->cookie_out)
{
Z_OtherInformation **oi;
yaz_oi_APDU(a, &oi);
yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, t->cookie_out);
}
-/* from ZAP */
-#if 0
- if (req->request->connection)
- {
- Z_OtherInformation **oi;
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1,
- req->request->connection->remote_ip);
- }
-#endif
if (!z_APDU(out, &a, 0, 0))
{
FILE *outf = fopen("/tmp/apdu.txt", "w");
static void send_init(Yaz_Association t)
{
- char *cp;
Z_APDU *apdu = zget_APDU(t->odr_out, Z_APDU_initRequest);
Z_InitRequest *ireq = apdu->u.initRequest;
Z_IdAuthentication *auth = odr_malloc(t->odr_out, sizeof(*auth));
strcpy(auth->u.open, auth_userId);
ireq->idAuthentication = auth;
}
- yaz_oi_set_string_oidval(&ireq->otherInfo, t->odr_out,
- VAL_PROXY, 1, t->host_port);
+ if (t->proxy)
+ yaz_oi_set_string_oidval(&ireq->otherInfo, t->odr_out,
+ VAL_PROXY, 1, t->host_port);
send_APDU (t, apdu);
}
int no = 0;
int max_fd = 0;
struct timeval tv;
+ YAZSLS_FETCH();
tv.tv_sec = timeout;
tv.tv_usec = 0;
{
Yaz_Association p = shared_associations[i];
int fd;
- if (!p || p->order != order_associations || !p->cs)
+ if (!p || p->order != YAZSG(assoc_seq) || !p->cs)
continue;
fd = cs_fileno (p->cs);
if (max_fd < fd)
{
int fd;
Yaz_Association p = shared_associations[i];
- if (!p || p->order != order_associations || !p->cs)
+ if (!p || p->order != YAZSG(assoc_seq) || !p->cs)
continue;
*id = i+1;
fd =cs_fileno(p->cs);
const char *user_str = 0, *group_str = 0, *pass_str = 0;
const char *cookie_str = 0, *proxy_str = 0;
int persistent = 1;
- pval **zurl, **user = 0, **group = 0, **pass = 0;
+ pval **zurl, **user = 0;
+ Yaz_Association as;
+ YAZSLS_FETCH();
+
if (ZEND_NUM_ARGS() == 1)
{
if (zend_get_parameters_ex (1, &zurl) == FAILURE)
#endif
for (i = 0; i<MAX_ASSOC; i++)
{
- Yaz_Association as = shared_associations[i];
- if (persistent && as && as->order != order_associations &&
+ as = shared_associations[i];
+ if (persistent && as && !as->in_use &&
!strcmp_null (as->host_port, zurl_str) &&
!strcmp_null (as->user, user_str) &&
!strcmp_null (as->group, group_str) &&
int min_order = 2000000000;
/* find completely free slot or the oldest one */
for (i = 0; i<MAX_ASSOC && shared_associations[i]; i++)
- if (shared_associations[i]->order < min_order
- && shared_associations[i]->order != order_associations)
+ {
+ as = shared_associations[i];
+ if (persistent && !as->in_use && as->order < min_order)
{
- min_order = shared_associations[i]->order;
+ min_order = as->order;
i0 = i;
}
+ }
if (i == MAX_ASSOC)
{
i = i0;
else /* "best" free slot */
yaz_association_destroy(shared_associations[i]);
}
- shared_associations[i] = yaz_association_mk ();
- shared_associations[i]->host_port = xstrdup (zurl_str);
+ shared_associations[i] = as = yaz_association_mk ();
+ as->host_port = xstrdup (zurl_str);
if (cookie_str)
- shared_associations[i]->cookie_out = xstrdup (cookie_str);
+ as->cookie_out = xstrdup (cookie_str);
if (user_str)
- shared_associations[i]->user = xstrdup (user_str);
+ as->user = xstrdup (user_str);
if (group_str)
- shared_associations[i]->group = xstrdup (group_str);
+ as->group = xstrdup (group_str);
if (pass_str)
- shared_associations[i]->pass = xstrdup (pass_str);
+ as->pass = xstrdup (pass_str);
if (proxy_str)
- shared_associations[i]->proxy = xstrdup (proxy_str);
- }
- shared_associations[i]->persistent = persistent;
- shared_associations[i]->order = order_associations;
- shared_associations[i]->error = 0;
- shared_associations[i]->numberOfRecordsRequested = 10;
- shared_associations[i]->resultSetStartPoint = 1;
- xfree (shared_associations[i]->local_databases);
- shared_associations[i]->local_databases = 0;
+ as->proxy = xstrdup (proxy_str);
+ }
+ as->in_use = 1;
+ as->persistent = persistent;
+ as->order = YAZSG(assoc_seq);
+ as->error = 0;
+ as->numberOfRecordsRequested = 10;
+ as->resultSetStartPoint = 1;
+ xfree (as->local_databases);
+ as->local_databases = 0;
#ifdef ZTS
tsrm_mutex_unlock (yaz_mutex);
#endif
- RETURN_LONG(i+1);
+ ZEND_REGISTER_RESOURCE(return_value, &shared_associations[i], le_link);
+/* RETURN_LONG(i+1); */
}
/* }}} */
{
Yaz_Association p;
pval **id;
- int i;
if (ZEND_NUM_ARGS() != 1)
WRONG_PARAM_COUNT;
if (zend_get_parameters_ex (1, &id) == FAILURE)
RETURN_FALSE;
- p = get_assoc (id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (!p)
RETURN_FALSE;
- convert_to_long_ex (id);
- i = (*id)->value.lval - 1;
- yaz_association_destroy (shared_associations[i]);
- shared_associations[i] = 0;
release_assoc (p);
+ zend_list_delete ((*id)->value.lval);
RETURN_TRUE;
}
/* }}} */
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (!p)
{
RETURN_FALSE;
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (!p)
{
RETURN_FALSE;
Process events. */
PHP_FUNCTION(yaz_wait)
{
- int i;
- int id;
- int timeout = 15;
+ int i, id, timeout = 15;
+ YAZSLS_FETCH();
+
if (ZEND_NUM_ARGS() == 1)
{
long *val = 0;
for (i = 0; i<MAX_ASSOC; i++)
{
Yaz_Association p = shared_associations[i];
- if (!p || p->order != order_associations || !p->action
+ if (!p || p->order != YAZSG(assoc_seq) || !p->action
|| p->mask_select)
continue;
if (!p->cs)
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (!p)
{
RETURN_LONG(0);
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (p && p->error)
{
const char *msg = 0;
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (p && p->error > 0 && p->addinfo && *p->addinfo)
{
RETVAL_STRING(p->addinfo, 1);
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (!p || !p->resultSets)
{
RETVAL_LONG(0);
tag->content->u.subtree =
odr_malloc (o, sizeof(*tag->content->u.subtree));
- tag->content->u.subtree->elements = odr_malloc (o, sizeof(*r->elements));
+ tag->content->u.subtree->elements =
+ odr_malloc (o, sizeof(*r->elements));
tag->content->u.subtree->num_elements = 1;
tag = tag->content->u.subtree->elements[0] =
i = data_offset + base_address;
end_offset = i+data_length-1;
+
if (indicator_length == 2)
{
if (buf[i + indicator_length] != ISO2709_IDFS)
}
else if (!memcmp (tag_str, "00", 2))
identifier_flag = 0;
+
- if (identifier_flag && indicator_length)
+ if (identifier_flag && indicator_length)
{
/* indicator */
tag->tagValue->u.string = odr_malloc(o, indicator_length+1);
{
int i0;
/* prepare tag */
- Z_TaggedElement *parent_tag = tag;
+ Z_TaggedElement *parent_tag = tag;
Z_TaggedElement *tag = odr_malloc (o, sizeof(*tag));
- if (parent_tag->content->u.subtree->num_elements < 256)
- parent_tag->content->u.subtree->elements[
- parent_tag->content->u.subtree->num_elements++] = tag;
+ if (parent_tag->content->u.subtree->num_elements < 256)
+ parent_tag->content->u.subtree->elements[
+ parent_tag->content->u.subtree->num_elements++] = tag;
tag->tagType = odr_malloc(o, sizeof(*tag->tagType));
*tag->tagType = 3;
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
convert_to_long_ex(pval_pos);
pos = (*pval_pos)->value.lval;
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
if (p)
{
convert_to_string_ex (pval_syntax);
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
if (p)
{
convert_to_string_ex (pval_element);
{
WRONG_PARAM_COUNT;
}
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
if (p)
{
convert_to_long_ex (pval_start);
php_error(E_WARNING, "yaz_itemorder: Expected array parameter");
RETURN_FALSE;
}
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
if (p)
{
Z_APDU *apdu;
convert_to_string_ex (pval_type);
convert_to_string_ex (pval_query);
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
if (p)
{
Z_APDU *apdu;
{
RETURN_FALSE;
}
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
if (p && p->scan_response)
{
int i;
php_error(E_WARNING, "yaz_ccl_conf: Expected array parameter");
RETURN_FALSE;
}
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
if (p)
{
HashTable *ht = Z_ARRVAL_PP(pval_package);
RETURN_FALSE;
}
convert_to_string_ex (pval_query);
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
if (p)
{
const char *query_str = (*pval_query)->value.str.val;
WRONG_PARAM_COUNT;
}
convert_to_string_ex (pval_database);
- p = get_assoc (pval_id);
+ get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
if (p)
{
xfree (p->local_databases);
/* }}} */
+/* {{{ php_yaz_init_globals
+ */
+static void php_yaz_init_globals(zend_yaz_globals *yaz_globals)
+{
+ yaz_globals->assoc_seq = 0;
+}
+/* }}} */
+
+void yaz_close_session(Yaz_Association *as)
+{
+ YAZSLS_FETCH();
+
+ if (*as && (*as)->order == YAZSG(assoc_seq))
+ {
+ if ((*as)->persistent)
+ (*as)->in_use = 0;
+ else
+ {
+ yaz_association_destroy(*as);
+ *as = 0;
+ }
+ }
+}
+
+static void yaz_close_link (zend_rsrc_list_entry *rsrc)
+{
+ Yaz_Association *as = (Yaz_Association *) rsrc->ptr;
+ yaz_close_session (as);
+}
+
PHP_MINIT_FUNCTION(yaz)
{
int i;
#ifdef ZTS
yaz_mutex = tsrm_mutex_alloc();
#endif
+ ZEND_INIT_MODULE_GLOBALS(yaz, php_yaz_init_globals, NULL);
+
+ le_link = zend_register_list_destructors_ex (yaz_close_link, 0,
+ "YAZ link", module_number);
order_associations = 1;
shared_associations = xmalloc (sizeof(*shared_associations) * MAX_ASSOC);
for (i = 0; i<MAX_ASSOC; i++)
PHP_RSHUTDOWN_FUNCTION(yaz)
{
- int i;
-
-#ifdef ZTS
- tsrm_mutex_lock (yaz_mutex);
-#endif
- if (shared_associations)
- {
- for (i = 0; i<MAX_ASSOC; i++)
- /* destroy those where password has been used */
- if (shared_associations[i] && !shared_associations[i]->persistent)
- {
- yaz_association_destroy(shared_associations[i]);
- shared_associations[i] = 0;
- }
- }
-#ifdef ZTS
- tsrm_mutex_unlock (yaz_mutex);
-#endif
return SUCCESS;
}
PHP_RINIT_FUNCTION(yaz)
{
+ YAZSLS_FETCH();
#ifdef ZTS
tsrm_mutex_lock (yaz_mutex);
#endif
- order_associations++;
+ YAZSG(assoc_seq) = order_associations++;
#ifdef ZTS
tsrm_mutex_unlock (yaz_mutex);
#endif
ZEND_GET_MODULE(yaz)
#endif
-
#endif
/*