+# Показ сообщения для редактирования
+sub show_messageedit {
+ my ($cgi,$forum,$path,$id)=@_;
+ my ($tree,$lockfd) = gettree($path);
+ my $dirname = $path;
+ $dirname =~ s/\/[^\/]+$//;
+ my ($msg) = $tree->look_down(id => $id);
+ show_error($forum,"В данной теме нет реплики с id=$id") if (!$msg);
+ show_error($forum,"У вас нет прав на редактированиее этого сообщения")
+ unless moderator_or_author($cgi,$forum,$msg);
+ # Выбираем данные сообщения
+ my ($text) = $msg->look_down(class=>"mtext")->content_list;
+ my ($subject) = $msg->look_down(class=>"subject");
+ tree2input($cgi,"text",$text);
+ $cgi->param("subject"=> $subject->as_text);
+ discardtree($tree,$lockfd);
+ # Подготавливаем шаблон
+ my $form = prepare_template("edit_message",$cgi,$forum);
+ # Подставляем информацию о картинках.
+ opendir D,$dirname;
+ while ( my $filename=readdir D) {
+ next unless $filename =~/^${id}_/;
+ my $pic = "$dirname/$filename";
+ my $picitem = newlistelement($form,"picture","picturelist");
+ my ($w,$h) = imgsize($pic);
+ substinfo($picitem,[_tag=>"img", class=>"msgpicture"],
+ src=>dir2url($cgi,$pic),width=>$w,height=>$h);
+ substinfo($picitem,[_tag=>"input",name=>"delpicture"],
+ value=>$filename);
+ substinfo($picitem,[class=>"filename"],_content =>$filename);
+ }
+ closedir D;
+ send_to_user($form,"edit_message",$cgi,$forum);
+}
+# Показ темы для редактирования
+sub show_topicedit {
+ my ($cgi,$forum,$path)=@_;
+ my ($tree,$lockfd) = gettree($path);
+ my $dirname = $path;
+ $dirname =~ s/\/([^\/]+).html$//;
+ my $urlname=$1;
+ my $topic = $tree->look_down("class"=>"topic");
+ show_error($forum,"У вас нет прав на редактированиее этой темы")
+ unless moderator_or_author($cgi,$forum,$topic);
+ # Выбираем данные сообщения
+ my ($text) = $topic->look_down(class=>"abstract")->content_list;
+ my ($subject) = $topic->look_down(class=>"title");
+ tree2input($cgi,"abstract",$text);
+ $cgi->param("title"=> $subject->as_text);
+ discardtree($tree,$lockfd);
+ # Подготавливаем шаблон
+ my $form = prepare_template("edit_topic",$cgi,$forum);
+ # Подставляем информацию об URL.
+ substinfo($form,[class=>"urlname"],_content=>$urlname);
+ send_to_user($form,"edit_topic",$cgi,$form);
+}
+# Показ форума для редактирования
+sub show_forumedit {
+ my ($cgi,$forum,$path) = @_;
+ show_error($forum,"У вас нет прав на редактированиее этого форума")
+ unless (getrights($cgi,$forum) eq 'moderator' || getrights($cgi,$forum) eq
+ 'admin');
+
+ my ($tree,$lockfd) = gettree($path."/".$forum->{indexfile});
+ my $dirname = $path;
+ $dirname =~ s/\/([^\/]+)$//;
+ my $urlname=$1;
+ my $anno = $tree->look_down("class"=>"annotation");
+ # Выбираем данные сообщения
+ my ($text) = $anno->look_down(class=>"abstract")->content_list;
+ my ($subject) = $anno->look_down(class=>"title");
+ tree2input($cgi,"abstract",$text);
+ $cgi->param("title"=> $subject->as_text);
+ discardtree($tree,$lockfd);
+ # Подготавливаем шаблон
+ my $form = prepare_template("edit_forum",$cgi,$forum);
+ # Подставляем информацию об URL.
+ substinfo($form,[class=>"urlname"],_content=>$urlname);
+ # Подставляем логотип
+ opendir D,$path;
+ my $logo=0;
+ while (my $file = readdir(D)) {
+ next unless $file =~ /^logo.\w+$/;
+ my ($w,$h) = imgsize($path."/".$file);
+ substinfo ($form,[_tag =>"img",class=>"logo"],
+ src=>$cgi->path_info."/".$file,
+ width=>$w,
+ height=>$h);
+ substinfo($form,[_tag=>"input",
+ name=>"delpicture"],value=>$file);
+ $logo=1;
+ last;
+ }
+ closedir D;
+ unless ($logo) {
+ substinfo($form,[class=>"picture"],
+ style=>"display: none;");
+ }
+ send_to_user($form,"edit_forum",$cgi,$form);
+}
+
+sub messageedit {
+ my ($cgi,$forum,$topic,$id) = @_;
+ my ($tree,$lockfd) = gettree($path_translated);
+ my ($msg) = $tree->look_down(id=>$id);
+ show_error($forum,"В данной теме нет реплики с id=$id") if (!$msg);
+ show_error($forum,"У вас нет прав на редактированиее этого сообщения")
+ unless moderator_or_author($cgi,$forum,$msg);
+ # Обработка картинок
+ my ($dirname,$topic_id)=($1,$2) if $path_translated=~/(.+)\/([^\/]+).html/;
+ # 1. Удаляем помеченные как таковые
+ for my $file ($cgi->param("delpictire")) {
+ unlink $dirname."/".$file;
+ }
+ # 2. Сохраняем свежеприаттаченные
+ my %attached=save_attached_images($cgi,$forum,$dirname,"${id}_%f",grep(/^image\d+/,$cgi->param));
+
+ # Преобразуем текст в html
+ my $txtree = input2tree($cgi,$forum,"text");
+ fix_image_links($txtree,\%attached,$dirname);
+ my $edited = strftime("%d.%m.%Y %H:%M",localtime());
+ substinfo($msg,[class=>"subject"],_content=>$cgi->param("subject"));
+ substinfo($msg,[class=>"editdate"],_content=>$edited);
+ makeuserlink($forum,$msg,"editor",$forum->{authenticated});
+ unhide_list($msg,"edited");
+ my $textnode = $msg->look_down(class=>"mtext");
+ my ($old_text)=$textnode->splice_content(0,1,$txtree);
+ $old_text->delete;
+ $msg = $msg->clone;
+ my @msgs = $tree->look_down(class=>"message");
+ my $msgcount=@msgs;
+ savetree($path_translated,$tree,$lockfd);
+ record_as_recent($forum,$msg);
+ update_topic_list($forum,$path_translated,$msgcount,$edited);
+ forum_redirect($cgi,$forum,$cgi->url(-base=>1).$cgi->path_info()."#$id");
+}
+
+
+sub topicedit {
+ my ($cgi,$forum,$topic) = @_;
+ show_error($forum,"Нет такой темы ".$cgi->path_info)
+ if (!-f $path_translated);
+ show_error($forum,"У вас нет прав на редактированиее этой темы")
+ unless moderator_or_author($cgi,$forum,$topic);
+ form_error("edit_topic",$cgi,$forum,"Тема должна иметь непустое название")
+ if (!$cgi->param("title"));
+ my $abstract = input2tree($cgi,$forum,"abstract");
+ my ($tree,$lockfd) = gettree($path_translated);
+ my $subtree = $tree->look_down("class"=>"topic");
+ if ($subtree) {
+ substinfo($subtree,["class"=>"title"],
+ _content=>$cgi->param("title"));
+ # Вставляем в страницу КОПИЮ аннотации, поскольку аннотация
+ # нам еще понадобится в списке тем.
+ substinfo($subtree,["class"=>"abstract"],_content=>$abstract->clone);
+ } else {
+ substinfo($tree,["class"=>"title"],
+ _content=>$cgi->param("title"));
+ }
+ savetree($path_translated,$tree,$lockfd);
+ $tree->destroy;
+ my ($dir,$id) = ($1,$2) if $path_translated=~/^(.*)\/([^\/]+).html$/;
+ ($tree,$lockfd) = gettree($dir."/".$forum->{indexfile});
+ my $item = $tree->look_down(id=>$id);
+ substinfo($item,[_tag=>"a","class"=>"title"],
+ _content=>$cgi->param("title"));
+ substinfo($item,["class"=>"abstract"],_content=>$abstract);
+ savetree($dir."/".$forum->{"indexfile"},$tree,$lockfd);
+ forum_redirect($cgi,$forum);
+}
+
+sub forumedit {
+ my ($cgi,$forum,$dir) = @_;
+ show_error($forum,"У вас нет прав на редактированиее этого форума")
+ unless (getrights($cgi,$forum) eq 'moderator' || getrights($cgi,$forum) eq
+ 'admin');
+
+ if (!-d $path_translated) {
+ show_error($forum,"Операция редактирования форумаможет быть вызвана только со
+ страницы форума");
+ }
+ if (!$cgi->param("title")) {
+ form_error("edit_forum",$cgi,$forum,"Форум должен иметь непустое название");
+ }
+ my ($logo_name,$logo_width,$logo_height);
+ if ($cgi->param("delpicture")) {
+ # Удаляем старый логотип
+ unlink $path_translated ."/".$cgi->param("delpicture");
+ $logo_name = $forum->{"templatesurl"}."/1x1.gif";
+ $logo_width = 1;
+ $logo_height=1;
+ }
+ if ($cgi->param("logo")) {
+ # Удаляем все, что похоже на логотип
+ opendir D, $path_translated;
+ for my $f (readdir(D)) {
+ next unless -f $path_translated."/".$f && $f=~/^logo\.\w+$/;
+ unlink $path_translated."/".$f;
+ }
+ closedir D;
+ $logo_name=(save_attached_images($cgi,$forum,$path_translated,"logo.%e","logo"))[1];
+ ($logo_width,$logo_height) = imgsize("$path_translated/$logo_name");
+ }
+ my ($tree,$lockfd) = gettree($path_translated . "/".$forum->{indexfile});
+ my $abstract = input2tree($cgi,$forum,"abstract");
+ substinfo($tree,[_tag=>"meta","name"=>"description"],content=>$abstract->as_trimmed_text);
+ substinfo($tree,[_tag=>"title"],_content=>$cgi->param("title"));
+ my $subtree = $tree->look_down("class"=>"annotation");
+ substinfo($subtree,["class"=>"title"],
+ _content=>$cgi->param("title"));
+ # Вставляем в страницу КОПИЮ аннотации, поскольку аннотация
+ # нам еще понадобится в списке тем.
+ substinfo($subtree,["class"=>"abstract"],_content=>$abstract->clone);
+ substinfo($subtree,[_tag=>"img","class"=>"logo"],
+ src=> $logo_name, width=>$logo_width, height=>$logo_height)
+ if defined($logo_name);
+ savetree($path_translated ."/".$forum->{indexfile},$tree,$lockfd);
+ $tree->destroy;
+ if ($path_translated ne $forum->{forumroot}) {
+ my ($updir,$id) = ($1,$2) if $path_translated=~/^(.*)\/([^\/]+)$/;
+ ($tree,$lockfd) = gettree($updir . "/".$forum->{indexfile});
+ my $item = $tree->look_down(id=>$id);
+ substinfo($item,[_tag=>"a","class"=>"title"],
+ _content=>$cgi->param("title"));
+ substinfo($item,["class"=>"abstract"],_content=>$abstract);
+ substinfo($item,[_tag=>"img","class"=>"logo"],src=>"$id/$logo_name",
+ width=>$logo_width,height=>$logo_height) if $logo_name;
+ savetree($updir."/".$forum->{"indexfile"},$tree,$lockfd);
+ }
+ forum_redirect($cgi,$forum);
+}