1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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 <stdlib.h>
22 #include "php.h"
23 #include "php_syslog.h"
24
25 #include "zend.h"
26 #include "zend_smart_string.h"
27
28 /*
29 * The SCO OpenServer 5 Development System (not the UDK)
30 * defines syslog to std_syslog.
31 */
32
33 #ifdef HAVE_STD_SYSLOG
34 #define syslog std_syslog
35 #endif
36
37 #ifdef PHP_WIN32
php_syslog(int priority,const char * format,...)38 PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
39 {
40 va_list args;
41
42 /*
43 * don't rely on openlog() being called by syslog() if it's
44 * not already been done; call it ourselves and pass the
45 * correct parameters!
46 */
47 if (!PG(have_called_openlog)) {
48 php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
49 }
50
51 va_start(args, format);
52 vsyslog(priority, format, args);
53 va_end(args);
54 }
55 /* }}} */
56 #else
php_syslog(int priority,const char * format,...)57 PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
58 {
59 const char *ptr;
60 unsigned char c;
61 smart_string fbuf = {0};
62 smart_string sbuf = {0};
63 va_list args;
64
65 /*
66 * don't rely on openlog() being called by syslog() if it's
67 * not already been done; call it ourselves and pass the
68 * correct parameters!
69 */
70 if (!PG(have_called_openlog)) {
71 php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
72 }
73
74 va_start(args, format);
75 zend_printf_to_smart_string(&fbuf, format, args);
76 smart_string_0(&fbuf);
77 va_end(args);
78
79 if (PG(syslog_filter) == PHP_SYSLOG_FILTER_RAW) {
80 /* Just send it directly to the syslog */
81 syslog(priority, "%.*s", (int)fbuf.len, fbuf.c);
82 smart_string_free(&fbuf);
83 return;
84 }
85
86 for (ptr = fbuf.c; ; ++ptr) {
87 c = *ptr;
88 if (c == '\0') {
89 syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
90 break;
91 }
92
93 /* check for NVT ASCII only unless test disabled */
94 if (((0x20 <= c) && (c <= 0x7e)))
95 smart_string_appendc(&sbuf, c);
96 else if ((c >= 0x80) && (PG(syslog_filter) != PHP_SYSLOG_FILTER_ASCII))
97 smart_string_appendc(&sbuf, c);
98 else if (c == '\n') {
99 syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
100 smart_string_reset(&sbuf);
101 } else if ((c < 0x20) && (PG(syslog_filter) == PHP_SYSLOG_FILTER_ALL))
102 smart_string_appendc(&sbuf, c);
103 else {
104 const char xdigits[] = "0123456789abcdef";
105
106 smart_string_appendl(&sbuf, "\\x", 2);
107 smart_string_appendc(&sbuf, xdigits[(c / 0x10)]);
108 c &= 0x0f;
109 smart_string_appendc(&sbuf, xdigits[c]);
110 }
111 }
112
113 smart_string_free(&fbuf);
114 smart_string_free(&sbuf);
115 }
116 /* }}} */
117 #endif
118