snippets.php
author Dan
Fri, 09 May 2008 22:19:32 -0400
changeset 6 a802bb5b3e08
parent 5 fcc1eac04772
child 8 0acb8d9a3194
permissions -rw-r--r--
Made the channel list completely dynamic now
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
     1
<?php
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
     2
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
     3
/**
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
     4
 * EnanoBot - copyright (C) 2008 Dan Fuhry
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
     5
 * All rights reserved.
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
     6
 */
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
     7
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
     8
/*****************************************************************
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
     9
 * YOU NEED TO SET THE PATH TO THE REST OF THE EnanoBot FILES HERE.
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    10
 * Include a trailing slash.
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    11
 * This script MUST be placed in an Enano installation directory.
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    12
 *****************************************************************/
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    13
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    14
define('ENANOBOT_ROOT', './');
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    15
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    16
// load Enano for auth
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    17
require('includes/common.php');
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    18
if ( $session->user_level < USER_LEVEL_ADMIN )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    19
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    20
  die_friendly('Access denied', '<p>Admin rights needed to use this script.</p>');
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    21
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    22
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    23
$db->close();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    24
unset($db, $session, $paths, $template, $plugins);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    25
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    26
// We're authed.
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    27
// Load config
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    28
require(ENANOBOT_ROOT . 'config.php');
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    29
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    30
// check config
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    31
if ( empty($mysql_host) || empty($mysql_user) || empty($mysql_dbname) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    32
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    33
  die("Bad config file - have a look at config-sample.php.\n");
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    34
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    35
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    36
// connect to MySQL
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    37
$mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    38
if ( !$mysql_conn )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    39
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    40
  $m_e = mysql_error();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    41
  echo "Error connecting to MySQL: $m_e\n";
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    42
  exit(1);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    43
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    44
$q = @mysql_query('USE `' . $mysql_dbname . '`;', $mysql_conn);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    45
if ( !$q )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    46
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    47
  $m_e = mysql_error();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    48
  echo "Error selecting database: $m_e\n";
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    49
  exit(1);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    50
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    51
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    52
function mysql_die()
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    53
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    54
  $m_e = mysql_error();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    55
  die("MySQL error: $m_e");
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    56
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    57
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    58
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    59
<html>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    60
  <head>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    61
    <title>EnanoBot snippet management</title>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    62
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    63
  </head>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    64
  <body>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    65
    <h1>EnanoBot snippet management</h1>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    66
    <form action="enanobot-snippets.php" method="post" enctype="multipart/form-data">
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    67
      <fieldset>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    68
        <legend>Add a snippet</legend>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    69
        <table border="1" cellspacing="0" cellpadding="4">
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    70
          <tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    71
            <td>Snippet code<br />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    72
                <small>all lowercase, no spaces; ex: mysnippet</small></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    73
            <td><input type="text" name="snippet_add_code" size="100" tabindex="1" /></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    74
          </tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    75
          <tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    76
            <td>Text<br />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    77
                <small>anything you want, keep it relatively short.</small></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    78
            <td><input type="text" name="snippet_add_text" size="100" tabindex="2" /></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    79
          </tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    80
          <tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    81
            <td>Channels<br />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    82
                <small>separate with pipe characters, ex: #enano|#enano-dev|#ubuntu</small></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    83
            <td><input type="text" name="snippet_add_channels" size="100" tabindex="3" /></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    84
          </tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    85
        </table>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    86
      </fieldset>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    87
      <fieldset>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    88
        <legend>Edit existing snippets</legend>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    89
        <table border="1" cellspacing="0" cellpadding="4">
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    90
          <tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    91
            <th>Code</th>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    92
            <th>Snippet text</th>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    93
            <th>Channels</th>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    94
            <th>Delete</th>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    95
          </tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    96
        <?php
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    97
            if ( !empty($_POST['snippet_add_code']) && !empty($_POST['snippet_add_text']) && !empty($_POST['snippet_add_channels']) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    98
            {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    99
              $code = mysql_real_escape_string($_POST['snippet_add_code']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   100
              $text = mysql_real_escape_string($_POST['snippet_add_text']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   101
              $channels = mysql_real_escape_string($_POST['snippet_add_channels']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   102
              $q2 = @mysql_query("INSERT INTO snippets(snippet_code, snippet_text, snippet_channels) VALUES
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   103
                                    ( '$code', '$text', '$channels' );", $mysql_conn);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   104
              if ( !$q2 )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   105
                mysql_die();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   106
            }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   107
            $q = @mysql_query('SELECT snippet_id, snippet_code, snippet_text, snippet_channels FROM snippets ORDER BY snippet_code ASC;');
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   108
            if ( !$q )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   109
              mysql_die();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   110
            while ( $row = @mysql_fetch_assoc($q) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   111
            {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   112
              if ( isset($_POST['snippet']) && @is_array(@$_POST['snippet']) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   113
              {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   114
                if ( isset($_POST['snippet'][$row['snippet_id']]) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   115
                {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   116
                  // delete it?
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   117
                  if ( isset($_POST['snippet'][$row['snippet_id']]['delete']) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   118
                  {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   119
                    $q2 = mysql_query("DELETE FROM snippets WHERE snippet_id = {$row['snippet_id']};", $mysql_conn);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   120
                    if ( !$q2 )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   121
                      mysql_die();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   122
                    continue;
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   123
                  }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   124
                  // has it changed?
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   125
                  else if ( $_POST['snippet'][$row['snippet_id']]['code'] != $row['snippet_code'] ||
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   126
                       $_POST['snippet'][$row['snippet_id']]['text'] != $row['snippet_text'] ||
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   127
                       $_POST['snippet'][$row['snippet_id']]['channels'] != $row['snippet_channels'] )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   128
                  {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   129
                    // yeah, update it.
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   130
                    $code = mysql_real_escape_string($_POST['snippet'][$row['snippet_id']]['code']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   131
                    $text = mysql_real_escape_string($_POST['snippet'][$row['snippet_id']]['text']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   132
                    $channels = mysql_real_escape_string($_POST['snippet'][$row['snippet_id']]['channels']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   133
                    $q2 = mysql_query("UPDATE snippets SET snippet_code = '$code', snippet_text = '$text', snippet_channels = '$channels' WHERE snippet_id = {$row['snippet_id']};", $mysql_conn);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   134
                    if ( !$q2 )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   135
                      mysql_die();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   136
                    $row = array(
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   137
                        'snippet_id' => $row['snippet_id'],
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   138
                        'snippet_code' => $_POST['snippet'][$row['snippet_id']]['code'],
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   139
                        'snippet_text' => $_POST['snippet'][$row['snippet_id']]['text'],
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   140
                        'snippet_channels' => $_POST['snippet'][$row['snippet_id']]['channels']
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   141
                      );
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   142
                  }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   143
                }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   144
              }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   145
              echo '  <tr>';
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   146
              echo '<td><input type="text" name="snippet[' . $row['snippet_id'] . '][code]" value="' . htmlspecialchars($row['snippet_code']) . '" /></td>';
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   147
              echo '<td><input type="text" size="100" name="snippet[' . $row['snippet_id'] . '][text]" value="' . htmlspecialchars($row['snippet_text']) . '" /></td>';
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   148
              echo '<td><input type="text" name="snippet[' . $row['snippet_id'] . '][channels]" value="' . htmlspecialchars($row['snippet_channels']) . '" /></td>';
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   149
              echo '<td style="text-align: center;"><input type="checkbox" name="snippet[' . $row['snippet_id'] . '][delete]" /></td>';
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   150
              echo '</tr>' . "\n        ";
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   151
            }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   152
          ?></table>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   153
      </fieldset>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   154
      <div style="text-align: center; margin-top: 20px;">
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   155
        <input type="submit" value="Save changes" />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   156
      </div>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   157
    </form>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   158
  </body>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   159
</html><?php
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   160
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   161
mysql_close($mysql_conn);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   162