25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# Setup:
set BASEURL https://sqlite.org/u
set ADMIN_CAPABILITY i
set DATABASE /url-shortener.db
#
########
package require wapp
# Open the shortener database. Initialize it if necessary.
#
proc open-database {} {
global DATABASE
sqlite3 db $DATABASE
db eval {
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
# Setup:
set BASEURL https://sqlite.org/u
set ADMIN_CAPABILITY i
set DATABASE /url-shortener.db
#
########
package require wapp
# Common header for all pages that do no redirect
#
proc common-header {} {
wapp-trim {
<div class='fossil-doc' data-title='URL Shortener'>
}
}
proc common-footer {} {
wapp-trim {
</div>
}
}
# Open the shortener database. Initialize it if necessary.
#
proc open-database {} {
global DATABASE
sqlite3 db $DATABASE
db eval {
|
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
|
) WITHOUT ROWID;
}
}
# Debugging: show the environment
proc wapp-page-env {} {
wapp-allow-xorigin-params
wapp-trim {
<h1>Wapp Environment</h1>
<pre>%html([wapp-debug-env])</pre>
}
}
# Issue the "Not Found" page
proc wapp-page-notfound {} {
wapp-reply-code "404 Not Found"
wapp-trim {
<h1>Not Found</h1>
<p>No such resource:
"%html%([wapp-param BASE_URL])%%html%([wapp-param PATH_INFO])%"</p>
}
}
# Do a redirect
proc wapp-default {} {
set x [wapp-param PATH_HEAD {}]
if {$x==""} {
wapp-page-search
return
}
open-database
set redir [db one {SELECT redir FROM url WHERE name=$x}]
if {$redir==""} {
wapp-page-notfound
return
}
wapp-redirect $redir
}
# Check credentials. Return true to abort because credentials
# are invalid.
#
proc check-credentials {} {
global ADMIN_CAPABILITY env
if {![info exists env(FOSSIL_CAPABILITIES)]} {
wapp-page-notfound
return 1
}
return 0
}
# Search for an existing entry
#
proc wapp-page-search {} {
if {[check-credentials]} return
set base [wapp-param BASE_URL]
wapp-trim {
<h1>Admin URL Shortener At %html%($::DATABASE)%</h1>
<p><a href="%url($base)/editentry">Create-New-URL</a>
<p>
<form>
Search: <input type="text" size="60" name="s">
<input type="submit" name="s1" value="Go">
</form>
}
if {[wapp-param-exists s]} {
set s [wapp-param s]
wapp-trim {
<h2>Search results for %html($s):</h2><ol>
}
open-database
set pattern %$s%
set cnt 0
db eval {
SELECT name, redir FROM url
WHERE name LIKE $pattern OR redir LIKE $pattern
} {
wapp-trim {
<li><a href="%url($base)/editentry/%url($name)">%html($name)</a>
}
incr cnt
}
wapp-trim {</ol>}
if {$cnt==0} {
wapp-trim {
<p>No matches found</p>
}
}
}
}
# Create or edit a new URL shortener entry.
#
# Name of the entry to be editted is in PATH_TAIL. Or if PATH_TAIL is
# blank, create a new entry.
#
proc wapp-page-editentry {} {
global BASEURL
if {[check-credentials]} return
if {[wapp-param-exists cancel]} {
wapp-redirect [wapp-param BASEURL]/search
return
}
open-database
set hardname [wapp-param PATH_TAIL {}]
set name [wapp-param name $hardname]
set redir [wapp-param redir {}]
set notes [wapp-param notes {}]
wapp-trim {
<head></head>
<body>
}
set errmsg {}
if {[wapp-param-exists s1]} {
if {$hardname==""} {
set e [db one {SELECT count(*) FROM url WHERE name=$name}]
if {$e==1} {
set errmsg "Shortened URL name "$name" already exists.\
Pick a different name."
} elseif {[string length $name]<4} {
set errmsg "Shortened URL name "$name" too short.\
Must be at least 4 characters."
} elseif {[regexp {[^a-z0-9]} $name]} {
set errmsg "Shortened URL name "$name" contains invalid characters. \
The name must be lower-case ASCII letters and digits only."
} else {
set user $::env(FOSSIL_USER)
db eval {
INSERT INTO url(name,redir,user,notes,ctime,mtime)
VALUES($name,$redir,$user,$notes,datetime(),datetime())
}
set hardname $name
|
>
>
|
|
|
|
|
>
<
>
|
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
|
|
|
>
>
>
|
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
) WITHOUT ROWID;
}
}
# Debugging: show the environment
proc wapp-page-env {} {
wapp-allow-xorigin-params
common-header
wapp-trim {
<h1>Wapp Environment</h1>
<pre>%html([wapp-debug-env])</pre>
}
common-footer
}
# Issue the "Not Found" page
proc wapp-page-nf {} {
wapp-reply-code "404 Not Found"
wapp-trim {
<h1>Not Found</h1>
<p>No such resource:
"%html%([wapp-param BASE_URL])%%html%([wapp-param PATH_INFO])%"</p>
}
}
# Do a redirect
proc wapp-default {} {
set x [wapp-param PATH_HEAD {}]
if {$x==""} {
wapp-page-sx
return
}
open-database
set redir [db one {SELECT redir FROM url WHERE name=$x}]
if {$redir==""} {
wapp-page-nf
return
}
wapp-redirect $redir
}
# Check credentials. Return true to abort because credentials
# are invalid.
#
proc check-credentials {} {
global ADMIN_CAPABILITY env
if {![info exists env(FOSSIL_CAPABILITIES)]} {
wapp-page-nf
return 1
}
return 0
}
# Search for an existing entry
#
proc wapp-page-sx {} {
if {[check-credentials]} return
common-header
set base [wapp-param BASE_URL]
wapp-trim {
<p>
<a href="%url($base)/ee">Create-New-URL</a>
<a href="%url($base)/lr">List-Recents</a>
<a href="%url($base)/env">CGI-Environment</a>
<p>
<form>
Search: <input type="text" size="60" name="s">
<input type="submit" name="s1" value="Go">
</form>
}
if {[wapp-param-exists s]} {
set s [wapp-param s]
wapp-trim {
<h2>Search results for %html($s):</h2><ol>
}
open-database
set pattern %$s%
set cnt 0
db eval {
SELECT name, redir, user, mtime FROM url
WHERE name LIKE $pattern OR redir LIKE $pattern
} {
wapp-trim {
<li><a href="%url($base)/ee/%url($name)">%html($name)</a>
→ %html($redir) (by %html($user) on %html($mtime))
}
incr cnt
}
wapp-trim {</ol>}
if {$cnt==0} {
wapp-trim {
<p>No matches found</p>
}
}
}
common-footer
}
# Show recent modified URL shorteners pages
#
proc wapp-page-lr {} {
if {[check-credentials]} return
common-header
set base [wapp-param BASE_URL]
wapp-trim {
<p>
<a href="%url($base)/ee">Create-New-URL</a>
<a href="%url($base)/sx">Search</a>
<a href="%url($base)/env">CGI-Environment</a>
}
wapp-trim {
<ol>
}
open-database
set cnt 0
db eval {
SELECT name, redir, user, mtime FROM url
ORDER BY mtime DESC LIMIT 50
} {
wapp-trim {
<li><a href="%url($base)/ee/%url($name)">%html($name)</a>
→ %html($redir) (by %html($user) on %html($mtime))
}
incr cnt
}
wapp-trim {</ol>}
if {$cnt==0} {
wapp-trim {
<p>No entries</p>
}
}
common-footer
}
# Create or edit a new URL shortener entry.
#
# Name of the entry to be editted is in PATH_TAIL. Or if PATH_TAIL is
# blank, create a new entry.
#
proc wapp-page-ee {} {
global BASEURL
if {[check-credentials]} return
common-header
if {[wapp-param-exists cancel]} {
wapp-redirect [wapp-param BASE_URL]/sx
return
}
open-database
set hardname [wapp-param PATH_TAIL {}]
set name [wapp-param name $hardname]
set redir [wapp-param redir {}]
set notes [wapp-param notes {}]
wapp-trim {
<head></head>
<body>
}
set errmsg {}
if {[wapp-param-exists s1]} {
if {$hardname==""} {
set e [db one {SELECT count(*) FROM url WHERE name=$name}]
if {$e==1} {
set errmsg "Shortened URL name \"$name\" already exists.\
Pick a different name."
} elseif {[string length $name]<4} {
set errmsg "Shortened URL name \"$name\" too short.\
Must be at least 4 characters."
} elseif {[regexp {[^a-z0-9]} $name]} {
set errmsg "Shortened URL name \"$name\" contains invalid characters. \
The name must be lower-case ASCII letters and digits only."
} elseif {[lsearch [info procs] wapp-page-$name]>=0} {
set errmsg "Shortened URL name \"$name\" already exists.\
Pick a different name."
} else {
set user $::env(FOSSIL_USER)
db eval {
INSERT INTO url(name,redir,user,notes,ctime,mtime)
VALUES($name,$redir,$user,$notes,datetime(),datetime())
}
set hardname $name
|
247
248
249
250
251
252
253
254
255
256
|
<td> </td>
<td><input type="submit" name="s1" value="Submit">
<input type="submit" name="cancel" value="Cancel">
</tr>
</table>
</form>
}
}
wapp-start $argv
|
>
|
308
309
310
311
312
313
314
315
316
317
318
|
<td> </td>
<td><input type="submit" name="s1" value="Submit">
<input type="submit" name="cancel" value="Cancel">
</tr>
</table>
</form>
}
common-footer
}
wapp-start $argv
|