Added external functions allowing the markup to be extended.
[cocanwiki.git] / scripts / lib / cdvmm_phone_numbers.ml
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 $
3  *)
4
5 open Apache
6 open Cgi
7
8 open ExtString
9
10 open Cocanwiki_extensions
11
12 (* Check we're running against the correct website. *)
13 let rex =
14   Pcre.regexp "(\\.cdvmortgage\\.com|chasedevere\\.team-?notepad\\.com)$"
15 let check_website r =
16   let hostname =
17     try Request.hostname r
18     with Not_found ->
19       failwith "Cdvmm_phone_numbers: no Host header sent in request" in
20   Pcre.pmatch ~rex (String.lowercase hostname)
21
22 (* The phone numbers. *)
23 let 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 *)
40 ]
41
42 (* Default numbers go to Fweb. *)
43 let default_id = "fweb"
44 let default_number = "0800 358 5067"
45
46 (* The name of the cookie. *)
47 let cookie_name = "phone"
48
49 (* When cookies expire. *)
50 let expires = "Wed, 18-May-2033 04:33:20 GMT"
51
52 (* Get the phone cookie if the browser sent one.
53  * If no phone cookies, raises Not_found.
54  *)
55 let get_phone_cookie q = q#cookie cookie_name
56
57 let mse_re = Pcre.regexp ~flags:[`CASELESS] "moneysavingexpert"
58
59 let pre_page r (q : cgi) dbh hostid _ =
60   if check_website r then (
61     let id =
62       try
63         (* Get the phone cookie, if it exists. *)
64         let phone = get_phone_cookie q in
65         let phone = phone#value in
66
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.
69          *)
70         ignore (List.assoc phone numbers);
71
72         phone
73       with
74         Not_found -> (* No cookie or invalid cookie - send one. *)
75           (* Which cookie should we send? *)
76           let id =
77             let headers = Request.headers_in r in
78             let referer =
79               try Table.get headers "Referer" with Not_found -> "" in
80             if Pcre.pmatch ~rex:mse_re referer then
81               "mse"
82             else (
83               let utm_source =
84                 try q#param "utm_source" with Not_found -> "" in
85               let utm_campaign =
86                 try q#param "utm_campaign" with Not_found -> "" in
87
88               if String.starts_with utm_campaign "currency" ||
89                 String.starts_with utm_campaign "libor" then
90                   "euro"
91               else if String.starts_with utm_campaign "mass" then
92                 "mass"
93               else if String.starts_with utm_campaign "offset" then
94                 "offset"
95               else if String.starts_with utm_campaign "hnw" then
96                 "hnw"
97               else if String.starts_with utm_campaign "buy" then
98                 "btl"
99               else if String.starts_with utm_campaign "sub" then
100                 "sp"
101               else if String.starts_with utm_campaign "self" then
102                 "selfcert"
103               else if String.starts_with utm_source "bidver" then
104                 "iweb"
105               else
106                 default_id
107             ) in
108
109           let cookie = Cookie.cookie cookie_name id ~path:"/" ~expires in
110           Table.set (Request.headers_out r) "Set-Cookie" cookie#to_string;
111
112           id in
113     (* Make a note of the id which we can use in the {{phone}}
114      * external function (defined below).
115      *)
116     let notes = Request.notes r in
117     Table.set notes cookie_name id
118   )
119
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
124     let id =
125       try
126         Table.get notes cookie_name
127       with
128         Not_found ->
129           prerr_endline "Cdvmm_phone_numbers: warning: no 'phone' note";
130           default_id in
131
132     (* Is it a valid note?  Get the phone number itself. *)
133     let number =
134       try List.assoc id numbers
135       with
136         Not_found ->
137           prerr_endline ("Cdvmm_phone_numbers: warning: bad id: " ^ id);
138           default_number in
139
140     (* Return the number. *)
141     number
142
143   ) else
144     "{{phone}}" (* XXX Should be able to decline this call. *)
145
146 (* Register pre-page handler and external function. *)
147 let () =
148   pre_page_handlers := pre_page :: !pre_page_handlers;
149   external_functions := ("phone", phone) :: !external_functions