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

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