plugins/ajim/server.php
author Dan
Fri, 29 May 2009 14:36:51 -0400
changeset 1 f7b24c744747
parent 0 db23957ad1c7
permissions -rw-r--r--
Updated for 1.1.6 compatibility
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']);
1
f7b24c744747 Updated for 1.1.6 compatibility
Dan
parents: 0
diff changeset
    84
          $row['rank_info']['rank_title'] = $lang->get($row['rank_info']['rank_title']);
0
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    85
          $row['message_html'] = RenderMan::render($row['message']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    86
          $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
    87
          $messages[] = $row;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    88
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    89
        $response = array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    90
          'mode' => 'messages',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    91
          'now' => time(),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    92
          'messages' => $messages
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    93
        );
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    94
        return print enano_json_encode($response);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    95
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    96
      else
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    97
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    98
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
    99
            'mode' => 'messages',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   100
            'now' => time(),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   101
            'messages' => array()
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
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   104
      break;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   105
    case 'submit':
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   106
      if ( !$session->get_permissions('ajim_post') )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   107
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   108
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   109
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   110
            'error' => $lang->get('ajim_err_post_denied')
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
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   113
      $name = $session->user_logged_in ? $session->username : $request['user'];
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   114
      $content = trim($request['message']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   115
      if ( empty($content) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   116
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   117
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   118
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   119
            'error' => $lang->get('ajim_err_no_post')
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
      
1
f7b24c744747 Updated for 1.1.6 compatibility
Dan
parents: 0
diff changeset
   123
      $content = RenderMan::preprocess_text($content, true, false);
f7b24c744747 Updated for 1.1.6 compatibility
Dan
parents: 0
diff changeset
   124
      
0
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   125
      $now = time();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   126
      $content_db = $db->escape($content);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   127
      $name_db = $db->escape($name);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   128
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   129
      $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
   130
                        . "  ({$session->user_id}, '$name_db', '$content_db', $now, $now);";
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   131
      if ( !$db->sql_query($sql) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   132
        $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   133
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   134
      // workaround for no insert_id() on postgresql
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   135
      $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
   136
      if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   137
        $db->die_json();
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
      list($message_id) = $db->fetchrow_num();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   140
      $db->free_result();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   141
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   142
      return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   143
          'mode' => 'messages',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   144
          'messages' => array(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   145
            'rank_info' => $session->get_user_rank($session->user_id),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   146
            'human_time' => enano_date('n/j, g:ia'),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   147
            'message' => $content,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   148
            'username' => $name,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   149
            'user_id' => $session->user_id,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   150
            'message_time' => time(),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   151
            'message_update_time' => time(),
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   152
            'message_id' => $message_id,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   153
            'message_html' => RenderMan::render($content)
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   154
            ))
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   155
        ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   156
      break;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   157
    case 'delete':
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   158
      if ( empty($request['message_id']) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   159
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   160
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   161
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   162
            'error' => 'No message_id specified.'
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   163
          ));
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
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   166
      $message_id = intval($request['message_id']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   167
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   168
      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
   169
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   170
        // 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
   171
        // own posts. if so, we can allow this to continue.
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   172
        $perm_override = false;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   173
        if ( $session->get_permissions('ajim_edit') && $session->user_logged_in )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   174
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   175
          $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
   176
          if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   177
            $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   178
          
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   179
          list($user_id) = $db->fetchrow_num();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   180
          $db->free_result();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   181
          if ( $user_id === $session->user_id )
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
            $perm_override = true;
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
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   186
        if ( !$perm_override )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   187
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   188
          return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   189
              'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   190
              'error' => $lang->get('ajim_err_access_denied')
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
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   193
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   194
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   195
      $now = time();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   196
      $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
   197
      if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   198
        $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   199
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   200
      return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   201
          'mode' => 'delete',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   202
          'message_id' => $message_id
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   203
        ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   204
      break;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   205
    case 'update':
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   206
      if ( empty($request['message_id']) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   207
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   208
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   209
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   210
            'error' => 'No message_id specified.'
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   211
          ));
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
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   214
      $message_id = intval($request['message_id']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   215
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   216
      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
   217
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   218
        // 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
   219
        // own posts. if so, we can allow this to continue.
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   220
        $perm_override = false;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   221
        if ( $session->get_permissions('ajim_edit') && $session->user_logged_in )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   222
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   223
          $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
   224
          if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   225
            $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   226
          
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   227
          list($user_id) = $db->fetchrow_num();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   228
          $db->free_result();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   229
          if ( $user_id === $session->user_id )
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
            $perm_override = true;
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
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   234
        if ( !$perm_override )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   235
        {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   236
          return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   237
              'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   238
              'error' => $lang->get('ajim_err_access_denied')
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
        }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   241
      }
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
      $message = trim(@$request['message']);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   244
      if ( empty($message) )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   245
      {
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   246
        return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   247
            'mode' => 'error',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   248
            'error' => $lang->get('ajim_err_no_post')
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   249
          ));
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   250
      }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   251
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   252
      $message_db = $db->escape($message);
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   253
      $now = time();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   254
      $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
   255
      if ( !$q )
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   256
        $db->die_json();
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   257
      
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   258
      return print enano_json_encode(array(
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   259
          'mode' => 'update',
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   260
          'message_id' => $message_id,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   261
          'message' => $message,
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   262
          'message_html' => RenderMan::render($message)
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
      break;
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   265
  }
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   266
}
db23957ad1c7 First commit; requires latest Enano from Hg. It works!
Dan
parents:
diff changeset
   267