aboutsummaryrefslogtreecommitdiff
path: root/cgi/anarkia.py
blob: 6b7e5fd1d759e89a8748ea371b4d87875e30604c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# coding=utf-8
import _mysql
from database import *
from framework import *
from template import *
from img import *
from post import *
from settings import Settings

d_thread = {}
d_post = {}

def anarkia(self, path_split):
  setBoard('anarkia')
  
  if len(path_split) <= 2:
    self.output = main()
    return
    
  raise UserError, 'Ya fue, baisano...'
  
  if path_split[2] == 'opt':
    self.output = boardoptions(self.formdata)
  elif path_split[2] == 'mod':
    self.output = mod(self.formdata)
  elif path_split[2] == 'bans':
    self.output = bans(self.formdata)
  elif path_split[2] == 'css':
    self.output = css(self.formdata)
  elif path_split[2] == 'type':
    self.output = type(self.formdata)
  elif path_split[2] == 'emojis':
    self.output = emojis(self.formdata)
  else:
    raise UserError, 'ke?'

def main():
  board = Settings._.BOARD
  
  logs = FetchAll("SELECT * FROM `logs` WHERE `staff` = 'Anarko' ORDER BY `timestamp` DESC")
  for log in logs:
    log['timestamp_formatted'] = formatTimestamp(log['timestamp'])
  
  return renderTemplate('anarkia.html', {'mode': 0, 'logs': logs})

def type(formdata):
  board = Settings._.BOARD
  
  if board['board_type'] == '1':
    (type_now, type_do, do_num) = ('BBS', 'IB', '0')
  else:
    (type_now, type_do, do_num) = ('IB', 'BBS', '1')
    
  if formdata.get('transform') == 'do':
    t = 0
    try:
      with open('anarkia_time') as f:
        t = int(f.read())
    except IOError:
      pass
    
    dif = time.time() - t
    if dif > (10 * 60):
    #if True:
      import re
      t = time.time()
      
      board['board_type'] = do_num
      board['force_css'] = Settings.HOME_URL + 'anarkia/style_' + type_do.lower() + '.css'
      updateBoardSettings()
      
      # update posts
      fix_board()
      
      # regenerate
      setBoard('anarkia')
      regenerateBoard(True)
      
      tf = timeTaken(t, time.time())
    
      with open('anarkia_time', 'w') as f:
        t = f.write(str(int(time.time())))
        
      msg = 'Cambiada estructura de sección a %s. (%s)' % (type_do, tf)
      logAction(msg)
      return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg})
    else:
      raise UserError, 'Esta acción sólo se puede realizar cada 10 minutos. Faltan: %d mins.' % (10-int(dif/60))
    
  return renderTemplate('anarkia.html', {'mode': 7, 'type_now': type_now, 'type_do': type_do})

def fix_board():
  board = Settings._.BOARD
  get_fix_dictionary()
  
  if board['board_type'] == '1':
    to_fix = FetchAll("SELECT * FROM posts WHERE message LIKE '%%anarkia/res/%%' AND boardid = %s" % board['id'])
  else:
    to_fix = FetchAll("SELECT * FROM posts WHERE message LIKE '%%anarkia/read/%%' AND boardid = %s" % board['id'])
    
  for p in to_fix:
    try:
      if board['board_type'] == '1':
        newmessage = re.sub(r'/anarkia/res/(\d+).html#(\d+)">&gt;&gt;(\d+)', fix_to_bbs, p['message'])
      else:
        newmessage = re.sub(r'/anarkia/read/(\d+)/(\d+)">&gt;&gt;(\d+)', fix_to_ib, p['message'])
        
      UpdateDb("UPDATE posts SET message = '%s' WHERE boardid = %s AND id = %s" % \
        (_mysql.escape_string(newmessage), board['id'], p['id']))
    except KeyError:
      pass
    
  return True

def fix_to_bbs(matchobj):
  threadid = matchobj.group(1)
  pid = matchobj.group(2)
  new_thread = d_thread[threadid]
  new_post = d_post[new_thread][pid]
  return '/anarkia/read/%s/%s">&gt;&gt;%s' % (new_thread, new_post, new_post)
  
def fix_to_ib(matchobj):
  threadid = matchobj.group(1)
  num = int(matchobj.group(2))
  new_thread = d_thread[threadid]
  new_post = d_post[new_thread][num]
  return '/anarkia/res/%s.html#%s">&gt;&gt;%s' % (new_thread, new_post, new_post)
  
def get_fix_dictionary():
  global d_thread, d_post
  board = Settings._.BOARD
  res = FetchAll("SELECT id, timestamp, parentid FROM posts WHERE boardid = %s ORDER BY CASE parentid WHEN 0 THEN id ELSE parentid END ASC, `id` ASC" % board['id'])
  num = 1
  thread = 0
  for p in res:
    pid = p['id']
    if p['parentid'] == '0':
      num = 1
      
      time = p['timestamp']
      if board['board_type'] == '1':
        d_thread[pid] = time
        thread = time
      else:
        d_thread[time] = pid
        thread = pid
      
      d_post[thread] = {}
      
    if board['board_type'] == '1':
      d_post[thread][pid] = num
    else:
      d_post[thread][num] = pid
    num += 1
      
  return
    
def css(formdata):
  board = Settings._.BOARD
  
  if board['board_type'] == '1':
    basename = 'style_bbs.css'
  else:
    basename = 'style_ib.css'
    
  fname = '%sanarkia/%s' % (Settings.HOME_DIR, basename)
    
  if formdata.get('cssfile'):
    with open(fname, 'w') as f:
      cssfile = f.write(formdata['cssfile'])
      
    msg = 'CSS actualizado.'
    logAction(msg)
    return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg})
    
  with open(fname) as f:
    cssfile = f.read()
  
  return renderTemplate('anarkia.html', {'mode': 6, 'basename': basename, 'cssfile': cssfile})
  
def bans(formdata):
  board = Settings._.BOARD
  
  if formdata.get('unban'):
    unban = int(formdata['unban'])
    boardpickle = pickle.dumps(['anarkia'])
    
    ban = FetchOne("SELECT * FROM `bans` WHERE id = %d" % unban)
    if not ban:
      raise UserError, "Ban inválido."
    if ban['boards'] != boardpickle:
      raise USerError, "Ban inválido."
      
    UpdateDb('DELETE FROM `bans` WHERE id = %s' % ban['id'])
    logAction("Usuario %s desbaneado." % ban['ip'][:4])
    regenerateAccess()
    
  bans = FetchAll('SELECT * FROM `bans` WHERE staff = \'anarko\'')
  for ban in bans:
    ban['added'] = formatTimestamp(ban['added'])
    if ban['until'] == '0':
      ban['until'] = _('Does not expire')
    else:
      ban['until'] = formatTimestamp(ban['until'])
  return renderTemplate('anarkia.html', {'mode': 5, 'bans': bans})
  
def mod(formdata):
  board = Settings._.BOARD
  
  if formdata.get('thread'):
    parentid = int(formdata['thread'])
    posts = FetchAll('SELECT * FROM `posts` WHERE (parentid = %d OR id = %d) AND boardid = %s ORDER BY `id` ASC' % (parentid, parentid, board['id']))
    return renderTemplate('anarkia.html', {'mode': 3, 'posts': posts})
  elif formdata.get('lock'):
    postid = int(formdata['lock'])
    post = FetchOne('SELECT id, locked FROM posts WHERE boardid = %s AND id = %d AND parentid = 0 LIMIT 1' % (board['id'], postid))
    if post['locked'] == '0':
      setLocked = 1
      msg = "Hilo %s cerrado." % post['id']
    else:
      setLocked = 0
      msg = "Hilo %s abierto." % post['id']
            
    UpdateDb("UPDATE `posts` SET `locked` = %d WHERE `boardid` = '%s' AND `id` = '%s' LIMIT 1" % (setLocked, board["id"], post["id"]))
    threadUpdated(post['id'])
    logAction(msg)
    return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg})
  elif formdata.get('del'):
    postid = int(formdata['del'])
    post = FetchOne('SELECT id, parentid FROM posts WHERE boardid = %s AND id = %d LIMIT 1' % (board['id'], postid))
    if post['parentid'] != '0':
      deletePost(post['id'], None, '3', False)
      msg = "Mensaje %s eliminado." % post['id']
      logAction(msg)
      return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg})
    else:
      raise UserError, "jaj no"
  elif formdata.get('restore'):
    postid = int(formdata['restore'])
    post = FetchOne('SELECT id, parentid FROM posts WHERE boardid = %s AND id = %d LIMIT 1' % (board['id'], postid))
    
    UpdateDb('UPDATE `posts` SET `IS_DELETED` = 0 WHERE `boardid` = %s AND `id` = %s LIMIT 1' % (board['id'], post['id']))
    if post['parentid'] != '0':
      threadUpdated(post['parentid'])
    else:
      regenerateFrontPages()
    msg = "Mensaje %s recuperado." % post['id']
    logAction(msg)
    return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg})
  elif formdata.get('ban'):
    postid = int(formdata['ban'])
    post = FetchOne('SELECT id, ip FROM posts WHERE boardid = %s AND id = %d LIMIT 1' % (board['id'], postid))
    
    return renderTemplate('anarkia.html', {'mode': 4, 'post': post})
  elif formdata.get('banto'):
    postid = int(formdata['banto'])
    post = FetchOne('SELECT id, message, parentid, ip FROM posts WHERE boardid = %s AND id = %d LIMIT 1' % (board['id'], postid))
  
    reason = formdata.get('reason').replace('script', '').replace('meta', '')
    if reason is not None:
      if formdata['seconds'] != '0':
        until = str(timestamp() + int(formdata['seconds']))
      else:
        until = '0'
      where = pickle.dumps(['anarkia'])

      ban = FetchOne("SELECT `id` FROM `bans` WHERE `ip` = '" + post['ip'] + "' AND `boards` = '" + _mysql.escape_string(where) + "' LIMIT 1")
      if ban:
        raise UserError, "Este usuario ya esta baneado."
      
      # Blind mode
      if formdata.get('blind') == '1':
        blind = '1'
      else:
        blind = '0'
      
      InsertDb("INSERT INTO `bans` (`ip`, `netmask`, `boards`, `added`, `until`, `staff`, `reason`, `blind`) VALUES ('" + post['ip'] + "', INET_ATON('255.255.255.255'), '" + _mysql.escape_string(where) + "', " + str(timestamp()) + ", " + until + ", 'anarko', '" + _mysql.escape_string(formdata['reason']) + "', '"+blind+"')")
      
      newmessage = post['message'] + '<hr /><span class="banned">A este usuario se le revocó el acceso. Razón: %s</span>' % reason
      
      UpdateDb("UPDATE posts SET message = '%s' WHERE boardid = %s AND id = %s" % (_mysql.escape_string(newmessage), board['id'], post['id']))
      if post['parentid'] != '0':
        threadUpdated(post['parentid'])
      else:
        regenerateFrontPages()
      regenerateAccess()
      
      msg = "Usuario %s baneado." % post['ip'][:4]
      logAction(msg)
      return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg})
  else:
    reports = FetchAll("SELECT * FROM `reports` WHERE board = 'anarkia'")
    threads = FetchAll('SELECT * FROM `posts` WHERE boardid = %s AND parentid = 0 ORDER BY `bumped` DESC' % board['id'])
    return renderTemplate('anarkia.html', {'mode': 2, 'threads': threads, 'reports': reports})
    
def boardoptions(formdata):
  board = Settings._.BOARD
  
  if formdata.get('longname'):
    # submitted
    board['longname'] = formdata['longname'].replace('script', '')
    board['postarea_desc'] = formdata['postarea_desc'].replace('script', '').replace('meta', '')
    board['postarea_extra'] = formdata['postarea_extra'].replace('script', '').replace('meta', '')
    board['anonymous'] = formdata['anonymous'].replace('script', '')
    board['subject'] = formdata['subject'].replace('script', '')
    board['message'] = formdata['message'].replace('script', '')
    board['useid'] = formdata['useid']
    if 'disable_name' in formdata.keys():
      board['disable_name'] = '1'
    else:
      board['disable_name'] = '0'
    if 'disable_subject' in formdata.keys():
      board['disable_subject'] = '1'
    else:
      board['disable_subject'] = '0'
    if 'allow_noimage' in formdata.keys():
      board['allow_noimage'] = '1'
    else:
      board['allow_noimage'] = '0'
    if 'allow_images' in formdata.keys():
      board['allow_images'] = '1'
    else:
      board['allow_images'] = '0'
    if 'allow_image_replies' in formdata.keys():
      board['allow_image_replies'] = '1'
    else:
      board['allow_image_replies'] = '0'

    # Update file types
    UpdateDb("DELETE FROM `boards_filetypes` WHERE `boardid` = %s" % board['id'])
    for filetype in filetypelist():
      if 'filetype'+filetype['ext'] in formdata.keys():
        UpdateDb("INSERT INTO `boards_filetypes` VALUES (%s, %s)" % (board['id'], filetype['id']))

    try:
      board['maxsize'] = int(formdata['maxsize'])
      if board['maxsize'] > 10000:
        board['maxsize'] = 10000
    except:
      raise UserError, _("Max size must be numeric.")

    try:
      board['thumb_px'] = int(formdata['thumb_px'])
      if board['thumb_px'] > 500:
        board['thumb_px'] = 500
    except:
      raise UserError, _("Max thumb dimensions must be numeric.")

    try:
      board['numthreads'] = int(formdata['numthreads'])
      if board['numthreads'] > 15:
        board['numthreads'] = 15
    except:
      raise UserError, _("Max threads shown must be numeric.")

    try:
      board['numcont'] = int(formdata['numcont'])
      if board['numcont'] > 15:
        board['numcont'] = 15
    except:
      raise UserError, _("Max replies shown must be numeric.")

    t = time.time()
    updateBoardSettings()
    setBoard('anarkia')
    regenerateBoard(True)
    tf = timeTaken(t, time.time())
    
    msg = 'Opciones cambiadas. %s' % tf
    logAction(msg)
    return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg})
  else:
    return renderTemplate('anarkia.html', {'mode': 1, 'boardopts': board, 'filetypes': filetypelist(), 'supported_filetypes': board['filetypes_ext']})

def emojis(formdata):
  board = Settings._.BOARD
  board_pickle = _mysql.escape_string(pickle.dumps([board['dir']]))
  
  if formdata.get('new'):
    import re
    ext = {'image/jpeg': 'jpg', 'image/gif': 'gif', 'image/png': 'png'}
    
    if not formdata['name']:
      raise UserError, 'Ingresa nombre.'
    if not re.match(r"^[0-9a-zA-Z]+$", formdata['name']):
      raise UserError, 'Nombre inválido; solo letras/números.'
      
    name = ":%s:" % formdata['name'][:15]
    data = formdata['file']
    
    if not data:
      raise UserError, 'Ingresa imagen.'
    
    # check if it exists
    already = FetchOne("SELECT 1 FROM `filters` WHERE `boards` = '%s' AND `from` = '%s'" % (board_pickle, _mysql.escape_string(name)))
    if already:
      raise UserError, 'Este emoji ya existe.'
    
    # get image information
    content_type, width, height, size, extra = getImageInfo(data)
    
    if content_type not in ext.keys():
      raise UserError, 'Formato inválido.'
    if width > 500 or height > 500:
      raise UserError, 'Dimensiones muy altas.'
    if size > 150000:
      raise UserError, 'Tamaño muy grande.'
      
    # create file names
    thumb_width, thumb_height = getThumbDimensions(width, height, 32)
    
    file_path = Settings.ROOT_DIR + board["dir"] + "/e/" + formdata['name'][:15] + '.' + ext[content_type]
    file_url = Settings.BOARDS_URL + board["dir"] + "/e/" + formdata['name'][:15] + '.' + ext[content_type]
    to_filter = '<img src="%s" width="%d" height="%d" />' % (file_url, thumb_width, thumb_height)
    
    # start processing image
    args = [Settings.CONVERT_PATH, "-", "-limit" , "thread", "1", "-resize", "%dx%d" % (thumb_width, thumb_height), "-quality", "80", file_path]
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    out = p.communicate(input=data)[0]
    
    # insert into DB
    sql = "INSERT INTO `filters` (`boards`, `type`, `action`, `from`, `to`, `staff`, `added`) VALUES ('%s', 0, 1, '%s', '%s', 'Anarko', '%s')" % (board_pickle, _mysql.escape_string(name), _mysql.escape_string(to_filter), timestamp())
    UpdateDb(sql)
    
    msg = "Emoji %s agregado." % name
    logAction(msg)
    return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg})
  elif formdata.get('del'):
    return renderTemplate('anarkia.html', {'mode': 99, 'msg': 'Del.'})
  else:
    filters = FetchAll("SELECT * FROM `filters` WHERE `boards` = '%s' ORDER BY `added` DESC" % board_pickle)
    return renderTemplate('anarkia.html', {'mode': 8, 'emojis': filters})

def filetypelist():
  filetypes = FetchAll('SELECT * FROM `filetypes` ORDER BY `ext` ASC')
  return filetypes

def logAction(action):
  InsertDb("INSERT INTO `logs` (`timestamp`, `staff`, `action`) VALUES (" + str(timestamp()) + ", 'Anarko', '" + _mysql.escape_string(action) + "')")