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

Small fix to blog and ls
author Christophe Lincoln <pankso@slitaz.org>
date Sun Feb 16 23:36:43 2014 +0100 (2014-02-16)
parents d73353aed02d
children c9ab61b3774a
line source
1 #!/bin/sh
2 #
3 # TinyCM Plugin - Blog
4 #
5 . /usr/lib/slitaz/httphelper
7 blog="$tiny/$content/blog"
9 # # Blog tools for admin users only
10 blog_tools() {
11 if check_auth && admin_user; then
12 cat << EOT
13 <div id="tools">
14 <a href="$script?blog">$(gettext "Blog")</a>
15 <a href="$script?blog=edit&amp;p=new">$(gettext "New post")</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 #
114 # Index main page can display the latest Blog posts
115 #
116 if fgrep -q '[BLOG]' $tiny/$wiki/index.txt && [ ! "$(GET)" ]; then
117 d="Blog posts"
118 index="blog"
119 header
120 html_header
121 user_box
122 # Post tools for auth users
123 if admin_user; then
124 blog_tools
125 fi
126 show_posts 5
127 echo "<p><a href='$script?blog'>$(gettext "More blog posts")</a></p>"
128 html_footer && exit 0
129 fi
131 #
132 # Handle GET requests
133 #
135 if [ "$(GET blog)" ]; then
136 case " $(GET blog) " in
137 *\ edit\ *)
138 d="Editing: $(GET p)"
139 p="$(GET p)"
140 header
141 html_header
142 user_box
143 if ! check_auth && admin_user; then
144 gettext "You must be admin to create a new Blog post"
145 html_footer && exit 0
146 fi
147 blog_tools
148 # New post
149 if [ "$p" == "new" ]; then
150 last=$(ls $blog | sort -r -n | head -n 1)
151 p=$(($last + 1))
152 AUTHOR="$user"
153 DATE=$(date '+%Y-%m-%d')
154 else
155 . ${blog}/${p}/post.conf
156 fi
157 cat << EOT
158 <h2>$(gettext "Blog post"): $p</h2>
160 <div id="edit">
161 <form method="get" action="$script?" name="editor">
162 <input type="text" name="title" value="$TITLE" placeholder="Title" />
163 <input type="hidden" name="blog" value="save" />
164 <input type="hidden" name="p" value="$p" />
165 <textarea name="content">$(cat "$blog/$p/post.txt")</textarea>
166 <div>
167 <input type="submit" value="$(gettext "Post content")" />
168 <input style="width: 20%;" type="text"
169 name="author" value="$AUTHOR" />
170 <input style="width: 20%; display: inline;" type="text"
171 name="date" value="$DATE" />
172 </div>
174 <p>
175 $(gettext "Code Helper:")
176 $(cat lib/jseditor.html)
177 </p>
178 </form>
179 </div>
180 EOT
181 html_footer && exit 0 ;;
183 *\ save\ *)
184 p="$(GET p)"
185 if check_auth && admin_user; then
186 [ -d "$blog/$p" ] || mkdir -p ${blog}/${p}
187 # New post ?
188 if [ ! -f "${blog}/${p}/post.txt" ]; then
189 echo "New Blog post: <a href='$script?blog=$p'>Read it!</a>" \
190 | log_activity
191 fi
192 # Write config file
193 cat > ${blog}/${p}/post.conf << EOT
194 # TinyCM Blog post configuration
195 AUTHOR="$(GET author)"
196 DATE="$(GET date)"
197 TITLE="$(GET title)"
198 EOT
199 # Write content to file
200 sed "s/$(echo -en '\r') /\n/g" > ${blog}/${p}/post.txt << EOT
201 $(GET content)
202 EOT
203 fi
204 [ -f "${blog}/${p}/post.xml" ] || gen_rss
205 header "Location: $script?blog&p=$p" ;;
207 *\ rm\ *)
208 if check_auth && admin_user; then
209 rm -rf ${blog}/"$(GET p)"
210 fi
211 header "Location: $script?blog" ;;
213 *\ archives\ *)
214 # List all posts with title only
215 d="Blog archives"
216 header
217 html_header
218 user_box
219 blog_tools
220 echo "<h2>Blog archives</h2>"
221 echo "<pre>"
222 for p in $(ls $blog)
223 do
224 . ${blog}/${p}/post.conf
225 echo "\
226 <span class='date'>$DATE :</span> <a href='$script?blog&amp;p=$p'>$TITLE</a>"
227 done
228 echo "</pre>" ;;
230 *)
231 if [ "$(GET blog)" == "rss" ]; then
232 rss && exit 0
233 fi
234 d="Blog posts"
235 count="20"
236 header
237 html_header
238 user_box
239 blog_tools
240 # Exit if plugin is disabled
241 if [ ! -d "$blog" ]; then
242 echo "<p class='error box'>"
243 gettext "Blog plugin is not yet active."; echo "</p>"
244 html_footer && exit 0
245 fi
246 # Single post
247 if [ "$(GET p)" ]; then
248 show_post "$(GET p)"
249 else
250 show_posts ${count}
251 echo "<p><a href='$script?blog=archives'>$(gettext "Blog archives")</a></p>"
252 fi ;;
253 esac
254 html_footer && exit 0
255 fi