tinycm view plugins/blog/blog.cgi @ rev 121

Remove ashism ==
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 26 12:26:25 2019 +0100 (2019-02-26)
parents 798212b3ce40
children
line source
1 #!/bin/sh
2 #
3 # TinyCM Plugin - Blog
4 #
6 blog="$tiny/$content/blog"
8 # # Blog tools for admin users only
9 blog_tools() {
10 if check_auth && admin_user; then
11 cat << EOT
12 <div id="tools">
13 <a href="$script?blog">$(gettext "Blog")</a>
14 <a href="$script?blog=edit&amp;p=new">$(gettext "New post")</a>
15 <a href="$script?blog=archives">$(gettext "Archives")</a>
16 <a href="$script?dashboard">Dashboard</a>
17 $([ "$index" = "blog" ] && echo "<a href='$script?d=index'>Index</a>")
18 $([ "$HG" = "yes" ] && echo "<a href='$script?hg'>Hg Log</a>")
19 </div>
20 EOT
21 fi
22 }
24 # Post tools
25 post_tools() {
26 cat << EOT
27 - <a href="$script?blog=edit&amp;p=${p}">$(gettext "Edit it!")</a>
28 || <a href="$script?blog=rm&amp;p=${p}">$(gettext "Remove")</a>
29 EOT
30 }
32 # Create a XML feed for a new post
33 gen_rss() {
34 pubdate=$(date "+%a, %d %b %Y %X")
35 desc="$(cat ${blog}/${p}/post.txt | grep ^[A-Za-z] | head -n 4)"
36 . ${blog}/${p}/post.conf
37 cat > ${blog}/${p}/post.xml << EOT
38 <item>
39 <title>$TITLE</title>
40 <link>http://${SERVER_NAME}?blog&amp;p=$p</link>
41 <guid>blog-$p</guid>
42 <pubDate>$pubdate</pubDate>
43 <description>$desc</description>
44 </item>
45 EOT
46 }
48 # RSS Feed
49 rss() {
50 pubdate=$(date "+%a, %d %b %Y %X")
51 cat << EOT
52 Content-Type: text/xml
54 <?xml version="1.0" encoding="utf-8" ?>
55 <rss version="2.0">
56 <channel>
57 <title>TinyCM RSS</title>
58 <description>The Blog feed</description>
59 <link>http://${SERVER_NAME}</link>
60 <lastBuildDate>$pubdate GMT</lastBuildDate>
61 <pubDate>$pubdate GMT</pubDate>
62 EOT
63 for p in $(ls $blog | sort -r -n | head -n 8)
64 do
65 cat $blog/$p/post.xml
66 done
67 cat << EOT
68 </channel>
69 </rss>
70 EOT
71 }
73 # Display blog post: show_posts nb
74 show_post() {
75 [ -f "$blog/$1/post.conf" ] || return 1
76 p="$1"
77 . ${blog}/${p}/post.conf
78 d="$TITLE"
80 # Author info
81 if [ -f "${PEOPLE}/${AUTHOR}/account.conf" ]; then
82 . ${PEOPLE}/${AUTHOR}/account.conf
83 else
84 echo "ERROR: ${PEOPLE}/${AUTHOR}/account.conf"
85 fi
87 echo "<h2>$TITLE</h2>"
88 echo "<div class='blogpost'>"
89 cat ${blog}/${p}/post.txt | wiki_parser
90 cat << EOT
91 <div class="post-tools">
92 <a href="$script?user=$USER">$(get_gravatar $MAIL 24)</a>
93 <span class="date">$DATE</span>
94 EOT
95 # Post tools for admin users
96 if check_auth && admin_user; then
97 post_tools
98 echo "</div>"
99 else
100 echo "</div>"
101 fi
102 echo "</div>"
103 }
105 # Display blog post: show_posts count
106 show_posts() {
107 for p in $(ls $blog | sort -r -n | head -n $1)
108 do
109 show_post ${p}
110 done
111 }
113 # RSS feed requested
114 if [ "$(GET blog)" = "rss" ]; then
115 rss && exit 0
116 fi
118 #
119 # Index main page can display the latest Blog posts
120 #
121 if fgrep -q '[BLOG]' $tiny/$wiki/index.txt && [ ! "$(GET)" ]; then
122 d="Blog posts"
123 index="blog"
124 header
125 html_header
126 user_box
127 # Post tools for auth users
128 if admin_user; then
129 blog_tools
130 fi
131 show_posts 5
132 echo "<p><a href='$script?blog'>$(gettext "More blog posts")</a></p>"
133 html_footer && exit 0
134 fi
136 #
137 # Handle GET requests
138 #
141 case " $(GET blog) " in
142 *\ edit\ *)
143 d="Editing: $(GET p)"
144 p="$(GET p)"
145 header
146 html_header
147 user_box
148 if ! check_auth && admin_user; then
149 gettext "You must be admin to create a new Blog post"
150 html_footer && exit 0
151 fi
152 blog_tools
153 # New post
154 if [ "$p" = "new" ]; then
155 last=$(ls $blog | sort -r -n | head -n 1)
156 p=$(($last + 1))
157 AUTHOR="$user"
158 DATE=$(date '+%Y-%m-%d')
159 else
160 . ${blog}/${p}/post.conf
161 fi
162 cat << EOT
163 <h2>$(gettext "Blog post"): $p</h2>
165 <div id="edit">
166 <form method="get" action="$script?" name="editor">
167 <input type="text" name="title" value="$TITLE" placeholder="Title" />
168 <input type="hidden" name="blog" value="save" />
169 <input type="hidden" name="p" value="$p" />
170 <textarea name="content">$(cat "$blog/$p/post.txt")</textarea>
171 <div>
172 <input type="submit" value="$(gettext "Post content")" />
173 <input style="width: 20%;" type="text"
174 name="author" value="$AUTHOR" />
175 <input style="width: 20%; display: inline;" type="text"
176 name="date" value="$DATE" />
177 </div>
179 <p>
180 $(gettext "Code Helper:")
181 $(cat lib/jseditor.html)
182 </p>
183 </form>
184 </div>
185 EOT
186 html_footer && exit 0 ;;
188 *\ save\ *)
189 p="$(GET p)"
190 if check_auth && admin_user; then
191 [ -d "$blog/$p" ] || mkdir -p ${blog}/${p}
192 # New post ?
193 if [ ! -f "${blog}/${p}/post.txt" ]; then
194 echo "New Blog post: <a href='$script?blog&amp;p=$p'>Read it!</a>" \
195 | log_activity
196 fi
197 # Write config file
198 cat > ${blog}/${p}/post.conf << EOT
199 # TinyCM Blog post configuration
200 AUTHOR="$(GET author)"
201 DATE="$(GET date)"
202 TITLE="$(GET title)"
203 EOT
204 # Write content to file
205 sed "s/$(echo -en '\r') /\n/g" > ${blog}/${p}/post.txt << EOT
206 $(GET content)
207 EOT
208 fi
209 [ -f "${blog}/${p}/post.xml" ] || gen_rss
210 header "Location: $script?blog&p=$p" ;;
212 *\ rm\ *)
213 if check_auth && admin_user; then
214 rm -rf ${blog}/"$(GET p)"
215 fi
216 header "Location: $script?blog" ;;
218 *\ archives\ *)
219 # List all posts with title only
220 d="Blog archives"
221 header
222 html_header
223 user_box
224 blog_tools
225 cat << EOT
226 <h2>Blog archives</h2>
227 <div id="plugins">
228 <table>
229 <thead>
230 <td>$(gettext "Date")</td>
231 <td>$(gettext "Title")</td>
232 </thead>
233 EOT
234 for p in $(ls $blog | sort -nr)
235 do
236 . ${blog}/${p}/post.conf
237 cat << EOT
238 <tr>
239 <td>$DATE</td>
240 <td><a href='$script?blog&amp;p=$p'>$TITLE</a></td>
241 </tr>
242 EOT
243 done
244 echo "</table></div>"
245 html_footer && exit 0 ;;
247 *\ blog\ *)
248 d="Blog posts"
249 count="10"
250 header
251 html_header
252 user_box
253 blog_tools
255 # Exit if plugin is disabled
256 if [ ! -d "$blog" ]; then
257 echo "<p class='error box'>"
258 gettext "Blog plugin is not yet active."; echo "</p>"
259 html_footer && exit 0
260 fi
262 # Single post
263 if [ "$(GET p)" ]; then
264 show_post "$(GET p)"
265 else
266 show_posts ${count}
267 echo "<p><a href='$script?blog=archives'>$(gettext "Blog archives")</a></p>"
268 fi
270 html_footer && exit 0 ;;
271 esac