Artifact
6089b80c7da7cb12d560e32cb030b2ed24a03374 :
File
extroot/survey
— part of check-in
[43bb630b42]
at
2021-09-19 09:31:17
on branch trunk
— crude survey tool (storage backend only)
(user:
mario
size: 1473)
0000: 23 21 2f 75 73 72 2f 62 69 6e 2f 70 68 70 2d 63 #!/usr/bin/php-c
0010: 67 69 20 2d 64 63 67 69 2e 66 6f 72 63 65 5f 72 gi -dcgi.force_r
0020: 65 64 69 72 65 63 74 3d 30 0a 3c 3f 70 68 70 0a edirect=0.<?php.
0030: 23 20 65 6e 63 6f 64 69 6e 67 3a 20 75 74 66 2d # encoding: utf-
0040: 38 0a 23 20 61 70 69 3a 20 63 67 69 0a 23 20 74 8.# api: cgi.# t
0050: 79 70 65 3a 20 73 74 6f 72 65 0a 23 20 63 61 74 ype: store.# cat
0060: 65 67 6f 72 79 3a 20 64 61 74 61 62 61 73 65 0a egory: database.
0070: 23 20 74 69 74 6c 65 3a 20 53 75 72 76 65 79 20 # title: Survey
0080: 73 75 62 6d 69 74 0a 23 20 64 65 73 63 72 69 70 submit.# descrip
0090: 74 69 6f 6e 3a 20 53 74 6f 72 65 20 73 69 6d 70 tion: Store simp
00a0: 6c 65 20 50 4f 53 54 20 66 6f 72 6d 20 64 61 74 le POST form dat
00b0: 61 20 69 6e 74 6f 20 64 61 74 61 62 61 73 65 20 a into database
00c0: 73 75 72 76 65 79 5f 2a 20 74 61 62 6c 65 0a 23 survey_* table.#
00d0: 20 76 65 72 73 69 6f 6e 3a 20 30 2e 31 0a 23 20 version: 0.1.#
00e0: 73 74 61 74 65 3a 20 61 6c 70 68 61 0a 23 20 63 state: alpha.# c
00f0: 6f 6e 66 69 67 3a 20 2d 0a 23 0a 23 20 52 65 71 onfig: -.#.# Req
0100: 75 69 72 65 73 20 61 20 66 6f 72 6d 20 6c 69 6b uires a form lik
0110: 65 3a 0a 23 0a 23 20 20 20 20 3c 61 63 74 69 6f e:.#.# <actio
0120: 6e 20 74 61 72 67 65 74 3d 22 2f 65 78 74 72 6f n target="/extro
0130: 6f 74 2f 73 75 72 76 65 79 2f 53 41 4d 50 4c 45 ot/survey/SAMPLE
0140: 22 20 6d 65 74 68 6f 64 3d 50 4f 53 54 3e 0a 23 " method=POST>.#
0150: 20 20 20 20 20 20 20 20 3c 69 6e 70 75 74 20 74 <input t
0160: 79 70 65 3d 63 68 65 63 6b 62 6f 78 20 6e 61 6d ype=checkbox nam
0170: 65 3d 71 31 3e 0a 23 20 20 20 20 20 20 20 20 3c e=q1>.# <
0180: 69 6e 70 75 74 20 74 79 70 65 3d 72 61 64 69 6f input type=radio
0190: 20 6e 61 6d 65 3d 71 32 3e 0a 23 20 20 20 20 20 name=q2>.#
01a0: 20 20 20 3c 69 6e 70 75 74 20 74 79 70 65 3d 74 <input type=t
01b0: 65 78 74 20 6e 61 6d 65 3d 71 33 3e 0a 23 0a 23 ext name=q3>.#.#
01c0: 20 41 6e 64 20 77 69 6c 6c 20 74 68 65 6e 20 70 And will then p
01d0: 6f 70 75 6c 61 74 65 20 61 20 60 73 75 72 76 65 opulate a `surve
01e0: 79 5f 53 41 4d 50 4c 45 60 20 74 61 62 6c 65 20 y_SAMPLE` table
01f0: 77 69 74 68 20 4a 53 4f 4e 20 65 6e 74 72 69 65 with JSON entrie
0200: 73 20 28 6e 6f 0a 23 20 66 69 65 6c 64 20 65 78 s (no.# field ex
0210: 70 61 6e 73 69 6f 6e 20 6f 72 20 61 6e 79 74 68 pansion or anyth
0220: 69 6e 67 29 2e 0a 23 0a 0a 0a 23 2d 2d 20 64 61 ing)..#...#-- da
0230: 74 61 62 61 73 65 20 28 3d 3d 20 66 6f 73 73 69 tabase (== fossi
0240: 6c 20 72 65 70 6f 29 0a 66 75 6e 63 74 69 6f 6e l repo).function
0250: 20 64 62 28 24 73 71 6c 3d 22 22 2c 20 24 70 61 db($sql="", $pa
0260: 72 61 6d 73 3d 5b 5d 29 20 7b 0a 20 20 20 20 73 rams=[]) {. s
0270: 74 61 74 69 63 20 24 64 62 3b 0a 20 20 20 20 69 tatic $db;. i
0280: 66 20 28 65 6d 70 74 79 28 24 64 62 29 29 20 7b f (empty($db)) {
0290: 0a 20 20 20 20 20 20 20 20 24 64 62 20 3d 20 6e . $db = n
02a0: 65 77 20 50 44 4f 28 22 73 71 6c 69 74 65 3a 24 ew PDO("sqlite:$
02b0: 5f 53 45 52 56 45 52 5b 46 4f 53 53 49 4c 5f 52 _SERVER[FOSSIL_R
02c0: 45 50 4f 53 49 54 4f 52 59 5d 22 29 3b 0a 20 20 EPOSITORY]");.
02d0: 20 20 7d 0a 20 20 20 20 69 66 20 28 24 70 61 72 }. if ($par
02e0: 61 6d 73 29 20 7b 0a 20 20 20 20 20 20 20 20 24 ams) {. $
02f0: 73 74 6d 74 20 3d 20 24 64 62 2d 3e 70 72 65 70 stmt = $db->prep
0300: 61 72 65 28 24 73 71 6c 29 3b 0a 20 20 20 20 20 are($sql);.
0310: 20 20 20 24 73 74 6d 74 2d 3e 65 78 65 63 75 74 $stmt->execut
0320: 65 28 24 70 61 72 61 6d 73 29 3b 0a 20 20 20 20 e($params);.
0330: 20 20 20 20 72 65 74 75 72 6e 20 24 73 74 6d 74 return $stmt
0340: 2d 3e 66 65 74 63 68 41 6c 6c 28 50 44 4f 3a 3a ->fetchAll(PDO::
0350: 46 45 54 43 48 5f 41 53 53 4f 43 29 3b 0a 20 20 FETCH_ASSOC);.
0360: 20 20 7d 0a 20 20 20 20 65 6c 73 65 20 7b 0a 20 }. else {.
0370: 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 24 64 return $d
0380: 62 2d 3e 71 75 65 72 79 28 24 73 71 6c 29 2d 3e b->query($sql)->
0390: 66 65 74 63 68 41 6c 6c 28 29 3b 0a 20 20 20 20 fetchAll();.
03a0: 7d 0a 7d 0a 0a 0a 23 2d 2d 20 69 6e 73 65 72 74 }.}...#-- insert
03b0: 0a 66 75 6e 63 74 69 6f 6e 20 73 74 6f 72 65 28 .function store(
03c0: 29 20 7b 0a 20 20 20 20 24 74 62 6c 20 3d 20 70 ) {. $tbl = p
03d0: 72 65 67 5f 72 65 70 6c 61 63 65 28 22 2f 5c 57 reg_replace("/\W
03e0: 2b 2f 22 2c 20 22 22 2c 20 62 61 73 65 6e 61 6d +/", "", basenam
03f0: 65 28 24 5f 53 45 52 56 45 52 5b 22 50 41 54 48 e($_SERVER["PATH
0400: 5f 49 4e 46 4f 22 5d 29 29 3b 0a 20 20 20 20 69 _INFO"]));. i
0410: 66 20 28 21 24 74 62 6c 29 20 7b 0a 20 20 20 20 f (!$tbl) {.
0420: 20 20 20 20 64 69 65 28 22 4e 6f 20 74 61 62 6c die("No tabl
0430: 65 20 6e 61 6d 65 20 67 69 76 65 6e 20 2f 73 75 e name given /su
0440: 72 76 65 79 2f 4e 41 4d 45 22 29 3b 0a 20 20 20 rvey/NAME");.
0450: 20 7d 0a 20 20 20 20 65 6c 73 65 20 7b 0a 20 20 }. else {.
0460: 20 20 20 20 20 20 24 6a 73 6f 6e 20 3d 20 6a 73 $json = js
0470: 6f 6e 5f 65 6e 63 6f 64 65 28 24 5f 50 4f 53 54 on_encode($_POST
0480: 29 3b 0a 20 20 20 20 20 20 20 20 64 62 28 22 43 );. db("C
0490: 52 45 41 54 45 20 54 41 42 4c 45 20 49 46 20 4e REATE TABLE IF N
04a0: 4f 54 20 45 58 49 53 54 53 20 60 73 75 72 76 65 OT EXISTS `surve
04b0: 79 5f 24 74 62 6c 60 20 28 73 75 62 6d 69 74 74 y_$tbl` (submitt
04c0: 65 64 20 44 41 54 45 54 49 4d 45 2c 20 72 65 6d ed DATETIME, rem
04d0: 6f 74 65 5f 61 64 64 72 2c 20 66 69 65 6c 64 73 ote_addr, fields
04e0: 20 54 45 58 54 29 22 29 3b 0a 20 20 20 20 20 20 TEXT)");.
04f0: 20 20 64 62 28 22 49 4e 53 45 52 54 20 49 4e 54 db("INSERT INT
0500: 4f 20 60 73 75 72 76 65 79 5f 24 74 62 6c 60 20 O `survey_$tbl`
0510: 28 73 75 62 6d 69 74 74 65 64 2c 20 72 65 6d 6f (submitted, remo
0520: 74 65 5f 61 64 64 72 2c 20 66 69 65 6c 64 73 29 te_addr, fields)
0530: 20 56 41 4c 55 45 53 20 28 4e 4f 57 28 29 2c 20 VALUES (NOW(),
0540: 3f 2c 20 3f 29 22 2c 20 5b 24 5f 53 45 52 56 45 ?, ?)", [$_SERVE
0550: 52 5b 22 52 45 4d 4f 54 45 5f 41 44 44 52 22 5d R["REMOTE_ADDR"]
0560: 2c 20 24 6a 73 6f 6e 5d 29 3b 0a 20 20 20 20 7d , $json]);. }
0570: 0a 7d 0a 0a 23 2d 2d 20 72 75 6e 0a 69 66 20 28 .}..#-- run.if (
0580: 63 6f 75 6e 74 28 24 5f 50 4f 53 54 29 29 20 7b count($_POST)) {
0590: 0a 20 20 20 20 73 74 6f 72 65 28 29 3b 0a 7d 0a . store();.}.
05a0: 65 6c 73 65 20 7b 0a 20 20 20 20 64 69 65 28 22 else {. die("
05b0: 45 6d 70 74 79 20 66 6f 72 6d 22 29 3b 0a 7d 0a Empty form");.}.
05c0: 0a .