aboutsummaryrefslogtreecommitdiff
path: root/src/ellinika/cgi.scm4
blob: 28adea1feeb5d74671f1f5ae3f134eeda24ddc45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
;;;; Greek Dictionary Web Engine       -*- scheme -*-
;;;; Copyright (C) 2005, 2007, 2010, 2015 Sergey Poznyakoff
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;;;
(define-module (ellinika cgi)
  #:use-module (ellinika config)
  #:use-module (ellinika i18n)
  #:use-module ifelse(IFACE,[CGI],(www cgi),(guile-user))
  #:use-module (ice-9 iconv)
  #:use-module (rnrs bytevectors)
  #:re-export (base-dir html-dir sysconf-dir locale-path
			ellinika-sql-connection
			config-file-name ))


ifelse(IFACE,[CGI],[
(define-public cgi-script-name
  (cgi:getenv 'script-name))
(define-public cgi-server-hostname
  (cgi:getenv 'server-hostname))
(define-public cgi-server-protocol-name
  (cgi:getenv 'server-protocol-name))
(define-public cgi-server-protocol-version
  (cgi:getenv 'server-protocol-version))
(define-public (cgi:value-u8 key)
  (let ((x (cgi:value key)))
    (if x
	(utf8->string (string->bytevector x "ISO-8859-1"))
	x)))
],[
(define form-data
  (append
   (parse-form-data
    (table:get (request-rec:subprocess-env Request) "QUERY_STRING"))
   (if (= (request-rec:method-number Request) 2)
       (parse-form-data (read-post-data Request))
       '())))

(define-public (cgi:value name)
  (assoc-ref form-data name))

(define-public (cgi:names)
  (map car form-data))

(define-public cgi-script-name
  (table:get (request-rec:subprocess-env Request)
	     "SCRIPT_NAME"))

(define-public cgi-server-hostname
  (table:get (request-rec:subprocess-env Request)
	     "SERVER_NAME"))

(define-public cgi-server-protocol-name #f)
(define-public cgi-server-protocol-version #f)

(let* ((server-protocol (table:get (request-rec:subprocess-env Request)
	                            "SERVER_PROTOCOL")))
    (if server-protocol
        (let ((slash (string-index server-protocol #\/)))
          (set! cgi-server-protocol-name     (substring server-protocol
                                                        0 slash))
          (set! cgi-server-protocol-version  (substring server-protocol
                                                        (1+ slash))))))

])


;;; User-definable variables
(define-public dict-template-file-name "dict.html")
(define-public nea-template-file-name "nea.html")
(define-public monima-nea-template-file-name "monima.html")
(define-public target-language "el_GR")

(define-public word-forms-reference '())

(define-public ref-loc #f)

;; Number of colums in fuzzy search output
(define-public match-list-columns 4)    
;;; End of user-definable variables

(define-public (language-code lang)
  (cond
   ((string-index lang #\_) =>
    (lambda (len)
      (substring lang 0 len)))
   (else
    lang)))

(define-public (template-file lang template-file-name)
  (string-append html-dir "/" (language-code lang) "/" template-file-name))

(define-public (make-cgi-name cgi-path . rest)
  (apply
   string-append
   (cons
    cgi-path
    (let ((arglist (let ((lang (cgi:value "lang")))
		     (do ((ilist (if lang
				     (cons "lang" (cons lang rest))
				     rest) (cdr ilist))
			  (i 1 (1+ i))
			  (olist '()))
			 ((null? ilist) (if (null? olist)
					    olist
					    (reverse (cdr olist))))
		       (set! olist (cons (car ilist) olist))
		       (set! olist (cons 
				    (if (odd? i) "=" "&amp;")
				    olist))))))
      (if (null? arglist)
	  arglist
	  (cons "?" arglist))))))

(define-public (cgi-program-name prog)
  (string-append
   (substring cgi-script-name 0 (string-rindex cgi-script-name #\/)) "/"
   prog ifelse(SCRIPT_SUFFIX,,,"SCRIPT_SUFFIX")))

(define-public (expand-template explist template)
  "(expand-template EXPLIST TEMPLATE)

Expands string TEMPLATE in accordance with EXPLIST. EXPLIST is a list
of elements:

  (cons WORD THUNK)

Each occurrence of WORD in TEMPLATE is replaced with the return value of
THUNK.
"
  (let loop ((template template))
    (cond
     ((string-index template #\@) =>
      (lambda (w)
	(display (substring template 0 w))
	(if (and (< (+ w 2) (string-length template))
		 (char=? (string-ref template (1+ w)) #\@))
	    (let ((end-pos (string-index template #\@ (+ w 2))))
	      (if (and end-pos
		       (< (1+ end-pos) (string-length template))
		       (char=? (string-ref template (1+ end-pos)) #\@))
		  (let* ((name (substring template w (+ end-pos 2)))
			 (entry (assoc name explist)))
		    (cond
		     (entry
		      ((cdr entry))
		      (loop (substring template (+ end-pos 2))))
		     (else
		      (display "@@")
		      (loop (substring template (+ w 2))))))
		  (begin
		    (display "@")
		    (loop (substring template (+ w 1))))))
	    (begin
	      (display "@")
	      (loop (substring template (1+ w)))))))
     (else
      (display template)))))

  
(define-public (ellinika-cgi-init template-file-name)
  ;;; Load the site defaults
  (ellinika-config-setup)
  
  ;;; Load the language-specific defaults
  (cond
   ((cgi:value "lang") =>
    (lambda (x)
      (if (file-exists? (template-file x template-file-name))
	  (set! target-language x)))))
  ;;; Initialize i18n
  (let ((x (locale-setup target-language "PACKAGE" locale-path)))
    (if x
	(set! target-language x))))

;; Protect occurences of " in a string.
;; Usual backslash escapes do not work in INPUT widgets, so I
;; change all quotation marks to &#34;
;; Possibly not the better solution, though...
(define-public (cgi-protect-quotes string)
  (list->string
   (apply append
	  (map
	   (lambda (x)
	     (if (eq? x #\")
		 (list #\& #\# #\3 #\4 #\;)
		 (list x)))
	   (string->list string)))))


;;; End of cgi.scmi

Return to:

Send suggestions and report system problems to the System administrator.