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