tinycm annotate plugins/blog/blog.cgi @ rev 72

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