1 (* An example of a pre-page handler and an external function.
2 * $Id: cdvmm_phone_numbers.ml,v 1.1 2006/07/27 16:46:55 rich Exp $
10 open Cocanwiki_extensions
12 (* Check we're running against the correct website. *)
14 Pcre.regexp "(\\.cdvmortgage\\.com|chasedevere\\.team-?notepad\\.com)$"
17 try Request.hostname r
19 failwith "Cdvmm_phone_numbers: no Host header sent in request" in
20 Pcre.pmatch ~rex (String.lowercase hostname)
22 (* The phone numbers. *)
24 "bweb", "0800 358 5062"; (* Bweb *)
25 "cweb", "0800 358 5063"; (* Cweb *)
26 "dweb", "0800 358 5064"; (* Dweb *)
27 "eweb", "0800 358 5066"; (* Eweb *)
28 "fweb", "0800 358 5067"; (* Fweb *)
29 "aweb", "0800 358 5068"; (* Aweb (not paid) *)
30 "mse", "0800 358 5533"; (* Moneysavingexpert *)
31 "euro", "0800 358 1780"; (* Euro/dollar/euribor/libor *)
32 "mass", "0800 358 1781"; (* Mortgage broker *)
33 "offset", "0800 358 1782"; (* Offset *)
34 "hnw", "0800 358 1783"; (* Professionals / HNW *)
35 "btl", "0800 358 1784"; (* Buy to let *)
36 "sp", "0800 358 1785"; (* Subprimes *)
37 "selfcert", "0800 358 1786"; (* Self-cert *)
38 "hweb", "0800 358 1787"; (* Hweb (appears in brand adverts) *)
39 "iweb", "0800 358 1788"; (* Iweb Bidvertiser *)
42 (* Default numbers go to Fweb. *)
43 let default_id = "fweb"
44 let default_number = "0800 358 5067"
46 (* The name of the cookie. *)
47 let cookie_name = "phone"
49 (* When cookies expire. *)
50 let expires = "Wed, 18-May-2033 04:33:20 GMT"
52 (* Get the phone cookie if the browser sent one.
53 * If no phone cookies, raises Not_found.
55 let get_phone_cookie q = q#cookie cookie_name
57 let mse_re = Pcre.regexp ~flags:[`CASELESS] "moneysavingexpert"
59 let pre_page r (q : cgi) dbh hostid _ =
60 if check_website r then (
63 (* Get the phone cookie, if it exists. *)
64 let phone = get_phone_cookie q in
65 let phone = phone#value in
67 (* Is it a valid cookie? If not this raises Not_found and we
68 * treat it as if we hadn't seen a cookie at all.
70 ignore (List.assoc phone numbers);
74 Not_found -> (* No cookie or invalid cookie - send one. *)
75 (* Which cookie should we send? *)
77 let headers = Request.headers_in r in
79 try Table.get headers "Referer" with Not_found -> "" in
80 if Pcre.pmatch ~rex:mse_re referer then
84 try q#param "utm_source" with Not_found -> "" in
86 try q#param "utm_campaign" with Not_found -> "" in
88 if String.starts_with utm_campaign "currency" ||
89 String.starts_with utm_campaign "libor" then
91 else if String.starts_with utm_campaign "mass" then
93 else if String.starts_with utm_campaign "offset" then
95 else if String.starts_with utm_campaign "hnw" then
97 else if String.starts_with utm_campaign "buy" then
99 else if String.starts_with utm_campaign "sub" then
101 else if String.starts_with utm_campaign "self" then
103 else if String.starts_with utm_source "bidver" then
109 let cookie = Cookie.cookie cookie_name id ~path:"/" ~expires in
110 Table.set (Request.headers_out r) "Set-Cookie" cookie#to_string;
113 (* Make a note of the id which we can use in the {{phone}}
114 * external function (defined below).
116 let notes = Request.notes r in
117 Table.set notes cookie_name id
120 let phone r dbh hostid _ =
121 if check_website r then (
122 (* Have we got a noted phone number? *)
123 let notes = Request.notes r in
126 Table.get notes cookie_name
129 prerr_endline "Cdvmm_phone_numbers: warning: no 'phone' note";
132 (* Is it a valid note? Get the phone number itself. *)
134 try List.assoc id numbers
137 prerr_endline ("Cdvmm_phone_numbers: warning: bad id: " ^ id);
140 (* Return the number. *)
144 "{{phone}}" (* XXX Should be able to decline this call. *)
146 (* Register pre-page handler and external function. *)
148 pre_page_handlers := pre_page :: !pre_page_handlers;
149 external_functions := ("phone", phone) :: !external_functions