@LIBRARY=SILC HTTP Library
@FILENAME=silchttplib.html
@LINK=silchttpserver.html:SILC HTTP Server Interface
+@LINK=silchttpphp.html:SILC HTTP PHP Translator
-->
<big><b>SILC HTTP Library</b></big>
<br /><br />
SILC HTTP Library provides currently interface for very simple HTTP
-Server.
+Server. It also provides support to serve PHP pages through the server.
<br /><br />
@LINKS@
noinst_LTLIBRARIES = libsilchttp.la
libsilchttp_la_SOURCES = \
- silchttpserver.c
+ silchttpserver.c \
+ silchttpphp.c
#ifdef SILC_DIST_TOOLKIT
-include_HEADERS = \
- silchttpserver.h
+include_HEADERS = \
+ silchttpserver.h \
+ silchttpphp.h
SILC_EXTRA_DIST = tests
#endif SILC_DIST_TOOLKIT
--- /dev/null
+/*
+
+ silchttpphp.c
+
+ Author: Pekka Riikonen <priikone@silcnet.org>
+
+ Copyright (C) 2006 Pekka Riikonen
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the 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.
+
+*/
+
+#include "silc.h"
+#include "silchttpphp.h"
+
+/* Executes PHP code and returns result */
+
+SilcBuffer silc_http_php(char *php_data)
+{
+ SilcBuffer ret;
+ char *name, tmp[32];
+
+ /* Write the PHP data to temporary file */
+#ifdef SILC_WIN32
+ name = _mktemp("silchttpphpXXXXXX");
+ if (!name)
+ return NULL;
+#else
+ memset(tmp, 0, sizeof(tmp));
+ snprintf(tmp, sizeof(tmp) - 1, "/tmp/silchttpphpXXXXXX");
+ if (mkstemp(tmp) == -1)
+ return NULL;
+ name = tmp;
+#endif /* SILC_WIN32 */
+
+ silc_file_writefile_mode(name, php_data, strlen(php_data), 0600);
+
+ /* Execute PHP */
+ ret = silc_http_php_file(name);
+
+#ifdef SILC_WIN32
+ _unlink(name);
+#else
+ unlink(name);
+#endif /* SILC_WIN32 */
+
+ return ret;
+}
+
+/* Loads PHP file and executes the PHP code and returns the result */
+
+SilcBuffer silc_http_php_file(const char *filename)
+{
+ SilcBuffer ret = NULL;
+ unsigned char tmp[8192];
+ FILE *fd;
+ int len, off = 0;
+
+ SILC_LOG_DEBUG(("Executing PHP"));
+
+ memset(tmp, 0, sizeof(tmp));
+ snprintf(tmp, sizeof(tmp) - 1, "php -f %s", filename);
+
+#ifdef SILC_WIN32
+ fd = _popen(tmp, "r");
+#else
+ fd = popen(tmp, "r");
+#endif /* SILC_WIN32 */
+ if (!fd)
+ return NULL;
+
+ /* Read the result */
+ do {
+ len = fread(tmp, 1, sizeof(tmp), fd);
+ if (len < 0) {
+ silc_buffer_free(ret);
+ pclose(fd);
+ return NULL;
+ }
+
+ if (len) {
+ ret = silc_buffer_alloc(0);
+ if (!ret) {
+ pclose(fd);
+ return NULL;
+ }
+
+ silc_buffer_format(ret,
+ SILC_STR_OFFSET(off),
+ SILC_STR_UI_XNSTRING(tmp, len),
+ SILC_STR_END);
+ off += len;
+ }
+ } while (len);
+
+ if (ret)
+ silc_buffer_strformat(ret, "\0", SILC_STRFMT_END);
+
+ return ret;
+}
--- /dev/null
+/*
+
+ silchttpphp.h
+
+ Author: Pekka Riikonen <priikone@silcnet.org>
+
+ Copyright (C) 2006 Pekka Riikonen
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the 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.
+
+*/
+
+/****h* silchttp/SILC HTTP PHP Translator
+ *
+ * DESCRIPTION
+ *
+ * PHP translator for SILC HTTP Server, enabling PHP support for the pages
+ * served through the SILC HTTP Server interface (silchttpserver.h).
+ * The PHP must be installed in the system and must be in the execution
+ * path for the interface to work.
+ *
+ ***/
+
+#ifndef SILCHTTPPHP_H
+#define SILCHTTPPHP_H
+
+/****f* silchttp/SilcHTTPServer/silc_http_php
+ *
+ * SYNOPSIS
+ *
+ * SilcBuffer silc_http_php(char *php_data);
+ *
+ * DESCRIPTION
+ *
+ * Executes the PHP code contained in the buffer `php_data' and returns
+ * the result in the allocated SilcBuffer or NULL on error. The caller
+ * must free the returned buffer.
+ *
+ ***/
+SilcBuffer silc_http_php(char *php_data);
+
+/****f* silchttp/SilcHTTPServer/silc_http_php
+ *
+ * SYNOPSIS
+ *
+ * SilcBuffer silc_http_php_file(const char *filepath);
+ *
+ * DESCRIPTION
+ *
+ * Reads the PHP contents from the file indicated by the `filepath' and
+ * executes the PHP code and returns the result in the allocated
+ * SilcBuffer or NULL on error. The caller must free the returned buffer.
+ *
+ ***/
+SilcBuffer silc_http_php_file(const char *filename);
+
+#endif /* SILCHTTPPHP_H */
test_silchttpserver_SOURCES = test_silchttpserver.c
LIBS = $(SILC_COMMON_LIBS)
-LDADD = -L.. -L../.. -lsilc
+LDADD = -L.. -L../.. -lsilc -lsilchttp
include $(top_srcdir)/Makefile.defines.in
#include "silc.h"
#include "../silchttpserver.h"
+#include "../silchttpphp.h"
static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn,
const char *uri, const char *method,
if (!strcasecmp(method, "GET")) {
/* Send our default page */
if (!strcmp(uri, "/") || !strcmp(uri, "/index.html")) {
+ SilcBuffer php;
+ const char *php_data = NULL;
+
+ /* Execute PHP data */
+ php = silc_http_php("<small>"
+ "UPDATED <?php echo getcwd(); echo date(\"Y/m/d\", filemtime(\"test_silchttpserver.c\")); ?>"
+ "| VERSION 4.0 | A HANDMADE WEB-SITE | (C) 1995 - 2006 PEKKA RIIKONEN");
+ if (php)
+ php_data = silc_buffer_data(php);
+
memset(&page, 0, sizeof(page));
silc_buffer_strformat(&page,
"<html><head></head><body>",
"<INPUT type=\"radio\" name=\"sex\" value=\"Male\"> Male<BR>"
"<INPUT type=\"radio\" name=\"sex\" value=\"Female\"> Female<BR>"
"<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"
- "</P></FORM>"
+ "</P></FORM>",
+ php_data,
"</body></html>",
SILC_STRFMT_END);
silc_http_server_add_header(httpd, conn, "X-Date",
silc_time_string(silc_time()));
silc_http_server_send(httpd, conn, &page);
silc_buffer_purge(&page);
+ silc_buffer_free(php);
return;
}