(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: page.ml,v 1.24 2004/09/27 18:08:02 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 ExtString open Cocanwiki open Cocanwiki_template open Cocanwiki_ok open Cocanwiki_date open Cocanwiki_server_settings (* Maximum level of redirection. *) let max_redirect = 4 type fp_status = FPOK of int * string * string * Dbi.datetime * bool | FPRedirect of string | FPNotFound (* Referer strings which help us decide if the user came from * a search engine and highlight terms in the page appropriately. *) let search_engines = [ Pcre.regexp "^http://.*google\\.", [ "q"; "as_q"; "as_epq"; "as_oq" ]; Pcre.regexp "^http://.*yahoo\\.", [ "p" ]; Pcre.regexp "^http://.*msn\\.", [ "q"; "MT" ] ] let split_words = Pcre.regexp "\\W+" let split_qs_re = Pcre.regexp "\\?" let xhtml_re = Pcre.regexp "<.*?>|[^<>]+" let run r (q : cgi) (dbh : Dbi.connection) hostid {edit_anon=edit_anon} user = let template_page = get_template dbh hostid "page.html" in let template_404 = get_template dbh hostid "page_404.html" in let page = q#param "page" in let page = if page = "" then "index" else page in (* Host-specific fields. *) let sth = dbh#prepare_cached "select css is not null, feedback_email is not null, mailing_list, search_box from hosts where id = ?" in sth#execute [`Int hostid]; let has_host_css, has_feedback_email, mailing_list, search_box = match sth#fetch1 () with | [ `Bool has_host_css; `Bool has_feedback_email; `Bool mailing_list; `Bool search_box ] -> has_host_css, has_feedback_email, mailing_list, search_box | _ -> assert false in (* Can the user edit? Manage users? etc. *) let can_edit = can_edit edit_anon user in let can_manage_users = can_manage_users user in let can_manage_contacts = can_manage_contacts user in let can_manage_site = can_manage_site user in let can_edit_global_css = can_edit_global_css user in (* Do we have a stats page set up? *) let has_stats = server_settings_stats_page dbh <> None in (* Given the referer string, return the list of search terms. If none * can be found, then throws Not_found. *) let search_terms_from_referer referer = let _, argnames = List.find (fun (rex, _) -> Pcre.pmatch ~rex referer) search_engines in let url, qs = match Pcre.split ~rex:split_qs_re ~max:2 referer with | [url] | [url;""] -> url, "" | [url;qs] -> url, qs | _ -> assert false in let args = Cgi_args.parse qs in let argname = List.find (fun argname -> List.mem_assoc argname args) argnames in let search_string = List.assoc argname args in Pcre.split ~rex:split_words search_string in (* Given a full page of XHTML, highlight search terms found in the * part of the page. *) let highlight_search_terms xhtml search_terms span_class = (* Split the original XHTML into strings and tags. For example if * the original string is: "This is some bold text.
", then * after this step we will have the following list: * [ "This is some "; ""; "bold"; ""; " text."; "
" ] *) let xhtml = Pcre.extract_all ~rex:xhtml_re xhtml in let xhtml = Array.to_list xhtml in let xhtml = List.map (fun matches -> matches.(0)) xhtml in (* Find the ... tags. We only want to apply * highlighting to tags within this area. *) let rec list_split f acc = function | [] -> List.rev acc, [] | ((x :: _) as xs) when f x -> List.rev acc, xs | x :: xs -> let acc = x :: acc in list_split f acc xs in let head, body = list_split (fun str -> String.starts_with str "") [] body in (* NB: Hopefully, xhtml = head @ body @ tail. *) (* The search terms are a list of simple words. Turn into a big * regular expression, because we want to substitute for each. We * end up with a regexp like '(word1|word2|word3)'. *) let rex = Pcre.regexp ~flags:[`CASELESS] ("(" ^ String.concat "|" search_terms ^ ")") in (* Do the substitution, but only on text, not elements! *) let body = let subst text = "" ^ text ^ "" in List.map (fun str -> if String.length str > 0 && str.[0] != '<' then Pcre.substitute ~rex ~subst str else str) body in (* Join the XHTML fragments back together again. *) String.concat "" (List.concat [ head ; body ; tail ]) in (* This code generates ordinary pages. *) let make_page title description pageid last_modified_date has_page_css version page page' = let t = template_page in t#set "title" title; t#set "description" description; t#set "pageid" (string_of_int pageid); t#set "last_modified_date" (printable_date last_modified_date); if page <> page' then (* redirection *) ( t#set "page" page'; t#set "original_page" page; (* XXX title - get it from database *) t#conditional "redirected" true ) else ( t#set "page" page; t#conditional "redirected" false ); t#conditional "has_host_css" has_host_css; t#conditional "has_page_css" has_page_css; t#conditional "has_feedback_email" has_feedback_email; t#conditional "mailing_list" mailing_list; t#conditional "search_box" search_box; t#conditional "can_edit" can_edit; t#conditional "can_manage_users" can_manage_users; t#conditional "can_manage_contacts" can_manage_contacts; t#conditional "can_manage_site" can_manage_site; t#conditional "can_edit_global_css" can_edit_global_css; t#conditional "has_stats" has_stats; (* Pull out the sections in this page. *) let sth = dbh#prepare_cached "select ordering, sectionname, content, divname from contents where pageid = ? order by ordering" in sth#execute [`Int pageid]; let sections = sth#map (function [`Int ordering; (`Null | `String _) as sectionname; `String content; (`Null | `String _) as divname] -> let divname, has_divname = match divname with `Null -> "", false | `String divname -> divname, true in let sectionname, has_sectionname = match sectionname with `Null -> "", false | `String sectionname -> sectionname, true in let linkname = linkname_of_sectionname sectionname in [ "ordering", Template.VarString (string_of_int ordering); "has_sectionname", Template.VarConditional has_sectionname; "sectionname", Template.VarString sectionname; "linkname", Template.VarString linkname; "content", Template.VarString (Wikilib.xhtml_of_content dbh hostid content); "has_divname", Template.VarConditional has_divname; "divname", Template.VarString divname ] | _ -> assert false) in t#table "sections" sections; (* Are we showing an old version of the page? If so, warn. *) (match version with None -> t#conditional "is_old_version" false | Some pageid -> t#conditional "is_old_version" true; t#set "old_version" (string_of_int pageid)); (* Login status. *) (match user with Anonymous -> t#conditional "user_logged_in" false | User (_, username, _) -> t#conditional "user_logged_in" true; t#set "username" username); (* If we are coming from a search engine then we want to highlight * search terms throughout the whole page ... *) try let referer = Table.get (Request.headers_in r) "Referer" in let search_terms = search_terms_from_referer referer in (* Highlight the search terms. *) let xhtml = t#to_string in let xhtml = highlight_search_terms xhtml search_terms "search_term" in (* Deliver the page. *) q#header (); print_string r xhtml with Not_found -> (* No referer / no search terms / not a search engine referer. *) q#template t in (* This code generates 404 pages. *) let make_404 () = Request.set_status r 404; (* Return a 404 error code. *) let t = template_404 in t#set "page" page; let search_terms = String.map (function ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c | _ -> ' ') page in t#set "search_terms" search_terms; t#conditional "has_host_css" has_host_css; t#conditional "can_edit" can_edit; t#conditional "can_manage_users" can_manage_users; t#conditional "can_manage_contacts" can_manage_contacts; t#conditional "can_manage_site" can_manage_site; t#conditional "can_edit_global_css" can_edit_global_css; t#conditional "has_stats" has_stats; q#template t in (* Fetch a page by name. This function can give three answers: * (1) Page fetched OK (fetches some details of the page). * (2) Page is a redirect (fetches the name of the redirect page). * (3) Page not found in database, ie. 404 error. *) (* XXX Should do a case-insensitive matching of URLs, and if the URL differs * in case only should redirect to the lowercase version. *) let fetch_page page version allow_redirect = match version with | None -> if allow_redirect then ( let sth = dbh#prepare_cached "select redirect, id, title, description, last_modified_date, css is not null from pages where hostid = ? and url = ?" in sth#execute [`Int hostid; `String page]; (try (match sth#fetch1 () with | [ `Null; `Int id; `String title; `String description; `Timestamp last_modified_date; `Bool has_page_css ] -> FPOK (id, title, description, last_modified_date, has_page_css) | `String redirect :: _ -> FPRedirect redirect | _ -> assert false) with Not_found -> FPNotFound) ) else (* redirects not allowed ... *) ( let sth = dbh#prepare_cached "select id, title, description, last_modified_date, css is not null from pages where hostid = ? and url = ?" in sth#execute [`Int hostid; `String page]; (try (match sth#fetch1 () with | [ `Int id; `String title; `String description; `Timestamp last_modified_date; `Bool has_page_css ] -> FPOK (id, title, description, last_modified_date, has_page_css) | _ -> assert false) with Not_found -> FPNotFound) ) | Some version -> let sth = dbh#prepare_cached "select id, title, description, last_modified_date, css is not null from pages where hostid = ? and id = ? and (url = ? or url_deleted = ?)" in sth#execute [`Int hostid; `Int version; `String page; `String page]; (try (match sth#fetch1 () with | [ `Int id; `String title; `String description; `Timestamp last_modified_date; `Bool has_page_css ] -> FPOK (id, title, description, last_modified_date, has_page_css) | _ -> assert false) with Not_found -> FPNotFound) in (* Here we deal with the complex business of redirects and versions. *) (* Only allow the no_redirect and version syntax for editors. *) let allow_redirect, version = if can_edit then ( not (q#param_true "no_redirect"), try Some (int_of_string (q#param "version")) with Not_found -> None ) else (true, None) in let rec loop page' i = if i > max_redirect then ( error ~title:"Too many redirections" ~back_button:true q ("Too many redirects between pages. This may happen because " ^ "of a cycle of redirections."); return () ) else match fetch_page page' version allow_redirect with | FPOK (pageid, title, description, last_modified_date, has_page_css)-> make_page title description pageid last_modified_date has_page_css version page page' | FPRedirect page' -> loop page' (i+1) | FPNotFound -> make_404 () in loop page 0 let () = register_script run