2 # -*- coding: utf-8 -*-
4 #= Atom Feed 1.0を管理するWEBアプリケーション
6 #Autohr:: Kureha Hisame (http://lunardial.sakura.ne.jp/) & Yui Naruse (http://airemix.com/)
8 #Copyright:: Copyright 2009 FeedBlog Project (http://sourceforge.jp/projects/feedblog/)
14 require "rexml/document"
27 XMLPATH = "./../lunardial/xml/"
29 LISTXMLPATH = "#{XMLPATH}loglist.xml"
30 # FeedBlog上の表示ページからログ格納ディレクトリまでのパス
34 # ファイルマネージャー機能を使用するならtrue
36 # ファイルマネージャー機能スクリプト(filemanager.rb)のパス
37 FILEMANAGER = "./filemanager.rb"
38 # XMLに書き込む際、改行部分を<br>のまま保持するか、改行記号に直すか
42 APPVERSION = "- FeedGenerator for Ruby version 2.0.0.0 -<br>Copyright(c) 2009 Kureha.H (<a href=\"http://lunardial.sakura.ne.jp/\" target=\"_blank\">http://lunardial.sakura.ne.jp/</a>) & Yui Naruse (<a href=\"http://airemix.com/\" target=\"_blank\">http://airemix.com/</a>)"
44 APPTITLE = "FeedGenerator for Ruby version 2.0.0.0"
52 # ruby-1.9.x以降ではファイルを開いた際、エンコードの指定を行わないとエラーの原因になります。
53 # ただしruby-1.8.6以前はエンコードの指定に対応していないため、独自メソッドを定義してファイルの入出力を行います。
55 # _arg[0]_ :: 入出力を行うファイルのパス
56 # _arg[1]_ :: モードの指定。例 : w:utf-8(書き込みモード・UTF-8エンコードでファイルを開く)
59 arg[1] = mode[/[^:]+/] if mode && RUBY_VERSION < "1.8.7" && mode.include?(':')
61 f.flock(/\A\w+r/ =~ mode ? File::LOCK_SH : File::LOCK_EX)
85 # = Feed/Entryのスーパークラス
87 # 入出力用のメソッドなど、共通する機能を提供します
91 # _hash_ :: 値を格納したhash配列。superfeed.new(CGI::Session.new(new CGI).params)のようにして使う。
97 @paramlist.each do |key|
98 val = hash[key.to_sym] || hash[key.to_s]
101 val.gsub!(/\r\n|\r/, "\n")
102 @attr[key.to_sym] = CGI.escapeHTML(val)
105 @attr[key.to_sym] = ""
109 # 可視・不可視を示すハッシュキーを格納する配列です
112 # ハッシュキーの日本語説明を格納する配列です
117 attr_reader :attr, :paramlist, :name, :display
119 # 内部の@attrハッシュにアクセスする手段を提供するメソッドです
121 # AbstractEntry.attr[:title]にアクセスしたい場合はAbstractEntry.titleで可能になります。
122 # AbstractEntry.send("title")という方法でもアクセス可能です。
123 def method_missing(methname, *args)
124 methname = methname.to_s
126 if methname[-1] == ?=
128 raise ArgumentError, "wrong number of arguments (#{args.length} for 1)" unless args.length == 1
130 methname = @paramlist.find{|par|par == methname}
131 return @attr[methname.to_sym] = args[0] if methname
134 raise ArgumentError, "wrong number of arguments (#{args.length} for 0)" unless args.empty?
135 return @attr[methname.to_sym] if @attr.key?(methname.to_sym)
137 # attr上にキーがない値を入れようとした場合はNoMethodError
146 @attr[key.to_sym] = value
152 # Feedの基礎情報を保有するクラスです
153 class Feed < AbstractEntry
156 # _hash_ :: 値を格納したhash配列。feed.new(CGI::Session.new(new CGI).params)のようにして使います。
162 @paramlist = ["feedattr", "title", "subtitle", "self", "url", "updated", "feedid", "rights", "aname", "amail", "others"]
164 # AbstractEntryのinitializeメソッドを呼び出し、値を@attr配列に格納します
167 # 可視・不可視を示すハッシュキーを格納する配列です
168 @display = {"feedattr" => "none", "title" => "", "subtitle" => "",
169 "self" => "", "url" => "",
170 "updated" => "none", "feedid" => "",
171 "rights" => "", "aname" => "",
172 "amail" => "", "others" => "none"}
174 # デバッグモードの場合、全ての入力要素を表示します
176 @display.each do |key, val|
181 # ハッシュキーの日本語説明を格納する配列です
182 @name = {"feedattr" => "feedの保持している属性", "title" => "ウェブページのタイトル", "subtitle" => "ウェブページの簡単な説明",
183 "self" => "このXMLファイルのURL", "url" => "ウェブページのURL",
184 "updated" => "XMLファイルの最終更新日", "feedid" => "あなたのページ独自のID",
185 "rights" => "ページの著作権表記", "aname" => "ページの製作者",
186 "amail" => "ページ製作者のメールアドレス", "others" => "その他の要素"}
190 # Atom XMLファイルを読み込んで解析し、等価なFeedオブジェクトを返却するメソッドです
192 # _path_ :: Atom XMLファイルのパス
193 def self.readxml(path)
196 doc = REXML::Document.new(myopen(path, "r:utf-8"){|f|f.read})
202 doc.elements["feed"].attributes.each_attribute do |attr|
203 xmlattr.push("#{attr.to_string} ")
205 xml[:feedattr] = xmlattr.join("\n").gsub(/"/, "'")
207 # XML解析部分です。各element毎に判定を行います
208 doc.elements.each("feed") do |elm|
209 elm.elements.each { |child|
213 xml[:feedid] = child.text
214 when "title", "subtitle", "updated", "rights"
215 xml[child.name.to_sym] = child.text
217 child.elements.each do |gchild|
220 xml[:aname] = gchild.text
222 xml[:amail] = gchild.text
226 child.attributes.each do |k, v|
229 xml[:self] = child.attributes["href"]
230 elsif v == "alternate"
231 xml[:url] = child.attributes["href"]
238 # 上記判定以外の全要素は配列に格納します
239 others.push(child.to_s)
248 xml[:others] = others.join("\n")
253 # 内部に保持している情報を、Atom Feed1.0形式の文字列に出力するメソッドです
257 buf.push("<feed #{@attr[:feedattr]}>")
258 buf.push("<title type=\"text\">#{@attr[:title]}</title>")
259 buf.push("<subtitle type=\"text\">#{@attr[:subtitle]}</subtitle>")
260 buf.push("<link rel=\"self\" type=\"application/atom+xml\" href=\"#{@attr[:self]}\" />")
261 buf.push("<link rel=\"alternate\" type=\"text/html\" href=\"#{@attr[:url]}\" />")
262 buf.push("<updated>#{Time.now.iso8601}</updated>")
263 buf.push("<id>#{@attr[:feedid]}</id>")
264 buf.push("<rights type=\"text\">#{@attr[:rights]}</rights>")
266 buf.push("\t<name>#{@attr[:aname]}</name>")
267 buf.push("\t<email>#{@attr[:amail]}</email>")
268 buf.push("</author>")
269 buf.push("#{CGI.unescapeHTML(@attr[:others])}") if @attr[:others] != ""
271 return buf.join("\n")
275 def self.update(path, feed)
276 entrylist = Entry.readxml(path)
277 Feed.to_xml(path, feed, entrylist)
284 # _feed_ :: Feedオブジェクト
285 # _entry_ :: Entryオブジェクトの配列
286 def self.to_xml(path, feed, entrylist)
288 buf.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
289 buf.push("#{feed.to_s}\n")
290 entrylist.each { |entry|
291 buf.push("#{entry.to_s}\n")
295 myopen(path, "w") do |f|
296 f.print buf.join("\n")
304 # Entryの基礎情報を保有するクラスです
305 class Entry < AbstractEntry
308 # _hash_ :: 値を格納したhash配列。entry.new(CGI::Session.new(new CGI).params)のようにして使います。
314 @paramlist = ["entryid", "title", "summary", "published", "updated", "url", "content", "others"]
316 # AbstractEntryのinitializeメソッドを呼び出し、値を@attr配列に格納します
319 # 可視・不可視を示すハッシュキーを格納する配列です
320 @display = {"entryid" => "none", "title" => "",
321 "summary" => "", "published" => "none",
322 "updated" => "none", "url" => "none",
323 "content" => "", "others"=>"none"}
325 # デバッグモードの場合、全ての入力要素を表示します
327 @display.each do |key, val|
332 # ハッシュキーの日本語説明を格納する配列です
333 @name = {"entryid" => "記事固有のID", "title" => "記事のタイトル",
334 "summary" => "記事の簡単な説明", "published" => "記事の出版時刻",
335 "updated" => "記事の更新時刻", "url" => "記事へのURLアドレス",
336 "content" => "記事の本文", "others"=>"その他の項目"}
339 # Atom XMLファイルを読み込んで解析し、等価なEntryオブジェクト配列を返却するメソッドです
341 # _path_ :: Atom XMLファイルのパス
342 def self.readxml(path)
345 doc = REXML::Document.new(myopen(path, "r:utf-8"){|f|f.read})
349 # XML解析部分です。各element毎に判定を行います
350 doc.elements.each("feed/entry") do |elm|
353 elm.elements.each do |child|
357 xml[:entryid] = child.text
359 xml[:url] = child.attributes["href"]
360 when "title", "summary", "summary", "published", "updated", "content"
361 xml[child.name.to_sym] = child.text
363 # 上記判定以外の全要素は配列に格納します
364 others.push(child.to_s)
370 xml[:others] = others.join("\n")
371 entrylist.push(Entry.new(xml))
378 def self.insert(path, entry)
379 feed = Feed.readxml(path)
380 entrylist = Entry.readxml(path)
381 entrylist.unshift entry
383 Feed.to_xml(path, feed, entrylist)
389 def self.update(path, entry)
390 feed = Feed.readxml(path)
391 entrylist = Entry.readxml(path)
394 entrylist.each_with_index { |e, i|
395 if e.entryid == entry.entryid
400 Feed.to_xml(path, feed, entrylist) if successed
406 def self.select(path, entryid)
407 feed = Feed.readxml(path)
408 entrylist = Entry.readxml(path)
411 entrylist.each_with_index { |e, i|
412 entry = entrylist[i].dup if e.entryid == entryid
419 def self.delete(path, entryid)
420 feed = Feed.readxml(path)
421 entrylist = Entry.readxml(path)
425 entrylist.each_with_index { |e, i|
426 if e.entryid == entryid
433 entrylist.delete_at delete_index
434 Feed.to_xml(path, feed, entrylist)
440 # データソースから読み取ったHTMLを、エディタで編集可能な形式に変換するメソッドです
441 def content_for_generator
442 str = @attr[:content].dup
444 str.gsub!(/(<\/(?:p|h\d|div)(?:>|>))\n/i, '\1')
445 str.gsub!(/\n/, '<br>') if REPLACEBRTAG
446 str.gsub!(/(<(?:(?!>).)*?)#{Regexp.escape(FEEDXMLDIR)}/) { "#$1#{XMLPATH}" }
450 # エディタで編集されたCONTENT要素を、データソースに書き込める形式に変換するメソッドです
452 str = @attr[:content].dup
453 str = CGI.unescapeHTML(str)
455 str.gsub!(/(\r\n|\n)/, "")
456 str.gsub!(/<br>/i, "\n") if REPLACEBRTAG
457 str.gsub!(/(<br>|<\/p>|<\/h\d>|<\/div>)(?=[^\n])/i) { "#$1\n" } unless REPLACEBRTAG
458 str.gsub!(/(<[^>]*?)#{Regexp.escape(XMLPATH)}/) { "#$1#{FEEDXMLDIR}" }
462 # 確認画面で表示されるCONTENT要素を生成するメソッドです
464 str = @attr[:content].dup
465 str = CGI.unescapeHTML(str)
467 str.gsub!(/<br>/i, "\n") if REPLACEBRTAG
468 str.gsub!(/(<[^>]*?)#{Regexp.escape(FEEDXMLDIR)}/) { "#$1#{XMLPATH}" }
472 # 内部に保持している情報を、Atom Feed1.0形式の文字列に出力するメソッドです
476 buf.push("<id>#{@attr[:entryid]}</id>")
477 buf.push("<title>#{@attr[:title]}</title>")
478 buf.push("<summary>#{@attr[:summary]}</summary>")
479 buf.push("<published>#{@attr[:published]}</published>")
480 buf.push("<updated>#{@attr[:updated]}</updated>")
481 buf.push("<link href=\"#{@attr[:url]}\" />")
482 buf.push("<content type=\"html\">#{@attr[:content]}</content>")
483 buf.push("#{CGI.unescapeHTML(@attr[:others])}") if @attr[:others] != ""
486 return buf.join("\n")
492 # テンプレートファイル(*.erb)を読み込み、管理するクラスです
496 # _template_ :: テンプレートファイル(*.erb)のパス
497 # _binding_ :: binding変数
498 def initialize(template, binding)
499 @erb = ERB.new(myopen(template, "r:utf-8") {|f| f.read}, nil, "-")
503 # テンプレートファイルの文字列を返却するメソッドです
505 @erb.result(@binding)
511 # loglist.xmlにかかわる入出力を行います
515 # _display_ :: 画面表示用文字の配列
516 # _path_ :: XMLファイルパスの配列
517 # _logpath_ :: loglist.xmlのパス
518 def initialize(display, path, logpath)
524 attr_accessor :display, :path, :logpath
526 # loglist.xmlファイルを読み込んで解析し、LogListオブジェクトを返却するメソッドです
528 # _logpath_ :: loglist.xmlへのパス
529 def LogList.readxml(logpath)
533 myopen(logpath, "r:utf-8") { |f|
537 doc = REXML::Document.new(lines.join("\n"))
541 doc.elements.each("list/file") { |elm|
542 elm.elements.each {|child|
545 @display.push(child.text)
547 @path.push(child.text)
554 return LogList.new(@display, @path, logpath)
557 # loglist.xmlにオブジェクトの内容を反映するメソッドです
561 path.each_with_index do |path, i|
563 buf.push("<display>#{@display[i]}</display>")
564 buf.push("<path>#{@path[i]}</path>")
569 myopen(@logpath, "w") do |f|
570 f.print buf.join("\n")
584 # ファイルの一覧を取得し、ファイル名称を格納した配列を返却します
586 arr = Dir::entries(XMLPATH).sort
595 # _name_ :: 消去するファイル名
597 File.delete(XMLPATH + name.match(/[^\/]*?$/).to_a[0])
602 # _name_ :: アップロードファイルの名称
603 # _img_ :: アップロードファイル
604 def upload(name, img)
605 open(XMLPATH + File.basename(name), "w") do |f|
614 # コントローラ部分に相当する処理を受け持つクラスです
616 def Controller.NormalForm(cgi, session, params, db)
618 # modeとactionの相関図は以下のようになります。
626 session["filepath"] = params["logpath"]
627 db["loglist"] = LogList.readxml(LISTXMLPATH)
628 # 初期状態で選択されるログは「loglist.xml」の最上に位置するログになります
629 session["filepath"] = db["loglist"].path[0] if session["filepath"] == nil
630 db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
631 db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
635 case params["action"]
638 db["newentry"] = Entry.new(params)
639 db["newentry"].content = db["newentry"].content_for_blog
642 successed = Entry.insert(XMLPATH + File.basename(session["filepath"]), Entry.new(params))
644 db["error"] = "日記の新規追加に失敗しました。"
645 params["mode"] = "error"
649 db["newentry"] = Entry.new(params)
651 # New Diary - Default
652 db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
653 db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
658 case params["action"]
661 session["editid"] = cgi["editid"].to_s
662 db["editentry"] = Entry.select(XMLPATH + File.basename(session["filepath"]), session["editid"])
665 session["editid"] = cgi["editid"].to_s
666 db["editentry"] = Entry.new(params)
667 db["editentry"].content = db["editentry"].content_for_blog
670 successed = Entry.update(XMLPATH + File.basename(session["filepath"]), Entry.new(params))
672 db["error"] = "日記の編集処理に失敗しました。<br>該当の日記が既に存在しない可能性があります。"
673 params["mode"] = "error"
676 db["editentry"] = Entry.new(params)
678 # Edit Diary - Default
679 db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
680 db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
685 case params["action"]
688 session["delid"] = cgi["delid"].to_s
691 successed = Entry.delete(XMLPATH + File.basename(session["filepath"]), cgi["delid"].to_s)
693 db["error"] = "日記の編集処理に失敗しました。<br>該当の日記が既に存在しない可能性があります。"
694 params["mode"] = "error"
697 # Delete Diary - Default
698 db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
699 db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
704 case params["action"]
707 db["feed"] = Feed.new(params)
708 # 実際にFeed情報の変更をファイルに反映
710 db["feed"] = Feed.new(params)
712 Feed.update(XMLPATH + File.basename(session["filepath"]), Feed.new(params))
720 db["loglist"] = LogList.readxml(LISTXMLPATH)
721 case params["action"]
722 # ログファイルの編集を実際にファイルに反映
724 # エラーチェック。この段階のエラーは強度のエラーを発する。
725 db["loglist"].path.each do |val|
726 if val == cgi["logpath"]
727 # 重複していた場合エラーメッセージを表示し、処理を行わない
728 params["action"] = ""
729 params["mode"] = "error"
730 db["error"] = "ログファイルの追加中に重大なエラーが発生しました。<br>環境を見直してください。"
736 db["loglist"].path[1, 0] = cgi["logpath"]
737 db["loglist"].display[1, 0] = cgi["logdisplay"]
739 # 既存のdiary.xmlを指定された名称でコピーして保存
740 FileUtils.copy_file(XMLPATH + File.basename(db["loglist"].path[0]), XMLPATH + File.basename(db["logpath"]), true)
742 db["loglist"] = LogList.readxml(LISTXMLPATH)
744 # 新たなdiary.xmlを生成。この際保持する情報は同一のFeedオブジェクトnew()
745 Feed.to_xml(XMLPATH + File.basename(db["loglist"].path[0]), db["feed"], [])
749 db["loglist"].path.each do |val|
750 if val == cgi["logpath"]
751 # 重複していた場合エラーメッセージを表示し、処理を行わない
752 params["action"] = ""
753 db["error"] = "<span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
758 db["logpath"] = cgi["logpath"]
759 db["logdisplay"] = cgi["logdisplay"]
761 if db["logpath"].blank? || db["logdisplay"].blank?
762 params["action"] = ""
768 db["logdelindex"] = params["logdelindex"].to_i
770 if db["logdelindex"] < 1
771 db["error"] = "<span style='color: #ff0000'>ログファイルの削除パラメタが不正です。</span><br>"
772 params["action"] = ""
776 if cgi["logdelindex"].to_i < 1
777 params["action"] = ""
778 params["mode"] = "error"
779 db["error"] = "ログファイルの削除中に重大なエラーが発生しました。<br>環境を見直してください。"
783 File.delete(XMLPATH + File.basename(db["loglist"].path[db["logdelindex"]]))
785 db["loglist"].path.delete_at(cgi["logdelindex"].to_i)
786 db["loglist"].display.delete_at(cgi["logdelindex"].to_i)
789 db["loglist"] = LogList.readxml(LISTXMLPATH)
795 db["logeditindex"] = params["logdelindex"].to_i
797 db["logpath"] = db["loglist"].path[db["logeditindex"]]
798 db["logdisplay"] = db["loglist"].display[db["logeditindex"]]
800 if db["logeditindex"] == 0
801 db["error"] = "<span style='color: #ff0000'>ログファイルの編集パラメタが不正です。</span><br>"
802 params["action"] = ""
807 db["loglist"].path.each_with_index do |val, i|
808 if db["logeditindex"] != i
809 if params["logpath"].to_s == db["loglist"].path[i].to_s
815 if checkflag == false
816 params["action"] = "edit"
817 db["error"] = "<span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
819 db["loginsertindex"] = params["loginsertindex"].to_i
821 db["logpath"] = params["logpath"].to_s
822 db["logdisplay"] = params["logdisplay"].to_s
827 db["loglist"].path.each_with_index do |val, i|
828 if db["logeditindex"] != i
829 if params["logpath"].to_s == db["loglist"].path[i].to_s
835 if checkflag == false
836 params["action"] = ""
837 params["mode"] = "error"
838 db["error"] = "ログファイルの編集中に重大なエラーが発生しました。<br>環境を見直してください。"
841 db["loginsertindex"] = params["loginsertindex"].to_i
843 db["logpath"] = params["logpath"].to_s
844 db["logdisplay"] = params["logdisplay"].to_s
847 if XMLPATH + File.basename(db["loglist"].path[db["logeditindex"]]) != XMLPATH + File.basename(db["logpath"])
848 FileUtils.move(XMLPATH + File.basename(db["loglist"].path[db["logeditindex"]]), XMLPATH + File.basename(db["logpath"]))
850 db["loglist"].path.delete_at(db["logeditindex"])
851 db["loglist"].display.delete_at(db["logeditindex"])
852 db["loglist"].path.insert(db["loginsertindex"] + 1, db["logpath"])
853 db["loglist"].display.insert(db["loginsertindex"] + 1, db["logdisplay"])
860 # 現在編集中のデータを強制的に最上のファイルパスに変更
861 session["filepath"] = db["loglist"].path[0]
864 prevmonth = (DateTime.now << 1)
866 # 前月の時刻を元に、ディフォルト書式を生成します
867 db["logpath"] = FEEDXMLDIR + prevmonth.strftime("%Y%m") + ".xml"
868 db["logdisplay"] = prevmonth.strftime("%Y年%m月").gsub("年0", "年")
873 db["loglist"] = LogList.readxml(LISTXMLPATH)
877 case params["action"]
880 file = FileUploader.new
881 file.filelist().each { |fname|
882 file.delete(fname) if File.ftype(XMLPATH + fname) == "file"
887 loglist = LogList.new(["最新の記事"], ["#{FEEDXMLDIR}diary.xml"], LISTXMLPATH)
890 db["loglist"] = LogList.readxml(LISTXMLPATH)
893 Feed.to_xml(XMLPATH + db["loglist"].path[0].match(/[^\/]*?$/).to_a[0], Feed.new({}), [])
895 # 初期の編集ファイルはloglist.xml上の最も上のファイル
896 session["filepath"] = db["loglist"].path[0]
897 db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
898 db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
902 params["action"] = ""
906 filelist = FileUploader.new.filelist()
908 # タイプがファイルのみリストとして取得する
910 filelist.each { |fname| db["filelist"] << fname if File.ftype(XMLPATH + fname) == "file"}
915 # loglist.xmlが存在するかチェック
916 if File.exist?(LISTXMLPATH) == false
917 # なかった場合はloglist.xmlを自動生成
918 loglist = LogList.new(["最新の記事"], ["#{FEEDXMLDIR}diary.xml"], LISTXMLPATH)
920 Feed.to_xml(XMLPATH + File.basename(loglist.path[0]), Feed.new({}), [])
923 db["loglist"] = LogList.readxml(LISTXMLPATH)
925 # diary.xmlが存在するかチェック
926 if File.exist?(XMLPATH + File.basename(db["loglist"].path[0])) == false
927 # なかった場合はdiary.xmlを自動生成
928 Feed.to_xml(XMLPATH + File.basename(db["loglist"].path[0]), Feed.new({}), [])
931 # 初期の編集ファイルはloglist.xml上の最も上のファイル
932 session["filepath"] = db["loglist"].path[0] if session["filepath"] == nil
933 db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
934 db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
940 def Controller.MultiForm(cgi, session, params, db)
944 db["loglist"] = LogList.readxml(LISTXMLPATH)
949 case params["action"]
951 # この段階のエラーに対しては強度のエラーを発する
953 db["logpath"] = Controller.get_mpart_value(cgi["logpath"])
954 db["loglist"].path.each do |val|
955 if val == db["logpath"]
956 # 重複していた場合エラーメッセージを表示し、処理を行わない
958 params["action"] = ""
959 params["mode"] = "error"
960 db["error"] = "ログファイルの追加中に重大なエラーが発生しました。"
965 db["loginsertindex"] = cgi["loginsertindex"].read.to_i
966 db["logdisplay"] = Controller.get_mpart_value(cgi["logdisplay"])
969 if db["loginsertindex"] == 0
970 params["action"] = ""
971 params["mode"] = "error"
972 db["error"] = "ログファイルの追加中に重大なエラーが発生しました。"
976 if File.basename(db["logpath"]).blank? || db["logdisplay"].blank?
977 params["action"] = ""
978 params["mode"] = "error"
979 db["error"] = "ログファイルの追加中に重大なエラーが発生しました。"
984 file = FileUploader.new
985 file.upload(db["logpath"], db["importxml"])
988 db["loglist"].path[db["loginsertindex"], 0] = db["logpath"]
989 db["loglist"].display[db["loginsertindex"], 0] = db["logdisplay"]
993 db["loglist"] = LogList.readxml(LISTXMLPATH)
994 session["filepath"] = db["loglist"].path[0] if session["filepath"] == nil
995 db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
996 db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
999 # 入力されたログファイルパスが既に存在するかを確認
1001 db["logpath"] = Controller.get_mpart_value(cgi["logpath"])
1002 db["loglist"].path.each do |val|
1003 if val == db["logpath"]
1004 # 重複していた場合エラーメッセージを表示し、処理を行わない
1006 params["action"] = ""
1007 db["error"] = "<br><span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
1014 db["loginsertindex"] = cgi["loginsertindex"].read.to_i
1015 db["logdisplay"] = Controller.get_mpart_value(cgi["logdisplay"])
1016 db["importxml"] = cgi["updata"].read
1020 REXML::Document.new(Controller.fix_updata_enc(db["importxml"].to_s))
1022 db["error"] = "<br><span style='color: #ff0000'>不正な整形のXMLです!ファイルを見直してください。</span><br>"
1023 db["error"] << "<div class='divstyle' style='width: 560px; color: #ff0000; text-align: left;'>"
1024 db["error"] << CGI.escapeHTML(exception.to_s).gsub("\n", "<br>")
1025 db["error"] << "</div>"
1026 params["action"] = ""
1032 if db["loginsertindex"] == 0
1033 params["action"] = ""
1034 db["error"] = "<br><span style='color: #ff0000'>ラジオボックスでログの挿入位置を選択してください!</span><br>"
1038 if db["logpath"] == FEEDXMLDIR || db["logdisplay"].blank?
1039 params["action"] = ""
1040 db["error"] = "<br><span style='color: #ff0000'>インポートファイル、及び、ログの表示名は空欄にできません。</span><br>"
1049 case params["action"]
1051 # この段階のエラーに対しては強度のエラーを発する
1053 db["logpath"] = Controller.get_mpart_value(cgi["logpath"])
1054 db["loglist"].path.each do |val|
1055 if val == db["logpath"]
1056 # 重複していた場合エラーメッセージを表示し、処理を行わない
1057 params["action"] = ""
1058 params["mode"] = "error"
1059 db["error"] = "ログファイルの追加中に重大なエラーが発生しました。"
1064 db["logdisplay"] = Controller.get_mpart_value(cgi["logdisplay"])
1066 if File.basename(db["logpath"]).blank? || db["logdisplay"].blank? || db["importxml"].blank?
1067 params["action"] = ""
1068 params["mode"] = "error"
1069 db["error"] = "ログファイルの追加中に重大なエラーが発生しました。"
1074 FileUtils.move(XMLPATH + "diary.xml", XMLPATH + File.basename(db["logpath"]))
1076 file = FileUploader.new
1077 file.upload("diary.xml", db["importxml"])
1080 db["loglist"].path[1, 0] = db["logpath"]
1081 db["loglist"].display[1, 0] = db["logdisplay"]
1082 db["loglist"].to_xml
1085 db["loglist"] = LogList.readxml(LISTXMLPATH)
1086 session["filepath"] = db["loglist"].path[0] if session["filepath"] == nil
1087 db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
1088 db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
1091 # 入力されたログファイルパスが既に存在するかを確認
1093 db["logpath"] = Controller.get_mpart_value(cgi["logpath"])
1094 db["loglist"].path.each do |val|
1095 if val == db["logpath"]
1096 # 重複していた場合エラーメッセージを表示し、処理を行わない
1097 params["action"] = ""
1098 db["error"] = "<br><span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
1104 if checkflag == true
1105 db["logdisplay"] = Controller.get_mpart_value(cgi["logdisplay"])
1106 db["importxml"] = cgi["updata"].read
1110 REXML::Document.new(Controller.fix_updata_enc(db["importxml"].to_s))
1112 db["error"] = "<br><span style='color: #ff0000'>不正な整形のXMLです!ファイルを見直してください。</span><br>"
1113 db["error"] << "<div class='divstyle' style='width: 560px; color: #ff0000; text-align: left;'>"
1114 db["error"] << CGI.escapeHTML(exception.to_s).gsub("\n", "<br>")
1115 db["error"] << "</div>"
1116 params["action"] = ""
1121 if db["logpath"].blank? || db["logdisplay"].blank? || db["importxml"].blank?
1122 params["action"] = ""
1123 db["error"] = "<br><span style='color: #ff0000'>インポートファイル、及び、ログの表示名、ログのパスは空欄にできません。</span><br>"
1135 # Formのenctypeがmultypartだった場合、入力された値をrubyバージョンに左右されずに読み取るメソッドです。
1136 def Controller.get_mpart_value(cgi_param)
1137 if RUBY_VERSION >= "1.9.0"
1138 cgi_param[0..cgi_param.length].to_s
1144 # アップロードされたXMLをバージョンに左右されずに読み取るために、ruby-1.9.1以上ではエンコーディングを強制指定します
1145 def Controller.fix_updata_enc(file_to_s)
1146 if RUBY_VERSION >= "1.9.0"
1147 file_to_s.force_encoding("UTF-8")
1156 # SESSION変数、パラメータなどを取得します
1158 session = CGI::Session.new(cgi)
1159 params = Hash[*cgi.params.to_a.map{|k, v| [k, v[0].to_s]}.flatten]
1163 if session["login"] != "true"
1164 if (cgi["loginid"] == LOGINID && cgi["password"] == PASSWORD)
1165 session["login"] = "true"
1167 # ワークフォルダの中をクリーンアップします
1168 filelist = Dir::entries("./work")
1169 # 削除条件 : 最終変更日時から1日(60*60*24sec)かつ、ファイルタイプがファイルの場合
1170 filelist.each do |file|
1171 File.delete("./work/#{file}") if Time.now - File.ctime("./work/#{file}") > 86400 && File.ftype("./work/#{file}") == "file"
1177 if params["mode"] == "logout"
1178 session["login"] = nil
1179 session["logini"] = nil
1180 session["password"] = nil
1186 # セッションが有効な場合のも実行します
1187 if session["login"] == "true"
1190 db = PStore.new("./work/#{session.session_id}.dat")
1196 File.delete("./work/#{session.session_id}.dat")
1197 # PStoreが破損していた場合はセッション情報を破棄する
1198 session["login"] = nil
1199 session["logini"] = nil
1200 session["password"] = nil
1205 if cgi["mode"].respond_to?(:read) && cgi["action"].respond_to?(:read)
1206 params["mode"] = cgi["mode"].read
1207 params["action"] = cgi["action"].read
1208 Controller.MultiForm(cgi, session, params, db)
1210 Controller.NormalForm(cgi, session, params, db)
1214 # メニューとして表示されるHTML文字列です
1215 if USEFILEMANAGER == true
1216 menu = "<div class=\"divstyle\" style=\"width: #{TABLEWIDTH}px;\"><div class=\"divstyle\" align=\"center\" style=\"margin-bottom: 5px;\">現在編集中のファイル : #{session["filepath"]} [ <a href=\"#{cgi.script_name}?mode=selectlog\">他のファイルを選択</a> ]</div>[ <a href=\"#{cgi.script_name}\">トップページ</a> | 記事管理 ( <a href=\"#{cgi.script_name}?mode=newentry\">作成</a> | <a href=\"#{cgi.script_name}?mode=editentry\">編集</a> | <a href=\"#{cgi.script_name}?mode=delentry\">消去</a> ) | <a href=\"#{cgi.script_name}?mode=editfeed\">XML情報編集</a> | <a href=\"#{cgi.script_name}?mode=log\">ログ管理</a> | <a href=\"#{cgi.script_name}?mode=import\">インポート</a> | <a href=\"#{FILEMANAGER}\" target=\"_blank\">ファイル管理</a> | <a href=\"#{cgi.script_name}?mode=reset\">初期化</a> | <a href=\"#{cgi.script_name}?mode=logout\">ログアウト</a> ]</div>"
1218 menu = "<div class=\"divstyle\" style=\"width: #{TABLEWIDTH}px;\"><div class=\"divstyle\" align=\"center\" style=\"margin-bottom: 5px;\">現在編集中のファイル : #{session["filepath"]} [ <a href=\"#{cgi.script_name}?mode=selectlog\">他のファイルを選択</a> ]</div>[ <a href=\"#{cgi.script_name}\">トップページ</a> | 記事管理 ( <a href=\"#{cgi.script_name}?mode=newentry\">作成</a> | <a href=\"#{cgi.script_name}?mode=editentry\">編集</a> | <a href=\"#{cgi.script_name}?mode=delentry\">消去</a> ) | <a href=\"#{cgi.script_name}?mode=editfeed\">XML情報編集</a> | <a href=\"#{cgi.script_name}?mode=log\">ログ管理</a> | <a href=\"#{cgi.script_name}?mode=import\">インポート</a> | <a href=\"#{cgi.script_name}?mode=reset\">初期化</a> | <a href=\"#{cgi.script_name}?mode=logout\">ログアウト</a> ]</div>"
1222 # modeとactionの相関図は以下のようになります。
1227 if session["login"] != "true"
1228 # セッションが存在しない場合は強制的にトップページに遷移
1229 htmlwriter = HtmlWriter.new("./erbtemp/login.html.erb", binding)
1233 htmlwriter = HtmlWriter.new("./erbtemp/select.html.erb", binding)
1235 htmlwriter = HtmlWriter.new("./erbtemp/newentry.html.erb", binding)
1237 htmlwriter = HtmlWriter.new("./erbtemp/editentry.html.erb", binding)
1239 htmlwriter = HtmlWriter.new("./erbtemp/delentry.html.erb", binding)
1241 htmlwriter = HtmlWriter.new("./erbtemp/editfeed.html.erb", binding)
1243 htmlwriter = HtmlWriter.new("./erbtemp/log.html.erb", binding)
1245 htmlwriter = HtmlWriter.new("./erbtemp/insertfeed.html.erb", binding)
1247 htmlwriter = HtmlWriter.new("./erbtemp/replacefeed.html.erb", binding)
1249 htmlwriter = HtmlWriter.new("./erbtemp/indeximport.html.erb", binding)
1251 htmlwriter = HtmlWriter.new("./erbtemp/reset.html.erb", binding)
1253 htmlwriter = HtmlWriter.new("./erbtemp/error.html.erb", binding)
1255 htmlwriter = HtmlWriter.new("./erbtemp/index.html.erb", binding)
1259 # エラーが発生した場合、それを画面に表示します
1260 htmlwriter = HtmlWriter.new("./erbtemp/exception.html.erb", binding)
1265 cgi.out{htmlwriter.to_code}
1267 # エラーが発生した場合、それを画面に表示します
1268 htmlwriter = HtmlWriter.new("./erbtemp/exception.html.erb", binding)
1269 cgi.out{htmlwriter.to_code}
1276 # エラーが発生した場合、それを画面に表示します
1277 detail = ("%s: %s (%s)\n" %
1278 [evar.backtrace[0], evar.message, evar.send('class')]) +
1279 evar.backtrace[1..-1].join("\n")
1280 puts "content-type: text/html\n\n<plaintext>\n" + detail