plugins/ajim/server.php
author Dan
Sun, 11 Jan 2009 21:38:42 -0500
changeset 0 db23957ad1c7
child 1 f7b24c744747
permissions -rw-r--r--
First commit; requires latest Enano from Hg. It works!
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
     1
<?php
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
     2
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
     3
$plugins->attachHook('session_started', 'ajim_page_init();');
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
     4
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
     5
function ajim_page_init()
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
     6
{
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
     7
  global $db, $session, $paths, $template, $plugins; // Common objects
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
     8
  
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
     9
  $paths->add_page(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    10
      'name' => 'AjIM JSON handler',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    11
      'urlname' => 'AjimJson',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    12
      'namespace' => 'Special',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    13
      'visible' => 0,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    14
      'special' => 1,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    15
      'comments_on' => 0,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    16
      'protected' => 0
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    17
    ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    18
}
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    19
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    20
function page_Special_AjimJson()
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    21
{
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    22
  global $db, $session, $paths, $template, $plugins; // Common objects
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    23
  global $lang;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    24
  
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    25
  header('Content-type: text/javascript');
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    26
  if ( !isset($_GET['r']) && !isset($_POST['r']) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    27
  {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    28
    return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    29
        'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    30
        'error' => 'No request specified.'
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    31
      ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    32
  }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    33
  $request = enano_json_decode($_REQUEST['r']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    34
  if ( !isset($request['mode']) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    35
  {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    36
    return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    37
        'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    38
        'error' => 'No mode specified.'
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    39
      ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    40
  }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    41
  switch($request['mode'])
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    42
  {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    43
    case 'watch':
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    44
      @set_time_limit(0);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    45
      $time = ( !empty($request['lastrefresh']) ) ? intval($request['lastrefresh']) : 0;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    46
      $end = microtime_float() + 59;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    47
      // run cron-ish stuff
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    48
      if ( intval(getConfig('ajim_last_cleanout', 0)) + 86400 < time() )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    49
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    50
        $q = $db->sql_query('SELECT COUNT(message_id) FROM ' . table_prefix . "ajim2;");
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    51
        if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    52
          $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    53
        
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    54
        list($count) = $db->fetchrow_num();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    55
        $db->free_result();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    56
        if ( intval($count) > 50 )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    57
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    58
          // if there are more than 50 messages in the database, clean it out
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    59
          $limit = $count - 15;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    60
          $q = $db->sql_query('DELETE FROM ' . table_prefix . "ajim2 ORDER BY message_time ASC LIMIT $limit;");
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    61
          if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    62
            $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    63
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    64
        
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    65
        setConfig('ajim_last_cleanout', time());
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    66
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    67
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    68
      while ( microtime_float() < $end )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    69
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    70
        $q = $db->sql_query('SELECT * FROM ' . table_prefix . "ajim2 WHERE message_time >= $time OR message_update_time >= $time ORDER BY message_time DESC LIMIT 30;");
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    71
        if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    72
          $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    73
        if ( $db->numrows() > 0 || $time == 0 )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    74
          break;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    75
        $db->free_result();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    76
        usleep(500000); // 0.5s
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    77
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    78
      if ( $q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    79
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    80
        $messages = array();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    81
        while ( $row = $db->fetchrow() )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    82
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    83
          $row['rank_info'] = $session->get_user_rank($row['user_id']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    84
          $row['message_html'] = RenderMan::render($row['message']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    85
          $row['human_time'] = enano_date('n/j, g:ia', $row['message_time']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    86
          $messages[] = $row;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    87
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    88
        $response = array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    89
          'mode' => 'messages',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    90
          'now' => time(),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    91
          'messages' => $messages
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    92
        );
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    93
        return print enano_json_encode($response);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    94
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    95
      else
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    96
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    97
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    98
            'mode' => 'messages',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    99
            'now' => time(),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   100
            'messages' => array()
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   101
          ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   102
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   103
      break;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   104
    case 'submit':
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   105
      if ( !$session->get_permissions('ajim_post') )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   106
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   107
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   108
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   109
            'error' => $lang->get('ajim_err_post_denied')
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   110
          ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   111
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   112
      $name = $session->user_logged_in ? $session->username : $request['user'];
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   113
      $content = trim($request['message']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   114
      if ( empty($content) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   115
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   116
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   117
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   118
            'error' => $lang->get('ajim_err_no_post')
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   119
          ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   120
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   121
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   122
      $now = time();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   123
      $content_db = $db->escape($content);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   124
      $name_db = $db->escape($name);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   125
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   126
      $sql = 'INSERT INTO ' . table_prefix . "ajim2(user_id, username, message, message_time, message_update_time) VALUES\n"
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   127
                        . "  ({$session->user_id}, '$name_db', '$content_db', $now, $now);";
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   128
      if ( !$db->sql_query($sql) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   129
        $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   130
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   131
      // workaround for no insert_id() on postgresql
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   132
      $q = $db->sql_query('SELECT message_id FROM ' . table_prefix . "ajim2 WHERE username = '$name_db' AND message = '$content_db' AND message_time = $now ORDER BY message_id DESC LIMIT 1;");
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   133
      if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   134
        $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   135
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   136
      list($message_id) = $db->fetchrow_num();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   137
      $db->free_result();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   138
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   139
      return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   140
          'mode' => 'messages',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   141
          'messages' => array(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   142
            'rank_info' => $session->get_user_rank($session->user_id),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   143
            'human_time' => enano_date('n/j, g:ia'),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   144
            'message' => $content,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   145
            'username' => $name,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   146
            'user_id' => $session->user_id,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   147
            'message_time' => time(),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   148
            'message_update_time' => time(),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   149
            'message_id' => $message_id,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   150
            'message_html' => RenderMan::render($content)
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   151
            ))
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   152
        ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   153
      break;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   154
    case 'delete':
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   155
      if ( empty($request['message_id']) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   156
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   157
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   158
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   159
            'error' => 'No message_id specified.'
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   160
          ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   161
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   162
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   163
      $message_id = intval($request['message_id']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   164
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   165
      if ( ( !$session->get_permissions('ajim_mod') || $session->auth_level < USER_LEVEL_CHPREF ) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   166
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   167
        // we don't have permission according to ACLs, but try to see if we can edit our
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   168
        // own posts. if so, we can allow this to continue.
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   169
        $perm_override = false;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   170
        if ( $session->get_permissions('ajim_edit') && $session->user_logged_in )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   171
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   172
          $q = $db->sql_query('SELECT user_id FROM ' . table_prefix . "ajim2 WHERE message_id = $message_id;");
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   173
          if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   174
            $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   175
          
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   176
          list($user_id) = $db->fetchrow_num();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   177
          $db->free_result();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   178
          if ( $user_id === $session->user_id )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   179
          {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   180
            $perm_override = true;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   181
          }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   182
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   183
        if ( !$perm_override )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   184
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   185
          return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   186
              'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   187
              'error' => $lang->get('ajim_err_access_denied')
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   188
            ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   189
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   190
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   191
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   192
      $now = time();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   193
      $q = $db->sql_query('UPDATE ' . table_prefix . "ajim2 SET message = '', message_update_time = $now WHERE message_id = $message_id;");
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   194
      if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   195
        $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   196
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   197
      return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   198
          'mode' => 'delete',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   199
          'message_id' => $message_id
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   200
        ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   201
      break;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   202
    case 'update':
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   203
      if ( empty($request['message_id']) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   204
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   205
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   206
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   207
            'error' => 'No message_id specified.'
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   208
          ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   209
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   210
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   211
      $message_id = intval($request['message_id']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   212
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   213
      if ( ( !$session->get_permissions('ajim_mod') || $session->auth_level < USER_LEVEL_CHPREF ) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   214
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   215
        // we don't have permission according to ACLs, but try to see if we can edit our
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   216
        // own posts. if so, we can allow this to continue.
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   217
        $perm_override = false;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   218
        if ( $session->get_permissions('ajim_edit') && $session->user_logged_in )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   219
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   220
          $q = $db->sql_query('SELECT user_id FROM ' . table_prefix . "ajim2 WHERE message_id = $message_id;");
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   221
          if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   222
            $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   223
          
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   224
          list($user_id) = $db->fetchrow_num();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   225
          $db->free_result();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   226
          if ( $user_id === $session->user_id )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   227
          {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   228
            $perm_override = true;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   229
          }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   230
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   231
        if ( !$perm_override )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   232
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   233
          return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   234
              'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   235
              'error' => $lang->get('ajim_err_access_denied')
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   236
            ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   237
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   238
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   239
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   240
      $message = trim(@$request['message']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   241
      if ( empty($message) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   242
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   243
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   244
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   245
            'error' => $lang->get('ajim_err_no_post')
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   246
          ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   247
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   248
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   249
      $message_db = $db->escape($message);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   250
      $now = time();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   251
      $q = $db->sql_query('UPDATE ' . table_prefix . "ajim2 SET message = '{$message_db}', message_update_time = $now WHERE message_id = $message_id;");
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   252
      if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   253
        $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   254
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   255
      return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   256
          'mode' => 'update',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   257
          'message_id' => $message_id,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   258
          'message' => $message,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   259
          'message_html' => RenderMan::render($message)
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   260
        ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   261
      break;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   262
  }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   263
}
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   264