snippets.php
author Dan
Sun, 14 Dec 2008 00:51:06 -0500
changeset 35 a76ae7eaa2eb
parent 8 0acb8d9a3194
permissions -rw-r--r--
Updated news page
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
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 5
diff changeset
    14
define('ENANOBOT_ROOT', dirname(__FILE__) . '/');
5
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    15
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    16
// load Enano for auth
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 5
diff changeset
    17
/*
5
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    18
require('includes/common.php');
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    19
if ( $session->user_level < USER_LEVEL_ADMIN )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    20
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    21
  die_friendly('Access denied', '<p>Admin rights needed to use this script.</p>');
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    22
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    23
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    24
$db->close();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    25
unset($db, $session, $paths, $template, $plugins);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 5
diff changeset
    26
*/
5
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    27
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    28
// We're authed.
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    29
// Load config
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    30
require(ENANOBOT_ROOT . 'config.php');
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    31
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    32
// check config
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    33
if ( empty($mysql_host) || empty($mysql_user) || empty($mysql_dbname) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    34
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    35
  die("Bad config file - have a look at config-sample.php.\n");
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    36
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    37
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    38
// connect to MySQL
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    39
$mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    40
if ( !$mysql_conn )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    41
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    42
  $m_e = mysql_error();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    43
  echo "Error connecting to MySQL: $m_e\n";
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    44
  exit(1);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    45
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    46
$q = @mysql_query('USE `' . $mysql_dbname . '`;', $mysql_conn);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    47
if ( !$q )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    48
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    49
  $m_e = mysql_error();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    50
  echo "Error selecting database: $m_e\n";
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    51
  exit(1);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    52
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    53
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    54
function mysql_die()
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    55
{
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    56
  $m_e = mysql_error();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    57
  die("MySQL error: $m_e");
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    58
}
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    59
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    60
?><!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
    61
<html>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    62
  <head>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    63
    <title>EnanoBot snippet management</title>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    64
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    65
  </head>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    66
  <body>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    67
    <h1>EnanoBot snippet management</h1>
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 5
diff changeset
    68
    <form action="snippets.php" method="post" enctype="multipart/form-data">
5
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    69
      <fieldset>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    70
        <legend>Add a snippet</legend>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    71
        <table border="1" cellspacing="0" cellpadding="4">
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    72
          <tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    73
            <td>Snippet code<br />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    74
                <small>all lowercase, no spaces; ex: mysnippet</small></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    75
            <td><input type="text" name="snippet_add_code" size="100" tabindex="1" /></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    76
          </tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    77
          <tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    78
            <td>Text<br />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    79
                <small>anything you want, keep it relatively short.</small></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    80
            <td><input type="text" name="snippet_add_text" size="100" tabindex="2" /></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    81
          </tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    82
          <tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    83
            <td>Channels<br />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    84
                <small>separate with pipe characters, ex: #enano|#enano-dev|#ubuntu</small></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    85
            <td><input type="text" name="snippet_add_channels" size="100" tabindex="3" /></td>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    86
          </tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    87
        </table>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    88
      </fieldset>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    89
      <fieldset>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    90
        <legend>Edit existing snippets</legend>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    91
        <table border="1" cellspacing="0" cellpadding="4">
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    92
          <tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    93
            <th>Code</th>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    94
            <th>Snippet text</th>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    95
            <th>Channels</th>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    96
            <th>Delete</th>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    97
          </tr>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    98
        <?php
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
    99
            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
   100
            {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   101
              $code = mysql_real_escape_string($_POST['snippet_add_code']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   102
              $text = mysql_real_escape_string($_POST['snippet_add_text']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   103
              $channels = mysql_real_escape_string($_POST['snippet_add_channels']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   104
              $q2 = @mysql_query("INSERT INTO snippets(snippet_code, snippet_text, snippet_channels) VALUES
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   105
                                    ( '$code', '$text', '$channels' );", $mysql_conn);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   106
              if ( !$q2 )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   107
                mysql_die();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   108
            }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   109
            $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
   110
            if ( !$q )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   111
              mysql_die();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   112
            while ( $row = @mysql_fetch_assoc($q) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   113
            {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   114
              if ( isset($_POST['snippet']) && @is_array(@$_POST['snippet']) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   115
              {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   116
                if ( isset($_POST['snippet'][$row['snippet_id']]) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   117
                {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   118
                  // delete it?
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   119
                  if ( isset($_POST['snippet'][$row['snippet_id']]['delete']) )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   120
                  {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   121
                    $q2 = mysql_query("DELETE FROM snippets WHERE snippet_id = {$row['snippet_id']};", $mysql_conn);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   122
                    if ( !$q2 )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   123
                      mysql_die();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   124
                    continue;
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   125
                  }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   126
                  // has it changed?
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   127
                  else if ( $_POST['snippet'][$row['snippet_id']]['code'] != $row['snippet_code'] ||
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   128
                       $_POST['snippet'][$row['snippet_id']]['text'] != $row['snippet_text'] ||
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   129
                       $_POST['snippet'][$row['snippet_id']]['channels'] != $row['snippet_channels'] )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   130
                  {
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   131
                    // yeah, update it.
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   132
                    $code = mysql_real_escape_string($_POST['snippet'][$row['snippet_id']]['code']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   133
                    $text = mysql_real_escape_string($_POST['snippet'][$row['snippet_id']]['text']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   134
                    $channels = mysql_real_escape_string($_POST['snippet'][$row['snippet_id']]['channels']);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   135
                    $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
   136
                    if ( !$q2 )
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   137
                      mysql_die();
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   138
                    $row = array(
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   139
                        'snippet_id' => $row['snippet_id'],
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   140
                        'snippet_code' => $_POST['snippet'][$row['snippet_id']]['code'],
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   141
                        'snippet_text' => $_POST['snippet'][$row['snippet_id']]['text'],
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   142
                        'snippet_channels' => $_POST['snippet'][$row['snippet_id']]['channels']
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
                }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   146
              }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   147
              echo '  <tr>';
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   148
              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
   149
              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
   150
              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
   151
              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
   152
              echo '</tr>' . "\n        ";
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   153
            }
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   154
          ?></table>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   155
      </fieldset>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   156
      <div style="text-align: center; margin-top: 20px;">
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   157
        <input type="submit" value="Save changes" />
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   158
      </div>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   159
    </form>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   160
  </body>
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   161
</html><?php
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   162
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   163
mysql_close($mysql_conn);
fcc1eac04772 Prepped for public distribution.
Dan
parents:
diff changeset
   164