#!/usr/bin/perl # MimerDesk # Web based groupware and eLearning environment # www.mimerdesk.org # # Copyright (C) 2001 Ionstream Ltd. # www.ionstream.fi # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 2 of the License, or (at your option) # any later version. # # This program is distributed with a hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # $Id: chatconfig_add.html ########################################### # # # MimerDesk: Chatconfig editor # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # (c) Ionstream OY 1999 - 2001 # # # # Programmed by: Teemu Vainio # # # ########################################### use strict; use vars qw ($APPLICATION $ACTIVEGRP $ARTICLE $MONTH $USER $IP $LAST_ACT $FORWARDED $TIME_USED $IDLE $form $ref $sth $action $submit_buttons $name $topic $description $flags $trans); use lib::MimerDesk; use CGI::Carp "fatalsToBrowser"; $APPLICATION='Tools - Chatconfig'; sub add_room; sub print_template; sub check_action; sub modify_room; sub remove_room; sub clean_values; sub mega_html_escape; sub check_flags; ################ # Main Program # ################ # # 1. Usual stuff # 2. Adds, modifies or removes chatroom if form is filled # 3. Prepares template # 4. Prints template read_config('../config/mimerdesk.cfg'); initialize(); $form = decode_multipart(); $form->{'ID'} =~ tr/0-9//cd; $form->{'auth'} =~ tr/0-9a-z//cd; ($USER, $IP, $LAST_ACT, $FORWARDED, $TIME_USED, $IDLE, $ACTIVEGRP) = authenticate($form->{'ID'}, $form->{'auth'}, $form->{'changeGroup'}); $trans = lib::MimerDesk->new_gettext(program => 'chatconfig_add',language => $config{'language'}); $APPLICATION = $trans->gettext('Tools - Chatconfig'); add_room if ($form->{'ADD'} =~ 'Add'); remove_room if ($form->{'DELETE'} eq 'Remove'); modify_room if ($form->{'UPDATE'} eq 'Modify'); check_action; print_template("$config{'theme'}_chatconfig_add"); ########################## # ___ # # ___ | | | \ ___ # # / \ | | | / / \ # # \___ | | |--< \___ # # \ | | | \ \ # # \___/ \__/ |___/ \___/ # # # ########################## ################ # check_action # ################ # # Modifies templatevalues # # 1. Checks if user is going to add or edit # 2. Changes $submit_buttons and $action # 3. If action isn't add or edit redirects to chatconfig.html sub check_action { if ($form->{'action'} eq 'add') { $submit_buttons = hiddenfield('ADD',''). hiddenfield('SAVEADD',''). tagged('a',{href => 'javascript:ADD()', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/add.gif", alt => 'Add')}). '  '. tagged('a',{href => 'javascript:ADDANOTHER()', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/saveaddnew.gif", alt => 'Save changes & add new')}). '  '. tagged('a',{href => 'javascript:if(confirm(\'Are you sure you want to reset?\'))document.new_room.reset(); void(0);', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/reset.gif")}). '  '. tagged('a',{href =>"chatconfig.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=$form->{'order'}", onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/back2.gif", alt => 'Back')}); $action = $trans->gettext('Add a new chat channel'); } elsif ($form->{'action'} eq 'edit') { $submit_buttons = hiddenfield('UPDATE','Modify'). hiddenfield('DELETE','PERKELE'). tagged('a',{href => 'javascript:MODIFY()', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/savechanges.gif", alt => 'Save changes')}). '  '. tagged('a',{href => 'javascript:REMOVE()', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/remove.gif")}). '  '. tagged('a',{href => 'javascript:if(confirm(\'Are you sure you want to reset?\'))document.new_room.reset(); void(0);', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/reset.gif")}). '  '. tagged('a',{href =>"chatconfig.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=$form->{'order'}", onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/back2.gif", alt => 'Back')}); $action = $trans->gettext('Edit an existing chat channel'); my (@row); lock_tables('READ','chatrooms'); db_list("SELECT * FROM chatrooms WHERE ID = '$form->{'number'}'"); while (my $ref = $sth->fetchrow_hashref()) { $name = $ref->{'NAME'}; $topic = $ref->{'TOPIC'}; $description = $ref->{'INFO'}; $flags = $ref->{'FLAGS'}; } db_end(); unlock_tables(); $topic =~ s/^(<.+?>)//; $topic = urlify($topic); if ($flags) {$flags = 'checked';} else {$flags='';} } else { redirect("chatconfig.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=$form->{'order'}"); exit; } } ############ # add_room # ############ # # Adds new chatroom into database # # 1. removes dangerous stuff from $form values # 2. inserts values into database # 3. redirects user to chatconfig sub add_room { my $roomid; my $flags = check_flags(); clean_values(); lock_tables('WRITE', 'chatrooms'); db_list("select NAME from chatrooms where NAME = '$form->{'name'}'"); my ($check) = $sth->fetchrow_array(); db_end(); if ($check) { unlock_tables(); check_action(); print_template("$config{'theme'}_chatconfig_add",sprintf($trans->gettext("Room '%s' already exists!"), $form->{'name'}). tag('br'),'error'); } if ($form->{'name'} =~ /^\s*$/) { unlock_tables(); check_action(); print_template("$config{'theme'}_chatconfig_add",$trans->gettext('You must write a name!').tag('br'),'error'); exit; } db_list("insert into chatrooms ( ID, FOUNDER, NAME, TOPIC, FLAGS, INFO ) values ( 'NULL', '$USER', '$form->{'name'}', '$form->{'topic'}', '$flags', '$form->{'description'}' ) "); db_end(); $roomid = get_lastid(); unlock_tables(); db_list("create table chatroom_$roomid (LINE INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,TARGET varchar(30) NULL,SENDER varchar(30),TIME int unsigned NULL, TYPE varchar(10) NULL, MESSAGE text NULL)"); db_end(); if ($form->{'ADD'} eq 'Addanother') { redirect("chatconfig_add.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=$form->{'order'}&action=add"); } else { redirect("chatconfig.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=$form->{'order'}"); } exit; } ############### # modify_room # ############### # # Modifies chatroom # # 1. removes dangerous stuff from $form values # 2. gets original values from db and compares them with new values # if something has changed, writes it to chat db # 3. updates values in database # 4. redirects user to chatconfig sub modify_room { clean_values; my $flags = check_flags; my ($name, $topic, $viesti); my (@row); my(%db); my $time = time(); lock_tables('READ','chatrooms'); db_list("SELECT * FROM chatrooms WHERE ID = '$form->{'number'}'"); while (my $ref = $sth->fetchrow_hashref()) { $name = $ref->{'NAME'}; $topic = $ref->{'TOPIC'}; } db_end(); unlock_tables(); $topic =~ s/^(<.+?>)//; $topic = urlify($topic); if ($name ne $form->{'name'}) { $viesti = "This channel is now known as \"$form->{'name'}\""; lock_tables('WRITE',"chatroom_$form->{'number'}"); db_list("insert into chatroom_$form->{'number'} (LINE, TARGET, SENDER, TIME, TYPE, MESSAGE) values ('NULL','kaikki','i-system','$time', 'topic', '$viesti')"); db_end(); unlock_tables(); } if ($topic ne $form->{'topic'}) { $form->{'topic'} = html_escape($form->{'topic'}); $viesti = "System operator changes topic to \"$form->{'topic'}\""; lock_tables('WRITE',"chatroom_$form->{'number'}"); db_list("insert into chatroom_$form->{'number'} (LINE, TARGET, SENDER, TIME, TYPE, MESSAGE) values ('NULL','kaikki','i-system','$time', 'topic', '$viesti')"); db_end(); unlock_tables(); $form->{'topic'} = ''.$form->{'topic'}; %db = ( 'NAME' => $form->{'name'}, 'TOPIC' => $form->{'topic'}, 'FLAGS' => $flags, 'INFO' => $form->{'description'} ); } else { $form->{'topic'} = $topic; %db = ( 'NAME' => $form->{'name'}, 'FLAGS' => $flags, 'INFO' => $form->{'description'} ); } lock_tables('WRITE','chatrooms'); db_update("chatrooms",\%db,"ID = '$form->{'number'}'"); db_end(); unlock_tables(); redirect("chatconfig.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=$form->{'order'}"); exit; } ############### # remove_room # ############### # # Removes chatroom from database # sub remove_room { lock_tables('WRITE','chatrooms'); db_delete('chatrooms',"ID = '$form->{'number'}'"); db_end(); unlock_tables(); drop_table("chatroom_$form->{'number'}"); redirect("chatconfig.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=$form->{'order'}"); exit; } ################ # clean_values # ################ # # Clean values for db # # 2. make sure the values aren't too long # 3. prepare_fordb sub clean_values { $form->{'name'} = substr $form->{'name'}, 0, 100; $form->{'topic'} = substr $form->{'topic'}, 0, 200; ( $form->{'name'}, $form->{'topic'}, $form->{'description'} ) = prepare_fordb( ( $form->{'name'}, $form->{'topic'}, $form->{'description'} )); } #################### # mega_html_escape # #################### # # html_escapes all values # sub mega_html_escape { ( $name, #$topic, $description ) = html_escape ( ( $name, #$topic, $description )); } ################## # print_template # ################## # # Prints template # sub print_template { mega_html_escape; my ($ref, $fulltemplate); my ($template,$message,$colorclass) = @_; print_header('pragma'); $ref = get_template('maintemplate',$template); $ref->{'maintemplate'} =~ s/<>/$ref->{$template}/m; $fulltemplate = $ref->{'maintemplate'}; $fulltemplate = create_buttons($fulltemplate, 'Communication', 'Chat', $form); $fulltemplate =~ s/<>/minea/gm; $ref = get_template('js_doClock', 'js_help', 'js_gotosite'); my $stuff = $ref->{'js_doClock'}.$ref->{'js_help'}.$ref->{'js_gotosite'}; $fulltemplate =~ s/<>/$stuff/ms; $fulltemplate =~ s/<>/MimerDesk\: $APPLICATION/ms; $fulltemplate =~ s/<>/$form->{'action'}/gm; $fulltemplate =~ s/<>/$action/gm; $fulltemplate =~ s/<>/$submit_buttons/ms; $fulltemplate =~ s/<>/$form->{'number'}/ms; $fulltemplate =~ s/<>/$name/ms; $fulltemplate =~ s/<>/$topic/ms; $fulltemplate =~ s/<>/$description/ms; $fulltemplate =~ s/<>/$flags/ms; $fulltemplate =~ s/<>/$form->{'order'}/gm; $fulltemplate =~ s/<>/$colorclass/ms; $fulltemplate =~ s/<>/$message/ms; $fulltemplate = replace_tags($fulltemplate, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); $fulltemplate = add_popups($fulltemplate, $USER, $form->{'auth'}, $form->{'ID'}); print $fulltemplate; db_end('disconnect'); exit; } ############### # check_flags # ############### # # checks flagsettings from form and returns value for database # sub check_flags { if ($form->{'topicflag'} eq 'yes') { return 1; } else { return 0; } }