File: database_functions.php

package info (click to toggle)
squirrelmail-logger 2.2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 180 kB
  • ctags: 207
  • sloc: php: 811; sh: 136; makefile: 40
file content (122 lines) | stat: -rw-r--r-- 2,793 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

/**
  * SquirrelMail Squirrel Logger Plugin
  * Copyright (c) 2001-2008 Ron Chinn, Pat Winn, Paul Lesniewski
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * @package plugins
  * @subpackage squirrel_logger
  *
  */



//if (!include_once('DB.php'))
if (!@include_once('DB.php'))
{
   echo '<strong><font color="red">DB.php could not be loaded. Please make sure you have Pear installed.  For more details see <a href="http://pear.php.net" target="_blank">http://pear.php.net</a></font></strong>';
   exit;
}



/**
  * Log a message to a database
  *
  * @param string $event The event text
  * @param string $user The user that generated the event
  * @param string $dom The user's domain 
  * @param string $user_address The remote IP and/or host address
  * @param int $timestamp The date/time of the event
  * @param string $message The message to be logged
  *
  */
function sl_log_to_db($event, $user, $dom, $user_address, $timestamp, $message)
{

   global $sl_insert_event_query, $sl_fail_silently;
   sl_get_config();


   $db = sl_get_database_connection();
   if ($sl_fail_silently && $db === FALSE) return;


   $sql = str_replace(array('%1', '%2', '%3', '%4', '%5', '%6'), 
                      array($event, $user, $dom, $user_address, 
                            sldate('Y-m-d H:i:s', $timestamp), 
                            $message), 
                      $sl_insert_event_query);

   
   // send query to database
   //
   $result = $db->query($sql);


   // check for database errors
   //
   if (!$sl_fail_silently && DB::isError($result))
   {

      $msg = $result->getMessage();

      sl_error('ERROR: cannot write to logging database - ' . $msg . ' - ' . $sql);
//      sl_error('ERROR: cannot write to logging database - ' . $msg);
      exit;

   }

}



/**
  * Get a database connection
  *
  * If a connection has already been opened, return that,
  * otherwise, open a new connection.
  *
  * @return object The database connection handle, or FALSE
  *                if a database connection could not be made
  *                (when in silent mode per $sl_fail_silently).
  *
  */
function sl_get_database_connection()
{

   global $sl_db_connection, $sl_dsn, $sl_fail_silently;
   sl_get_config();


   // make a new connection if needed; exit if failure
   //
   if (empty($sl_db_connection))
   {

      $sl_db_connection = DB::connect($sl_dsn);
      if (!$sl_fail_silently && DB::isError($sl_db_connection))
      {

         sl_error('Could not make database connection.');
         exit;

      }

      if (!DB::isError($sl_db_connection))
         $sl_db_connection->setFetchMode(DB_FETCHMODE_ORDERED);
      else
         return FALSE;

   }


   // return connection
   //
   return $sl_db_connection;

}