cvs: pecl /pop3 php_pop3.h pop3.c

From: Date: Fri, 09 Jan 2004 23:05:29 +0000
Subject: cvs: pecl /pop3 php_pop3.h pop3.c
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-724@lists.php.net to get a copy of this message
iliaa Fri Jan 9 18:05:29 2004 EDT Modified files: /pecl/pop3 php_pop3.h pop3.c Log: CS, OOP added pop3_get_account_size(). # more functions to be added shortly.

Index: pecl/pop3/php_pop3.h diff -u pecl/pop3/php_pop3.h:1.1 pecl/pop3/php_pop3.h:1.2 --- pecl/pop3/php_pop3.h:1.1 Fri Jan 9 12:29:57 2004 +++ pecl/pop3/php_pop3.h Fri Jan 9 18:05:29 2004 @@ -15,7 +15,7 @@ | Author: | +----------------------------------------------------------------------+ - $Id: php_pop3.h,v 1.1 2004/01/09 17:29:57 wez Exp $ + $Id: php_pop3.h,v 1.2 2004/01/09 23:05:29 iliaa Exp $ */ #ifndef PHP_POP3_H @@ -34,6 +34,18 @@ #include "TSRM.h" #endif +static void pop3_objects_dtor(void *object, zend_object_handle handle TSRMLS_DC); + +PHP_POP3_API zend_object_value pop3_objects_new(zend_class_entry * TSRMLS_DC); + +zend_class_entry *pop3_class_entry; + +typedef struct _pop3_object { + zend_object zo; + int *ptr; +} pop3_object; /* extends zend_object */ + + PHP_MINIT_FUNCTION(pop3); PHP_MSHUTDOWN_FUNCTION(pop3); PHP_RINIT_FUNCTION(pop3); @@ -42,10 +54,11 @@ PHP_FUNCTION(pop3_open); PHP_FUNCTION(pop3_get_message_count); +PHP_FUNCTION(pop3_get_account_size); PHP_FUNCTION(pop3_get_message_list); -PHP_FUNCTION (pop3_get_message_header); -PHP_FUNCTION (pop3_get_message); -PHP_FUNCTION (pop3_delete_message); +PHP_FUNCTION(pop3_get_message_header); +PHP_FUNCTION(pop3_get_message); +PHP_FUNCTION(pop3_delete_message); /* Declare any global variables you may need between the BEGIN Index: pecl/pop3/pop3.c diff -u pecl/pop3/pop3.c:1.3 pecl/pop3/pop3.c:1.4 --- pecl/pop3/pop3.c:1.3 Fri Jan 9 13:10:00 2004 +++ pecl/pop3/pop3.c Fri Jan 9 18:05:29 2004 @@ -15,7 +15,7 @@ | Author: | +----------------------------------------------------------------------+ - $Id: pop3.c,v 1.3 2004/01/09 18:10:00 andrey Exp $ + $Id: pop3.c,v 1.4 2004/01/09 23:05:29 iliaa Exp $ */ #ifdef HAVE_CONFIG_H @@ -41,7 +41,7 @@ #define POP3_SAFE_EFREE(ptr) \ if (ptr) { \ - efree (ptr); \ + efree(ptr); \ } #define POP3_SAFE_POINTER(ptr) \ @@ -49,73 +49,118 @@ php_error_docref(NULL TSRMLS_CC, E_ERROR, "POP3: Memory error in %s(%d)", __FILE__, __LINE__); \ } -#define POP3_SAFE_EMALLOC(ptr, size) \ - ptr = emalloc (size); \ - if (!ptr) { \ - php_error_docref(NULL TSRMLS_CC, E_ERROR, "POP3: Unable to allocate %d bytes of memory in %s(%d)", size, __FILE__, __LINE__);\ - } - #define POP3_RESOURCE_NAME "POP3 Library Session" #define POP3_CALL(function_call) \ pop3_return_val = function_call; \ if (pop3_return_val) { \ if (pop3_error(pop3_return_val)) { \ - pop3_disconnect (session->socket_id); \ - efree (session); \ - php_error_docref(NULL TSRMLS_CC, E_WARNING, pop3_return_val); \ - free (pop3_return_val); \ - RETURN_NULL(); \ + pop3_disconnect(*socket_id); \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", pop3_return_val); \ + free(pop3_return_val); \ + RETURN_FALSE; \ } \ } else { \ - efree (session); \ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection failed"); \ - RETURN_NULL(); \ + RETURN_FALSE; \ } #define POP3_CALL_DESTROY(function_call) \ pop3_return_val = function_call; \ if (pop3_return_val) { \ if (pop3_error(pop3_return_val)) { \ - pop3_disconnect (session->socket_id); \ - efree (session); \ - php_error_docref(NULL TSRMLS_CC, E_WARNING, pop3_return_val); \ - free (pop3_return_val); \ - RETURN_NULL(); \ + pop3_disconnect (*socket_id); \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", pop3_return_val); \ + free(pop3_return_val); \ + RETURN_FALSE; \ } else { \ - free (pop3_return_val); \ + free(pop3_return_val); \ } \ } else { \ - efree (session); \ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection failed"); \ - RETURN_NULL(); \ + RETURN_FALSE; \ } #define POP3_FETCH_SESSION(session, session_container) \ - ZEND_FETCH_RESOURCE ((void *) session, PPOP3_SESSION, &session_container, -1, POP3_RESOURCE_NAME, le_pop3_resource); + ZEND_FETCH_RESOURCE (session, int *, &session_container, -1, POP3_RESOURCE_NAME, le_pop3_resource); + +#define POP3_REGISTER_OBJECT(_object, _ptr) \ + { \ + pop3_object *obj; \ + obj = (pop3_object*)zend_object_store_get_object(_object TSRMLS_CC); \ + obj->ptr = _ptr; \ + } + +#define POP3_FROM_OBJECT(socket_id, object) \ + { \ + pop3_object *obj = zend_object_store_get_object(object TSRMLS_CC); \ + socket_id = obj->ptr; \ + if (!socket_id) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "The pop3 connection was not established."); \ + RETURN_FALSE; \ + } \ + } + /* }}} */ -/* {{{ Internal structures */ -/* - * Internal structures +#define PHP_POP3_HEADER 1 +#define PHP_POP3_MESSAGE 2 +#define PHP_POP3_DELETE 3 + +/* True global resources - no need for thread safety here */ + +static int le_pop3_resource; + +/* }}} */ + +static zend_object_handlers pop3_object_handlers; + +/* {{{ pop3_objects_dtor */ +static void pop3_objects_dtor(void *object, zend_object_handle handle TSRMLS_DC) +{ + pop3_object *intern = (pop3_object *) object; + + if (intern->zo.properties) { + zend_hash_destroy(intern->zo.properties); + FREE_HASHTABLE(intern->zo.properties); + } + + pop3_quit(*((int *)intern->ptr)); + efree(intern->ptr); -typedef struct __POP3_SESSION + efree(intern); +} +/* }}} */ + +/* {{{ pop3_objects_new + */ +PHP_POP3_API zend_object_value pop3_objects_new(zend_class_entry *class_type TSRMLS_DC) { - int socket_id; - struct hostent server; - struct sockaddr_in connection; - int portid; -} POP3_SESSION, *PPOP3_SESSION; + zend_object_value retval; + pop3_object *intern; + zval *tmp; -/* True global resources - no need for thread safety here */ + intern = emalloc(sizeof(pop3_object)); + intern->zo.ce = class_type; + intern->zo.in_get = 0; + intern->zo.in_set = 0; + intern->zo.properties = NULL; -static int le_pop3_resource; + ALLOC_HASHTABLE(intern->zo.properties); + zend_hash_init(intern->zo.properties, 0, NULL, ZVAL_PTR_DTOR, 0); + zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); + retval.handle = zend_objects_store_put(intern, pop3_objects_dtor, NULL TSRMLS_CC); + retval.handlers = (zend_object_handlers *) &pop3_object_handlers; + + return retval; +} /* }}} */ + /* {{{ Forward declarations */ /* @@ -131,16 +176,35 @@ * Every user visible function must have an entry in pop3_functions[]. */ function_entry pop3_functions[] = { - PHP_FE(pop3_open, NULL) /* For testing, remove later. */ + PHP_FE(pop3_open, NULL) PHP_FE(pop3_get_message_count, NULL) + PHP_FE(pop3_get_account_size, NULL) PHP_FE(pop3_get_message_list, NULL) - PHP_FE (pop3_get_message_header, NULL) - PHP_FE (pop3_get_message, NULL) - PHP_FE (pop3_delete_message, NULL) - {NULL, NULL, NULL} /* Must be the last line in pop3_functions[] */ + PHP_FE(pop3_get_message_header, NULL) + PHP_FE(pop3_get_message, NULL) + PHP_FE(pop3_delete_message, NULL) + {NULL, NULL, NULL} +}; +/* }}} */ + +#define POP3_ME_MAPPING(name, func_name, arg_types) \ + ZEND_NAMED_FE(name, ZEND_FN(func_name), arg_types) + +/* {{{ pop3_class_functions + */ +function_entry pop3_class_functions[] = { + POP3_ME_MAPPING(pop3, pop3_open, NULL) + POP3_ME_MAPPING(get_message_count, pop3_get_message_count, NULL) + POP3_ME_MAPPING(get_account_size, pop3_get_account_size, NULL) + POP3_ME_MAPPING(get_message_list, pop3_get_message_list, NULL) + POP3_ME_MAPPING(get_message_header, pop3_get_message_header, NULL) + POP3_ME_MAPPING(get_message, pop3_get_message, NULL) + POP3_ME_MAPPING(delete_message, pop3_delete_message, NULL) + {NULL, NULL, NULL} }; /* }}} */ + /* {{{ pop3_module_entry */ zend_module_entry pop3_module_entry = { @@ -190,6 +254,14 @@ */ PHP_MINIT_FUNCTION(pop3) { + zend_class_entry _pop3_class_entry; + INIT_CLASS_ENTRY(_pop3_class_entry, "pop3", pop3_class_functions); + _pop3_class_entry.create_object = pop3_objects_new; + pop3_class_entry = zend_register_internal_class(&_pop3_class_entry TSRMLS_CC); + + /* copy the standard object handlers to you handler table */ + memcpy(&pop3_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + /* If you have INI entries, uncomment these lines ZEND_INIT_MODULE_GLOBALS(pop3, php_pop3_init_globals, NULL); REGISTER_INI_ENTRIES(); @@ -247,13 +319,11 @@ void pop3_resource_destructor (zend_rsrc_list_entry *rsrc TSRMLS_DC) { - PPOP3_SESSION session; - - session = (PPOP3_SESSION) rsrc->ptr; - - pop3_quit(session->socket_id); - - efree(session); + if (rsrc->ptr) { + pop3_quit(*((int *)rsrc->ptr)); + efree(rsrc->ptr); + rsrc->ptr = NULL; + } } /* }}} */ @@ -262,281 +332,241 @@ */ PHP_FUNCTION(pop3_open) { - char *server_name; - int server_name_length; - - char *user_name; - int user_name_length; + char *server_name, *user_name, *password; + int server_name_length, user_name_length, password_length; + long port_number = 110; - char *password; - int password_length; - - int port_number = 110; - - PPOP3_SESSION session; + int *socket_id; struct hostent server; struct sockaddr_in connection; - char *pop3_return_val; + char *pop3_return_val; + zval *object = getThis(); /* Parse parameters */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|l", &server_name, &server_name_length, &user_name, &user_name_length, &password, &password_length, &port_number) == FAILURE) { - return; + RETURN_FALSE; } - /* Create the session holder */ - - POP3_SAFE_EMALLOC (session, sizeof (session[0])); - /* Attempt to initialize the session */ + socket_id = emalloc(sizeof(int)); + *socket_id = pop3_prepare(server_name, 0, &connection, &server); - session->socket_id = pop3_prepare (server_name, 0, &connection, &server); - - if (session->socket_id == -1) { - efree (session); + if (*socket_id == -1) { + efree(socket_id); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to server"); - RETURN_NULL(); + RETURN_FALSE; } /* Attempt to connect */ - - POP3_CALL_DESTROY(pop3_connect (session->socket_id, &connection)) + POP3_CALL_DESTROY(pop3_connect(*socket_id, &connection)) /* Send username */ - - POP3_CALL_DESTROY(pop3_user (session->socket_id, user_name)) + POP3_CALL_DESTROY(pop3_user(*socket_id, user_name)) /* Send password */ + POP3_CALL_DESTROY(pop3_pass(*socket_id, password)) - POP3_CALL_DESTROY(pop3_pass (session->socket_id, password)) - - /* Create and return resource */ - - ZEND_REGISTER_RESOURCE (return_value, session, le_pop3_resource); + /* Create and return resource or object */ + if (object) { + POP3_REGISTER_OBJECT(object, socket_id); + } else { + ZEND_REGISTER_RESOURCE(return_value, socket_id, le_pop3_resource); + } } - /* }}} */ -/* {{{ proto long pop3_get_message_count (resource session) - */ -PHP_FUNCTION(pop3_get_message_count) +static void _pop3_get_stats(INTERNAL_FUNCTION_PARAMETERS, int mode) { - PPOP3_SESSION session; - zval *session_container; - - char *pop3_return_val; + zval *object = getThis(); + zval *session_container; + char *pop3_return_val; + int *socket_id; /* Retrieve session data */ - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &session_container) == FAILURE) { - return; + if (object) { + POP3_FROM_OBJECT(socket_id, object); + } else { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &session_container) == FAILURE) { + RETURN_FALSE; + } + POP3_FETCH_SESSION(socket_id, session_container); + } + + POP3_CALL(pop3_stat(*socket_id)); + + if (!mode) { + /* Stat number of messages */ + RETVAL_LONG(stat2last(pop3_return_val)); + } else { + /* return pop3 box size */ + RETVAL_LONG(stat2bytes(pop3_return_val)); } - POP3_FETCH_SESSION(session, session_container); - - /* Stat number of messages */ - - POP3_CALL (pop3_stat (session->socket_id)); - - /* Return */ - - RETVAL_LONG (stat2last (pop3_return_val)); + free(pop3_return_val); +} - free (pop3_return_val); +/* {{{ proto long pop3_get_message_count (resource session) + */ +PHP_FUNCTION(pop3_get_message_count) +{ + _pop3_get_stats(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); } +/* }}} */ +/* {{{ proto long pop3_get_message_count (resource session) + */ +PHP_FUNCTION(pop3_get_account_size) +{ + _pop3_get_stats(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); +} /* }}} */ /* {{{ proto array pop3_get_message_list (resource session) */ PHP_FUNCTION(pop3_get_message_list) { - PPOP3_SESSION session; - zval *session_container; - - char *pop3_return_val; - - int *size_list; - char **uidl_list; + zval *object = getThis(); + zval *session_container; + char *pop3_return_val; + int *socket_id; + int *size_list; + char **uidl_list; unsigned long message_count, i; - zval *row; - /* Retrieve session data */ - - if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, "r", &session_container) == FAILURE) { - return; + if (object) { + POP3_FROM_OBJECT(socket_id, object); + } else { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &session_container) == FAILURE) { + RETURN_FALSE; + } + POP3_FETCH_SESSION(socket_id, session_container); } - POP3_FETCH_SESSION(session, session_container); - /* Retrieve message size list */ + POP3_CALL(pop3_list(*socket_id, 0)); - POP3_CALL(pop3_list (session->socket_id, 0)); - - size_list = list2array (pop3_return_val); + size_list = list2array(pop3_return_val); - POP3_SAFE_POINTER (size_list); + POP3_SAFE_POINTER(size_list); - free (pop3_return_val); + free(pop3_return_val); /* Retrieve message IDs */ - POP3_CALL(pop3_uidl (session->socket_id, 0)); + POP3_CALL(pop3_uidl(*socket_id, 0)); - uidl_list = uidl2array (pop3_return_val); + uidl_list = uidl2array(pop3_return_val); - POP3_SAFE_POINTER (uidl_list); + POP3_SAFE_POINTER(uidl_list); - free (pop3_return_val); + free(pop3_return_val); /* Create return array */ - - array_init (return_value); + array_init(return_value); /* Cycle through messages */ - message_count = size_list[0]; for (i = 1; i <= message_count; i++) { + zval *row; + /* Create row array */ - MAKE_STD_ZVAL(row); - array_init (row); + array_init(row); - add_assoc_long (row, "message_id", i); - add_assoc_long (row, "size", size_list[i]); - add_assoc_string (row, "uid", uidl_list[i], 1); + add_assoc_long(row, "message_id", i); + add_assoc_long(row, "size", size_list[i]); + add_assoc_string(row, "uid", uidl_list[i], 1); /* Insert row in return value */ - add_index_zval (return_value, i, row); + add_index_zval(return_value, i, row); } /* Clean up */ - - free (size_list); - freeuidlarray (uidl_list); + free(size_list); + freeuidlarray(uidl_list); } - /* }}} */ -/* {{{ proto string pop3_get_message_header (resource session, int message_id) - */ -PHP_FUNCTION (pop3_get_message_header) +static void _pop3_fetch_message_part(INTERNAL_FUNCTION_PARAMETERS, int mode) { - PPOP3_SESSION session; - zval *session_container; - - char *pop3_return_val; - char *message; - - long message_id; + zval *object = getThis(); + zval *session_container; + char *pop3_return_val; + int *socket_id; + char *message; + long message_id; /* Retrieve session data and other parameters */ - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &session_container, &message_id) == FAILURE) { - return; + if (object) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &session_container, &message_id) == FAILURE) { + RETURN_FALSE; + } + POP3_FROM_OBJECT(socket_id, object); + } else { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &session_container, &message_id) == FAILURE) { + RETURN_FALSE; + } + POP3_FETCH_SESSION(socket_id, session_container); } - - POP3_FETCH_SESSION(session, session_container); - - /* Retrieve message headers */ - POP3_CALL (pop3_top (session->socket_id, message_id, 0)); + switch (mode) { + case PHP_POP3_HEADER: + /* Retrieve message headers */ + POP3_CALL(pop3_top(*socket_id, message_id, 0)) + break; + case PHP_POP3_MESSAGE: + /* Retrieve message */ + POP3_CALL(pop3_retr(*socket_id, message_id)) + break; + case PHP_POP3_DELETE: + /* Delete message */ + POP3_CALL(pop3_dele(*socket_id, message_id)) + RETURN_TRUE; + break; + default: /* can't happen */ + RETURN_FALSE; + } - message = retr2msg (pop3_return_val); - free (pop3_return_val); + message = retr2msg(pop3_return_val); + free(pop3_return_val); - POP3_SAFE_POINTER (message); + POP3_SAFE_POINTER(message); - RETVAL_STRING (message, 1); + RETVAL_STRING(message, 1); - free (message); + free(message); } +/* {{{ proto string pop3_get_message_header (resource session, int message_id) + */ +PHP_FUNCTION (pop3_get_message_header) +{ + _pop3_fetch_message_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_POP3_HEADER); +} /* }}} */ /* {{{ proto string pop3_get_message (resource session, int message_id) */ PHP_FUNCTION (pop3_get_message) { - PPOP3_SESSION session; - zval *session_container; - - char *pop3_return_val; - char *message; - - long message_id; - - /* Retrieve session data and other parameters */ - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &session_container, &message_id) == FAILURE) { - return; - } - - POP3_FETCH_SESSION(session, session_container); - - /* Retrieve message */ - - POP3_CALL (pop3_retr (session->socket_id, message_id)); - - message = retr2msg (pop3_return_val); - free (pop3_return_val); - - POP3_SAFE_POINTER (message); - - RETVAL_STRING (message, 1); - - free (message); + _pop3_fetch_message_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_POP3_MESSAGE); } - /* }}} */ /* {{{ proto bool pop3_delete_message (resource session, int message_id) */ PHP_FUNCTION (pop3_delete_message) { - PPOP3_SESSION session; - zval *session_container; - - char *pop3_return_val; - - long message_id; - - /* Retrieve session data and other parameters */ - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &session_container, &message_id) == FAILURE) { - return; - } - - POP3_FETCH_SESSION(session, session_container); - - /* Retrieve message headers */ - - pop3_return_val = pop3_dele (session->socket_id, message_id); - if (pop3_return_val) { - if (pop3_error(pop3_return_val)) { - pop3_disconnect (session->socket_id); - efree (session); - free (pop3_return_val); - RETURN_FALSE; - } - } else { - efree (session); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection failed"); - RETURN_NULL(); - } - - free (pop3_return_val); - - RETURN_TRUE; + _pop3_fetch_message_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_POP3_DELETE); } - /* }}} */ /*
« previous php.pecl.cvs (#724) next »