tinycm view plugins/blog/blog.cgi @ rev 30
only admin can post on the blog
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Mon Jan 06 15:15:38 2014 +0000 (2014-01-06) |
parents | 09f5185ff6e0 |
children | 9d1c1115562a |
line source
1 #!/bin/sh
2 #
3 # TinyCM Plugin - Blog
4 #
5 . /usr/lib/slitaz/httphelper
7 blog="$tiny/$content/blog"
9 # Blog tools
10 blog_tools() {
11 cat << EOT
12 <div id="tools">
13 <a href="$script?blogedit&d=new">$(gettext "New post")</a>
14 <a href="$script?dashboard">Dashboard</a>
15 $([ "$index" == "blog" ] && echo "<a href='$script?d=index'>Index</a>")
16 $([ "$HG" == "yes" ] && echo "<a href='$script?hg'>Hg Log</a>")
17 </div>
18 EOT
19 }
21 # Post tools
22 post_tools() {
23 cat << EOT
24 - <a href="$script?blogedit&d=${d}">$(gettext "Edit it!")</a>
25 EOT
26 #<a href="$script?blogrm&d=${d}">$(gettext "Remove")</a>
27 }
29 # Display blog post: show_posts nb
30 show_post() {
31 d=${1%.txt}
32 date=$(fgrep 'DATE=' ${blog}/${d}.txt | cut -d '"' -f 2)
33 # Get post author
34 author=$(fgrep 'AUTHOR=' ${blog}/${d}.txt | cut -d '"' -f 2)
35 if [ -f "${PEOPLE}/${author}/account.conf" ]; then
36 . ${PEOPLE}/${author}/account.conf
37 else
38 echo ERROR: ${PEOPLE}/${author}/account.conf
39 fi
40 echo "<div class=\"blogpost\">"
41 cat ${blog}/${d}.txt | sed -e '/AUTHOR=/'d -e '/DATE=/'d | wiki_parser
42 cat << EOT
43 <div class="post-tools">
44 <a href="$script?user=$USER">$(get_gravatar $MAIL 24)</a>
45 <span class="date">$date</span>
46 EOT
47 # Post tools for auth users
48 if admin_user; then
49 post_tools
50 echo "</div>"
51 else
52 echo "</div>"
53 fi
54 echo "</div>"
55 }
57 # Display blog post: show_posts count
58 show_posts() {
59 for p in $(ls -r $blog | head -n $1)
60 do
61 show_post ${p}
62 done
63 }
65 #
66 # Index main page can display the lastest Blog posts
67 #
68 if fgrep -q '[BLOG]' $tiny/$wiki/index.txt && [ ! "$(GET)" ]; then
69 d="Blog posts"
70 index="blog"
71 header
72 html_header
73 user_box
74 # Post tools for auth users
75 if admin_user; then
76 blog_tools
77 fi
78 show_posts 5
79 echo "<p><a href='$script?blog'>$(gettext "More blog posts")</a></p>"
80 html_footer && exit 0
81 fi
83 case " $(GET) " in
84 *\ blogedit\ *)
85 d="$(GET d)"
86 header
87 html_header
88 user_box
89 # Blog tools for auth users
90 if ! admin_user; then
91 gettext "You must be admin to create a new Blog post"
92 html_footer && exit 0
93 fi
94 # New post
95 if [ "$d" == "new" ]; then
96 date=$(date '+%Y-%M-%d')
97 last=$(ls -r $blog | head -n 1)
98 nb=${last%.txt}
99 d=$(($nb + 1))
100 conf=$(echo -e "\n\nAUTHOR=\"$user\"\nDATE=\"$date\"\n\n====Title====")
101 fi
102 cat << EOT
103 <h2>$(gettext "Blog post"): $d</h2>
105 <div id="edit">
106 <form method="get" action="$script" name="editor">
107 <input type="hidden" name="blogsave" value="$d" />
108 <textarea name="content">$conf $(cat "$blog/$d.txt")</textarea>
109 <input type="submit" value="$(gettext "Post content")" />
110 $(gettext "Code Helper:")
111 $(cat lib/jseditor.html)
112 </form>
113 </div>
114 EOT
115 html_footer && exit 0 ;;
117 *\ blogsave\ *)
118 d="$(GET blogsave)"
119 if admin_user; then
120 [ -d "$blog" ] || mkdir -p ${blog}
121 # New post ?
122 if [ ! -f "${blog}/${d}.txt" ]; then
123 echo "New Blog post: <a href='$script?blog=$d'>Read it!</a>" \
124 | log_activity
125 fi
126 # Write content to file
127 sed "s/$(echo -en '\r') /\n/g" > ${blog}/${d}.txt << EOT
128 $(GET content)
129 EOT
130 fi
131 header "Location: $script?blog" ;;
133 *\ blog\ *)
134 d="Blog posts"
135 count="20"
136 header
137 html_header
138 user_box
139 #echo "<h2>$(gettext "Latest blog posts")</h2>"
140 # Blog tools for auth users
141 if admin_user; then
142 blog_tools
143 fi
144 # Exit if plugin is disabled
145 if [ ! -d "$blog" ]; then
146 echo "<p class='error box'>"
147 gettext "Blog plugin is not yet active."; echo "</p>"
148 html_footer && exit 0
149 fi
150 # Single post
151 if [ "$(GET blog)" != "blog" ]; then
152 show_post "$(GET blog)"
153 else
154 show_posts ${count}
155 fi
156 html_footer
157 exit 0 ;;
158 esac