diary/cgi.rb

135 lines
3.2 KiB
Ruby
Raw Normal View History

#!/usr/bin/ruby -w
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2021 OKTET Labs Ltd. All rights reserved.
#
# Cgi for Diary Management Application
#
# "." is removed from LOAD_PATH in Ruby 1.9
# Backporting require_relative to Ruby 1.8
# See http://steveklabnik.github.io/require_relative/require_relative.html
#
unless Object.new.respond_to?(:require_relative, true)
def require_relative(relative_feature)
file = caller.first.split(/:\d/,2).first
raise LoadError, "require_relative is called in #{$1}" if
/\A\((.*)\)/ =~ file
require File.expand_path(relative_feature, File.dirname(file))
end
end
require 'cgi'
require 'ldap'
require_relative 'diary_env'
require 'dbi'
require 'amrita/template'
include Amrita
class AmritaTemplate < Amrita::TemplateText
def initialize(filename)
# Use TemplateText instead of TemplateFile to avoid problem
# in amrita/node.rb with tainted template strings, because
# they have been read from external file
super(IO.read(filename).untaint)
self.amrita_id = :_id
self.asxml = true
end
end
require_relative 'sql_cache'
require_relative 'project'
require_relative 'diary'
require_relative 'mycgi'
$SAFE = 1
$DEBUG = 1
class NeedConfirm < Exception
def initialize(code = nil)
@code = code
end
attr :code
end
#
# Initialize global variables
#
# CGI default parameters
cgi_defaults = {
"do" => "diary",
"prj" => {
"list" => {
"customer" => ["*"],
"prj_status" => [], #Project::STATUS_DEFAULT,
"edit" => []
},
"action" => {}
},
"diary" => {
"list" => {
"start" => MyCGI::date2hash(Date.today),
"end" => MyCGI::date2hash(Date.today),
"prj_id" => ["*"],
"customer" => ["*"],
"who" => ["*"],
"newest_first" => ["off"],
"just_stats" => ["off"],
"edit" => []
},
"action" => {}
},
"confirm" => []
}
env = DiaryEnv.instance
env.user = ENV["REMOTE_USER"]
# Customize default parameters
# TODO: make this user-defined
cgi_defaults["diary"]["list"]["start"]["day"] = 1
cgi_defaults["diary"]["list"]["who"] = [env.user.uid] if env.user.local?
# CGI object
cgi = MyCGI.new("post", cgi_defaults, ["list", "confirm"], ["descr"], "html4Tr")
prj = ProjectUI.new(cgi)
diary = DiaryUI.new(cgi)
cgi.out("charset" => "utf-8") do begin
env.confirmation = cgi.tree_params["confirm"]
# Process cgi parameters
prj.action
diary.action
# Output html data
if cgi.tree_params["do"] == "project"
prj.show
elsif cgi.tree_params["do"] == "diary"
diary.show
else
""
end
rescue NeedConfirm => detail
tmpl = AmritaTemplate.new("confirm.html")
s = String.new
tmpl.expand(s, {
:message => noescape{$!},
:hiddens => cgi.hiddens(cgi.tree_flatten([])),
:code => cgi.hiddens([["confirm", detail.code]])
})
rescue RuntimeError
tmpl = AmritaTemplate.new("error.html")
s = String.new
tmpl.expand(s, {
:message => noescape{$!},
:debug => $!.backtrace.join("\n")
})
end
end