1 # =====================================================================
2 # listAttachments.awk: RPC I/O function for rpclib/listAttachments.
3 #
4 # Copyright (c) 2007-2021 Carlo Strozzi
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 dated June, 1991.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 # =====================================================================
20
21 # =====================================================================
22 # void _userproc(int mode)
23 # =====================================================================
24
25 function _userproc(mode, value,outfile,i,url,a,blksize,\
26 del,fmt,blkno,curr,first,last,\
27 tot,fmt1,tmp) {
28
29 if (mode == _O_REQUEST) { # request.
30
31 # target group
32 value = _request("1",1)
33
34 # group must not be null and it may not contain the
35 # unescaped ``.'' character.
36
37 if (value != _NULL && value !~ /\./) {
38 _rcset("cgi.group",unixify(value))
39 _rcset("cgi.group.literal",value)
40 }
41
42 # target page within group.
43 value = _strip(_request("2",1),_O_CRUSH)
44
45 # page name must be at least 2-character long.
46 if (length(value) > 1) {
47 _rcset("cgi.page.literal",value)
48 value = unixify(value,1)
49 _rcset("cgi.page",value)
50 }
51
52 # what to list:
53 #
54 # web: stuff in the public Web area
55 # wiki: stuff in the private Wiki area
56
57 value = _request("3",1)
58
59 # add more types as needed.
60 if (value ~ /^(web|wiki)$/) _rcset("cgi.where",value)
61
62 # optional author name.
63 if ((value=_request("twauthor",1)) != _NULL) {
64 sub(/[\t\r\n<>]+/," ",value)
65 _rcset("cgi.author",substr(_strip(value,_O_MIDDLE),1,32))
66 }
67
68 # Optional starting page no. for paged results.
69 if ((value=_request("4",1)/1) > 0) _rcset("cgi.paging",value)
70
71 # optional search keyword, with any special chars turned
72 # into the "." wildcard.
73 if ((value=_request("5",1)) != _NULL) {
74 _rcset("cgi.grep.literal",value)
75 gsub(/[^a-zA-Z0-9]/,".",value)
76 _rcset("cgi.grep.file",value)
77 }
78
79 # The following test is necessary since the address could,
80 # at least in theory, have been set to any string by the
81 # remote user, due to how it is handled to cope with stunnel(8)
82 # and the lack of transproxy support in kernel 2.4.x.
83
84 if (_isipaddr(ENVIRON["REMOTE_ADDR"]) == _TRUE)
85 value = ENVIRON["REMOTE_ADDR"]
86 else value = "0.0.0.0"
87
88 _rcset("REMOTE_ADDR",value)
89 }
90
91 else { # response.
92
93 if (ENVIRON["TNS_ATTACH_PUBDIR"] == _NULL)
94 ENVIRON["TNS_ATTACH_PUBDIR"] = "/"
95 else {
96 sub(/^\/*/,"/",ENVIRON["TNS_ATTACH_PUBDIR"])
97 sub(/\/*$/,"/",ENVIRON["TNS_ATTACH_PUBDIR"])
98 }
99
100 # Make sure that if pubdir is null it is set to "/",
101 # and strip any repeated slashes afterwards.
102 #
103 #ENVIRON["TNS_ATTACH_PUBDIR"] = "/" ENVIRON["TNS_ATTACH_PUBDIR"] "/"
104 #gsub(/\/+/,"/",ENVIRON["TNS_ATTACH_PUBDIR"])
105
106 outfile = _rcget("tpl.include.tw.page")
107 if (outfile !~ /^\/\.*[a-zA-Z0-9]/)
108 return(_sys("csaExit.fault 0041 outfile"))
109
110 # Set attachment list format string.
111
112 fmt = readfmt("tw-page-attachments")
113 gsub(/%/,"%%",fmt) # turn plain '%' into '%%'.
114 gsub(/\\/,"\\\\&",fmt) # turn '\' into '\\'.
115 gsub(/[\n\r]+/,"",fmt) # just in case.
116
117 # the attachment format string is currently not customizable.
118 for (i=1; i<=5; i++) {
119 if (!sub(//,"%s",fmt)) fmt = fmt ""
120 }
121
122 # encode any extra markers.
123 gsub(//,"\\<tw:s/\\>",fmt)
124
125 fmt = fmt "\n"
126
127 #del = ENVIRON["CSA_CGI_STEM"] ENVIRON["CSA_REQUEST_URI"] "/"
128 #gsub(/.UNKNOWN/,_NULL,del) # can be /UNKNOWN?UNKNOWN
129 #gsub(/ /,"%20",del) # just in case.
130
131 del = ENVIRON["CSA_RPC_URI"] "/" \
132 ENVIRON["CSA_LANG"] "/" _rcget("tbl_group.g_uri")
133
134 if (_rcget("cgi.where") == "wiki") {
135
136 del = url = del "/" _rcget("tbl_page.p_uri") "/wikifile/"
137
138 _response("tpl.var.nls.1",\
139 _nlsmap(_NULL,"private","listAttachments"))
140 }
141
142 else {
143 del = del "/tw-webfile/"
144
145 url = ENVIRON["CSA_URL"] "/" ENVIRON["CSA_LANG"] \
146 ENVIRON["TNS_ATTACH_PUBDIR"] _rcget("cgi.group") "/"
147
148 _response("tpl.var.nls.1",\
149 _nlsmap(_NULL,"public","listAttachments"))
150 }
151
152 # trigger paging if appropriate.
153 if ((blkno=_rcget("cgi.paging"))) {
154 blksize = _rcget("TNS_PAGER_BLKSIZE",1)
155 if ((blksize/=1) <= 0) blksize = 10
156 first = blkno * blksize - blksize + 1
157 last = first + blksize - 1
158 }
159
160 # Make sure output file is cleared, in case we are re-using
161 # an old temporary file and we have nothing to print to it.
162 _creat(outfile,_O_TRUNC)
163
164 # read input table.
165 # k_attach, a_mtime, a_modip, a_author, a_descr, a_allow
166
167 i=1
168 while (split(_TBLS[1,i++],a,"\t")) {
169
170 # Handle output paging.
171 if (blkno && ++tot && (++curr < first || curr > last)) continue
172
173 sub(/^(wiki|web)./,"",a[1]) # strip prefix.
174
175 # Remove any explicit exclusion directive. At the moment this
176 # field is not used here, but any such directives are to be
177 # ignored in this view anyway, because it is restricted to
178 # editors, who must be able to list everithing.
179 sub(/^ *! */,_NULL,a[5])
180
181 # Strip leading and trailing commas from group names.
182 sub(/^,+/,"",a[6]); sub(/,+$/,"",a[6])
183
184 # Make the listing more compact.
185 sub(/T/," ",a[2]); sub(/\+.*/,_NULL,a[2])
186
187 printf(fmt,a[2],_xmlencode(url a[1]),_xmlencode(a[1]),\
188 _xmlencode(a[6]),\
189 _xmlencode(del _uriencode(a[1],_O_PATHINFO))) > outfile
190
191 # Provide also a machine-readable format.
192 _wsresponse(1,i-1,url a[1])
193 _wsresponse(2,i-1,a[2])
194 }
195
196 close(outfile)
197
198 # show the pager if appropriate.
199 if (blkno && tot > blksize) {
200
201 # Set pager output format string.
202
203 fmt1 = readfmt("tw-pager")
204 gsub(/%/,"%%",fmt1) # turn plain '%' into '%%'.
205 gsub(/\\/,"\\\\&",fmt1) # turn '\' into '\\'.
206 gsub(/[\n\r]+/,"",fmt1) # just in case.
207
208 # The pager format string is currently not customizable.
209 # And, yes, this must be done twice.
210 if (!sub(//,"%s",fmt1)) fmt1 = fmt1 ""
211 if (!sub(//,"%s",fmt1)) fmt1 = fmt1 ""
212
213 # encode any extra markers.
214 gsub(//,"\\<tw:s/\\>",fmt1)
215
216 fmt1 = fmt1 "\n"
217
218
219 url = ENVIRON["CSA_RPC_URI"] "/" \
220 ENVIRON["CSA_LANG"] "/" _rcget("tbl_group.g_uri") "/"
221
222 if (_rcget("cgi.where") == "wiki")
223 url = url _rcget("tbl_page.p_uri") "/wikifile"
224 else url = url "tw-webfile"
225
226 # modify here to include the search parameters (if any)
227 # in the pager links.
228 #if ((tmp=_rcget("cgi.subcat")) != _NULL) url = url "/" tmp "/"
229 #else url = url "?3="
230
231 url = url "?5=" _uriencode(_rcget("tpl.var.tw.arg5")) "&4="
232
233 tot % blksize ? tmp = 1 : tmp = 0
234
235 _response("tpl.var.tw.pager",\
236 pager(blkno,int(tot/blksize)+tmp,url,fmt1))
237 }
238
239 # generic template conditionals.
240
241 ifsections()
242 }
243 }
244
245 # EOF