tinycm annotate index.cgi @ rev 65

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