(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: edit_contact.ml,v 1.3 2005/03/31 14:24:04 rich Exp $ * * 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 in the 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; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Apache open Registry open Cgi open Printf open Cocanwiki open Cocanwiki_ok open Cocanwiki_strings let split_re = Pcre.regexp "[\\r\\n,;]+" let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ = let id = int_of_string (q#param "id") in let name = trim (q#param "name") in let subject = trim (q#param "subject") in let emails = try q#param "emails" with Not_found -> "" in let emails = Pcre.split ~rex:split_re emails in let emails = List.map trim emails in let emails = List.filter ((<>) "") emails in if name = "" then ( error ~back_button:true ~title:"Name field missing" q "You must name your contact form."; return () ); if subject = "" then ( error ~back_button:true ~title:"Subject line missing" q "You must give a subject line, which appears on contact emails."; return () ); if emails = [] then ( error ~back_button:true ~title:"No email addresses" q ("There are no email addresses listed for this contact form. You " ^ "must list at least one valid email address."); return () ); (* Need to verify that the contact belongs to the host. *) let sth = dbh#prepare_cached "select 1 from contacts where hostid = ? and id = ?" in sth#execute [`Int hostid; `Int id]; assert (sth#fetch1int () = 1); (* Update the database. *) let sth = dbh#prepare_cached "update contacts set name = ?, subject = ? where hostid = ? and id = ?" in sth#execute [`String name; `String subject; `Int hostid; `Int id]; let sth = dbh#prepare_cached "delete from contact_emails where contactid = ?" in sth#execute [`Int id]; let sth = dbh#prepare_cached "insert into contact_emails (contactid, email) values (?, ?)" in List.iter (fun email -> sth#execute [`Int id; `String email]) emails; (* Finish off. *) dbh#commit (); let buttons = [ ok_button "/_bin/contacts.cmo"; { Template.StdPages.label = " View contact form "; Template.StdPages.link = "/_bin/contact_show.cmo"; Template.StdPages.method_ = None; Template.StdPages.params = [ "id", string_of_int id ] } ] in ok ~title:"Contact form edited" ~buttons q "The contact form was edited." let () = register_script ~restrict:[CanManageContacts] run