Mercurial > hgrepos > hgweb.cgi > s4
comparison s4-funcs.sh @ 17:01f579d2c889
Rename to s4
author | HIROSE Yuuji <yuuji@gentei.org> |
---|---|
date | Tue, 21 Jul 2015 14:43:24 +0900 |
parents | y4-funcs.sh@636df1c1bdf2 |
children | 9951e231df30 |
comparison
equal
deleted
inserted
replaced
16:636df1c1bdf2 | 17:01f579d2c889 |
---|---|
1 #!/bin/sh | |
2 # Here's global variable table. Do not use this names. | |
3 # $HGid$ | |
4 myname=`basename ${SCRIPT_NAME:-$0}` | |
5 mydir=`dirname ${SCRIPT_FILENAME:-$0}` | |
6 myargs="$@" | |
7 #bindir=`(cd $mydir/bin; pwd)` | |
8 # :$bindir | |
9 PATH=/usr/local/sqlite3/bin:/usr/local/vim7/bin:/usr/iekei/ImageMagick/bin:/usr/local/ImageMagick/bin:$PATH | |
10 tmpdir=${TMPDIR:-tmp} | |
11 dbdir=${DBDIR:-db} | |
12 tmpfiles="" | |
13 db=${DB:-$dbdir/cgi.sq3} | |
14 admin=wwwadmin@gentei.org | |
15 templ=${TEMPL:-templ} | |
16 layout=$templ/default | |
17 formdir=$templ/form | |
18 imgdir=img | |
19 url=${URL:-"${REQUEST_SCHEME:-http${HTTPS:+s}}://$HTTP_HOST$REQUEST_URI"} | |
20 urlbase=${url%%\?*} | |
21 msg=$templ/msg | |
22 timeout="+2 days" | |
23 dumpcollen=20 | |
24 thumbxy=120x120 | |
25 thumbxy=96x96 | |
26 maximagexy=1600x1600 | |
27 ### maximagexy=400x400 | |
28 tconfs="" | |
29 imgcached=imgcache.`date +%Y/%m` | |
30 conftbl=_tblconf | |
31 nl=" | |
32 " | |
33 . ./s4-cgi.sh | |
34 | |
35 : <<EOF | |
36 | |
37 資料配布、グループ管理・ML、ファイル交換、クリッカー、アンケート | |
38 レポート提出管理 | |
39 グループへの一斉送信機能は付いたが表に出てない(7/6) | |
40 ひとつのarticleをheadingにして新規ツリーを作成、あるといいかも。 | |
41 | |
42 7/19 設置 | |
43 締切設定 | |
44 一斉送信 | |
45 自分の提出物リスト | |
46 getparfilename の tmpd の扱い | |
47 やっぱりs4にしようかな | |
48 7/18 ○書込著者からホームへのリンク | |
49 7/17 ○個人blogに「レポート提出用」がついたときの挙動 | |
50 ○添付ファイル回収 | |
51 ○imgcacheは別ディレクトリにしないと + .htaccess | |
52 7/15 ○レポート提出モードの表示を付ける | |
53 管理者権限での削除? → まだいいか | |
54 | |
55 7/13 ○前回アクセス基準の新着数は欲しいなあ | |
56 ○レポート提出はどうしよう | |
57 → ○blogにモードを追加: | |
58 ○レポート提出モード | |
59 添付ファイル (誰が見たかログ) | |
60 クリッカーは別立てメニューにしないと(管理者がON/OFF) | |
61 ○添付ファイルの読み出し権(6/22から) ← モードで対処 | |
62 | |
63 | |
64 7/9 ○管理者の追加 | |
65 △グループメンバの操作 → 要不要を吟味 | |
66 ○グループ情報編集の行先はそのグループがいい? | |
67 ○新規グループの作成はどこから入るか | |
68 △グループホームとユーザホームを揃える | |
69 | |
70 7/8 ○グループ一覧をユーザ一覧と揃える。 | |
71 | |
72 7/6の次 ○グループのconf編集の入口 | |
73 ○グループ検索 | |
74 | |
75 6/22の次 ○ホーム画面、○招待状、親記事追跡、○編集ボタン、削除ボタン、 | |
76 | |
77 | |
78 6/7の次 ○blogを作ってみる || userconfig || _mのまとめ編集(削除) | |
79 6/7の次の次 ○userconfigの画面だけ作ってみる。 | |
80 | |
81 ○ 5/28の次 edittableに「削除」ボタンを足す | |
82 ○6/1 par2tableを triplex 対応に | |
83 select "yuuji@gentei.org",var,"text",NULL,val from par where var in (select col from _tblconf where tbl="/user" and keytype in ('p', 's')); | |
84 →とすると 一気に | |
85 | |
86 ## form.def を考えなおそう: | |
87 ## userのように必須カラムを決まった位置に付ける? | |
88 ## 必須カラム、owner(foreign key passwd(name)), update datetime | |
89 ## ユーザ管理とグループ管理はデフォルトで持たせてしまえ | |
90 | |
91 ## 縦持ちデータの入力/編集を供給する関数 single + multi | |
92 ## 持てるテーブル構造はシステム標準5種 + ユーザ定義2種類 | |
93 ## 1. passwd | |
94 ## 2. grp | |
95 ## 3. grp_mem | |
96 ## 4. topic 記事のIDとなる | |
97 ## 5. topic_cont 特定IDの記事の内容物 | |
98 ## 6. list 繰り返し登場あり | |
99 ## 7. hash 繰り返し登場なし | |
100 | |
101 ## ● listの定義: | |
102 ## create table list(id unique, parentID, type, value); | |
103 ## ● hashの定義: | |
104 ## create table hash(parentID, type, value, primary key(parentID, type)); | |
105 | |
106 ## グループ属性: community, friend | |
107 ## ○ blob使えるのかな。streamで行けるのか? xxdで行けた。ありがたい。 | |
108 ## form-defとtableは1対1対応でいいか | |
109 ## csv2sq3 で .csv.sq3 の Makefile | |
110 | |
111 ## 書き込みオブジェクトとは何か? | |
112 ## topic : id, belongto, title, owner, mode | |
113 ## type := root | comment | |
114 ## topic_cont : id, topicid(F), ppath, contenttype, filename, content, | |
115 ## unique(id, filename) | |
116 ## type := body(single) | attachment(multi) | |
117 | |
118 ## group := name(P), tag, gecos, owner(F), mode | |
119 ## tag := personal | friend | ... any string | |
120 ## group_member := gname(F), type, name(F), UNIQUE(gname, type, name) | |
121 ## type := "u" | "g" | |
122 ## できたー! | |
123 ## with recursive allmem as (select * from grp_mem where gname='bar' union all select grp_mem.* from grp_mem,allmem where allmem.name=grp_mem.gname) select * from allmem where type='u'; | |
124 ↓ | |
125 ↓以下に変更 | |
126 with recursive allmem as | |
127 (select gname,val from grp_m where gname='foo' | |
128 union all select grp_m.gname,grp_m.val from | |
129 grp_m,allmem where allmem.val=grp_m.gname) | |
130 select val from allmem where val in (select name from user); | |
131 | |
132 | |
133 with recursive allmem as | |
134 (select gname,val from grp_m where gname='foo' | |
135 union all select grp_m.gname,grp_m.val from grp_m,allmem | |
136 where allmem.val=grp_m.gname) | |
137 select a.*, coalesce(b.val,a.val) from allmem a left join grp_mem_s b | |
138 on a.gname=b.gname and a.val=b.user and b.key='email' | |
139 where a.val in (select name from user); | |
140 | |
141 | |
142 ## triggerもできた。 | |
143 ## 5/22から:グループ作成画面 | |
144 ## 埋め込み画像 data:CONTENT-TYPE;base64,..... | |
145 | |
146 ## 考え得るノードタイプ | |
147 ## 日報 - 個人所属かグループ所属か | |
148 ## 課題提出 - 個人所属かグループ所属か | |
149 ## グループ管理 | |
150 ## 個人情報管理 | |
151 ## | |
152 | |
153 ## 例: group:sip - topic:1:sip:Aperture:yuuji:rw | |
154 ## - topic:2:sip:ISO:yuuji:rw | |
155 ## topic_cont 1:1:/:body:text...Aperture | |
156 ## 2:1:/1:body:text..Aperture | |
157 ## 3:1:/1:attachment:binary..Aperture | |
158 ## 4:1:/2:body:text..Aperture | |
159 ## 5:1:/2:attachment:binary..Aperture | |
160 ## 6:2:/:body:text..ISO | |
161 ## 7:2:/6:body:text..ISO | |
162 ## 8:2:/6:attachment:binary.. | |
163 | |
164 ## ログテーブル | |
165 ## time, who, action, tbl, id idなんか取れるかな | |
166 | |
167 | |
168 | |
169 ■表設計 | |
170 * 3つの表に分散管理 | |
171 id格納表 + hash表 + list表 | |
172 * *_s *_m | |
173 | |
174 | |
175 | |
176 user, user_map, user_col | |
177 | |
178 ■抽象エントリタイプ | |
179 * user | |
180 idとして機能 → table中の owner に自動挿入(?) | |
181 * group | |
182 権限判定に利用 | |
183 * serial | |
184 自動idとして機能 | |
185 * password | |
186 入力 type=passwordで入力 | |
187 変更 oldpasswd, password×2 で確認後修正 | |
188 * session | |
189 password認証後のセッションキーとして機能 | |
190 * text | |
191 入力 type=text | |
192 * textarea | |
193 入力 textarea | |
194 * image|document | |
195 入力 type=fileで入力し、mime-typeを確認 | |
196 * owner | |
197 入力時の $user で、外部キー制約が付く | |
198 * gowner | |
199 グループとしての所有者で、外部キー制約が付く | |
200 * timestamp | |
201 datetime() | |
202 * parent | |
203 木構造の場合の親の位置 | |
204 * path | |
205 木構造の場合の自分の位置 | |
206 | |
207 格納タイプ | |
208 * list | |
209 表 parentID, key, val でUNIQUE(parentID, key, val) | |
210 * hash | |
211 表 parentID, key, val でUNIQUE(parentID, key) | |
212 | |
213 オブジェクトタイプ | |
214 * entry | |
215 id, title, owner | |
216 * textpart | |
217 id, parentID, text | |
218 * binarypart | |
219 id, parentID, contenttype, filename, content | |
220 * content | |
221 hash(textpart), list(binarypart) | |
222 * topic | |
223 id, hash(content), list(reply) | |
224 * reply | |
225 id, parentID, content | |
226 * blog | |
227 list(entry) | |
228 blog = [topic, list(reply)] | |
229 | |
230 | |
231 blog = [ {"title" => "hoge", "owner" => "yuuji", "date" => "2015-04-27", | |
232 "text" => "hogehoge ..", | |
233 "reply" => [ {"serial" => 1, | |
234 "author" => "taro", | |
235 "date" => "2015-04-28", | |
236 "parent" => "/", | |
237 "path" => "/1", | |
238 "text" => "blah, blah, ....", | |
239 "image" => ["a.jpg", "b.jpg"] }, | |
240 {"serial" => 2, | |
241 "author" => "hanako", | |
242 "date" => "2015-04-29", | |
243 "parent" => "/", | |
244 "path" => "/2", | |
245 "text" => "blah, blah, ....", | |
246 "image" => [] }]}, | |
247 {"title" => "buha", ...} ] | |
248 | |
249 | |
250 user:= | |
251 ユーザ名(英数字):name:p:text:length="20" maxlength="40" | |
252 パスワード:pswd:s:password:length="20" maxlength="40" | |
253 説明(日本語OK):gecos:s:text:length="20" maxlength="40" | |
254 セッションキー:skey:s:session | |
255 メイルアドレス:email:m:text:length="20" maxlength="40" | |
256 住所:address:m:textarea:maxlength="400" | |
257 プロフィール画像:profimg:m:image:maxlength="400K" | |
258 履歴書:profpdf:m:document:maxlength="4M" | |
259 | |
260 変換表 | |
261 /user/email=m | |
262 | |
263 blog:= | |
264 シリアル:id:p:serial | |
265 タイトル:title:s:text: | |
266 所有者:owner:s:owner: | |
267 時刻:ctime:s:stamp: | |
268 リード文:heading:s:textarea: | |
269 リプライ:reply:m:*article: | |
270 | |
271 article:= | |
272 シリアル:id:p:serial | |
273 筆者:author:s:owner | |
274 時刻:ctime:s:stamp: | |
275 参照元:parent:s:parent: | |
276 パス:path:s:path: | |
277 本文:text:s:textarea: | |
278 画像:image:m:image: | |
279 | |
280 履歴書:profpdf:m:document:maxlength="4M" | |
281 | |
282 | |
283 EOF | |
284 | |
285 sq() { | |
286 # ./args.rb -cmd ".timeout 3000" "$@" | |
287 sqlite3 -cmd 'PRAGMA foreign_keys=ON' -cmd ".timeout 3000" "$@" | |
288 } | |
289 ####XXXXXXXXXXXXXXXXXXXXXX 場所移動 | |
290 sqi=/tmp/sqi.$$ | |
291 sqo=/tmp/sqo.$$ | |
292 mkfifo $sqi $sqo | |
293 tail -f $sqi | sq $db & | |
294 sq3pid="`jobs -p` $!" | |
295 | |
296 query() { | |
297 cat<<EOF > $sqi | |
298 .once $sqo | |
299 $@ | |
300 EOF | |
301 cat $sqo | |
302 } | |
303 exec 3>> $tmpdir/debug.out | |
304 exec 2>> $tmpdir/error.out | |
305 err() { | |
306 echo "$@" 1>&3 | |
307 } | |
308 | |
309 _m4() { | |
310 | |
311 } | |
312 ismember() { | |
313 # $1=user, $2=group | |
314 err ismem: "select user from grp_mem where gname=$(sqlquote $2) and user='$1';" | |
315 test -n "`query \"select user from grp_mem where gname=$(sqlquote $2) and user='$1';\"`" | |
316 } | |
317 isuser() { # Check if $1 is a valid user | |
318 test -n "`query \"select name from user where name='$1';\"`" | |
319 } | |
320 isgroup() { # Check if $1 is a valid group | |
321 err isgroup: "select gname from grp where gname=$(sqlquote $1);" | |
322 test -n "`query \"select gname from grp where gname=$(sqlquote $1);\"`" | |
323 } | |
324 isgrpowner() ( | |
325 # $1=user, $2=group | |
326 gn=`sqlquote "$2"` | |
327 sql="select user from grp_adm where gname=$gn and user='$1';" | |
328 err isgrpowner: $sql | |
329 test -n "`query $sql`" | |
330 ) | |
331 getgroupbyid() ( | |
332 # $1=id|gname | |
333 sql="select coalesce((select gname from grp where gname=$(sqlquote $1)), | |
334 (select gname from grp where rowid=$(sqlquote $1)));" | |
335 err ggbyid: `echo $sql` | |
336 query $sql | |
337 ) | |
338 isfilereadable() { # $1=user $2=tbl $3=rowid | |
339 # Return true if user($1) can read attachment files in tbl($2):rowid($3) | |
340 [ -z "$1" -o -z "$2" -o -z "$3" ] && return 1 # invalid argument | |
341 | |
342 # case `getvalbyid blog mode $2` in | |
343 # normal|*open*|"") return 0 ;; | |
344 # *closed*) | |
345 # owner=`getvalbyid blog owner $2` | |
346 # if isgrp $owner; then | |
347 # isgrpowner $1 $owner && return 0 || return 1 | |
348 # elif isuser $owner; then | |
349 # [ x"$1" = x"$owner" ] && return 0 || return 1 | |
350 # fi | |
351 # esac | |
352 # ↑ 要はこういう処理を↓で一気にやっている | |
353 sql="with getblog as (\ | |
354 select key,val from blog_s where id=(\ | |
355 select blogid from article where id in\ | |
356 (select id from $2 where rowid=$3))),\ | |
357 getowner as (select val from getblog where key='owner'),\ | |
358 getmode as (select val from getblog where key='mode')\ | |
359 select case\ | |
360 when (select author from article where\ | |
361 id=(select id from $2 where rowid=$3))='$1' \ | |
362 then 'author'\ | |
363 when (select val from getmode) in ('report-open', 'normal')\ | |
364 then 'open'\ | |
365 when (select val from getmode) is null \ | |
366 then 'open' | |
367 when (select val from getowner) in (select gname from grp)\ | |
368 then (select user from grp_adm where \ | |
369 gname=(select val from getowner) and \ | |
370 user='$1')\ | |
371 else 'user' end;" | |
372 err isfilereadable: sql="$sql" | |
373 # caseのネストで内側のcaseがスカラーtrueを返しても外側はtrue扱いにならない | |
374 result=`query "$sql"` | |
375 [ -n "$result" ] && return 0 | |
376 return 2 | |
377 } | |
378 linkhome() { | |
379 # $1=UserOrGroup | |
380 echo -n '<a href="?' | |
381 if isuser $1; then | |
382 err "select 'home+'||rowid from user where name='$1';" | |
383 query "select 'home+'||rowid from user where name='$1';" | |
384 else | |
385 echo -n "grp+$1" | |
386 fi | |
387 echo "\">`gecos $1`</a>" | |
388 } | |
389 acclog() { | |
390 # $1=table, $2=rowid | |
391 if [ -n "$2" ]; then | |
392 now=`date +"%F %T"` | |
393 query "replace into acclog values('$user', '$1', '$2', '$now');" | |
394 fi | |
395 } | |
396 gecos() ( | |
397 u=`sqlquote ${1:-$user}` | |
398 #gecos=`query "select val from user_s where name='$u' and key='gecos';"` | |
399 sql="select case when (select name from user where name=$u) is not null | |
400 then (select val from user_s where name=$u and key='gecos') | |
401 when (select gname from grp where gname=$u) is not null | |
402 then (select val from grp_s where gname=$u and key='gecos') | |
403 else $u | |
404 end;" | |
405 query "$sql" | |
406 ) | |
407 setpar() { | |
408 query "replace into par values('$session', '$1', '$2', \"$3\");" | |
409 } | |
410 replpar() { | |
411 query "update par set val=\"$3\" where sessid='$session' and var='$1' and type='$2';" | |
412 } | |
413 getpar() { | |
414 err getpar: "select val from par where var='$1' and sessid='$session' $2;" | |
415 val=`query "select val from par where var='$1' and sessid='$session' $2;"` | |
416 err getpar/val1: "val=[$val]" | |
417 if [ -z "$val" ]; then | |
418 val=`query "select val from cookie where var='$1' and sessid='$session' $2;"` | |
419 fi | |
420 err getpar/val2: "val=[$val]" | |
421 case "$var" in | |
422 owner) | |
423 if [ x"$user" = x"$val" ]; then | |
424 echo $user; return | |
425 elif ismember $user $val; then | |
426 echo $val; return | |
427 fi ;; | |
428 esac | |
429 err getpar/ret: "val=[$val]" | |
430 echo "$val" | |
431 } | |
432 | |
433 getpartype() { | |
434 query "select type from par where var='$1' and sessid='$session' $2;" | |
435 } | |
436 getparcount() { | |
437 query "select count(*) from par where var='$1' and sessid='$session' $2;" | |
438 } | |
439 getparfilename() { | |
440 # null if type of $1 is not file | |
441 (f=`query "select val from par where var='$1' and sessid='$session' and type='file' $2;"` | |
442 [ -n "$f" ] && echo $f) | |
443 } | |
444 sqlquote() { | |
445 (v="$1" | |
446 case "$v" in | |
447 "") return ;; # null | |
448 "X'"*) # quoted hex string | |
449 echo $1 ;; | |
450 *\"*) # string including dbl-quote" | |
451 v=`echo "$v"|sed -e 's/\"/\"\"/g'` | |
452 echo "\"$v\"" | |
453 return ;; | |
454 *.*.*|*-*-*|*[Ee]*[Ee]*|[Ee]*|*[\ -,:-df-~]*) # string | |
455 echo "\"$v\"" | |
456 return ;; | |
457 *) | |
458 if expr "$v" : '[-0-9.Ee][-0-9.Ee]*$' >/dev/null 2>&1; then | |
459 echo $v # MAYBE numeric, maybe... | |
460 else | |
461 echo "\"$v\"" | |
462 fi ;; | |
463 esac) | |
464 } | |
465 mktempd() { | |
466 TMPDIR=$tmpd mktemp -d -t $session | |
467 } | |
468 getval() { | |
469 # $1=table $2=col $3(optional)=condition | |
470 case `gettbl_coltype "/$1/$2"` in | |
471 user|author) # author added 2015-06-18 for article(author) | |
472 echo "$user" ;; | |
473 stamp|datetime) | |
474 date "+%F %T" ;; | |
475 serial) | |
476 (s=`getpar serial` | |
477 if [ -n "$s" ]; then echo $s; else echo "`date +%s`x$$"; fi) ;; | |
478 *) | |
479 getpar "$2" "$3";; | |
480 esac | |
481 } | |
482 | |
483 getvalquote() { | |
484 # $1=table $2=col $3(optional)=condition | |
485 (v=`getval "$@"` | |
486 case "$v" in | |
487 "") echo NULL ;; | |
488 *) sqlquote "$v" ;; | |
489 esac) | |
490 } | |
491 getparquote() { | |
492 sqlquote `getpar $1` | |
493 } | |
494 getvalbyid() { | |
495 # $1=tbl $2=col $3=rowid $4=tmpdirForBinary | |
496 # If two or more values found, save them to $tmpd/${column}.$N and | |
497 # store the number of files into $tmpd/${column}.count and | |
498 # their each rowid stored into $tmpd/${column}.$N.rowid. | |
499 ### err gtb-$1=`gettblcols $1`, tbl=$1, col=$2, '$3'=$3 | |
500 (for c in `gettblcols $1`; do | |
501 if [ x"$2" = x"$c" ]; then | |
502 err "select $2 from $1 where rowid=$3" | |
503 ###sq $db "select $2 from $1 where rowid=$3" | |
504 query "select $2 from $1 where rowid=$3;" | |
505 return | |
506 fi | |
507 done | |
508 pk=`gettblpkey $1` | |
509 # key=`sq $db "select $pk from $1 where rowid=$3"` | |
510 key=`query "select $pk from $1 where rowid=$3;"` | |
511 ### err "select $pk from $1 where rowid=$3" - key=$key '$4(tmp)'=$4 | |
512 for kt in s m; do | |
513 td=${4:-$tmpd} | |
514 [ -d $td ] || mkdir -p $td | |
515 t=${1}_$kt | |
516 for c in `gettbl_${kt}_cols $1`; do | |
517 vcount=1 # count(val) | |
518 if [ x"$2" = x"$c" ]; then | |
519 cond="$t where $pk=\"$key\" and key=\"$c\"" | |
520 ###val=`query "select val from $cond;"` | |
521 val=`query "select val from $cond limit 1;"` | |
522 ###type=`query "select type from $cond;"` | |
523 type=`query "select type from $cond limit 1;"` | |
524 if [ $kt = m ]; then | |
525 ###vcount=`sq $db "select count(val) from $cond"` | |
526 # Reset val to store filenames if type is string | |
527 val=`query "select val from $cond and type like 'file:%' order by rowid;"` | |
528 vcount=`query "select count(val) from $cond;"` | |
529 echo $vcount > $td/$c.count | |
530 i=0 | |
531 while [ $i -lt $vcount ]; do | |
532 slice="order by rowid limit 1 offset $i" | |
533 i=$((i+1)) | |
534 fn=$c.$i | |
535 ##err td=$td, fn=$fn, type=$type, val="[$val]" | |
536 case $type in | |
537 file:*) | |
538 #file=$td/$val | |
539 file=$td/`query "select val from $cond $slice;"` | |
540 ####tmp [ -s $file -a -s $td/$fn.rowid ] && continue | |
541 sq $db<<EOF | unhexize > $file | |
542 .output $td/$fn.rowid | |
543 select rowid from $cond $slice; | |
544 .output $td/$fn | |
545 select val from $cond $slice; | |
546 .output $file.content-type | |
547 select substr(type, 6) from $cond $slice; | |
548 .output stdout | |
549 select quote(bin) from $cond $slice; | |
550 EOF | |
551 ## err i=$i - file=$file rowid=`cat $td/$fn.rowid` | |
552 ln $td/$fn.rowid $file.rowid 2>&3 # for convenience | |
553 case $type in | |
554 *:[Ii]mage*) mogrify -geometry $thumbxy $file ;; | |
555 ### ここのアイコンを増やしたい | |
556 *|*:[Aa]pplication*) | |
557 convert -geometry $thumbxy $imgdir/file-icon.png \ | |
558 png:- > $file | |
559 ;; | |
560 esac | |
561 ;; | |
562 *) | |
563 sq $db<<EOF | |
564 .output $td/$fn.rowid | |
565 select rowid from $cond $slice; | |
566 .output $td/$fn | |
567 select val from $cond $slice; | |
568 EOF | |
569 val=$val"`echo $fn`" # should be delimited by newline | |
570 ;; | |
571 esac | |
572 done | |
573 else | |
574 rm -f $td/$c.count | |
575 case $type in | |
576 file:*) | |
577 echo "$val" \ | |
578 | while read fn; do | |
579 file=$td/$fn | |
580 if [ ! -s $file ]; then | |
581 ## sq $db "select quote(bin) from $cond and val=\"$fn\"" \ | |
582 query "select quote(bin) from $cond and val=\"$fn\";" \ | |
583 | unhexize > $file | |
584 echo ${type#file:} > $file.content-type | |
585 # err TTTTTTTTTTTTTTTT: $type | |
586 case $type in | |
587 *:[Ii]mage*) mogrify -geometry $thumbxy $file ;; | |
588 *:[Aa]pplication*) | |
589 convert -geometry $thumbxy $imgdir/file-icon.png \ | |
590 png:- > $file ;; | |
591 esac | |
592 fi | |
593 done | |
594 ;; | |
595 esac | |
596 fi | |
597 echo "$val" # Keep newlines by "" | |
598 return | |
599 fi | |
600 done | |
601 done) | |
602 } | |
603 getvalbypkey() ( | |
604 # $1=tbl $2=col $3=pkey $4=tmpdirForBinary | |
605 pk=`gettblpkey $1` | |
606 rowid=`query "select rowid from $1 where $pk='$3';"` | |
607 getvalbyid "$1" "$2" $rowid $4 | |
608 ) | |
609 getvalbycond() { | |
610 # $1=tbl $2=col $3=SQL-Condition | |
611 ###rowid=`sq $db "select rowid from $1 where $3"` | |
612 rowid=`query "select rowid from $1 where $3;"` | |
613 if [ -n "$rowid" ]; then | |
614 getvalbyid "$1" "$2" $rowid "$4" | |
615 fi | |
616 } | |
617 getpwfield() { | |
618 # getpwfield user column | |
619 # val=`sqlite3 $db "select $2 from passwd where name='$1' $3"` | |
620 val=`getvalbycond user $2 "name='$1'"` | |
621 if [ -n "$val" ]; then | |
622 echo "$val" | |
623 return 0 | |
624 else | |
625 return 1 | |
626 fi | |
627 } | |
628 encode() { | |
629 if [ -z "$sha1" ]; then | |
630 if type sha1 >/dev/null 2>&1; then | |
631 sha1=sha1 | |
632 elif type sha1sum >/dev/null 2>&1; then | |
633 sha1=sha1sum | |
634 elif type gsha1sum >/dev/null 2>&1; then | |
635 sha1=gsha1sum | |
636 fi | |
637 fi | |
638 $sha1 "$@" | cut -d' ' -f1 | |
639 } | |
640 mycrypt() ( | |
641 key=$1 salt=$2 | |
642 err \$2=$2 | |
643 case $2 in | |
644 '$'*'$'*) salt=${salt#\$4\$} | |
645 salt=${salt%\$*} ;; | |
646 esac | |
647 echo -n '$4$'"$salt"'$' | |
648 echo "$salt$key" | encode | |
649 ) | |
650 hexize() { | |
651 if [ -z "$hexize" ]; then | |
652 if type xxd >/dev/null 2>&1; then | |
653 hexize="xxd -p" | |
654 else | |
655 hexize_hd() { | |
656 hexdump -ve '1/1 "%.2x"' | |
657 } | |
658 hexize="hexize_hd" | |
659 fi | |
660 fi | |
661 cat $1 | $hexize | tr -d '\n' | |
662 } | |
663 unhexize() { | |
664 if [ -z "$unhex" ]; then | |
665 if type xxd >/dev/null 2>&1; then | |
666 unhex="xxd -p -r" | |
667 elif type perl >/dev/null 2>&1; then | |
668 cat >$tmpd/unhex.pl<<EOF | |
669 s/([0-9a-f]{2})/print chr hex \$1/gie | |
670 EOF | |
671 # Perl refuses -e in setuid circumstances, which can be absurdly | |
672 # avoided by creating scripts in a file where its parent directory is | |
673 # world writable...:) | |
674 unhex="perl -n $tmpd/unhex.pl" | |
675 fi | |
676 fi | |
677 cat $1 | $unhex | |
678 # cat $1 | tee /tmp/uh.in| $unhex | tee /tmp/uh.out | |
679 } | |
680 percenthex() { | |
681 hexize $1 | sed 's/\(..\)/%\1/g' | |
682 } | |
683 enascii() { | |
684 if [ -z "$enascii" ]; then | |
685 if type kakasi >/dev/null 2>&1; then | |
686 enascii="kakasi -Ha -Ka -Ja -Ea -ka" | |
687 else | |
688 enascii_now=`date +%FT%T` | |
689 enascii_sed() { | |
690 nkf -Z0Z1Z2 \ | |
691 | sed -e "s/^/$enascii_now/" -e "s|[^-0-9.A-z/,()_=]|x|g" | |
692 } | |
693 enascii="enascii_sed" | |
694 fi | |
695 fi | |
696 cat "$@" | $enascii | |
697 } | |
698 gettblconf() { | |
699 if [ -z "$tconfs" ]; then | |
700 ## tconfs=`sq $db \ | |
701 tconfs=`query \ | |
702 "select tbl||'/'||col||'='||keytype||'/'||objtype from $conftbl;"` | |
703 fi | |
704 # /tb1/col1=p/text /tb1/col2=s/text /tb1/col3=m/image /tb2/col1=p/text ... | |
705 } | |
706 gettblkeys() { | |
707 # $1=tbl | |
708 gettblconf | |
709 echo "$tconfs" | fgrep "/$1/" | \ | |
710 (type="" keys="" fks="" cols="" scols="" mcols="" hcols="" | |
711 while IFS='=' read tc conf; do # tc=/tb1/col1 conf=s/text | |
712 col=${tc##*/} type=${conf%%/*} | |
713 case $type in | |
714 *p*) | |
715 cols=$cols"${cols:+:}$col" | |
716 keys=$keys"${keys:+:}$col" ;; | |
717 *f*) cols=$cols"${cols:+:}$col" | |
718 fks=$fks"${fks:+:}$col" ;; | |
719 *m*) mcols=$mcols"${mcols:+:}$col" ;; | |
720 *s*) scols=$scols"${scols:+:}$col" ;; | |
721 esac | |
722 case $type in | |
723 *h*) hcols=$hcols"${hcols:+:}$col" ;; | |
724 esac | |
725 done | |
726 echo "_keys=$keys _fks=$fks _cols=$cols _scols=$scols _mcols=$mcols _hcols=$hcols") | |
727 } | |
728 gettblpkey() { | |
729 # $1=tbl | |
730 gettblkeys $1 | cut -d ' ' -f 1 | sed -e 's/.*=//' -e 's/:/ /g' | |
731 } | |
732 gettblfkey() { | |
733 (x=`gettblkeys $1` | |
734 x=${x#*_fks=} # cut before "_fks=" including | |
735 echo ${x%% *} | tr ':' ' ') | |
736 } | |
737 gettblcols() { | |
738 (x=`gettblkeys $1` | |
739 x=${x#*_cols=} # cut before "_cols=" including | |
740 echo ${x%% *} | tr ':' ' ') | |
741 } | |
742 gettbl_s_cols() { | |
743 (x=`gettblkeys $1` | |
744 x=${x#*_scols=} # cut before "_scols=" including | |
745 echo ${x%% *} | tr ':' ' ') | |
746 } | |
747 gettbl_m_cols() { | |
748 (x=`gettblkeys $1` | |
749 x=${x#*_mcols=} # cut before "_mcols=" including | |
750 echo ${x%% *} | tr ':' ' ') | |
751 } | |
752 gettbl_h_cols() { | |
753 (x=`gettblkeys $1` | |
754 x=${x#*_hcols=} # cut before "_hcols=" including | |
755 echo ${x%% *} | tr ':' ' ') | |
756 } | |
757 gettbl_coltype() ( | |
758 gettblconf | |
759 x=`echo "$tconfs"|fgrep $1=` | |
760 x=${x#*=} # cut before = | |
761 echo ${x#*/} # cut before p/ including | |
762 ) | |
763 is_hidden() { | |
764 # $1=Tbl $2=col | |
765 gettblconf | |
766 x=`echo "$tconfs"|fgrep /$1/$2=` | |
767 x=${x#*=} # cut before = | |
768 x=${x%%/*} # cut after / | |
769 case $x in | |
770 *h*) return 0 ;; | |
771 *) return 1 ;; | |
772 esac | |
773 } | |
774 | |
775 dbsetbyid() { | |
776 # $1=tbl $2=id $3=col $4=val/filename - &optional - $5=content-type | |
777 (t0=$1 t=$1 p=$2 c=$3 | |
778 tsc=$t/$c val=$4 | |
779 unset primary update | |
780 gettblconf | |
781 #err tsc=$tsc, tconfs="$tconfs" | |
782 conf=`echo "$tconfs"|fgrep "$tsc"=` | |
783 #err conf=$conf | |
784 case ${conf#*=} in | |
785 p*) primary=1 ;; | |
786 f*) update=1 ;; | |
787 u*) ;; | |
788 m*) t=${t}_m;; | |
789 s*) t=${t}_s;; | |
790 esac | |
791 #err t=$t | |
792 type=text fn="" | |
793 case $conf in | |
794 */password) | |
795 type=encoded ### val=`echo $val|encode` | |
796 ;; | |
797 */image*|*/document*) | |
798 type=`file --mime-type $val` | |
799 bin="X'`hexize $val`'" | |
800 ;; | |
801 esac | |
802 pkey=`echo "$tconfs"|grep "${t0}/.*=p"|sed 1q` | |
803 pkey=${pkey#/*/} # cut $tbl/ | |
804 pkey=${pkey%=p/*} # cut =p/... -> primary key | |
805 if [ "$primary" ]; then | |
806 nulls=`echo "$tconfs"|grep "$t/.*=[fu]/"|sed 's/^.*/, NULL/'|tr -d '\n'` | |
807 ###sq $db "replace into $t values(\"$val\"$nulls)" | |
808 query "replace into $t values(\"$val\"$nulls);" | |
809 elif [ "$update" ]; then | |
810 ###sq $db "update $1 set $c=\"$val\" where $pkey=\"$p\"" | |
811 query "update $1 set $c=\"$val\" where $pkey=\"$p\";" | |
812 else | |
813 #err "replace into $t values(\"$p\", \"$c\", \"$type\", \"$val\", \"$bin\")" | |
814 ###sq $db "replace into $t values(\"$p\", \"$c\", \"$type\", \"$val\", \"$bin\")" | |
815 query "replace into $t values(\"$p\", \"$c\", \"$type\", \"$val\", \"$bin\");" | |
816 fi | |
817 ) | |
818 } | |
819 expire() ( | |
820 at="${1:-$timeout}" | |
821 FMT="${2:-%F %T}" | |
822 TZ=GMT gdate -d "$at" +"$FMT" | |
823 ) | |
824 addsession() { | |
825 # expireをセット | |
826 # loginの先にどの画面に行くかの状態遷移表書式を決める | |
827 expire=`expire ${2:-"+1min"}` | |
828 err addsession "$1" exp=$expire | |
829 sq $db "replace into session values('$1', '$expire')" | |
830 # Remove old session parameters | |
831 now=`expire now` | |
832 sq $db "delete from session where expire < '$now'" | |
833 } | |
834 gencookie() ( | |
835 for kv; do | |
836 expire="`expire '' '%a, %d-%b-%Y %H:%M:%S GMT'`" | |
837 echo "Set-Cookie: $kv; expires=$expire" | |
838 done | |
839 ) | |
840 contenttype() { | |
841 echo "Content-type: ${1:-text/html; charset=utf-8}" | |
842 contenttype() {} # Only need to work once | |
843 } | |
844 putheader() { | |
845 | |
846 } | |
847 putfooter() { | |
848 m4 -D_TITLE_="${TITLE:-$myname}" $layout/footer.m4.html | |
849 } | |
850 getcookie() ( | |
851 for kv in `echo $HTTP_COOKIE|sed 's/[;, ]/ /g'`; do | |
852 k="${kv%%=*}" | |
853 v="`echo ${kv#*=}|nkf -Ww -mQ|sed -e 's/\"/\"\"/g'`" | |
854 query "replace into cookie values('$session', '$k', 'string', \"$v\");" | |
855 done | |
856 ) | |
857 genrandom() { | |
858 # $1=columns (default: 10) | |
859 dd if=/dev/urandom count=1 2>/dev/null|nkf -MB|fold -w${1:-10}|sed -n 10p | |
860 } | |
861 smail() { | |
862 # smail rcpt subj (file) | |
863 rcpt=`echo $1` # strip newlines | |
864 subj="`echo $2|nkf -jM`" | |
865 # echo rcpt=$rcpt | |
866 (m4 -D_RCPT_="$rcpt" -D_SUBJ_="$subj" -D_FROM_=$admin $msg/mail-header.m4 | |
867 cat $3 | nkf -jd ) | sendmail -f $admin $rcpt | |
868 } | |
869 setviastring() { | |
870 table=$1 | |
871 oifs="$IFS" | |
872 IFS="&" | |
873 for us in $2; do | |
874 k=${us%%=*} | |
875 v="`echo ${us#*=}|tr '%+' '= '|nkf -Ww -mQ|sed -e 's/\"/\"\"/g'`" | |
876 sq $db "replace into $table values('$session', '$k', 'string', \"$v\")"; | |
877 #echo $k=$v | |
878 done | |
879 IFS="$oifs" | |
880 } | |
881 checkdomain() ( | |
882 # Check the validity of domain by referring DNS | |
883 item=$1 | |
884 host ${item#@*} >/dev/null 2>&1 | |
885 ) | |
886 pwcheck() { | |
887 # $1=passwd | |
888 dbpswd=`getpwfield $user pswd` | |
889 encpswd=`mycrypt "$1" "$dbpswd"` | |
890 err user=$user, pswd=$1, db=$dbpswd, enc=$encpswd | |
891 [ x"$dbpswd" = x"$encpswd" ] | |
892 } | |
893 mypwhash() { | |
894 mycrypt `cat` `genrandom 5` | |
895 } | |
896 wasureta() { | |
897 user=$1 | |
898 if checkdomain $user; then | |
899 echo "ユーザ名には正しいメイルアドレスが必要です。" | |
900 exit 0 | |
901 fi | |
902 newpswd=`genrandom` # newsalt=`genrandom 5` | |
903 #encpswd=`mycrypt "$newpswd" "$newsalt"` | |
904 encpswd=`echo $newpswd|mypwhash` | |
905 dbsetbyid user $user pswd "$encpswd" | |
906 m4 -D_USER_="$user" -D_PSWD_="$newpswd" -D_URL_="$url" \ | |
907 $msg/mail-newaccount.m4 \ | |
908 | smail $user "New Account" | |
909 } | |
910 checkauth() { | |
911 user=`getpar user` | |
912 skc=`getpar skey` # from cookie | |
913 [ -z "$user" ] && return 3 | |
914 skey="`getpwfield $user skey`" | |
915 err user=$user skey=$skey | |
916 if [ -n "$skey" ]; then | |
917 if [ x"$skey" = x"$skc" ]; then | |
918 return 0 | |
919 fi | |
920 fi | |
921 pswd=`getpar pswd` | |
922 if [ x"$pswd" = x"wasureta" ]; then | |
923 wasureta $user | |
924 return 1 # wasureta error | |
925 fi | |
926 # dbpswd="`sq $db \"select pswd from passwd where name='$user'\"`" | |
927 # putheader; echo; echo user=$user, db=$dbpswd, enc=$encpswd | |
928 if pwcheck "$pswd"; then | |
929 newsession=`genrandom 50` | |
930 dbsetbyid user $user skey "$newsession" | |
931 gencookie "user=$user" "skey=$newsession" | |
932 return 0 | |
933 fi | |
934 return 2 # Password mismatch | |
935 } | |
936 showlogin() { | |
937 args=`echo $myargs|tr ' ' '+'` | |
938 m4 -D_SYSNAME_="Welcome" -D_MYNAME_="$myname${args+?}$args" \ | |
939 $layout/login.m4.html | |
940 exit 0 | |
941 } | |
942 dologin() { | |
943 checkauth | |
944 st=$? | |
945 if [ $st != 0 ]; then | |
946 contenttype; echo | |
947 m4 -D_USER_="$user" -D_URL_="$url" -D_ADMIN_="$admin" \ | |
948 $msg/login-fail-$st.m4.html | |
949 showlogin # and EXIT | |
950 fi | |
951 } | |
952 cleanup() { | |
953 echo .quit > $sqi | |
954 kill $sq3pid | |
955 rm -f $sqo $sqi | |
956 rm -rf $tmpfiles | |
957 } | |
958 trap cleanup INT HUP EXIT | |
959 # trap cleanup INT HUP | |
960 cgiinit() { | |
961 session=`date +%F-$$` | |
962 tmpf=tmp/stream | |
963 tmpd=`tmpd=$tmpdir mktempd` | |
964 tmpfiles=$tmpfiles" $tmpd" | |
965 addsession $session | |
966 getcookie | |
967 case "$REQUEST_METHOD" in | |
968 get|GET) s="$QUERY_STRING" ;; | |
969 post|POST) ## dd count=$CONTENT_LENGTH bs=1 of=$tmpf 2>/dev/null #slow | |
970 # cat > $tmpf # too much? | |
971 head -c $CONTENT_LENGTH > $tmpf # safe? | |
972 (echo CL=$CONTENT_LENGTH; ls -lF $tmpf) 1>&3 | |
973 s="`cat tmp/stream`" | |
974 tmpfiles=$tmpfiles"${tmpfiles+ }$tmpf" | |
975 ;; | |
976 esac | |
977 case "$CONTENT_TYPE" in | |
978 *boundary*) | |
979 bndry=${CONTENT_TYPE#*boundary=} | |
980 #for us in `LC_CTYPE=C ./mpsplit.rb "$bndry" $tmpd < $tmpf` | |
981 for us in `LC_CTYPE=C ./mpsplit.pl "$bndry" $tmpd < $tmpf` | |
982 do | |
983 k=${us%%\=*} | |
984 #echo u=$us | |
985 #v="`echo ${us#*=}|nkf -Ww -mQ|sed -e 's/\"/\"\"/g'`" | |
986 v="`echo ${us#*=}|unhexize|sed -e 's/\"/\"\"/g'`" | |
987 # err k=$k v=$v | |
988 case "$k" in | |
989 *:filename) | |
990 type='file'; k=${k%:filename} | |
991 (echo k=$k; ls -lF $tmpd/$v; file --mime-type $tmpd/$v) 1>&3 | |
992 case `file --mime-type $tmpd/$v|cut -d' ' -f2` in | |
993 [Ii]mage/*) | |
994 mogrify -resize $maximagexy'>' $tmpd/$v | |
995 ;; | |
996 esac | |
997 ;; | |
998 *) | |
999 type='string' | |
1000 ;; | |
1001 esac | |
1002 #sq $db "replace into par values('$session', '$k', '$type', \"$v\")" | |
1003 setpar "$k" "$type" "$v" | |
1004 done | |
1005 ;; | |
1006 *) | |
1007 setviastring par "$s" | |
1008 ;; | |
1009 esac | |
1010 } | |
1011 collectemail() { | |
1012 # Collect email addresses for group $1 | |
1013 if isuser "$1"; then | |
1014 em=`query "select val from user_m where name='$1' and key='email';"` | |
1015 [ -n "$em" ] && echo "$em" || echo "$1" | |
1016 else | |
1017 query "with recursive allmem as | |
1018 (select gname,val from grp_m where gname='$1' | |
1019 union all select grp_m.gname,grp_m.val from grp_m,allmem | |
1020 where allmem.val=grp_m.gname) | |
1021 select coalesce(b.val,a.val) | |
1022 from allmem a left join grp_mem_s b | |
1023 on a.gname=b.gname and a.val=b.user and b.key='email' | |
1024 where a.val in (select name from user) limit 10000;" | |
1025 fi | |
1026 } | |
1027 sendinvitation() ( | |
1028 # $1=email | |
1029 iss="invite-`date +%s`-$(genrandom 10)" | |
1030 addsession $iss +7days # 1 week due date | |
1031 query "replace into par values('$iss', 'invite', 'string', \"$1\");" | |
1032 gecos=`gecos` | |
1033 name=$user"${gecos:+($gecos)}" | |
1034 regist="$urlbase?reg+$iss" | |
1035 m4 -D_URL_="$urlbase" \ | |
1036 -D_USER_="$name" \ | |
1037 -D_EMAIL_="$1" \ | |
1038 -D_REGIST_="$regist" \ | |
1039 -D_ADMIN_="$admin" \ | |
1040 $msg/mail-invite.m4 \ | |
1041 | smail $1 "BBSへの御招待" | |
1042 return 0 | |
1043 ) | |
1044 emaildomaincheck() { | |
1045 case "$1" in | |
1046 *@*@*) echo "無効なアドレスです"; return 1 ;; | |
1047 *@*) | |
1048 local=${1%@*} domain=${1#*@} | |
1049 if ! host $domain >/dev/null 2>&1; then | |
1050 echo "ドメイン($domain)が見付かりません。" | |
1051 return 2 | |
1052 fi | |
1053 return 0 | |
1054 ;; | |
1055 *) echo "正しいメイルアドレスをいれてください"; return 3 ;; | |
1056 esac | |
1057 } | |
1058 invite() { | |
1059 email=`getpar email` | |
1060 case $email in | |
1061 *@*@*) repo="無効なアドレスです" ;; | |
1062 *@*) | |
1063 local=${email%@*} domain=${email#*@} | |
1064 if ! repo=`emaildomaincheck $email`; then | |
1065 repo="招待アドレスのエラー: $repo" | |
1066 elif [ -n "`query \"select * from user where name='$email';\"`" ]; then | |
1067 repo="$email さんは既に加入しています。" | |
1068 elif sendinvitation $email; then | |
1069 repo="アドレス($email)宛に案内を送信しました。" | |
1070 fi ;; | |
1071 "") repo="招待したい人のメイルアドレスを入力してください。" ;; | |
1072 *) repo="無効なアドレスです" ;; | |
1073 esac | |
1074 m4 -D_REPORT_="$repo" -D_ACTION_="$myname?invite" \ | |
1075 $layout/html.m4.html $layout/invite.m4.html | |
1076 } | |
1077 regist() { | |
1078 # $1=session-id-for-invitation | |
1079 m4 -D_TITLE_="Invitation" $layout/html.m4.html | |
1080 if [ -z "$1" ]; then | |
1081 echo "<p>bye bye</p>" | |
1082 reutrn | |
1083 fi | |
1084 email=`session=$1 getpar invite` | |
1085 if [ -z "$email" ];then | |
1086 cat<<EOF | |
1087 <p>無効な招待状チケットです。</p> | |
1088 <p>招待状の有効期限(1週間)が切れているか、チケット番号が異なっています。 | |
1089 加入している人に、再度招待してもらいましょう。</p> | |
1090 EOF | |
1091 reutrn | |
1092 fi | |
1093 echo "<h2>$email さんようこそ</h2>" | |
1094 query "replace into user values('$email');" | |
1095 # Fake login password to wasureta | |
1096 query "replace into par values('$session', 'pswd', 'string', 'wasureta'), | |
1097 ('$session', 'user', 'string', '$email');" | |
1098 wasureta $email | |
1099 m4 -D_SYSNAME_="Initial Login" -D_MYNAME_="$myname?userconf" \ | |
1100 $layout/login.m4.html | |
1101 return | |
1102 } | |
1103 userconf() { | |
1104 m4 -D_BODYCLASS_=userconf -D_TITLE_="ユーザ情報編集" $layout/html.m4.html | |
1105 GF_ACTION="?home" edittable "$formdir/user.def" "user" "$user" | |
1106 } | |
1107 groupconf() { | |
1108 # $1=rowid in grp (2015-07-21 changed from gname) | |
1109 m4 -D_BODYCLASS_=groupconf -D_TITLE_="グループ情報編集" $layout/html.m4.html | |
1110 #rowid=`query "select rowid from grp where gname='$1';"` | |
1111 rowid=${1%%[!A-Z0-9a-z_]*} | |
1112 err gcon \$1=$1 rowid=$rowid | |
1113 GF_ACTION="?grp+$1" edittable "$formdir/grp.def" "grp" "$rowid" | |
1114 } | |
1115 showhome() { | |
1116 # $1=userRowIdToShow | |
1117 err showhome \$1=$1 | |
1118 case "$1" in | |
1119 *@*) uname=`getvalbypkey user name "$1"` ;; | |
1120 *) uname=`getvalbyid user name $1` ;; | |
1121 esac | |
1122 err ShowHome: uname=$uname | |
1123 gecos=`gecos "$uname"` | |
1124 GF_VIEWONLY=1 | |
1125 cond="gname in (select gname from grp_mem where user='$uname')" | |
1126 if [ x"$user" = x"$uname" ]; then | |
1127 conflink="<a href=\"?userconf\">プロフィールの編集</a> / | |
1128 <a href=\"?blog\">新規話題の作成</a>" | |
1129 fi | |
1130 . ./s4-blog.sh | |
1131 | |
1132 m4 -D_BODYCLASS_=home -D_TITLE_="$gecos さん" \ | |
1133 -D_PROFILE_="`viewtable $formdir/user.def user $1`$conflink" \ | |
1134 -D_BLOGS_="`listblog $uname`" \ | |
1135 -D_GROUPS_="`listgroupbytable $formdir/grp.def $cond`" \ | |
1136 $layout/html.m4.html $layout/home.m4.html | |
1137 | |
1138 # | |
1139 # Record access log | |
1140 [ -n "$1" ] && [ x"$1" != x"$user" ] && acclog user $1 | |
1141 } | |
1142 | |
1143 listgroupbytable() { | |
1144 # $1=deffile | |
1145 tagline=`grep :tag: $1`; shift | |
1146 and="${1:+and }" | |
1147 href="<a href=\"$myname?grp+" | |
1148 echo '<div class="listgroup">' | |
1149 sql="select val from grp_s where key='tag' $and$* group by val order by ctime desc;" | |
1150 err ListGRP: query | |
1151 for tag in `query "$sql"` | |
1152 do | |
1153 err ListGrp: tag=$tag | |
1154 tn=${tagline%%=${tag}*} | |
1155 tn=${tn##*[ :]} | |
1156 sql="select gname as 'グループ名',説明 from (select gname,max(case key when 'gecos' then val end) as '説明',max(case key when 'tag' then val end) as 'tag' from grp_s group by gname having tag='$tag');" | |
1157 err PersonalGroupList= `echo $sql` | |
1158 echo "<h2>$tn</h2>" | |
1159 echo '<table class="b listgroup">' | |
1160 sq -header -html $db "$sql" \ | |
1161 | sed "s,\(<TR><TD>\)\([-_A-z0-9]*\)</TD>,\1$href\2\">\2</a>," | |
1162 echo '</table>' | |
1163 done | |
1164 echo '</div>' | |
1165 } | |
1166 iconhref() ( | |
1167 # $1=icon-file, $2=Href $3=title $4...=anchor | |
1168 data=`percenthex $1` | |
1169 ct=`file --mime-type $1|cut -d' ' -f2` | |
1170 err iconhref: \$1=$1 \$2=$2 \$3="$@" | |
1171 href=$2; title=$3; shift 3 | |
1172 echo "<a href=\"$href\"><img title=\"$title\" src=\"data:$ct,$data\">$@</a>" | |
1173 ) | |
1174 iconhref2() ( | |
1175 # $1=icon-file, $2=Href $3=title $4...=anchor | |
1176 src=$1 | |
1177 href=$2; title=$3; shift 3 | |
1178 echo "<a href=\"$href\"><img title=\"$title\" src=\"$src\">$@</a>" | |
1179 ) | |
1180 listentry() ( | |
1181 # $1=user/group $2=SearchKeyword $3=condition(if any) | |
1182 # Referring variable $iamowner=$grp to attach owner-request links | |
1183 err listentry: \$1=$1 \$2=$2 \$3=$3 | |
1184 cond= | |
1185 offset=`getpar offset` | |
1186 offset=${offset%%[!0-9]*} | |
1187 offset=$((offset + 0)) # change to numeric forcibly | |
1188 [ $offset -lt 0 ] && offset=0 | |
1189 limit=30 | |
1190 dir=$tmpdir/$imgcached/$thumbxy.`echo $1|md5` | |
1191 if [ x"$1" = x"user" ]; then | |
1192 hrb="$myname?home" | |
1193 deficon=person-default.png | |
1194 entity="ユーザ" tbl=user link=rowid nm=name stage=mems | |
1195 else # if group | |
1196 hrb="$myname?grp" | |
1197 deficon=person-default.png | |
1198 entity="グループ" tbl=grp link=rowid nm=gname stage=grps | |
1199 tagline=`grep :tag: $formdir/grp.def|cut -d: -f5-` | |
1200 if [ -n "$tagline" ]; then | |
1201 tagconv=`echo $tagline|sed 's/\([^= :]*\)=\([^= :]*\)/-D\2=\1/g'` | |
1202 err tagconv=$tagconv | |
1203 fi | |
1204 fi | |
1205 if [ ! -d $dir ]; then | |
1206 mkdir -p $dir | |
1207 convert -geometry $thumbxy $imgdir/$deficon $dir/$deficon | |
1208 fi | |
1209 if [ -n "$2" ]; then | |
1210 cond="where nick like '%$2%' or b.name like '%$2%'" | |
1211 fi | |
1212 | |
1213 # XX: これ複雑すぎるかな。もっとシンプルにしたい。$3条件も。2015-07-08 | |
1214 qgrp=`sqlquote $grp` | |
1215 qgrp=${qgrp:-'""'} | |
1216 sql="select a.rowid, a.$link, coalesce(b.gecos, a.$nm) as nick, b.tag, | |
1217 case when a.$nm in (select user from grp_adm | |
1218 where gname=$qgrp) then '(管理者)' -- from group mode | |
1219 when '$user' in (select user from grp_adm where gname=a.$nm) | |
1220 then '(ADMIN)' | |
1221 when '$iamowner' = '' then '' | |
1222 else ',not='||a.rowid end as ownerlink | |
1223 from $tbl a left join | |
1224 (select $nm as name, | |
1225 max(case key when 'gecos' then val end) as gecos, | |
1226 max(case key when 'tag' then val end) as tag | |
1227 from ${tbl}_s group by $nm) | |
1228 b on a.$nm=b.name $cond $3 | |
1229 order by b.tag desc, a.rowid asc" | |
1230 err LE:sql.1="$sql" | |
1231 total=`query "with x as ($sql) select count(*) from x;"` | |
1232 echo "<h2>${entity} 一覧</h2>" | |
1233 if [ $total -gt $limit ]; then | |
1234 echo '<div class="right">' | |
1235 cgi_form $stage <<EOF | |
1236 <label>次の語を含む${entity}で検索: | |
1237 `cgi_text kwd $kwd`</label> | |
1238 EOF | |
1239 echo '</div>' | |
1240 fi | |
1241 cat<<EOF | |
1242 <p>${total}件中の${offset}件めから${kwd:+" - 検索語: $kwd"}</p> | |
1243 EOF | |
1244 if [ $((offset+limit)) -lt $total ]; then | |
1245 cat<<EOF | |
1246 <div class="right"><form action="$myname" method="POST"> | |
1247 `cgi_submit 次の${limit}件` | |
1248 `cgi_hidden kwd "$kwd"` | |
1249 `cgi_hidden stage "$stage"` | |
1250 `cgi_hidden offset $((offset + limit))`</form></div> | |
1251 EOF | |
1252 fi | |
1253 if [ $offset -gt 0 ]; then | |
1254 cat<<EOF | |
1255 <form action="$myname" method="POST"> | |
1256 `cgi_submit 前の${limit}件` | |
1257 `cgi_hidden stage "$stage"` | |
1258 `cgi_hidden kwd "$kwd"` | |
1259 `cgi_hidden offset $((offset - limit))`</form> | |
1260 EOF | |
1261 fi | |
1262 | |
1263 err ListEntry: `echo $sql\;` | |
1264 | |
1265 query "$sql limit $limit ${offset:+offset $offset};" \ | |
1266 | while IFS='|' read id lnk name tag ownerp; do | |
1267 err name=$name owner=$ownerp lnk=$lnk | |
1268 err newlnk=$lnk | |
1269 files=`getvalbyid $tbl profimg $id $dir` | |
1270 # Pick up only first icon | |
1271 echo "<div class=\"iconlist xy$thumbxy\"><p class=\"tag _$tag\">$tag</p>" \ | |
1272 | m4 $tagconv | |
1273 if [ -n "$files" ]; then | |
1274 icon=`echo "$files"|head -1` | |
1275 iconhref $dir/$icon "$hrb+$lnk" "$name" | |
1276 else | |
1277 iconhref $dir/$deficon "$hrb+$lnk" "$name" | |
1278 fi | |
1279 echo "<br>$name${ownerp:+<br>$ownerp}" | |
1280 echo "</div>" | |
1281 done | |
1282 ) | |
1283 listmember() { | |
1284 listentry user "$@" | |
1285 } | |
1286 listgroup() { | |
1287 listentry group "$@" | |
1288 } | |
1289 showgroup() { | |
1290 grp=$1 | |
1291 err showgroup1: grp=$grp qgrp="[$(sqlquote $grp)]" | |
1292 | |
1293 gname=`getpar gname` | |
1294 if [ -n "$gname" ]; then | |
1295 err UPdating/Removing of group::::::: | |
1296 par2table $formdir/grp.def | |
1297 fi | |
1298 err showgroup2: grp=$grp qgrp="[$(sqlquote $grp)]" | |
1299 if isgroup "$grp"; then | |
1300 showgroupsub $formdir/grp.def "$grp" | \ | |
1301 m4 -D_TITLE_="グループ $grp" \ | |
1302 -D_FORM_="syscmd(\`cat')" \ | |
1303 -D_DUMPTABLE_="" \ | |
1304 $layout/groupman.m4.html | |
1305 else # if $grp is removed at par2table | |
1306 listgroup | |
1307 fi | |
1308 } | |
1309 showgroupsub() { | |
1310 # $1=def-file $2=group | |
1311 grp=$2 | |
1312 qgrp=`sqlquote $grp` | |
1313 rowid=`sq $db "select rowid from grp where gname=$qgrp"` | |
1314 if [ -z "$rowid" ]; then | |
1315 rowid=`sq $db "select rowid from grp where rowid=$grp"` | |
1316 grp=`sq $db "select gname from grp where rowid=$grp"` | |
1317 fi | |
1318 mmgrp=`echo "$grp"|nkf -Ww -MQ|tr '=' '%'` | |
1319 val=`getvalbyid grp profimg $rowid $tmpd` | |
1320 # 6/14の次グループのHOMEで出す情報を作る Done | |
1321 viewtable $1 grp $rowid | |
1322 if isgrpowner "$user" "$grp"; then | |
1323 echo "<p><a href=\"?groupconf+$rowid\">グループ情報の編集</a>" | |
1324 iamowner=$grp | |
1325 fi | |
1326 if ismember "$user" "$grp"; then | |
1327 echo "${iamowner:+ / }<a href=\"?blog+$rowid\">グループの新規話題作成</a></p>" | |
1328 fi | |
1329 # 加入ボタン + 加入者リスト | |
1330 err ismember $user $grp | |
1331 ismember "$user" "$grp" && ismem='checked' || nomem='checked' | |
1332 # このグループでの加入アドレス | |
1333 eml=`query "select val from grp_mem_s where gname='$2' and user='$user' \ | |
1334 and key='email';"` | |
1335 err EML: "select val from grp_mem_s where gname='$2' and user='$user' \ | |
1336 and key='email';" | |
1337 err email=$eml | |
1338 echo '<h2>このグループに</h2>' | |
1339 cgi_form grp <<EOF | |
1340 <table class="b"> | |
1341 <tr><th>メンバーとして</th><td> | |
1342 <label>`cgi_radio joingrp "yes" $ismem`参加</label> / | |
1343 <label>`cgi_radio joingrp "no" $nomem`参加しない</label></td></tr> | |
1344 <tr><th>参加する場合のメイルアドレス<br> | |
1345 <small>(メインのアドレスとは違うものにする場合に記入<br> | |
1346 同じでよい場合は空欄に)</small></th> | |
1347 <td>`cgi_text email $eml`</td></tr> | |
1348 </table> | |
1349 `cgi_hidden grp $grp` | |
1350 EOF | |
1351 | |
1352 echo '<h2>話題一覧</h2>' | |
1353 cgi_form searchart<<EOF | |
1354 <label>`cgi_text kwd`という語を含むコメントを検索</label> | |
1355 `cgi_hidden owner $grp` | |
1356 EOF | |
1357 cond="where a.id in (select id from blog_s where key='owner' and val='$grp') order by ctime desc" | |
1358 DT_CHLD=article:blogid \ | |
1359 DT_VIEW=replyblog dumptable html blog 'ctime title heading' "$cond" | |
1360 | |
1361 c="group by b.name having b.name in (select user from grp_mem where gname='$grp')" | |
1362 cm="?commission+$mmgrp" | |
1363 thumbxy=50x50 listmember "" "$c" \ | |
1364 |sed -e "s|\(<br>\),not=\(.*\)|\1<a href=\"$cm+\2\">管理者委託</a>|" | |
1365 } | |
1366 joingrp() { | |
1367 # $1=group $2=user $3=yes/no $4=email(if any $5=AsAdmin) | |
1368 err joingrp: \$1=$1 \$2=$2 \$3=$3 \$4=$4 | |
1369 isgrpowner "$user" "$1" && isowner="yes" || isowner="" | |
1370 err jg:isgrpowner: isowner="$isowner" | |
1371 if [ x"$2" != x"$user" ]; then # if user is not login user | |
1372 if [ -z "$isowner" ; then | |
1373 echo "<p>本人か、グループ管理者しか加入操作はできません。</p>" | |
1374 return | |
1375 fi | |
1376 fi | |
1377 cond="where gname='$1' and user='$2'" | |
1378 if [ x"$3" = x"yes" ]; then | |
1379 query "replace into grp_mem values('$1', '$2');" | |
1380 if [ -n "$4" ]; then | |
1381 if msg=`emaildomaincheck "$4"`; then | |
1382 err "replace into grp_mem_s values('$1', '$user', 'email', \ | |
1383 'string', '$4', NULL);" | |
1384 query "replace into grp_mem_s values('$1', '$user', 'email', \ | |
1385 'string', '$4', NULL);" | |
1386 if [ -n "$5" ]; then # as ADMIN | |
1387 # Coming here means newly created group | |
1388 sql="select case\ | |
1389 when (select count(*) from grp_mem where gname='$1')=1\ | |
1390 then (select user from grp_mem\ | |
1391 where gname='$1' and user='$user')\ | |
1392 else '' end;" | |
1393 err NewGrpChk: $sql | |
1394 if [ -n "`query \"$sql\"`" ]; then | |
1395 err ADMIN: "replace into grp_adm values('$1', '$user');" | |
1396 query "replace into grp_adm values('$1', '$user');" | |
1397 fi | |
1398 fi | |
1399 else | |
1400 echo $msg | |
1401 fi | |
1402 else | |
1403 query "delete from grp_mem_s $cond and key='email';" | |
1404 fi | |
1405 else | |
1406 query "delete from grp_mem $cond; | |
1407 delete from grp_mem_s $cond; | |
1408 delete from grp_mem_m $cond;" | |
1409 fi | |
1410 } | |
1411 grp_reg_adm() { | |
1412 # $1=grp $2=user-rowid | |
1413 if ! isgrpowner $user $1; then | |
1414 echo "<p>$1 グループの管理者しかこの操作はできません。"; return | |
1415 fi | |
1416 newadm=`query "select name from user where rowid=$2;"` | |
1417 if [ -z "$newadm" ]; then | |
1418 echo "<p>指定ユーザIDがおかしいようです。</p>"; return | |
1419 fi | |
1420 err GRP_reg_adm: "replace into grp_adm values('$1', '$newadm');" | |
1421 err ismember $newadm $1 | |
1422 if ismember $newadm $1; then | |
1423 # OK, go ahead | |
1424 query "replace into grp_adm values('$1', '$newadm');" | |
1425 # confirm insertion | |
1426 sql="select * from grp_adm where gname='$1' and user='$newadm'" | |
1427 if [ -n "`query \"$sql;\"`" ]; then | |
1428 echo "<p>追加完了</p>" | |
1429 else | |
1430 echo "<p>追加失敗($1 $2)</p>" | |
1431 fi | |
1432 fi | |
1433 showgroup $1 | |
1434 } | |
1435 dumptable() { | |
1436 # $1=mode $2=Table $3=column-list-of-*_s(defaults to *) $4=conditions(if any) | |
1437 # textのフィールドだけ全てダンプにしたほうがいいか | |
1438 # $DT_VIEW sets link | |
1439 # 6/17の次: editリンクじゃなくてスレッドVIEWリンクでいいんちゃう? | |
1440 ### elink="<a href=\"$myname?edittable+$2+\\2\">EDIT</a>" | |
1441 VIEW=${DT_VIEW-replyblog} | |
1442 if [ -n "$VIEW" ]; then | |
1443 dvlink=" <a href=\"$myname?$VIEW+\\2\">VIEW</a>" | |
1444 fi | |
1445 # $DT_CHLD=ChildTable:BindColumn | |
1446 if [ -n "$DT_CHLD" ]; then | |
1447 _t=${DT_CHLD%:*} _i=${DT_CHLD#*:} | |
1448 cntall="(select count($_i) from $_t where $_i=a.id)" | |
1449 cntnew="(select count(val) from ${_t}_s where key='ctime' \ | |
1450 and id in (select id from $_t where $_i=a.id) \ | |
1451 and val > coalesce((select time from acclog where \ | |
1452 user='$user' and tbl='$2' and rowid=a.rowid),\ | |
1453 '1970-01-01'))" | |
1454 cnt="$cntnew||'/'||$cntall as '新着/総数'," | |
1455 dt_class=" td2r" | |
1456 fi | |
1457 # Construct join expression | |
1458 eav="" scols="" | |
1459 pk=`gettblpkey $2` | |
1460 substr=${dumpcollen:+"substr(val, 0, $dumpcollen)"} | |
1461 substr=${substr:-val} | |
1462 for col in ${3:-`gettbl_s_cols $2`}; do | |
1463 case $col in | |
1464 *:*) col=${col%:*} as=${col#*:} ;; | |
1465 *) as=${col} ;; | |
1466 esac | |
1467 eav=$eav${eav:+,}" max(case key when '$col' then $substr end) as $as" | |
1468 scols="$scols${scols:+, }b.$as" | |
1469 done | |
1470 #case author when '$user' then a.rowid else '---' end as ID, | |
1471 sql="select \ | |
1472 a.rowid as ID,\ | |
1473 $cnt\ | |
1474 $scols from $2 a left join (select $pk,$eav from ${2}_s group by $pk) b on a.$pk=b.$pk $4;" | |
1475 err SQL="$sql" | |
1476 cat<<EOF | sed "s,\(<TR><TD>\)\([1-9][0-9]*\)</TD>,\1$elink$dvlink</TD>," | |
1477 <div class="dumptable"> | |
1478 <table class="b$dt_class"> | |
1479 `sq -header -cmd ".mode $1" $db "$sql"` | |
1480 </table> | |
1481 </div> <!-- dumptable --> | |
1482 EOF | |
1483 } | |
1484 | |
1485 par2table() ( | |
1486 # copy current parameters of par into destination table | |
1487 # $1=definition-file | |
1488 # Using $user and $session | |
1489 rowid=`getpar rowid` | |
1490 err ...........rowid=$rowid | |
1491 if [ ! -e $1 ]; then | |
1492 echo "<p>テーブル定義ファイルが見付かりません</p>" | |
1493 exit 1 | |
1494 fi | |
1495 tbl=${1%.def} | |
1496 tbl=${tbl##*/} | |
1497 if [ -n "$rowid" ]; then # Modify existing entry | |
1498 if [ x"$tbl" = x"user" ]; then | |
1499 rowowner=`query "select name from $tbl where rowid=$rowid;"` | |
1500 elif [ x"$tbl" = x"grp" ]; then | |
1501 sql="select gname from $tbl where rowid=$rowid;" | |
1502 ##err p2t:grp:q $sql | |
1503 isgrpowner $user "`query $sql`" && rowowner=$user | |
1504 else | |
1505 rowowner=`query "select owner from $tbl where rowid=$rowid;"` | |
1506 rowowner=${rowowner:-`query "select author from $tbl | |
1507 where rowid=$rowid;"`} | |
1508 fi | |
1509 ### err rowowner=$rowowner | |
1510 if [ x"$user" != x"$rowowner" ]; then | |
1511 echo "<p>他人のレコードはいじれないの</p>" | |
1512 return | |
1513 elif [ -z "$rowowner" ]; then | |
1514 echo "<p>指定したレコードはないみたい</p>" | |
1515 return | |
1516 fi | |
1517 rm=`getpar rm` cfm=`getpar confirm` | |
1518 # Editing existent entry | |
1519 if [ x"$rm" = x"yes" ]; then | |
1520 if [ x"$rm$cfm" = x"yesyes" ]; then | |
1521 query "delete from $tbl where rowid=$rowid;" | |
1522 return | |
1523 else | |
1524 echo "<p>消去確認のチェックがないので消さなかったの...</p>" | |
1525 return | |
1526 fi | |
1527 fi | |
1528 fi | |
1529 # XX: Subshelling here is unnecessary 2015-07-05 | |
1530 (ts=${tbl}_s tm=${tbl}_m val="" pval="" formaster="" getrow="" | |
1531 if [ -n "$rowid" ]; then | |
1532 # Update of existing record | |
1533 for col in `gettblcols $tbl`; do | |
1534 val=`getparquote $col` | |
1535 [ -z "$val" ] && continue | |
1536 err sq $db "update $tbl set $col=$val where rowid=$rowid" | |
1537 query "update $tbl set $col=$val where rowid=$rowid;" | |
1538 done | |
1539 # Then, set up $pval for further insertion of tbl_s and tbl_m | |
1540 for col in `gettblpkey $tbl`; do | |
1541 val=`query "select $col from $tbl where rowid=$rowid;"|sed -e 's/\"/\"\"/g'` | |
1542 pval="$pval${pval:+, }\"$val\"" | |
1543 done | |
1544 else | |
1545 # New entry | |
1546 # Generate values() for primary keys | |
1547 for col in `gettblpkey $tbl`; do | |
1548 # Genuine primary keys for _m and _s | |
1549 val=`getvalquote $tbl $col` | |
1550 [ -z "$val" ] && continue | |
1551 pval="$pval${pval:+, }$val" | |
1552 done | |
1553 err pval=$pval | |
1554 for col in `gettblfkey $tbl`; do | |
1555 # args for values() to insertion into master table | |
1556 val=`getvalquote $tbl $col` | |
1557 [ -z "$val" ] && continue | |
1558 formaster=$formaster"${formaster:+, }$val" | |
1559 done | |
1560 formaster="$pval${formaster:+, }$formaster" | |
1561 err formaster=$formaster | |
1562 if [ -z "$formaster" ]; then | |
1563 echo "<pre>項目を全て埋めてください</pre>" | |
1564 return 1 | |
1565 fi | |
1566 err "replace into $tbl values($formaster);" | |
1567 echo "replace into $tbl values($formaster); $getrow" | sq $db | |
1568 ## Insertion to master table, done | |
1569 fi | |
1570 | |
1571 for kt in s m; do | |
1572 tb2=${tbl}_$kt | |
1573 for col in `gettbl_${kt}_cols $tbl`; do | |
1574 ptype=`getpartype $col "limit 1"` | |
1575 | |
1576 # First, check update of existing entries in _m | |
1577 if [ $kt = m ]; then | |
1578 # sessID|address.1.22|string|Somewhere-x.y.z | |
1579 sql="" | |
1580 err dots from query "select var from par where var like '$col.%';" | |
1581 for v in `query "select var from par where var like '$col.%';"`; do | |
1582 # v=address.1.22 | |
1583 rowid=${v##*.} | |
1584 origcol=${v%%.*} # original column derived from | |
1585 err Updating for $v rowid=$rowid, partype=`getpartype $v` | |
1586 ##case `getpartype $v` in | |
1587 err CASE `gettbl_coltype $tbl/$origcol` in | |
1588 err edit flag = `getpar action.$v` | |
1589 case `getpar action.$v` in | |
1590 rm) | |
1591 if [ x`getpar confirm.$v` = x"yes" ]; then | |
1592 newsql="delete from $tb2" | |
1593 else | |
1594 echo "<p>削除確認未チェック</p>" | |
1595 fi ;; | |
1596 edit) | |
1597 case `gettbl_coltype $tbl/$origcol` in | |
1598 image|document|binary) | |
1599 file=$tmpd/`getparfilename $v` | |
1600 err type=file=$file | |
1601 [ -z "$file" ] && continue | |
1602 bn=${file##*/} | |
1603 bin="X'"$(hexize $file)"'" | |
1604 ct=`file --mime-type $file|cut -d' ' -f2` | |
1605 type=\"file:$ct\" | |
1606 newsql="update $tb2 set val='$bn', type=$type, bin=$bin" | |
1607 ;; | |
1608 *) | |
1609 newsql="update $tb2 set val=(select val from par where var \ | |
1610 like '$col.%.$rowid')" | |
1611 ;; | |
1612 esac | |
1613 ;; | |
1614 *) # maybe `keep', do not modify value | |
1615 continue | |
1616 ;; | |
1617 esac | |
1618 # err newsql=$newsql | |
1619 sql=$sql$nl"$newsql where rowid=$rowid;" | |
1620 done | |
1621 echo "$sql | |
1622 delete from $tb2 where type='string' and val='';" | sq $db | |
1623 # Rest of kt==m: set multiple mode | |
1624 nr=`getparcount $col` | |
1625 else | |
1626 nr=1 # for kt==s, number of records is 1 | |
1627 fi | |
1628 | |
1629 i=0 | |
1630 while [ $i -lt $nr ]; do | |
1631 limit="limit 1 offset $i" | |
1632 i=$((i+1)) # increase beforehand against continue | |
1633 val=`getvalquote $tbl $col "$limit"` | |
1634 [ -z "$val" -o x"$val" = x'""' -o x"$val" = x"NULL" ] && continue | |
1635 err $col=$val | |
1636 bin=NULL | |
1637 err partype$col=`getpartype $col "$limit"` | |
1638 case $ptype in | |
1639 file) file=$tmpd/`getparfilename $col "$limit"` | |
1640 err parfile-$col=$file | |
1641 [ -z "$file" ] && continue | |
1642 bin="X'"$(hexize $file)"'" | |
1643 ct=`file --mime-type $file|cut -d' ' -f2` | |
1644 type=\"file:$ct\" ;; | |
1645 "*"*) continue ;; # foreign table | |
1646 *) type=\"string\" ;; | |
1647 esac | |
1648 case `gettbl_coltype $tbl/$col` in | |
1649 password) # special care for password | |
1650 # name={password,pswd1,pswd2} | |
1651 p1=`getpar pswd1 "$limit"` | |
1652 if [ -z "$p1" ]; then | |
1653 continue # SKIP password setting, if p1 is empty | |
1654 else | |
1655 pswd=`getpar pswd "$limit"` p2=`getpar pswd2 "$limit"` | |
1656 ## err pswd=$pswd | |
1657 if pwcheck "$pswd"; then | |
1658 if [ x"$p1" = x"$p2" ]; then | |
1659 case "$p1" in | |
1660 ????????*) ;; | |
1661 *) echo "<p>8字以上にしてください。</p>" | |
1662 return ;; | |
1663 esac | |
1664 val="\"`echo $p1|mypwhash`\"" | |
1665 else | |
1666 echo "<p>2つの新パスワード不一致</p>" | |
1667 return | |
1668 fi | |
1669 else | |
1670 echo "<p>旧パスワード違います</p>" | |
1671 return | |
1672 fi | |
1673 fi | |
1674 ;; | |
1675 esac | |
1676 ## err p2t: "replace into $tb2 values($pval, \"$col\", $type, $val, $bin);" | |
1677 echo "replace into $tb2 values($pval, \"$col\", $type, $val, $bin);" \ | |
1678 | sq $db | |
1679 done | |
1680 done | |
1681 done | |
1682 return 0 | |
1683 err donee) | |
1684 ) | |
1685 par2table_old() { | |
1686 # copy current parameters of par into destination table | |
1687 # $1=dst-table $2=definition-file | |
1688 # Using $user and $session | |
1689 rowid=`getpar rowid` | |
1690 if [ -n "$rowid" ]; then | |
1691 rm=`getpar rm` cfm=`getpar confirm` | |
1692 if [ x"$rm$cfm" = x"yesyes" ]; then | |
1693 sq $db "delete from $1 where rowid=$rowid and owner=\"$user\"" | |
1694 return | |
1695 fi | |
1696 fi | |
1697 cat $2 \ | |
1698 | (cols="" | |
1699 while IFS=: read prompt name type args; do | |
1700 [ x"$name" = x"stage" ] && continue | |
1701 if [ -n "$rowid" ]; then | |
1702 val=`getpar $name|sed -e 's/\"/\"\"/g'` | |
1703 sq $db "update $1 set $name=\"$val\" where rowid=$rowid and owner=\"$user\"" | |
1704 else | |
1705 eav=$eav${eav+,}" max(case var when '$name' then val end)" | |
1706 fi | |
1707 done | |
1708 [ -n "$rowid" ] && return | |
1709 cond="where sessid='$session' group by sessid" | |
1710 sq $db "replace into $1 select $eav,\"$user\" from par $cond" | |
1711 # Think over again about putting $user | |
1712 ) | |
1713 } | |
1714 genform() { | |
1715 # $1 = form definition file | |
1716 # $2, $3 (optional)= table name and ROWID | |
1717 # If $GF_VIEWONLY set and nonNull, output values without form | |
1718 # If $GF_HIDDEN set, use it hidden values | |
1719 # If $GF_OWNER set, use it as value of name="owner" | |
1720 # If $GF_STAGE set, use it as value of name="stage" | |
1721 forms="" hiddens="" rowid=$3 | |
1722 if [ ! -e "$1" ]; then | |
1723 echo "<p>そのようなデータベースはないようです($2)。</p>" | |
1724 return | |
1725 elif [ -n "$2" ]; then | |
1726 err genform1: "select * from $2 where rowid='$rowid'" | |
1727 rec=`query "select * from $2 where rowid='$rowid';"` | |
1728 if [ -z "$rec" ]; then | |
1729 pk=`gettblpkey $2` | |
1730 ###rec=`sq $db "select rowid from $2 where $pk='$rowid'"` | |
1731 err "select rowid from $2 where $pk='$rowid';" | |
1732 rec=`query "select rowid from $2 where $pk='$rowid';"` | |
1733 err rec-rowid=$rec | |
1734 rowid=$rec | |
1735 rec=$3 | |
1736 fi | |
1737 if [ -z "$rec" ]; then | |
1738 echo "<p>そんなレコードはないみたいね...</p>" | |
1739 return | |
1740 fi | |
1741 fi | |
1742 if [ -z "$GF_VIEWONLY" ]; then | |
1743 rm='<input id="rm" name="rm" type="checkbox" | |
1744 value="yes"><label for="rm">このエントリの削除</label> | |
1745 <span>ほんとうに消しますよ(確認)! | |
1746 <input name="confirm" type=checkbox value="yes">はい</span>' | |
1747 fi | |
1748 while IFS=: read prompt name keytype type args; do | |
1749 [ -z "${prompt%%\#*}" ] && continue # skip comment line(#) | |
1750 sp="${args:+ }" | |
1751 form="" val="" | |
1752 if [ -n "$rowid" ]; then | |
1753 val=`getvalbyid $2 $name $rowid $tmpd` | |
1754 fi | |
1755 if [ -n "$GF_VIEWONLY" ]; then | |
1756 is_hidden "$2" "$name" && continue | |
1757 fi | |
1758 case "$type" in | |
1759 text*) | |
1760 cgiform=cgi_multi_$type | |
1761 if [ -s $tmpd/$name.count ]; then | |
1762 form=`$cgiform $name $tmpd` | |
1763 val=$(echo "$val"| | |
1764 while read fn; do | |
1765 echo "<tr><td>`cat $tmpd/$fn`</td></tr>$nl" | |
1766 done) | |
1767 val="<table>$nl$val$nl</table>" | |
1768 else | |
1769 #form="<input name=\"$name\" value=\"$val\" type=\"$type\"$sp$args>$nl" | |
1770 err genform: cgi_$type $name $val "$args" | |
1771 form=`cgi_$type $name "$val" "$args"` | |
1772 fi | |
1773 ;; | |
1774 [Rr][Aa][Dd][Ii][Oo]) | |
1775 fh="<input type=\"radio\" name=\"$name\"" | |
1776 form="`echo $args|sed -e \ | |
1777 \"s,\([^=][^=]*\)=\([^= ][^= ]*\),$fh value=\\"\2\\">\1,g\"`" | |
1778 ;; | |
1779 [Ss][Ee][Ll][Ee][Cc][Tt]) | |
1780 fh="<select name=\"$name\">$nl" | |
1781 form="`echo $args|sed -e \ | |
1782 's,\([^=][^=]*\)=\([^= ][^= ]*\),<option value=\"\2\">\1</option>,g'`" | |
1783 | |
1784 if [ -n "$val" ]; then | |
1785 form=`echo $form|sed -e "s,\(value=.$val.\),\\1 selected,"` | |
1786 fi | |
1787 form="$fh$form</select>" | |
1788 ;; | |
1789 [Ii][Mm][Aa][Gg][Ee]|[Dd][Oo][Cc][Uu][Mm][Ee][Nn][Tt]|[Bb]inary) | |
1790 if [ -s $tmpd/$name.count ]; then | |
1791 form=`cgi_multi_file $name $tmpd "$args"` | |
1792 if [ -n "$val" ]; then | |
1793 hrfb="$myname?showattc+$2_m" | |
1794 val=$(echo "$val" \ | |
1795 | while read fn; do | |
1796 data=`percenthex $tmpd/$fn` | |
1797 #ct=`cat $tmpd/$fn.content-type` | |
1798 ct=`file --mime-type $tmpd/$fn|cut -d' ' -f2` | |
1799 ri=`cat $tmpd/$fn.rowid` | |
1800 ## err fn=$fn, name=$name, ri=$ri; ls -lF $tmpd 1>&3 | |
1801 imgsrc="<img src=\"data:$ct,$data\">" | |
1802 echo "<a href=\"$hrfb+$ri\">$imgsrc</a><br>" | |
1803 done) | |
1804 fi | |
1805 else | |
1806 form="<input type=\"file\" name=\"$name\" $args>" | |
1807 if [ -n "$val" ]; then | |
1808 imgs=$(echo "$val"\ | |
1809 |while read fn;do | |
1810 data=`percenthex $tmpd/$fn` | |
1811 echo "<img src=\"data:image/png,$data\">$fn<br>" | |
1812 done) | |
1813 form=$form"<br>$imgs" | |
1814 val=$imgs # 2015-06-15 | |
1815 else | |
1816 form="<input type=\"file\" name=\"$name\" $args>" | |
1817 fi | |
1818 fi | |
1819 ;; | |
1820 [Hh][Ii][Dd][Dd][Ee][Nn]) | |
1821 if [ -n "$GF_STAGE" -a x"$name" = x"stage" ]; then | |
1822 args="value=\"$GF_STAGE\"" | |
1823 fi | |
1824 form="<input type=\"hidden\" name=\"$name\" $args>" | |
1825 prompt='' # Remove prompt | |
1826 ;; | |
1827 [Aa][Uu][Tt][Hh][Oo][Rr]) | |
1828 form="<input type=\"hidden\" name=\"author\" value=\"$user\">" | |
1829 prompt="" ;; | |
1830 [Oo][Ww][Nn][Ee][Rr]) | |
1831 val=${GF_OWNER:-$val} | |
1832 val=${val:-$user} | |
1833 form="<input type=\"hidden\" name=\"owner\" value=\"$val\">" | |
1834 prompt="" ;; | |
1835 [Uu][Ss][Ee][Rr]) | |
1836 # XXX: is null $user ok? | |
1837 #form="<input type=\"hidden\" name=\"user\" value=\"$user\">" | |
1838 [ -n "$GF_VIEWONLY" ] && continue | |
1839 form="$user" | |
1840 ;; | |
1841 [Pp]assword) | |
1842 [ -n "$GF_VIEWONLY" ] && continue | |
1843 form="`cgi_passwd`" | |
1844 val="" | |
1845 ;; | |
1846 [Ss][Ee][Rr][Ii][Aa][Ll]|[Ss][Tt][Aa][Mm][Pp]) | |
1847 val=$((($(date +%s)-1433084400)/10))c$$ | |
1848 form="<input type=\"hidden\" name=\"serial\" value=\"$val\">" | |
1849 prompt="" ;; | |
1850 [Ss][Ee][Ss][Ss][Ii][Oo][Nn]) | |
1851 prompt="" | |
1852 ;; | |
1853 parent|path|blog*) | |
1854 prompt="" | |
1855 ;; | |
1856 "*"*) | |
1857 tail=$tail"``" | |
1858 continue ;; | |
1859 esac | |
1860 if [ -n "$prompt" ]; then | |
1861 if [ -n "${GF_VIEWONLY}" ]; then | |
1862 form=$val | |
1863 else | |
1864 : | |
1865 fi | |
1866 forms=$forms" <tr class=\"$name\"><th>$prompt</th><td>$form</td></tr>$nl" | |
1867 else | |
1868 hiddens=$hiddens$nl"$form" | |
1869 fi | |
1870 done < $1 | |
1871 # enctype="multipart/form-data" | |
1872 cat<<EOF | |
1873 <form action="${GF_ACTION:-$myname}" method="POST" enctype="multipart/form-data"> | |
1874 ${rowid:+$rm} | |
1875 <table class="b $2"> | |
1876 $forms | |
1877 </table>$hiddens | |
1878 ${GF_STAGE:+`cgi_hidden stage $GF_STAGE`} | |
1879 ${rowid:+<input type="hidden" name="rowid" value="$rowid">} | |
1880 EOF | |
1881 if [ -z $GF_VIEWONLY ]; then | |
1882 cat<<EOF | |
1883 <input type="submit" name="sub" value="OK"> | |
1884 <input type="reset" name="res" value="Reset"> | |
1885 EOF | |
1886 fi | |
1887 cat<<EOF | |
1888 </form> | |
1889 $tail | |
1890 EOF | |
1891 } | |
1892 edittable() { | |
1893 # $1=form-def $2=table $3 rowid | |
1894 genform "$@" | |
1895 } | |
1896 viewtable() { | |
1897 GF_VIEWONLY=1 genform "$@" | |
1898 } |