File: plugin.rb

package info (click to toggle)
hiki 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,812 kB
  • ctags: 2,033
  • sloc: ruby: 14,572; lisp: 926; sh: 19; makefile: 16
file content (413 lines) | stat: -rw-r--r-- 9,641 bytes parent folder | download | duplicates (3)
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
# $Id: plugin.rb,v 1.37 2008-02-12 15:06:08 hiraku Exp $
# Copyright (C) 2002-2003 TAKEUCHI Hitoshi <hitoshi@namaraii.com>
# Copyright (C) 2004-2005 Kazuhiko <kazuhiko@fdiary.net>
#
# TADA Tadashi <sho@spc.gr.jp> holds the copyright of Config class.

require 'cgi' unless Object.const_defined?(:Rack)
require 'uri'
require 'erb'
require 'hiki/util'

module Hiki
  class PluginError < StandardError; end

  class Plugin
    include Hiki::Util
    attr_reader   :toc_f, :plugin_command
    attr_accessor :text, :title, :cookies, :user, :data, :session_id

    TOC_STRING = "\000\000[Table of Contents]"

    def initialize( options, conf )
      @options      = options
      @conf         = conf
      set_tdiary_env

      @cookies = []
      @plugin_method_list = []
      @header_procs     = []
      @update_procs     = []
      @delete_procs     = []
      @body_enter_procs = []
      @body_leave_procs = []
      @page_attribute_procs = []
      @footer_procs     = []
      @edit_procs       = []
      @form_procs       = []
      @menu_procs       = []
      @conf_keys        = []
      @conf_procs       = {}
      @mode             = ''

      options.each do |opt, val|
        instance_variable_set("@#{opt}", val) unless opt.index('.')
      end

      @toc_f            = false
      @context          = nil
      @plugin_command   = []
      @plugin_menu      = []
      @text             = ''

      @mode = 'conf' if @request.params['c'] == 'admin'
      @mode = 'saveconf' if @request.params['saveconf']

      # loading plugins
      @plugin_files = []
      plugin_path = @conf.plugin_path || "#{PATH}/plugin"
      plugin_file = ''
      begin
        Dir::glob( "#{plugin_path}/*.rb" ).sort.each do |file|
          plugin_file = file
          load_plugin( file )
          @plugin_files << file
        end
      rescue Exception
        raise PluginError, "Plugin error in '#{File::basename( plugin_file )}'.\n#{$!}\n#{$!.backtrace[0]}"
      end
    end

    def block_context?
      @context == :block
    end

    def inline_context?
      @context == :inline
    end

    def block_context( &proc )
      in_context( :block, &proc )
    end

    def inline_context( &proc )
      in_context( :inline, &proc )
    end

    def cookie_path
      path = URI.parse(@conf.base_url).path
      if path[-1] == ?/
        path
      else
        File.dirname(path) + '/'
      end
    end

    def header_proc
      r = []
      @header_procs.each do |proc|
        begin
          r << proc.call
        rescue Exception
          r << plugin_error( 'header_proc', $! )
        end
      end
      r.join.chomp
    end

    def update_proc
      @update_procs.each do |proc|
        begin
          proc.call
        rescue Exception
        end
      end
    end

    def delete_proc
      @delete_procs.each do |proc|
        begin
          proc.call
        rescue Exception
        end
      end
    end

    def body_enter_proc
      r = []
      @body_enter_procs.each do |proc|
        begin
          r << proc.call( @date )
        rescue Exception
          r << plugin_error( 'body_enter_proc', $! )
        end
      end
      r.join
    end

    def body_leave_proc
      r = []
      @body_leave_procs.each do |proc|
        begin
          r << proc.call( @date )
        rescue Exception
          r << plugin_error( 'body_leave_proc', $! )
        end
      end
      r.join
    end

    def page_attribute_proc
      r = []
      @page_attribute_procs.each do |proc|
        begin
          r << proc.call( @date )
        rescue Exception
          r << plugin_error( 'page_attribute_proc', $! )
        end
      end
      r.join
    end

    def footer_proc
      r = []
      @footer_procs.each do |proc|
        begin
          r << proc.call
        rescue Exception
          r << plugin_error( 'footer_proc', $! )
        end
      end
      r.join
    end

    def edit_proc
      r = []
      @edit_procs.each do |proc|
        begin
          r << proc.call
        rescue Exception
          r << plugin_error( 'edit_proc', $! )
        end
      end
      r.join
    end

    def form_proc
      r = []
      @form_procs.each do |proc|
        begin
          r << proc.call
        rescue Exception
          r << plugin_error( 'form_proc', $! )
        end
      end
      r.join
    end

    def menu_proc
      r = []
      @menu_procs.each do |proc|
        begin
          r << proc.call
        rescue Exception
          r << plugin_error( 'menu_proc', $! )
        end
      end
      r.compact
    end

    def add_cookie( cookie )
      @cookies << cookie
    end

    def singleton_method_added(name)
      @defined_method_list.push(name)
    end

    def load_file(filename)
      open(filename) do |src|
        instance_eval(src.read.untaint, filename, 1)
      end
    end

    def send(name, *args)
      name = name.intern if name.is_a?(String)
      if not name.is_a?(Symbol)
        raise ArgumentError, "#{name.inspect} is not a symbol"
      end
      if not @plugin_method_list.include?(name)
        method_missing(name, *args)
      else
        __send__(name, *args)
      end
    end

    def each_conf_key
      @conf_keys.each do |key|
        yield key
      end
    end

    def conf_proc( key )
      r = ''
      label, block = @conf_procs[key]
      r = block.call if block
      r
    end

    def conf_label( key )
      label, block = @conf_procs[key]
      label
    end

    def load_plugin( file )
      file.untaint
      @defined_method_list = []
      @export_method_list = nil
      @resource_loaded = false
      dirname, basename = File.split( file )
      [@conf.lang, 'en', 'ja'].uniq.each do |lang|
        begin
          load_file( File.join( dirname, lang, basename ) )
          @resource_loaded = true
          break
        rescue IOError, Errno::ENOENT
        end
      end
      load_file( file )
      if @export_method_list
        @plugin_method_list.concat(@export_method_list)
      else
        @plugin_method_list.concat(@defined_method_list)
      end
    end

    def load( page )
      @db.load( page )
    end

    def load_backup( page )
      @db.load_backup( page )
    end

    def save( page, src, md5, update_timestamp = true, filtering = true )
      return false if filtering and Filter.new_page_is_spam?(page, src)

      src.gsub!(/\r/, '')
      src.sub!(/\A\n*/, '')
      src.sub!(/\n*\z/, "\n")
      result = @db.store(page, src, md5, update_timestamp)
      if result
        @db.set_attribute( page, :editor => @user )
        @db.delete_cache( page )
        begin
          update_proc
        rescue Exception
        end
      end
      result
    end

    def admin?
      ( @user == @conf.admin_name ) || @conf.password.empty?
    end

    def login( name, password )
      return if @user
      return nil unless password
      name ||= @conf.admin_name
      if @conf.password.empty? || password.crypt( @conf.password ) == @conf.password && name == @conf.admin_name
        @user = @conf.admin_name
      elsif @conf['user.list']
        if @conf['user.list'].has_key?(name) && @conf['user.list'][name] == password.crypt(@conf['user.list'][name])
          @user = name
        end
      end
    end

    private

    def in_context(context)
      begin
        @context = context
        yield
      ensure
        @context = nil
      end
    end

    def export_plugin_methods(*names)
      @export_method_list = names.collect do |name|
        name = name.intern if name.is_a?(String)
        if not name.is_a?(Symbol)
          raise TypeError, "#{name.inspect} is not a symbol"
        end
        name
      end
    end

    def add_header_proc( block = Proc.new )
      @header_procs << block
    end

    def add_footer_proc( block = Proc.new )
      @footer_procs << block
    end

    def add_update_proc( block = Proc.new )
      @update_procs << block
    end

    def add_delete_proc( block = Proc.new )
      @delete_procs << block
    end

    def add_body_enter_proc( block = Proc.new )
      @body_enter_procs << block
    end

    def add_page_attribute_proc( block = Proc.new )
      @page_attribute_procs << block
    end

    def add_body_leave_proc( block = Proc.new )
      @body_leave_procs << block
    end

    def add_edit_proc( block = Proc.new )
      @edit_procs << block
    end

    def add_form_proc( block = Proc.new )
      @form_procs << block
    end

    def add_menu_proc( block = Proc.new )
      @menu_procs << block
    end

    def add_plugin_command(command, display_text, option = {})
      @plugin_command << command
      @plugin_menu    << {:command => command,
                          :display_text => display_text,
                          :option => option} if display_text
      nil
    end

    def add_conf_proc( key, label, block = Proc.new )
      return unless @mode =~ /^(conf|saveconf)$/
      @conf_keys << key unless @conf_keys.index( key )
      @conf_procs[key] = [label, block]
    end

    def set_tdiary_env
      @date             = Time.now
      @cookies          = []

      @options['cache_path']  = @conf.cache_path
      @options['mode']        = "day"
#      @options['author_name'] = @conf.author_name || 'anonymous'
#      @options['author_mail'] = @conf.mail
#      @options['index_page']  = @conf.index_page
#      @options['html_title']  = @conf.site_name
      @options['years']       = {}
      @options['diaries']     = nil,
      @options['date']        = Time.now

      %w(cache_path mode years diaries date).each do |p|
        instance_variable_set("@#{p}", @options[p])
      end
    end
  end
end