1 # =====================================================================
2 # groupFileList.awk: RPC I/O function for rpclib/groupFileList.
3 #
4 # Copyright (c) 2007,2008,2009,2010 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,fmt,\
26 stem1,stem2,blksize,blkno,\
27 curr,first,last,tot,fmt1,\
28 tmp) {
29
30 if (mode == _O_REQUEST) { # request.
31
32 # target group
33 value = _request("1",1)
34
35 # group must not be null and it may not contain the
36 # unescaped ``.'' character.
37
38 if (value != _NULL && value !~ /\./) {
39 _rcset("cgi.group",unixify(value))
40 _rcset("cgi.group.literal",value)
41 }
42
43 # what to list:
44 #
45 # links: all linkable objects in the specified group
46 # images: all embeddable images in the specified group
47 # media: all embedable multimedia objects in the specified group
48
49 value = _request("2",1)
50
51 # add more types as needed.
52 if (value ~ /^(links|images|media)$/) _rcset("cgi.what",value)
53
54 # where to look for things to list:
55 #
56 # web: stuff in the public Web area
57 # wiki: stuff in the private Wiki area
58 #
59 # Note: this parameter is currently not used.
60
61 value = _request("3",1)
62
63 # add more types as needed.
64 if (value ~ /^(web|wiki)$/) _rcset("cgi.where",value)
65
66 # Optional starting page no. for paged results.
67 if ((value=_request("4",1)/1) > 0) _rcset("cgi.paging",value)
68
69 # optional search keyword, with any special chars turned
70 # into the "." wildcard.
71 if ((value=_request("5",1)) != _NULL) {
72 _rcset("cgi.grep.literal",value)
73 gsub(/[^a-zA-Z0-9]/,".",value)
74 _rcset("cgi.grep.pattern",value)
75 }
76
77 # Page visual appearance.
78 if ((value=_request("6",1)) ~ /^(screen|print|toc)$/)
79 _rcset("cgi.style",value)
80
81 # The following test is necessary since the address could,
82 # at least in theory, have been set to any string by the
83 # remote user, due to how it is handled to cope with stunnel(8)
84 # and the lack of transproxy support in kernel 2.4.x.
85
86 if (_isipaddr(ENVIRON["REMOTE_ADDR"]) == _TRUE)
87 value = ENVIRON["REMOTE_ADDR"]
88 else value = "0.0.0.0"
89
90 _rcset("REMOTE_ADDR",value)
91 }
92
93 else { # response
94
95 outfile = _rcget("tpl.include.tw.page")
96 if (outfile !~ /^\/\.*[a-zA-Z0-9]/)
97 return(_sys("csaExit.fault 0041 outfile"))
98
99 # Set output format string.
100
101 fmt = readfmt("tw-group-filelist")
102 gsub(/%/,"%%",fmt) # turn plain '%' into '%%'.
103 gsub(/\\/,"\\\\&",fmt) # turn '\' into '\\'.
104 gsub(/[\n\r]+/,"",fmt) # just in case.
105
106 # this format string is currently not customizable.
107 if (!sub(//,"%s",fmt)) fmt = fmt ""
108 if (!sub(//,"%s",fmt)) fmt = fmt ""
109
110 # encode any extra markers.
111 gsub(//, "\\<tw:s/\\>",fmt)
112
113 fmt = fmt "\n"
114
115 # URL stem for wiki pages and private objects.
116 stem1 = ENVIRON["CSA_RPC_URI"] "/" \
117 ENVIRON["CSA_LANG"] "/" \
118 _rcget("tbl_group.g_uri")
119
120 # URL stem for public objects.
121 stem2 = ENVIRON["CSA_URL"] "/" ENVIRON["CSA_LANG"]
122 if (ENVIRON["TNS_ATTACH_PUBDIR"] != _NULL)
123 stem2 = stem2 "/" ENVIRON["TNS_ATTACH_PUBDIR"]
124
125 # trigger paging if appropriate.
126 if ((blkno=_rcget("cgi.paging"))) {
127 blksize = _rcget("TNS_PAGER_BLKSIZE",1)
128 if ((blksize/=1) <= 0) blksize = 10
129 first = blkno * blksize - blksize + 1
130 last = first + blksize - 1
131 }
132
133 # read input table.
134
135 i=1
136 while (split(_TBLS[1,i++],a,"\t")) {
137
138 # Handle output paging.
139 if (blkno && ++tot && (++curr < first || curr > last)) continue
140
141 # targets are either wiki pages or private objects ?
142 if (a[1] ~ /^[\/:]/) {
143
144 # strip leading directory path and leading/trailing TW stuff.
145 #sub(/.*\//,"",a[1])
146
147 if (sub(/^:\.\//,"/",a[1])) sub(/\+/,"/wikifile/",a[1])
148
149 url = stem1 a[1]
150 }
151
152 # targets are public objects.
153 else {
154 sub(/^\.\//,"",a[1])
155 url = stem2 "/" a[1]
156 }
157
158 # Let's keep the '+' separator from being URL-decoded
159 # into a space.
160
161 gsub(/\+/,"%2B",a[1])
162 value = _uridecode(a[1],_O_PATHINFO)
163
164 printf(fmt,_xmlencode(url),_xmlencode(value)) > outfile
165
166 # Provide also a machine-readable format. This is currently
167 # as it seems to cure the problem stated ad near the end of
168 # rpclib/groupFileList regarding excessive output.
169 #_wsresponse(1,i-1,url)
170 }
171
172 close(outfile)
173
174 # show the pager if appropriate.
175 if (blkno && tot > blksize) {
176
177 # Set pager output format string.
178
179 fmt1 = readfmt("tw-pager")
180 gsub(/%/,"%%",fmt1) # turn plain '%' into '%%'.
181 gsub(/\\/,"\\\\&",fmt1) # turn '\' into '\\'.
182 gsub(/[\n\r]+/,"",fmt1) # just in case.
183
184 # The pager format string is currently not customizable.
185 # And, yes, this must be done twice.
186 if (!sub(//,"%s",fmt1)) fmt1 = fmt1 ""
187 if (!sub(//,"%s",fmt1)) fmt1 = fmt1 ""
188
189 # encode any extra markers.
190 gsub(//,"\\<tw:s/\\>",fmt1)
191
192 fmt1 = fmt1 "\n"
193
194
195 url = ENVIRON["CSA_RPC_URI"] "/" \
196 ENVIRON["CSA_LANG"] "/" _rcget("tbl_group.g_uri") "/"
197
198 if (_rcget("cgi.where") == "wiki") url = url "3"
199 else url = url "2"
200
201 url = url "/" _rcget("cgi.what") \
202 "?5=" _uriencode(_rcget("cgi.grep.literal")) \
203 "?6=" _rcget("cgi.style")
204
205 # modify here to include the search parameters (if any)
206 # in the pager links.
207 #if ((tmp=_rcget("cgi.subcat")) != _NULL) url = url "/" tmp "/"
208 #else url = url "?3="
209
210 url = url "?4="
211
212 tot % blksize ? tmp = 1 : tmp = 0
213
214 _response("tpl.var.tw.pager",\
215 pager(blkno,int(tot/blksize)+tmp,url,fmt1))
216 }
217
218
219 # generic template conditionals.
220
221 ifsections()
222 }
223 }
224
225 # EOF