1 (* An example of a pre-page handler and an external function.
2 * $Id: cdvmm_phone_numbers.ml,v 1.3 2006/09/11 09:36:43 rich Exp $
10 open Cocanwiki_extensions
11 open Cocanwiki_strings
13 (* Check we're running against the correct website. *)
15 Pcre.regexp "(cdvmortgage\\.co(\\.uk|m)|chasedevere\\.team-?notepad\\.com)$"
18 try Request.hostname r
20 failwith "Cdvmm_phone_numbers: no Host header sent in request" in
21 Pcre.pmatch ~rex (lowercase hostname)
23 (* The phone numbers. *)
25 "bweb", "0800 358 5062"; (* Bweb *)
26 "cweb", "0800 358 5063"; (* Cweb *)
27 "dweb", "0800 358 5064"; (* Dweb *)
28 "eweb", "0800 358 5066"; (* Eweb *)
29 "fweb", "0800 358 5067"; (* Fweb *)
30 "aweb", "0800 358 5068"; (* Aweb (not paid) *)
31 "mse", "0800 358 5533"; (* Moneysavingexpert *)
32 "euro", "0800 358 1780"; (* Euro/dollar/euribor/libor *)
33 "mass", "0800 358 1781"; (* Mortgage broker *)
34 "offset", "0800 358 1782"; (* Offset *)
35 "hnw", "0800 358 1783"; (* Professionals / HNW *)
36 "btl", "0800 358 1784"; (* Buy to let *)
37 "sp", "0800 358 1785"; (* Subprimes *)
38 "selfcert", "0800 358 1786"; (* Self-cert *)
39 "hweb", "0800 358 1787"; (* Hweb (appears in brand adverts) *)
40 "iweb", "0800 358 1788"; (* Iweb Bidvertiser *)
43 (* Default numbers go to Fweb. *)
44 let default_id = "fweb"
45 let default_number = "0800 358 5067"
47 (* The name of the cookie. *)
48 let cookie_name = "phone"
50 (* When cookies expire. *)
51 let expires = "Wed, 18-May-2033 04:33:20 GMT"
53 (* Get the phone cookie if the browser sent one.
54 * If no phone cookies, raises Not_found.
56 let get_phone_cookie q = q#cookie cookie_name
58 let mse_re = Pcre.regexp ~flags:[`CASELESS] "moneysavingexpert"
60 let pre_page r (q : cgi) dbh hostid _ =
61 if check_website r then (
64 (* Get the phone cookie, if it exists. *)
65 let phone = get_phone_cookie q in
66 let phone = phone#value in
68 (* Is it a valid cookie? If not this raises Not_found and we
69 * treat it as if we hadn't seen a cookie at all.
71 ignore (List.assoc phone numbers);
75 Not_found -> (* No cookie or invalid cookie - send one. *)
76 (* Which cookie should we send? *)
78 let headers = Request.headers_in r in
80 try Table.get headers "Referer" with Not_found -> "" in
81 if Pcre.pmatch ~rex:mse_re referer then
85 try q#param "utm_source" with Not_found -> "" in
87 try q#param "utm_campaign" with Not_found -> "" in
89 if String.starts_with utm_campaign "currency" ||
90 String.starts_with utm_campaign "libor" then
92 else if String.starts_with utm_campaign "mass" then
94 else if String.starts_with utm_campaign "offset" then
96 else if String.starts_with utm_campaign "hnw" then
98 else if String.starts_with utm_campaign "buy" then
100 else if String.starts_with utm_campaign "sub" then
102 else if String.starts_with utm_campaign "self" then
104 else if String.starts_with utm_source "bidver" then
110 let cookie = Cookie.cookie cookie_name id ~path:"/" ~expires in
111 Table.set (Request.headers_out r) "Set-Cookie" cookie#to_string;
114 (* Make a note of the id which we can use in the {{phone}}
115 * external function (defined below).
117 let notes = Request.notes r in
118 Table.set notes cookie_name id
121 let phone r dbh hostid _ =
122 if check_website r then (
123 (* Have we got a noted phone number? *)
124 let notes = Request.notes r in
127 Table.get notes cookie_name
130 prerr_endline "Cdvmm_phone_numbers: warning: no 'phone' note";
133 (* Is it a valid note? Get the phone number itself. *)
135 try List.assoc id numbers
138 prerr_endline ("Cdvmm_phone_numbers: warning: bad id: " ^ id);
141 (* Return the number. *)
145 "{{phone}}" (* XXX Should be able to decline this call. *)
147 (* Register pre-page handler and external function. *)
149 pre_page_handlers := pre_page :: !pre_page_handlers;
150 external_functions := ("phone", phone) :: !external_functions