cvs: pecl /pop3 .cvsignore CREDITS README config.m4 php_pop3.h pop3.c pop3.php /pop3/tests 001.phpt

From: Date: Fri, 09 Jan 2004 17:29:59 +0000
Subject: cvs: pecl /pop3 .cvsignore CREDITS README config.m4 php_pop3.h pop3.c pop3.php /pop3/tests 001.phpt
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-714@lists.php.net to get a copy of this message
wez Fri Jan 9 12:29:59 2004 EDT Added files: /pecl/pop3 .cvsignore CREDITS README config.m4 php_pop3.h pop3.c pop3.php /pecl/pop3/tests 001.phpt Log: import Marco's pop3 extension

Index: pecl/pop3/.cvsignore +++ pecl/pop3/.cvsignore deps *.lo *.la Index: pecl/pop3/config.m4 +++ pecl/pop3/config.m4 dnl $Id: config.m4,v 1.1 2004/01/09 17:29:57 wez Exp $ dnl config.m4 for extension pop3 PHP_ARG_WITH(pop3, for pop3 support, [ --with-pop3 Include pop3 support]) if test "$PHP_POP3" != "no"; then dnl Write more examples of tests here... dnl # --with-pop3 -> check with-path dnl SEARCH_PATH="/usr/local /usr" # you might want to change this dnl SEARCH_FOR="/include/pop3.h" # you most likely want to change this dnl if test -r $PHP_POP3/; then # path given as parameter dnl POP3_DIR=$PHP_POP3 dnl else # search default path list dnl AC_MSG_CHECKING([for pop3 files in default path]) dnl for i in $SEARCH_PATH ; do dnl if test -r $i/$SEARCH_FOR; then dnl POP3_DIR=$i dnl AC_MSG_RESULT(found in $i) dnl fi dnl done dnl fi dnl dnl if test -z "$POP3_DIR"; then dnl AC_MSG_RESULT([not found]) dnl AC_MSG_ERROR([Please reinstall the pop3 distribution]) dnl fi dnl --with-pop3 -> add include path PHP_ADD_INCLUDE($POP3_DIR/include) dnl --with-pop3 -> check for lib and symbol presence LIBNAME=spopc LIBSYMBOL=popdelmsg PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL, [ PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $POP3_DIR/lib, POP3_SHARED_LIBADD) AC_DEFINE(HAVE_POP3LIB,1,[ ]) ],[ AC_MSG_ERROR([wrong pop3 lib version or lib not found]) ],[ -L$POP3_DIR/lib -lm -ldl ]) PHP_SUBST(POP3_SHARED_LIBADD) PHP_NEW_EXTENSION(pop3, pop3.c, $ext_shared) fi Index: pecl/pop3/php_pop3.h +++ pecl/pop3/php_pop3.h /* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3 of the PHP license, | | 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/3.txt. | | 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. | +----------------------------------------------------------------------+ | Author: | +----------------------------------------------------------------------+ $Id: php_pop3.h,v 1.1 2004/01/09 17:29:57 wez Exp $ */ #ifndef PHP_POP3_H #define PHP_POP3_H extern zend_module_entry pop3_module_entry; #define phpext_pop3_ptr &pop3_module_entry #ifdef PHP_WIN32 #define PHP_POP3_API __declspec(dllexport) #else #define PHP_POP3_API #endif #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(pop3); PHP_MSHUTDOWN_FUNCTION(pop3); PHP_RINIT_FUNCTION(pop3); PHP_RSHUTDOWN_FUNCTION(pop3); PHP_MINFO_FUNCTION(pop3); PHP_FUNCTION(pop3_open); PHP_FUNCTION(pop3_get_message_count); PHP_FUNCTION(pop3_get_message_list); 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 and END macros here: ZEND_BEGIN_MODULE_GLOBALS(pop3) ZEND_END_MODULE_GLOBALS(pop3) */ /* In every utility function you add that needs to use variables in php_pop3_globals, call TSRM_FETCH(); after declaring other variables used by that function, or better yet, pass in TSRMLS_CC after the last function argument and declare your utility function with TSRMLS_DC after the last declared argument. Always refer to the globals in your function as POP3_G(variable). You are encouraged to rename these macros something shorter, see examples in any other php module directory. */ #ifdef ZTS #define POP3_G(v) TSRMG(pop3_globals_id, zend_pop3_globals *, v) #else #define POP3_G(v) (pop3_globals.v) #endif #endif /* PHP_POP3_H */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * indent-tabs-mode: t * End: */ Index: pecl/pop3/pop3.c +++ pecl/pop3/pop3.c /* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3 of the PHP license, | | 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/3.txt. | | 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. | +----------------------------------------------------------------------+ | Author: | +----------------------------------------------------------------------+ $Id: pop3.c,v 1.1 2004/01/09 17:29:57 wez Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_pop3.h" #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> #include <libspopc.h> /* {{{ Internal macros */ /* * Internal macros */ #define POP3_SAFE_EFREE(ptr) \ if (ptr) \ efree (ptr); #define POP3_SAFE_POINTER(ptr) \ if (!ptr) \ zend_error (E_ERROR, "POP3: Memory error in %s(%d)", __FILE__, __LINE__); #define POP3_SAFE_EMALLOC(ptr, size) \ ptr = emalloc (size); \ if (!ptr) \ zend_error (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); \ zend_error (E_WARNING, pop3_return_val); \ free (pop3_return_val); \ RETURN_NULL(); \ } \ } \ else \ { \ efree (session); \ zend_error (E_WARNING, "Connection failed"); \ RETURN_NULL(); \ } #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); \ zend_error (E_WARNING, pop3_return_val); \ free (pop3_return_val); \ RETURN_NULL(); \ } \ else \ free (pop3_return_val); \ } \ else \ { \ efree (session); \ zend_error (E_WARNING, "Connection failed"); \ RETURN_NULL(); \ } #define POP3_FETCH_SESSION(session, session_container) \ ZEND_FETCH_RESOURCE ((void *) session, PPOP3_SESSION, &session_container, -1, POP3_RESOURCE_NAME, le_pop3_resource); /* }}} */ /* {{{ Internal structures */ /* * Internal structures */ typedef struct __POP3_SESSION { int socket_id; struct hostent server; struct sockaddr_in connection; int portid; } POP3_SESSION, *PPOP3_SESSION; /* True global resources - no need for thread safety here */ static int le_pop3_resource; /* }}} */ /* {{{ Forward declarations */ /* * Forward declarations */ void pop3_resource_destructor (zend_rsrc_list_entry *rsrc TSRMLS_DC); /* }}} */ /* {{{ pop3_functions[] * * 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_get_message_count, 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[] */ }; /* }}} */ /* {{{ pop3_module_entry */ zend_module_entry pop3_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "pop3", pop3_functions, PHP_MINIT(pop3), PHP_MSHUTDOWN(pop3), PHP_RINIT(pop3), /* Replace with NULL if there's nothing to do at request start */ PHP_RSHUTDOWN(pop3), /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(pop3), #if ZEND_MODULE_API_NO >= 20010901 "0.1", /* Replace with version number for your extension */ #endif STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_POP3 ZEND_GET_MODULE(pop3) #endif /* {{{ PHP_INI */ /* Remove comments and fill if you need to have entries in php.ini PHP_INI_BEGIN() STD_PHP_INI_ENTRY("pop3.global_value", "42", PHP_INI_ALL, OnUpdateInt, global_value, zend_pop3_globals, pop3_globals) STD_PHP_INI_ENTRY("pop3.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_pop3_globals, pop3_globals) PHP_INI_END() */ /* }}} */ /* {{{ php_pop3_init_globals */ /* Uncomment this function if you have INI entries static void php_pop3_init_globals(zend_pop3_globals *pop3_globals) { pop3_globals->global_value = 0; pop3_globals->global_string = NULL; } */ /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(pop3) { /* If you have INI entries, uncomment these lines ZEND_INIT_MODULE_GLOBALS(pop3, php_pop3_init_globals, NULL); REGISTER_INI_ENTRIES(); */ le_pop3_resource = zend_register_list_destructors_ex (pop3_resource_destructor, NULL, POP3_RESOURCE_NAME, module_number); return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(pop3) { /* uncomment this line if you have INI entries UNREGISTER_INI_ENTRIES(); */ return SUCCESS; } /* }}} */ /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION(pop3) { return SUCCESS; } /* }}} */ /* {{{ PHP_RSHUTDOWN_FUNCTION */ PHP_RSHUTDOWN_FUNCTION(pop3) { return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(pop3) { php_info_print_table_start(); php_info_print_table_header(2, "pop3 support", "enabled"); php_info_print_table_end(); /* Remove comments if you have entries in php.ini DISPLAY_INI_ENTRIES(); */ } /* }}} */ /* {{{ pop3_resource_destructor */ 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); } /* }}} */ /* {{{ proto resource pop3_open(string server_name, string user_name, string password, [int port_number]) */ PHP_FUNCTION(pop3_open) { char *server_name; int server_name_length; char *user_name; int user_name_length; char *password; int password_length; int port_number; PPOP3_SESSION session; struct hostent server; struct sockaddr_in connection; char *pop3_return_val; // Parse parameters switch (ZEND_NUM_ARGS()) { case 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &server_name, &server_name_length, &user_name, &user_name_length, &password, &password_length) == FAILURE) { return; } port_number = 110; break; case 4 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sssl", &server_name, &server_name_length, &user_name, &user_name_length, &password, &password_length, &port_number) == FAILURE) { return; } break; default : return; } // Create the session holder POP3_SAFE_EMALLOC (session, sizeof (session[0])); // Attempt to initialize the session session->socket_id = pop3_prepare (server_name, 0, &connection, &server); if (session->socket_id == -1) { efree (session); zend_error (E_WARNING, "Unable to connect to server"); RETURN_NULL(); } // Attempt to connect POP3_CALL_DESTROY(pop3_connect (session->socket_id, &connection)) // Send username POP3_CALL_DESTROY(pop3_user (session->socket_id, user_name)) // Send password POP3_CALL_DESTROY(pop3_pass (session->socket_id, password)) // Create and return resource ZEND_REGISTER_RESOURCE (return_value, session, le_pop3_resource); } /* }}} */ /* {{{ proto long pop3_get_message_count (resource session) */ PHP_FUNCTION(pop3_get_message_count) { PPOP3_SESSION session; zval *session_container; char *pop3_return_val; // Retrieve session data if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &session_container) == FAILURE) { return; } 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); } /* }}} */ /* {{{ 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; unsigned long message_count, i; zval *row; // Retrieve session data if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, "r", &session_container) == FAILURE) { return; } POP3_FETCH_SESSION(session, session_container); // Retrieve message size list POP3_CALL(pop3_list (session->socket_id, 0)); size_list = list2array (pop3_return_val); POP3_SAFE_POINTER (size_list); free (pop3_return_val); // Retrieve message IDs POP3_CALL(pop3_uidl (session->socket_id, 0)); uidl_list = uidl2array (pop3_return_val); POP3_SAFE_POINTER (uidl_list); free (pop3_return_val); // Create return array array_init (return_value); // Cycle through messages message_count = size_list[0]; for (i = 1; i <= message_count; i++) { // Create row array MAKE_STD_ZVAL(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); // Insert row in return value add_index_zval (return_value, i, row); } // Clean up free (size_list); freeuidlarray (uidl_list); } /* }}} */ /* {{{ proto string pop3_get_message_header (resource session, int message_id) */ PHP_FUNCTION (pop3_get_message_header) { 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 headers POP3_CALL (pop3_top (session->socket_id, message_id, 0)); message = retr2msg (pop3_return_val); free (pop3_return_val); POP3_SAFE_POINTER (message); RETVAL_STRING (message, 1); free (message); } /* }}} */ /* {{{ 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); } /* }}} */ /* {{{ 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); zend_error (E_WARNING, "Connection failed"); RETURN_NULL(); } free (pop3_return_val); RETURN_TRUE; } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ Index: pecl/pop3/pop3.php +++ pecl/pop3/pop3.php <?php if (!extension_loaded("pop3")) { dl("pop3.so"); } // Establish a connection $username = "me"; $password = "me"; $server = "myserver"; $port = 110; $session = pop3_open ($server, $username, $password, $port); // $port is optional if (is_null ($session)) { die ("CONNECTION ERROR"); } // Retrieve the number of messages available $message_count = pop3_get_message_count ($session); echo "Message count: $message_count\n"; // Retrieve a list of available messages // The list is returned as an array of arrays // with the following entries: // // message_id -> the numeric id of the message (also the key of the array) // size -> the size, in bytes, of the message // uid -> the unique ID of the message $messages = pop3_get_message_list ($session); echo "Message list follows:\n"; var_dump ($messages); // Retrieve a particular message $message_id = 1; // Must be a numeric id between 1 and pop3_get_message_count() // Get only the headers $message_headers = pop3_get_message_header ($session, $message_id); echo "Message headers for message #$message_id:\n$message_headers"; // Get the whole thing $message = pop3_get_message ($session, $message_id); echo "Message #$message_id:\n$message"; // Delete message if (pop3_delete_message ($session, $message_id) === true) { echo "Deleted message #$message_id successfully\n\n"; } else { echo "Error deleting message #$message_id"; } ?> Index: pecl/pop3/tests/001.phpt +++ pecl/pop3/tests/001.phpt --TEST-- Check for pop3 presence --SKIPIF-- <?php if (!extension_loaded("pop3")) print "skip"; ?> --POST-- --GET-- --INI-- --FILE-- <?php echo "pop3 extension is available"; /* you can add regression tests for your extension here the output of your test code has to be equal to the text in the --EXPECT-- section below for the tests to pass, differences between the output and the expected text are interpreted as failure see php4/README.TESTING for further information on writing regression tests */ ?> --EXPECT-- pop3 extension is available
« previous php.pecl.cvs (#714) next »