/
tdiary.rb
1121 lines (993 loc) · 25.2 KB
/
tdiary.rb
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8; -*-
=begin
== NAME
tDiary: the "tsukkomi-able" web diary system.
Copyright (C) 2001-2012, TADA Tadashi <t@tdtds.jp>
You can redistribute it and/or modify it under GPL2.
=end
TDIARY_VERSION = '3.1.3.20120614'
$:.unshift File.join(File::dirname(__FILE__), '/misc/lib').untaint
Dir["#{File::dirname(__FILE__) + '/vendor/*/lib'}"].each {|dir| $:.unshift dir.untaint }
require 'cgi'
require 'uri'
require 'logger'
require 'fileutils'
require 'pstore'
require 'json'
require 'erb'
require 'tdiary/compatible'
require 'tdiary/core_ext'
#
# module TDiary
#
module TDiary
PATH = File::dirname( __FILE__ ).untaint
autoload :Config, 'tdiary/config'
autoload :Plugin, 'tdiary/plugin'
autoload :Comment, 'tdiary/comment'
autoload :CommentManager, 'tdiary/comment_manager'
autoload :RefererManager, 'tdiary/referer_manager'
autoload :Filter, 'tdiary/filter'
autoload :Dispatcher, 'tdiary/dispatcher'
autoload :Request, 'tdiary/request'
autoload :Response, 'tdiary/response'
#
# module DiaryBase
# Base module of Diary.
#
module DiaryBase
include ERB::Util
include CommentManager
include RefererManager
def init_diary
init_comments
init_referers
@show = true
end
def date
@date
end
def set_date( date )
if date.class == String then
y, m, d = date.scan( /^(\d{4})(\d\d)(\d\d)$/ )[0]
raise ArgumentError::new( 'date string needs YYYYMMDD format.' ) unless y
@date = Time::local( y, m, d )
else
@date = date
end
end
def title
@title || ''
end
def set_title( title )
@title = title
@last_modified = Time::now
end
def show( s )
@show = s
end
def visible?
@show != false;
end
def last_modified
@last_modified ? @last_modified : Time::at( 0 )
end
def last_modified=( lm )
@last_modified = lm
end
def eval_rhtml( opt, path = '.' )
ERB::new( File::open( "#{path}/skel/#{opt['prefix']}diary.rhtml" ){|f| f.read }.untaint ).result( binding )
end
end
#
# exception classes
#
class TDiaryError < StandardError; end
class PermissionError < TDiaryError; end
class PluginError < TDiaryError; end
class BadStyleError < TDiaryError; end
class NotFound < TDiaryError; end
# class ForceRedirect
# force redirect to another page
#
class ForceRedirect < StandardError
attr_reader :path
def initialize( path )
@path = path
end
end
#
# module CategorizableDiary
#
module CategorizableDiary
def categorizable?; true; end
end
#
# module UncategorizableDiary
#
module UncategorizableDiary
def categorizable?; false; end
end
#
# class TDiaryBase
# tDiary CGI
#
class TDiaryBase
include ERB::Util
DIRTY_NONE = 0
DIRTY_DIARY = 1
DIRTY_COMMENT = 2
DIRTY_REFERER = 4
attr_reader :cookies
attr_reader :date
attr_reader :diaries
attr_reader :cgi, :rhtml, :conf
attr_reader :ignore_parser_cache
def initialize( cgi, rhtml, conf )
@cgi, @rhtml, @conf = cgi, rhtml, conf
@diaries = {}
@cookies = []
@io = @conf.io_class.new( self )
@logger = @conf.logger || load_logger
@ignore_parser_cache = false
end
def eval_rhtml( prefix = '' )
begin
r = do_eval_rhtml( prefix )
rescue PluginError, SyntaxError, ArgumentError
r = ERB::new( File::open( "#{PATH}/skel/plugin_error.rhtml" ) {|f| f.read }.untaint ).result( binding )
rescue Exception
raise
end
return r
end
def last_modified
nil
end
def []( date )
@diaries[date.strftime( '%Y%m%d' )]
end
def calendar
@years = @io.calendar unless @years
end
protected
def do_eval_rhtml( prefix )
# load plugin files
load_plugins
# load and apply rhtmls
r = @io.restore_cache( prefix )
if r.nil?
files = ["header.rhtml", @rhtml, "footer.rhtml"]
rhtml = files.collect {|file|
path = "#{PATH}/skel/#{prefix}#{file}"
begin
File::open( "#{path}.#{@conf.lang}" ) {|f| f.read }
rescue
File::open( path ) {|f| f.read }
end
}.join
begin
r = ERB::new( rhtml.untaint ).result( binding )
rescue => e
if defined?(::Encoding) && e.class == ::Encoding::CompatibilityError
# migration error on ruby 1.9 only 1st time, reload.
raise ForceRedirect::new( @conf.base_url )
end
end
r = ERB::new( r ).src
@io.store_cache( r, prefix ) unless @diaries.empty?
end
# apply plugins
r = @plugin.eval_src( r.untaint, @conf.secure ) if @plugin
@cookies += @plugin.cookies
r
end
def mode
self.class.to_s.sub( /^TDiary::TDiary/, '' ).downcase
end
def load_plugins
calendar
@plugin = Plugin::new(
'conf' => @conf,
'mode' => mode,
'diaries' => @diaries,
'cgi' => @cgi,
'years' => @years,
'cache_path' => @io.cache_path,
'date' => @date,
'comment' => @comment,
'last_modified' => last_modified,
'logger' => @logger
)
end
def <<( diary )
@diaries[diary.date.strftime( '%Y%m%d' )] = diary
end
def delete( date )
@diaries.delete( date.strftime( '%Y%m%d' ) )
end
def load_filters
return if @filters
@filters = []
filter_path = @conf.filter_path || "#{PATH}/tdiary/filter"
Dir::glob( "#{filter_path}/*.rb" ).sort.each do |file|
require file.untaint
@filters << TDiary::Filter::const_get( "#{File::basename( file, '.rb' ).capitalize}Filter" )::new( @cgi, @conf, @logger )
end
end
def all_filters
load_filters
@filters + (load_plugins.sf_filters || [])
end
def comment_filter( diary, comment )
all_filters.each do |filter|
return false unless filter.comment_filter( diary, comment )
break unless comment.visible?
end
true
end
def referer_filter( referer )
all_filters.each do |filter|
return false unless filter.referer_filter( referer )
end
true
end
def load_logger
return if @logger
log_path = (@conf.log_path || "#{@conf.data_path}log").untaint
FileUtils.mkdir_p(log_path) unless FileTest.directory?(log_path)
@logger = Logger.new(File.join(log_path, "debug.log"), 'daily')
@logger.level = Logger.const_get(@conf.log_level || 'DEBUG')
@logger
end
end
#
# class TDiaryAuthorOnlyBase
# base class for author-only access pages
#
class TDiaryAuthorOnlyBase < TDiaryBase
def csrf_protection_get_is_okay
false
end
def initialize( cgi, rhtml, conf )
super
csrf_check( cgi, conf )
end
private
def csrf_check( cgi, conf )
# CSRF condition check
protection_method = conf.options['csrf_protection_method']
masterkey = conf.options['csrf_protection_key']
updaterb_regexp = conf.options['csrf_protection_allowed_referer_regexp_for_update']
protection_method = 1 unless protection_method
return if protection_method == -1 # don't use this setting!
check_key = (protection_method & 2 != 0)
check_referer = (protection_method & 1 != 0)
masterkey = '' unless masterkey
updaterb_regexp = '' unless updaterb_regexp
if (masterkey != '' && check_key)
@csrf_protection = %Q[<input type="hidden" name="csrf_protection_key" value="#{h masterkey}">]
else
@csrf_protection="<!-- no CSRF protection key used -->"
end
referer = cgi.referer || ''
referer = referer.sub(/\?.*$/, '')
base_uri = URI.parse(conf.base_url)
config_uri = URI.parse(conf.base_url) + conf.update
referer_is_empty = referer == ''
referer_uri = URI.parse(referer) if !referer_is_empty
referer_is_config = !referer_is_empty && config_uri == referer_uri
referer_is_config ||= Regexp.new(updaterb_regexp) =~ referer if !referer_is_empty && updaterb_regexp != ''
is_post = cgi.request_method == 'POST'
given_key = nil
if cgi.valid?('csrf_protection_key')
given_key = cgi.params['csrf_protection_key'][0]
case given_key
when String
else
given_key = given_key.read
end
end
is_key_ok = masterkey != '' && given_key == masterkey
keycheck_ok = !check_key || is_key_ok
referercheck_ok = referer_is_config || (!check_referer && referer_is_empty)
if csrf_protection_get_is_okay then
return if is_post || given_key == nil
else
return if keycheck_ok && referercheck_ok
end
raise Exception.new(<<"EOS")
Security Error: Possible Cross-site Request Forgery (CSRF)
Diagnostics:
- Protection Method is #{ protection_method }
- Mode is #{ self.mode || 'unknown' }
- GET is #{ csrf_protection_get_is_okay ? '' : 'not '}allowed
- Request Method is #{ is_post ? 'POST' : 'not POST' }
- Referer is #{ referer_is_empty ? 'empty' : referer_is_config ? 'config' : 'another page' }
- Given referer: #{h referer_uri.to_s}
- Expected base URI: #{h base_uri.to_s}
- Expected update URI: #{h config_uri.to_s}
- CSRF key is #{ is_key_ok ? 'OK' : given_key ? 'NG (' + (given_key || '') + ')' : 'nothing' }
EOS
end
def load_plugins
super
@plugin.instance_eval("def csrf_protection\n#{(@csrf_protection.untaint || '').dump}\nend;")
end
end
#
# class TDiaryAdmin
# base class of administration
#
class TDiaryAdmin < TDiaryAuthorOnlyBase
def initialize( cgi, rhtml, conf )
super
begin
@date = Time::local( @cgi.params['year'][0].to_i, @cgi.params['month'][0].to_i, @cgi.params['day'][0].to_i )
rescue ArgumentError, NameError
raise TDiaryError, 'bad date'
end
end
end
#
# class TDiaryForm
# show diary append form
#
class TDiaryForm < TDiaryAdmin
def csrf_protection_get_is_okay; true; end
def initialize( cgi, rhtml, conf )
begin
super
rescue TDiaryError
end
@date = Time::now + (@conf.hour_offset * 3600).to_i
title = ''
@io.transaction( @date ) do |diaries|
@diaries = diaries
diary = self[@date]
if diary then
title = diary.title
end
DIRTY_NONE
end
@diary = @io.diary_factory( @date, title, '', @conf.style )
end
end
#
# class TDiaryEdit
# show edit diary form
#
class TDiaryEdit < TDiaryAdmin
def csrf_protection_get_is_okay; true; end
def initialize( cgi, rhtm, conf )
super
@io.transaction( @date ) do |diaries|
@diaries = diaries
@diary = self[@date]
if @diary then
@conf.style = @diary.style
else
@diary = @io.diary_factory( @date, '', '', @conf.style )
end
DIRTY_NONE
end
end
end
#
# class TDiaryPreview
# preview diary
#
class TDiaryPreview < TDiaryAdmin
def initialize( cgi, rhtm, conf )
super
@title = @cgi.params['title'][0]
@body = @cgi.params['body'][0]
if @conf.mobile_agent? && String.method_defined?(:encode)
@title.force_encoding(@conf.mobile_encoding)
@body.force_encoding(@conf.mobile_encoding)
end
@title = @conf.to_native( @title )
@body = @conf.to_native( @body )
@old_date = @cgi.params['old'][0]
@hide = @cgi.params['hide'][0] == 'true' ? true : false
@io.transaction( @date ) do |diaries|
@diaries = diaries
diary = @diaries[@date.strftime( '%Y%m%d' )]
@conf.style = diary.style if diary
@diary = @io.diary_factory( @date, @title, @body, @conf.style )
@diary.show( ! @hide )
DIRTY_NONE
end
end
def eval_rhtml( prefix = '' )
begin
@show_result = true
r = do_eval_rhtml( prefix )
rescue PluginError, SyntaxError, ArgumentError
@exception = $!.dup
@show_result = false
r = super
end
r
end
end
#
# class TDiaryUpdate
# super class of diary saving classes
#
class TDiaryUpdate < TDiaryAdmin
def initialize( cgi, rhtml, conf )
@title = cgi.params['title'][0]
@body = cgi.params['body'][0]
if conf.mobile_agent? && String.method_defined?(:encode)
@title.force_encoding(conf.mobile_encoding)
@body.force_encoding(conf.mobile_encoding)
end
@title = conf.to_native( @title )
@body = conf.to_native( @body )
@hide = cgi.params['hide'][0] == 'true' ? true : false
super
end
protected
def do_eval_rhtml( prefix )
super
@plugin.instance_eval { update_proc }
anchor_str = @plugin.instance_eval( %Q[anchor "#{@diary.date.strftime('%Y%m%d')}"].untaint )
@io.clear_cache( /(latest|#{@date.strftime( '%Y%m' )})/ )
raise ForceRedirect::new( "#{@conf.index}#{anchor_str}" )
end
end
#
# class TDiaryAppend
# append diary
#
class TDiaryAppend < TDiaryUpdate
def initialize( cgi, rhtml, conf )
begin
super
rescue TDiaryError
@date = newdate
end
@author = @conf.multi_user ? @cgi.remote_user : nil
@io.transaction( @date ) do |diaries|
@diaries = diaries
@diary = self[@date] || @io.diary_factory( @date, @title, '', @conf.style )
self << @diary.append( @body, @author )
@diary.set_title( @title ) unless @title.empty?
@diary.show( ! @hide )
DIRTY_DIARY
end
end
protected
def newdate
Time::now + (@conf.hour_offset * 3600).to_i
end
end
#
# class TDiaryReplace
# replace diary
#
class TDiaryReplace < TDiaryUpdate
def initialize( cgi, rhtm, conf )
super
old_date = @cgi.params['old'][0]
@io.transaction( @date ) do |diaries|
@diaries = diaries
@diary = self[@date]
if @diary then
if @date.strftime( '%Y%m%d' ) != old_date then
@diary.append( @body, @append )
@diary.set_title( @title ) if @title.length > 0
else
@diary.replace( @date, @title, @body )
end
else
@diary = @io.diary_factory( @date, @title, @body, @conf.style )
end
@diary.show( ! @hide )
self << @diary
DIRTY_DIARY
end
end
end
#
# class TDiaryShowComment
# change visible mode of comments
#
class TDiaryShowComment < TDiaryAdmin
def initialize( cgi, rhtml, conf )
super
@io.transaction( @date ) do |diaries|
@diaries = diaries
dirty = DIRTY_NONE
@diary = self[@date]
if @diary then
idx = 0
@diary.each_comment do |com|
com.show = @cgi.params[(idx += 1).to_s][0] == 'true' ? true : false;
end
self << @diary
@io.clear_cache( /(latest|#{@date.strftime( '%Y%m' )})/ )
dirty = DIRTY_COMMENT
end
dirty
end
end
def eval_rhtml( prefix = '' )
load_plugins
@plugin.instance_eval { update_proc }
super
end
end
#
# class TDiaryFormPlugin
# show edit diary form after calling form plugin.
#
class TDiaryFormPlugin < TDiaryAuthorOnlyBase
def initialize( cgi, rhtm, conf )
super
if @cgi.valid?( 'date' ) then
if @cgi.params['date'][0].kind_of?( String ) then
date = @cgi.params['date'][0]
else
date = @cgi.params['date'][0].read
end
@date = Time::local( *date.scan( /(\d{4})(\d\d)(\d\d)/ )[0] )
else
@date = Time::now + (@conf.hour_offset * 3600).to_i
@diary = @io.diary_factory( @date, '', '', @conf.style )
end
@io.transaction( @date ) do |diaries|
@diaries = diaries
@diary = self[@date]
if @diary then
@conf.style = @diary.style
else
@diary = @io.diary_factory( @date, '', '', @conf.style )
end
DIRTY_NONE
end
end
end
#
# class TDiaryConf
# show configuration form
#
class TDiaryConf < TDiaryAuthorOnlyBase
def csrf_protection_get_is_okay; true; end
def initialize( cgi, rhtml, conf )
super
@key = @cgi.params['conf'][0] || ''
end
end
#
# class TDiarySaveConf
# save configuration
#
class TDiarySaveConf < TDiaryConf
def csrf_protection_get_is_okay; false; end
def initialize( cgi, rhtml, conf )
super
end
def eval_rhtml( prefix = '' )
r = super
begin
@conf.save
@io.clear_cache
rescue
@error = [$!.dup, $@.dup]
end
r
end
end
#
# class TDiaryView
# base of view mode classes
#
class TDiaryView < TDiaryBase
def initialize( cgi, rhtml, conf )
super
unless referer_filter( @cgi.referer )
def @cgi.referer; nil; end
end
# save referer to latest
if (!@conf.referer_day_only or (@cgi.params['date'][0] and @cgi.params['date'][0].length == 8)) and @cgi.referer then
ym = latest_month
@date = ym ? Time::local( ym[0], ym[1] ) : Time::now
@io.transaction( @date ) do |diaries|
@diaries = diaries
dirty = DIRTY_NONE
@diaries.keys.sort.reverse_each do |key|
@diary = @diaries[key]
break if @diary.visible?
end
if @diary then
@diary.add_referer( @cgi.referer )
dirty = DIRTY_REFERER
end
dirty
end
@date = @diary.date if @diary
end
end
def last_modified
lm = Time::at( 0 )
@diaries.each_value do |diary|
lmd = diary.last_modified
lm = lmd if lm < lmd and diary.visible?
end
lm
end
protected
def each_day
@diaries.keys.sort.each do |date|
diary = @diaries[date]
next unless diary.visible?
yield diary
end
end
def latest_month
result = nil
calendar
@years.keys.sort.reverse_each do |year|
@years[year.to_s].sort.reverse_each do |month|
result = [year, month]
break
end
break
end
result
end
def oldest_month
result = nil
calendar
@years.keys.sort.each do |year|
@years[year.to_s].sort.each do |month|
result = [year, month]
break
end
break
end
result
end
end
#
# class TDiaryDay
# show day mode view
#
class TDiaryDay < TDiaryView
def initialize( cgi, rhtm, conf )
super
begin
# time is noon for easy to calc leap second.
@date = Time::local( *@cgi.params['date'][0].scan( /^(\d{4})(\d\d)(\d\d)$/ )[0] ) + 12*60*60
load( @date )
rescue ArgumentError, NameError
raise TDiaryError, 'bad date'
end
@diary = nil if @diary and not @diary.visible?
end
def last_modified
@diary ? @diary.last_modified : Time::at( 0 )
end
def eval_rhtml( prefix = '' )
if not @diary and @conf.bot?
raise NotFound
else
super(prefix)
end
end
protected
def load( date )
if not @diary or (@diary.date.dup + 12*60*60).gmtime.strftime( '%Y%m%d' ) != date.dup.gmtime.strftime( '%Y%m%d' ) then
@io.transaction( date ) do |diaries|
@diaries = diaries
dirty = DIRTY_NONE
@diary = self[date]
if @diary and @cgi.referer then
@diary.add_referer( @cgi.referer )
dirty = DIRTY_REFERER
end
dirty
end
else
@diary = self[date]
end
end
def cookie_name
@cgi.cookies['tdiary'][0] or ''
end
def cookie_mail
@cgi.cookies['tdiary'][1] or ''
end
end
#
# class TDiaryComment
# save a comment
#
class TDiaryComment < TDiaryDay
def initialize( cgi, rhtml, conf )
super
end
protected
def load( date )
@date = date
@name = @cgi.params['name'][0]
@mail = @cgi.params['mail'][0]
@body = @cgi.params['body'][0]
if @conf.mobile_agent? && String.method_defined?(:encode)
@name.force_encoding(conf.mobile_encoding)
@body.force_encoding(conf.mobile_encoding)
end
@name = @conf.to_native( @name )
@body = @conf.to_native( @body )
@comment = Comment::new( @name, @mail, @body )
dirty = DIRTY_NONE
@io.transaction( @date ) do |diaries|
@diaries = diaries
@diary = self[@date]
if @diary and comment_filter( @diary, @comment ) then
@diary.add_comment( @comment )
dirty = DIRTY_COMMENT
cookie_path = File::dirname( @cgi.script_name )
cookie_path += '/' if cookie_path !~ /\/$/
@cookies << CGI::Cookie::new( {
'name' => 'tdiary',
'value' => [@name,@mail],
'path' => cookie_path,
'expires' => Time::now.gmtime + 90*24*60*60 # 90days
} )
@io.clear_cache( /(latest|#{@date.strftime( '%Y%m' )})/ )
else
@comment = nil
end
dirty
end
end
def do_eval_rhtml( prefix )
load_plugins
@plugin.instance_eval { update_proc } if @comment
anchor_str = @plugin.instance_eval( %Q[anchor "#{@diary.date.strftime('%Y%m%d')}"].untaint )
raise ForceRedirect::new( "#{@conf.index}#{anchor_str}#c#{'%02d' % @diary.count_comments( true )}" )
end
end
#
# class TDiaryMonthBase
# base of TDiaryMonth and TDiaryNYear
#
class TDiaryMonthBase < TDiaryView
def eval_rhtml( prefix = '' )
if @diaries.empty? and @conf.bot?
raise NotFound
else
super(prefix)
end
end
end
#
# class TDiaryMonth
# show month mode view
#
class TDiaryMonth < TDiaryMonthBase
def initialize( cgi, rhtml, conf )
super
begin
date = Time::local( *@cgi.params['date'][0].scan( /^(\d{4})(\d\d)$/ )[0] )
d1 = @date.dup.gmtime if @date
d2 = date.dup.gmtime
if not @date or d1.year != d2.year or d1.month != d2.month then
@date = date
@io.transaction( @date ) do |diaries|
@diaries = diaries
@diary = @diaries[@diaries.keys.sort.reverse[0]]
DIRTY_NONE
end
end
rescue ArgumentError, NameError
raise TDiaryError, 'bad date'
end
end
end
#
# class TDiaryNYear
# show nyear mode view
#
class TDiaryNYear < TDiaryMonthBase
def initialize(cgi, rhtml, conf)
super
@diaries = {}
month, day = @cgi.params['date'][0].scan(/^(\d\d)(\d\d)$/)[0]
nyear(month).each do |y, m|
@date = Time::local(y, m)
@io.transaction(@date) do |diaries|
ymd = y + m + day
@diaries[ymd] = diaries[ymd] if diaries[ymd]
DIRTY_NONE
end
end
end
protected
def nyear(month)
r = []
calendar
@years.keys.reverse_each do |year|
r << [year, month] if @years[year].include? month
end
r
end
end
#
# class TDiaryMonthWithoutFilter
#
class TDiaryMonthWithoutFilter < TDiaryMonth
def referer_filter(referer); end
end
#
# class TDiaryLatest
# show latest mode view
#
class TDiaryLatest < TDiaryView
def initialize( cgi, rhtml, conf )
super
if @cgi.params['date'][0] then
ym = [@cgi.params['date'][0][0,4].to_i, @cgi.params['date'][0][4,2].to_i]
@date = nil
else
ym = latest_month
end
unless @date then
@date = ym ? Time::local( ym[0], ym[1] ) : Time::now
@io.transaction( @date ) do |diaries|
@diaries = diaries
if @cgi.params['date'][0] then
@diary = @diaries[@cgi.params['date'][0][0,8]]
@date = @diary.date if @diary
end
unless @diary then
@diaries.keys.sort.reverse_each do |d|
diary = @diaries[d]
if diary.visible?
@diary = diary
break
end
end
@diary = @diaries[@diaries.keys.sort.reverse[0]] unless @diary
@date = @diary.date if @diary
end
DIRTY_NONE
end
end
if ym then
# read +2 days for calc ndays.prev in count_diaries method
limit = limit_size( @conf.latest_limit ) + 2
# read next month data until limit
y = ym[0].to_i
m = ym[1].to_i
latest = latest_month
diaries_tmp = {}.update( @diaries )
diaries_size = count_diaries_after( diaries_tmp )
while ( latest and diaries_size < limit )
date = if m == 12 then
Time::local( y += 1, m = 1 )
else
Time::local( y, m += 1 )
end
break if date > Time::local( *latest )
@io.transaction( date ) do |diaries|
diaries_tmp.update( diaries )
diaries_size = count_diaries_after( diaries_tmp )
DIRTY_NONE
end
end