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

Tiny edits
author Paul Issott <paul@slitaz.org>
date Thu Feb 06 19:35:03 2014 +0000 (2014-02-06)
parents 03338ebb4d86
children 007a950f9808
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 cat > ${blog}/${p}/post.conf << EOT
190 # TinyCM Blog post configuration
191 AUTHOR="$(GET author)"
192 DATE="$(GET date)"
193 TITLE="$(GET title)"
194 EOT
195 echo "New Blog post: <a href='$script?blog=$p'>Read it!</a>" \
196 | log_activity
197 fi
198 # Write content to file
199 sed "s/$(echo -en '\r') /\n/g" > ${blog}/${p}/post.txt << EOT
200 $(GET content)
201 EOT
202 fi
203 [ -f "${blog}/${p}/post.xml" ] || gen_rss
204 header "Location: $script?blog&p=$p" ;;
206 *\ rm\ *)
207 if check_auth && admin_user; then
208 rm -rf ${blog}/"$(GET p)"
209 fi
210 header "Location: $script?blog" ;;
212 *\ archives\ *)
213 # List all posts with title only
214 d="Blog archives"
215 header
216 html_header
217 user_box
218 blog_tools
219 echo "<h2>Blog archives</h2>"
220 echo "<pre>"
221 for p in $(ls $blog)
222 do
223 . ${blog}/${p}/post.conf
224 echo "\
225 <span class='date'>$DATE :</span> <a href='$script?blog&amp;p=$p'>$TITLE</a>"
226 done
227 echo "</pre>" ;;
229 *)
230 if [ "$(GET blog)" == "rss" ]; then
231 rss && exit 0
232 fi
233 d="Blog posts"
234 count="20"
235 header
236 html_header
237 user_box
238 blog_tools
239 # Exit if plugin is disabled
240 if [ ! -d "$blog" ]; then
241 echo "<p class='error box'>"
242 gettext "Blog plugin is not yet active."; echo "</p>"
243 html_footer && exit 0
244 fi
245 # Single post
246 if [ "$(GET p)" ]; then
247 show_post "$(GET p)"
248 else
249 show_posts ${count}
250 echo "<p><a href='$script?blog=archives'>$(gettext "Blog archives")</a></p>"
251 fi ;;
252 esac
253 html_footer && exit 0
254 fi