| | 74 | (defclass kml-root-handler (object-handler) |
|---|
| | 75 | ()) |
|---|
| | 76 | |
|---|
| | 77 | (defun replace-all-url-hosts (string new-host) |
|---|
| | 78 | "Replaces all hostnames in STRING by NEW-HOST." |
|---|
| | 79 | (ppcre:regex-replace-all #?r"((?:https?|ftp)://)\w+(?:\.\w+)*" string #?r"\1${new-host}")) |
|---|
| | 80 | |
|---|
| | 81 | (defun replace-lang-query-params (string new-lang) |
|---|
| | 82 | (ppcre:regex-replace-all #?r"(?i)(lang=)[a-z]{2,2}" string #?r"\1${new-lang}")) |
|---|
| | 83 | |
|---|
| | 84 | (defun replace-personalized-contract-placeholder (string sponsor lang) |
|---|
| | 85 | (if (null sponsor) |
|---|
| | 86 | string |
|---|
| | 87 | (let ((contract (first (sponsor-contracts sponsor)))) |
|---|
| | 88 | (ppcre:regex-replace #?r"<!-- +personalized +contract +placemark +-->" |
|---|
| | 89 | string |
|---|
| | 90 | (cxml:with-xml-output (cxml:make-string-sink :omit-xml-declaration-p t) |
|---|
| | 91 | (write-personalized-contract-placemark-kml contract lang)))))) |
|---|
| | 92 | |
|---|
| | 93 | (defun serve-kml-root-data (&optional sponsor) |
|---|
| | 94 | (with-query-params ((lang "en")) |
|---|
| | 95 | (let* ((kml-root-data (kml-root-data-with-language lang)) |
|---|
| | 96 | (last-modified (store-object-last-change kml-root-data 0))) |
|---|
| | 97 | (hunchentoot:handle-if-modified-since last-modified ) |
|---|
| | 98 | (setf (hunchentoot:header-out :last-modified) |
|---|
| | 99 | (hunchentoot:rfc-1123-date last-modified) |
|---|
| | 100 | (hunchentoot:header-out :content-type) |
|---|
| | 101 | "application/vnd.google-earth.kml+xml" |
|---|
| | 102 | (hunchentoot:header-out :content-disposition) |
|---|
| | 103 | (format nil "attachment; filename=kml-root-~A.kml" lang)) |
|---|
| | 104 | (let ((kml-string (kml-string kml-root-data))) |
|---|
| | 105 | (setq kml-string (replace-all-url-hosts kml-string (website-host)) |
|---|
| | 106 | kml-string (replace-lang-query-params kml-string lang) |
|---|
| | 107 | kml-string (replace-personalized-contract-placeholder kml-string sponsor lang)))))) |
|---|
| | 108 | |
|---|
| | 109 | (defmethod handle-object ((handler kml-root-handler) (object sponsor)) |
|---|
| | 110 | (serve-kml-root-data object)) |
|---|
| | 111 | |
|---|
| | 112 | (defmethod handle-object ((handler kml-root-handler) (object contract)) |
|---|
| | 113 | (serve-kml-root-data (contract-sponsor object))) |
|---|
| | 114 | |
|---|
| | 115 | (defmethod handle-object ((handler kml-root-handler) (object null)) |
|---|
| | 116 | (serve-kml-root-data)) |
|---|
| | 117 | |
|---|