foist
[fingerprinter.git] / include / syslog.php
1 <?php
2
3 /*********************************************************************
4  *
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
10  *            CreatedBy: SysCo/al
11  *     $LastChangedDate: 2005-12-24 $
12  *       $LastChangedBy: SysCo/al $
13  *              WebSite: http://developer.sysco.ch/php/
14  *                Email: developer [at] sysco [dot] ch
15  *
16  *
17  * Description
18  *
19  *   The Syslog class is a syslog device implementation in PHP
20  *   following the RFC 3164 rules.
21  *
22  *   Facility values:
23  *      0 kernel messages
24  *      1 user-level messages
25  *      2 mail system
26  *      3 system daemons
27  *      4 security/authorization messages
28  *      5 messages generated internally by syslogd
29  *      6 line printer subsystem
30  *      7 network news subsystem
31  *      8 UUCP subsystem
32  *      9 clock daemon
33  *     10 security/authorization messages
34  *     11 FTP daemon
35  *     12 NTP subsystem
36  *     13 log audit
37  *     14 log alert
38  *     15 clock daemon
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)
47  *
48  *   Severity values:
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
57  *
58  *
59  * Usage
60  *
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);
64  *
65  *
66  * Examples
67  *
68  *   Example 1
69  *     <?php
70  *         require_once('syslog.php');
71  *         $syslog = new Syslog();
72  *         $syslog->Send('192.168.0.12', 'My first PHP syslog message');
73  *     ?>
74  *
75  *   Example 2
76  *     <?php
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');
80  *     ?>
81  *
82  *   Example 3
83  *     <?php
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');
94  *         $syslog->Send();
95  *     ?>
96  *
97  *   Example 4
98  *     <?php
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');
109  *         $syslog->Send();
110  *     ?>
111  *
112  *
113  * External file needed
114  *
115  *   none.
116  *
117  *
118  * External file created
119  *
120  *   none.
121  *
122  *
123  * Special issues
124  *
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
130  *
131  *
132  * Licence
133  *
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.
137  *
138  *   Redistribution and use in source and binary forms, with or without 
139  *   modification, are permitted provided that the following conditions are met:
140  *
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. 
149  *
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.
159  *
160  *
161  * Change Log
162  *
163  *   2005-12-24 1.1 SysCo/al Generic release and documentation
164  *   2005-11-05 1.0 SysCo/al Initial release
165  *
166  *********************************************************************/
167  
168     class Syslog
169     {
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
173         var $_fqdn;
174         var $_ip_from;
175         var $_process;
176         var $_content;
177         var $_msg;
178         var $_server;   // Syslog destination server
179         var $_port;     // Standard syslog port is 514
180         var $_timeout;  // Timeout of the UDP connection (in seconds)
181         
182         function Syslog($facility = 16, $severity = 5, $hostname = "", $fqdn= "", $ip_from = "", $process="", $content = "")
183         {
184             $this->_msg      = '';
185             $this->_server   = '127.0.0.1';
186             $this->_port     = 514;
187             $this->_timeout  = 10;
188             
189             $this->_facility = $facility;
190             
191             $this->_severity = $severity;
192             
193             $this->_hostname = $hostname;
194             if ($this->_hostname == "")
195             {
196                 if (isset($_ENV["COMPUTERNAME"]))
197                 {
198                     $this->_hostname = $_ENV["COMPUTERNAME"];
199                 }
200                 elseif (isset($_ENV["HOSTNAME"]))
201                 {
202                     $this->_hostname = $_ENV["HOSTNAME"];
203                 }
204                 else
205                 {
206                     $this->_hostname = "WEBSERVER";
207                 }
208             }
209             $this->_hostname = substr($this->_hostname, 0, strpos($this->_hostname.".", "."));
210             
211             $this->_fqdn = $fqdn;
212             if ($this->_fqdn == "")
213             {
214                 if (isset($_SERVER["SERVER_NAME"]))
215                 {
216                     $this->_fqdn = $_SERVER["SERVER_NAME"];
217                 }
218             }
219
220             $this->_ip_from = $ip_from;
221             if ($this->_ip_from == "")
222             {
223                 if (isset($_SERVER["SERVER_ADDR"]))
224                 {
225                     $this->_ip_from = $_SERVER["SERVER_ADDR"];
226                 }
227             }
228
229             $this->_process = $process;
230             if ($this->_process == "")
231             {
232                 $this->_process = "PHP";
233             }
234
235             $this->_content = $content;
236             if ($this->_content == "")
237             {
238                 $this->_content = "PHP generated message";
239             }
240             
241         }
242
243         function SetFacility($facility)
244         {
245             $this->_facility = $facility;
246         }
247         
248         
249         function SetSeverity($severity)
250         {
251             $this->_severity = $severity;
252         }
253         
254         
255         function SetHostname($hostname)
256         {
257             $this->_hostname = $hostname;
258         }
259         
260         
261         function SetFqdn($fqdn)
262         {
263             $this->_fqdn = $fqdn;
264         }
265         
266         
267         function SetIpFrom($ip_from)
268         {
269             $this->_ip_from = $ip_from;
270         }
271         
272         
273         function SetProcess($process)
274         {
275             $this->_process = $process;
276         }
277         
278         
279         function SetContent($content)
280         {
281             $this->_content = $content;
282         }
283         
284         
285         function SetMsg($msg)
286         {
287             $this->_msg = $msg;
288         }
289         
290         
291         function SetServer($server)
292         {
293             $this->_server = $server;
294         }
295         
296         
297         function SetPort($port)
298         {
299             if ((intval($port) > 0) && (intval($port) < 65536))
300             {
301                 $this->_port = intval($port);
302             }
303         }
304
305
306         function SetTimeout($timeout)
307         {
308             if (intval($timeout) > 0)
309             {
310                 $this->_timeout = intval($timeout);
311             }
312         }
313         
314         
315         function Send($server = "", $content = "", $timeout = 0)
316         {
317             if ($server != "")
318             {
319                 $this->_server = $server;
320             }
321
322             if ($content != "")
323             {
324                 $this->_content = $content;
325             }
326             
327             if (intval($timeout) > 0)
328             {
329                 $this->_timeout = intval($timeout);
330             }
331             
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; }
336             
337             $this->_process = substr($this->_process, 0, 32);
338             
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;
344             
345             $pri    = "<".($this->_facility*8 + $this->_severity).">";
346             $header = $timestamp." ".$this->_hostname;
347             
348             if ($this->_msg != "")
349             {
350                 $msg = $this->_msg;
351             }
352             else
353             {
354                 $msg = $this->_process.": ".$this->_fqdn." ".$this->_ip_from." ".$this->_content;
355             }
356             
357             $message = substr($pri.$header." ".$msg, 0, 1024);
358             
359             $fp = fsockopen("udp://".$this->_server, $this->_port, $errno, $errstr);
360             if ($fp)
361             {
362                 fwrite($fp, $message);
363                 fclose($fp);
364                 $result = $message;
365             }
366             else
367             {
368                 $result = "ERROR: $errno - $errstr";
369             }
370             return $result;
371         }
372     }
373
374 ?>