tinycm annotate index.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 a85e81a401ee
children ea608c745b41
rev   line source
pankso@5 1 #!/bin/sh
pankso@5 2 #
pankso@5 3 # TinyCM - Small, fast and elegent CGI/SHell Content Manager
pankso@5 4 #
pankso@15 5 # Copyright (C) 2012-2014 SliTaz GNU/Linux - BSD License
pankso@5 6 #
pankso@5 7 . /usr/lib/slitaz/httphelper
pankso@5 8
paul@8 9 # Let's have a peer site config file with a .cgi extension so content
pankso@5 10 # is secure even if left in a web server directory.
pankso@57 11 . ./config.cgi
pankso@5 12
pankso@5 13 tiny="$PWD"
pankso@5 14 content="content"
pankso@5 15 wiki="$content/wiki"
pankso@5 16 index="index"
pankso@5 17 cache="cache"
pankso@5 18 plugins="plugins"
pankso@5 19 tmp="/tmp/tinycm"
pankso@5 20 sessions="$tmp/sessions"
pankso@15 21 script="$SCRIPT_NAME"
pankso@28 22 activity="$cache/log/activity.log"
pankso@5 23
pankso@5 24 # Content negotiation for Gettext
pankso@5 25 IFS=","
pankso@5 26 for lang in $HTTP_ACCEPT_LANGUAGE
pankso@5 27 do
pankso@5 28 lang=${lang%;*} lang=${lang# } lang=${lang%-*}
pankso@5 29 case "$lang" in
pankso@57 30 en) lang="C" && break ;;
pankso@57 31 fr) lang="fr_FR" && break ;;
pankso@57 32 pt) lang="pt_BR" && break ;;
pankso@57 33 ru) lang="ru_RU" && break ;;
pankso@5 34 esac
pankso@5 35 done
pankso@5 36 unset IFS
pankso@5 37 export LANG=$lang LC_ALL=$lang
pankso@5 38
pankso@55 39 # Internationalization
pankso@55 40 . /usr/bin/gettext.sh
pankso@55 41 TEXTDOMAIN='tinycm'
pankso@55 42 export TEXTDOMAIN
pankso@55 43
pankso@5 44 #
pankso@5 45 # Functions
pankso@5 46 #
pankso@5 47
paul@8 48 # Used by edit to display language name and the language box. This is
pankso@5 49 # for CM content not gettext support.
pankso@5 50 get_lang() {
pankso@57 51 dlang=$(echo $d | cut -d "/" -f 1)
pankso@57 52 doc=${d#$dlang/}
pankso@5 53 echo '<div id="lang">'
pankso@5 54 for l in $LANGUAGES
pankso@5 55 do
pankso@57 56 case $dlang in
pankso@5 57 en) i18n="English" ;;
pankso@5 58 fr) i18n="Français" ;;
pankso@5 59 pt) i18n="Português" ;;
pankso@5 60 ru) i18n="Русский" ;;
pankso@5 61 *) i18n="*" ;;
pankso@5 62 esac
pankso@5 63 echo "<a href='?d=$l/$doc'>$l</a>"
pankso@5 64 done
pankso@5 65 echo '</div>'
pankso@5 66 }
pankso@5 67
pankso@5 68 # HTML 5 header.
pankso@5 69 html_header() {
pankso@5 70 if [ -f "$tiny/lib/header.html" ]; then
pankso@5 71 cat $tiny/lib/header.html | sed -e s!'%TITLE%'!"$TITLE - $d"!g
pankso@5 72 else
pankso@5 73 cat << EOT
pankso@5 74 <!DOCTYPE html>
pankso@5 75 <html xmlns="http://www.w3.org/1999/xhtml">
pankso@5 76 <head>
pankso@5 77 <title>$TITLE</title>
pankso@5 78 <meta charset="utf-8" />
pankso@5 79 <style type="text/css">body { margin: 40px 120px; }</style>
pankso@5 80 </head>
pankso@5 81 <body>
pankso@5 82 <!-- Content -->
pankso@5 83 <div id="content">
pankso@5 84 EOT
pankso@5 85 fi
pankso@5 86 }
pankso@5 87
pankso@5 88 # HTML 5 footer.
pankso@5 89 html_footer() {
pankso@5 90 if [ -f "$tiny/lib/footer.html" ]; then
pankso@5 91 cat $tiny/lib/footer.html
pankso@5 92 else
pankso@5 93 cat << EOT
pankso@5 94
pankso@5 95 <!-- End content -->
pankso@5 96 </div>
pankso@5 97
pankso@5 98 <div id="footer">&hearts;</div>
pankso@5 99
pankso@5 100 </body>
pankso@5 101 </html>
pankso@5 102 EOT
pankso@5 103 fi
pankso@5 104 }
pankso@5 105
pankso@5 106 # Default index if missing
pankso@5 107 default_index() {
pankso@11 108 mkdir -p "$wiki"
pankso@5 109 cat > $wiki/$index.txt << EOT
pankso@5 110 ==== Welcome ====
pankso@5 111
pankso@36 112 <p>
pankso@32 113 This is the default index page of your TinyCM, you can login then start to
pankso@32 114 edit and adding some content. You can read the help about text formating
pankso@48 115 and functions: [Help page|en/help]
pankso@36 116 </p>
pankso@5 117
pankso@5 118 EOT
pankso@5 119 }
pankso@5 120
pankso@28 121 # Log main activity.
pankso@28 122 log_activity() {
pankso@28 123 [ -d "$cache/log" ] || mkdir -p ${cache}/log
pankso@28 124 #gravatar="$(get_gravatar $MAIL 24)"
pankso@28 125 grep ^[A-Z] | \
pankso@28 126 sed s"#^[A-Z]\([^']*\)#$user|$(date '+%Y-%m-%d')|\0#" \
pankso@28 127 >> $cache/log/activity.log
pankso@28 128 }
pankso@28 129
pankso@5 130 # Log documents activity.
pankso@5 131 log() {
pankso@5 132 grep ^[A-Z] | \
pankso@5 133 sed s"#^[A-Z]\([^']*\)#$(date '+%Y-%m-%d %H:%M') : \0#" \
pankso@5 134 >> $cache/$d/activity.log
pankso@5 135 }
pankso@5 136
pankso@5 137 # Check if user is auth
pankso@5 138 check_auth() {
pankso@5 139 auth="$(COOKIE auth)"
pankso@5 140 user="$(echo $auth | cut -d ":" -f 1)"
pankso@5 141 md5cookie="$(echo $auth | cut -d ":" -f 2)"
pankso@5 142 [ -f "$sessions/$user" ] && md5session="$(cat $sessions/$user)"
pankso@5 143 if [ "$md5cookie" == "$md5session" ] && [ "$auth" ]; then
pankso@5 144 . $PEOPLE/$user/account.conf
pankso@5 145 return 0
pankso@5 146 else
pankso@5 147 return 1
pankso@5 148 fi
pankso@5 149 }
pankso@5 150
pankso@28 151 # Check if user is admin
pankso@28 152 admin_user() {
pankso@28 153 fgrep -q 'ADMIN_USER="yes"' ${PEOPLE}/${user}/account.conf
pankso@28 154 }
pankso@28 155
pankso@5 156 # Authentified or not
pankso@5 157 user_box() {
pankso@5 158 if check_auth; then
pankso@5 159 cat << EOT
pankso@5 160
pankso@5 161 <div id="user">
pankso@5 162 <a href="$script?user=$user">$(get_gravatar $MAIL 20)</a>
pankso@5 163 <a href="$script?logout">Logout</a>
pankso@5 164 </div>
pankso@5 165
pankso@5 166 EOT
pankso@5 167 else
pankso@5 168 cat << EOT
pankso@5 169
pankso@5 170 <div id="user">
pankso@5 171 <a href="$script?login"><img src="images/avatar.png" alt="[ User ]" /></a>
pankso@5 172 <a href="$script?login">Login</a>
pankso@5 173 </div>
pankso@5 174
pankso@5 175 EOT
pankso@5 176 fi
pankso@5 177 cat << EOT
pankso@5 178 <!--
pankso@5 179 <div id="search">
pankso@5 180 <form method="get" action="$script">
pankso@5 181 <input type="text" name="search" placeholder="$(gettext "Search")" />
pankso@5 182 </form>
pankso@5 183 </div>
pankso@5 184 -->
pankso@5 185 EOT
pankso@5 186 }
pankso@5 187
pankso@5 188 # Link for online signup if enabled.
pankso@5 189 online_signup() {
pankso@5 190 if [ "$ONLINE_SIGNUP" == "yes" ]; then
pankso@5 191 echo -n "<p><a href='$script?signup'>"
pankso@5 192 gettext "Create a new account"
pankso@5 193 echo '</a></p>'
pankso@5 194 fi
pankso@5 195 }
pankso@5 196
pankso@5 197 # Login page
pankso@5 198 login_page() {
pankso@5 199 cat << EOT
pankso@5 200 <h2>$(gettext "Login")</h2>
pankso@5 201
pankso@5 202 <div id="account-info">
paul@8 203 $(gettext "No account yet or trouble with you account? Please send
pankso@5 204 a request to $ADMIN_MAIL with your real name, user name, mail and password.")
pankso@5 205 $(online_signup)
pankso@5 206 </div>
pankso@5 207
pankso@5 208 <div id="login">
pankso@5 209 <form method="post" action="$script">
pankso@5 210 <input type="text" name="auth" placeholder="$(gettext "User name")" />
pankso@5 211 <input type="password" name="pass" placeholder="$(gettext "Password")" />
pankso@5 212 <div>
pankso@5 213 <input type="submit" value="Login" /> $error
pankso@5 214 </div>
pankso@5 215 </form>
pankso@5 216 </div>
pankso@5 217
pankso@5 218 <div style="clear: both;"></div>
pankso@5 219 EOT
pankso@5 220 }
pankso@5 221
pankso@5 222 # Signup page
pankso@5 223 signup_page() {
pankso@5 224 cat << EOT
pankso@5 225
pankso@5 226 <div id="signup">
pankso@5 227 <form method="post" name="signup" action="$script" onsubmit="return checkSignup();">
pankso@5 228 <input type="hidden" name="signup" value="new" />
pankso@5 229 <input type="text" name="name" placeholder="$(gettext "Real name")" />
pankso@5 230 <input type="text" name="user" placeholder="$(gettext "User name")" />
pankso@5 231 <input type="text" name="mail" placeholder="$(gettext "Email")" />
pankso@5 232 <input type="password" name="pass" placeholder="$(gettext "Password")" />
pankso@5 233 <div>
pankso@5 234 <input type="submit" value="$(gettext "Create new account")" />
pankso@5 235 </div>
pankso@5 236 </form>
pankso@5 237 </div>
pankso@5 238
pankso@5 239 EOT
pankso@5 240 }
pankso@5 241
pankso@5 242 # Create a new user in AUTH_FILE and PEOPLE
pankso@5 243 new_user_config() {
pankso@20 244 if [ ! -f "$AUTH_FILE" ];then
pankso@20 245 touch $(DESTDIR)$(LOGIN)/auth/people
pankso@20 246 chmod 0600 $(DESTDIR)$(LOGIN)/auth/people
pankso@20 247 fi
pankso@5 248 key=$(echo -n "$user:$mail:$pass" | md5sum | awk '{print $1}')
pankso@5 249 echo "$user:$pass" >> $AUTH_FILE
pankso@5 250 mkdir -p $PEOPLE/$user/
pankso@5 251 cat > $PEOPLE/$user/account.conf << EOT
pankso@5 252 # SliTaz user configuration
pankso@5 253 #
pankso@5 254
pankso@5 255 NAME="$name"
pankso@5 256 USER="$user"
pankso@5 257 MAIL="$mail"
pankso@5 258 KEY="$key"
pankso@5 259
pankso@5 260 EOT
pankso@5 261 chmod 0600 $PEOPLE/$user/account.conf
pankso@28 262 # First created user is admin
pankso@28 263 if [ $(ls ${PEOPLE} | wc -l) == "1" ]; then
pankso@28 264 echo 'ADMIN_USER="yes"' >> $PEOPLE/$user/account.conf
pankso@28 265 fi
pankso@5 266 }
pankso@5 267
pankso@5 268 # Display user public profile.
pankso@5 269 public_people() {
pankso@36 270 echo "</pre>"
pankso@23 271 # Display personnal user profile
pankso@23 272 if [ -f "$PEOPLE/$USER/profile.txt" ]; then
pankso@23 273 cat $PEOPLE/$USER/profile.txt | wiki_parser
pankso@23 274 fi
pankso@5 275 }
pankso@5 276
pankso@5 277 # Display authentified user profile. TODO: change password
pankso@5 278 auth_people() {
pankso@5 279 cat << EOT
pankso@5 280 Email : $MAIL
pankso@5 281 Secure key : $KEY
pankso@5 282 </pre>
pankso@5 283 EOT
pankso@23 284 # Each user can have personal profile page
pankso@23 285 if [ -f "$PEOPLE/$USER/profile.txt" ]; then
pankso@23 286 cat $PEOPLE/$USER/profile.txt | wiki_parser
pankso@23 287 cat << EOT
pankso@23 288 <div id="tools">
pankso@23 289 <a href="$script?edit=profile">$(gettext "Edit profile")</a>
pankso@59 290 <a href="$script?dashboard">Dashboard</a>
pankso@23 291 </div>
pankso@23 292 EOT
pankso@23 293 else
pankso@23 294 cat << EOT
pankso@23 295 <div id="tools">
pankso@23 296 <a href="$script?edit=profile">$(gettext "Create a profile page")</a>
pankso@59 297 <a href="$script?dashboard">Dashboard</a>
pankso@23 298 </div>
pankso@23 299 EOT
pankso@23 300 fi
pankso@5 301 }
pankso@5 302
paul@8 303 # The CM style parser. Just a title, simple text formating and internal
paul@8 304 # links, as well as images and use HTML for other stuff. Keep it fast!
pankso@5 305 # To make TinyCM as easy as possible we have a small HTML editor/helper
pankso@5 306 # written in Javascript
pankso@5 307 wiki_parser() {
pankso@5 308 doc="[0-9a-zA-Z\.\#/~\_%=\?\&,\+\:@;!\(\)\*\$'\-]*"
pankso@5 309 sed \
pankso@5 310 -e s"#====\([^']*\)====#<h2>\1</h2>#"g \
pankso@5 311 -e s"#===\([^']*\)===#<h3>\1</h3>#"g \
pankso@5 312 -e s"#==\([^']*\)==#<h4>\1</h4>#"g \
pankso@13 313 -e s"#\*\*\([^']*\)\*\*#<b>\1</b>#"g \
pankso@5 314 -e s"#''\([^']*\)''#<em>\1</em>#"g \
pankso@5 315 -e s"#__\([^']*\)__#<u>\1</u>#"g \
pankso@5 316 -e s"#\[\([^]]*\)|\($doc\)\]#<a href='$script?d=\2'>\1</a>#"g \
pankso@17 317 -e s"#\[\([^]]*\)!\($doc\)\]#<a href='\2'>\1</a>#"g \
pankso@17 318 -e s"#\[\(http://*[^]]*.png\)\]#<img src='\1' />#"g \
pankso@17 319 -e s"#\[\([^]]*.png\)\]#<img src='content/cloud/\1' />#"g
pankso@5 320 }
pankso@5 321
pankso@5 322 link_user() {
pankso@15 323 echo "<a href='$(basename $script)?user=$user'>$user</a>"
pankso@5 324 }
pankso@5 325
pankso@5 326 # Save a document. Do we need more than 1 backup and diff ?
pankso@5 327 save_document() {
pankso@5 328 mkdir -p $cache/$d $(dirname $wiki/$d)
pankso@5 329 # May be a new page.
pankso@5 330 if [ ! -f "$wiki/$d.txt" ]; then
pankso@5 331 new=0
pankso@5 332 touch $wiki/$d.txt
pankso@5 333 fi
pankso@5 334 cp $wiki/$d.txt $cache/$d/last.bak
pankso@5 335 sed "s/$(echo -en '\r') /\n/g" > $wiki/$d.txt << EOT
pankso@5 336 $(GET content)
pankso@5 337 EOT
pankso@5 338 diff $cache/$d/last.bak $wiki/$d.txt > $cache/$d/last.diff
pankso@5 339 # Log
pankso@5 340 if [ "$new" ]; then
pankso@5 341 echo "Page created by: $(link_user)" | log
pankso@31 342 echo "New document: <a href='$script?d=$d'>$d</a>" | log_activity
pankso@5 343 if [ "$HG" == "yes" ]; then
pankso@5 344 cd $content && hg -q add
pankso@5 345 hg commit -q -u "$NAME <$MAIL>" -m "Created new document: $d"
pankso@5 346 cd $tiny
pankso@5 347 fi
pankso@5 348 else
pankso@28 349 # Here we may clean log: cat && tail -n 40
pankso@5 350 echo "Page edited by: $(link_user)" | log
pankso@5 351 if [ "$HG" == "yes" ]; then
pankso@5 352 cd $content && hg commit -q -u "$NAME <$MAIL>" \
pankso@5 353 -m "Edited document: $d"
pankso@5 354 cd $tiny
pankso@5 355 fi
pankso@5 356 fi
pankso@5 357 }
pankso@5 358
pankso@23 359 # Save a user profile.
pankso@23 360 save_profile() {
pankso@23 361 path="$PEOPLE/$user"
pankso@23 362 cp -f ${path}/${d}.txt ${path}/${d}.bak
pankso@23 363 sed "s/$(echo -en '\r') /\n/g" > ${path}/${d}.txt << EOT
pankso@23 364 $(GET content)
pankso@23 365 EOT
pankso@23 366 }
pankso@23 367
pankso@52 368 # CM tools (edit, diff, etc) for auth users
pankso@5 369 wiki_tools() {
pankso@52 370 if check_auth; then
pankso@52 371 cat << EOT
pankso@5 372 <div id="tools">
pankso@5 373 <a href="$script?edit=$d">$(gettext "Edit document")</a>
pankso@59 374 <a href="$script?log=$d">$(gettext "File log")</a>
pankso@5 375 <a href="$script?diff=$d">$(gettext "Last diff")</a>
pankso@59 376 $PLUGINS_TOOLS
pankso@52 377 EOT
pankso@52 378 [ "$HG" == "yes" ] && echo "<a href='$script?hg'>Hg Log</a>"
pankso@52 379 echo "</div>"
pankso@52 380 fi
pankso@52 381 }
pankso@52 382
pankso@59 383 # Built-in tools such as log/ls and PLUGINS_TOOLS
pankso@59 384 tiny_tools() {
pankso@52 385 if check_auth; then
pankso@52 386 cat << EOT
pankso@52 387 <div id='tools'>
pankso@52 388 <a href='$script?log'>Activity log</a>
pankso@52 389 <a href='$script?ls'>Pages list</a>
pankso@59 390 $PLUGINS_TOOLS
pankso@5 391 </div>
pankso@5 392 EOT
pankso@52 393 fi
pankso@5 394 }
pankso@5 395
pankso@5 396 # Get and display Gravatar image: get_gravatar email size
pankso@5 397 # Link to profile: <a href="http://www.gravatar.com/$md5">...</a>
pankso@5 398 get_gravatar() {
pankso@5 399 email=$1
pankso@5 400 size=$2
pankso@5 401 [ "$size" ] || size=48
pankso@5 402 url="http://www.gravatar.com/avatar"
pankso@5 403 md5=$(md5crypt $email)
pankso@5 404 echo "<img src='$url/$md5?d=identicon&s=$size' alt='&lowast;' />"
pankso@5 405 }
pankso@5 406
pankso@5 407 # List hg logs
pankso@5 408 hg_log() {
pankso@5 409 cd $content
pankso@5 410 cat << EOT
pankso@5 411 <table>
pankso@5 412 <thead>
pankso@5 413 <td>$(gettext "User")</td>
pankso@5 414 <td>$(gettext "Description")</td>
pankso@5 415 <td>$(gettext "Revision")</td>
pankso@5 416 </thead>
pankso@5 417 EOT
pankso@5 418 hg log --template "<tr><td>{author}</td><td>{desc}</td><td>{rev}</td></tr>\n"
pankso@5 419 echo '</table>'
pankso@5 420 }
pankso@5 421
pankso@5 422 #
pankso@5 423 # POST actions
pankso@5 424 #
pankso@5 425
pankso@5 426 case " $(POST) " in
pankso@5 427 *\ auth\ *)
pankso@5 428 # Authenticate user. Create a session file in $sessions to be used
pankso@5 429 # by check_auth. We have the user login name and a peer session
pankso@5 430 # md5 string in the COOKIE.
pankso@5 431 user="$(POST auth)"
pankso@5 432 pass="$(md5crypt "$(POST pass)")"
pankso@5 433 valid=$(fgrep "${user}:" $AUTH_FILE | cut -d ":" -f 2)
pankso@5 434 if [ "$pass" == "$valid" ] && [ "$pass" != "" ]; then
pankso@5 435 md5session=$(echo -n "$$:$user:$pass:$$" | md5sum | awk '{print $1}')
pankso@5 436 [ -d $sessions ] || mkdir -p $sessions
pankso@36 437 date '+%Y-%m-%d' > ${PEOPLE}/${user}/last
pankso@5 438 echo "$md5session" > $sessions/$user
pankso@5 439 header "Location: $script" \
pankso@5 440 "Set-Cookie: auth=$user:$md5session; HttpOnly"
pankso@5 441 else
pankso@5 442 header "Location: $script?login&error"
pankso@5 443 fi ;;
pankso@5 444 *\ signup\ *)
pankso@5 445 # POST action for signup
pankso@5 446 name="$(POST name)"
pankso@5 447 user="$(POST user)"
pankso@5 448 mail="$(POST mail)"
pankso@5 449 pass="$(md5crypt "$(POST pass)")"
pankso@5 450 if ! grep "^${user}:" $AUTH_FILE; then
pankso@5 451 new_user_config
pankso@5 452 header "Location: $script?login"
pankso@5 453 else
pankso@5 454 header
pankso@5 455 html_header
pankso@5 456 user_box
pankso@44 457 echo "<h2>$(gettext 'User already exists:') $user</h2>"
pankso@5 458 html_footer
pankso@5 459 fi ;;
pankso@5 460 esac
pankso@5 461
pankso@5 462 #
pankso@5 463 # Plugins
pankso@5 464 #
pankso@5 465 for p in $(ls -1 $plugins)
pankso@5 466 do
pankso@5 467 [ -f "$plugins/$p/$p.conf" ] && . $plugins/$p/$p.conf
pankso@5 468 [ -x "$plugins/$p/$p.cgi" ] && . $plugins/$p/$p.cgi
pankso@5 469 done
pankso@5 470
pankso@5 471 #
pankso@5 472 # GET actions
pankso@5 473 #
pankso@5 474
pankso@5 475 case " $(GET) " in
pankso@5 476 *\ edit\ *)
pankso@5 477 d="$(GET edit)"
pankso@5 478 header
pankso@5 479 html_header
pankso@5 480 user_box
pankso@5 481 get_lang
pankso@59 482 wiki_tools
pankso@5 483 if check_auth; then
pankso@23 484 if [ "$doc" == "profile" ]; then
pankso@23 485 wiki="$PEOPLE/$user"
pankso@23 486 fi
pankso@5 487 cat << EOT
pankso@5 488 <h2>$(gettext "Edit $doc [ $i18n ]")</h2>
pankso@5 489
pankso@5 490 <div id="edit">
pankso@5 491
pankso@5 492 <form method="get" action="$script" name="editor">
pankso@5 493 <input type="hidden" name="save" value="$d" />
pankso@5 494 <textarea name="content">$(cat "$wiki/$d.txt")</textarea>
pankso@5 495 <input type="submit" value="$(gettext "Save document")" />
pankso@5 496 $(gettext "Code Helper:")
pankso@5 497 $(cat lib/jseditor.html)
pankso@5 498 </form>
pankso@5 499
pankso@5 500 </div>
pankso@5 501 EOT
pankso@5 502 else
paul@8 503 gettext "You must be logged in to edit pages"
pankso@5 504 fi
pankso@5 505 html_footer ;;
pankso@15 506
pankso@5 507 *\ save\ *)
pankso@5 508 d="$(GET save)"
pankso@5 509 if check_auth; then
pankso@23 510 # User profile
pankso@23 511 if [ "$d" == "profile" ]; then
pankso@23 512 save_profile
pankso@23 513 header "Location: $script?user=$user"
pankso@23 514 else
pankso@23 515 save_document
pankso@23 516 fi
pankso@5 517 fi
pankso@5 518 header "Location: $script?d=$d" ;;
pankso@15 519
pankso@5 520 *\ log\ *)
pankso@5 521 d="$(GET log)"
pankso@5 522 header
pankso@5 523 html_header
pankso@5 524 user_box
pankso@28 525 # Main activity
pankso@28 526 if [ "$d" == "log" ]; then
pankso@59 527 tiny_tools
pankso@52 528 echo "<h2>$(gettext "Activity log")</h2>"
pankso@28 529 echo '<pre>'
pankso@28 530 if [ -f "$cache/log/activity.log" ]; then
pankso@28 531 IFS="|"
pankso@28 532 tac $cache/log/activity.log | while read USER DATE LOG
pankso@28 533 do
pankso@28 534 . ${PEOPLE}/${USER}/account.conf
pankso@28 535 cat << EOT
pankso@28 536 <a href='$script?user=$USER'>$(get_gravatar $MAIL 24)</a>\
pankso@28 537 <span class='date'>$DATE -</span> $LOG
pankso@28 538 EOT
pankso@28 539 done
pankso@28 540 unset IFS
pankso@28 541 else
pankso@28 542 gettext "No activity log yet"; echo
pankso@28 543 fi
pankso@28 544 echo '</pre>'
pankso@28 545 html_footer && exit 0
pankso@28 546 fi
pankso@52 547 # Document activity
pankso@5 548 get_lang
pankso@52 549 wiki_tools
pankso@5 550 echo "<h2>$(gettext "Activity for:") <a href='$script?d=$d'>$d</a></h2>"
pankso@5 551 echo '<pre>'
pankso@5 552 if [ -f "$cache/$d/activity.log" ]; then
pankso@5 553 tac $cache/$d/activity.log
pankso@5 554 else
pankso@5 555 gettext "No log for: $d"; echo
pankso@5 556 fi
pankso@5 557 echo '</pre>'
pankso@5 558 html_footer ;;
pankso@28 559
pankso@28 560 *\ ls\ *)
pankso@28 561 d="Document list"
pankso@28 562 header
pankso@28 563 html_header
pankso@28 564 user_box
pankso@59 565 tiny_tools
pankso@59 566 [ ! check_auth ] && auth=0
pankso@48 567 echo "<h2>$(gettext "Pages list")</h2>"
pankso@28 568 echo '<pre>'
pankso@28 569 cd ${wiki}
pankso@28 570 for d in $(find . -type f | sed s'/.\///')
pankso@28 571 do
pankso@59 572 echo "<a href='$script?d=${d%.txt}'>${d%.txt}</a>"
pankso@59 573 [ "$auth" ] && cat << EOT
pankso@59 574 : <a href="$script?rm=$d">$(gettext "Remove")</a> || \
pankso@52 575 <a href="$script?edit=$d">$(gettext "Edit")</a>
pankso@48 576 EOT
pankso@59 577 done && unset auth
pankso@28 578 echo '</pre>'
pankso@28 579 html_footer ;;
pankso@48 580
pankso@48 581 *\ rm\ *)
pankso@59 582 [ ! check_auth ] && exit 1
pankso@48 583 d="$(GET rm)"
pankso@48 584 rm ${wiki}/"${d}"
pankso@48 585 rm -rf ${cache}/"${d%.txt}"
pankso@48 586 header "Location: $script?ls" ;;
pankso@15 587
pankso@5 588 *\ diff\ *)
pankso@5 589 d="$(GET diff)"
pankso@5 590 date="last"
pankso@5 591 header
pankso@5 592 html_header
pankso@5 593 user_box
pankso@5 594 get_lang
pankso@52 595 wiki_tools
pankso@5 596 echo "<h2>$(gettext "Diff for:") <a href='$script?d=$d'>$d</a></h2>"
pankso@5 597 echo '<pre>'
pankso@5 598 if [ -f "$cache/$d/$date.diff" ]; then
pankso@5 599 cat $cache/$d/$date.diff | sed \
pankso@5 600 -e 's|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' \
pankso@5 601 -e s"#^-\([^']*\).#<span style='color: red;'>\0</span>#"g \
pankso@5 602 -e s"#^+\([^']*\).#<span style='color: green;'>\0</span>#"g \
pankso@5 603 -e s"#@@\([^']*\)@@#<span style='color: blue;'>@@\1@@</span>#"g
pankso@5 604 else
pankso@59 605 gettext "No diff for:"; echo " $d"
pankso@5 606 fi
pankso@5 607 echo '</pre>'
pankso@5 608 html_footer ;;
pankso@15 609
pankso@5 610 *\ login\ *)
pankso@5 611 # The login page
pankso@5 612 d="Login"
pankso@5 613 [ "$(GET error)" ] && \
pankso@5 614 error="<p class="error">$(gettext "Bad login or pass")</p>"
pankso@5 615 header
pankso@5 616 html_header
pankso@5 617 user_box
pankso@5 618 login_page
pankso@5 619 html_footer ;;
pankso@15 620
pankso@5 621 *\ signup\ *)
pankso@5 622 # The login page
pankso@5 623 d="$(gettext "Sign Up")"
pankso@5 624 header
pankso@5 625 html_header
pankso@5 626 user_box
pankso@5 627 echo "<h2>$d</h2>"
pankso@5 628 if [ "$ONLINE_SIGNUP" == "yes" ]; then
pankso@5 629 signup_page
pankso@5 630 else
paul@8 631 gettext "Online registration is disabled"
pankso@5 632 fi
pankso@5 633 html_footer ;;
pankso@15 634
pankso@5 635 *\ logout\ *)
pankso@5 636 # Set a Cookie in the past to logout.
pankso@5 637 expires="Expires=Wed, 01-Jan-1980 00:00:00 GMT"
pankso@5 638 if check_auth; then
pankso@5 639 rm -f "$sessions/$user"
pankso@5 640 header "Location: $script" "Set-Cookie: auth=none; $expires; HttpOnly"
pankso@5 641 fi ;;
pankso@15 642
pankso@5 643 *\ user\ *)
pankso@5 644 # User profile
pankso@15 645 d="$(GET user)"
pankso@36 646 last="$(cat $PEOPLE/"$(GET user)"/last)"
pankso@5 647 header
pankso@5 648 html_header
pankso@5 649 user_box
pankso@5 650 . $PEOPLE/"$(GET user)"/account.conf
pankso@36 651 cat << EOT
pankso@36 652 <h2>$(get_gravatar $MAIL) $NAME</h2>
pankso@36 653
pankso@36 654 <pre>
pankso@36 655 $(gettext "User name :") $USER
pankso@36 656 $(gettext "Last login :") $last
pankso@36 657 EOT
pankso@5 658 if check_auth && [ "$(GET user)" == "$user" ]; then
pankso@5 659 auth_people
pankso@5 660 else
pankso@28 661 # check_auth will set VARS to current logged user: re-source
pankso@28 662 . $PEOPLE/"$(GET user)"/account.conf
pankso@5 663 public_people
pankso@5 664 fi
pankso@5 665 html_footer ;;
pankso@15 666
pankso@59 667 *\ hg\ *)
pankso@59 668 d="Hg Log"
pankso@5 669 header
pankso@5 670 html_header
pankso@5 671 user_box
pankso@5 672 [ "$HG" != "yes" ] && gettext "Hg is disabled" && exit 0
pankso@5 673 [ ! -x /usr/bin/hg ] && gettext "Hg is not installed" && exit 0
pankso@5 674 echo "<h2>$d</h2>"
pankso@5 675 case " $(GET hg) " in
pankso@5 676 *\ init\ *)
pankso@5 677 if check_auth; then
pankso@5 678 [ -d "$content/.hg" ] && exit 0
pankso@5 679 echo '<pre>'
pankso@5 680 gettext "Executing: hg init"; echo
pankso@5 681 cd $content/ && hg init
pankso@5 682 echo '[hooks]' > .hg/hgrc
pankso@5 683 echo 'incoming = hg update' >> .hg/hgrc
paul@8 684 gettext "Adding current content and committing"; echo
pankso@59 685 [ ! -f "$wiki/index.txt" ] && default_index
pankso@5 686 hg add && hg commit -u "$NAME <$MAIL>" \
paul@22 687 -m "Initial commit with current content"
pankso@5 688 echo '</pre>' && cd ..
pankso@5 689 fi ;;
pankso@5 690 esac
pankso@5 691 hg_log
pankso@5 692 html_footer ;;
pankso@15 693
pankso@5 694 *)
pankso@5 695 # Display requested page
pankso@5 696 d="$(GET d)"
pankso@5 697 [ "$d" ] || d=$index
pankso@5 698 header
pankso@5 699 html_header
pankso@5 700 user_box
pankso@5 701 get_lang
pankso@52 702
pankso@34 703 # Generate a default index on first run
pankso@11 704 if [ ! -f "$wiki/$index.txt" ]; then
pankso@34 705 if ! default_index; then
pankso@37 706 echo "<pre class='error'>Directory : content/ is not writable</pre>"
pankso@32 707 html_footer && exit 0
pankso@32 708 fi
pankso@11 709 fi
pankso@52 710
pankso@34 711 # Check cache dir
pankso@34 712 if [ ! -w "$cache" ]; then
pankso@34 713 echo "<pre class='error'>Directory : cache/ is not writable"
pankso@34 714 echo "Command : install -m 0777 -d $tiny/cache</pre>"
pankso@34 715 html_footer && exit 0
pankso@34 716 fi
pankso@52 717
pankso@59 718 # Hg warning if enabled but not initiated
pankso@52 719 if [ "$HG" == "yes" ] && [ ! -d "$content/.hg" ]; then
pankso@52 720 echo '<p class="error box">'
pankso@52 721 gettext "Mercurial is enabled but no repository found"
pankso@59 722 echo ": <a href='$script?hg=init'>Hg init</a>"
pankso@52 723 echo '</p>'
pankso@52 724 fi
pankso@52 725
pankso@52 726 # Wiki tools
pankso@52 727 wiki_tools
pankso@52 728
pankso@34 729 # Wiki document
pankso@5 730 if [ ! -f "$wiki/$d.txt" ]; then
pankso@5 731 echo "<h2>$d</h2>"
pankso@5 732 gettext "The document does not exist. You can create it or read the"
pankso@59 733 echo " <a href='$script?d=en/help'>help</a>"
pankso@5 734 else
pankso@26 735 if fgrep -q [NOWIKI] $wiki/$d.txt; then
pankso@35 736 cat $wiki/$d.txt | sed '/\[NOWIKI\]/'d
pankso@5 737 else
pankso@5 738 cat $wiki/$d.txt | wiki_parser
pankso@5 739 fi
pankso@5 740 fi
pankso@5 741 html_footer ;;
pankso@5 742 esac
pankso@5 743
pankso@5 744 exit 0