3 /*********************************************************************
5 * $HeadURL: syslog.php $
6 * $LastChangedRevision: 1.1 $
7 * Language: PHP 4.x or higher
8 * Copyright: SysCo systèmes de communication sa
9 * CreationDate: 2005-11-05
11 * $LastChangedDate: 2005-12-24 $
12 * $LastChangedBy: SysCo/al $
13 * WebSite: http://developer.sysco.ch/php/
14 * Email: developer [at] sysco [dot] ch
19 * The Syslog class is a syslog device implementation in PHP
20 * following the RFC 3164 rules.
24 * 1 user-level messages
27 * 4 security/authorization messages
28 * 5 messages generated internally by syslogd
29 * 6 line printer subsystem
30 * 7 network news subsystem
33 * 10 security/authorization messages
39 * 16 local user 0 (local0) (default value)
40 * 17 local user 1 (local1)
41 * 18 local user 2 (local2)
42 * 19 local user 3 (local3)
43 * 20 local user 4 (local4)
44 * 21 local user 5 (local5)
45 * 22 local user 6 (local6)
46 * 23 local user 7 (local7)
49 * 0 Emergency: system is unusable
50 * 1 Alert: action must be taken immediately
51 * 2 Critical: critical conditions
52 * 3 Error: error conditions
53 * 4 Warning: warning conditions
54 * 5 Notice: normal but significant condition (default value)
55 * 6 Informational: informational messages
56 * 7 Debug: debug-level messages
61 * require_once('syslog.php');
62 * $syslog = new Syslog($facility = 16, $severity = 5, $hostname = "", $fqdn= "", $ip_from = "", $process="", $content = "");
63 * $syslog->Send($server = "", $content = "", $timeout = 0);
70 * require_once('syslog.php');
71 * $syslog = new Syslog();
72 * $syslog->Send('192.168.0.12', 'My first PHP syslog message');
77 * require_once('syslog.php');
78 * $syslog = new Syslog(23, 7, 'MYSERVER', 'myserver.mydomain.net', '192.168.0.1', 'webautomation');
79 * $syslog->Send('192.168.0.12', 'My second PHP syslog message');
84 * require_once('syslog.php');
85 * $syslog = new Syslog();
86 * $syslog->SetFacility(23);
87 * $syslog->SetSeverity(7);
88 * $syslog->SetHostname('MYSERVER');
89 * $syslog->SetFqdn('myserver.mydomain.net');
90 * $syslog->SetIpFrom('192.168.0.1');
91 * $syslog->SetProcess('webautomation');
92 * $syslog->SetContent('My third PHP syslog message');
93 * $syslog->SetServer('192.168.0.12');
99 * // Do not follow the conventions of the RFC
100 * // and send a customized MSG part instead of
101 * // the recommanded format "process fqdn ip content"
102 * require_once('syslog.php');
103 * $syslog = new Syslog();
104 * $syslog->SetFacility(23);
105 * $syslog->SetSeverity(7);
106 * $syslog->SetHostname('MYSERVER');
107 * $syslog->SetMsg('My customized MSG PHP syslog message');
108 * $syslog->SetServer('192.168.0.12');
113 * External file needed
118 * External file created
125 * - Sockets support must be enabled.
126 * * In Linux and *nix environments, the extension is enabled at
127 * compile time using the --enable-sockets configure option
128 * * In Windows, PHP Sockets can be activated by un-commenting
129 * extension=php_sockets.dll in php.ini
134 * Copyright (c) 2005, SysCo systèmes de communication sa
135 * SysCo (tm) is a trademark of SysCo systèmes de communication sa
136 * All rights reserved.
138 * Redistribution and use in source and binary forms, with or without
139 * modification, are permitted provided that the following conditions are met:
141 * - Redistributions of source code must retain the above copyright notice,
142 * this list of conditions and the following disclaimer.
143 * - Redistributions in binary form must reproduce the above copyright notice,
144 * this list of conditions and the following disclaimer in the documentation
145 * and/or other materials provided with the distribution.
146 * - Neither the name of SysCo systèmes de communication sa nor the names of its
147 * contributors may be used to endorse or promote products derived from this
148 * software without specific prior written permission.
150 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
151 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
152 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
153 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
154 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
155 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
156 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
157 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
158 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
163 * 2005-12-24 1.1 SysCo/al Generic release and documentation
164 * 2005-11-05 1.0 SysCo/al Initial release
166 *********************************************************************/
170 var $_facility; // 0-23
171 var $_severity; // 0-7
172 var $_hostname; // no embedded space, no domain name, only a-z A-Z 0-9 and other authorized characters
178 var $_server; // Syslog destination server
179 var $_port; // Standard syslog port is 514
180 var $_timeout; // Timeout of the UDP connection (in seconds)
182 function Syslog($facility = 16, $severity = 5, $hostname = "", $fqdn= "", $ip_from = "", $process="", $content = "")
185 $this->_server = '127.0.0.1';
187 $this->_timeout = 10;
189 $this->_facility = $facility;
191 $this->_severity = $severity;
193 $this->_hostname = $hostname;
194 if ($this->_hostname == "")
196 if (isset($_ENV["COMPUTERNAME"]))
198 $this->_hostname = $_ENV["COMPUTERNAME"];
200 elseif (isset($_ENV["HOSTNAME"]))
202 $this->_hostname = $_ENV["HOSTNAME"];
206 $this->_hostname = "WEBSERVER";
209 $this->_hostname = substr($this->_hostname, 0, strpos($this->_hostname.".", "."));
211 $this->_fqdn = $fqdn;
212 if ($this->_fqdn == "")
214 if (isset($_SERVER["SERVER_NAME"]))
216 $this->_fqdn = $_SERVER["SERVER_NAME"];
220 $this->_ip_from = $ip_from;
221 if ($this->_ip_from == "")
223 if (isset($_SERVER["SERVER_ADDR"]))
225 $this->_ip_from = $_SERVER["SERVER_ADDR"];
229 $this->_process = $process;
230 if ($this->_process == "")
232 $this->_process = "PHP";
235 $this->_content = $content;
236 if ($this->_content == "")
238 $this->_content = "PHP generated message";
243 function SetFacility($facility)
245 $this->_facility = $facility;
249 function SetSeverity($severity)
251 $this->_severity = $severity;
255 function SetHostname($hostname)
257 $this->_hostname = $hostname;
261 function SetFqdn($fqdn)
263 $this->_fqdn = $fqdn;
267 function SetIpFrom($ip_from)
269 $this->_ip_from = $ip_from;
273 function SetProcess($process)
275 $this->_process = $process;
279 function SetContent($content)
281 $this->_content = $content;
285 function SetMsg($msg)
291 function SetServer($server)
293 $this->_server = $server;
297 function SetPort($port)
299 if ((intval($port) > 0) && (intval($port) < 65536))
301 $this->_port = intval($port);
306 function SetTimeout($timeout)
308 if (intval($timeout) > 0)
310 $this->_timeout = intval($timeout);
315 function Send($server = "", $content = "", $timeout = 0)
319 $this->_server = $server;
324 $this->_content = $content;
327 if (intval($timeout) > 0)
329 $this->_timeout = intval($timeout);
332 if ($this->_facility < 0) { $this->_facility = 0; }
333 if ($this->_facility > 23) { $this->_facility = 23; }
334 if ($this->_severity < 0) { $this->_severity = 0; }
335 if ($this->_severity > 7) { $this->_severity = 7; }
337 $this->_process = substr($this->_process, 0, 32);
339 $actualtime = time();
340 $month = date("M", $actualtime);
341 $day = substr(" ".date("j", $actualtime), -2);
342 $hhmmss = date("H:i:s", $actualtime);
343 $timestamp = $month." ".$day." ".$hhmmss;
345 $pri = "<".($this->_facility*8 + $this->_severity).">";
346 $header = $timestamp." ".$this->_hostname;
348 if ($this->_msg != "")
354 $msg = $this->_process.": ".$this->_fqdn." ".$this->_ip_from." ".$this->_content;
357 $message = substr($pri.$header." ".$msg, 0, 1024);
359 $fp = fsockopen("udp://".$this->_server, $this->_port, $errno, $errstr);
362 fwrite($fp, $message);
368 $result = "ERROR: $errno - $errstr";