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 Feed.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")
277 # Entryの基礎情報を保有するクラスです
278 class Entry < AbstractEntry
281 # _hash_ :: 値を格納したhash配列。entry.new(CGI::Session.new(new CGI).params)のようにして使います。
287 @paramlist = ["entryid", "title", "summary", "published", "updated", "url", "content", "others"]
289 # AbstractEntryのinitializeメソッドを呼び出し、値を@attr配列に格納します
292 # 可視・不可視を示すハッシュキーを格納する配列です
293 @display = {"entryid" => "none", "title" => "",
294 "summary" => "", "published" => "none",
295 "updated" => "none", "url" => "none",
296 "content" => "", "others"=>"none"}
298 # デバッグモードの場合、全ての入力要素を表示します
300 @display.each do |key, val|
305 # ハッシュキーの日本語説明を格納する配列です
306 @name = {"entryid" => "記事固有のID", "title" => "記事のタイトル",
307 "summary" => "記事の簡単な説明", "published" => "記事の出版時刻",
308 "updated" => "記事の更新時刻", "url" => "記事へのURLアドレス",
309 "content" => "記事の本文", "others"=>"その他の項目"}
312 # Atom XMLファイルを読み込んで解析し、等価なEntryオブジェクト配列を返却するメソッドです
314 # _path_ :: Atom XMLファイルのパス
315 def Entry.readxml(path)
318 doc = REXML::Document.new(myopen(path, "r:utf-8"){|f|f.read})
322 # XML解析部分です。各element毎に判定を行います
323 doc.elements.each("feed/entry") do |elm|
326 elm.elements.each do |child|
330 xml[:entryid] = child.text
332 xml[:url] = child.attributes["href"]
333 when "title", "summary", "summary", "published", "updated", "content"
334 xml[child.name.to_sym] = child.text
336 # 上記判定以外の全要素は配列に格納します
337 others.push(child.to_s)
343 xml[:others] = others.join("\n")
344 entrylist.push(Entry.new(xml))
350 # データソースから読み取ったHTMLを、エディタで編集可能な形式に変換するメソッドです
351 def content_for_generator
352 str = @attr[:content].dup
354 str.gsub!(/(<\/(?:p|h\d|div)(?:>|>))\n/i, '\1')
355 str.gsub!(/\n/, '<br>') if REPLACEBRTAG
356 str.gsub!(/(<(?:(?!>).)*?)#{Regexp.escape(FEEDXMLDIR)}/) { "#$1#{XMLPATH}" }
360 # エディタで編集されたCONTENT要素を、データソースに書き込める形式に変換するメソッドです
362 str = @attr[:content].dup
363 str = CGI.unescapeHTML(str)
365 str.gsub!(/(\r\n|\n)/, "")
366 str.gsub!(/<br>/i, "\n") if REPLACEBRTAG
367 str.gsub!(/(<br>|<\/p>|<\/h\d>|<\/div>)(?=[^\n])/i) { "#$1\n" } unless REPLACEBRTAG
368 str.gsub!(/(<[^>]*?)#{Regexp.escape(XMLPATH)}/) { "#$1#{FEEDXMLDIR}" }
372 # 確認画面で表示されるCONTENT要素を生成するメソッドです
374 str = @attr[:content].dup
375 str = CGI.unescapeHTML(str)
377 str.gsub!(/<br>/i, "\n") if REPLACEBRTAG
378 str.gsub!(/(<[^>]*?)#{Regexp.escape(FEEDXMLDIR)}/) { "#$1#{XMLPATH}" }
382 # 内部に保持している情報を、Atom Feed1.0形式の文字列に出力するメソッドです
386 buf.push("<id>#{@attr[:entryid]}</id>")
387 buf.push("<title>#{@attr[:title]}</title>")
388 buf.push("<summary>#{@attr[:summary]}</summary>")
389 buf.push("<published>#{@attr[:published]}</published>")
390 buf.push("<updated>#{@attr[:updated]}</updated>")
391 buf.push("<link href=\"#{@attr[:url]}\" />")
392 buf.push("<content type=\"html\">#{@attr[:content]}</content>")
393 buf.push("#{CGI.unescapeHTML(@attr[:others])}") if @attr[:others] != ""
396 return buf.join("\n")
402 # Atom Feed 1.0 XMLファイルに書き込みを行うクラスです
406 # _path_ :: Atom Feed 1.0 XMLファイルの書き込みパス
413 # _feed_ :: Feedオブジェクト
414 # _entry_ :: Entryオブジェクトの配列
415 def to_xml(feed, entrylist)
417 buf.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
418 buf.push("#{feed.to_s}\n")
419 entrylist.each { |entry|
420 buf.push("#{entry.to_s}\n")
424 myopen(@path, "w") do |f|
425 f.print buf.join("\n")
432 # テンプレートファイル(*.erb)を読み込み、管理するクラスです
436 # _template_ :: テンプレートファイル(*.erb)のパス
437 # _binding_ :: binding変数
438 def initialize(template, binding)
439 @erb = ERB.new(myopen(template, "r:utf-8") {|f| f.read}, nil, "-")
443 # テンプレートファイルの文字列を返却するメソッドです
445 @erb.result(@binding)
451 # loglist.xmlにかかわる入出力を行います
455 # _display_ :: 画面表示用文字の配列
456 # _path_ :: XMLファイルパスの配列
457 # _logpath_ :: loglist.xmlのパス
458 def initialize(display, path, logpath)
464 attr_accessor :display, :path, :logpath
466 # loglist.xmlファイルを読み込んで解析し、LogListオブジェクトを返却するメソッドです
468 # _logpath_ :: loglist.xmlへのパス
469 def LogList.readxml(logpath)
473 myopen(logpath, "r:utf-8") { |f|
477 doc = REXML::Document.new(lines.join("\n"))
481 doc.elements.each("list/file") { |elm|
482 elm.elements.each {|child|
485 @display.push(child.text)
487 @path.push(child.text)
494 return LogList.new(@display, @path, logpath)
497 # loglist.xmlにオブジェクトの内容を反映するメソッドです
501 path.each_with_index do |path, i|
503 buf.push("<display>#{@display[i]}</display>")
504 buf.push("<path>#{@path[i]}</path>")
509 myopen(@logpath, "w") do |f|
510 f.print buf.join("\n")
524 # ファイルの一覧を取得し、ファイル名称を格納した配列を返却します
526 arr = Dir::entries(XMLPATH).sort
535 # _name_ :: 消去するファイル名
537 File.delete(XMLPATH + name.match(/[^\/]*?$/).to_a[0])
542 # _name_ :: アップロードファイルの名称
543 # _img_ :: アップロードファイル
544 def upload(name, img)
545 open(XMLPATH + File.basename(name), "w") do |f|
554 # コントローラ部分に相当する処理を受け持つクラスです
556 def Controller.NormalForm(cgi, session, params, db)
558 # modeとactionの相関図は以下のようになります。
566 session["filepath"] = params["logpath"]
567 session["loglist"] = LogList.readxml(LISTXMLPATH)
568 # 初期状態で選択されるログは「loglist.xml」の最上に位置するログになります
569 session["filepath"] = session["loglist"].path[0] if session["filepath"] == nil
570 session["feed"] = Feed.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
571 session["entry"] = Entry.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
575 case params["action"]
578 session["newentry"] = Entry.new(params)
579 session["newentry"].content = session["newentry"].content_for_blog
582 session["newentry"] = Entry.new(params)
583 session["entry"].unshift session["newentry"]
584 feedwriter = FeedWriter.new(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
585 feedwriter.to_xml(session["feed"], session["entry"])
588 session["newentry"] = Entry.new(params)
590 # New Diary - Default
591 session["feed"] = Feed.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
592 session["entry"] = Entry.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
597 case params["action"]
600 session["editid"] = cgi["editid"].to_s
603 session["editid"] = cgi["editid"].to_s
604 session["editentry"] = Entry.new(params)
605 session["editentry"].content = session["editentry"].content_for_blog
608 session["editentry"] = Entry.new(params)
609 session["entry"].each_with_index { |e, i|
610 session["entry"][i] = session["editentry"] if e.entryid == cgi["editid"].to_s
612 feedwriter = FeedWriter.new(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
613 feedwriter.to_xml(session["feed"], session["entry"])
616 session["editid"] = cgi["editid"].to_s
617 session["editentry"] = Entry.new(params)
619 # Edit Diary - Default
620 session["feed"] = Feed.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
621 session["entry"] = Entry.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
626 case params["action"]
629 session["delid"] = cgi["delid"].to_s
633 session["entry"].each_with_index { |e, i|
634 delindex = i if e.entryid == cgi["delid"].to_s
636 session["entry"].delete_at(delindex) unless delindex == nil
637 feedwriter = FeedWriter.new(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
638 feedwriter.to_xml(session["feed"], session["entry"])
640 # Delete Diary - Default
641 session["feed"] = Feed.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
642 session["entry"] = Entry.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
647 case params["action"]
650 session["feed"] = Feed.new(params)
651 # 実際にFeed情報の変更をファイルに反映
653 session["feed"] = Feed.new(params)
654 feedwriter = FeedWriter.new(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0])
655 feedwriter.to_xml(session["feed"], session["entry"])
658 session["feed"] = Feed.new(params)
665 case params["action"]
666 # ログファイルの編集を実際にファイルに反映
669 session["loglist"].path.each do |val|
670 if val == cgi["logpath"]
671 # 重複していた場合エラーメッセージを表示し、処理を行わない
672 params["action"] = ""
673 error = "<span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
678 session["logpath"] = cgi["logpath"]
679 session["logdisplay"] = cgi["logdisplay"]
682 session["loglist"].path[1, 0] = session["logpath"]
683 session["loglist"].display[1, 0] = session["logdisplay"]
684 session["loglist"].to_xml
686 # 既存のdiary.xmlを指定された名称でコピーして保存
687 FileUtils.copy_file(XMLPATH + session["loglist"].path[0].match(/[^\/]*?$/).to_a[0], XMLPATH + session["logpath"].match(/[^\/]*?$/).to_a[0], true)
690 session["loglist"] = LogList.readxml(LISTXMLPATH)
691 session["entry"] = []
693 # 新たなdiary.xmlを生成。この際保持する情報は同一のFeedオブジェクト
694 writer = FeedWriter.new(XMLPATH + session["loglist"].path[0].match(/[^\/]*?$/).to_a[0])
695 writer.to_xml(session["feed"], [])
700 session["loglist"].path.each do |val|
701 if val == cgi["logpath"]
702 # 重複していた場合エラーメッセージを表示し、処理を行わない
703 params["action"] = ""
704 error = "<span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
709 session["logpath"] = cgi["logpath"]
710 session["logdisplay"] = cgi["logdisplay"]
712 if session["logpath"].blank? || session["logdisplay"].blank?
713 params["action"] = ""
718 session["logdelindex"] = params["logdelindex"].to_i
720 if session["logdelindex"] == 0
721 params["action"] = ""
725 session["logdelindex"] = params["logdelindex"].to_i
726 if session["logdelindex"] == 0
727 params["action"] = ""
730 File.delete(XMLPATH + session["loglist"].path[session["logdelindex"]].match(/[^\/]*?$/).to_s)
732 session["loglist"].path.delete_at(session["logdelindex"])
733 session["loglist"].display.delete_at(session["logdelindex"])
734 session["loglist"].to_xml
736 session["loglist"] = LogList.readxml(LISTXMLPATH)
737 session["entry"] = []
741 session["logeditindex"] = params["logdelindex"].to_i
743 session["logpath"] = session["loglist"].path[session["logeditindex"]]
744 session["logdisplay"] = session["loglist"].display[session["logeditindex"]]
746 if session["logeditindex"] == 0
747 params["action"] = ""
751 session["logeditindex"] = params["logeditindex"].to_i
753 session["loglist"].path.each_with_index do |val, i|
754 if session["logeditindex"] != i
755 if params["logpath"].to_s == session["loglist"].path[i].to_s
761 if checkflag == false
762 params["action"] = "edit"
763 session["error"] = "<span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
765 session["loginsertindex"] = params["loginsertindex"].to_i
767 session["logpath"] = params["logpath"].to_s
768 session["logdisplay"] = params["logdisplay"].to_s
772 session["logeditindex"] = params["logeditindex"].to_i
774 session["loglist"].path.each_with_index do |val, i|
775 if session["logeditindex"] != i
776 if params["logpath"].to_s == session["loglist"].path[i].to_s
782 if checkflag == false
783 params["action"] = "edit"
784 session["error"] = "<span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
787 session["loginsertindex"] = params["loginsertindex"].to_i
789 session["logpath"] = params["logpath"].to_s
790 session["logdisplay"] = params["logdisplay"].to_s
794 if XMLPATH + session["loglist"].path[session["logeditindex"]].match(/[^\/]*?$/).to_s != XMLPATH + session["logpath"].match(/[^\/]*?$/).to_s
795 FileUtils.move(XMLPATH + session["loglist"].path[session["logeditindex"]].match(/[^\/]*?$/).to_s, XMLPATH + session["logpath"].match(/[^\/]*?$/).to_s)
798 session["loglist"].path.delete_at(session["logeditindex"])
799 session["loglist"].display.delete_at(session["logeditindex"])
800 session["loglist"].path.insert(session["loginsertindex"] + 1, session["logpath"])
801 session["loglist"].display.insert(session["loginsertindex"] + 1, session["logdisplay"])
802 session["loglist"].to_xml
805 # 初期表示の際に内部保持データをリフレッシュ
806 session["loglist"] = LogList.readxml(LISTXMLPATH)
808 # 現在編集中のデータを強制的に最上のファイルパスに変更
809 session["filepath"] = session["loglist"].path[0]
812 prevmonth = (DateTime.now << 1)
814 # 前月の時刻を元に、ディフォルト書式を生成します
815 session["logpath"] = FEEDXMLDIR + prevmonth.strftime("%Y%m") + ".xml"
816 session["logdisplay"] = prevmonth.strftime("%Y年%m月").gsub("年0", "年")
821 session["loglist"] = LogList.readxml(LISTXMLPATH)
825 case params["action"]
828 file = FileUploader.new
829 file.filelist().each { |fname|
830 file.delete(fname) if File.ftype(XMLPATH + fname) == "file"
835 loglist = LogList.new(["最新の記事"], ["#{FEEDXMLDIR}diary.xml"], LISTXMLPATH)
837 writer = FeedWriter.new(XMLPATH + loglist.path[0].match(/[^\/]*?$/).to_a[0])
838 writer.to_xml(Feed.new({}), [])
840 session["loglist"] = LogList.readxml(LISTXMLPATH)
843 writer = FeedWriter.new(XMLPATH + session["loglist"].path[0].match(/[^\/]*?$/).to_a[0])
844 writer.to_xml(Feed.new({}), [])
846 # 初期の編集ファイルはloglist.xml上の最も上のファイル
847 session["filepath"] = session["loglist"].path[0]
848 session["feed"] = Feed.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0].to_s)
849 session["entry"] = Entry.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0].to_s)
853 params["action"] = ""
857 filelist = FileUploader.new.filelist()
859 # タイプがファイルのみリストとして取得する
860 session["filelist"] = []
861 filelist.each { |fname| session["filelist"] << fname if File.ftype(XMLPATH + fname) == "file"}
866 # loglist.xmlが存在するかチェック
867 if File.exist?(LISTXMLPATH) == false
868 # なかった場合はloglist.xmlを自動生成
869 loglist = LogList.new(["最新の記事"], ["#{FEEDXMLDIR}diary.xml"], LISTXMLPATH)
871 writer = FeedWriter.new(XMLPATH + loglist.path[0].match(/[^\/]*?$/).to_a[0])
872 writer.to_xml(Feed.new({}), [])
875 session["loglist"] = LogList.readxml(LISTXMLPATH)
877 # diary.xmlが存在するかチェック
878 if File.exist?(XMLPATH + session["loglist"].path[0].match(/[^\/]*?$/).to_a[0]) == false
879 # なかった場合はdiary.xmlを自動生成
880 writer = FeedWriter.new(XMLPATH + session["loglist"].path[0].match(/[^\/]*?$/).to_a[0])
881 writer.to_xml(Feed.new({}), [])
884 # 初期の編集ファイルはloglist.xml上の最も上のファイル
885 session["filepath"] = session["loglist"].path[0] if session["filepath"] == nil
886 session["feed"] = Feed.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0].to_s)
887 session["entry"] = Entry.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0].to_s)
893 def Controller.MultiForm(cgi, session, params, db)
898 case params["action"]
901 file = FileUploader.new
902 file.upload(session["logpath"], session["importxml"])
905 session["loglist"].path[session["loginsertindex"], 0] = session["logpath"]
906 session["loglist"].display[session["loginsertindex"], 0] = session["logdisplay"]
907 session["loglist"].to_xml
910 session["loglist"] = LogList.readxml(LISTXMLPATH)
911 session["filepath"] = session["loglist"].path[0] if session["filepath"] == nil
912 session["feed"] = Feed.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0].to_s)
913 session["entry"] = Entry.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0].to_s)
916 # 入力されたログファイルパスが既に存在するかを確認
918 session["logpath"] = Controller.get_mpart_value(cgi["logpath"])
919 session["loglist"].path.each do |val|
920 if val == session["logpath"]
921 # 重複していた場合エラーメッセージを表示し、処理を行わない
922 session["logpath"] = ""
923 params["action"] = ""
924 session["error"] = "<br><span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
931 session["loginsertindex"] = cgi["loginsertindex"].read.to_i
932 session["logdisplay"] = Controller.get_mpart_value(cgi["logdisplay"])
933 session["importxml"] = cgi["updata"].read
937 REXML::Document.new(Controller.fix_updata_enc(session["importxml"].to_s))
939 session["error"] = "<br><span style='color: #ff0000'>不正な整形のXMLです!ファイルを見直してください。</span><br>"
940 session["error"] << "<div class='divstyle' style='width: 560px; color: #ff0000; text-align: left;'>"
941 session["error"] << CGI.escapeHTML(exception.to_s).gsub("\n", "<br>")
942 session["error"] << "</div>"
943 params["action"] = ""
948 if session["loginsertindex"] == 0
949 params["action"] = ""
950 session["error"] = "<br><span style='color: #ff0000'>ラジオボックスでログの挿入位置を選択してください!</span><br>"
953 if session["logpath"] == FEEDXMLDIR || session["logdisplay"].blank?
954 params["action"] = ""
955 session["error"] = "<br><span style='color: #ff0000'>インポートファイル、及び、ログの表示名は空欄にできません。</span><br>"
959 session["loglist"] = LogList.readxml(LISTXMLPATH)
964 case params["action"]
967 FileUtils.move(XMLPATH + "diary.xml", XMLPATH + session["logpath"].match(/[^\/]*?$/).to_s)
969 file = FileUploader.new
970 file.upload("diary.xml", session["importxml"])
973 session["loglist"].path[1, 0] = session["logpath"]
974 session["loglist"].display[1, 0] = session["logdisplay"]
975 session["loglist"].to_xml
978 session["loglist"] = LogList.readxml(LISTXMLPATH)
979 session["filepath"] = session["loglist"].path[0] if session["filepath"] == nil
980 session["feed"] = Feed.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0].to_s)
981 session["entry"] = Entry.readxml(XMLPATH + session["filepath"].match(/[^\/]*?$/).to_a[0].to_s)
984 # 入力されたログファイルパスが既に存在するかを確認
986 session["logpath"] = Controller.get_mpart_value(cgi["logpath"])
987 session["loglist"].path.each do |val|
988 if val == session["logpath"]
989 # 重複していた場合エラーメッセージを表示し、処理を行わない
990 params["action"] = ""
991 session["error"] = "<br><span style='color: #ff0000'>同一のファイルが存在します!別の名前を指定してください。</span><br>"
998 session["logdisplay"] = Controller.get_mpart_value(cgi["logdisplay"])
999 session["importxml"] = cgi["updata"].read
1003 REXML::Document.new(Controller.fix_updata_enc(session["importxml"].to_s))
1005 session["error"] = "<br><span style='color: #ff0000'>不正な整形のXMLです!ファイルを見直してください。</span><br>"
1006 session["error"] << "<div class='divstyle' style='width: 560px; color: #ff0000; text-align: left;'>"
1007 session["error"] << CGI.escapeHTML(exception.to_s).gsub("\n", "<br>")
1008 session["error"] << "</div>"
1009 params["action"] = ""
1013 if session["logpath"].blank? || session["logdisplay"].blank? || session["importxml"].blank?
1014 params["action"] = ""
1015 session["error"] = "<span style='color: #ff0000'>インポートファイル、及び、ログの表示名、ログのパスは空欄にできません。</span><br>"
1018 # loglist.xmlをロードします
1019 session["loglist"] = LogList.readxml(LISTXMLPATH)
1022 # loglist.xmlをロードします
1023 session["loglist"] = LogList.readxml(LISTXMLPATH)
1028 # Formのenctypeがmultypartだった場合、入力された値をrubyバージョンに左右されずに読み取るメソッドです。
1029 def Controller.get_mpart_value(cgi_param)
1030 if RUBY_VERSION >= "1.9.0"
1031 cgi_param[0..cgi_param.length].to_s
1037 # アップロードされたXMLをバージョンに左右されずに読み取るために、ruby-1.9.1以上ではエンコーディングを強制指定します
1038 def Controller.fix_updata_enc(file_to_s)
1039 if RUBY_VERSION >= "1.9.0"
1040 file_to_s.force_encoding("UTF-8")
1049 # SESSION変数、パラメータなどを取得します
1051 session = CGI::Session.new(cgi)
1052 params = Hash[*cgi.params.to_a.map{|k, v| [k, v[0].to_s]}.flatten]
1056 if session["login"] != "true"
1057 if (cgi["loginid"] == LOGINID && cgi["password"] == PASSWORD)
1058 session["login"] = "true"
1060 # ワークフォルダの中をクリーンアップします
1061 filelist = Dir::entries("./work")
1062 # 削除条件 : 最終変更日時から1日(60*60*24sec)かつ、ファイルタイプがファイルの場合
1063 filelist.each do |file|
1064 File.delete("./work/#{file}") if Time.now - File.ctime("./work/#{file}") > 86400 && File.ftype("./work/#{file}") == "file"
1070 if params["mode"] == "logout"
1071 session["login"] = nil
1072 session["logini"] = nil
1073 session["password"] = nil
1079 # セッションが有効な場合のも実行します
1080 if session["login"] == "true"
1083 db = PStore.new("./work/#{session.session_id}.dat")
1086 File.delete("./work/#{session.session_id}.dat")
1087 # PStoreが破損していた場合はセッション情報を破棄する
1088 session["login"] = nil
1089 session["logini"] = nil
1090 session["password"] = nil
1095 if cgi["mode"].respond_to?(:read) && cgi["action"].respond_to?(:read)
1096 session["error"] = ""
1097 params["mode"] = cgi["mode"].read
1098 params["action"] = cgi["action"].read
1099 Controller.MultiForm(cgi, session, params, db)
1101 Controller.NormalForm(cgi, session, params, db)
1105 # メニューとして表示されるHTML文字列です
1106 if USEFILEMANAGER == true
1107 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>"
1109 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>"
1113 # modeとactionの相関図は以下のようになります。
1118 if session["login"] != "true"
1119 # セッションが存在しない場合は強制的にトップページに遷移
1120 htmlwriter = HtmlWriter.new("./erbtemp/login.html.erb", binding)
1124 htmlwriter = HtmlWriter.new("./erbtemp/select.html.erb", binding)
1126 htmlwriter = HtmlWriter.new("./erbtemp/newentry.html.erb", binding)
1128 htmlwriter = HtmlWriter.new("./erbtemp/editentry.html.erb", binding)
1130 htmlwriter = HtmlWriter.new("./erbtemp/delentry.html.erb", binding)
1132 htmlwriter = HtmlWriter.new("./erbtemp/editfeed.html.erb", binding)
1134 htmlwriter = HtmlWriter.new("./erbtemp/log.html.erb", binding)
1136 htmlwriter = HtmlWriter.new("./erbtemp/insertfeed.html.erb", binding)
1138 htmlwriter = HtmlWriter.new("./erbtemp/replacefeed.html.erb", binding)
1140 htmlwriter = HtmlWriter.new("./erbtemp/indeximport.html.erb", binding)
1142 htmlwriter = HtmlWriter.new("./erbtemp/reset.html.erb", binding)
1144 htmlwriter = HtmlWriter.new("./erbtemp/index.html.erb", binding)
1148 # エラーが発生した場合、それを画面に表示します
1149 htmlwriter = HtmlWriter.new("./erbtemp/exception.html.erb", binding)
1154 cgi.out{htmlwriter.to_code}
1156 # エラーが発生した場合、それを画面に表示します
1157 htmlwriter = HtmlWriter.new("./erbtemp/exception.html.erb", binding)
1158 cgi.out{htmlwriter.to_code}
1165 # エラーが発生した場合、それを画面に表示します
1166 detail = ("%s: %s (%s)\n" %
1167 [evar.backtrace[0], evar.message, evar.send('class')]) +
1168 evar.backtrace[1..-1].join("\n")
1169 puts "content-type: text/html\n\n<plaintext>\n" + detail