Logging in and logging out.
[cocanwiki.git] / scripts / page.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: page.ml,v 1.4 2004/09/07 16:19:43 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open ExtString
13
14 open Cocanwiki
15 open Cocanwiki_template
16 open Cocanwiki_ok
17 open Cocanwiki_date
18
19 let template_page = get_template "page.html"
20 let template_404  = get_template "page_404.html"
21
22 (* Maximum level of redirection. *)
23 let max_redirect = 4
24
25 type fp_status = FPOK of int * string * string * Dbi.datetime * bool
26                | FPRedirect of string
27                | FPNotFound
28
29 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) user =
30   let page = q#param "page" in
31   let page = if page = "" then "index" else page in
32
33   (* Host-specific CSS? *)
34   let sth = dbh#prepare_cached "select css is not null from hosts
35                                  where id = ?" in
36   sth#execute [`Int hostid];
37   let has_host_css =
38     match sth#fetch1 () with
39       | [ `Bool has_host_css ] -> has_host_css
40       | _ -> assert false in
41
42   (* This code generates ordinary pages. *)
43   let make_page title description pageid last_modified_date has_page_css
44       version page page' =
45     let t = template_page in
46     t#set "title" title;
47     t#set "description" description;
48     t#set "pageid" (string_of_int pageid);
49     t#set "last_modified_date" (printable_date last_modified_date);
50     t#set "cocanwiki_package" Cocanwiki_version.package;
51     t#set "cocanwiki_version" Cocanwiki_version.version;
52
53     if page <> page' then (* redirection *) (
54       t#set "page" page';
55       t#set "original_page" page; (* XXX title - get it from database *)
56       t#conditional "redirected" true
57     ) else (
58       t#set "page" page;
59       t#conditional "redirected" false
60     );
61
62     t#conditional "has_host_css" has_host_css;
63     t#conditional "has_page_css" has_page_css;
64
65     (* Pull out the sections in this page. *)
66     let sth = dbh#prepare_cached
67                 "select ordering, sectionname, content, divname
68                    from contents
69                   where pageid = ?
70                   order by ordering" in
71     sth#execute [`Int pageid];
72
73     let sections =
74       sth#map
75         (function [`Int ordering; `String sectionname; `String content;
76                    (`Null | `String _) as divname] ->
77            let divname, has_divname =
78              match divname with
79                  `Null -> "", false
80                | `String divname -> divname, true in
81            [ "ordering", Template.VarString (string_of_int ordering);
82              "sectionname", Template.VarString sectionname;
83              "content",
84                Template.VarString
85                  (Wikilib.xhtml_of_content dbh hostid content);
86              "has_divname", Template.VarConditional has_divname;
87              "divname", Template.VarString divname ]
88            | _ -> assert false) in
89
90     t#table "sections" sections;
91
92     (* Are we showing an old version of the page?  If so, warn. *)
93     (match version with
94          None ->
95            t#conditional "is_old_version" false
96        | Some pageid ->
97            t#conditional "is_old_version" true;
98            t#set "old_version" (string_of_int pageid));
99
100     (* Login status. *)
101     (match user with
102          Anonymous ->
103            t#conditional "user_logged_in" false
104        | User (_, username, _) ->
105            t#conditional "user_logged_in" true;
106            t#set "username" username);
107
108     q#template t
109   in
110
111   (* This code generates 404 pages. *)
112   let make_404 () =
113     Request.set_status r 404;           (* Return a 404 error code. *)
114
115     let t = template_404 in
116     t#set "page" page;
117
118     let search_terms =
119       String.map
120         (function
121              ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
122            | _ -> ' ') page in
123
124     template_404#set "search_terms" search_terms;
125
126     q#template t
127   in
128
129   (* Fetch a page by name.  This function can give three answers:
130    * (1) Page fetched OK (fetches some details of the page).
131    * (2) Page is a redirect (fetches the name of the redirect page).
132    * (3) Page not found in database, ie. 404 error.
133    *)
134   (* XXX Should do a case-insensitive matching of URLs, and if the URL differs
135    * in case only should redirect to the lowercase version.
136    *)
137   let fetch_page page version allow_redirect =
138     match version with
139       | None ->
140           if allow_redirect then (
141             let sth =
142               dbh#prepare_cached
143                 "select redirect, id, title, description, last_modified_date,
144                         css is not null
145                    from pages where hostid = ? and url = ?" in
146             sth#execute [`Int hostid; `String page];
147             (try
148                (match sth#fetch1 () with
149                   | [ `Null; `Int id; `String title; `String description;
150                       `Timestamp last_modified_date; `Bool has_page_css ] ->
151                       FPOK (id, title, description, last_modified_date,
152                             has_page_css)
153                   | `String redirect :: _ ->
154                       FPRedirect redirect
155                   | _ -> assert false)
156              with
157                  Not_found -> FPNotFound)
158           ) else (* redirects not allowed ... *) (
159             let sth =
160               dbh#prepare_cached
161                 "select id, title, description, last_modified_date,
162                         css is not null
163                    from pages where hostid = ? and url = ?" in
164             sth#execute [`Int hostid; `String page];
165             (try
166                (match sth#fetch1 () with
167                   | [ `Int id; `String title; `String description;
168                       `Timestamp last_modified_date; `Bool has_page_css ] ->
169                       FPOK (id, title, description, last_modified_date,
170                             has_page_css)
171                   | _ -> assert false)
172              with
173                  Not_found -> FPNotFound)
174           )
175       | Some version ->
176             let sth =
177               dbh#prepare_cached
178                 "select id, title, description, last_modified_date,
179                         css is not null
180                    from pages
181                   where hostid = ? and id = ? and
182                         (url = ? or url_deleted = ?)" in
183             sth#execute [`Int hostid; `Int version;
184                          `String page; `String page];
185             (try
186                (match sth#fetch1 () with
187                   | [ `Int id; `String title; `String description;
188                       `Timestamp last_modified_date; `Bool has_page_css ] ->
189                       FPOK (id, title, description, last_modified_date,
190                             has_page_css)
191                   | _ -> assert false)
192              with
193                  Not_found -> FPNotFound)
194   in
195
196   (* Here we deal with the complex business of redirects and versions. *)
197   let allow_redirect = not (q#param_true "no_redirect") in
198   let version =
199     try Some (int_of_string (q#param "version")) with Not_found -> None in
200
201   let rec loop page' i =
202     if i > max_redirect then (
203       error ~title:"Too many redirections" ~back_button:true
204         q ("Too many redirects between pages.  This may happen because " ^
205            "of a cycle of redirections.");
206       raise CgiExit
207     ) else
208       match fetch_page page' version allow_redirect with
209         | FPOK (pageid, title, description, last_modified_date, has_page_css)->
210             make_page title description pageid last_modified_date has_page_css
211               version page page'
212         | FPRedirect page' ->
213             loop page' (i+1)
214         | FPNotFound ->
215             make_404 ()
216   in
217   loop page 0
218
219 let () =
220   register_script run