xref: /PHP-7.3/main/php_syslog.c (revision 9f0515c4)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2017-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Philip Prindeville <philipp@redfish-solutions.com>           |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include <stdio.h>
20 #include <string.h>
21 #include <assert.h>
22 #include <stdlib.h>
23 #include "php.h"
24 #include "php_syslog.h"
25 
26 #include "zend.h"
27 #include "zend_smart_string.h"
28 
29 /*
30  * The SCO OpenServer 5 Development System (not the UDK)
31  * defines syslog to std_syslog.
32  */
33 
34 #ifdef HAVE_STD_SYSLOG
35 #define syslog std_syslog
36 #endif
37 
38 #ifdef PHP_WIN32
php_syslog(int priority,const char * format,...)39 PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
40 {
41 	va_list args;
42 
43 	/*
44 	 * don't rely on openlog() being called by syslog() if it's
45 	 * not already been done; call it ourselves and pass the
46 	 * correct parameters!
47 	 */
48 	if (!PG(have_called_openlog)) {
49 		php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
50 	}
51 
52 	va_start(args, format);
53 	vsyslog(priority, format, args);
54 	va_end(args);
55 }
56 /* }}} */
57 #else
php_syslog(int priority,const char * format,...)58 PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
59 {
60 	const char *ptr;
61 	unsigned char c;
62 	smart_string fbuf = {0};
63 	smart_string sbuf = {0};
64 	va_list args;
65 
66 	/*
67 	 * don't rely on openlog() being called by syslog() if it's
68 	 * not already been done; call it ourselves and pass the
69 	 * correct parameters!
70 	 */
71 	if (!PG(have_called_openlog)) {
72 		php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
73 	}
74 
75 	va_start(args, format);
76 	zend_printf_to_smart_string(&fbuf, format, args);
77 	smart_string_0(&fbuf);
78 	va_end(args);
79 
80 	if (PG(syslog_filter) == PHP_SYSLOG_FILTER_RAW) {
81 		/* Just send it directly to the syslog */
82 		syslog(priority, "%.*s", (int)fbuf.len, fbuf.c);
83 		smart_string_free(&fbuf);
84 		return;
85 	}
86 
87 	for (ptr = fbuf.c; ; ++ptr) {
88 		c = *ptr;
89 		if (c == '\0') {
90 			syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
91 			break;
92 		}
93 
94 		/* check for NVT ASCII only unless test disabled */
95 		if (((0x20 <= c) && (c <= 0x7e)))
96 			smart_string_appendc(&sbuf, c);
97 		else if ((c >= 0x80) && (PG(syslog_filter) != PHP_SYSLOG_FILTER_ASCII))
98 			smart_string_appendc(&sbuf, c);
99 		else if (c == '\n') {
100 			syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
101 			smart_string_reset(&sbuf);
102 		} else if ((c < 0x20) && (PG(syslog_filter) == PHP_SYSLOG_FILTER_ALL))
103 			smart_string_appendc(&sbuf, c);
104 		else {
105 			const char xdigits[] = "0123456789abcdef";
106 
107 			smart_string_appendl(&sbuf, "\\x", 2);
108 			smart_string_appendc(&sbuf, xdigits[(c / 0x10)]);
109 			c &= 0x0f;
110 			smart_string_appendc(&sbuf, xdigits[c]);
111 		}
112 	}
113 
114 	smart_string_free(&fbuf);
115 	smart_string_free(&sbuf);
116 }
117 /* }}} */
118 #endif
119 
120 /*
121  * Local variables:
122  * tab-width: 4
123  * c-basic-offset: 4
124  * End:
125  * vim600: sw=4 ts=4 fdm=marker
126  * vim<600: sw=4 ts=4
127  */
128