database.php
author Dan
Tue, 20 Jan 2009 22:08:17 -0500
changeset 52 a8f0e99883d1
parent 51 508400fc5282
permissions -rw-r--r--
Web interface can now update with AJAX
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
     1
<?php
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
     2
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
     3
$mysql_conn = false;
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
     4
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
     5
function mysql_reconnect()
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
     6
{
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
     7
  global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass, $mysql_dbname;
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
     8
  if ( $mysql_conn )
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
     9
  {
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    10
    @mysql_close($mysql_conn);
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    11
    if ( defined('LIBIRC_DEBUG') )
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    12
    {
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    13
      echo "< > Reconnecting to MySQL\n";
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    14
    }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    15
  }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    16
  // connect to MySQL
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    17
  $mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    18
  if ( !$mysql_conn )
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    19
  {
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    20
    $m_e = mysql_error();
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    21
    echo "Error connecting to MySQL: $m_e\n";
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    22
    exit(1);
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    23
  }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    24
  $q = @mysql_query("USE `$mysql_dbname`;", $mysql_conn);
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    25
  if ( !$q )
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    26
  {
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    27
    $m_e = mysql_error();
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    28
    echo "Error selecting database: $m_e\n";
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    29
    exit(1);
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    30
  }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    31
}
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    32
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    33
function eb_mysql_query($sql, $conn = false)
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    34
{
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    35
  global $mysql_conn, $irc;
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    36
  $m_et = false;
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    37
  while ( true )
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    38
  {
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    39
    $q = mysql_query($sql, $mysql_conn);
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    40
    if ( !$q )
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    41
    {
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    42
      $m_e = mysql_error();
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    43
      if ( strpos($m_e, 'gone away') && !$m_et )
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    44
      {
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    45
        mysql_reconnect();
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    46
        continue;
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    47
      }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    48
      $m_et = true;
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    49
      // alert everyone on the bot's alert list
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    50
      if ( is_object($irc) )
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    51
      {
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 27
diff changeset
    52
        global $permissions;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 27
diff changeset
    53
        foreach ( $permissions as $nick => $perms )
27
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    54
        {
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 27
diff changeset
    55
          if ( check_permissions($nick, array('context' => 'alert')) )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 27
diff changeset
    56
            $irc->privmsg($nick, "MySQL query error: $m_e");
27
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    57
        }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    58
      }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    59
      else
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    60
      {
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    61
        echo "\nQUERY ERROR: $m_e\nQuery: $sql\n";
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    62
        exit(1);
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    63
      }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    64
      return false;
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    65
    }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    66
    break;
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    67
  }
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    68
  return $q;
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    69
}
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    70
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    71
function db_escape($str)
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    72
{
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    73
  return mysql_real_escape_string($str);
4a9469bada05 Oops! Forgot to add the DBAL.
Dan
parents:
diff changeset
    74
}