tazweb view data/bookmarks.cgi @ rev 149

Fix user bookmarks on first run
author Christophe Lincoln <pankso@slitaz.org>
date Sun Apr 13 02:30:09 2014 +0200 (2014-04-13)
parents c2ec2ff2e813
children e40064ab71b4
line source
1 #!/bin/sh
2 #
3 # TazWeb Bookmarks CGI handler
4 # Copyright (C) 2014 SliTaz GNU/Linux - BSD License
5 #
6 . /usr/lib/slitaz/httphelper.sh
8 script="$SCRIPT_NAME"
9 home="$(GET home)"
10 user="$(basename $home)"
11 config="/home/$user/.config/tazweb"
12 bookmarks="$config/bookmarks.txt"
14 # Security check
15 if [ "$REMOTE_ADDR" != "127.0.0.1" ]; then
16 echo "Security exit" && exit 1
17 fi
19 # HTML 5 header with built-in minimal CSS
20 html_header() {
21 cat << EOT
22 <!DOCTYPE html>
23 <html lang="en">
24 <head>
25 <meta charset="utf-8" />
26 <title>TazWeb - Bookmarks</title>
27 <style type="text/css">
28 body { margin: 2% 10%; } .rm { color: #666; } ul { padding: 0; }
29 .rm:hover { text-decoration: none; color: #B70000; }
30 h1 { color: #666; border-bottom: 4px solid #666; }
31 a { text-decoration: none; } a:hover { text-decoration: underline; }
32 li { list-style-type: none; color: #666; line-height: 1.4em; padding: 0; }
33 footer { font-size: 80%; border-top: 2px solid #666; padding: 5px 0; }
34 textarea { width: 100%; height: 240px; font-size: 98%; }
35 </style>
36 </head>
37 <body>
38 <section id="content">
40 EOT
41 }
43 # HTML 5 footer
44 html_footer() {
45 cat << EOT
47 </section>
49 <footer>
50 <a href="$script?home=$home">Bookmarks</a>
51 - <a href="$script?raw&amp;home=$home">Raw</a>
52 - <a href="$script?edit&amp;home=$home">Edit</a>
53 </footer>
55 </body>
56 </html>
57 EOT
58 }
60 # Handle GET actions: continue or exit
62 case " $(GET) " in
64 *\ edit\ *)
65 header
66 html_header
67 cat << EOT
68 <h1>Bookmarks Edit</h1>
69 <form method="get" action="$script" name="edit">
70 <input type="hidden" name="save" />
71 <input type="hidden" name="home" value="$home" />
72 <textarea name="content">$(cat "$bookmarks")</textarea>
73 <p><input type="submit" value="$(gettext "Save bookmarks")" /></p>
74 </form>
75 EOT
76 html_footer && exit 0 ;;
78 *\ save\ *)
79 sed "s/$(echo -en '\r') /\n/g" > ${bookmarks} << EOT
80 $(GET content)
81 EOT
82 ;;
84 *\ raw\ *)
85 # View bookmarks file
86 header
87 html_header
88 echo '<h1>Raw Bookmarks</h1>'
89 echo "<pre>"
90 IFS="|"
91 cat ${bookmarks} | cat ${bookmarks} | while read title url null
92 do
93 echo "$title | <a href='$url'>$url</a>"
94 done
95 unset IFS
96 echo "</pre>"
97 html_footer && exit 0 ;;
99 *\ rm\ *)
100 # Remove a bookmark item and continue
101 url=$(GET rm)
102 [ "$url" ] || continue
103 sed -i s"#.*${url}.*##" ${bookmarks}
104 sed -i "/^$/"d ${bookmarks} ;;
106 esac
108 # Show all bookmarks
109 header
110 html_header
111 echo '<h1>TazWeb Bookmarks</h1>'
112 echo '<ul>'
113 IFS="|"
114 cat ${bookmarks} | while read title url null
115 do
116 cat << EOT
117 <li><a class="rm" href="?rm=$url&amp;home=$home">&otimes;<a/>
118 <a href="${url}">${title}<a/></li>
119 EOT
120 done
121 unset IFS
122 echo '</ul>'
123 html_footer
125 exit 0