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

Improve dashboard and plugin integration
author Christophe Lincoln <pankso@slitaz.org>
date Sat Feb 01 01:28:51 2014 +0100 (2014-02-01)
parents 6400421b49c9
children 03338ebb4d86
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?blog=edit&amp;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&amp;d=${d}">$(gettext "Edit it!")</a>
25 EOT
26 #<a href="$script?blogrm=${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 admin users
48 if check_auth && 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 $blog | sort -r -n | 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 #
84 # Handle GET requests
85 #
87 if [ "$(GET blog)" ]; then
88 case " $(GET blog) " in
89 *\ edit\ *)
90 d="$(GET d)"
91 header
92 html_header
93 user_box
94 if ! check_auth && admin_user; then
95 gettext "You must be admin to create a new Blog post"
96 html_footer && exit 0
97 fi
98 # New post
99 if [ "$d" == "new" ]; then
100 date=$(date '+%Y-%m-%d')
101 last=$(ls $blog | sort -r -n | head -n 1)
102 nb=${last%.txt}
103 d=$(($nb + 1))
104 conf=$(echo -e "\n\nAUTHOR=\"$user\"\nDATE=\"$date\"\n\n====Title====")
105 fi
106 cat << EOT
107 <h2>$(gettext "Blog post"): $d</h2>
109 <div id="edit">
110 <form method="get" action="$script?" name="editor">
111 <input type="hidden" name="blog" value="save" />
112 <input type="hidden" name="d" value="$d" />
113 <textarea name="content">${conf}$(cat "$blog/$d.txt")</textarea>
114 <input type="submit" value="$(gettext "Post content")" />
115 $(gettext "Code Helper:")
116 $(cat lib/jseditor.html)
117 </form>
118 </div>
119 EOT
120 html_footer && exit 0 ;;
122 *\ save\ *)
123 d="$(GET d)"
124 if check_auth && admin_user; then
125 [ -d "$blog" ] || mkdir -p ${blog}
126 # New post ?
127 if [ ! -f "${blog}/${d}.txt" ]; then
128 echo "New Blog post: <a href='$script?blog=$d'>Read it!</a>" \
129 | log_activity
130 fi
131 # Write content to file
132 sed "s/$(echo -en '\r') /\n/g" > ${blog}/${d}.txt << EOT
133 $(GET content)
134 EOT
135 fi
136 header "Location: $script?blog" ;;
138 *)
139 d="Blog posts"
140 count="20"
141 header
142 html_header
143 user_box
144 # Blog tools for admin users
145 if check_auth && admin_user; then
146 blog_tools
147 fi
148 # Exit if plugin is disabled
149 if [ ! -d "$blog" ]; then
150 echo "<p class='error box'>"
151 gettext "Blog plugin is not yet active."; echo "</p>"
152 html_footer && exit 0
153 fi
154 # Single post
155 if [ "$(GET blog)" != "blog" ]; then
156 show_post "$(GET blog)"
157 else
158 show_posts ${count}
159 fi ;;
160 esac
161 html_footer && exit 0
162 fi